Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that bytes/str values are decoded to utf-8 #260

Merged
merged 7 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions iopipe/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
if PY3:
from urllib.parse import urlparse

binary_Types = bytes
string_types = (str,)
StringIO = io.StringIO
else:
from urlparse import urlparse # noqa

binary_types = str
string_types = (basestring,) # noqa
StringIO = io.BytesIO
27 changes: 14 additions & 13 deletions iopipe/contrib/trace/auto_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
BotocoreSession = None

from iopipe.compat import urlparse
from .util import ensure_utf8

if Session is not None:
original_session_send = Session.send
Expand Down Expand Up @@ -67,7 +68,7 @@ def patch_session_send(context, http_filter):
return

def send(self, *args, **kwargs):
id = str(uuid.uuid4())
id = ensure_utf8(str(uuid.uuid4()))
with context.iopipe.mark(id):
response = original_session_send(self, *args, **kwargs)
trace = context.iopipe.mark.measure(id)
Expand Down Expand Up @@ -139,36 +140,36 @@ def collect_metrics_for_response(http_response, context, trace, http_filter):
request_headers = []
if hasattr(http_response.request, "headers"):
request_headers = [
{"key": k, "string": v}
{"key": k, "string": ensure_utf8(v)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the key for the headers also have their UTF-8 ensured?

for k, v in http_response.request.headers.items()
if k.lower() in INCLUDE_HEADERS
]

request = Request(
hash=getattr(parsed_url, "fragment", None),
hash=ensure_utf8(getattr(parsed_url, "fragment", None)),
headers=request_headers,
hostname=getattr(parsed_url, "hostname", None),
method=getattr(http_response.request, "method", None),
path=getattr(parsed_url, "path", None),
hostname=ensure_utf8(getattr(parsed_url, "hostname", None)),
method=ensure_utf8(getattr(http_response.request, "method", None)),
path=ensure_utf8(getattr(parsed_url, "path", None)),
# TODO: Determine if this is redundant
pathname=getattr(parsed_url, "path", None),
port=getattr(parsed_url, "port", None),
protocol=getattr(parsed_url, "scheme", None),
query=getattr(parsed_url, "query", None),
url=getattr(http_response.request, "url", None),
pathname=ensure_utf8(getattr(parsed_url, "path", None)),
port=ensure_utf8(getattr(parsed_url, "port", None)),
protocol=ensure_utf8(getattr(parsed_url, "scheme", None)),
query=ensure_utf8(getattr(parsed_url, "query", None)),
url=ensure_utf8(getattr(http_response.request, "url", None)),
)

response_headers = []
if hasattr(http_response, "headers"):
response_headers = [
{"key": k, "string": v}
{"key": k, "string": ensure_utf8(v)}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the response header key also have UTF-8 ensured?

for k, v in http_response.headers.items()
if k.lower() in INCLUDE_HEADERS
]

response = Response(
headers=response_headers,
statusCode=getattr(http_response, "status_code", None),
statusCode=ensure_utf8(getattr(http_response, "status_code", None)),
statusMessage=None,
)

Expand Down
10 changes: 10 additions & 0 deletions iopipe/contrib/trace/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from iopipe.compat import binary_types


def add_timeline_measures(timeline):
entries = timeline.get_entries_by_type("mark")
measures = timeline.get_entries_by_type("measure")
Expand All @@ -14,3 +17,10 @@ def add_timeline_measures(timeline):
measure_name = "measure:%s" % base_name
if end_name in entry_names and measure_name not in measure_names:
timeline.measure(measure_name, name, end_name)


def ensure_utf8(v):
"""Ensures that a binary value is dedoced into utf-8"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decoded*

if isinstance(v, binary_types):
return v.decode("utf-8")
return v