Deduplicate setting Accept-Language HTTP header.

This adds a new function conf.defaultHttpHeaders that can be used by plugins
to get all the default HTTP headers for a given network/channel.
This commit is contained in:
Valentin Lorentz 2020-01-14 19:03:12 +01:00
parent ae5ad2ceab
commit c457b52067
3 changed files with 22 additions and 15 deletions

View File

@ -149,14 +149,7 @@ class Web(callbacks.PluginRegexp):
def getTitle(self, irc, url, raiseErrors, msg):
size = conf.supybot.protocols.http.peekSize()
timeout = self.registryValue('timeout')
headers = dict(utils.web.defaultHeaders)
language = conf.supybot.protocols.http.requestLanguage.getSpecific(
irc.network, msg.channel)()
lkey = 'Accept-Language'
if language:
headers[lkey] = language
elif lkey in headers:
del headers[lkey]
headers = conf.defaultHttpHeaders(irc.network, msg.channel)
try:
(target, text) = utils.web.getUrlTargetAndContent(url, size=size,
timeout=timeout, headers=headers)

View File

@ -1299,16 +1299,26 @@ registerGlobalValue(supybot.protocols.http, 'proxy',
through. The value should be of the form 'host:port'.""")))
utils.web.proxy = supybot.protocols.http.proxy
def defaultHttpHeaders(network, channel):
"""Returns the default HTTP headers to use for this channel/network."""
headers = utils.web.baseDefaultHeaders.copy()
try:
language = supybot.protocols.http.requestLanguage.getSpecific(
network, channel)()
except registry.NonExistentRegistryEntry:
pass # Starting up; language will be set by HttpRequestLanguage later
else:
if language:
headers['Accept-Language'] = language
elif 'Accept-Language' in headers:
del headers['Accept-Language']
return headers
class HttpRequestLanguage(registry.String):
"""Must be a valid HTTP Accept-Language value."""
__slots__ = ()
def setValue(self, v):
headers = utils.web.defaultHeaders
lkey = 'Accept-Language'
if v:
headers[lkey] = v
elif lkey in headers:
del headers[lkey]
utils.web.defaultHeaders = defaultHttpHeaders(None, None)
super(HttpRequestLanguage, self).setValue(v)
registerChannelValue(supybot.protocols.http, 'requestLanguage',

View File

@ -115,10 +115,14 @@ def strError(e):
else:
return str(e)
defaultHeaders = {
# Used to build defaultHeaders
baseDefaultHeaders = {
'User-agent': 'Mozilla/5.0 (compatible; utils.web python module)'
}
# overridable by other modules/plugins.
defaultHeaders = baseDefaultHeaders.copy()
# Other modules should feel free to replace this with an appropriate
# application-specific function. Feel free to use a callable here.
proxy = None