mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-27 21:22:39 +01:00
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:
parent
ae5ad2ceab
commit
c457b52067
@ -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)
|
||||
|
22
src/conf.py
22
src/conf.py
@ -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',
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user