From 04b9ad065ec02257f6fb13b850176d23f2845572 Mon Sep 17 00:00:00 2001 From: Ramkumar Rajanbabu Date: Tue, 19 May 2020 17:18:48 -0700 Subject: [PATCH 1/4] started assignment --- http_server.py | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/http_server.py b/http_server.py index 58d7386..f2389b1 100644 --- a/http_server.py +++ b/http_server.py @@ -20,13 +20,22 @@ 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 can't do that on this server!", + ]) def response_not_found(): @@ -45,7 +54,12 @@ def parse_request(request): """ # 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): """ @@ -119,19 +133,23 @@ 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. + + # 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() conn.sendall(response) except: From c635aad559d2a5e7234bfe2408a9948afdcd4db1 Mon Sep 17 00:00:00 2001 From: Ramkumar Rajanbabu Date: Thu, 21 May 2020 14:27:58 -0700 Subject: [PATCH 2/4] still need to work on response path --- http_server.py | 73 ++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/http_server.py b/http_server.py index f2389b1..769d93d 100644 --- a/http_server.py +++ b/http_server.py @@ -2,6 +2,10 @@ 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 @@ -18,8 +22,6 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):

Welcome:

\r\n ''' """ - - # TODO: Implement response_ok return b"\r\n".join([ b"HTTP/1.1 200 OK", b"Content-Type: " + mimetype, @@ -27,10 +29,9 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"): body, ]) + def response_method_not_allowed(): """Returns a 405 Method Not Allowed response""" - - # TODO: Implement response_method_not_allowed return b"\r\n".join([ b"HTTP/1.1 405 Method Not Allowed", b"", @@ -40,9 +41,11 @@ def response_method_not_allowed(): 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 Method Not Found", + b"", + b"Not found!", + ]) def parse_request(request): @@ -52,15 +55,12 @@ 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 method, path, version = request.split("\r\n")[0].split(" ") - if method != "GET": raise NotImplementedError - return path + def response_path(path): """ This method should return appropriate content and a mime type. @@ -88,21 +88,18 @@ def response_path(path): response_path('/a_page_that_doesnt_exist.html') -> Raises a NameError """ - - # 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`. + # See the assignment guidelines for help on "mapping mime-types" content = b"not implemented" mime_type = b"not implemented" + mime_type = mimetypes.guess_type('file.txt')[0] + mime_type = mimetypes.types_map['.txt'] + # Raise a NameError if the requested content is not present under webroot. + else: + raise NameError + return content, mime_type @@ -129,28 +126,27 @@ 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: + # Retrieve path from request path = parse_request(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" - ) + + # Use response_path to retrieve the content and mimetype, based on request path + content, mimetype = response_path + + # Use content and mimetype from response_path to build a response_ok + response = response_ok(content, mimetype) + + # If NotImplementedError, then response is method_not_allowed response except NotImplementedError: - response = response_method_not_allowed() - + response = response_method_not_allowed() # Error 405 + + # If NameError, then response is not_found response + except NameError: + response = response_not_found() # Error 404 + + # Send response conn.sendall(response) except: traceback.print_exc() @@ -160,6 +156,7 @@ def server(log_buffer=sys.stderr): except KeyboardInterrupt: sock.close() return + except: traceback.print_exc() From d45848782646fa1f06685e03e5f2c68bf54793a4 Mon Sep 17 00:00:00 2001 From: Ramkumar Rajanbabu Date: Thu, 11 Jun 2020 17:55:17 -0700 Subject: [PATCH 3/4] Finished assignment and passed all tests --- http_server.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/http_server.py b/http_server.py index 769d93d..e6e45d9 100644 --- a/http_server.py +++ b/http_server.py @@ -88,18 +88,21 @@ def response_path(path): response_path('/a_page_that_doesnt_exist.html') -> Raises a NameError """ - # TODO: Fill in the appropriate content and mime_type give the path. - # See the assignment guidelines for help on "mapping mime-types" - - content = b"not implemented" - mime_type = b"not implemented" - - mime_type = mimetypes.guess_type('file.txt')[0] - mime_type = mimetypes.types_map['.txt'] + home_path = os.getcwd() + "/webroot" + path + + if os.path.isdir(home_path): + mime_type = b"text/plain" + content = ('\r\n, '.join(os.listdir(home_path))).encode('utf8') + + elif os.path.isfile(home_path): + mime_type = mimetypes.guess_type(path)[0].encode('utf8') + with open(home_path, 'rb') as file: + content = file.read() + # Raise a NameError if the requested content is not present under webroot. else: - raise NameError - + raise NameError + return content, mime_type @@ -133,10 +136,10 @@ def server(log_buffer=sys.stderr): path = parse_request(request) # Use response_path to retrieve the content and mimetype, based on request path - content, mimetype = response_path + content, mime_type = response_path(path) # Use content and mimetype from response_path to build a response_ok - response = response_ok(content, mimetype) + response = response_ok(content, mime_type) # If NotImplementedError, then response is method_not_allowed response except NotImplementedError: @@ -164,5 +167,3 @@ def server(log_buffer=sys.stderr): if __name__ == '__main__': server() sys.exit(0) - - From 8579948d281995349f13efe14a643282bd90f655 Mon Sep 17 00:00:00 2001 From: Ramkumar Rajanbabu Date: Thu, 11 Jun 2020 18:01:34 -0700 Subject: [PATCH 4/4] Completed Assignment 3 --- http_server.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/http_server.py b/http_server.py index e6e45d9..64d2ef1 100644 --- a/http_server.py +++ b/http_server.py @@ -90,14 +90,16 @@ def response_path(path): """ home_path = os.getcwd() + "/webroot" + path + # URI is a directory, then return plain-text listing of the contents and mime_type if os.path.isdir(home_path): - mime_type = b"text/plain" content = ('\r\n, '.join(os.listdir(home_path))).encode('utf8') + mime_type = b"text/plain" + # URI is a file, then return contents of file and mime_type elif os.path.isfile(home_path): - mime_type = mimetypes.guess_type(path)[0].encode('utf8') with open(home_path, 'rb') as file: content = file.read() + mime_type = mimetypes.guess_type(path)[0].encode('utf8') # Raise a NameError if the requested content is not present under webroot. else: