Skip to content

Commit

Permalink
Merge pull request #10 from Moesif/fixed-multipart-stream
Browse files Browse the repository at this point in the history
Fixed: Access request's body/data stream
  • Loading branch information
dgilling authored Feb 8, 2018
2 parents 9888b3d + b2c0868 commit 28f42e7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
24 changes: 15 additions & 9 deletions moesifdjango/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import base64
import re
import django
import logging

from django.conf import settings
from django.utils import timezone
Expand All @@ -17,7 +18,7 @@
from django.http import HttpRequest, HttpResponse
from .http_response_catcher import HttpResponseCatcher
from .masks import *
import logging
from io import BytesIO

# Logger Config
logging.basicConfig()
Expand All @@ -41,7 +42,6 @@ def get_client_ip(request):
ip = request.META.get('REMOTE_ADDR')
return ip


def moesif_middleware(*args):
# One-time configuration and initialization.
middleware_settings = settings.MOESIF_MIDDLEWARE
Expand All @@ -63,10 +63,16 @@ def middleware(request):
# the view (and later middleware) are called.

req_time = timezone.now()
raw_request_body = copy.copy(request.body)

if not request.content_type.startswith('multipart/form-data'):
request._mo_body = request.body
request._stream = BytesIO(request.body)
request._read_started = False
else:
request._mo_body = None

if DEBUG:
print("raw body before getting response" + raw_request_body)
print("raw body before getting response")

if (len(args) < 1):
print("""
Expand Down Expand Up @@ -126,13 +132,13 @@ def flatten_to_string(value):
try:
# print("about to serialize request body" + request.body)
if DEBUG:
print("about to process request body" + raw_request_body)
if raw_request_body:
req_body = json.loads(raw_request_body)
print("about to process request body")
if request._mo_body:
req_body = json.loads(request._mo_body)
req_body = mask_body(req_body, middleware_settings.get('REQUEST_BODY_MASKS'))
except:
if raw_request_body:
req_body = base64.standard_b64encode(raw_request_body)
if request._mo_body:
req_body = base64.standard_b64encode(request._mo_body)
req_body_transfer_encoding = 'base64'

ip_address = get_client_ip(request)
Expand Down
25 changes: 15 additions & 10 deletions moesifdjango/middleware_pre19.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.http import HttpRequest, HttpResponse
from .http_response_catcher import HttpResponseCatcher
from .masks import *
from io import BytesIO

def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
Expand Down Expand Up @@ -45,17 +46,21 @@ def __init__(self):

def process_request(self, request):
request.moesif_req_time = timezone.now()
request._body = request.body
if not request.content_type.startswith('multipart/form-data'):
request._mo_body = request.body
request._stream = BytesIO(request.body)
request._read_started = False
else:
request._mo_body = None

def process_response(self, request, response):
# Code to be executed for each request before
# the view (and later middleware) are called.

req_time = request.moesif_req_time
raw_request_body = request._body

if self.DEBUG:
print("raw body before getting response" + raw_request_body)
print("raw body before getting response")

# response = get_response(request)
# Code to be executed for each request/response after
Expand Down Expand Up @@ -107,12 +112,12 @@ def flatten_to_string(value):
try:
# print("about to serialize request body" + request.body)
if self.DEBUG:
print("about to process request body" + raw_request_body)
if raw_request_body:
req_body = json.loads(raw_request_body)
print("about to process request body")
if request._mo_body:
req_body = json.loads(request._mo_body)
except:
if raw_request_body:
req_body = base64.standard_b64encode(raw_request_body)
if request._mo_body:
req_body = base64.standard_b64encode(request._mo_body)
req_body_transfer_encoding = 'base64'


Expand Down Expand Up @@ -155,7 +160,7 @@ def mapper(key):
body=req_body,
transfer_encoding=req_body_transfer_encoding)

event_rsp = EventResponseModel(time=req_time.isoformat(),
event_rsp = EventResponseModel(time=rsp_time.isoformat(),
status=response.status_code,
headers=rsp_headers,
body=rsp_body,
Expand All @@ -177,7 +182,7 @@ def mapper(key):

metadata = None
try:
get_metadata = middleware_settings.get('GET_METADATA', None)
get_metadata = self.middleware_settings.get('GET_METADATA', None)
if get_metadata is not None:
metadata = get_metadata(request, response)
except:
Expand Down

0 comments on commit 28f42e7

Please sign in to comment.