Skip to content

Instantly share code, notes, and snippets.

@veraposeidon
Last active June 27, 2022 09:17
Show Gist options
  • Save veraposeidon/5250b7deeb64dd51ee6e58bf0b74a73b to your computer and use it in GitHub Desktop.
Save veraposeidon/5250b7deeb64dd51ee6e58bf0b74a73b to your computer and use it in GitHub Desktop.
SImpleHTTPServer support brotli & gzip
#!/usr/bin/python
"""Simple HTTP Server.
This module builds on BaseHTTPServer by implementing the standard GET
and HEAD requests in a fairly straightforward manner.
"""
__version__ = "0.6"
__all__ = ["SimpleHTTPRequestHandler"]
from distutils.log import fatal
import os
import posixpath
from http.server import BaseHTTPRequestHandler, HTTPServer
import urllib.parse
import cgi
import sys
import mimetypes
import zlib
import brotli
from optparse import OptionParser
import gzip
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
SERVER_PORT = 8000
def zlib_encode(content):
zlib_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS)
data = zlib_compress.compress(content) + zlib_compress.flush()
return data
def deflate_encode(content):
deflate_compress = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
data = deflate_compress.compress(content) + deflate_compress.flush()
return data
def gzip_encode(content):
data = gzip.compress(content)
return data
def brotli_encode(content):
data = brotli.compress(content)
return data
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
"""Simple HTTP request handler with GET and HEAD commands.
This serves files from the current directory and any of its
subdirectories. The MIME type for files is determined by
calling the .guess_type() method.
The GET and HEAD requests are identical except that the HEAD
request omits the actual contents of the file.
"""
server_version = "SimpleHTTP/" + __version__
def do_GET(self):
"""Serve a GET request."""
content = self.send_head()
if content:
self.wfile.write(content)
def do_HEAD(self):
"""Serve a HEAD request."""
content = self.send_head()
def send_head(self):
"""Common code for GET and HEAD commands.
This sends the response code and MIME headers.
Return value is either a file object (which has to be copied
to the outputfile by the caller unless the command was HEAD,
and must be closed by the caller under all circumstances), or
None, in which case the caller has nothing further to do.
"""
path = self.translate_path(self.path)
print("Serving path '%s'" % path)
f = None
if os.path.isdir(path):
if not self.path.endswith('/'):
# redirect browser - doing basically what apache does
self.send_response(301)
self.send_header("Location", self.path + "/")
self.end_headers()
return None
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break
else:
return self.list_directory(path).read()
ctype = self.guess_type(path)
try:
# Always read in binary mode. Opening files in text mode may cause
# newline translations, making the actual size of the content
# transmitted *less* than the content-length!
f = open(path, 'rb')
except IOError:
self.send_error(404, "File not found")
return None
encoding_types = self.headers['accept-encoding'].split(',')
encoding_types = [o.strip() for o in encoding_types]
print(encoding_types)
fs = os.fstat(f.fileno())
raw_content_length = fs[6]
content = f.read()
encoding_type = ""
# Encode content based on runtime arg
if "br" in encoding_types:
encoding_type = "br"
content = brotli_encode(content)
elif "gzip" in encoding_types:
encoding_type = "gzip"
content = gzip_encode(content)
elif "deflate" in encoding_types:
encoding_type = "deflate"
content = deflate_encode(content)
elif "zlib" in encoding_types:
encoding_type = "zlib"
content = zlib_encode(content)
else:
fatal("encoding type not supported")
compressed_content_length = len(content)
f.close()
self.send_response(200)
self.send_header("Content-type", ctype)
self.send_header("Content-Encoding", encoding_type)
self.send_header("Content-Length", max(raw_content_length, compressed_content_length))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
return content
def list_directory(self, path):
"""Helper to produce a directory listing (absent index.html).
Return value is either a file object, or None (indicating an
error). In either case, the headers are sent, making the
interface the same as for send_head().
"""
try:
list = os.listdir(path)
except os.error:
self.send_error(404, "No permission to list directory")
return None
list.sort(key=lambda a: a.lower())
f = StringIO()
displaypath = cgi.escape(urllib.parse.unquote(self.path))
f.write('<!DOCTYPE html>')
f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
f.write("<hr>\n<ul>\n")
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
displayname = name + "@"
# Note: a link to a directory displays with @ and links with /
f.write('<li><a href="%s">%s</a>\n'
% (urllib.parse.quote(linkname), cgi.escape(displayname)))
f.write("</ul>\n<hr>\n</body>\n</html>\n")
length = f.tell()
f.seek(0)
self.send_response(200)
encoding = sys.getfilesystemencoding()
self.send_header("Content-type", "text/html; charset=%s" % encoding)
self.send_header("Content-Length", str(length))
self.end_headers()
return f
def translate_path(self, path):
"""Translate a /-separated PATH to the local filename syntax.
Components that mean special things to the local file system
(e.g. drive or directory names) are ignored. (XXX They should
probably be diagnosed.)
"""
# abandon query parameters
path = path.split('?',1)[0]
path = path.split('#',1)[0]
path = posixpath.normpath(urllib.parse.unquote(path))
words = path.split('/')
words = filter(None, words)
path = os.getcwd()
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir): continue
path = os.path.join(path, word)
return path
def guess_type(self, path):
"""Guess the type of a file.
Argument is a PATH (a filename).
Return value is a string of the form type/subtype,
usable for a MIME Content-type header.
The default implementation looks the file's extension
up in the table self.extensions_map, using application/octet-stream
as a default; however it would be permissible (if
slow) to look inside the data to make a better guess.
"""
base, ext = posixpath.splitext(path)
if ext in self.extensions_map:
return self.extensions_map[ext]
ext = ext.lower()
if ext in self.extensions_map:
return self.extensions_map[ext]
else:
return self.extensions_map['']
if not mimetypes.inited:
mimetypes.init() # try to read system mime.types
extensions_map = mimetypes.types_map.copy()
extensions_map.update({
'': 'application/octet-stream', # Default
'.py': 'text/plain',
'.c': 'text/plain',
'.h': 'text/plain',
})
def test(HandlerClass = SimpleHTTPRequestHandler,
ServerClass = HTTPServer):
"""Run the HTTP request handler class.
This runs an HTTP server on port 8000 (or the first command line
argument).
"""
server_address = ('0.0.0.0', SERVER_PORT)
SimpleHTTPRequestHandler.protocol_version = "HTTP/1.0"
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
httpd.serve_forever()
if __name__ == '__main__':
test()
@veraposeidon
Copy link
Author

Inspired by Kevin Smith's implementation. https://github.com/ksmith97/GzipSimpleHTTPServer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment