Skip to content

Commit

Permalink
Response bodies are now stored b64 encoded (support for binary respon…
Browse files Browse the repository at this point in the history
…ses).

This closes #85, closes #1
  • Loading branch information
trik committed Jan 19, 2016
1 parent 317ccb1 commit 8993d7e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 38 deletions.
32 changes: 3 additions & 29 deletions silk/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import sys
import traceback
import base64
from uuid import UUID

from django.core.urlresolvers import resolve
Expand Down Expand Up @@ -197,29 +198,6 @@ def body(self):
body = ''
content_type, char_set = _parse_content_type(self.response.get('Content-Type', ''))
content = getattr(self.response, 'content', '')
if char_set and content:
try:
content = content.decode(char_set)
except AttributeError:
pass
except LookupError: # If no encoding exists, default to UTF-8
try:
content = content.decode('UTF-8')
except AttributeError:
pass
except UnicodeDecodeError:
content = ''
except Exception as e:
Logger.error('Unable to decode response body using char_set %s due to error: %s. Will ignore. Stacktrace:' % (char_set, e))
traceback.print_exc()
else:
# Default to an attempt at UTF-8 decoding.
try:
content = content.decode('UTF-8')
except AttributeError:
pass
except UnicodeDecodeError:
content = ''
if content:
max_body_size = SilkyConfig().SILKY_MAX_RESPONSE_BODY_SIZE
if max_body_size > -1:
Expand Down Expand Up @@ -259,10 +237,6 @@ def construct_response_model(self):
status_code=self.response.status_code,
encoded_headers=json.dumps(headers),
body=b)
# Text fields are encoded as UTF-8 in Django and hence will try to coerce
# anything to we pass to UTF-8. Some stuff like binary will fail.
try:
silky_response.raw_body = content
except UnicodeDecodeError:
Logger.debug('NYI: Saving of binary response body') # TODO
silky_response.raw_body = base64.b64encode(content)

return silky_response
5 changes: 5 additions & 0 deletions silk/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import Counter
import json
import base64

from django.db import models
from django.db.models import DateTimeField, TextField, CharField, ForeignKey, IntegerField, BooleanField, F, \
Expand Down Expand Up @@ -126,6 +127,10 @@ def headers(self):
raw = {}
return CaseInsensitiveDictionary(raw)

@property
def raw_body_decoded(self):
return base64.b64decode(self.raw_body)


# TODO rewrite docstring
class SQLQueryManager(models.Manager):
Expand Down
16 changes: 9 additions & 7 deletions silk/templates/silk/request.html
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,15 @@
{% endif %}
{% if silk_request.response.raw_body %}
{% heading 'Raw Response Body' %}
{% if silk_request.response.raw_body|length > 1000 %}
The raw response body is <b>{{ silk_request.response.raw_body|length }}</b> characters long
and hence is <b>too big</b> to show here.
Click <a href="{% url "silk:raw" silk_request.pk %}?typ=response&subtyp=raw">here</a> to view the raw response body.
{% else %}
<pre>{{ silk_request.response.raw_body }}</pre>
{% endif %}
{% with raw_body=silk_request.response.raw_body_decoded %}
{% if raw_body|length > 1000 %}
The raw response body is <b>{{ raw_body|length }}</b> characters long
and hence is <b>too big</b> to show here.
Click <a href="{% url "silk:raw" silk_request.pk %}?typ=response&subtyp=raw">here</a> to view the raw response body.
{% else %}
<pre>{{ raw_body }}</pre>
{% endif %}
{% endwith %}
{% endif %}
{% if silk_request.response.body %}
{% heading 'Response Body' %}
Expand Down
7 changes: 5 additions & 2 deletions silk/views/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.views.generic import View
from silk.auth import login_possibly_required, permissions_possibly_required
from silk.models import Request
import logging
Logger = logging.getLogger('silk')


class Raw(View):
Expand All @@ -17,9 +19,10 @@ def get(self, request, request_id):
if typ and subtyp:
silk_request = Request.objects.get(pk=request_id)
if typ == 'request':
body = silk_request.raw_body if subtyp == 'raw' else silk_request.body
body = silk_request.raw_body_ if subtyp == 'raw' else silk_request.body
elif typ == 'response':
body = silk_request.response.raw_body if subtyp == 'raw' else silk_request.response.body
Logger.debug(silk_request.response.raw_body_decoded)
body = silk_request.response.raw_body_decoded if subtyp == 'raw' else silk_request.response.body
return render_to_response('silk/raw.html', {
'body': body
})
Expand Down

0 comments on commit 8993d7e

Please sign in to comment.