rename get to send and allow webpages to be served from subfolders

This commit is contained in:
Jonathan Corwin 2010-05-04 22:49:02 +01:00
parent 465ce8c7f7
commit 4f8d058928

View File

@ -160,7 +160,7 @@ class HttpConnection(object):
if folders[1] == u'': if folders[1] == u'':
mimetype, html = self.serve_file(u'') mimetype, html = self.serve_file(u'')
elif folders[1] == u'files': elif folders[1] == u'files':
mimetype, html = self.serve_file(folders[2]) mimetype, html = self.serve_file(os.sep.join(folders[2:]))
elif folders[1] == u'send': elif folders[1] == u'send':
html = self.process_event(folders[2], params) html = self.process_event(folders[2], params)
elif folders[1] == u'request': elif folders[1] == u'request':
@ -168,12 +168,12 @@ class HttpConnection(object):
return return
if html: if html:
if mimetype: if mimetype:
self.socket.write(self.get_200_ok(mimetype)) self.send_200_ok(mimetype)
else: else:
self.socket.write(self.get_200_ok()) self.send_200_ok()
self.socket.write(html) self.socket.write(html)
else: else:
self.socket.write(self.get_404_not_found()) self.send_404_not_found()
self.close() self.close()
def serve_file(self, filename): def serve_file(self, filename):
@ -188,7 +188,8 @@ class HttpConnection(object):
log.debug(u'serve file request %s' % filename) log.debug(u'serve file request %s' % filename)
if not filename: if not filename:
filename = u'index.html' filename = u'index.html'
if os.path.basename(filename) != filename: path = os.path.normpath(os.path.join(self.parent.html_dir, filename))
if not path.startswith(self.parent.html_dir):
return None return None
(fileroot, ext) = os.path.splitext(filename) (fileroot, ext) = os.path.splitext(filename)
if ext == u'.html': if ext == u'.html':
@ -205,7 +206,6 @@ class HttpConnection(object):
mimetype = u'image/png' mimetype = u'image/png'
else: else:
return (None, None) return (None, None)
path = os.path.join(self.parent.html_dir, filename)
try: try:
f = open(path, u'rb') f = open(path, u'rb')
except: except:
@ -282,31 +282,31 @@ class HttpConnection(object):
return return
self.timer.stop() self.timer.stop()
html = json.dumps(data) html = json.dumps(data)
html = self.get_200_ok() + html + u'\n' self.send_200_ok()
self.socket.write(html) self.socket.write(html)
self.close() self.close()
def get_200_ok(self, mimetype='text/html; charset="utf-8"'): def send_200_ok(self, mimetype='text/html; charset="utf-8"'):
""" """
Successful request. Send OK headers. Assume html for now. Successful request. Send OK headers. Assume html for now.
""" """
return u'HTTP/1.1 200 OK\r\n' + \ self.socket.write(u'HTTP/1.1 200 OK\r\n' + \
u'Content-Type: %s\r\n\r\n' % mimetype u'Content-Type: %s\r\n\r\n' % mimetype)
def get_404_not_found(self): def send_404_not_found(self):
""" """
Invalid url. Say so Invalid url. Say so
""" """
return u'HTTP/1.1 404 Not Found\r\n'+ \ self.socket.write(u'HTTP/1.1 404 Not Found\r\n'+ \
u'Content-Type: text/html; charset="utf-8"\r\n' + \ u'Content-Type: text/html; charset="utf-8"\r\n' + \
u'\r\n' u'\r\n')
def get_408_timeout(self): def send_408_timeout(self):
""" """
A _request hasn't returned anything in the timeout period. A _request hasn't returned anything in the timeout period.
Return timeout Return timeout
""" """
return u'HTTP/1.1 408 Request Timeout\r\n' self.socket.write(u'HTTP/1.1 408 Request Timeout\r\n')
def timeout(self): def timeout(self):
""" """
@ -314,8 +314,7 @@ class HttpConnection(object):
""" """
if not self.socket: if not self.socket:
return return
html = self.get_408_timeout() html = self.send_408_timeout()
self.socket.write(html)
self.close() self.close()
def disconnected(self): def disconnected(self):