-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttpserver.py
290 lines (262 loc) · 10.3 KB
/
httpserver.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
import BaseHTTPServer
import os
import posixpath
import urllib
import re
import shutil
import mimetypes
import cgi
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
class GPGUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
INDEX = "index.html"
LIST_HEAD = "list-head.htmlpart"
LIST_FOOT = "list-foot.htmlpart"
UPLOAD_DIRECTORY = "file-upload"
# if servers handles a GET request, it executes this method
def do_GET(self):
"""Serve a GET request."""
print("GET " + self.path)
root = self.path.split('/')[1]
# if requesting anything outside the uploading file directory, serve as html
print(root)
if(root != self.UPLOAD_DIRECTORY):
self.serve_html()
else:
# listing requested upload directory
if(os.path.isdir(self.translate_path(self.path))):
self.serve_upload_directory()
else:
# try to download file
self.serve_stored_file()
# if servers handles a POST request, it executes this method
def do_POST(self):
"""Serve a POST request."""
print("POST " + self.path)
# if upload directory
if(os.path.isdir(self.UPLOAD_DIRECTORY + "/" + self.path)):
self.path = self.UPLOAD_DIRECTORY + "/" + self.path
self.upload_file()
else:
print("wrong upload destination")
self.path = "/"
self.serve_html()
# serve html files, like any http server
def serve_html(self):
print("Serving html")
if(self.path == '/'):
self.path = self.INDEX
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(self.translate_path(self.path), 'rb')
except IOError:
# self.send_error(404, "File not found" + self.path)
self.serve_404()
return None
self.send_response(200)
self.send_header("Content-type", self.guess_type(self.translate_path(self.path)))
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
shutil.copyfileobj(f, self.wfile)
f.close()
# list selected upload directory content
def serve_upload_directory(self):
print("upload dir")
path = self.translate_path(self.path)
dirlist = os.listdir(self.translate_path(self.path))
dirlist.sort(key=lambda a: a.lower())
try:
hd = open(self.LIST_HEAD, 'rb')
except IOError:
self.send_error(404, "File not found" + self.path)
return None
head = hd.read()
hd.close()
try:
ft = open(self.LIST_FOOT, 'rb')
except IOError:
self.send_error(404, "File not found" + self.path)
return None
foot = ft.read()
ft.close()
f = StringIO()
f.write(head)
displaypath = cgi.escape(urllib.unquote(self.path))
# link to parent directory
f.write('<li class="parent-dir"><a href="%s"><i class="fa fa-folder-open"></i>Parent directory: %s</a></li>\n' % (urllib.quote(os.path.dirname(os.path.dirname(self.path))), cgi.escape(os.path.dirname(os.path.dirname(self.path)))))
for name in dirlist:
print(path)
fullname = os.path.join(path, name)
displayname = name
if(self.path[-1] != "/"):
self.path = self.path + "/"
linkname = self.path + name
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
css_class="dir"
icon="folder"
displayname = displayname + "/"
linkname = linkname + "/"
else:
css_class="file"
icon="file"
if os.path.islink(fullname):
displayname = displayname + "@"
# Note: a link to a directory displays with @ and links with /
f.write('<li class="%s"><a href="%s"><i class="fa fa-%s"></i>%s</a></li>\n' % (css_class, urllib.quote(linkname), icon, cgi.escape(displayname)))
f.write(foot)
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
shutil.copyfileobj(f, self.wfile)
f.close()
# download stored file to client
def serve_stored_file(self):
#TODO download file or 404
print("Serving file")
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(self.translate_path(self.path), 'rb')
except IOError:
# self.send_error(404, "File not found" + self.path)
self.serve_404()
return None
self.send_response(200)
self.send_header("Content-type", "application/octet-stream")
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Cache-Control", "no-cache")
#self.send_header("Content-Disposition", "attachment")
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
shutil.copyfileobj(f, self.wfile)
f.close()
# serve 404.html instead of ugly 404 response
def serve_404(self):
print("Serving 404")
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!
print(self.translate_path("404.html"))
print(self.path)
f = open(self.translate_path("404.html"), 'rb')
except IOError:
self.send_error(404, "File not found" + self.path)
return None
self.send_response(404)
self.send_header("Content-type", 'text/html')
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
shutil.copyfileobj(f, self.wfile)
f.close()
# upload file to server
def upload_file(self):
print("uploading file")
boundary = self.headers.plisttext.split("=")[1]
remainbytes = int(self.headers['content-length'])
line = self.rfile.readline()
remainbytes -= len(line)
if not boundary in line:
return (False, "Content NOT begin with boundary")
line = self.rfile.readline()
remainbytes -= len(line)
fn = re.findall(r'Content-Disposition.*name="file"; filename="(.*)"', line)
if not fn:
return (False, "Can't find out file name...")
path = self.translate_path(self.path)
fn = os.path.join(path, fn[0])
print(fn)
line = self.rfile.readline()
remainbytes -= len(line)
line = self.rfile.readline()
remainbytes -= len(line)
try:
out = open(fn, 'wb')
except IOError:
return (False, "Can't create file to write, do you have permission to write?")
preline = self.rfile.readline()
remainbytes -= len(preline)
while remainbytes > 0:
line = self.rfile.readline()
remainbytes -= len(line)
if boundary in line:
preline = preline[0:-1]
if preline.endswith('\r'):
preline = preline[0:-1]
out.write(preline)
out.close()
return (True, "File '%s' upload success!" % fn)
else:
out.write(preline)
preline = line
return (False, "Unexpect Ends of data.")
################
# tool methods #
################
# tool method to translate path
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
# tool method to guess mime type depending of file
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.
"""
if not mimetypes.inited:
mimetypes.init() # try to read system mime.types
extensions_map = mimetypes.types_map.copy()
base, ext = posixpath.splitext(path)
if ext in extensions_map:
return extensions_map[ext]
else:
return 'text/plain'
#############
# class end #
#############
##################
# running server #
##################
def run(server_class=BaseHTTPServer.HTTPServer, handler_class=GPGUploadHandler):
print(handler_class)
server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
add = server_address[0] if len(server_address[0])>0 else "localhost"
print("Serving http on " + add + " on port " + str(server_address[1]))
httpd.serve_forever()
run()