2014-07-24 06:55:44 -04:00
|
|
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
2015-05-11 06:34:05 -04:00
|
|
|
from exc.server_error import ServerError, AuthError, NoBodyServerError
|
2014-07-24 07:11:25 -04:00
|
|
|
from socketserver import BaseServer
|
2014-07-24 06:55:44 -04:00
|
|
|
from posixpath import basename, splitext
|
|
|
|
from base64 import b64encode
|
|
|
|
from random import random
|
|
|
|
from hashlib import md5
|
|
|
|
import threading
|
2014-07-24 07:11:25 -04:00
|
|
|
import socket
|
|
|
|
import os
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2014-10-08 07:27:22 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
class StoppableHTTPServer(HTTPServer):
|
2014-08-08 01:54:08 -04:00
|
|
|
""" This class extends the HTTPServer class from default http.server library
|
|
|
|
in Python 3. The StoppableHTTPServer class is capable of starting an HTTP
|
|
|
|
server that serves a virtual set of files made by the WgetFile class and
|
|
|
|
has most of its properties configurable through the server_conf()
|
|
|
|
method. """
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
request_headers = list()
|
2014-07-24 06:55:44 -04:00
|
|
|
|
|
|
|
""" Define methods for configuring the Server. """
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def server_conf(self, filelist, conf_dict):
|
2014-07-24 06:55:44 -04:00
|
|
|
""" Set Server Rules and File System for this instance. """
|
|
|
|
self.server_configs = conf_dict
|
|
|
|
self.fileSys = filelist
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def get_req_headers(self):
|
2014-07-24 06:55:44 -04:00
|
|
|
return self.request_headers
|
|
|
|
|
2014-07-24 07:11:25 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
class HTTPSServer(StoppableHTTPServer):
|
2014-08-08 01:54:08 -04:00
|
|
|
""" The HTTPSServer class extends the StoppableHTTPServer class with
|
|
|
|
additional support for secure connections through SSL. """
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def __init__(self, address, handler):
|
2014-10-08 07:27:22 -04:00
|
|
|
import ssl
|
2015-04-14 01:06:20 -04:00
|
|
|
BaseServer.__init__(self, address, handler)
|
|
|
|
# step one up because test suite change directory away from $srcdir
|
|
|
|
# (don't do that !!!)
|
|
|
|
CERTFILE = os.path.abspath(os.path.join('..',
|
|
|
|
os.getenv('srcdir', '.'),
|
|
|
|
'certs',
|
|
|
|
'server-cert.pem'))
|
|
|
|
KEYFILE = os.path.abspath(os.path.join('..',
|
|
|
|
os.getenv('srcdir', '.'),
|
|
|
|
'certs',
|
|
|
|
'server-key.pem'))
|
|
|
|
self.socket = ssl.wrap_socket(
|
|
|
|
sock=socket.socket(self.address_family, self.socket_type),
|
|
|
|
ssl_version=ssl.PROTOCOL_TLSv1,
|
|
|
|
certfile=CERTFILE,
|
|
|
|
keyfile=KEYFILE,
|
|
|
|
server_side=True
|
2014-08-08 01:54:08 -04:00
|
|
|
)
|
|
|
|
self.server_bind()
|
|
|
|
self.server_activate()
|
|
|
|
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
class _Handler(BaseHTTPRequestHandler):
|
2014-08-08 01:54:08 -04:00
|
|
|
""" This is a private class which tells the server *HOW* to handle each
|
|
|
|
request. For each HTTP Request Command that the server should be capable of
|
|
|
|
responding to, there must exist a do_REQUESTNAME() method which details the
|
|
|
|
steps in which such requests should be processed. The rest of the methods
|
|
|
|
in this class are auxilliary methods created to help in processing certain
|
|
|
|
requests. """
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def get_rule_list(self, name):
|
2014-10-08 07:27:22 -04:00
|
|
|
return self.rules.get(name)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2014-08-08 01:54:08 -04:00
|
|
|
# The defailt protocol version of the server we run is HTTP/1.1 not
|
|
|
|
# HTTP/1.0 which is the default with the http.server module.
|
2014-07-24 06:55:44 -04:00
|
|
|
protocol_version = 'HTTP/1.1'
|
|
|
|
|
|
|
|
""" Define functions for various HTTP Requests. """
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def do_HEAD(self):
|
|
|
|
self.send_head("HEAD")
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def do_GET(self):
|
2014-08-08 01:54:08 -04:00
|
|
|
""" Process HTTP GET requests. This is the same as processing HEAD
|
|
|
|
requests and then actually transmitting the data to the client. If
|
|
|
|
send_head() does not specify any "start" offset, we send the complete
|
|
|
|
data, else transmit only partial data. """
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
content, start = self.send_head("GET")
|
2014-07-24 06:55:44 -04:00
|
|
|
if content:
|
|
|
|
if start is None:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.wfile.write(content.encode('utf-8'))
|
2014-07-24 06:55:44 -04:00
|
|
|
else:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.wfile.write(content.encode('utf-8')[start:])
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def do_POST(self):
|
2014-08-08 01:54:08 -04:00
|
|
|
""" According to RFC 7231 sec 4.3.3, if the resource requested in a POST
|
|
|
|
request does not exist on the server, the first POST request should
|
|
|
|
create that resource. PUT requests are otherwise used to create a
|
|
|
|
resource. Hence, we call the handle for processing PUT requests if the
|
|
|
|
resource requested does not already exist.
|
|
|
|
|
|
|
|
Currently, when the server recieves a POST request for a resource, we
|
|
|
|
simply append the body data to the existing file and return the new
|
|
|
|
file to the client. If the file does not exist, a new file is created
|
|
|
|
using the contents of the request body. """
|
|
|
|
|
2014-07-24 06:55:44 -04:00
|
|
|
path = self.path[1:]
|
|
|
|
if path in self.server.fileSys:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.rules = self.server.server_configs.get(path)
|
2014-08-08 01:54:08 -04:00
|
|
|
if not self.rules:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.rules = dict()
|
2014-08-08 01:54:08 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
if not self.custom_response():
|
|
|
|
return(None, None)
|
2014-08-08 01:54:08 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
body_data = self.get_body_data()
|
|
|
|
self.send_response(200)
|
|
|
|
self.add_header("Content-type", "text/plain")
|
|
|
|
content = self.server.fileSys.pop(path) + "\n" + body_data
|
|
|
|
total_length = len(content)
|
2014-07-24 06:55:44 -04:00
|
|
|
self.server.fileSys[path] = content
|
2015-04-14 01:06:20 -04:00
|
|
|
self.add_header("Content-Length", total_length)
|
|
|
|
self.add_header("Location", self.path)
|
|
|
|
self.finish_headers()
|
2014-07-24 06:55:44 -04:00
|
|
|
try:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.wfile.write(content.encode('utf-8'))
|
2014-07-24 06:55:44 -04:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
else:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_put(path)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def do_PUT(self):
|
2014-07-24 06:55:44 -04:00
|
|
|
path = self.path[1:]
|
2015-04-14 01:06:20 -04:00
|
|
|
self.rules = self.server.server_configs.get(path)
|
|
|
|
if not self.custom_response():
|
|
|
|
return(None, None)
|
|
|
|
self.send_put(path)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
|
|
|
""" End of HTTP Request Method Handlers. """
|
|
|
|
|
|
|
|
""" Helper functions for the Handlers. """
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def parse_range_header(self, header_line, length):
|
2014-10-08 07:27:22 -04:00
|
|
|
import re
|
2014-07-24 06:55:44 -04:00
|
|
|
if header_line is None:
|
|
|
|
return None
|
2015-04-14 01:06:20 -04:00
|
|
|
if not header_line.startswith("bytes="):
|
|
|
|
raise ServerError("Cannot parse header Range: %s" %
|
|
|
|
(header_line))
|
|
|
|
regex = re.match(r"^bytes=(\d*)\-$", header_line)
|
|
|
|
range_start = int(regex.group(1))
|
2014-07-24 06:55:44 -04:00
|
|
|
if range_start >= length:
|
2015-04-14 01:06:20 -04:00
|
|
|
raise ServerError("Range Overflow")
|
2014-07-24 06:55:44 -04:00
|
|
|
return range_start
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def get_body_data(self):
|
|
|
|
cLength_header = self.headers.get("Content-Length")
|
|
|
|
cLength = int(cLength_header) if cLength_header is not None else 0
|
|
|
|
body_data = self.rfile.read(cLength).decode('utf-8')
|
2014-07-24 06:55:44 -04:00
|
|
|
return body_data
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def send_put(self, path):
|
2014-08-08 01:54:08 -04:00
|
|
|
if path in self.server.fileSys:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.server.fileSys.pop(path, None)
|
|
|
|
self.send_response(204)
|
2014-08-08 01:54:08 -04:00
|
|
|
else:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.rules = dict()
|
|
|
|
self.send_response(201)
|
|
|
|
body_data = self.get_body_data()
|
2014-07-24 06:55:44 -04:00
|
|
|
self.server.fileSys[path] = body_data
|
2015-04-14 01:06:20 -04:00
|
|
|
self.add_header("Location", self.path)
|
|
|
|
self.finish_headers()
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2014-08-08 01:54:08 -04:00
|
|
|
""" This empty method is called automatically when all the rules are
|
|
|
|
processed for a given request. However, send_header() should only be called
|
|
|
|
AFTER a response has been sent. But, at the moment of processing the rules,
|
|
|
|
the appropriate response has not yet been identified. As a result, we defer
|
|
|
|
the processing of this rule till later. Each do_* request handler MUST call
|
|
|
|
finish_headers() instead of end_headers(). The finish_headers() method
|
|
|
|
takes care of sending the appropriate headers before completing the
|
|
|
|
response. """
|
2015-04-14 01:06:20 -04:00
|
|
|
def SendHeader(self, header_obj):
|
2014-07-24 06:55:44 -04:00
|
|
|
pass
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def send_cust_headers(self):
|
|
|
|
header_obj = self.get_rule_list('SendHeader')
|
2014-07-24 06:55:44 -04:00
|
|
|
if header_obj:
|
|
|
|
for header in header_obj.headers:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.add_header(header, header_obj.headers[header])
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def finish_headers(self):
|
|
|
|
self.send_cust_headers()
|
2015-03-14 09:01:48 -04:00
|
|
|
try:
|
|
|
|
for keyword, value in self._headers_dict.items():
|
2015-06-22 14:46:01 -04:00
|
|
|
if isinstance(value, list):
|
|
|
|
for value_el in value:
|
|
|
|
self.send_header(keyword, value_el)
|
|
|
|
else:
|
|
|
|
self.send_header(keyword, value)
|
2015-03-14 09:01:48 -04:00
|
|
|
# Clear the dictionary of existing headers for the next request
|
|
|
|
self._headers_dict.clear()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2015-04-14 01:06:20 -04:00
|
|
|
self.end_headers()
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def Response(self, resp_obj):
|
|
|
|
self.send_response(resp_obj.response_code)
|
|
|
|
self.finish_headers()
|
2015-05-11 06:34:05 -04:00
|
|
|
if resp_obj.response_code == 304:
|
|
|
|
raise NoBodyServerError("Conditional get falling to head")
|
2015-04-14 01:06:20 -04:00
|
|
|
raise ServerError("Custom Response code sent.")
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def custom_response(self):
|
|
|
|
codes = self.get_rule_list('Response')
|
2014-07-24 06:55:44 -04:00
|
|
|
if codes:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_response(codes.response_code)
|
|
|
|
self.finish_headers()
|
2014-07-24 06:55:44 -04:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def add_header(self, keyword, value):
|
|
|
|
if not hasattr(self, "_headers_dict"):
|
2015-03-14 09:01:48 -04:00
|
|
|
self._headers_dict = dict()
|
|
|
|
self._headers_dict[keyword.lower()] = value
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def base64(self, data):
|
|
|
|
string = b64encode(data.encode('utf-8'))
|
|
|
|
return string.decode('utf-8')
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-03-14 09:01:48 -04:00
|
|
|
""" Send an authentication challenge.
|
|
|
|
This method calls self.send_header() directly instead of using the
|
|
|
|
add_header() method because sending multiple WWW-Authenticate headers
|
|
|
|
actually makes sense and we do use that feature in some tests. """
|
2015-09-22 07:08:40 -04:00
|
|
|
def send_challenge(self, auth_type, auth_parm):
|
2014-11-17 11:16:54 -05:00
|
|
|
auth_type = auth_type.lower()
|
|
|
|
if auth_type == "both":
|
2015-09-22 07:08:40 -04:00
|
|
|
self.send_challenge("basic", auth_parm)
|
|
|
|
self.send_challenge("digest", auth_parm)
|
2014-07-24 06:55:44 -04:00
|
|
|
return
|
2014-11-17 11:16:54 -05:00
|
|
|
if auth_type == "basic":
|
|
|
|
challenge_str = 'BasIc realm="Wget-Test"'
|
|
|
|
elif auth_type == "digest" or auth_type == "both_inline":
|
2015-04-14 01:06:20 -04:00
|
|
|
self.nonce = md5(str(random()).encode('utf-8')).hexdigest()
|
|
|
|
self.opaque = md5(str(random()).encode('utf-8')).hexdigest()
|
2014-11-17 11:16:54 -05:00
|
|
|
# 'DIgest' to provoke a Wget failure with turkish locales
|
|
|
|
challenge_str = 'DIgest realm="Test", nonce="%s", opaque="%s"' % (
|
2014-08-08 01:54:08 -04:00
|
|
|
self.nonce,
|
|
|
|
self.opaque)
|
2015-09-22 07:08:40 -04:00
|
|
|
try:
|
|
|
|
if auth_parm['qop']:
|
|
|
|
challenge_str += ', qop="%s"' % auth_parm['qop']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-11-17 11:16:54 -05:00
|
|
|
if auth_type == "both_inline":
|
|
|
|
# 'BasIc' to provoke a Wget failure with turkish locales
|
|
|
|
challenge_str = 'BasIc realm="Wget-Test", ' + challenge_str
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_header("WWW-Authenticate", challenge_str)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def authorize_basic(self, auth_header, auth_rule):
|
2014-11-17 11:16:54 -05:00
|
|
|
if auth_header is None or auth_header.split(' ')[0].lower() != 'basic':
|
2014-07-24 06:55:44 -04:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
self.user = auth_rule.auth_user
|
|
|
|
self.passw = auth_rule.auth_pass
|
2015-04-14 01:06:20 -04:00
|
|
|
auth_str = "basic " + self.base64(self.user + ":" + self.passw)
|
2014-11-17 11:16:54 -05:00
|
|
|
return True if auth_str.lower() == auth_header.lower() else False
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def parse_auth_header(self, auth_header):
|
2014-11-17 11:16:54 -05:00
|
|
|
n = len("digest ")
|
2014-07-24 06:55:44 -04:00
|
|
|
auth_header = auth_header[n:].strip()
|
|
|
|
items = auth_header.split(", ")
|
2014-08-08 01:54:08 -04:00
|
|
|
keyvals = [i.split("=", 1) for i in items]
|
|
|
|
keyvals = [(k.strip(), v.strip().replace('"', '')) for k, v in keyvals]
|
|
|
|
return dict(keyvals)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def KD(self, secret, data):
|
|
|
|
return self.H(secret + ":" + data)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def H(self, data):
|
|
|
|
return md5(data.encode('utf-8')).hexdigest()
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def A1(self):
|
2014-07-24 06:55:44 -04:00
|
|
|
return "%s:%s:%s" % (self.user, "Test", self.passw)
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def A2(self, params):
|
2014-07-24 06:55:44 -04:00
|
|
|
return "%s:%s" % (self.command, params["uri"])
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def check_response(self, params):
|
2014-07-24 06:55:44 -04:00
|
|
|
if "qop" in params:
|
|
|
|
data_str = params['nonce'] \
|
2014-08-08 01:54:08 -04:00
|
|
|
+ ":" + params['nc'] \
|
|
|
|
+ ":" + params['cnonce'] \
|
|
|
|
+ ":" + params['qop'] \
|
2015-04-14 01:06:20 -04:00
|
|
|
+ ":" + self.H(self.A2(params))
|
2014-07-24 06:55:44 -04:00
|
|
|
else:
|
2015-04-14 01:06:20 -04:00
|
|
|
data_str = params['nonce'] + ":" + self.H(self.A2(params))
|
|
|
|
resp = self.KD(self.H(self.A1()), data_str)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
|
|
|
return True if resp == params['response'] else False
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def authorize_digest(self, auth_header, auth_rule):
|
|
|
|
if auth_header is None or \
|
|
|
|
auth_header.split(' ')[0].lower() != 'digest':
|
2014-07-24 06:55:44 -04:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
self.user = auth_rule.auth_user
|
|
|
|
self.passw = auth_rule.auth_pass
|
2015-04-14 01:06:20 -04:00
|
|
|
params = self.parse_auth_header(auth_header)
|
2014-07-24 06:55:44 -04:00
|
|
|
if self.user != params['username'] or \
|
2015-01-30 08:07:11 -05:00
|
|
|
self.nonce != params['nonce'] or \
|
|
|
|
self.opaque != params['opaque']:
|
|
|
|
return False
|
2014-07-24 06:55:44 -04:00
|
|
|
req_attribs = ['username', 'realm', 'nonce', 'uri', 'response']
|
|
|
|
for attrib in req_attribs:
|
2014-08-08 01:54:08 -04:00
|
|
|
if attrib not in params:
|
2015-01-30 08:07:11 -05:00
|
|
|
return False
|
2015-04-14 01:06:20 -04:00
|
|
|
if not self.check_response(params):
|
2015-01-30 08:07:11 -05:00
|
|
|
return False
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def authorize_both(self, auth_header, auth_rule):
|
2014-07-24 06:55:44 -04:00
|
|
|
return False
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def authorize_both_inline(self, auth_header, auth_rule):
|
2014-07-24 06:55:44 -04:00
|
|
|
return False
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def Authentication(self, auth_rule):
|
2014-07-24 06:55:44 -04:00
|
|
|
try:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.handle_auth(auth_rule)
|
2015-01-30 08:07:11 -05:00
|
|
|
except AuthError as se:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_response(401, "Authorization Required")
|
2015-09-22 07:08:40 -04:00
|
|
|
self.send_challenge(auth_rule.auth_type, auth_rule.auth_parm)
|
2015-04-14 01:06:20 -04:00
|
|
|
self.finish_headers()
|
2015-01-30 08:07:11 -05:00
|
|
|
raise se
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def handle_auth(self, auth_rule):
|
2014-07-24 06:55:44 -04:00
|
|
|
is_auth = True
|
2015-04-14 01:06:20 -04:00
|
|
|
auth_header = self.headers.get("Authorization")
|
2014-11-17 11:16:54 -05:00
|
|
|
required_auth = auth_rule.auth_type.lower()
|
|
|
|
if required_auth == "both" or required_auth == "both_inline":
|
2015-04-14 01:06:20 -04:00
|
|
|
if auth_header:
|
|
|
|
auth_type = auth_header.split(' ')[0].lower()
|
|
|
|
else:
|
|
|
|
auth_type = required_auth
|
2014-07-24 06:55:44 -04:00
|
|
|
else:
|
|
|
|
auth_type = required_auth
|
2014-07-24 07:11:25 -04:00
|
|
|
try:
|
2015-04-14 01:06:20 -04:00
|
|
|
assert hasattr(self, "authorize_" + auth_type)
|
|
|
|
is_auth = getattr(self, "authorize_" + auth_type)(auth_header,
|
|
|
|
auth_rule)
|
2014-07-24 07:11:25 -04:00
|
|
|
except AssertionError:
|
2015-04-14 01:06:20 -04:00
|
|
|
raise AuthError("Authentication Mechanism %s not supported" %
|
|
|
|
auth_type)
|
2014-07-24 07:11:25 -04:00
|
|
|
except AttributeError as ae:
|
2015-04-14 01:06:20 -04:00
|
|
|
raise AuthError(ae.__str__())
|
2014-07-24 06:55:44 -04:00
|
|
|
if is_auth is False:
|
2015-04-14 01:06:20 -04:00
|
|
|
raise AuthError("Unable to Authenticate")
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def ExpectHeader(self, header_obj):
|
2014-07-24 06:55:44 -04:00
|
|
|
exp_headers = header_obj.headers
|
|
|
|
for header_line in exp_headers:
|
2015-04-14 01:06:20 -04:00
|
|
|
header_recd = self.headers.get(header_line)
|
2014-07-24 06:55:44 -04:00
|
|
|
if header_recd is None or header_recd != exp_headers[header_line]:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_error(400, "Expected Header %s not found" %
|
|
|
|
header_line)
|
|
|
|
self.finish_headers()
|
|
|
|
raise ServerError("Header " + header_line + " not found")
|
2014-10-08 07:27:22 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def RejectHeader(self, header_obj):
|
2014-07-24 06:55:44 -04:00
|
|
|
rej_headers = header_obj.headers
|
|
|
|
for header_line in rej_headers:
|
2015-04-14 01:06:20 -04:00
|
|
|
header_recd = self.headers.get(header_line)
|
2016-01-31 11:27:11 -05:00
|
|
|
if header_recd and header_recd == rej_headers[header_line]:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_error(400, 'Blacklisted Header %s received' %
|
|
|
|
header_line)
|
|
|
|
self.finish_headers()
|
|
|
|
raise ServerError("Header " + header_line + ' received')
|
|
|
|
|
|
|
|
def __log_request(self, method):
|
2014-07-24 06:55:44 -04:00
|
|
|
req = method + " " + self.path
|
2015-04-14 01:06:20 -04:00
|
|
|
self.server.request_headers.append(req)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def send_head(self, method):
|
2014-07-24 06:55:44 -04:00
|
|
|
""" Common code for GET and HEAD Commands.
|
|
|
|
This method is overriden to use the fileSys dict.
|
|
|
|
|
|
|
|
The method variable contains whether this was a HEAD or a GET Request.
|
|
|
|
According to RFC 2616, the server should not differentiate between
|
|
|
|
the two requests, however, we use it here for a specific test.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.path == "/":
|
|
|
|
path = "index.html"
|
|
|
|
else:
|
|
|
|
path = self.path[1:]
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
self.__log_request(method)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
|
|
|
if path in self.server.fileSys:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.rules = self.server.server_configs.get(path)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
content = self.server.fileSys.get(path)
|
|
|
|
content_length = len(content)
|
2014-07-24 06:55:44 -04:00
|
|
|
for rule_name in self.rules:
|
|
|
|
try:
|
2015-04-14 01:06:20 -04:00
|
|
|
assert hasattr(self, rule_name)
|
|
|
|
getattr(self, rule_name)(self.rules[rule_name])
|
2014-07-24 06:55:44 -04:00
|
|
|
except AssertionError as ae:
|
2015-01-30 06:16:13 -05:00
|
|
|
msg = "Rule " + rule_name + " not defined"
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_error(500, msg)
|
|
|
|
return(None, None)
|
2015-01-30 08:07:11 -05:00
|
|
|
except AuthError as ae:
|
2015-04-14 01:06:20 -04:00
|
|
|
print(ae.__str__())
|
|
|
|
return(None, None)
|
2015-05-11 06:34:05 -04:00
|
|
|
except NoBodyServerError as nbse:
|
|
|
|
print(nbse.__str__())
|
|
|
|
return(None, None)
|
2014-07-24 06:55:44 -04:00
|
|
|
except ServerError as se:
|
2015-04-14 01:06:20 -04:00
|
|
|
print(se.__str__())
|
|
|
|
return(content, None)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
|
|
|
try:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.range_begin = self.parse_range_header(
|
|
|
|
self.headers.get("Range"), content_length)
|
2014-08-08 01:54:08 -04:00
|
|
|
except ServerError as ae:
|
2014-07-24 06:55:44 -04:00
|
|
|
# self.log_error("%s", ae.err_message)
|
|
|
|
if ae.err_message == "Range Overflow":
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_response(416)
|
|
|
|
self.finish_headers()
|
|
|
|
return(None, None)
|
2014-07-24 06:55:44 -04:00
|
|
|
else:
|
|
|
|
self.range_begin = None
|
|
|
|
if self.range_begin is None:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_response(200)
|
2014-07-24 06:55:44 -04:00
|
|
|
else:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_response(206)
|
|
|
|
self.add_header("Accept-Ranges", "bytes")
|
|
|
|
self.add_header("Content-Range",
|
|
|
|
"bytes %d-%d/%d" % (self.range_begin,
|
|
|
|
content_length - 1,
|
|
|
|
content_length))
|
2014-07-24 06:55:44 -04:00
|
|
|
content_length -= self.range_begin
|
2015-04-14 01:06:20 -04:00
|
|
|
cont_type = self.guess_type(path)
|
|
|
|
self.add_header("Content-Type", cont_type)
|
|
|
|
self.add_header("Content-Length", content_length)
|
|
|
|
self.finish_headers()
|
|
|
|
return(content, self.range_begin)
|
2014-07-24 06:55:44 -04:00
|
|
|
else:
|
2015-04-14 01:06:20 -04:00
|
|
|
self.send_error(404, "Not Found")
|
|
|
|
return(None, None)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def guess_type(self, path):
|
|
|
|
base_name = basename("/" + path)
|
|
|
|
name, ext = splitext(base_name)
|
2014-07-24 06:55:44 -04:00
|
|
|
extension_map = {
|
2015-04-14 01:06:20 -04:00
|
|
|
".txt": "text/plain",
|
|
|
|
".css": "text/css",
|
|
|
|
".html": "text/html"
|
2014-07-24 06:55:44 -04:00
|
|
|
}
|
2014-10-08 07:27:22 -04:00
|
|
|
return extension_map.get(ext, "text/plain")
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
|
|
|
|
class HTTPd(threading.Thread):
|
2014-07-24 06:55:44 -04:00
|
|
|
server_class = StoppableHTTPServer
|
|
|
|
handler = _Handler
|
2014-08-08 01:54:08 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def __init__(self, addr=None):
|
|
|
|
threading.Thread.__init__(self)
|
2014-07-24 06:55:44 -04:00
|
|
|
if addr is None:
|
|
|
|
addr = ('localhost', 0)
|
2015-04-14 01:06:20 -04:00
|
|
|
self.server_inst = self.server_class(addr, self.handler)
|
2014-07-24 06:55:44 -04:00
|
|
|
self.server_address = self.server_inst.socket.getsockname()[:2]
|
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def run(self):
|
|
|
|
self.server_inst.serve_forever()
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
def server_conf(self, file_list, server_rules):
|
|
|
|
self.server_inst.server_conf(file_list, server_rules)
|
2014-07-24 06:55:44 -04:00
|
|
|
|
2014-07-24 07:11:25 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
class HTTPSd(HTTPd):
|
2014-07-24 07:11:25 -04:00
|
|
|
|
2014-08-08 01:54:08 -04:00
|
|
|
server_class = HTTPSServer
|
2014-07-24 07:11:25 -04:00
|
|
|
|
2015-04-14 01:06:20 -04:00
|
|
|
# vim: set ts=4 sts=4 sw=4 tw=79 et :
|