Skip to content

Commit 0ea6472

Browse files
authored
Merge pull request #79 from bgreni/fix-chunked-transfer
fix recv hanging on chunked transfer
2 parents 875657d + fb48653 commit 0ea6472

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

client.mojo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ from lightbug_http.client import Client
33

44

55
fn test_request(inout client: Client) raises -> None:
6-
var uri = URI.parse_raises("http://httpbin.org/status/404")
7-
var headers = Headers(Header("Host", "httpbin.org"))
6+
var uri = URI.parse_raises("google.com")
7+
var headers = Headers(Header("Host", "google.com"))
88
var request = HTTPRequest(uri, headers)
99
var response = client.do(request^)
1010

lightbug_http/http/response.mojo

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from small_time.small_time import now
22
from lightbug_http.uri import URI
33
from lightbug_http.utils import ByteReader, ByteWriter
4-
from lightbug_http.io.bytes import Bytes, bytes, Byte
4+
from lightbug_http.io.bytes import Bytes, bytes, Byte, byte
55
from lightbug_http.strings import (
66
strHttp11,
77
strHttp,
@@ -70,8 +70,15 @@ struct HTTPResponse(Writable, Stringable):
7070
var buff = Bytes(capacity=default_buffer_size)
7171
while conn.value().read(buff) > 0:
7272
b += buff
73-
buff.resize(0)
7473

74+
if buff[-5] == byte('0') and buff[-4] == byte('\r')
75+
and buff[-3] == byte('\n')
76+
and buff[-2] == byte('\r')
77+
and buff[-1] == byte('\n'):
78+
break
79+
80+
81+
buff.resize(0)
7582
response.read_chunks(b^)
7683
return response
7784

@@ -150,7 +157,6 @@ struct HTTPResponse(Writable, Stringable):
150157

151158
fn read_chunks(inout self, owned chunks: Bytes) raises:
152159
var reader = ByteReader(chunks^)
153-
154160
while True:
155161
var size = atol(StringSlice(unsafe_from_utf8=reader.read_line()), 16)
156162
if size == 0:

lightbug_http/net.mojo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ struct SysConnection(Connection):
249249
buf.size += bytes_recv
250250
if bytes_recv == 0:
251251
return 0
252-
if bytes_recv < buf.capacity:
253-
return bytes_recv
254252
return bytes_recv
255253

256254
fn write(self, owned msg: String) raises -> Int:

0 commit comments

Comments
 (0)