mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-12 13:12:35 +01:00
Selectable User-Agent for HTTP requests (#1423)
https://github.com/ProgVal/Limnoria/issues/1382
This commit is contained in:
parent
e5a04aaf13
commit
73de5616e8
18
src/conf.py
18
src/conf.py
@ -32,6 +32,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import socket
|
import socket
|
||||||
|
import random
|
||||||
|
|
||||||
from . import ircutils, registry, utils
|
from . import ircutils, registry, utils
|
||||||
from .utils import minisix
|
from .utils import minisix
|
||||||
@ -1330,13 +1331,17 @@ def defaultHttpHeaders(network, channel):
|
|||||||
try:
|
try:
|
||||||
language = supybot.protocols.http.requestLanguage.getSpecific(
|
language = supybot.protocols.http.requestLanguage.getSpecific(
|
||||||
network, channel)()
|
network, channel)()
|
||||||
|
agent = random.choice(supybot.protocols.http.userAgents.getSpecific(
|
||||||
|
network, channel)())
|
||||||
except registry.NonExistentRegistryEntry:
|
except registry.NonExistentRegistryEntry:
|
||||||
pass # Starting up; language will be set by HttpRequestLanguage later
|
pass # Starting up; headers will be set by HttpRequestLanguage/UserAgents later
|
||||||
else:
|
else:
|
||||||
if language:
|
if language:
|
||||||
headers['Accept-Language'] = language
|
headers['Accept-Language'] = language
|
||||||
elif 'Accept-Language' in headers:
|
elif 'Accept-Language' in headers:
|
||||||
del headers['Accept-Language']
|
del headers['Accept-Language']
|
||||||
|
if agent.strip():
|
||||||
|
headers['User-agent'] = agent
|
||||||
return headers
|
return headers
|
||||||
|
|
||||||
class HttpRequestLanguage(registry.String):
|
class HttpRequestLanguage(registry.String):
|
||||||
@ -1346,12 +1351,23 @@ class HttpRequestLanguage(registry.String):
|
|||||||
super(HttpRequestLanguage, self).setValue(v)
|
super(HttpRequestLanguage, self).setValue(v)
|
||||||
utils.web.defaultHeaders = defaultHttpHeaders(None, None)
|
utils.web.defaultHeaders = defaultHttpHeaders(None, None)
|
||||||
|
|
||||||
|
class HttpUserAgents(registry.CommaSeparatedListOfStrings):
|
||||||
|
"""Must be a valid HTTP User-Agent value."""
|
||||||
|
__slots__ = ()
|
||||||
|
def setValue(self, v):
|
||||||
|
super(HttpUserAgent, self).setValue(v)
|
||||||
|
utils.web.defaultHeaders = defaultHttpHeaders(None, None)
|
||||||
|
|
||||||
registerChannelValue(supybot.protocols.http, 'requestLanguage',
|
registerChannelValue(supybot.protocols.http, 'requestLanguage',
|
||||||
HttpRequestLanguage('', _("""If set, the Accept-Language HTTP header will be set to this
|
HttpRequestLanguage('', _("""If set, the Accept-Language HTTP header will be set to this
|
||||||
value for requests. Useful for overriding the auto-detected language based on
|
value for requests. Useful for overriding the auto-detected language based on
|
||||||
the server's location.""")))
|
the server's location.""")))
|
||||||
|
|
||||||
|
|
||||||
|
registerChannelValue(supybot.protocols.http, 'userAgents',
|
||||||
|
HttpUserAgents([], _("""If set, the User-Agent HTTP header will be set to a randomly
|
||||||
|
selected value from this comma-separated list of strings for requests.""")))
|
||||||
|
|
||||||
###
|
###
|
||||||
# supybot.protocols.ssl
|
# supybot.protocols.ssl
|
||||||
###
|
###
|
||||||
|
Loading…
Reference in New Issue
Block a user