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
|
# Server implementation
|
||||||
def getrequest(conn):
|
def getrequest(conn):
|
||||||
|
ishttp = False
|
||||||
data = ''
|
data = ''
|
||||||
while True:
|
while True:
|
||||||
chunk = conn.recv(1024)
|
chunk = conn.recv(1024)
|
||||||
|
|
||||||
if not chunk:
|
if not chunk:
|
||||||
return None
|
return None, ishttp
|
||||||
|
|
||||||
data += chunk
|
data += chunk
|
||||||
|
|
||||||
|
@ -92,8 +93,17 @@ def getrequest(conn):
|
||||||
# Minimal HTTP support
|
# Minimal HTTP support
|
||||||
if len(data) >= 4 and data[:4] == 'GET ':
|
if len(data) >= 4 and data[:4] == 'GET ':
|
||||||
data = data.split()[1]
|
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):
|
def serveurlredirect(conn, path):
|
||||||
path = path[4:]
|
path = path[4:]
|
||||||
|
@ -153,13 +163,15 @@ class Serve(threading.Thread):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
def run(self):
|
def run(self):
|
||||||
try:
|
try:
|
||||||
request = getrequest(self.conn)
|
(request, ishttp) = getrequest(self.conn)
|
||||||
|
|
||||||
if not request:
|
if not request:
|
||||||
self.conn.shutdown(socket.SHUT_RDWR)
|
self.conn.shutdown(socket.SHUT_RDWR)
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
sendheader(conn, ishttp)
|
||||||
|
|
||||||
serverequest(self.conn, request)
|
serverequest(self.conn, request)
|
||||||
|
|
||||||
self.conn.shutdown(socket.SHUT_RDWR)
|
self.conn.shutdown(socket.SHUT_RDWR)
|
||||||
|
|
Loading…
Reference in New Issue