2015-09-03 21:09:35 +00:00
|
|
|
import http.server
|
2016-07-05 07:24:08 +00:00
|
|
|
import mimetypes
|
2015-10-03 23:29:52 +00:00
|
|
|
import os
|
2016-07-05 07:24:08 +00:00
|
|
|
import urllib.parse
|
2016-07-20 03:31:47 +00:00
|
|
|
import pathlib
|
2016-07-05 07:24:08 +00:00
|
|
|
import random
|
2016-07-20 03:31:47 +00:00
|
|
|
import socketserver
|
2016-07-05 07:24:08 +00:00
|
|
|
import sys
|
2016-07-20 03:31:47 +00:00
|
|
|
import types
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-10 04:38:49 +00:00
|
|
|
sys.path.append('C:\\git\\else\\Bytestring'); import bytestring
|
|
|
|
sys.path.append('C:\\git\\else\\Ratelimiter'); import ratelimiter
|
2016-07-20 03:31:47 +00:00
|
|
|
sys.path.append('C:\\git\\else\\SpinalTap'); import spinal
|
2015-09-03 21:09:35 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
FILE_READ_CHUNK = bytestring.MIBIBYTE
|
|
|
|
|
|
|
|
#f = open('favicon.png', 'rb')
|
|
|
|
#FAVI = f.read()
|
|
|
|
#f.close()
|
2016-07-05 07:24:08 +00:00
|
|
|
CWD = os.getcwd()
|
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
# The paths which the user may access.
|
|
|
|
# Attempting to access anything outside will 403.
|
|
|
|
# These are convered to Path objects after that class definition.
|
|
|
|
OKAY_PATHS = set(['files', 'favicon.ico'])
|
|
|
|
|
2016-07-05 07:24:08 +00:00
|
|
|
OPENDIR_TEMPLATE = '''
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<style type="text/css">Body {{font-family:Consolas}}</style>
|
|
|
|
<table style="width: 95%">
|
|
|
|
{entries}
|
|
|
|
</table>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
'''
|
|
|
|
|
|
|
|
class Path:
|
2016-07-20 03:31:47 +00:00
|
|
|
'''
|
|
|
|
I started to use pathlib.Path, but it was too much of a pain.
|
|
|
|
'''
|
2016-07-05 07:24:08 +00:00
|
|
|
def __init__(self, path):
|
2016-07-20 03:31:47 +00:00
|
|
|
path = urllib.parse.unquote(path)
|
|
|
|
path = path.strip('/')
|
|
|
|
path = os.path.normpath(path)
|
|
|
|
path = spinal.get_path_casing(path).path
|
|
|
|
self.absolute_path = path
|
|
|
|
|
|
|
|
def __contains__(self, other):
|
|
|
|
return other.absolute_path.startswith(self.absolute_path)
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.absolute_path)
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
@property
|
|
|
|
def allowed(self):
|
|
|
|
return any(self in okay for okay in OKAY_PATHS)
|
2016-07-05 07:24:08 +00:00
|
|
|
|
|
|
|
def anchor(self, display_name=None):
|
|
|
|
if display_name is None:
|
|
|
|
display_name = self.basename
|
2016-07-20 03:31:47 +00:00
|
|
|
|
2016-07-05 07:24:08 +00:00
|
|
|
if self.is_dir:
|
|
|
|
# Folder emoji
|
|
|
|
icon = '\U0001F4C1'
|
|
|
|
else:
|
|
|
|
# Diamond emoji, because there's not one for files.
|
|
|
|
icon = '\U0001F48E'
|
2016-07-10 04:38:49 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
#print('anchor', path)
|
2016-07-10 04:38:49 +00:00
|
|
|
a = '<a href="{full}">{icon} {display}</a>'.format(
|
2016-07-20 03:31:47 +00:00
|
|
|
full=self.url_path,
|
2016-07-10 04:38:49 +00:00
|
|
|
icon=icon,
|
|
|
|
display=display_name,
|
|
|
|
)
|
|
|
|
return a
|
2016-07-05 07:24:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def basename(self):
|
2016-07-20 03:31:47 +00:00
|
|
|
return os.path.basename(self.absolute_path)
|
2016-07-05 07:24:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_dir(self):
|
2016-07-20 03:31:47 +00:00
|
|
|
return os.path.isdir(self.absolute_path)
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-10 04:38:49 +00:00
|
|
|
@property
|
|
|
|
def is_file(self):
|
2016-07-20 03:31:47 +00:00
|
|
|
return os.path.isfile(self.absolute_path)
|
2016-07-05 07:24:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def parent(self):
|
2016-07-20 03:31:47 +00:00
|
|
|
parent = os.path.dirname(self.absolute_path)
|
|
|
|
parent = Path(parent)
|
|
|
|
return parent
|
2016-07-05 07:24:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def relative_path(self):
|
2016-07-20 03:31:47 +00:00
|
|
|
relative = self.absolute_path
|
|
|
|
relative = relative.replace(CWD, '')
|
|
|
|
relative = relative.lstrip(os.sep)
|
|
|
|
return relative
|
2016-07-05 07:24:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def size(self):
|
2016-07-20 03:31:47 +00:00
|
|
|
if self.is_file:
|
|
|
|
return os.path.getsize(self.absolute_path)
|
|
|
|
else:
|
|
|
|
return None
|
2016-07-05 07:24:08 +00:00
|
|
|
|
|
|
|
def table_row(self, display_name=None, shaded=False):
|
2016-07-20 03:31:47 +00:00
|
|
|
form = '<tr style="background-color:#{bg}"><td style="width:90%">{anchor}</td><td>{size}</td></tr>'
|
|
|
|
size = self.size
|
|
|
|
if size is None:
|
|
|
|
size = ''
|
|
|
|
else:
|
|
|
|
size = bytestring.bytestring(size)
|
|
|
|
|
2016-07-05 07:24:08 +00:00
|
|
|
bg = 'ddd' if shaded else 'fff';
|
2016-07-10 04:38:49 +00:00
|
|
|
row = form.format(
|
|
|
|
bg=bg,
|
|
|
|
anchor=self.anchor(display_name=display_name),
|
|
|
|
size=size,
|
|
|
|
)
|
2016-07-05 07:24:08 +00:00
|
|
|
return row
|
2015-09-03 21:09:35 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
@property
|
|
|
|
def url_path(self):
|
|
|
|
url = self.relative_path
|
|
|
|
url = url.replace(os.sep, '/')
|
|
|
|
url = '/' + url
|
|
|
|
url = urllib.parse.quote(url)
|
|
|
|
return url
|
|
|
|
|
|
|
|
OKAY_PATHS = set(Path(p) for p in OKAY_PATHS)
|
2015-10-03 23:29:52 +00:00
|
|
|
|
2015-09-03 21:09:35 +00:00
|
|
|
class RequestHandler(http.server.BaseHTTPRequestHandler):
|
2016-07-20 03:31:47 +00:00
|
|
|
def write(self, data):
|
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode('utf-8')
|
|
|
|
if isinstance(data, types.GeneratorType):
|
|
|
|
for chunk in data:
|
|
|
|
self.wfile.write(chunk)
|
|
|
|
else:
|
|
|
|
self.wfile.write(data)
|
2015-09-03 21:09:35 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
def read_filebytes(self, path, range_min=None, range_max=None):
|
2015-10-03 23:29:52 +00:00
|
|
|
#print(path)
|
2016-07-20 03:31:47 +00:00
|
|
|
|
|
|
|
if path.is_file:
|
|
|
|
if range_min is None:
|
|
|
|
range_min = 0
|
|
|
|
|
|
|
|
if range_max is None:
|
|
|
|
range_max = path.size
|
|
|
|
|
|
|
|
range_span = range_max - range_min
|
|
|
|
|
|
|
|
#print('read span', range_min, range_max, range_span)
|
|
|
|
f = open(path.absolute_path, 'rb')
|
|
|
|
f.seek(range_min)
|
|
|
|
sent_amount = 0
|
|
|
|
while sent_amount < range_span:
|
|
|
|
chunk = f.read(FILE_READ_CHUNK)
|
|
|
|
if len(chunk) == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
yield chunk
|
|
|
|
sent_amount += len(chunk)
|
|
|
|
|
|
|
|
#print('I read', len(fr))
|
2015-10-03 23:29:52 +00:00
|
|
|
f.close()
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
elif path.is_dir:
|
2016-07-05 07:24:08 +00:00
|
|
|
text = generate_opendir(path)
|
|
|
|
text = text.encode('utf-8')
|
2016-07-20 03:31:47 +00:00
|
|
|
yield text
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
else:
|
|
|
|
self.send_error(404)
|
|
|
|
yield bytes()
|
2015-10-03 23:29:52 +00:00
|
|
|
|
2015-09-03 21:09:35 +00:00
|
|
|
def do_GET(self):
|
|
|
|
#print(dir(self))
|
2016-07-20 03:31:47 +00:00
|
|
|
path = Path(self.path)
|
2016-07-10 04:38:49 +00:00
|
|
|
if self.send_path_validation_error(path):
|
2015-10-03 23:29:52 +00:00
|
|
|
return
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
range_min = None
|
|
|
|
range_max = None
|
|
|
|
|
|
|
|
status_code = 200
|
|
|
|
headers = {}
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
if path.is_file:
|
|
|
|
file_size = path.size
|
|
|
|
if 'range' in self.headers:
|
|
|
|
desired_range = self.headers['range']
|
|
|
|
desired_range = desired_range.lower()
|
|
|
|
desired_range = desired_range.split('bytes=')[-1]
|
|
|
|
|
|
|
|
helper = lambda x: int(x) if x and x.isdigit() else None
|
|
|
|
if '-' in desired_range:
|
|
|
|
(desired_min, desired_max) = desired_range.split('-')
|
|
|
|
#print('desire', desired_min, desired_max)
|
|
|
|
range_min = helper(desired_min)
|
|
|
|
range_max = helper(desired_max)
|
|
|
|
else:
|
|
|
|
range_min = helper(desired_range)
|
|
|
|
|
|
|
|
if range_min is None:
|
|
|
|
range_min = 0
|
|
|
|
if range_max is None:
|
|
|
|
range_max = file_size
|
|
|
|
|
|
|
|
# because ranges are 0 indexed
|
|
|
|
range_max = min(range_max, file_size - 1)
|
|
|
|
range_min = max(range_min, 0)
|
|
|
|
|
|
|
|
status_code = 206
|
|
|
|
range_header = 'bytes {min}-{max}/{outof}'.format(
|
|
|
|
min=range_min,
|
|
|
|
max=range_max,
|
|
|
|
outof=file_size,
|
|
|
|
)
|
|
|
|
headers['Content-Range'] = range_header
|
|
|
|
headers['Accept-Ranges'] = 'bytes'
|
|
|
|
content_length = (range_max - range_min) + 1
|
|
|
|
|
|
|
|
else:
|
|
|
|
content_length = file_size
|
|
|
|
|
|
|
|
headers['Content-length'] = content_length
|
|
|
|
|
|
|
|
mime = mimetypes.guess_type(path.absolute_path)[0]
|
2016-07-05 07:24:08 +00:00
|
|
|
if mime is not None:
|
|
|
|
#print(mime)
|
2016-07-20 03:31:47 +00:00
|
|
|
headers['Content-type'] = mime
|
2016-07-10 04:38:49 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
self.send_response(status_code)
|
|
|
|
for (key, value) in headers.items():
|
|
|
|
self.send_header(key, value)
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
d = self.read_filebytes(path, range_min=range_min, range_max=range_max)
|
2016-07-10 04:38:49 +00:00
|
|
|
#print('write')
|
|
|
|
self.end_headers()
|
2015-10-03 23:29:52 +00:00
|
|
|
self.write(d)
|
2015-09-03 21:09:35 +00:00
|
|
|
|
2016-07-10 04:38:49 +00:00
|
|
|
def do_HEAD(self):
|
2016-07-20 03:31:47 +00:00
|
|
|
path = Path(self.path)
|
2016-07-10 04:38:49 +00:00
|
|
|
if self.send_path_validation_error(path):
|
|
|
|
return
|
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
status_code = 200
|
2016-08-03 02:06:59 +00:00
|
|
|
self.send_response(status_code)
|
2016-07-10 04:38:49 +00:00
|
|
|
|
|
|
|
if path.is_dir:
|
|
|
|
mime = 'text/html'
|
|
|
|
else:
|
2016-07-20 03:31:47 +00:00
|
|
|
mime = mimetypes.guess_type(path.absolute_path)[0]
|
|
|
|
self.send_header('Content-length', path.size)
|
2016-07-10 04:38:49 +00:00
|
|
|
|
|
|
|
if mime is not None:
|
|
|
|
self.send_header('Content-type', mime)
|
|
|
|
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def send_path_validation_error(self, path):
|
2016-07-20 03:31:47 +00:00
|
|
|
if not path.allowed:
|
|
|
|
self.send_error(403, 'Stop that!')
|
2016-07-10 04:38:49 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
|
|
|
|
class ThreadedServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
|
|
|
|
'''
|
|
|
|
Thanks root and twasbrillig http://stackoverflow.com/a/14089457
|
|
|
|
'''
|
|
|
|
pass
|
|
|
|
|
2016-07-05 07:24:08 +00:00
|
|
|
|
|
|
|
def generate_opendir(path):
|
2016-07-10 04:38:49 +00:00
|
|
|
#print('Listdir:', path)
|
2016-07-20 03:31:47 +00:00
|
|
|
items = os.listdir(path.absolute_path)
|
|
|
|
items = [os.path.join(path.absolute_path, f) for f in items]
|
|
|
|
#print(items)
|
2016-07-10 04:38:49 +00:00
|
|
|
|
|
|
|
# This places directories above files, each ordered alphabetically
|
|
|
|
items.sort(key=str.lower)
|
2016-07-05 07:24:08 +00:00
|
|
|
directories = []
|
|
|
|
files = []
|
|
|
|
for item in items:
|
|
|
|
if os.path.isdir(item):
|
|
|
|
directories.append(item)
|
|
|
|
else:
|
|
|
|
files.append(item)
|
2016-07-10 04:38:49 +00:00
|
|
|
|
|
|
|
items = directories + files
|
|
|
|
items = [Path(f) for f in items]
|
2016-07-05 07:24:08 +00:00
|
|
|
entries = []
|
2016-07-20 03:31:47 +00:00
|
|
|
|
|
|
|
if any(path.absolute_path == okay.absolute_path for okay in OKAY_PATHS):
|
|
|
|
# This is different than a permission check, we're seeing if they're
|
|
|
|
# actually at the top, in which case they don't need an up button.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
entry = path.parent.table_row(display_name='up')
|
|
|
|
entries.append(entry)
|
2016-07-10 04:38:49 +00:00
|
|
|
|
2016-07-05 07:24:08 +00:00
|
|
|
shaded = True
|
2016-07-10 04:38:49 +00:00
|
|
|
for item in items:
|
|
|
|
entry = item.table_row(shaded=shaded)
|
2016-07-05 07:24:08 +00:00
|
|
|
entries.append(entry)
|
|
|
|
shaded = not shaded
|
2016-07-10 04:38:49 +00:00
|
|
|
|
2016-07-05 07:24:08 +00:00
|
|
|
entries = '\n'.join(entries)
|
|
|
|
text = OPENDIR_TEMPLATE.format(entries=entries)
|
|
|
|
return text
|
|
|
|
|
|
|
|
def generate_random_filename(original_filename='', length=8):
|
|
|
|
import random
|
2016-07-20 03:31:47 +00:00
|
|
|
bits = length * 44
|
2016-07-05 07:24:08 +00:00
|
|
|
bits = random.getrandbits(bits)
|
|
|
|
identifier = '{:x}'.format(bits).rjust(length, '0')
|
|
|
|
return identifier
|
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
def main():
|
|
|
|
server = ThreadedServer(('', 32768), RequestHandler)
|
|
|
|
print('server starting')
|
|
|
|
server.serve_forever()
|
2016-07-05 07:24:08 +00:00
|
|
|
|
2016-07-20 03:31:47 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|