From 1f32e6f34231f0b7d02baf1c4b900bc3fe0e53ad Mon Sep 17 00:00:00 2001 From: Ethan Nguyen Date: Sat, 16 Nov 2019 14:26:57 -0800 Subject: [PATCH] Updated http_server.py for HW --- http_server.py | 100 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 20 deletions(-) diff --git a/http_server.py b/http_server.py index 58d7386..3733915 100644 --- a/http_server.py +++ b/http_server.py @@ -1,6 +1,9 @@ import socket import sys import traceback +import mimetypes +import os +import subprocess def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): """ @@ -20,20 +23,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 are not allowed to do this on my 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"What you looking for are not found on my server!!" + ]) def parse_request(request): @@ -44,8 +60,11 @@ 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 not in "GET": + raise NotImplementedError + + return path def response_path(path): """ @@ -74,6 +93,35 @@ def response_path(path): response_path('/a_page_that_doesnt_exist.html') -> Raises a NameError """ + content = b"\r\n" + + path = os.getcwd()+ "/webroot" + path + + if not os.path.exists(path): + raise NameError + + if os.path.isdir(path): + for filename in os.listdir(path): + content = content + filename.encode("utf-8") + b"\r\n" + mime_type = b"text/plain" + + if os.path.isfile(path): + + if path.endswith('.py'): + + content = subprocess.check_output([sys.executable, path, ""]) + mime_type = b"text/plain" + + else: + with open(path, "rb") as f: + content =f.read() + # byte = b" " + # while byte: + # # Do stuff with byte. + # byte = f.read(1) + # content = content + b"".join([byte]) + + mime_type = mimetypes.guess_type(path)[0].encode('utf-8') # TODO: Raise a NameError if the requested content is not present # under webroot. @@ -86,8 +134,8 @@ 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" + # content = b"not implemented" + # mime_type = b"not implemented" return content, mime_type @@ -119,19 +167,31 @@ 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. + + content, 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 = content, + mimetype= mimetype + # body=b"Welcome to my web server", + # mimetype=b"text/plain" + ) + except NotImplementedError: + response = response_method_not_allowed() + + except NameError: + response = response_not_found() conn.sendall(response) except: