From 949ff7137831aeaecfeb4181f882f939fd1c065c Mon Sep 17 00:00:00 2001 From: Juhani Haverinen Date: Thu, 1 Jan 2015 22:13:16 +0200 Subject: [PATCH] Extended "HTTP" support --- gophersrv.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/gophersrv.py b/gophersrv.py index 404460f..311444f 100644 --- a/gophersrv.py +++ b/gophersrv.py @@ -74,12 +74,13 @@ def notallowederror(conn, path): # Server implementation def getrequest(conn): + ishttp = False data = '' while True: chunk = conn.recv(1024) if not chunk: - return None + return None, ishttp data += chunk @@ -92,8 +93,17 @@ def getrequest(conn): # Minimal HTTP support if len(data) >= 4 and data[:4] == 'GET ': data = data.split()[1] + ishttp = True - return data.split('\t') + return data.split('\t'), ishttp + +def sendheader(conn, ishttp): + if ishttp: + # Default to text/plain + contenttype="text/plain; encoding=utf-8" + conn.sendall('HTTP/1.1 200 OK\r\n' + 'Content-type: %s\r\n' + '\r\n' % contenttype) def serveurlredirect(conn, path): path = path[4:] @@ -153,13 +163,15 @@ class Serve(threading.Thread): threading.Thread.__init__(self) def run(self): try: - request = getrequest(self.conn) + (request, ishttp) = getrequest(self.conn) if not request: self.conn.shutdown(socket.SHUT_RDWR) self.conn.close() return + sendheader(conn, ishttp) + serverequest(self.conn, request) self.conn.shutdown(socket.SHUT_RDWR)