-
Notifications
You must be signed in to change notification settings - Fork 13
/
GzipSimpleHTTPServer.py
313 lines (259 loc) · 10.6 KB
/
GzipSimpleHTTPServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""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"]
import os
import posixpath
import BaseHTTPServer
import urllib
import cgi
import sys
import mimetypes
import zlib
import webbrowser
from optparse import OptionParser
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
SERVER_PORT = 8000
encoding_type = 'gzip'
source = ''
source_files = ['index.html', 'index.htm', 'index.html.gz', 'index.htm.gz', 'index.gz']
def parse_options():
# Option parsing logic.
parser = OptionParser()
global encoding_type
parser.add_option("-e", "--encoding", dest="encoding_type",
help="Encoding type for server to utilize",
metavar=encoding_type, default=encoding_type)
global SERVER_PORT
parser.add_option("-p", "--port", dest="port", default=SERVER_PORT,
help="The port to serve the files on",
metavar=SERVER_PORT)
global source
parser.add_option("-s", "--source", dest="source",
help="Pick what file to serve ['index.html', 'index.htm', 'index.html.gz', 'index.htm.gz', 'index.gz'], default will serve in this order",
metavar="index.htm.gz", default='')
(options, args) = parser.parse_args()
encoding_type = options.encoding_type
SERVER_PORT = int(options.port)
source = options.source
global source_type
base, ext = posixpath.splitext(source)
ext = ext.lower()
if ext == '.gz':
source_type = 'gzipped'
else:
source_type = 'normal'
global source_files
if encoding_type not in ['zlib', 'deflate', 'gzip']:
sys.stderr.write("Please provide a valid encoding_type for the server to utilize.\n")
sys.stderr.write("Possible values are 'zlib', 'gzip', and 'deflate'\n")
sys.stderr.write("Usage: python GzipSimpleHTTPServer.py --encoding=<encoding_type>\n")
sys.exit()
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):
gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
data = gzip_compress.compress(content) + gzip_compress.flush()
return data
class SimpleHTTPRequestHandler(BaseHTTPServer.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)
f = None
type = 'normal'
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
if not source == '':
index = os.path.join(path, source)
if os.path.exists(index):
path = index
else:
return self.list_directory(path).read()
else:
for index in source_files:
index = os.path.join(path, index)
if os.path.exists(index):
base, ext = posixpath.splitext(index)
ext = ext.lower()
if ext == '.gz':
type = 'gzipped'
path = index
break
else:
return self.list_directory(path).read()
if not source_type == 'normal':
type = source_type
ctype = self.guess_type(path, type)
print("Serving path '%s'" % 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
self.send_response(200)
self.send_header("Content-type", ctype)
self.send_header("Content-Encoding", encoding_type)
fs = os.fstat(f.fileno())
raw_content_length = fs[6]
content = f.read()
if type == 'normal':
# Encode content based on runtime arg
if encoding_type == "gzip":
content = gzip_encode(content)
elif encoding_type == "deflate":
content = deflate_encode(content)
elif encoding_type == "zlib":
content = zlib_encode(content)
compressed_content_length = len(content)
f.close()
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.unquote(self.path))
f.write('<!DOCTYPE html>')
f.write("<html>\n<title>GUI Easy %s</title>\n" % displaypath)
f.write("<body>\n<h2>Directory: %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.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.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, type):
"""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 type == 'gzipped':
return self.extensions_map['.gzipped']
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
'.gzipped': 'text/html',
'.py': 'text/plain',
'.c': 'text/plain',
'.h': 'text/plain'
})
def test(HandlerClass = SimpleHTTPRequestHandler,
ServerClass = BaseHTTPServer.HTTPServer):
"""Run the HTTP request handler class.
This runs an HTTP server on port 8000 (or the first command line
argument).
"""
parse_options()
server_address = ('localhost', SERVER_PORT)
SimpleHTTPRequestHandler.protocol_version = "HTTP/1.0"
httpd = BaseHTTPServer.HTTPServer(server_address, SimpleHTTPRequestHandler)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
url = 'http://localhost:' + str(sa[1])
webbrowser.open(url)
httpd.serve_forever()
BaseHTTPServer.test(HandlerClass, ServerClass)
if __name__ == '__main__':
test()