#!/usr/bin/env python3 """Simple server written using an event loop.""" import email.message import os import sys assert sys.version >= '3.3', 'Please use Python 3.3 or higher.' import tulip import tulip.http class HttpServer(tulip.http.ServerHttpProtocol): def handle_request(self, request_info, message): print('method = {!r}; path = {!r}; version = {!r}'.format( request_info.method, request_info.uri, request_info.version)) path = request_info.uri if (not (path.isprintable() and path.startswith('/')) or '/.' in path): print('bad path', repr(path)) path = None else: path = '.' + path if not os.path.exists(path): print('no file', repr(path)) path = None else: isdir = os.path.isdir(path) if not path: raise tulip.http.HttpStatusException(404) headers = email.message.Message() for hdr, val in message.headers: print(hdr, val) headers.add_header(hdr, val) if isdir and not path.endswith('/'): path = path + '/' raise tulip.http.HttpStatusException( 302, headers=(('URI', path), ('Location', path))) response = tulip.http.Response(self.transport, 200) response.add_header('Transfer-Encoding', 'chunked') # content encoding accept_encoding = headers.get('accept-encoding', '').lower() if 'deflate' in accept_encoding: response.add_header('Content-Encoding', 'deflate') response.add_compression_filter('deflate') elif 'gzip' in accept_encoding: response.add_header('Content-Encoding', 'gzip') response.add_compression_filter('gzip') response.add_chunking_filter(1025) if isdir: response.add_header('Content-type', 'text/html') response.send_headers() response.write(b'