diff options
| author | rr- | 2017-03-03 17:24:58 +0100 |
|---|---|---|
| committer | rr- | 2017-03-03 17:27:23 +0100 |
| commit | 5681fd11efe93b5b2a1719f16c9f253ffd508842 (patch) | |
| tree | 4fbab2966a607b1ed69ec4e22d7cb3740c410596 | |
| parent | e087b83082703c54cad919c961a95cadc8e19cfa (diff) | |
server/net: make the user-agent configurable
Fixes #127
| -rw-r--r-- | config.yaml.dist | 1 | ||||
| -rw-r--r-- | server/szurubooru/func/net.py | 3 | ||||
| -rw-r--r-- | server/szurubooru/tests/func/test_net.py | 5 |
3 files changed, 8 insertions, 1 deletions
diff --git a/config.yaml.dist b/config.yaml.dist index 3b00d0e..d1dc97c 100644 --- a/config.yaml.dist +++ b/config.yaml.dist @@ -10,6 +10,7 @@ api_url: # where frontend connects to, example: http://api.example.com/ base_url: # used to form links to frontend, example: http://example.com/ data_url: # used to form links to posts and avatars, example: http://example.com/data/ data_dir: # absolute path for posts and avatars storage, example: /srv/www/booru/client/public/data/ +user_agent: # user agent name used to download files from the web on behalf of the api users # usage: schema://user:password@host:port/database_name diff --git a/server/szurubooru/func/net.py b/server/szurubooru/func/net.py index a6e1821..e6326c0 100644 --- a/server/szurubooru/func/net.py +++ b/server/szurubooru/func/net.py @@ -1,10 +1,13 @@ import urllib.request +from szurubooru import config from szurubooru import errors def download(url: str) -> bytes: assert url request = urllib.request.Request(url) + if config.config['user_agent']: + request.add_header('User-Agent', config.config['user_agent']) request.add_header('Referer', url) try: with urllib.request.urlopen(request) as handle: diff --git a/server/szurubooru/tests/func/test_net.py b/server/szurubooru/tests/func/test_net.py index f749d38..fb149b0 100644 --- a/server/szurubooru/tests/func/test_net.py +++ b/server/szurubooru/tests/func/test_net.py @@ -1,7 +1,10 @@ from szurubooru.func import net -def test_download(): +def test_download(config_injector): + config_injector({ + 'user_agent': None + }) url = 'http://info.cern.ch/hypertext/WWW/TheProject.html' expected_content = ( |