From 8179643067745763563f819cb615bffb73cdcf82 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sun, 20 Aug 2017 20:32:16 +0100 Subject: [PATCH] Improve handling of HTTP headers --- AnkiConnect.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index a4acd20..ac1cfff 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -202,16 +202,22 @@ class AjaxServer: self.resetHeaders() - def addHeader(self, name, value): - self.headers.append([name, value]) - - + def setHeader(self, name, value): + self.headers[name] = value + + def resetHeaders(self): - self.headers = [ - ['HTTP/1.1 200 OK', None], - ['Content-Type', 'text/json'], - ['Content-Length', ''] - ] + self.headers = { + 'HTTP/1.1 200 OK': None, + 'Content-Type': 'text/json' + } + + + def getHeaders(self): + headers = [] + for name in self.headers: + headers.append([name, self.headers[name]]) + return headers def advance(self): @@ -256,9 +262,9 @@ class AjaxServer: body = json.dumps(None); resp = bytes() - - headers = self.headers - headers[2][1] = str(len(body)) + + self.setHeader('Content-Length', str(len(body))) + headers = self.getHeaders() for key, value in headers: if value is None: