From 7eea643d9caaf5e666b3cf00403724a8f09ba380 Mon Sep 17 00:00:00 2001 From: b_galloway Date: Sun, 10 Jan 2021 18:40:00 -0800 Subject: [PATCH] lesson 3 assignment submission --- http_server.py | 76 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/http_server.py b/http_server.py index 58d7386..4fec7b4 100644 --- a/http_server.py +++ b/http_server.py @@ -1,8 +1,10 @@ import socket -import sys +import os,sys import traceback +import mimetypes -def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): + +def response_ok(body, mimetype): """ returns a basic HTTP response Ex: @@ -20,20 +22,33 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): """ # TODO: Implement response_ok - return b"" + return b"\r\n".join([ + b"HTTP/1.1 200 OK", + b"Content-Type: " + mimetype, + b"", + body + ]) def response_method_not_allowed(): """Returns a 405 Method Not Allowed response""" # TODO: Implement response_method_not_allowed - return b"" + return b"\r\n".join([ + b"HTTP/1.1 405 Method Not Allowed", + b"", + b"You can't do that on this server!" + ]) def response_not_found(): """Returns a 404 Not Found response""" # TODO: Implement response_not_found - return b"" + return b"\r\n".join([ + b"HTTP/1.1 404 Not Found", + b"", + b"Resource not found" + ]) def parse_request(request): @@ -43,9 +58,11 @@ def parse_request(request): This server only handles GET requests, so this method shall raise a NotImplementedError if the method of the request is not GET. """ + method, path, version = request.split("\r\n")[0].split(" ") + if method != "GET": + raise NotImplementedError - # TODO: implement parse_request - return "" + return path def response_path(path): """ @@ -77,6 +94,16 @@ def response_path(path): # TODO: Raise a NameError if the requested content is not present # under webroot. + path = os.path.abspath(os.path.dirname(__file__)) + "/webroot" + path + + if os.path.isdir(path): + content = "\n".join(os.listdir(path)).encode() + mime_type = b"text/plain" + elif os.path.isfile(path): + content = open(path, 'rb').read() + mime_type = mimetypes.guess_type(path)[0].encode() + else: + 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 @@ -85,9 +112,6 @@ def response_path(path): # If the path is "make_time.py", then you may OPTIONALLY return the # 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" return content, mime_type @@ -119,20 +143,24 @@ 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) + + # TODO: Use response_path to retrieve the content and the mimetype, + # based on the request path. + body, mimetype = response_path(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, mimetype) + + except NotImplementedError: + response = response_method_not_allowed() + except NameError: + response = response_not_found() conn.sendall(response) except: traceback.print_exc()