@@ -88,18 +88,21 @@ def response_path(path):
8888 response_path('/a_page_that_doesnt_exist.html') -> Raises a NameError
8989
9090 """
91- # TODO: Fill in the appropriate content and mime_type give the path.
92- # See the assignment guidelines for help on "mapping mime-types"
93-
94- content = b"not implemented"
95- mime_type = b"not implemented"
96-
97- mime_type = mimetypes .guess_type ('file.txt' )[0 ]
98- mime_type = mimetypes .types_map ['.txt' ]
91+ home_path = os .getcwd () + "/webroot" + path
92+
93+ if os .path .isdir (home_path ):
94+ mime_type = b"text/plain"
95+ content = ('\r \n , ' .join (os .listdir (home_path ))).encode ('utf8' )
96+
97+ elif os .path .isfile (home_path ):
98+ mime_type = mimetypes .guess_type (path )[0 ].encode ('utf8' )
99+ with open (home_path , 'rb' ) as file :
100+ content = file .read ()
101+
99102 # Raise a NameError if the requested content is not present under webroot.
100103 else :
101- raise NameError
102-
104+ raise NameError
105+
103106 return content , mime_type
104107
105108
@@ -133,10 +136,10 @@ def server(log_buffer=sys.stderr):
133136 path = parse_request (request )
134137
135138 # Use response_path to retrieve the content and mimetype, based on request path
136- content , mimetype = response_path
139+ content , mime_type = response_path ( path )
137140
138141 # Use content and mimetype from response_path to build a response_ok
139- response = response_ok (content , mimetype )
142+ response = response_ok (content , mime_type )
140143
141144 # If NotImplementedError, then response is method_not_allowed response
142145 except NotImplementedError :
@@ -164,5 +167,3 @@ def server(log_buffer=sys.stderr):
164167if __name__ == '__main__' :
165168 server ()
166169 sys .exit (0 )
167-
168-
0 commit comments