server/net: make the user-agent configurable

Fixes #127
This commit is contained in:
rr- 2017-03-03 17:24:58 +01:00
parent e087b83082
commit 5681fd11ef
3 changed files with 8 additions and 1 deletions

View File

@ -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/ 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_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/ 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 # usage: schema://user:password@host:port/database_name

View File

@ -1,10 +1,13 @@
import urllib.request import urllib.request
from szurubooru import config
from szurubooru import errors from szurubooru import errors
def download(url: str) -> bytes: def download(url: str) -> bytes:
assert url assert url
request = urllib.request.Request(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) request.add_header('Referer', url)
try: try:
with urllib.request.urlopen(request) as handle: with urllib.request.urlopen(request) as handle:

View File

@ -1,7 +1,10 @@
from szurubooru.func import net 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' url = 'http://info.cern.ch/hypertext/WWW/TheProject.html'
expected_content = ( expected_content = (