Extended "HTTP" support
This commit is contained in:
parent
7a1a69fc93
commit
949ff71378
18
gophersrv.py
18
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)
|
||||
|
|
Loading…
Reference in New Issue