This repository was archived by the owner on Jan 13, 2021. It is now read-only.
File tree 2 files changed +48
-3
lines changed
2 files changed +48
-3
lines changed Original file line number Diff line number Diff line change @@ -483,11 +483,30 @@ def _consume_single_frame(self):
483
483
frame , length = Frame .parse_frame_header (header )
484
484
485
485
# Read the remaining data from the socket.
486
- if length :
486
+ data = self ._recv_payload (length )
487
+ self ._consume_frame_payload (frame , data )
488
+
489
+ def _recv_payload (self , length ):
490
+ if not length :
491
+ return memoryview (b'' )
492
+
493
+ buffer = bytearray (length )
494
+ buffer_view = memoryview (buffer )
495
+ index = 0
496
+ data_length = - 1
497
+ # _sock.recv(length) might not read out all data if the given length
498
+ # is very large. So it should be to retrieve from socket repeatedly.
499
+ while length and data_length :
487
500
data = self ._sock .recv (length )
488
- else :
489
- data = memoryview (b'' )
501
+ data_length = len (data )
502
+ end = index + data_length
503
+ buffer_view [index :end ] = data [:]
504
+ length -= data_length
505
+ index = end
506
+
507
+ return buffer_view [:end ]
490
508
509
+ def _consume_frame_payload (self , frame , data ):
491
510
frame .parse_body (data )
492
511
493
512
log .info (
Original file line number Diff line number Diff line change @@ -1072,6 +1072,23 @@ def test_we_can_read_from_the_socket(self):
1072
1072
s = c .recent_stream
1073
1073
assert s .data == [b'testdata' ]
1074
1074
1075
+ def test_we_can_read_fitfully_from_the_socket (self ):
1076
+ sock = DummyFitfullySocket ()
1077
+ sock .buffer = BytesIO (
1078
+ b'\x00 \x00 \x18 \x00 \x01 \x00 \x00 \x00 \x01 '
1079
+ b'testdata'
1080
+ b'+payload'
1081
+ )
1082
+
1083
+ c = HTTP20Connection ('www.google.com' )
1084
+ c ._sock = sock
1085
+ c .putrequest ('GET' , '/' )
1086
+ c .endheaders ()
1087
+ c ._recv_cb ()
1088
+
1089
+ s = c .recent_stream
1090
+ assert s .data == [b'testdata+payload' ]
1091
+
1075
1092
def test_putrequest_sends_data (self ):
1076
1093
sock = DummySocket ()
1077
1094
@@ -2045,6 +2062,15 @@ def recv(self, l):
2045
2062
def close (self ):
2046
2063
pass
2047
2064
2065
+
2066
+ class DummyFitfullySocket (DummySocket ):
2067
+ def recv (self , l ):
2068
+ length = l
2069
+ if l != 9 and l >= 4 :
2070
+ length = int (round (l / 2 ))
2071
+ return memoryview (self .buffer .read (length ))
2072
+
2073
+
2048
2074
class DummyStream (object ):
2049
2075
def __init__ (self , data , trailers = None ):
2050
2076
self .data = data
You can’t perform that action at this time.
0 commit comments