buranun/server.py

95 lines
2.6 KiB
Python

import http.cookies
import http.server
import urllib.parse
import config
import generate_html
class HTTPRequestHandler(http.server.BaseHTTPRequestHandler):
server_version = 'Buranun/0.0'
# TODO: Look into keepalive problems in links2
#protocol_version = 'HTTP/1.1'
protocol_version = 'HTTP/1.0'
def __send_html(self, html, *, status_code = 200):
encoded = html.encode('utf-8')
length = len(encoded)
# TODO: Make this more sensical
sent_cookies = http.cookies.SimpleCookie()
sent_cookies['buranun_session'] = 'dihutenosa'
sent_cookies['buranun_session']['path'] = config.url_prefix if config.url_prefix != '' else '/'
sent_cookies['buranun_session']['max-age'] = 60
sent_cookies['buranun_session']['secure'] = config.ssl
sent_cookies['buranun_session']['httponly'] = True
self.send_response(status_code)
self.send_header('Content-Type', 'text/html; charset=utf-8')
self.send_header('Content-Length', length)
# Since http.cookies doesn't play nicely with http.server we need to do this manually
self.flush_headers()
self.wfile.write(sent_cookies.output().encode('utf-8') + b'\r\n')
self.end_headers()
self.wfile.write(encoded)
def __send_404(self, path):
html = generate_html.error_404(path)
self.__send_html(html, status_code = 404)
def do_GET(self):
# TODO: Do something with the session
cookies_string = self.headers['cookie']
if cookies_string is not None:
received_cookies = http.cookies.SimpleCookie()
try:
received_cookies.load(cookies_string)
except http.cookies.CookieError:
print('malformed cookies')
if 'buranun_session' in received_cookies:
print(received_cookies['buranun_session'].value)
else:
# Display page that tells user to login
# TODO: Have it forward the user back to the page where they were at
html = generate_html.login_forward(self.path)
self.__send_html(html)
# Don't run rest of the function
return
path = urllib.parse.unquote(self.path)
path_components = [component for component in path.split('/') if component != '']
if len(path_components) == 0:
# Path of format / → index
html = generate_html.index()
self.__send_html(html)
elif len(path_components) == 1:
# Path of format /foo/ → board index
board_name = path_components[0]
html = generate_html.board(board_name)
self.__send_html(html)
else:
# Path not understood, send 404
self.__send_404(path)
def main():
# TODO: Don't hardcode
config.load('buranun.conf')
httpd = http.server.HTTPServer(('', config.port), HTTPRequestHandler)
httpd.serve_forever()
if __name__ == '__main__':
main()