Skip to content

Commit

Permalink
Switched to pytz. Added comment for clarification on behaviour of HAR…
Browse files Browse the repository at this point in the history
…Encodable. Added missing parameter in reset(). Fixed accessing headers.
  • Loading branch information
JustusW committed Nov 15, 2014
1 parent 4227fee commit a7ab06d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
23 changes: 8 additions & 15 deletions examples/har_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@
This inline script utilizes harparser.HAR from https://github.com/JustusW/harparser
to generate a HAR log object.
"""
from pytz import utc
from harparser import HAR
from datetime import datetime, timedelta, tzinfo


class UTC(tzinfo):
def utcoffset(self, dt):
return timedelta(0)

def dst(self, dt):
return timedelta(0)

def tzname(self, dt):
return "Z"


class _HARLog(HAR.log):
# The attributes need to be registered here for them to actually be available later via self. This is
# due to HAREncodable linking __getattr__ to __getitem__. Anything that is set only in __init__ will
# just be added as key/value pair to self.__classes__.
__page_list__ = []
__page_count__ = 0
__page_ref__ = {}
Expand All @@ -35,7 +28,7 @@ def __init__(self, page_list):
"entries": []})

def reset(self):
self.__init__()
self.__init__(self.__page_list__)

def add(self, obj):
if isinstance(obj, HAR.pages):
Expand Down Expand Up @@ -110,7 +103,7 @@ def response(context, flow):
if item > -1:
full_time += item

started_date_time = datetime.fromtimestamp(flow.request.timestamp_start, tz=UTC()).isoformat()
started_date_time = datetime.fromtimestamp(flow.request.timestamp_start, tz=utc).isoformat()

request_query_string = [{"name": k, "value": v} for k, v in flow.request.get_query()]
request_http_version = ".".join([str(v) for v in flow.request.httpversion])
Expand All @@ -128,8 +121,8 @@ def response(context, flow):
response_body_size = len(flow.response.content)
response_body_decoded_size = len(flow.response.get_decoded_content())
response_body_compression = response_body_decoded_size - response_body_size
response_mime_type = flow.response.headers.get('Content-Type', [''])[0]
response_redirect_url = flow.response.headers.get('Location', [''])[0]
response_mime_type = flow.response.headers.get_first('Content-Type', '')
response_redirect_url = flow.response.headers.get_first('Location', '')

entry = HAR.entries({"startedDateTime": started_date_time,
"time": full_time,
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"urwid>=1.1",
"lxml>=3.3.6",
"Pillow>=2.3.0",
"pytz",
"harparser",
},
"mitmdump": set()
Expand Down

0 comments on commit a7ab06d

Please sign in to comment.