2015-07-11 07:51:30 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2013-11-04 02:35:57 +01:00
|
|
|
import BaseHTTPServer
|
|
|
|
import SimpleHTTPServer
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import ssl
|
2014-01-22 03:59:09 +01:00
|
|
|
import gzip
|
2015-02-08 17:03:09 +01:00
|
|
|
import base64
|
2013-11-04 02:35:57 +01:00
|
|
|
|
|
|
|
chunked_encoding = False
|
2014-06-15 20:02:59 +02:00
|
|
|
keepalive = True
|
2013-11-04 02:35:57 +01:00
|
|
|
|
2014-01-22 05:05:23 +01:00
|
|
|
try:
|
|
|
|
fin = open('test_file', 'rb')
|
|
|
|
f = gzip.open('test_file.gz', 'wb')
|
|
|
|
f.writelines(fin)
|
|
|
|
f.close()
|
|
|
|
fin.close()
|
|
|
|
except:
|
|
|
|
pass
|
2014-01-22 03:59:09 +01:00
|
|
|
|
2013-11-06 05:05:13 +01:00
|
|
|
class http_server_with_timeout(BaseHTTPServer.HTTPServer):
|
|
|
|
allow_reuse_address = True
|
2014-02-05 10:38:32 +01:00
|
|
|
timeout = 190
|
2013-11-06 05:05:13 +01:00
|
|
|
|
|
|
|
def handle_timeout(self):
|
|
|
|
raise Exception('timeout')
|
|
|
|
|
2013-11-04 02:35:57 +01:00
|
|
|
class http_handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
|
|
|
|
|
|
|
|
def do_GET(s):
|
|
|
|
|
2015-01-03 14:09:09 +01:00
|
|
|
print 'INCOMING-REQUEST: ', s.requestline
|
|
|
|
print s.headers
|
|
|
|
|
2013-11-04 02:35:57 +01:00
|
|
|
global chunked_encoding
|
2014-06-15 20:02:59 +02:00
|
|
|
global keepalive
|
2013-11-04 02:35:57 +01:00
|
|
|
|
|
|
|
# if the request contains the hostname and port. strip it
|
|
|
|
if s.path.startswith('http://') or s.path.startswith('https://'):
|
|
|
|
s.path = s.path[8:]
|
|
|
|
s.path = s.path[s.path.find('/'):]
|
|
|
|
|
2014-01-21 10:34:51 +01:00
|
|
|
file_path = os.path.normpath(s.path)
|
2015-06-21 18:41:50 +02:00
|
|
|
print file_path
|
|
|
|
print s.path
|
2013-11-04 02:35:57 +01:00
|
|
|
|
2015-02-08 17:03:09 +01:00
|
|
|
if s.path == '/password_protected':
|
|
|
|
passed = False
|
|
|
|
if 'Authorization' in s.headers:
|
|
|
|
auth = s.headers['Authorization']
|
|
|
|
passed = auth == 'Basic %s' % base64.b64encode('testuser:testpass')
|
|
|
|
|
|
|
|
if not passed:
|
|
|
|
s.send_response(401)
|
|
|
|
s.send_header("Connection", "close")
|
|
|
|
s.end_headers()
|
|
|
|
return
|
|
|
|
|
|
|
|
s.path = '/test_file'
|
|
|
|
file_path = os.path.normpath('/test_file')
|
|
|
|
|
2013-11-04 02:35:57 +01:00
|
|
|
if s.path == '/redirect':
|
|
|
|
s.send_response(301)
|
|
|
|
s.send_header("Location", "/test_file")
|
|
|
|
s.send_header("Connection", "close")
|
|
|
|
s.end_headers()
|
|
|
|
elif s.path == '/infinite_redirect':
|
|
|
|
s.send_response(301)
|
|
|
|
s.send_header("Location", "/infinite_redirect")
|
|
|
|
s.send_header("Connection", "close")
|
|
|
|
s.end_headers()
|
|
|
|
elif s.path == '/relative/redirect':
|
|
|
|
s.send_response(301)
|
|
|
|
s.send_header("Location", "../test_file")
|
|
|
|
s.send_header("Connection", "close")
|
|
|
|
s.end_headers()
|
|
|
|
elif s.path.startswith('/announce'):
|
|
|
|
s.send_response(200)
|
2015-08-29 22:04:35 +02:00
|
|
|
response = 'd8:intervali1800e8:completei1e10:incompletei1e' + \
|
|
|
|
'5:peers12:AAAABBCCCCDD' + \
|
|
|
|
'6:peers618:EEEEEEEEEEEEEEEEFF' + \
|
|
|
|
'e'
|
2013-11-04 02:35:57 +01:00
|
|
|
s.send_header("Content-Length", "%d" % len(response))
|
|
|
|
s.send_header("Connection", "close")
|
|
|
|
s.end_headers()
|
|
|
|
s.wfile.write(response)
|
|
|
|
elif os.path.split(s.path)[1].startswith('seed?'):
|
|
|
|
query = s.path[6:]
|
|
|
|
args_raw = query.split('&')
|
|
|
|
args = {}
|
|
|
|
for a in args_raw:
|
|
|
|
kvp = a.split('=')
|
|
|
|
args[kvp[0]] = kvp[1]
|
|
|
|
piece = int(args['piece'])
|
|
|
|
ranges = args['ranges'].split('-')
|
|
|
|
|
2015-01-09 15:02:48 +01:00
|
|
|
filename = ''
|
2013-11-04 02:35:57 +01:00
|
|
|
try:
|
2015-01-09 15:02:48 +01:00
|
|
|
filename = os.path.normpath(s.path[1:s.path.find('seed?') + 4])
|
|
|
|
print 'filename = %s' % filename
|
2013-11-04 02:35:57 +01:00
|
|
|
f = open(filename, 'rb')
|
|
|
|
f.seek(piece * 64 * 1024 + int(ranges[0]))
|
|
|
|
data = f.read(int(ranges[1]) - int(ranges[0]) + 1)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
s.send_response(200)
|
|
|
|
print 'sending %d bytes' % len(data)
|
|
|
|
s.send_header("Content-Length", "%d" % len(data))
|
|
|
|
s.end_headers()
|
|
|
|
s.wfile.write(data);
|
|
|
|
except Exception, e:
|
2015-01-09 15:02:48 +01:00
|
|
|
print 'FILE ERROR: ', filename, e
|
2013-11-04 02:35:57 +01:00
|
|
|
s.send_response(404)
|
|
|
|
s.send_header("Content-Length", "0")
|
|
|
|
s.end_headers()
|
|
|
|
else:
|
2015-01-09 15:02:48 +01:00
|
|
|
filename = ''
|
2013-11-04 02:35:57 +01:00
|
|
|
try:
|
2015-01-09 15:02:48 +01:00
|
|
|
filename = os.path.normpath(file_path[1:])
|
2013-11-04 02:35:57 +01:00
|
|
|
# serve file by invoking default handler
|
2014-01-21 01:04:52 +01:00
|
|
|
f = open(filename, 'rb')
|
2013-11-04 02:35:57 +01:00
|
|
|
size = int(os.stat(filename).st_size)
|
|
|
|
start_range = 0
|
|
|
|
end_range = size
|
|
|
|
if 'Range' in s.headers:
|
|
|
|
s.send_response(206)
|
|
|
|
st, e = s.headers['range'][6:].split('-', 1)
|
|
|
|
sl = len(st)
|
|
|
|
el = len(e)
|
|
|
|
if sl > 0:
|
|
|
|
start_range = int(st)
|
|
|
|
if el > 0:
|
|
|
|
end_range = int(e) + 1
|
|
|
|
elif el > 0:
|
|
|
|
ei = int(e)
|
|
|
|
if ei < size:
|
|
|
|
start_range = size - ei
|
2015-01-03 14:09:09 +01:00
|
|
|
s.send_header('Content-Range', 'bytes ' + str(start_range) \
|
|
|
|
+ '-' + str(end_range - 1) + '/' + str(size))
|
2013-11-04 02:35:57 +01:00
|
|
|
else:
|
|
|
|
s.send_response(200)
|
|
|
|
s.send_header('Accept-Ranges', 'bytes')
|
|
|
|
if chunked_encoding:
|
|
|
|
s.send_header('Transfer-Encoding', 'chunked')
|
|
|
|
s.send_header('Content-Length', end_range - start_range)
|
|
|
|
if filename.endswith('.gz'):
|
|
|
|
s.send_header('Content-Encoding', 'gzip')
|
2014-06-15 20:02:59 +02:00
|
|
|
if not keepalive:
|
|
|
|
s.send_header("Connection", "close")
|
2015-01-05 17:17:23 +01:00
|
|
|
try:
|
2015-07-16 06:44:03 +02:00
|
|
|
s.request.shutdown();
|
2015-01-05 17:17:23 +01:00
|
|
|
except Exception, e:
|
|
|
|
print 'Failed to shutdown read-channel of socket: ', e
|
2014-06-15 20:02:59 +02:00
|
|
|
|
2013-11-04 02:35:57 +01:00
|
|
|
s.end_headers()
|
2015-06-21 18:41:50 +02:00
|
|
|
|
2013-11-04 02:35:57 +01:00
|
|
|
f.seek(start_range)
|
|
|
|
length = end_range - start_range
|
|
|
|
while length > 0:
|
|
|
|
to_send = min(length, 0x900)
|
|
|
|
if chunked_encoding:
|
|
|
|
s.wfile.write('%x\r\n' % to_send)
|
|
|
|
data = f.read(to_send)
|
2015-01-11 08:45:06 +01:00
|
|
|
print 'read %d bytes' % to_send
|
2013-11-04 02:35:57 +01:00
|
|
|
s.wfile.write(data)
|
|
|
|
if chunked_encoding:
|
|
|
|
s.wfile.write('\r\n')
|
|
|
|
length -= to_send
|
2015-01-11 08:45:06 +01:00
|
|
|
print 'sent %d bytes (%d bytes left)' % (len(data), length)
|
2013-11-04 02:35:57 +01:00
|
|
|
if chunked_encoding:
|
|
|
|
s.wfile.write('0\r\n\r\n')
|
|
|
|
except Exception, e:
|
2015-01-09 15:02:48 +01:00
|
|
|
print 'FILE ERROR: ', filename, e
|
2013-11-04 02:35:57 +01:00
|
|
|
s.send_response(404)
|
|
|
|
s.send_header("Content-Length", "0")
|
|
|
|
s.end_headers()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
port = int(sys.argv[1])
|
|
|
|
chunked_encoding = sys.argv[2] != '0'
|
2013-11-04 11:06:14 +01:00
|
|
|
use_ssl = sys.argv[3] != '0'
|
2014-06-15 20:02:59 +02:00
|
|
|
keepalive = sys.argv[4] != '0'
|
2013-11-04 02:35:57 +01:00
|
|
|
|
|
|
|
http_handler.protocol_version = 'HTTP/1.1'
|
2013-11-06 05:05:13 +01:00
|
|
|
httpd = http_server_with_timeout(('127.0.0.1', port), http_handler)
|
2013-11-04 11:06:14 +01:00
|
|
|
if use_ssl:
|
2013-11-04 02:35:57 +01:00
|
|
|
httpd.socket = ssl.wrap_socket(httpd.socket, certfile='../ssl/server.pem', server_side=True)
|
|
|
|
|
2013-11-06 05:05:13 +01:00
|
|
|
while True:
|
|
|
|
httpd.handle_request()
|