diff --git a/http_server.py b/http_server.py index 58d7386..4b68acb 100644 --- a/http_server.py +++ b/http_server.py @@ -1,6 +1,9 @@ +import os import socket import sys import traceback +import mimetypes + def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): """ @@ -19,22 +22,30 @@ 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"We haven't written that code yet." + ]) 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"We do not have that resource." + ]) def parse_request(request): """ @@ -44,8 +55,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 != 'GET': + raise NotImplementedError + return path def response_path(path): """ @@ -77,6 +91,27 @@ def response_path(path): # TODO: Raise a NameError if the requested content is not present # under webroot. + # if path == '/': + # path = '' + # elif path.startswith('/'): + # path = path[-1:] + try: + with open('webroot' + path, 'rb') as resource: + content = resource.read() + mime_type, encoding = mimetypes.guess_type(path) + mime_type = mime_type.encode('utf-8') + except IsADirectoryError: + content = '' + for entry in os.listdir('webroot' + path): + content += entry + '\r\n' + content = content.encode('utf-8') + mime_type = b'text/plain' + except OSError: + 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,8 +121,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 @@ -114,24 +149,31 @@ 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. - # TODO: Use response_path to retrieve the content and the mimetype, - # based on the request path. + print("Request received:\n{}\n\n".format(request)) - # 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, mimetype = response_path(path) + response = response_ok(body=content, mimetype=mimetype) + + # 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" + # ) + except NotImplementedError: + response = response_method_not_allowed() + except NameError: + response = response_not_found() conn.sendall(response) except: