mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-10-03 01:48:52 +02: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):
|
def getTitle(self, irc, url, raiseErrors, msg):
|
||||||
size = conf.supybot.protocols.http.peekSize()
|
size = conf.supybot.protocols.http.peekSize()
|
||||||
timeout = self.registryValue('timeout')
|
timeout = self.registryValue('timeout')
|
||||||
headers = dict(utils.web.defaultHeaders)
|
headers = conf.defaultHttpHeaders(irc.network, msg.channel)
|
||||||
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]
|
|
||||||
try:
|
try:
|
||||||
(target, text) = utils.web.getUrlTargetAndContent(url, size=size,
|
(target, text) = utils.web.getUrlTargetAndContent(url, size=size,
|
||||||
timeout=timeout, headers=headers)
|
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'.""")))
|
through. The value should be of the form 'host:port'.""")))
|
||||||
utils.web.proxy = supybot.protocols.http.proxy
|
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):
|
class HttpRequestLanguage(registry.String):
|
||||||
"""Must be a valid HTTP Accept-Language value."""
|
"""Must be a valid HTTP Accept-Language value."""
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
headers = utils.web.defaultHeaders
|
utils.web.defaultHeaders = defaultHttpHeaders(None, None)
|
||||||
lkey = 'Accept-Language'
|
|
||||||
if v:
|
|
||||||
headers[lkey] = v
|
|
||||||
elif lkey in headers:
|
|
||||||
del headers[lkey]
|
|
||||||
super(HttpRequestLanguage, self).setValue(v)
|
super(HttpRequestLanguage, self).setValue(v)
|
||||||
|
|
||||||
registerChannelValue(supybot.protocols.http, 'requestLanguage',
|
registerChannelValue(supybot.protocols.http, 'requestLanguage',
|
||||||
|
@ -115,10 +115,14 @@ def strError(e):
|
|||||||
else:
|
else:
|
||||||
return str(e)
|
return str(e)
|
||||||
|
|
||||||
defaultHeaders = {
|
# Used to build defaultHeaders
|
||||||
|
baseDefaultHeaders = {
|
||||||
'User-agent': 'Mozilla/5.0 (compatible; utils.web python module)'
|
'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
|
# Other modules should feel free to replace this with an appropriate
|
||||||
# application-specific function. Feel free to use a callable here.
|
# application-specific function. Feel free to use a callable here.
|
||||||
proxy = None
|
proxy = None
|
||||||
|
Loading…
Reference in New Issue
Block a user