From 5681fd11efe93b5b2a1719f16c9f253ffd508842 Mon Sep 17 00:00:00 2001 From: rr- Date: Fri, 3 Mar 2017 17:24:58 +0100 Subject: [PATCH] server/net: make the user-agent configurable Fixes #127 --- config.yaml.dist | 1 + server/szurubooru/func/net.py | 3 +++ server/szurubooru/tests/func/test_net.py | 5 ++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/config.yaml.dist b/config.yaml.dist index 3b00d0eb..d1dc97c5 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 a6e18214..e6326c06 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 f749d384..fb149b05 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 = (