Allow use of selectors in HTTP mode, and send corresponding content-type
This commit is contained in:
parent
f0cf8ade85
commit
723c24e883
41
gophersrv.py
41
gophersrv.py
|
@ -97,10 +97,39 @@ def getrequest(conn):
|
|||
|
||||
return data.split('\t'), ishttp
|
||||
|
||||
def sendheader(conn, ishttp):
|
||||
def getselector(request): # If a HTTP request with selector is used, this extracts the selector
|
||||
if len(request) < 1:
|
||||
return (request, None)
|
||||
|
||||
req = request[0].split('/')
|
||||
while '' in req:
|
||||
req.remove('')
|
||||
args = request[1:]
|
||||
|
||||
if len(req) >= 1 and req[0] in ['0', '1', '5', '9', 'g', 'h', 'I', 's']: # Supported selectors
|
||||
reqpath = '/'.join(req[1:])
|
||||
selector = req[0]
|
||||
else:
|
||||
reqpath = '/'.join(req)
|
||||
selector = None
|
||||
|
||||
return ([reqpath] + args, selector)
|
||||
|
||||
def sendheader(conn, ishttp, selector):
|
||||
if ishttp:
|
||||
# Default to text/plain
|
||||
contenttype="text/plain; charset=utf-8"
|
||||
# All others can safely be made text/plain
|
||||
contenttypes = {'5': 'application/octet-stream',
|
||||
'9': 'application/octet-stream',
|
||||
'g': 'image/gif',
|
||||
'h': 'text/html; charset=utf-8',
|
||||
'I': 'application/octet-stream',
|
||||
's': 'application/octet-stream'}
|
||||
|
||||
if selector is not None and selector in contenttypes:
|
||||
contenttype = contenttypes[selector]
|
||||
else:
|
||||
contenttype = 'text/plain; charset=utf-8' # Default to text/plain
|
||||
|
||||
conn.sendall('HTTP/1.1 200 OK\r\n'
|
||||
'Content-type: %s\r\n'
|
||||
'\r\n' % contenttype)
|
||||
|
@ -170,7 +199,11 @@ class Serve(threading.Thread):
|
|||
self.conn.close()
|
||||
return
|
||||
|
||||
sendheader(conn, ishttp)
|
||||
if ishttp:
|
||||
request, selector = getselector(request)
|
||||
else:
|
||||
selector = None
|
||||
sendheader(conn, ishttp, selector)
|
||||
|
||||
serverequest(self.conn, request)
|
||||
|
||||
|
|
Loading…
Reference in New Issue