Skip to content

wsgiserver: implement parse_headers #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions adafruit_esp32spi/adafruit_esp32spi_wsgiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import io
import gc
from micropython import const
from adafruit_requests import parse_headers
import adafruit_esp32spi.adafruit_esp32spi_socket as socket

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

NO_SOCK_AVAIL = const(255)


def parse_headers(client):
"""
Parses the header portion of an HTTP request from the socket.
Expects first line of HTTP request to have been read already.
"""
headers = {}
while True:
line = str(client.readline(), "utf-8")
if not line:
break
title, content = line.split(":", 1)
headers[title.strip().lower()] = content.strip()
return headers


# pylint: disable=invalid-name
class WSGIServer:
"""
Expand Down Expand Up @@ -99,7 +114,7 @@ def finish_response(self, result):
:param string result: the data string to send back in the response to the client.
"""
try:
response = "HTTP/1.1 {0}\r\n".format(self._response_status)
response = "HTTP/1.1 {0}\r\n".format(self._response_status or "500 ISE")
for header in self._response_headers:
response += "{0}: {1}\r\n".format(*header)
response += "\r\n"
Expand Down Expand Up @@ -159,7 +174,10 @@ def _start_response(self, status, response_headers):
ex ("header-name", "header value")
"""
self._response_status = status
self._response_headers = [("Server", "esp32WSGIServer")] + response_headers
self._response_headers = [
("Server", "esp32WSGIServer"),
("Connection", "close"),
] + response_headers

def _get_environ(self, client):
"""
Expand Down