-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebcrystal.py
executable file
·1002 lines (813 loc) · 34.3 KB
/
webcrystal.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
webcrystal is:
1. An HTTP proxy and web service that saves every web page accessed through it to disk.
2. An on-disk archival format for storing websites.
webcrystal is intended as a tool for archiving websites.
See the README for more information.
"""
import argparse
import atexit
from collections import namedtuple, OrderedDict
import html
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
import json
import os.path
import re
import shutil
from socketserver import ThreadingMixIn
import sys
from threading import Lock
if not (sys.version_info >= (3, 4)):
raise ImportError('webcrystal requires Python 3.4 or later.')
try:
import urllib3
except ImportError:
raise ImportError('webcrystal requires urllib3. Try: pip3 install urllib3')
# Use PyOpenSSL if it is available.
#
# This allows most HTTPS connections to succeed on operating systems
# like OS X 10.11 that ship with such old versions of OpenSSL that most
# HTTPS connections are dropped.
try:
# Requires: pip3 install pyopenssl ndg-httpsclient pyasn1
import urllib3.contrib.pyopenssl
except ImportError:
pass
else:
urllib3.contrib.pyopenssl.inject_into_urllib3()
# Force HTTPS certificate validation with certifi if it is available.
try:
import certifi
except ImportError:
_pool_manager_kwargs = dict()
else:
_pool_manager_kwargs = dict(
cert_reqs='CERT_REQUIRED', # Force certificate check.
ca_certs=certifi.where(), # Path to the Certifi bundle.
)
# ==============================================================================
# Service
_http = urllib3.PoolManager(retries=0, **_pool_manager_kwargs)
def main(raw_cli_args):
# Parse arguments
parser = argparse.ArgumentParser(
description='An archiving HTTP proxy and web service.',
add_help=False)
parser.add_argument('-h', '--help', action='help',
help='Show this help message and exit.')
parser.add_argument('-q', '--quiet', action='store_true', dest='is_quiet',
help='Suppresses all output.')
parser.add_argument('port', type=int,
help='Port on which to run the HTTP proxy. Suggest 9227 (WBCR).')
parser.add_argument('archive_dirpath',
help='Path to the archive directory. Usually has .wbcr extension.')
parser.add_argument('default_origin_domain', nargs='?', type=_domain,
help='Default HTTP domain which the HTTP proxy will redirect to if no URL is specified.')
cli_args = parser.parse_args(raw_cli_args)
proxy_info = _ProxyInfo(host='127.0.0.1', port=cli_args.port)
# Open archive
archive = HttpResourceArchive(cli_args.archive_dirpath)
try:
atexit.register(lambda: archive.close()) # last resort
# ProxyState -- is mutable and threadsafe
proxy_state = {
'is_online': True
}
def create_request_handler(*args):
return _ArchivingHTTPRequestHandler(*args,
archive=archive,
proxy_info=proxy_info,
default_origin_domain=cli_args.default_origin_domain,
is_quiet=cli_args.is_quiet,
proxy_state=proxy_state)
# Run service until user presses ^C
if not cli_args.is_quiet:
print('Listening on %s:%s' % (proxy_info.host, proxy_info.port))
httpd = _ThreadedHttpServer(
(proxy_info.host, proxy_info.port),
create_request_handler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
finally:
httpd.server_close()
finally:
archive.close()
def _domain(domain_descriptor):
m = re.search(r'^(?:(https?)://)?([^/]+)/?$', domain_descriptor)
if m is None:
raise argparse.ArgumentTypeError(
'%r must look like %r or %r' %
(domain_descriptor, 'xkcd.com', 'http://xkcd.com/'))
(protocol, domain) = m.groups()
if protocol == 'https':
raise argparse.ArgumentTypeError(
'The %r protocol is not supported for the default origin domain. Try %r instead.' %
('https', 'http'))
return domain
_ProxyInfo = namedtuple('_ProxyInfo', ['host', 'port'])
class _ThreadedHttpServer(ThreadingMixIn, HTTPServer):
pass
class _ArchivingHTTPRequestHandler(BaseHTTPRequestHandler):
"""
HTTP request handler that serves requests from an HttpResourceArchive.
When a resource is requested that isn't in the archive, it will be added
to the archive automatically.
"""
def __init__(self, *args, archive, proxy_info, default_origin_domain, is_quiet, proxy_state):
self._archive = archive
self._proxy_info = proxy_info
self._default_origin_domain = default_origin_domain
self._is_quiet = is_quiet
self._proxy_state = proxy_state
super().__init__(*args)
def do_HEAD(self):
f = self._send_head(method='HEAD')
f.close()
def do_GET(self):
f = self._send_head(method='GET')
try:
shutil.copyfileobj(f, self.wfile)
finally:
f.close()
def do_POST(self):
f = self._send_head(method='POST')
try:
shutil.copyfileobj(f, self.wfile)
finally:
f.close()
def _send_head(self, *, method):
try:
if self.path.startswith('/_') and not self.path.startswith('/_/'):
return self._send_head_for_special_request(method=method)
else:
return self._send_head_for_regular_request(method=method)
except Exception as e:
# Annotate exception with offending URL and method
raise Exception('Problem while serving %s of %s: %s' % (method, self.path, e)) from e
def _send_head_for_special_request(self, *, method):
if self.path == '/_online':
if method not in ['POST', 'GET']:
return self._send_head_for_simple_response(405) # Method Not Allowed
self._proxy_state['is_online'] = True
self.send_response(200) # OK
self.send_header('Content-Type', 'text/plain')
self.end_headers()
return BytesIO(b'OK')
elif self.path == '/_offline':
if method not in ['POST', 'GET']:
return self._send_head_for_simple_response(405) # Method Not Allowed
self._proxy_state['is_online'] = False
self.send_response(200) # OK
self.send_header('Content-Type', 'text/plain')
self.end_headers()
return BytesIO(b'OK')
elif self.path.startswith('/_raw/'):
if method not in ['GET', 'HEAD']:
return self._send_head_for_simple_response(405) # Method Not Allowed
parsed_request_url = _try_parse_client_request_path(self.path, self._default_origin_domain)
assert parsed_request_url is not None
request_url = '%s://%s%s' % (
parsed_request_url.protocol,
parsed_request_url.domain,
parsed_request_url.path
)
resource = self._archive.get(request_url)
if resource is None:
self.send_response(503) # Service Unavailable
self.send_header('Content-Type', 'text/html')
self.end_headers()
return BytesIO(
(('<html>Resource <a href="%s">%s</a> is not archived</html>') %
(html.escape(request_url), html.escape(request_url))
).encode('utf8')
)
else:
return self._send_head_for_resource(resource, filter=False)
elif self.path.startswith('/_delete/'):
if method not in ['POST', 'GET']:
return self._send_head_for_simple_response(405) # Method Not Allowed
parsed_request_url = _try_parse_client_request_path(self.path, self._default_origin_domain)
assert parsed_request_url is not None
request_url = '%s://%s%s' % (
parsed_request_url.protocol,
parsed_request_url.domain,
parsed_request_url.path
)
did_exist = self._archive.delete(request_url)
if did_exist:
return self._send_head_for_simple_response(200) # OK
else:
return self._send_head_for_simple_response(404) # Not Found
elif self.path.startswith('/_refresh/'):
if method not in ['POST', 'GET']:
return self._send_head_for_simple_response(405) # Method Not Allowed
parsed_request_url = _try_parse_client_request_path(self.path, self._default_origin_domain)
assert parsed_request_url is not None
request_url = '%s://%s%s' % (
parsed_request_url.protocol,
parsed_request_url.domain,
parsed_request_url.path
)
request_headers = self._archive.get_request_headers(request_url)
if request_headers is None:
return self._send_head_for_simple_response(404) # Not Found
try:
resource = self._fetch_from_origin_and_store_in_archive(
request_url, request_headers,
parsed_request_url=parsed_request_url)
except _OriginServerError as e:
return self._send_head_for_origin_server_error(e)
else:
resource.content.close()
return self._send_head_for_simple_response(200) # OK
else:
return self._send_head_for_simple_response(400) # Bad Request
def _send_head_for_regular_request(self, *, method):
if method not in ['GET', 'HEAD']:
return self._send_head_for_simple_response(405) # Method Not Allowed
canonical_request_headers = {k.lower(): v for (k, v) in self.headers.items()} # cache
parsed_request_url = _try_parse_client_request_path(self.path, self._default_origin_domain)
if parsed_request_url is None:
return self._send_head_for_simple_response(400) # Bad Request
assert parsed_request_url.command == '_'
request_referer = canonical_request_headers.get('referer')
parsed_referer = \
None if request_referer is None \
else _try_parse_client_referer(request_referer, self._default_origin_domain)
# Received a request at a site-relative path?
# Redirect to a fully qualified proxy path at the appropriate domain.
if not parsed_request_url.is_proxy:
if parsed_referer is not None and parsed_referer.is_proxy:
# Referer exists and is from the proxy?
# Redirect to the referer domain.
redirect_url = _format_proxy_url(
protocol=parsed_request_url.protocol,
domain=parsed_referer.domain,
path=parsed_request_url.path,
proxy_info=self._proxy_info
)
is_permanent = True
else:
if parsed_request_url.domain is None:
return self._send_head_for_simple_response(404) # Not Found
# No referer exists (or it's an unexpected external referer)?
# Redirect to the default origin domain.
redirect_url = _format_proxy_url(
protocol=parsed_request_url.protocol,
domain=parsed_request_url.domain,
path=parsed_request_url.path,
proxy_info=self._proxy_info
)
is_permanent = False # temporary because the default origin domain can change
self.send_response(308 if is_permanent else 307) # Permanent Redirect, Temporary Redirect
self.send_header('Location', redirect_url)
self.send_header('Vary', 'Referer')
self.end_headers()
return BytesIO(b'')
assert parsed_request_url.domain is not None
request_url = '%s://%s%s' % (
parsed_request_url.protocol,
parsed_request_url.domain,
parsed_request_url.path
)
# If client performs a hard refresh (Command-Shift-R in Chrome),
# ignore any archived response and refetch a fresh resource from the origin server.
request_cache_control = canonical_request_headers.get('cache-control')
request_pragma = canonical_request_headers.get('pragma')
should_disable_cache = (
(request_cache_control is not None and
# HACK: fuzzy match
'no-cache' in request_cache_control) or
(request_pragma is not None and
# HACK: fuzzy match
'no-cache' in request_pragma)
)
# Try fetch requested resource from archive.
if should_disable_cache:
resource = None
else:
resource = self._archive.get(request_url)
# If missing fetch the resource from the origin and add it to the archive.
if resource is None:
# Fail if in offline mode
if not self._proxy_state['is_online']:
self.send_response(503) # Service Unavailable
self.send_header('Content-Type', 'text/html')
self.end_headers()
return BytesIO(
(('<html>Resource <a href="%s">%s</a> is not archived, ' +
'and this proxy is in offline mode. Go <a href="/_online">online</a>?</html>') %
(html.escape(request_url), html.escape(request_url))
).encode('utf8')
)
try:
resource = self._fetch_from_origin_and_store_in_archive(
request_url,
self.headers,
parsed_request_url=parsed_request_url)
except _OriginServerError as e:
return self._send_head_for_origin_server_error(e)
return self._send_head_for_resource(resource)
def _fetch_from_origin_and_store_in_archive(
self, request_url, request_headers, *, parsed_request_url):
request_headers = OrderedDict(request_headers) # clone
# Set Host request header appropriately
_del_headers(request_headers, ['Host'])
request_headers['Host'] = parsed_request_url.domain
# Filter request headers before sending to origin server
_filter_headers(request_headers, 'request header', is_quiet=self._is_quiet)
_reformat_absolute_urls_in_headers(
request_headers,
proxy_info=self._proxy_info,
default_origin_domain=self._default_origin_domain)
try:
response = _http.request(
method='GET',
url=request_url,
headers=request_headers,
redirect=False
)
except Exception as e:
raise _OriginServerError(request_url) from e
# NOTE: Not streaming the response at the moment for simplicity.
# Probably want to use iter_content() later.
response_content_bytes = response.data
response_headers = OrderedDict(response.headers) # clone
_del_headers(response_headers, ['Content-Length', 'Content-Encoding'])
response_headers['Content-Length'] = str(len(response_content_bytes))
response_headers['X-Status-Code'] = str(response.status)
response_content = BytesIO(response_content_bytes)
try:
self._archive.put(request_url, request_headers, HttpResource(
headers=response_headers,
content=response_content
))
finally:
response_content.close()
resource = self._archive.get(request_url)
assert resource is not None
return resource
def _send_head_for_resource(self, resource, *, filter=True):
status_code = int(resource.headers['X-Status-Code'])
resource_headers = OrderedDict(resource.headers) # clone
resource_content = resource.content
if filter:
# Filter response headers before sending to client
_filter_headers(resource_headers, 'response header', is_quiet=self._is_quiet)
_reformat_absolute_urls_in_headers(
resource_headers,
proxy_info=self._proxy_info,
default_origin_domain=self._default_origin_domain)
# Filter response content before sending to client
(resource_headers, resource_content) = _reformat_absolute_urls_in_content(
resource_headers, resource_content,
proxy_info=self._proxy_info)
else:
# Minimal filtering: Remove only the internal response headers
for hn in list(resource_headers.keys()):
if hn.lower() in _RAW_RESPONSE_HEADER_BLACKLIST:
del resource_headers[hn]
# Send headers
self.send_response(status_code)
for (key, value) in resource_headers.items():
self.send_header(key, value)
self.end_headers()
return resource_content
def _send_head_for_simple_response(self, status_code):
self.send_response(status_code)
self.end_headers()
return BytesIO(b'')
def _send_head_for_origin_server_error(self, e):
f = e.__cause__
self.send_response(502) # Bad Gateway
self.send_header('Content-Type', 'text/html')
self.end_headers()
return BytesIO(
(('<html>Error while fetching resource <a href="%s">%s</a>.' +
'<pre>%s: %s</pre></html>') %
(html.escape(e.request_url), html.escape(e.request_url),
html.escape(type(f).__name__), html.escape(str(f)))
).encode('utf8')
)
def log_message(self, *args):
if self._is_quiet:
pass # operate silently
else:
super().log_message(*args)
def _del_headers(headers, header_names_to_delete):
header_names_to_delete = [hn.lower() for hn in header_names_to_delete]
for key in list(headers.keys()):
if key.lower() in header_names_to_delete:
del headers[key]
class _OriginServerError(Exception):
def __init__(self, request_url):
self.request_url = request_url
# ------------------------------------------------------------------------------
# Filter Headers
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Filter Header Keys
_REQUEST_HEADER_WHITELIST = [
# Request
'accept',
'accept-encoding',
'accept-language',
'cookie',
'host',
'referer',
'user-agent',
]
_RESPONSE_HEADER_WHITELIST = [
# Response
'access-control-allow-origin',
'access-control-allow-credentials',
'age',
'content-length',
'content-type',
'date',
'etag',
'expires',
'last-modified',
'location',
'retry-after',
'server',
'set-cookie',
'via',
'x-content-type-options',
'x-frame-options',
'x-runtime',
'x-served-by',
'x-xss-protection',
]
_HEADER_WHITELIST = (
_REQUEST_HEADER_WHITELIST +
_RESPONSE_HEADER_WHITELIST
)
_REQUEST_HEADER_BLACKLIST = [
# Request
'cache-control',
'connection',
'if-modified-since',
'if-none-match',
'pragma',
'upgrade-insecure-requests',
'x-pragma',
]
_RESPONSE_HEADER_BLACKLIST = [
# Response
'accept-ranges',
'cache-control',
'connection',
'strict-transport-security',
'transfer-encoding',
'vary',
'x-cache',
'x-cache-hits',
'x-request-id',
'x-served-time',
'x-timer',
]
_INTERNAL_RESPONSE_HEADERS = [
# Internal
'x-status-code',
]
_HEADER_BLACKLIST = (
_REQUEST_HEADER_BLACKLIST +
_RESPONSE_HEADER_BLACKLIST +
_INTERNAL_RESPONSE_HEADERS
)
_RAW_RESPONSE_HEADER_BLACKLIST = (
['connection', 'transfer-encoding'] +
_INTERNAL_RESPONSE_HEADERS
)
# TODO: Should differentiate between request & response headers.
def _filter_headers(headers, header_type_title, *, is_quiet):
for k in list(headers.keys()):
k_lower = k.lower()
if k_lower in _HEADER_WHITELIST:
pass
elif k_lower in _HEADER_BLACKLIST:
del headers[k]
else: # graylist
if not is_quiet:
print(' - Removing unrecognized %s: %s' % (header_type_title, k))
del headers[k]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Filter Header URLs
def _reformat_absolute_urls_in_headers(headers, *, proxy_info, default_origin_domain):
for k in list(headers.keys()):
if k.lower() == 'location':
parsed_url = _try_parse_absolute_url(headers[k])
if parsed_url is not None:
headers[k] = _format_proxy_url(
protocol=parsed_url.protocol,
domain=parsed_url.domain,
path=parsed_url.path,
proxy_info=proxy_info,
)
elif k.lower() == 'referer':
referer = headers[k]
parsed_referer = _try_parse_client_referer(referer, default_origin_domain)
if parsed_referer is not None:
headers[k] = '%s://%s%s' % (
parsed_referer.protocol,
parsed_referer.domain,
parsed_referer.path
)
# ------------------------------------------------------------------------------
# Filter Content
_ABSOLUTE_URL_BYTES_IN_HTML_RE = re.compile(rb'([\'"])(https?://.*?)\1')
_PROTOCOL_RELATIVE_URL_BYTES_IN_HTML_RE = re.compile(rb'([\'"])(//.*?)\1')
def _reformat_absolute_urls_in_content(resource_headers, resource_content, *, proxy_info):
"""
If specified resource is an HTML document, replaces any obvious absolute
URL references with references of the format "/_/http/..." that will be
interpreted by the archiving proxy appropriately.
Otherwise returns the original content unmodified.
"""
is_html = False
for (k, v) in resource_headers.items():
if k.lower() == 'content-type':
is_html = 'text/html' in v # HACK: Loose test
break
if not is_html:
return (resource_headers, resource_content)
try:
content_bytes = resource_content.read()
finally:
resource_content.close()
def reformat_absolute_url_match(match_in_html):
nonlocal proxy_info
(quote, url) = match_in_html.groups()
parsed_url = _try_parse_absolute_url_in_bytes(url)
assert parsed_url is not None # inner regex should be subset of outer
return quote + _format_proxy_url_in_bytes(
protocol=parsed_url.protocol,
domain=parsed_url.domain,
path=parsed_url.path,
proxy_info=proxy_info
) + quote
content_bytes = _ABSOLUTE_URL_BYTES_IN_HTML_RE.sub(reformat_absolute_url_match, content_bytes)
def reformat_protocol_relative_url_match(match_in_html):
nonlocal proxy_info
(quote, url) = match_in_html.groups()
parsed_url = _try_parse_protocol_relative_url_in_bytes(url, protocol=b'http')
assert parsed_url is not None # inner regex should be subset of outer
return quote + _format_proxy_url_in_bytes(
protocol=parsed_url.protocol,
domain=parsed_url.domain,
path=parsed_url.path,
proxy_info=proxy_info
) + quote
content_bytes = _PROTOCOL_RELATIVE_URL_BYTES_IN_HTML_RE.sub(reformat_protocol_relative_url_match, content_bytes)
# Update Content-Length in the headers
assert 'Content-Encoding' not in resource_headers
_del_headers(resource_headers, ['Content-Length'])
resource_headers['Content-Length'] = str(len(content_bytes))
return (resource_headers, BytesIO(content_bytes))
# ------------------------------------------------------------------------------
# Parse URLs
_ABSOLUTE_REQUEST_URL_RE = re.compile(r'^/(_[^/]*)/(https?)/([^/]+)(/.*)$')
_ClientRequestUrl = namedtuple('_ClientRequestUrl',
['protocol', 'domain', 'path', 'is_proxy', 'command'])
def _try_parse_client_request_path(path, default_origin_domain):
if path.startswith('/_'):
m = _ABSOLUTE_REQUEST_URL_RE.match(path)
if m is None:
return None
(command, protocol, domain, path) = m.groups()
return _ClientRequestUrl(
protocol=protocol,
domain=domain,
path=path,
is_proxy=True,
command=command
)
else:
return _ClientRequestUrl(
protocol='http',
domain=default_origin_domain,
path=path,
is_proxy=False,
command='_'
)
_REFERER_LONG_RE = re.compile(r'^https?://[^/]*/_/(https?)/([^/]*)(/.*)?$')
_REFERER_SHORT_RE = re.compile(r'^(https?)://[^/]*(/.*)?$')
_ClientReferer = namedtuple('_ClientReferer',
['protocol', 'domain', 'path', 'is_proxy'])
def _try_parse_client_referer(referer, default_origin_domain):
m = _REFERER_LONG_RE.match(referer)
if m is not None:
(protocol, domain, path) = m.groups()
if path is None:
path = ''
return _ClientReferer(
protocol=protocol,
domain=domain,
path=path,
is_proxy=True
)
m = _REFERER_SHORT_RE.match(referer)
if m is not None:
(protocol, path) = m.groups()
if path is None:
path = ''
return _ClientReferer(
protocol=protocol,
domain=default_origin_domain,
path=path,
is_proxy=False
)
return None # failed to parse header
_Url = namedtuple('_Url', ['protocol', 'domain', 'path'])
_ABSOLUTE_URL_RE = re.compile(r'^(https?)://([^/]*)(/.*)?$')
def _try_parse_absolute_url(url):
url_match = _ABSOLUTE_URL_RE.match(url)
if url_match is None:
return None
(protocol, domain, path) = url_match.groups()
if path is None:
path = ''
return _Url(
protocol=protocol,
domain=domain,
path=path
)
_ABSOLUTE_URL_BYTES_RE = re.compile(rb'^(https?)://([^/]*)(/.*)?$')
def _try_parse_absolute_url_in_bytes(url):
url_match = _ABSOLUTE_URL_BYTES_RE.match(url)
if url_match is None:
return None
(protocol, domain, path) = url_match.groups()
if path is None:
path = b''
return _Url(
protocol=protocol,
domain=domain,
path=path
)
_PROTOCOL_RELATIVE_URL_BYTES_RE = re.compile(rb'^//([^/]*)(/.*)?$')
def _try_parse_protocol_relative_url_in_bytes(url, *, protocol):
url_match = _PROTOCOL_RELATIVE_URL_BYTES_RE.match(url)
if url_match is None:
return None
(domain, path) = url_match.groups()
if path is None:
path = b''
return _Url(
protocol=protocol,
domain=domain,
path=path
)
def _format_proxy_path(protocol, domain, path, *, command='_'):
return '/%s/%s/%s%s' % (
command, protocol, domain, path)
def _format_proxy_url(protocol, domain, path, *, proxy_info):
return 'http://%s:%s%s' % (
proxy_info.host, proxy_info.port, _format_proxy_path(protocol, domain, path))
def _format_proxy_url_in_bytes(protocol, domain, path, *, proxy_info):
(proxy_host, proxy_port) = (proxy_info.host.encode('utf8'), str(proxy_info.port).encode('utf8'))
# TODO: After upgrading to Python 3.5+, replace the following code with:
# percent-substitution syntax like b'/_/%b/%b%b' % (protocol, domain, path
return b'http://' + proxy_host + b':' + proxy_port + b'/_/' + protocol + b'/' + domain + path
# ==============================================================================
# Archive
HttpResource = namedtuple('HttpResource', ['headers', 'content'])
class HttpResourceArchive:
"""
Persistent archive of HTTP resources, include the full content and headers of
each resource.
This class is threadsafe.
"""
def __init__(self, root_dirpath):
"""
Opens the existing archive at the specified directory,
or creates a new archive if there is no such directory.
"""
self._closed = False
self._lock = Lock()
self._root_dirpath = root_dirpath
# Create empty archive if archive does not already exist
if not os.path.exists(root_dirpath):
os.mkdir(root_dirpath)
with self._open_index('w') as f:
f.write('')
# Load archive
with self._open_index('r') as f:
self._urls = f.read().split('\n')
if self._urls == ['']:
self._urls = []
# NOTE: It is possible for the archive to contain multiple IDs for the
# same path under rare circumstances. In that case the last ID wins.
self._resource_id_for_url = {url: i for (i, url) in enumerate(self._urls)}
def get(self, url):
"""
Gets the HttpResource at the specified url from this archive,
or None if the specified resource is not in the archive.
"""
with self._lock:
resource_id = self._resource_id_for_url.get(url)
if resource_id is None:
return None
with self._open_response_headers(resource_id, 'r') as f:
headers = json.load(f, object_pairs_hook=OrderedDict)
f = self._open_response_content(resource_id, 'rb')
return HttpResource(
headers=headers,
content=f,
)
def get_request_headers(self, url):
"""
Gets the request headers for the resource at the specified url from this archive,
or None if the specified resource is not in the archive.
"""
with self._lock:
resource_id = self._resource_id_for_url.get(url)
if resource_id is None:
return None
with self._open_request_headers(resource_id, 'r') as f:
return json.load(f, object_pairs_hook=OrderedDict)
def put(self, url, request_headers, resource):
"""
Puts the specified HttpResource into this archive, replacing any previous
resource with the same url.
If two difference resources are put into this archive at the same url
concurrently, the last one put into the archive will eventually win.
"""
# Reserve resource ID (if new)
with self._lock:
resource_id = self._resource_id_for_url.get(url)
if resource_id is None:
resource_id = len(self._urls)
self._urls.append('') # reserve space
resource_id_is_new = True
else:
resource_id_is_new = False
# Write resource content
with self._open_request_headers(resource_id, 'w') as f:
json.dump(request_headers, f)
with self._open_response_headers(resource_id, 'w') as f:
json.dump(resource.headers, f)
with self._open_response_content(resource_id, 'wb') as f:
shutil.copyfileobj(resource.content, f)
# Commit resource ID (if new)
if resource_id_is_new:
# NOTE: Only commit an entry to self._urls AFTER the resource
# content has been written to disk successfully.
with self._lock:
self._urls[resource_id] = url
old_resource_id = self._resource_id_for_url.get(url)
if old_resource_id is None or old_resource_id < resource_id:
self._resource_id_for_url[url] = resource_id
def delete(self, url):
"""
Deletes the specified resource from this archive if it exists.
Returns whether the specified resource was found and deleted.
"""
with self._lock:
resource_id = self._resource_id_for_url.get(url)
if resource_id is None:
return False
else:
self._delete_resource(resource_id)
self._urls[resource_id] = ''
del self._resource_id_for_url[url]
return True
def flush(self):
"""
Flushes all pending changes made to this archive to disk.
"""
# TODO: Make this operation atomic, even if the write fails in the middle.
with self._open_index('w') as f:
f.write('\n'.join(self._urls))
def close(self):
"""
Closes this archive.
"""
if self._closed:
return # pragma: no cover: unreachable by tests
self.flush()
self._closed = True
# === Filesystem I/O ===
def _open_index(self, mode='r'):
return open(os.path.join(self._root_dirpath, 'index.txt'), mode, encoding='utf8')
def _open_request_headers(self, resource_id, mode='r'):
resource_ordinal = resource_id + 1
return open(os.path.join(self._root_dirpath, '%d.request_headers.json' % resource_ordinal), mode, encoding='utf8')
def _open_response_headers(self, resource_id, mode='r'):
resource_ordinal = resource_id + 1
return open(os.path.join(self._root_dirpath, '%d.response_headers.json' % resource_ordinal), mode, encoding='utf8')
def _open_response_content(self, resource_id, mode='rb'):
resource_ordinal = resource_id + 1
return open(os.path.join(self._root_dirpath, '%d.response_body.dat' % resource_ordinal), mode)
def _delete_resource(self, resource_id):
resource_ordinal = resource_id + 1
os.remove(os.path.join(self._root_dirpath, '%d.request_headers.json' % resource_ordinal))
os.remove(os.path.join(self._root_dirpath, '%d.response_headers.json' % resource_ordinal))
os.remove(os.path.join(self._root_dirpath, '%d.response_body.dat' % resource_ordinal))
# ------------------------------------------------------------------------------