Skip to content

Commit

Permalink
Adds a User-agent header to curl module requests
Browse files Browse the repository at this point in the history
The header will be of the form:

    peru/{version} Python-urllib/{version}

Fixes buildinspace#218
  • Loading branch information
colindean committed Nov 30, 2021
1 parent e9ba6e0 commit d30c8ca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 18 additions & 2 deletions peru/resources/plugins/curl/curl_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,26 @@
import tarfile
from urllib.error import HTTPError, URLError
from urllib.parse import urlsplit
from urllib.request import Request
import peru.main
import urllib.request
import zipfile


def peru_version():
return peru.main.get_version()


def build_request(url):
request = Request(url)
components = [
f"peru/{peru_version()}",
urllib.request.URLopener.version
]
request.add_header("User-agent", " ".join(components))
return request


def get_request_filename(request):
'''Figure out the filename for an HTTP download.'''
# Check to see if a filename is specified in the HTTP headers.
Expand Down Expand Up @@ -80,7 +96,7 @@ def plugin_sync(url, sha1):
# Download directly to the destination dir.
download_dir = dest

with urllib.request.urlopen(url) as request:
with urllib.request.urlopen(build_request(url)) as request:
filename = os.environ['PERU_MODULE_FILENAME']
if not filename:
filename = get_request_filename(request)
Expand Down Expand Up @@ -151,7 +167,7 @@ def __init__(self, message):

def plugin_reup(url, sha1):
reup_output = os.environ['PERU_REUP_OUTPUT']
with urllib.request.urlopen(url) as request:
with urllib.request.urlopen(build_request(url)) as request:
digest = download_file(request, None)
with open(reup_output, 'w') as output_file:
print('sha1:', digest, file=output_file)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_curl_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import importlib.machinery
import io
from os.path import abspath, join, dirname
import urllib

import peru
import shared
Expand Down Expand Up @@ -100,3 +101,14 @@ def test_evil_archives(self):
tar_archive = shared.test_resources / (case + '.tar')
with self.assertRaises(curl_plugin.EvilArchiveError):
curl_plugin.extract_tar(str(tar_archive), dest)

def test_request_has_user_agent_header(self):
actual = curl_plugin.build_request("http://example.test")
print(actual.header_items())
self.assertTrue(actual.has_header("User-agent"))
ua_header = actual.get_header("User-agent")
peru_component, urllib_component = ua_header.split(' ')
_, peru_version = peru_component.split('/')
_, urllib_version = urllib_component.split('/')
self.assertEqual(peru.main.get_version(), peru_version)
self.assertEqual(urllib.request.__version__, urllib_version)

0 comments on commit d30c8ca

Please sign in to comment.