examples/http_server*: Update for buffered-like streams (read line by line).

Since "read-exactly" stream refactor, where stream.read(N) will read
exactly N bytes (unless EOF), http_server* examples can't any longer do
client_socket.read(4096) and expect to get full request (it will block
on HTTP/1.1 client). Instead, read request line by line, as the HTTP
protocol requires.
This commit is contained in:
Paul Sokolovsky 2016-07-10 23:01:52 +03:00
parent 1459a8d5c9
commit e3f0f31e07
2 changed files with 13 additions and 2 deletions

View File

@ -34,7 +34,13 @@ def main(use_stream=False):
if use_stream:
# MicroPython socket objects support stream (aka file) interface
# directly.
print(client_s.read(4096))
req = client_s.readline()
print(req)
while True:
h = client_s.readline()
if h == b"" or h == b"\r\n":
break
print(h)
client_s.write(CONTENT % counter)
else:
print(client_s.recv(4096))

View File

@ -42,8 +42,13 @@ def main(use_stream=True):
# next request they issue will likely be more well-behaving and
# will succeed.
try:
req = client_s.read(4096)
req = client_s.readline()
print(req)
while True:
h = client_s.readline()
if h == b"" or h == b"\r\n":
break
print(h)
if req:
client_s.write(CONTENT % counter)
except Exception as e: