harparser 0.4.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: harparser
|
|
3
|
+
Version: 0.4.1
|
|
4
|
+
Summary: Python HAR Parser Utility
|
|
5
|
+
Author-email: Justus Wingert <wingert@kw-it.info>
|
|
6
|
+
Project-URL: Homepage, https://github.com/JustusW/harparser
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
harparse
|
|
12
|
+
========
|
|
13
|
+
|
|
14
|
+
This is yet another python HAR Parser utility. However unlike its contemporaries it uses a recursive strategy in order to provide an abstract yet easy to understand and adapt handle on the HAR format in python.
|
|
15
|
+
|
|
16
|
+
Installation
|
|
17
|
+
============
|
|
18
|
+
Installation is straight forward:
|
|
19
|
+
|
|
20
|
+
git clone https://github.com/JustusW/harparser.git
|
|
21
|
+
cd harparser
|
|
22
|
+
pip install .
|
|
23
|
+
|
|
24
|
+
or if you don't want the code locally:
|
|
25
|
+
|
|
26
|
+
pip install git+https://github.com/JustusW/harparser.git
|
|
27
|
+
|
|
28
|
+
or
|
|
29
|
+
|
|
30
|
+
pip install harparser
|
|
31
|
+
|
|
32
|
+
Usage
|
|
33
|
+
=====
|
|
34
|
+
If you just want to read and write HARs usage is simple:
|
|
35
|
+
|
|
36
|
+
from harparser import HAR
|
|
37
|
+
my_har = HAR.log().json(<JSON_STRING>)
|
|
38
|
+
|
|
39
|
+
This call is equivalent to:
|
|
40
|
+
|
|
41
|
+
from harparser import HAR
|
|
42
|
+
from json import loads
|
|
43
|
+
my_har = HAR.log(loads(<JSON_STRING>))
|
|
44
|
+
|
|
45
|
+
Every HAR class is implementing collections.abc.MutableMapping thus can be cast to dict easily:
|
|
46
|
+
|
|
47
|
+
dict(my_har)
|
|
48
|
+
my_har.__dict__
|
|
49
|
+
|
|
50
|
+
The library harparse provides convenience methods for loading HAR from json strings and compressed json strings.
|
|
51
|
+
|
|
52
|
+
from harparser import HAR
|
|
53
|
+
my_har = HAR.log().decompress(<COMPRESSED_JSON_STRING>)
|
|
54
|
+
|
|
55
|
+
This call is equivalent to:
|
|
56
|
+
|
|
57
|
+
from harparser import HAR
|
|
58
|
+
from zlib import decompress
|
|
59
|
+
from json import loads
|
|
60
|
+
my_har = HAR.log(loads(decompress(<COMPRESSED_JSON_STRING>)))
|
|
61
|
+
|
|
62
|
+
All HAR classes are accessible via their key as defined in the official spec for HAR 1.2. You can access them either as dict like keys or as attributes of HAR. The following methods of access are yielding identical classes:
|
|
63
|
+
|
|
64
|
+
HAR.pageTimings is HAR['pageTimings'] and HAR.pageTimings is HAR.get('pageTimings')
|
|
65
|
+
|
|
66
|
+
Obviously the real magic starts once you overwrite or inherit from the very basic structural integrity checking behaviour and add your own little helpers. As an example I give you this handy log helper, that provides an add method automatically deciding between pages and entries, as well as a reset method and some scaffolding.
|
|
67
|
+
|
|
68
|
+
class _HARLog(HAR.log):
|
|
69
|
+
def __init__(self):
|
|
70
|
+
HAR.log.__init__(self, {"version": "1.2",
|
|
71
|
+
"creator": {"name": "MITMPROXY HARExtractor",
|
|
72
|
+
"version": "0.1",
|
|
73
|
+
"comment": ""},
|
|
74
|
+
"pages": [],
|
|
75
|
+
"entries": []})
|
|
76
|
+
|
|
77
|
+
def reset(self):
|
|
78
|
+
self.__init__()
|
|
79
|
+
|
|
80
|
+
def add(self, obj):
|
|
81
|
+
if isinstance(obj, HAR.pages):
|
|
82
|
+
self['pages'].append(obj)
|
|
83
|
+
if isinstance(obj, HAR.entries):
|
|
84
|
+
self['entries'].append(obj)
|
|
85
|
+
|
|
86
|
+
Advanced Usage
|
|
87
|
+
==============
|
|
88
|
+
Internally HAR is only an instance of the private class `_HAR`. The `__init__` method instantiates all keys defined in the private attribute `__map__` as classes extending HAREncodable. By default all changes to HAR will be shared globally. If you want to make changes without exposing them like that you have two options.
|
|
89
|
+
|
|
90
|
+
from harparse import _HAR
|
|
91
|
+
MyHAR = _HAR()
|
|
92
|
+
|
|
93
|
+
or
|
|
94
|
+
|
|
95
|
+
class _MyOtherHAR(_HAR):
|
|
96
|
+
pass
|
|
97
|
+
MyOtherHAR = _MyOtherHAR()
|
|
98
|
+
|
|
99
|
+
Simply overwriting the keys of the `_HAR` instance will change the behaviour of all subsequent usages. An example would be this:
|
|
100
|
+
|
|
101
|
+
class HARpageTimings(HAR.pageTimings):
|
|
102
|
+
def __setitem__(self, key, value):
|
|
103
|
+
if key != 'comment':
|
|
104
|
+
self['comment'] = "Key '%s' was set to '%s'." % (key, str(value))
|
|
105
|
+
return super(HARpageTimings, self).__setitem__(key, value)
|
|
106
|
+
|
|
107
|
+
MyHAR.pageTimings = HARpageTimings
|
|
108
|
+
|
|
109
|
+
You are encouraged to use this behaviour extensively: Be it ORM, input validation, input or output mapping whatever special behaviour you want, harparse is compatible.
|
|
110
|
+
|
|
111
|
+
Finally
|
|
112
|
+
=======
|
|
113
|
+
If you find anything that bugs you about my implementation or have suggestions to improve it feel free to contact me or just send me a pull request.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
harparser.py,sha256=3OQ6QfqgTjpAbQrJOOmyxiNWcq2theO44ObOznNXQb4,12637
|
|
2
|
+
harparser-0.4.1.dist-info/licenses/LICENSE,sha256=HLGsNO5gyhQbatlBjWANCxP_l627pFBkUbMIQ7vL4dg,1097
|
|
3
|
+
harparser-0.4.1.dist-info/METADATA,sha256=qdI1ArF-npNpdkz3wgfjWEHDV0ZdISQWUaecDglCRtw,4315
|
|
4
|
+
harparser-0.4.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
harparser-0.4.1.dist-info/top_level.txt,sha256=zndo7zjSWwBV2lC1-S9wdCZSqnJzxuBMus7fuFF-PQo,10
|
|
6
|
+
harparser-0.4.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014 JustusW
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
harparser
|
harparser.py
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module implements HAR as per specification version 1.2.
|
|
3
|
+
"""
|
|
4
|
+
from collections.abc import MutableMapping
|
|
5
|
+
from json import dumps, loads
|
|
6
|
+
from zlib import compress, decompress
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class HAREncodable(MutableMapping):
|
|
10
|
+
"""
|
|
11
|
+
Base class that allows for recursive HAR structures using HAR as map.
|
|
12
|
+
"""
|
|
13
|
+
__required__ = {}
|
|
14
|
+
__optional__ = {}
|
|
15
|
+
|
|
16
|
+
def __init__(self, *args, **kwargs):
|
|
17
|
+
"""
|
|
18
|
+
Initialize the private dict that is used to actually store the information.
|
|
19
|
+
Fills it with the content given in args/kwargs then checks against
|
|
20
|
+
__required__ for missing mandatory fields.
|
|
21
|
+
|
|
22
|
+
Important: If no parameters are given no checks will be done for convenience.
|
|
23
|
+
"""
|
|
24
|
+
self.__dict__ = {}
|
|
25
|
+
|
|
26
|
+
if len(args) > 0:
|
|
27
|
+
kwargs = args[0]
|
|
28
|
+
elif len(kwargs) == 0:
|
|
29
|
+
return
|
|
30
|
+
|
|
31
|
+
for key, value in kwargs.items():
|
|
32
|
+
self[key] = value
|
|
33
|
+
|
|
34
|
+
for key in self.__required__:
|
|
35
|
+
self[key]
|
|
36
|
+
|
|
37
|
+
# Rather clunky exclusives check.
|
|
38
|
+
try:
|
|
39
|
+
self.__required_exclusives__
|
|
40
|
+
except AttributeError:
|
|
41
|
+
return
|
|
42
|
+
|
|
43
|
+
found = False
|
|
44
|
+
for key in self.__required_exclusives__:
|
|
45
|
+
if found and key in self:
|
|
46
|
+
raise Exception("Mutually exclusive key found. %r in %r (Violates: %r)" % (key, self, self.__required_exclusives__))
|
|
47
|
+
if not found and key in self:
|
|
48
|
+
found = True
|
|
49
|
+
|
|
50
|
+
if not found:
|
|
51
|
+
for key in self.__required_exclusives__:
|
|
52
|
+
self[key]
|
|
53
|
+
|
|
54
|
+
def __setitem__(self, key, value):
|
|
55
|
+
"""
|
|
56
|
+
Exposes self.__dict__.__setitem__ with typecasting.
|
|
57
|
+
|
|
58
|
+
Typecast any item to the correct type based on key name and position as
|
|
59
|
+
implicitly provided by __required__ and __optional__. If a key is used
|
|
60
|
+
that is not in the specification it will be added without type casting!
|
|
61
|
+
"""
|
|
62
|
+
item_type = self.__required__.get(key, self.__optional__.get(key, getattr(self, "__required_exclusives__", {}).get(key, None)))
|
|
63
|
+
if type(item_type) is type:
|
|
64
|
+
value = item_type(value)
|
|
65
|
+
elif type(item_type) is list:
|
|
66
|
+
value = [HAR[key](v) for v in value]
|
|
67
|
+
elif type(item_type) is dict:
|
|
68
|
+
value = HAR[key](value)
|
|
69
|
+
# If it is None or not in the handled cases we would use pass anyway.
|
|
70
|
+
|
|
71
|
+
return self.__dict__.__setitem__(key, value)
|
|
72
|
+
|
|
73
|
+
def __getitem__(self, *args, **kwargs):
|
|
74
|
+
"""
|
|
75
|
+
Directly exposes self.__dict__.__getitem__
|
|
76
|
+
"""
|
|
77
|
+
return self.__dict__.__getitem__(*args, **kwargs)
|
|
78
|
+
|
|
79
|
+
def __delitem__(self, *args, **kwargs):
|
|
80
|
+
"""
|
|
81
|
+
Directly exposes self.__dict__.__delitem__
|
|
82
|
+
"""
|
|
83
|
+
return self.__dict__.__delitem__(*args, **kwargs)
|
|
84
|
+
|
|
85
|
+
def __len__(self):
|
|
86
|
+
"""
|
|
87
|
+
Directly exposes self.__dict__.__len__
|
|
88
|
+
"""
|
|
89
|
+
return self.__dict__.__len__()
|
|
90
|
+
|
|
91
|
+
def __iter__(self):
|
|
92
|
+
"""
|
|
93
|
+
Directly exposes self.__dict__.__iter__
|
|
94
|
+
"""
|
|
95
|
+
return self.__dict__.__iter__()
|
|
96
|
+
|
|
97
|
+
def json(self, json_string=None):
|
|
98
|
+
"""
|
|
99
|
+
Convenience method allowing easy dumping to and loading from json.
|
|
100
|
+
"""
|
|
101
|
+
if json_string is not None:
|
|
102
|
+
data = loads(json_string)
|
|
103
|
+
# Accept the {"log": {...}} wrapper that json() emits for HAR.log.
|
|
104
|
+
if isinstance(self, HAR.log) and isinstance(data, dict) and list(data) == ["log"]:
|
|
105
|
+
data = data["log"]
|
|
106
|
+
return self.__init__(data)
|
|
107
|
+
dump = self
|
|
108
|
+
if isinstance(self, HAR.log):
|
|
109
|
+
dump = {"log": dump}
|
|
110
|
+
return dumps(dump, default=lambda x: dict(x))
|
|
111
|
+
|
|
112
|
+
def compress(self):
|
|
113
|
+
"""
|
|
114
|
+
Convenience method for compressing the json output.
|
|
115
|
+
Returns zlib compressed, utf-8 encoded json as bytes.
|
|
116
|
+
"""
|
|
117
|
+
return compress(self.json().encode("utf-8"))
|
|
118
|
+
|
|
119
|
+
def decompress(self, compressed_json_string):
|
|
120
|
+
"""
|
|
121
|
+
Convenience method for decompressing json input
|
|
122
|
+
(zlib compressed, utf-8 encoded json bytes as returned by compress()).
|
|
123
|
+
"""
|
|
124
|
+
return self.json(json_string=decompress(compressed_json_string).decode("utf-8"))
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class _HAR(MutableMapping, object):
|
|
128
|
+
"""
|
|
129
|
+
HAR implementation as per specification version 1.2 ()
|
|
130
|
+
This class maps the specification contained in __map__ to dynamic subclasses stored in __classes__.
|
|
131
|
+
It then exposes all of this by implementing MutableMapping, the generated subclasses being its keys.
|
|
132
|
+
"""
|
|
133
|
+
__map__ = {"log": {"__required__": {"version": str,
|
|
134
|
+
"creator": {},
|
|
135
|
+
"entries": [], },
|
|
136
|
+
"__optional__": {"browser": {},
|
|
137
|
+
"pages": [],
|
|
138
|
+
"comment": str, }},
|
|
139
|
+
"creator": {"__required__": {"name": str,
|
|
140
|
+
"version": str, },
|
|
141
|
+
"__optional__": {"comment": str}, },
|
|
142
|
+
"browser": {"__required__": {"name": str,
|
|
143
|
+
"version": str, },
|
|
144
|
+
"__optional__": {"comment": str}, },
|
|
145
|
+
"pages": {"__required__": {"startedDateTime": str,
|
|
146
|
+
"id": str,
|
|
147
|
+
"title": str, },
|
|
148
|
+
"__optional__": {"pageTimings": {},
|
|
149
|
+
"comment": str}, },
|
|
150
|
+
"pageTimings": {"__required__": {},
|
|
151
|
+
"__optional__": {"onContentLoad": int,
|
|
152
|
+
"onLoad": int,
|
|
153
|
+
"comment": str}, },
|
|
154
|
+
"entries": {"__required__": {"startedDateTime": str,
|
|
155
|
+
"time": int,
|
|
156
|
+
"request": {},
|
|
157
|
+
"response": {},
|
|
158
|
+
"cache": {},
|
|
159
|
+
"timings": {}, },
|
|
160
|
+
"__optional__": {"pageref": str,
|
|
161
|
+
"serverIPAddress": str,
|
|
162
|
+
"connection": str,
|
|
163
|
+
"comment": str}, },
|
|
164
|
+
"request": {"__required__": {"method": str,
|
|
165
|
+
"url": str,
|
|
166
|
+
"httpVersion": str,
|
|
167
|
+
"cookies": [],
|
|
168
|
+
"headers": [],
|
|
169
|
+
"queryString": [],
|
|
170
|
+
"headersSize": int,
|
|
171
|
+
"bodySize": int, },
|
|
172
|
+
"__optional__": {"postData": {},
|
|
173
|
+
"comment": str}, },
|
|
174
|
+
"response": {"__required__": {"status": int,
|
|
175
|
+
"statusText": str,
|
|
176
|
+
"httpVersion": str,
|
|
177
|
+
"cookies": [],
|
|
178
|
+
"headers": [],
|
|
179
|
+
"content": {},
|
|
180
|
+
"redirectURL": str,
|
|
181
|
+
"headersSize": int,
|
|
182
|
+
"bodySize": int, },
|
|
183
|
+
"__optional__": {"comment": str}, },
|
|
184
|
+
"cookies": {"__required__": {"name": str,
|
|
185
|
+
"value": str, },
|
|
186
|
+
"__optional__": {"path": str,
|
|
187
|
+
"domain": str,
|
|
188
|
+
"expires": str,
|
|
189
|
+
"httpOnly": bool,
|
|
190
|
+
"secure": bool,
|
|
191
|
+
"comment": str}, },
|
|
192
|
+
"headers": {"__required__": {"name": str,
|
|
193
|
+
"value": str, },
|
|
194
|
+
"__optional__": {"comment": str}, },
|
|
195
|
+
"queryString": {"__required__": {"name": str,
|
|
196
|
+
"value": str, },
|
|
197
|
+
"__optional__": {"comment": str}, },
|
|
198
|
+
"postData": {"__required__": {"mimeType": str, },
|
|
199
|
+
"__required_exclusives__": {"params": [],
|
|
200
|
+
"text": str, },
|
|
201
|
+
"__optional__": {"comment": str}, },
|
|
202
|
+
"params": {"__required__": {"name": str, },
|
|
203
|
+
"__optional__": {"value": str,
|
|
204
|
+
"fileName": str,
|
|
205
|
+
"contentType": str,
|
|
206
|
+
"comment": str}, },
|
|
207
|
+
"content": {"__required__": {"size": int,
|
|
208
|
+
"mimeType": str, },
|
|
209
|
+
"__optional__": {"compression": int,
|
|
210
|
+
"text": str,
|
|
211
|
+
"comment": str}, },
|
|
212
|
+
"cache": {"__required__": {},
|
|
213
|
+
"__optional__": {"beforeRequest": {},
|
|
214
|
+
"afterRequest": {},
|
|
215
|
+
"comment": str}, },
|
|
216
|
+
"beforeRequest": {"__required__": {"lastAccess": str,
|
|
217
|
+
"eTag": str,
|
|
218
|
+
"hitCount": int, },
|
|
219
|
+
"__optional__": {"expires": str,
|
|
220
|
+
"comment": str, }, },
|
|
221
|
+
"afterRequest": {"__required__": {"lastAccess": str,
|
|
222
|
+
"eTag": str,
|
|
223
|
+
"hitCount": int, },
|
|
224
|
+
"__optional__": {"expires": str,
|
|
225
|
+
"comment": str, }, },
|
|
226
|
+
"timings": {"__required__": {"send": int,
|
|
227
|
+
"wait": int,
|
|
228
|
+
"receive": int, },
|
|
229
|
+
"__optional__": {"blocked": int,
|
|
230
|
+
"dns": int,
|
|
231
|
+
"connect": int,
|
|
232
|
+
"ssl": int,
|
|
233
|
+
"comment": str, }, }}
|
|
234
|
+
|
|
235
|
+
def __init__(self):
|
|
236
|
+
"""
|
|
237
|
+
Exposes the classes mapped from __map__ extending HAREncodable as a
|
|
238
|
+
MutableMapping, dict like object.
|
|
239
|
+
"""
|
|
240
|
+
self.__classes__ = dict([(name,
|
|
241
|
+
type(name, (HAREncodable, ), self.__map__[name]))
|
|
242
|
+
for name in self.__map__])
|
|
243
|
+
|
|
244
|
+
def __getattr__(self, item):
|
|
245
|
+
"""
|
|
246
|
+
Exposes __getitem__ keys as attributes.
|
|
247
|
+
"""
|
|
248
|
+
return self[item]
|
|
249
|
+
|
|
250
|
+
def __getitem__(self, *args, **kwargs):
|
|
251
|
+
"""
|
|
252
|
+
Directly exposes self.__classes__.__getitem__
|
|
253
|
+
"""
|
|
254
|
+
return self.__classes__.__getitem__(*args, **kwargs)
|
|
255
|
+
|
|
256
|
+
def __setitem__(self, *args, **kwargs):
|
|
257
|
+
"""
|
|
258
|
+
Directly exposes self.__classes__.__setitem__
|
|
259
|
+
"""
|
|
260
|
+
return self.__classes__.__setitem__(*args, **kwargs)
|
|
261
|
+
|
|
262
|
+
def __delitem__(self, *args, **kwargs):
|
|
263
|
+
"""
|
|
264
|
+
Directly exposes self.__classes__.__delitem__
|
|
265
|
+
"""
|
|
266
|
+
return self.__classes__.__delitem__(*args, **kwargs)
|
|
267
|
+
|
|
268
|
+
def __iter__(self):
|
|
269
|
+
"""
|
|
270
|
+
Directly exposes self.__classes__.__iter__
|
|
271
|
+
"""
|
|
272
|
+
return self.__classes__.__iter__()
|
|
273
|
+
|
|
274
|
+
def __len__(self):
|
|
275
|
+
"""
|
|
276
|
+
Directly exposes self.__classes__.__len__
|
|
277
|
+
"""
|
|
278
|
+
return self.__classes__.__len__()
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
# Make an instance of _HAR available as HAR. If required one can
|
|
282
|
+
# instantiate another object from _HAR to avoid interference with
|
|
283
|
+
# other peoples code.
|
|
284
|
+
HAR = _HAR()
|