Extended "HTTP" support

This commit is contained in:
Juhani Haverinen 2015-01-01 22:13:16 +02:00
parent 7a1a69fc93
commit 949ff71378
1 changed files with 15 additions and 3 deletions

View File

@ -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)