Skip to content

Commit

Permalink
Merge pull request #5 from martynp/fix_encoding
Browse files Browse the repository at this point in the history
Ensure string encoding is consistent when creating RequestData elements
  • Loading branch information
martynp authored Jul 9, 2023
2 parents 707702c + adc2f55 commit 8e4a64a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion httpie_hmac/httpie_hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import hashlib
import hmac
import importlib.machinery
import io
import os
import requests
import types
Expand Down Expand Up @@ -143,18 +144,30 @@ def __call__(self, r):
content_type = r.headers.get('content-type')
if not content_type:
content_type = ''
else:
content_type = content_type.decode('utf-8')

# If content-md5 is already given, use it, otherwise calculate
# it ourselves and add it to the headers
content_md5 = r.headers.get('content-md5')
if not content_md5:
if content_type and r.body:
m = hashlib.md5()
body = r.body
# If we have a buffer, convert it in to a string
if type(r.body) == io.BufferedReader:
body = body.read()
r.body = body
m.update(r.body)
content_md5 = base64.b64encode(m.digest()).rstrip()
content_md5 = base64 \
.b64encode(m.digest()) \
.rstrip() \
.decode('utf-8')
r.headers['Content-MD5'] = content_md5
else:
content_md5 = ''
else:
content_md5 = content_md5.decode('utf-8')

# If date is given already, use it - otherwise generate it
# ourselves and add it to the headers
Expand All @@ -163,6 +176,8 @@ def __call__(self, r):
now = datetime.datetime.utcnow()
http_date = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
r.headers['Date'] = http_date
else:
http_date = http_date.decode('utf-8')

# Get the path from the UL
url = urlparse(r.url)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "httpie-hmac"
version = "1.1.1"
version = "1.2.0"
authors = [
{name = "Martyn Pittuck-Schols", email = "[email protected]"},
]
Expand Down

0 comments on commit 8e4a64a

Please sign in to comment.