From 6d65668355dd2b5e8bf385065677595c4e987e68 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sat, 13 Aug 2016 22:40:01 +0200 Subject: [PATCH] Strip request header values --- extras/get-homebrew-formula-vars.py | 55 +++++++++++++++++++++++++++++ httpie/client.py | 26 +++++++++----- 2 files changed, 73 insertions(+), 8 deletions(-) create mode 100755 extras/get-homebrew-formula-vars.py diff --git a/extras/get-homebrew-formula-vars.py b/extras/get-homebrew-formula-vars.py new file mode 100755 index 00000000..c2c80dc2 --- /dev/null +++ b/extras/get-homebrew-formula-vars.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +""" +Generate URLs and file hashes to be included in the Homebrew formula +after a new release of HTTPie is published on PyPi. + +https://github.com/Homebrew/homebrew-core/blob/master/Formula/httpie.rb + +""" +import hashlib +import requests + + +PACKAGES = [ + 'httpie', + 'requests', + 'pygments', +] + + +def get_info(package_name): + api_url = 'https://pypi.python.org/pypi/{}/json'.format(package_name) + resp = requests.get(api_url).json() + hasher = hashlib.sha256() + for release in resp['urls']: + download_url = release['url'] + if download_url.endswith('.tar.gz'): + hasher.update(requests.get(download_url).content) + return { + 'name': package_name, + 'url': download_url, + 'sha256': hasher.hexdigest(), + } + else: + raise RuntimeError( + '{}: download not found: {}'.format(package_name, resp)) + + +packages = { + package_name: get_info(package_name) for package_name in PACKAGES +} + + +httpie_info = packages.pop('httpie') +print(""" + url "{url}" + sha256 "{sha256}" +""".format(**httpie_info)) + + +for package_info in packages.values(): + print(""" + resource "{name}" do + url "{url}" + sha256 "{sha256}" + end""".format(**package_info)) diff --git a/httpie/client.py b/httpie/client.py index 054783f9..fb0fc62c 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -85,13 +85,23 @@ def dump_request(kwargs): % repr_dict_nice(kwargs)) -def encode_headers(headers): - # This allows for unicode headers which is non-standard but practical. - # See: https://github.com/jkbrzt/httpie/issues/212 - return dict( - (name, value.encode('utf8') if isinstance(value, str) else value) - for name, value in headers.items() - ) +def finalize_headers(headers): + final_headers = {} + for name, value in headers.items(): + if value is not None: + + # >leading or trailing LWS MAY be removed without + # >changing the semantics of the field value" + # -https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html + # Also, requests raises `InvalidHeader` for leading spaces. + value = value.strip() + + if isinstance(value, str): + # See: https://github.com/jkbrzt/httpie/issues/212 + value = value.encode('utf8') + + final_headers[name] = value + return final_headers def get_default_headers(args): @@ -133,7 +143,7 @@ def get_requests_kwargs(args, base_headers=None): if base_headers: headers.update(base_headers) headers.update(args.headers) - headers = encode_headers(headers) + headers = finalize_headers(headers) credentials = None if args.auth: