From 4f45baea6f96278c6754601c982104637244fb5e Mon Sep 17 00:00:00 2001 From: Joseph Schilz Date: Tue, 6 Oct 2020 17:04:14 -0700 Subject: [PATCH] Remove stubs. --- http_server.py | 150 +------------------------------------------------ 1 file changed, 1 insertion(+), 149 deletions(-) diff --git a/http_server.py b/http_server.py index 69ebaa3..f226ae3 100644 --- a/http_server.py +++ b/http_server.py @@ -6,126 +6,6 @@ class HttpServer(): - @staticmethod - def make_response( - code, - reason, - body=b"", - mimetype=b"text/plain" - ): - """ - returns a basic HTTP response - Ex: - make_response( - b"200", - b"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 - ''' - """ - - return b"\r\n".join([ - b"HTTP/1.1 " + code + b" " + reason, - b"Content-Type: " + mimetype, - b"", - body - ]) - - @staticmethod - def get_path(request): - """ - Given the content of an HTTP request, return the _path_ - of that request. - - For example, if request were: - - ''' - GET /images/sample_1.png HTTP/1.1 - Host: localhost:1000 - - ''' - - Then you would return "/images/sample_1.png" - """ - - return "TODO: COMPLETE THIS" # TODO - - - @staticmethod - def get_mimetype(path): - """ - This method should return a suitable mimetype for the given `path`. - - A mimetype is a short bytestring that tells a browser how to - interpret the response body. For example, if the response body - contains a web page then the mimetype should be b"text/html". If - the response body contains a JPG image, then the mimetype would - be b"image/jpeg". - - Here are a few concrete examples: - - get_mimetype('/a_web_page.html') -> b"text/html" - - get_mimetype('/images/sample_1.png') -> b"image/png" - - get_mimetype('/') -> b"text/plain" - # A directory listing should have either a plain text mimetype - # or a b"text/html" mimetype if you turn your directory listings - # into web pages. - - get_mimetype('/a_page_that_doesnt_exist.html') -> b"text/html" - # This function should return an appropriate mimetype event - # for files that don't exist. - """ - - if path.endswith('/'): - return b"text/plain" - else: - return b"TODO: FINISH THE REST OF THESE CASES" # TODO - - @staticmethod - def get_content(path): - """ - This method should return the content of the file/directory - indicated by `path`. For example, if path is `/a_web_page.html` - then this function would return the contents of the file - `webroot/a_web_page.html` as a byte string. - - * If the requested path is a directory, then the content should - be a plain-text listing of the contents of that directory. - - * If the path is a file, it should return the contents of that - file. - - * If the indicated path doesn't exist inside of `webroot`, then - raise a FileNotFoundError. - - Here are some concrete examples: - - Ex: - get_content('/a_web_page.html') -> b"

North Carolina..." - # Returns the contents of `webroot/a_web_page.html` - - get_content('/images/sample_1.png') -> b"A12BCF..." - # Returns the contents of `webroot/images/sample_1.png` - - get_content('/') -> images/, a_web_page.html, make_type.py,..." - # Returns a directory listing of `webroot/` - - get_content('/a_page_that_doesnt_exist.html') - # The file `webroot/a_page_that_doesnt_exist.html`) doesn't exist, - # so this should raise a FileNotFoundError. - """ - - return b"Not implemented!" # TODO: Complete this function. - def __init__(self, port): self.port = port @@ -148,35 +28,7 @@ def serve(self): try: print('connection - {0}:{1}'.format(*addr)) - request = '' - while True: - data = conn.recv(1024) - request += data.decode('utf8') - - if '\r\n\r\n' in request: - break - - print("Request received:\n{}\n\n".format(request)) - - path = self.get_path(request) - - try: - body = self.get_content(path) - mimetype = self.get_mimetype(path) - - response = self.make_response( - b"200", b"OK", body, mimetype - ) - - except FileNotFoundError: - body = b"Couldn't find the file you requested." - mimetype = b"text/plain" - - response = self.make_response( - b"404", b"NOT FOUND", body, mimetype - ) - - conn.sendall(response) + # TODO: fill me in! except: traceback.print_exc() finally: