This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paraproxio.py
executable file
·1429 lines (1156 loc) · 53 KB
/
paraproxio.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 python
"""
Paraproxio is an HTTP proxy with a parallel downloading of big files.
"""
# Copyright (C) 2016 Alexander Logger
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__version__ = '1.0'
PARAPROXIO_VERSION = "Paraproxio/" + __version__
import sys
req_ver = (3, 5)
if sys.version_info < req_ver:
print('Minimum Python/{0[0]}.{0[1]} required. You run this script with Python/{1[0]}.{1[1]}.'.format(
req_ver, sys.version_info),
file=sys.stderr)
exit(1)
import asyncio
import logging
import os
import shutil
import argparse
import concurrent.futures
import json
import traceback
import socket
import http.server
import threading
from time import time
from asyncio import AbstractEventLoop, Future, ensure_future, wait, wait_for, TimeoutError
from asyncio.futures import CancelledError
from typing import Tuple, Callable, Optional, List, Set, Dict, Any
from urllib.parse import urlparse, ParseResult
from math import ceil
from html import escape as html_escape
try:
import aiohttp
import aiohttp.hdrs as hdrs
import aiohttp.server
from aiohttp.multidict import CIMultiDictProxy
from aiohttp.protocol import RawRequestMessage
from aiohttp.streams import EmptyStreamReader
except ImportError as err:
print(
"Required module '{0}' not found. Try to run 'pip install {0}' to install it.".format(err.name),
file=sys.stderr)
exit(1)
if hasattr(socket, 'SO_KEEPALIVE'):
def tcp_keepalive(server, transport):
sock = transport.get_extra_info('socket')
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
else:
def tcp_keepalive(server, transport): # pragma: no cover
pass
DEFAULT_ERROR_MESSAGE = """
<html>
<head>
<title>{status} {reason}</title>
</head>
<body>
<h1>{status} {reason}</h1>
{message}
</body>
</html>"""
EMPTY_PAYLOAD = aiohttp.streams.EmptyStreamReader()
RESPONSES = http.server.BaseHTTPRequestHandler.responses
DEFAULT_HOST = '127.0.0.1'
DEFAULT_PORT = 8880
DEFAULT_CHUNK_DOWNLOAD_TIMEOUT = 10
DEFAULT_CHUNK_SIZE = 64 * 1024
DEFAULT_PARALLELS = 32
DEFAULT_MAX_WORKERS = 8
DEFAULT_PART_SIZE = DEFAULT_CHUNK_SIZE * DEFAULT_PARALLELS * DEFAULT_MAX_WORKERS
DEFAULT_WORKING_DIR = '.paraproxio'
DEFAULT_BUFFER_DIR = os.path.join(DEFAULT_WORKING_DIR, 'buffer')
DEFAULT_CACHE_DIR = os.path.join(DEFAULT_WORKING_DIR, 'cache')
DEFAULT_LOGS_DIR = os.path.join(DEFAULT_WORKING_DIR, 'logs')
DEFAULT_SERVER_LOG_FILENAME = 'paraproxio.server.log'
DEFAULT_ACCESS_LOG_FILENAME = 'paraproxio.access.log'
DEFAULT_PARACCESS_LOG_FORMAT = '%a %l %u %t "%r" %s %b "%{Referrer}i" "%{User-Agent}i" %{X-Parallels}o'
PARALLELS_HEADER = 'X-Parallels' # Used in responses. Value: number of parallel downloads used.
server_logger = logging.getLogger('paraproxio.server')
access_logger = logging.getLogger('paraproxio.access')
NOT_STARTED = 'NOT STARTED'
DOWNLOADING = 'DOWNLOADING'
DOWNLOADED = 'DOWNLOADED'
CANCELLED = 'CANCELLED'
_DOWNLOADER_STATES = {NOT_STARTED, DOWNLOADING, DOWNLOADED, CANCELLED}
files_to_parallel = ['.iso', '.zip', '.rpm', '.gz']
CACHE_INFO_FILE_NAME = 'info.json'
CACHE_BIN_FILE_NAME = 'file.bin'
CACHE_LAST_ACCESS_FILE_NAME = 'last-access'
DEFAULT_CACHE_MAX_AGE = 60 * 60 * 24 * 3 # 3 days in seconds
DEFAULT_CACHE_CLEANUP_INTERVAL = 60 * 10 # clean-up every 10 minutes
def need_file_to_parallel(url: str) -> bool:
pr = urlparse(url) # type: ParseResult
url, ext = os.path.splitext(pr.path)
return ext.lower() in files_to_parallel
def get_bytes_ranges_by_parts(length: int, parts: int) -> List[Tuple[int, int]]:
""" Get bytes ranges """
###################################################################################################
#
# length = 89
# parts = 5
# range_size = length // parts = 89 // 5 = 17
# last_range_size = range_size + length % parts = 17 + 89 % 5 = 17 + 4 = 21
#
# [<--range_size--->|<--range_size--->|<--range_size--->|<--range_size--->|<--last_range_size--->|
# [*****************|*****************|*****************|*****************|*****************|****]
# 0 17 34 51 68 85 89
#
###################################################################################################
range_size = length // parts
last_range_size = range_size + length % parts
last_range_idx = parts - 1
bytes_ranges = []
for part in range(0, last_range_idx):
bytes_range = (part * range_size, ((part + 1) * range_size) - 1)
bytes_ranges.append(bytes_range)
last_range_offset = last_range_idx * range_size
bytes_ranges.append((last_range_offset, last_range_offset + last_range_size - 1))
return bytes_ranges
def get_bytes_ranges_by_part_size(length: int, part_size: int) -> List[Tuple[int, int]]:
bytes_ranges = []
for offset in range(0, length, part_size):
left = length - offset
if left < part_size:
part_size = left
bytes_ranges.append((offset, offset + part_size - 1))
return bytes_ranges
def get_unique_name():
return str(time()).replace('.', '_')
class RangeDownloader:
def __init__(
self,
url: str,
bytes_range: Tuple[int, int],
buffer_file_path,
*,
loop: AbstractEventLoop = None,
server_logger=server_logger,
chunk_size=DEFAULT_CHUNK_SIZE,
chunk_download_timeout=DEFAULT_CHUNK_DOWNLOAD_TIMEOUT):
self._url = url
self._bytes_range = bytes_range
self._length = bytes_range[1] - bytes_range[0] + 1
self._buffer_file_path = buffer_file_path
self._loop = loop if loop is not None else asyncio.get_event_loop()
self._server_logger = server_logger
self._chunk_size = chunk_size
self._chunk_download_timeout = chunk_download_timeout
self._headers = {'Range': 'bytes={0[0]!s}-{0[1]!s}'.format(self._bytes_range)}
self._bytes_downloaded = 0
self._state = NOT_STARTED
@property
def state(self) -> str:
return self._state
@property
def bytes_downloaded(self):
return self._bytes_downloaded
@property
def buffer_file_path(self):
return self._buffer_file_path
async def download(self) -> str:
if self._state != NOT_STARTED:
return self._state
# Prepare an empty buffer file.
await self._loop.run_in_executor(None, self._create_buffer_file)
try:
# Create client session for downloading a file part from a host.
async with aiohttp.ClientSession(loop=self._loop, headers=self._headers) as session:
# Request a host for a file part.
async with session.request('GET', self._url) as res: # type: aiohttp.ClientResponse
if res.status != 206:
raise WrongResponseError('Expected status code 206, but {!s} ({!s}) received.',
res.status,
res.reason)
hrh = res.headers # type: CIMultiDictProxy
# TODO: check headers.
# Read content by chunks and write to the buffer file.
if self._state == NOT_STARTED:
self._state = DOWNLOADING
while self._state is DOWNLOADING:
with aiohttp.Timeout(self._chunk_download_timeout, loop=self._loop):
chunk = await res.content.read(self._chunk_size)
self._bytes_downloaded += len(chunk)
self._debug("Read ({!s} bytes). Downloaded: {!s} of {!s} bytes. [{:.2%}]".format(
len(chunk), self._bytes_downloaded, self._length,
self._bytes_downloaded / self._length))
if not chunk:
self._state = DOWNLOADED
break
await self._write_chunk(chunk)
await self._flush_and_release()
if self._state != DOWNLOADED:
res.close() # Close the response if not downloaded.
except aiohttp.ServerDisconnectedError as exc:
self._debug('Server disconnected error: {!r}.'.format(exc))
self.cancel()
except WrongResponseError as exc:
self._debug('Wrong response error: {!r}.'.format(exc))
self.cancel()
except asyncio.TimeoutError:
self._debug('Timeout.')
self.cancel()
except Exception as exc:
self._debug('Unexpected exception: {!r}.'.format(exc))
self.cancel()
finally:
return self._state
def cancel(self):
self._debug('Cancel called.')
self._state = CANCELLED
async def _write_chunk(self, chunk):
await self._run_nonblocking(lambda: self._buffer_file.write(chunk))
def _flush_and_release(self):
def flush_and_release():
if not self._buffer_file:
return
self._buffer_file.flush()
self._buffer_file.close()
del self._buffer_file
return self._run_nonblocking(flush_and_release)
def _run_nonblocking(self, func):
return self._loop.run_in_executor(None, lambda: func())
def _create_buffer_file(self):
f = open(self._buffer_file_path, 'xb')
f.seek(self._length - 1)
f.write(b'0')
f.flush()
f.seek(0)
self._buffer_file = f
def _debug(self, msg, *args, **kwargs):
msg = "{!r} {!s}".format(self, msg)
self._server_logger.debug(msg, *args, **kwargs)
def __repr__(self, *args, **kwargs):
return '<RangeDownloader ({2!s}): [{0[0]!s}-{0[1]!s}] {1!r}>'.format(self._bytes_range, self._url, self._state)
class ParallelDownloader:
"""Parallel downloader"""
_state = NOT_STARTED
_downloaders = [] # type: List[RangeDownloader]
_downloads = set() # type: Set[Future]
_filename = None # type: str
_next_id = 0
def __init__(
self,
url: str,
file_length: int,
*,
parallels: int = DEFAULT_PARALLELS,
part_size: int = DEFAULT_PART_SIZE,
chunk_size: int = DEFAULT_CHUNK_SIZE,
loop: AbstractEventLoop = None,
server_logger=server_logger,
buffer_dir: str = DEFAULT_BUFFER_DIR):
assert parallels > 1
self._url = url
self._file_length = file_length
self._part_size = part_size
self._parallels = parallels
self._chunk_size = chunk_size
self._loop = loop if loop is not None else asyncio.get_event_loop()
self._server_logger = server_logger
self._download_dir = os.path.join(buffer_dir, get_unique_name())
self._create_download_dir()
# Calculate bytes ranges.
self._bytes_ranges = get_bytes_ranges_by_part_size(self._file_length, self._part_size)
self._parts = len(self._bytes_ranges)
if self._parts < self._parallels:
self._bytes_ranges = get_bytes_ranges_by_parts(self._file_length, self._parallels)
self._parts = len(self._bytes_ranges)
self._state_condition = asyncio.Condition(loop=self._loop)
@property
def state(self) -> str:
return self._state
@property
def downloaders(self):
return self._downloaders
async def download(self):
if self._state == DOWNLOADING:
return
self._state = DOWNLOADING
# Create a downloader for each bytes range.
for i, bytes_range in enumerate(self._bytes_ranges):
filename = '{idx:03}_{range[0]!s}-{range[1]!s}.tmp'.format(idx=i, range=bytes_range)
buffer_file_path = os.path.join(self._download_dir, filename)
self._downloaders.append(
RangeDownloader(self._url,
bytes_range,
buffer_file_path,
loop=self._loop,
chunk_size=self._chunk_size))
# Start first single downloader for fast first part response to a client.
self._start_next_downloaders(1)
# Waiting for all downloads to complete.
try:
while self._state is DOWNLOADING and self._downloads:
done, self._downloads = await wait(self._downloads, loop=self._loop,
return_when=asyncio.FIRST_COMPLETED)
for dd in done: # type: Future
# Cancel downloading if any of completed downloads is not downloaded.
if dd.result() is not DOWNLOADED:
raise CancelledError()
self._start_next_downloaders()
# Notify all readers.
async with self._state_condition:
self._state_condition.notify_all()
except Exception as ex:
self._debug('Download failed. Error: {!r}.'.format(ex))
self.cancel()
# Notify all readers.
async with self._state_condition:
self._state_condition.notify_all()
raise DownloadError(ex)
else:
# OK. All done.
self._state = DOWNLOADED
def _start_next_downloaders(self, n=None):
"""Start next downloaders if needed according with parallels count."""
if not n:
n = self._parallels - len(self._downloads)
while n > 0 and self._next_id < self._parts:
downloader = self._downloaders[self._next_id]
self._next_id += 1
self._downloads.add(ensure_future(downloader.download(), loop=self._loop))
n -= 1
async def read(self, callback: Callable[[bytearray], Any]):
try:
for downloader in self._downloaders:
# Wait until downloader is not in a downloaded/cancelled state.
async with self._state_condition:
while downloader.state not in (DOWNLOADED, CANCELLED):
await self._state_condition.wait()
if downloader.state != DOWNLOADED:
self._debug('Downloader not in `DOWNLOADED` state, but in `{!s}`.'.format(downloader.state))
raise CancelledError()
# Open file and send all its bytes it to back.
await read_from_file_by_chunks(downloader.buffer_file_path, callback, self._chunk_size,
lambda: self._state != CANCELLED, loop=self._loop)
except Exception as exc:
raise ReadError(exc)
def cancel(self):
if self._state != DOWNLOADING:
return
self._state = CANCELLED
for downloader in self._downloaders: # type: RangeDownloader
downloader.cancel()
async def clear(self):
if self._state not in (DOWNLOADED, CANCELLED):
return
await self._clear()
async def _clear(self):
self._downloaders.clear()
await self._run_nonblocking(lambda: shutil.rmtree(self._download_dir, ignore_errors=True))
async def _run_nonblocking(self, func):
return await self._loop.run_in_executor(None, lambda: func())
def _create_download_dir(self):
if not os.path.exists(self._download_dir):
os.makedirs(self._download_dir, exist_ok=True)
def _debug(self, msg, *args, **kwargs):
msg = "{!r} {!s}".format(self, msg)
self._server_logger.debug(msg, *args, **kwargs)
def __repr__(self, *args, **kwargs):
return '<ParallelDownloader: {!r}>'.format(self._url)
INITIALIZING = 'INITIALIZING'
READY = 'READY'
def get_cache_bin_file_path(cache_entry_dir: str):
return os.path.join(cache_entry_dir, CACHE_BIN_FILE_NAME)
def get_cache_info_file_path(cache_entry_dir: str):
return os.path.join(cache_entry_dir, CACHE_INFO_FILE_NAME)
def get_cache_last_access_file_path(cache_entry_dir: str):
return os.path.join(cache_entry_dir, CACHE_LAST_ACCESS_FILE_NAME)
def create_new_cache_entry_dir(cache_dir: str):
cache_entry_dir = os.path.join(cache_dir, get_unique_name())
os.makedirs(cache_entry_dir, exist_ok=True)
return cache_entry_dir
async def read_from_file_by_chunks(
file_path: str,
callback: Callable[[bytearray], None],
chunk_size: int = DEFAULT_CHUNK_SIZE,
condition: Callable[[], bool] = lambda: True,
*,
loop):
chunk = bytearray(chunk_size)
with open(file_path, 'rb') as f:
while condition():
r = await loop.run_in_executor(None, lambda: f.readinto(chunk))
if not r:
break
if r < chunk_size:
callback(memoryview(chunk)[:r].tobytes())
else:
callback(chunk)
class CachingDownloader:
"""Downloader with caching."""
_state = INITIALIZING
_downloadings = {} # type: Dict[str, Tuple[ParallelDownloader, Future, Future]]
_uploadings = set() # type: Set[(str, Future)]
_cache = {} # type: Dict[str, str]
_last_access_file_lock = threading.Lock()
def __init__(self,
cache_dir: str,
parallels,
part_size,
chunk_size,
loop,
server_logger=server_logger):
self._cache_dir = cache_dir
self._parallels = parallels
self._part_size = part_size
self._chunk_size = chunk_size
self._loop = loop if loop is not None else asyncio.get_event_loop()
self._server_logger = server_logger
self._state_condition = asyncio.Condition(loop=self._loop)
ensure_future(self._init_cache(), loop=self._loop)
async def download(self, url: str, head: CIMultiDictProxy, callback: Callable[[bytearray], None]) -> str:
await self._when_state(READY)
if not await self._upload_from_cache(url, head, callback):
await self._upload_with_pd(url, head, callback)
async def _upload_from_cache(self, url: str, head: CIMultiDictProxy, callback: Callable[[bytearray], None]) -> bool:
"""Upload from cache"""
cache_entry_dir = self._cache.get(url)
if not cache_entry_dir:
return False
try:
cache_info = await self._load_cache_info(cache_entry_dir)
except CacheError as exc:
self._debug('Bad cache entry deleted: {!r}. Error: {!r}.'.format(cache_entry_dir, exc))
self._delete_cache_entry(cache_entry_dir)
return False
up_to_date = head.get(hdrs.LAST_MODIFIED) == cache_info.get(hdrs.LAST_MODIFIED)
up_to_date &= head.get(hdrs.ETAG) == cache_info.get(hdrs.ETAG)
if not up_to_date:
self._debug('Outdated cache for: {!r} deleted.'.format(url))
await self._delete_cache_entry(cache_entry_dir)
return False
self._debug('Uploading (from cache): {!r}.'.format(url))
await self._update_last_access(cache_entry_dir)
cache_bin_file_path = get_cache_bin_file_path(cache_entry_dir)
# Open file and send all its bytes it to back.
coro_read = read_from_file_by_chunks(cache_bin_file_path, callback, self._chunk_size,
lambda: self._state != CANCELLED, loop=self._loop)
uploading = ensure_future(coro_read, loop=self._loop)
self._uploadings.add((url, uploading))
try:
await uploading
except ReadError:
self._debug('Read error.')
pass
except Exception as exc:
self._debug('Uploading failed with exception: {!r}.'.format(exc))
raise
finally:
self._uploadings.remove((url, uploading))
return True
async def _upload_with_pd(self, url, head: CIMultiDictProxy, callback: Callable[[bytearray], None]):
"""Upload using parallel downloader."""
content_length = head.get(hdrs.CONTENT_LENGTH)
assert content_length is not None
content_length = int(content_length)
self._debug('Uploading (from parallel downloader): {!r}.'.format(url))
# Start or join existed downloading task.
dl = self._downloadings.get(url)
if dl:
pd, downloading, caching = dl
else:
def downloading_done(_):
self._debug('Downloading {!r} done.'.format(url))
assert url in self._downloadings
del self._downloadings[url]
# Create parallel downloader.
pd = ParallelDownloader(url, content_length,
parallels=self._parallels,
part_size=self._part_size,
chunk_size=self._chunk_size,
loop=self._loop)
# Start downloading.
downloading = ensure_future(pd.download(), loop=self._loop)
downloading.add_done_callback(downloading_done)
# Write downloading content to a cache entry.
async def cache():
cache_entry_dir = create_new_cache_entry_dir(self._cache_dir)
try:
cache_bin_file_path = get_cache_bin_file_path(cache_entry_dir)
with open(cache_bin_file_path, 'xb') as cache_bin_file:
await pd.read(lambda chunk: cache_bin_file.write(chunk))
await self._update_last_access(cache_entry_dir)
cache_info = {
'URL': url,
hdrs.CONTENT_LENGTH: content_length,
hdrs.LAST_MODIFIED: head.get(hdrs.LAST_MODIFIED),
hdrs.ETAG: head.get(hdrs.ETAG)
}
await self._save_cache_info(cache_entry_dir, cache_info)
# Add to a cache index.
self._cache[url] = cache_entry_dir
except:
# Remove cache entry dir in case of error or cancellation.
await self._delete_cache_entry(cache_entry_dir)
raise
caching = ensure_future(cache(), loop=self._loop)
self._downloadings[url] = pd, downloading, caching
uploading = ensure_future(pd.read(callback), loop=self._loop)
self._uploadings.add((url, uploading))
try:
await uploading
except ReadError:
self._debug('Read error.')
pass
except Exception as exc:
self._debug('Uploading failed with exception: {!r}.'.format(exc))
raise
finally:
self._uploadings.remove((url, uploading))
if not self._uploadings:
await pd.clear()
async def _save_cache_info(self, cache_entry_dir: str, cache_info: Dict[str, str]):
def do():
cache_info_file_path = get_cache_info_file_path(cache_entry_dir)
with open(cache_info_file_path, 'w') as f:
json.dump(cache_info, f, indent=4)
await self._run_nb(do)
async def _load_cache_info(self, cache_entry_dir: str) -> Dict[str, str]:
def do():
cache_info_file_path = get_cache_info_file_path(cache_entry_dir)
if not os.path.isfile(cache_info_file_path):
raise CacheError('Cache info file problem.')
try:
with open(cache_info_file_path) as f:
return json.load(f) # type: Dict
except Exception as exc:
raise CacheError(exc)
return await self._run_nb(do)
async def _update_last_access(self, cache_entry_dir: str):
def do():
cache_last_access_file_path = get_cache_last_access_file_path(cache_entry_dir)
with self._last_access_file_lock, open(cache_last_access_file_path, 'w') as f:
f.write(str(time()))
await self._run_nb(do)
async def _init_cache(self):
for cache_entry_dir in os.listdir(self._cache_dir):
cache_entry_dir = os.path.join(self._cache_dir, cache_entry_dir)
if not os.path.isdir(cache_entry_dir):
continue
try:
cache_bin_file_path = get_cache_bin_file_path(cache_entry_dir)
if not os.path.isfile(cache_bin_file_path):
raise CacheError('Cache bin file problem.')
cache_info = await self._load_cache_info(cache_entry_dir)
url = cache_info.get('URL')
if not url:
raise CacheError('Bad cache info file.')
self._cache[url] = cache_entry_dir
except Exception as exc:
self._debug('Cannot load cache from dir: {!r}. Error: {!r}.'.format(cache_entry_dir, exc))
continue
async with self._state_condition:
self._state = READY
self._state_condition.notify_all()
async def _delete_cache_entry(self, cache_entry_dir):
try:
await self._run_nb(lambda: shutil.rmtree(cache_entry_dir))
except:
pass
async def _when_state(self, state):
async with self._state_condition:
while self._state is not state:
await self._state_condition.wait()
async def _run_nb(self, func):
return await self._loop.run_in_executor(None, lambda: func())
def _debug(self, msg, *args, **kwargs):
msg = "{!r} {!s}".format(self, msg)
self._server_logger.debug(msg, *args, **kwargs)
def __repr__(self, *args, **kwargs):
return '<CachingDownloader (D:{!s} U:{!s})>'.format(len(self._downloadings), len(self._uploadings))
class ParallelHttpRequestHandler(aiohttp.StreamProtocol):
_request_count = 0
_request_handler = None
_reading_request = False
_keep_alive = False # keep transport open
_keep_alive_handle = None # keep alive timer handle
_timeout_handle = None # slow request timer handle
_request_prefix = aiohttp.HttpPrefixParser() # http method parser
_request_parser = aiohttp.HttpRequestParser() # default request parser
def __init__(
self, manager, *, loop: AbstractEventLoop = None,
keep_alive=75,
keep_alive_on=True,
timeout=0,
server_logger=server_logger,
access_logger=access_logger,
access_log_format=DEFAULT_PARACCESS_LOG_FORMAT,
debug=False,
log=None,
parallels: int = DEFAULT_PARALLELS,
part_size: int = DEFAULT_PART_SIZE,
chunk_size: int = DEFAULT_CHUNK_SIZE,
buffer_dir: str = DEFAULT_BUFFER_DIR,
cached_downloader: CachingDownloader,
**kwargs):
super().__init__(
loop=loop,
disconnect_error=aiohttp.errors.ClientDisconnectedError, **kwargs)
self._keep_alive_on = keep_alive_on
self._keep_alive_period = keep_alive # number of seconds to keep alive
self._timeout = timeout # slow request timeout
self._loop = loop if loop is not None else asyncio.get_event_loop()
self._server_logger = log or server_logger
self._debug = debug
self._access_logger = aiohttp.helpers.AccessLogger(access_logger, access_log_format) if access_logger else None
self._manager = manager
self._loop = loop
self._parallels = parallels
self._part_size = part_size
self._chunk_size = chunk_size
self._buffer_dir = buffer_dir
self._cached_downloader = cached_downloader
def check_request(self, message: RawRequestMessage):
if message.method == hdrs.METH_CONNECT:
self.handle_error(status=405, message=message)
raise UnsupportedError("Method '%s' is not supported." % message.method)
@property
def keep_alive_timeout(self):
return self._keep_alive_period
def closing(self, timeout=15.0):
"""Worker process is about to exit, we need cleanup everything and
stop accepting requests. It is especially important for keep-alive
connections."""
self._keep_alive = False
self._keep_alive_on = False
self._keep_alive_period = None
if not self._reading_request and self.transport is not None:
if self._request_handler:
self._request_handler.cancel()
self._request_handler = None
self.transport.close()
self.transport = None
elif self.transport is not None and timeout:
if self._timeout_handle is not None:
self._timeout_handle.cancel()
# use slow request timeout for closing
# connection_lost cleans timeout handler
now = self._loop.time()
self._timeout_handle = self._loop.call_at(
ceil(now + timeout), self.cancel_slow_request)
def connection_made(self, transport):
super().connection_made(transport)
self._request_handler = ensure_future(self.start(), loop=self._loop)
# start slow request timer
if self._timeout:
now = self._loop.time()
self._timeout_handle = self._loop.call_at(
ceil(now + self._timeout), self.cancel_slow_request)
if self._keep_alive_on:
tcp_keepalive(self, transport)
self._manager.connection_made(self, transport)
def connection_lost(self, exc):
self._manager.connection_lost(self, exc)
super().connection_lost(exc)
if self._request_handler is not None:
self._request_handler.cancel()
self._request_handler = None
if self._keep_alive_handle is not None:
self._keep_alive_handle.cancel()
self._keep_alive_handle = None
if self._timeout_handle is not None:
self._timeout_handle.cancel()
self._timeout_handle = None
def data_received(self, data):
super().data_received(data)
# reading request
if not self._reading_request:
self._reading_request = True
# stop keep-alive timer
if self._keep_alive_handle is not None:
self._keep_alive_handle.cancel()
self._keep_alive_handle = None
def keep_alive(self, val):
"""Set keep-alive connection mode.
:param bool val: new state.
"""
self._keep_alive = val
def log_access(self, message, environ, response, time):
if self._access_logger:
self._access_logger.log(message, environ, response, self.transport, time)
def log_debug(self, *args, **kw):
if self._debug:
self._server_logger.debug(*args, **kw)
def log_exception(self, *args, **kw):
self._server_logger.exception(*args, **kw)
def cancel_slow_request(self):
if self._request_handler is not None:
self._request_handler.cancel()
self._request_handler = None
if self.transport is not None:
self.transport.close()
self.log_debug('Close slow request.')
@asyncio.coroutine
def start(self):
"""Start processing of incoming requests.
It reads request line, request headers and request payload, then
calls handle_request() method. Subclass has to override
handle_request(). start() handles various exceptions in request
or response handling. Connection is being closed always unless
keep_alive(True) specified.
"""
reader = self.reader
while True:
message = None
self._keep_alive = False
self._request_count += 1
self._reading_request = False
payload = None
try:
# read http request method
prefix = reader.set_parser(self._request_prefix)
yield from prefix.read()
# start reading request
self._reading_request = True
# start slow request timer
if self._timeout and self._timeout_handle is None:
now = self._loop.time()
self._timeout_handle = self._loop.call_at(
ceil(now + self._timeout), self.cancel_slow_request)
# read request headers
httpstream = reader.set_parser(self._request_parser)
message = yield from httpstream.read()
# cancel slow request timer
if self._timeout_handle is not None:
self._timeout_handle.cancel()
self._timeout_handle = None
# request may not have payload
if (message.headers.get(hdrs.CONTENT_LENGTH, 0) or
hdrs.SEC_WEBSOCKET_KEY1 in message.headers or
'chunked' in message.headers.get(
hdrs.TRANSFER_ENCODING, '')):
payload = aiohttp.streams.FlowControlStreamReader(
reader, loop=self._loop)
reader.set_parser(
aiohttp.HttpPayloadParser(message), payload)
else:
payload = EMPTY_PAYLOAD
yield from self.handle_request(message, payload)
except asyncio.CancelledError:
return
except aiohttp.errors.ClientDisconnectedError:
self.log_debug(
'Ignored premature client disconnection #1.')
return
except aiohttp.errors.HttpProcessingError as exc:
if self.transport is not None:
yield from self.handle_error(exc.code, message,
None, exc, exc.headers,
exc.message)
except aiohttp.errors.LineLimitExceededParserError as exc:
yield from self.handle_error(400, message, None, exc)
except Exception as exc:
yield from self.handle_error(500, message, None, exc)
finally:
if self.transport is None:
self.log_debug(
'Ignored premature client disconnection #2.')
return
if payload and not payload.is_eof():
self.log_debug('Uncompleted request.')
self._request_handler = None
self.transport.close()
return
else:
reader.unset_parser()
if self._request_handler:
if self._keep_alive and self._keep_alive_period:
self.log_debug(
'Start keep-alive timer for %s sec.',
self._keep_alive_period)
now = self._loop.time()
self._keep_alive_handle = self._loop.call_at(
ceil(now + self._keep_alive_period),
self.transport.close)
elif self._keep_alive and self._keep_alive_on:
# do nothing, rely on kernel or upstream server
pass
else:
self.log_debug('Close client connection.')
self._request_handler = None
self.transport.close()
return
else:
# connection is closed
return
async def handle_request(self, message: RawRequestMessage, payload):
now = self._loop.time()
self.check_request(message)
self.keep_alive(True)
if message.method == hdrs.METH_CONNECT:
response = await self.process_connect(message, payload)
else:
# Try to process parallel.
response = await self.process_parallel(message, payload)
# Otherwise process normally.
if not response:
response = await self.process_normally(message, payload)
self.log_access(message, None, response, self._loop.time() - now)