From a1eb27c11af9e79ae82143c0eba216b19ab23e21 Mon Sep 17 00:00:00 2001 From: Jim Blackburn Date: Mon, 10 Sep 2018 00:27:02 -0700 Subject: [PATCH] All tests pass. --- http_server.py | 97 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 27 deletions(-) diff --git a/http_server.py b/http_server.py index 58d7386..b8f1b23 100644 --- a/http_server.py +++ b/http_server.py @@ -1,6 +1,8 @@ import socket import sys import traceback +import mimetypes +import os.path def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): """ @@ -19,21 +21,38 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): ''' """ - # TODO: Implement response_ok - return b"" + response = b"\r\n".join([ + b"HTTP/1.1 200 OK", + b"Content-Type: " + mimetype, + b"", + body + ]) + + return response + def response_method_not_allowed(): """Returns a 405 Method Not Allowed response""" - # TODO: Implement response_method_not_allowed - return b"" + response = b"\r\n".join([ + b"HTTP/1.1 405 METHOD NOT ALLOWED", + b"", + b"You're not allowed to do that on here." + ]) + + return response def response_not_found(): """Returns a 404 Not Found response""" - # TODO: Implement response_not_found - return b"" + response = b"\r\n".join([ + b"HTTP/1.1 404 NOT FOUND", + b"", + b"Requested resource could not be found." + ]) + + return response def parse_request(request): @@ -44,8 +63,12 @@ def parse_request(request): NotImplementedError if the method of the request is not GET. """ - # TODO: implement parse_request - return "" + method, path, version = request.split("\r\n")[0].split(" ") + + if method != "GET": + raise NotImplementedError + print(path) + return path def response_path(path): """ @@ -74,9 +97,10 @@ def response_path(path): response_path('/a_page_that_doesnt_exist.html') -> Raises a NameError """ + path = "webroot" + path - # TODO: Raise a NameError if the requested content is not present - # under webroot. + if not os.path.exists(path): + raise NameError # TODO: Fill in the appropriate content and mime_type give the path. # See the assignment guidelines for help on "mapping mime-types", though @@ -86,14 +110,23 @@ def response_path(path): # result of executing `make_time.py`. But you need only return the # CONTENTS of `make_time.py`. - content = b"not implemented" - mime_type = b"not implemented" + if os.path.isfile(path): + with open(path, 'br') as f: + content = f.read() + content = bytearray(content) + mime_type = mimetypes.guess_type(path)[0].encode() + elif os.path.isdir(path): + files = os.listdir(path) + content = b"" + for f in files: + content += f.encode()+b"\r\n" + mime_type = b'text/plain' return content, mime_type def server(log_buffer=sys.stderr): - address = ('127.0.0.1', 10000) + address = ('localhost', 10000) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) print("making a server on {0}:{1}".format(*address), file=log_buffer) @@ -119,20 +152,30 @@ def server(log_buffer=sys.stderr): print("Request received:\n{}\n\n".format(request)) # TODO: Use parse_request to retrieve the path from the request. - - # TODO: Use response_path to retrieve the content and the mimetype, - # based on the request path. - - # TODO; If parse_request raised a NotImplementedError, then let - # response be a method_not_allowed response. If response_path raised - # a NameError, then let response be a not_found response. Else, - # use the content and mimetype from response_path to build a - # response_ok. - response = response_ok( - body=b"Welcome to my web server", - mimetype=b"text/plain" - ) - + try: + path = parse_request(request) + content, mime_type = response_path(path) + # TODO: Use response_path to retrieve the content and the mimetype, + # based on the request path. + + # TODO; If parse_request raised a NotImplementedError, then let + # response be a method_not_allowed response. If response_path raised + # a NameError, then let response be a not_found response. Else, + # use the content and mimetype from response_path to build a + # response_ok. + + except NotImplementedError: + response = response_method_not_allowed() + + except NameError: + response = response_not_found() + + else: + response = response_ok( + body=content, + mimetype=mime_type + ) + conn.sendall(response) except: traceback.print_exc()