From 4e534a5e12c02bb1286a7e2f090681e4382e5234 Mon Sep 17 00:00:00 2001 From: Nick Lenssen Date: Wed, 7 Oct 2020 23:05:08 -0400 Subject: [PATCH] Assignment 3 --- .DS_Store | Bin 0 -> 6148 bytes http_server.py | 67 +++++++++++++++++++++++++++++++++---------------- tests.py | 2 -- 3 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..ed6f2a82585a638bb8a59116703c09e110afbc36 GIT binary patch literal 6148 zcmeHKJFWsT4733WBpOP}T;bgyLT~~ufcSJsEAc?ITX8Oq#`sf!7CI5L zh}R;bv(xrZWFjIXxS?EZ=$h@DcdVBY1;TMggXg&Y-|W{-C;NH8xI;P1PS$d`=Qn%X zqEP`VKn17(6`%rdRv-)PX#D1Lc^nm>0^hHI-46wBSQFbozdA5@3jiD-?1s7b62M{s zU`=cT5rJt?fkD-5F*N9im&~h)ZD7zvv-!|`vu1~){&t*SJYBQ~a-;%O;8B5oEGJg~ z7w{MT|09VjDnJE(N&y|rm-87uDQj!zi80h5~3oFM{Pl~)^ YbL`i|HqhyaI~~ZM0n>#>1wO674N_JWvj6}9 literal 0 HcmV?d00001 diff --git a/http_server.py b/http_server.py index 58d7386..5b3d1e8 100644 --- a/http_server.py +++ b/http_server.py @@ -20,20 +20,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: text/plain", + b"", + b"Hellow hello", + b"Hellow hello", + ]) 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"Sorry we couldn't find that." + ]) def parse_request(request): @@ -44,8 +57,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): """ @@ -86,9 +103,20 @@ def response_path(path): # result of executing `make_time.py`. But you need only return the # CONTENTS of `make_time.py`. + home_directory = os.path.abspath("webroot") + location_path = os.path.join(home_directory, path[1:]) + if not os.path.exists(location_path): + raise NameError + content = b"not implemented" mime_type = b"not implemented" - + if os.path.isdir(location_path): + mime_type = mimetypes.types_map['.txt'].encode() + content = "\n".join(os.listdir(location_path)).encode() + elif os.path.isfile(location_path): + mime_type = mimetypes.guess_type(location_path)[0].encode() + with open(location_path, 'rb') as my_file: + content = my_file.read() return content, mime_type @@ -110,7 +138,7 @@ def server(log_buffer=sys.stderr): request = '' while True: data = conn.recv(1024) - request += data.decode('utf8') + request += data.decode() if '\r\n\r\n' in request: break @@ -118,20 +146,15 @@ 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) + content, mime_type = response_path(path) + response = response_ok( + body=content, + mimetype=mime_type + ) + except NotImplementedError: + response = response_method_not_allowed() conn.sendall(response) except: diff --git a/tests.py b/tests.py index 21da57e..cc52a9d 100644 --- a/tests.py +++ b/tests.py @@ -39,10 +39,8 @@ def test_post_yields_method_not_allowed(self): """ Sending a POST request should yield a 405 Method Not Allowed response """ - conn = http.client.HTTPConnection('localhost:10000') conn.request('POST', '/') - response = conn.getresponse() conn.close()