Skip to content

Commit 6fa5901

Browse files
Merge pull request #115 from Anton-2/master
wsgiserver: implement parse_headers
2 parents 61e3863 + 0395419 commit 6fa5901

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

adafruit_esp32spi/adafruit_esp32spi_wsgiserver.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import io
3232
import gc
3333
from micropython import const
34-
from adafruit_requests import parse_headers
3534
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
3635

3736
_the_interface = None # pylint: disable=invalid-name
@@ -46,6 +45,22 @@ def set_interface(iface):
4645

4746
NO_SOCK_AVAIL = const(255)
4847

48+
49+
def parse_headers(client):
50+
"""
51+
Parses the header portion of an HTTP request from the socket.
52+
Expects first line of HTTP request to have been read already.
53+
"""
54+
headers = {}
55+
while True:
56+
line = str(client.readline(), "utf-8")
57+
if not line:
58+
break
59+
title, content = line.split(":", 1)
60+
headers[title.strip().lower()] = content.strip()
61+
return headers
62+
63+
4964
# pylint: disable=invalid-name
5065
class WSGIServer:
5166
"""
@@ -99,7 +114,7 @@ def finish_response(self, result):
99114
:param string result: the data string to send back in the response to the client.
100115
"""
101116
try:
102-
response = "HTTP/1.1 {0}\r\n".format(self._response_status)
117+
response = "HTTP/1.1 {0}\r\n".format(self._response_status or "500 ISE")
103118
for header in self._response_headers:
104119
response += "{0}: {1}\r\n".format(*header)
105120
response += "\r\n"
@@ -159,7 +174,10 @@ def _start_response(self, status, response_headers):
159174
ex ("header-name", "header value")
160175
"""
161176
self._response_status = status
162-
self._response_headers = [("Server", "esp32WSGIServer")] + response_headers
177+
self._response_headers = [
178+
("Server", "esp32WSGIServer"),
179+
("Connection", "close"),
180+
] + response_headers
163181

164182
def _get_environ(self, client):
165183
"""

0 commit comments

Comments
 (0)