Skip to content

Commit f97db45

Browse files
author
Antonin ENFRUN
committed
wsgiserver: implement parse_headers, add 500 Internal Server Error when the wsgi application misbehaves, and make sure nobody think we can handle keep-alive requests.
1 parent fce466b commit f97db45

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

adafruit_esp32spi/adafruit_esp32spi_wsgiserver.py

Lines changed: 14 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,18 @@ def set_interface(iface):
4645

4746
NO_SOCK_AVAIL = const(255)
4847

48+
49+
def parse_headers(client):
50+
headers = {}
51+
while True:
52+
line = str(client.readline(), "utf-8")
53+
if not line:
54+
break
55+
title, content = line.split(':', 1)
56+
headers[title.strip().lower()] = content.strip()
57+
return headers
58+
59+
4960
# pylint: disable=invalid-name
5061
class WSGIServer:
5162
"""
@@ -99,7 +110,7 @@ def finish_response(self, result):
99110
:param string result: the data string to send back in the response to the client.
100111
"""
101112
try:
102-
response = "HTTP/1.1 {0}\r\n".format(self._response_status)
113+
response = "HTTP/1.1 {0}\r\n".format(self._response_status or "500 ISE")
103114
for header in self._response_headers:
104115
response += "{0}: {1}\r\n".format(*header)
105116
response += "\r\n"
@@ -159,7 +170,7 @@ def _start_response(self, status, response_headers):
159170
ex ("header-name", "header value")
160171
"""
161172
self._response_status = status
162-
self._response_headers = [("Server", "esp32WSGIServer")] + response_headers
173+
self._response_headers = [("Server", "esp32WSGIServer"), ("Connection", "close")] + response_headers
163174

164175
def _get_environ(self, client):
165176
"""

0 commit comments

Comments
 (0)