From 1033fcb2514898afaf888900b48999f9297f5a93 Mon Sep 17 00:00:00 2001 From: Colleen Kaku Date: Mon, 15 Feb 2021 10:55:24 -0800 Subject: [PATCH] completed socket-http-server project --- http_server.py | 98 +++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/http_server.py b/http_server.py index 58d7386..fec1143 100644 --- a/http_server.py +++ b/http_server.py @@ -1,39 +1,38 @@ import socket import sys import traceback +import os +import mimetypes def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): - """ - returns a basic HTTP response - Ex: - response_ok( - b"

Welcome:

", - b"text/html" - ) -> - - b''' - HTTP/1.1 200 OK\r\n - Content-Type: text/html\r\n - \r\n -

Welcome:

\r\n - ''' - """ - - # TODO: Implement response_ok - return b"" + """returns a basic HTTP response""" + 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"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"404 - Page Not Found", + b"", + b"That page cannot be found :(", + ]) def parse_request(request): @@ -43,9 +42,10 @@ 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. """ - - # TODO: implement parse_request - return "" + method, path, version = request.split("\r\n")[0].split(" ") + if method != "GET": + raise NotImplementedError + return path def response_path(path): """ @@ -75,20 +75,22 @@ def response_path(path): """ - # TODO: Raise a NameError if the requested content is not present - # under webroot. - - # TODO: Fill in the appropriate content and mime_type give the path. - # See the assignment guidelines for help on "mapping mime-types", though - # you might need to create a special case for handling make_time.py - # - # 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" + path = "webroot" + path + + if not os.path.exists(path): + raise NameError + + if os.path.isdir(path): + content = "\r\n".join(os.listdir(path)).encode() + mime_type = b"text/plain" + else: + with open(path, "rb") as f: + content = f.read() + mime_type = mimetypes.guess_type(path)[0].encode() + return content, mime_type @@ -118,20 +120,16 @@ 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. + try: + path = parse_request(request) + body, mimetype = response_path(path) + response = response_ok(body, mimetype) - # TODO: Use response_path to retrieve the content and the mimetype, - # based on the request path. + except NotImplementedError: + response = response_method_not_allowed() - # 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" - ) + except NameError: + response = response_not_found() conn.sendall(response) except: