From 0718782d19b6ae74323ca79fc10482632490d90b Mon Sep 17 00:00:00 2001 From: ajaggarcia10 <45190522+ajaggarcia10@users.noreply.github.com> Date: Sun, 8 Mar 2020 16:40:09 -0700 Subject: [PATCH] Finished http_server --- http_server.py | 85 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/http_server.py b/http_server.py index 58d7386..221f35b 100644 --- a/http_server.py +++ b/http_server.py @@ -1,8 +1,11 @@ import socket import sys import traceback +import os +import mimetypes -def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): + +def response_ok(body=b"This is a minimal response", mime_type=b"text/plain"): """ returns a basic HTTP response Ex: @@ -19,21 +22,32 @@ 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: ' + mime_type, + 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"Page not found!", + ]) def parse_request(request): @@ -44,8 +58,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 + + return path + def response_path(path): """ @@ -75,19 +93,33 @@ 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" + + content = b"" + mime_type = b"" + + directory = os.path.abspath('webroot') + path = '\\' + path[1:] + pathname = directory + path + + if os.path.exists(pathname): + if os.path.isdir(pathname): + mime_type = b'text/plain' + dir_files = os.listdir(pathname) + for item in dir_files: + content += item.encode() + b'\r\n' + + if os.path.isfile(pathname): + mime_type = mimetypes.guess_type(pathname)[0].encode() + with open(pathname, 'rb') as f: + content = f.read() + else: + raise NameError return content, mime_type @@ -114,24 +146,19 @@ def server(log_buffer=sys.stderr): if '\r\n\r\n' in request: break - print("Request received:\n{}\n\n".format(request)) - # TODO: Use parse_request to retrieve the path from the request. + try: + path = parse_request(request) - # TODO: Use response_path to retrieve the content and the mimetype, - # based on the request path. + body, mime_type = 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=b"Welcome to my web server", - mimetype=b"text/plain" - ) + response = response_ok(body, mime_type) + except NotImplementedError: + response = response_method_not_allowed() + except NameError: + response = response_not_found() conn.sendall(response) except: