-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.py
437 lines (341 loc) · 13.7 KB
/
url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python
import argparse
import base64
import copy
import logging
import sys
"""
DifferenceURL
Library which shows the difference between several links
@package DifferenceURL
@author Shamsudin Serderov
@url https://www.steein.ru
@copyright 2017 - Steein Inc
@version 1.0.0
"""
# -------------------------------------------------------------------------------------------------------
class Error(Exception):
""" Base exception class. """
pass
class ParamDiffTypeError(Error):
""" Raised when an incorrect diff type used. """
pass
class HostnameParseError(Error):
""" Raised when unable to parse hostname from URL. """
pass
# -------------------------------------------------------------------------------------------------------
"""
Represents difference of 2 URL params with same name.
TODO(macpd): investigate making this a namedtuple
"""
class DiffEntry(object):
left_only = 1
right_only = 2
both_differ = 3
left_header_format = '{0}\n< {1}'
right_header_format = '{0}\n> {1}'
left_additional_separator = '\n< '
right_additional_separator = '\n> '
def __init__(self, name, left_value, right_value, diff_type):
self._name = name
self._left_val = list(left_value) if left_value else []
self._right_val = list(right_value) if right_value else []
try:
if self._valid_diff_type(diff_type):
self._type = diff_type
except ParamDiffTypeError:
logging.error("Incorrect diff type: %s", diff_type)
self._type = self.both_differ
def _valid_diff_type(self, diff_type):
if (diff_type != self.left_only and diff_type != self.right_only and
diff_type != self.both_differ):
raise ParamDiffTypeError('%s is not a valid diff type. ', diff_type)
return True
@property
def name(self):
return self._name
def __str__(self):
ret = self._name
if self._type == self.left_only or self._type == self.both_differ:
ret = self.left_header_format.format(ret, self.left_additional_separator.join(self._left_val))
if self._type == self.right_only or self._type == self.both_differ:
ret = self.right_header_format.format(ret, self.right_additional_separator.join(self._right_val))
return ret
# -------------------------------------------------------------------------------------------------------
"""
Object to diff URLs.
Diffs URLs upon initialization.
"""
class UrlDiffer(object):
PATH_DELIM = '?'
PARAM_DELIM = '&'
NAME_VAL_DELIM = '='
SCHEME_DELIM = '://'
UNIX_SLASH = '/'
URL_ESCAPE_CHAR = '%'
URL_ESCAPE_SEQ_LEN = 3 # expected length of URL espace sequences, aka len('%25')
"""
Initializes object and performs URL diffing.
"""
def __init__(self, left_url, right_url, names_only=False, hostnames=False,
url_decode_params=False, case_insensitive=False):
self._left_url = self._normalize_url(left_url)
self._right_url = self._normalize_url(right_url)
self._names_only = names_only
self._wants_hostname_diff = hostnames
self._url_decode_params = url_decode_params
self._case_insensitive = case_insensitive
self._diffs = []
self._do_diff()
def __str__(self):
ret = []
for diff in self._diffs:
if self._names_only:
ret.append(diff.name)
else:
ret.append(str(diff))
join_delim = '\n' if self._names_only else '\n\n'
return join_delim.join(ret)
"""
Strips white space, and removes all chars after #
"""
def _normalize_url(self, url):
ret = url.strip()
if '#' in ret:
idx = ret.index('#')
ret = ret[:idx]
return ret
"""
Parses the hostname from a URL"
Finds hostname between scheme and first unix slash.
"""
def _get_hostname(self, url):
if self.SCHEME_DELIM in url:
scheme_idx = url.index(self.SCHEME_DELIM)
hostname_begin = scheme_idx + len(self.SCHEME_DELIM)
else:
hostname_begin = 0
if self.UNIX_SLASH in url[hostname_begin:]:
hostname_end = url.index(self.UNIX_SLASH, hostname_begin)
else:
hostname_end = hostname_begin + len(url[hostname_begin:])
return url[hostname_begin:hostname_end]
"""
Diffs hostnames, if different appends ParamDiffEntry to diffs list.
Args:
left: String; left hostname.
right: String; right hostname.
Returns:
Bool; True if different, else False.
"""
def _diff_hostnames(self, left, right):
if left == right:
self._hostnames_differ = False
else:
self._hostnames_differ = True
self._diffs.append(
DiffEntry(
'Hostname',
[left],
[right],
DiffEntry.both_differ
)
)
return self._hostnames_differ
"""
Returns a dict of the url params.
Args:
url: String; URL to get parameter names and values from.
Returns:
Dict of parameter names that map to their values.
"""
def _get_params(self, url):
param_dict = {}
if self.PATH_DELIM not in url:
return param_dict
params_pos = url.find(self.PATH_DELIM) + 1
for token in url[params_pos:].split(self.PARAM_DELIM):
if not token:
continue
if '=' not in token:
token_key = token
token_value = ''
else:
partitioned_param = token.partition(self.NAME_VAL_DELIM)
token_key = partitioned_param[0]
token_value = partitioned_param[2]
if self._url_decode_params:
token_key = self._url_decode(token_key)
token_value = self._url_decode(token_value)
value_list = param_dict.get(token_key, [])
value_list.append(token_value)
param_dict[token_key] = value_list
return param_dict
"""
Returns a list of the diffence between dicts on key/values.
First all keys that exist in both URLs are compared, then keys only in the
left, followed by keys only in the right.
Args:
left_param: dict; param name -> values dict of the left URL.
right_param: dict; param name -> values dict of the right URL.
Returns:
List of ParamDiffEntry of differences between the left and right params.
"""
@staticmethod
def _diff_params(left_params, right_params):
diffs = []
left_key_set = frozenset(left_params.keys())
right_key_set = frozenset(right_params.keys())
left_key_diff = left_key_set.difference(right_key_set)
right_key_diff = right_key_set.difference(left_key_set)
key_intersection = left_key_set.intersection(right_key_set)
for common_key in key_intersection:
left_val_set = set(left_params[common_key])
right_val_set = set(right_params[common_key])
left_diff = left_val_set.difference(right_val_set)
right_diff = right_val_set.difference(left_val_set)
if left_diff and right_diff:
diff_type = DiffEntry.both_differ
elif left_diff:
diff_type = DiffEntry.left_only
elif right_diff:
diff_type = DiffEntry.right_only
else:
# if no diff skip to next iteration
continue
diffs.append(
DiffEntry(
common_key,
left_val_set.difference(right_val_set),
right_val_set.difference(left_val_set),
diff_type
)
)
for left_key in left_key_diff:
diffs.append(DiffEntry(
left_key, left_params[left_key], None, DiffEntry.left_only))
for right_key in right_key_diff:
diffs.append(DiffEntry(
right_key, None, right_params[right_key], DiffEntry.right_only))
return diffs
"""
Performs all appropriate diffing operations.
"""
def _do_diff(self):
if self._case_insensitive:
self._left_url, self._right_url = self._left_url.lower(), self._right_url.lower()
if self._wants_hostname_diff:
self._left_hostname = self._get_hostname(self._left_url)
self._right_hostname = self._get_hostname(self._right_url)
self._diff_hostnames(self._left_hostname, self._right_hostname)
self._left_params_dict = self._get_params(self._left_url)
self._right_params_dict = self._get_params(self._right_url)
if not self._left_params_dict == self._right_params_dict:
self._diffs.extend(self._diff_params(
self._left_params_dict,
self._right_params_dict
)
)
"""
URL decodes provided string.
Replaces all instances of %NN with the ascii value of hex(NN).
Args:
token: String to be decoded.
Returns:
String; deocded string.
"""
def _url_decode(self, token):
if self.URL_ESCAPE_CHAR not in token:
return token
new_token = []
cur = prev = 0
cur = token.find(self.URL_ESCAPE_CHAR, prev)
while cur != -1:
new_token.append(token[prev:cur])
decoded_hex_as_bytes = base64.b16decode(
token[cur + 1:cur + self.URL_ESCAPE_SEQ_LEN], casefold=True)
new_token.append(decoded_hex_as_bytes.decode())
prev = cur + self.URL_ESCAPE_SEQ_LEN
cur = token.find(self.URL_ESCAPE_CHAR, prev)
new_token.append(token[prev:])
return ''.join(new_token)
"""
Returns a deep coy of the left params dict.
"""
def left_params(self):
return copy.deepcopy(self._left_params_dict)
"""
Returns a deep coy of the left params dict.
"""
def right_params(self):
return copy.deepcopy(self._right_params_dict)
"""
Returns True if URLs differ, else false.
"""
def are_different(self):
return len(self._diffs) != 0
# -------------------------------------------------------------------------------------------------------
@property
def diff(self):
return copy.deepcopy(self._diffs)
# -------------------------------------------------------------------------------------------------------
"""
Parses args, inits and prints differ, and exits with appropriate value.
TODO(macpd): provide option for second diff delimeter. This would allow one to diff multivalued param values.
TODO(macpd): provide verbosity option
"""
def launcher():
arg_parser = argparse.ArgumentParser(
version='1.0.0',
description='It shows the difference between the two references',
epilog='This is a small library that throws everything after the # sign '
'https://github.com/SteeinSource/DifferenceURL',
add_help=True
)
# argument --hostname
arg_parser.add_argument('--hostname', default=False, required=False,
help='also diff URL hostname', action='store_true', dest='diff_hostname'
)
# argument --names
arg_parser.add_argument('--names', '-n', default=False, required=False,
help='only diff URL parameter names.', action='store_true', dest='names_only'
)
# argument --decode
arg_parser.add_argument('--decode', '-d', default=False, required=False,
help='URL decode parameter names and values (if applicable). Decoded params will be used '
'for comparison and printing.',
action='store_true', dest='decode_params'
)
# argument "left_url"
arg_parser.add_argument('left_url', type=str,
help='URL to diff against. Logically handled as the left argument of diff.',
metavar='<left URL>')
# argument "right_url"
arg_parser.add_argument('right_url', type=str,
help='URL to diff against. Logically handled as the right argument of diff.',
metavar='<right URL>', nargs='?', default=''
)
# argument --quiet
arg_parser.add_argument('--quiet', '-q', action='store_true',
help='suppress output and return non-zero if URLs differ.',
default=False, required=False
)
# argument --case_insensitive
arg_parser.add_argument('--case_insensitive', '-i', action='store_true',
help='Perform case insensitive diff. NOTE: this converts all input to lowercase.',
default=False, required=False
)
# parser argument
args = arg_parser.parse_args()
differ = UrlDiffer(args.left_url,
args.right_url,
names_only=args.names_only,
hostnames=args.diff_hostname,
url_decode_params=args.decode_params,
case_insensitive=args.case_insensitive
)
if not args.quiet:
sys.stdout.write('%s\n' % differ)
sys.exit(1 if differ.are_different() else 0)
if __name__ == '__main__':
launcher()