22import  sys 
33import  traceback 
44import  os 
5+ import  mimetypes 
56
67def  response_ok (body = b"This is a minimal response" , mimetype = b"text/plain" ):
78    """ 
@@ -51,11 +52,8 @@ def parse_request(request):
5152    This server only handles GET requests, so this method shall raise a 
5253    NotImplementedError if the method of the request is not GET. 
5354    """ 
54- 
55-     # TODO: implement parse_request 
5655    method , path , version  =  request .split ("\r \n " )[0 ].split (" " )
5756
58- 
5957    if  method  !=  "GET" :
6058        raise  NotImplementedError 
6159
@@ -66,19 +64,25 @@ def read_file(file_name):
6664    Read the provided file as bytes and return content 
6765    return byte string 
6866    ''' 
69- 
70-     with  open (os .path .join (os .getcwd (), "webroot" , file_name ), "rb" ) as  f :
71-         content  =  f .read ()
67+     try :
68+         with  open (file_name , "rb" ) as  f :
69+             content  =  f .read ()
70+     except  PermissionError :
71+         content  =  'directory' 
7272
7373    return  content 
7474
75- def  contents_to_bytes (path ):
75+ def  contents_to_bytes (path ,  file_name ):
7676    ''' 
7777    Converts the directory list to byte string 
7878    ''' 
79-     file_path  =  os .path .join (os .getcwd (), "webroot" , path )
80-     files  =  os .listdir (file_path )
81-     contents  =  '' .join (files ).encode ()
79+     try :
80+         file_path  =  os .path .join (path , file_name )
81+         files  =  os .listdir (file_path )
82+         contents  =  '' .join (files ).encode ('utf-8' )
83+     except  NotADirectoryError :
84+         raise  NotADirectoryError 
85+     
8286    return  contents 
8387
8488def  response_path (path ):
@@ -109,49 +113,26 @@ def response_path(path):
109113
110114    """ 
111115
112-     # TODO: Raise a NameError if the requested content is not present 
113-     # under webroot. 
114-     ''' 
115-     Get path 
116-     Determine if path is valid 
117-     Determine content 
118-     Determine mime_type 
119-     ''' 
120- 
121116    content  =  b"not implemented" 
122117    mime_type  =  b"not implemented" 
123118
124119    path_1  =  path .strip ("/" )
125- 
126-     if   path_1   ==   '' : 
127-         content = contents_to_bytes ( path_1 ) 
128-         mime_type = b"text/plain"   
129-         return   content ,  mime_type 
120+      path_2   =   os . path . join ( 
121+          os . getcwd (), 
122+         'socket-http-server' , 
123+         'webroot' 
124+         ) 
130125
131126    try :
132-         contents_to_bytes (path_1 )
127+         content  =  contents_to_bytes (path_2 , path_1 )
128+         mime_type  =  b"text/plain" 
133129    except  NotADirectoryError :
134-         file_type  =  path_1 .split ("." )
130+         guess_type  =  mimetypes .guess_type (path_1 )[0 ]
131+         content  =  read_file (os .path .join (path_2 , path_1 ))
132+         mime_type  =  guess_type .encode ('utf-8' )
135133    except  FileNotFoundError :
136134        raise  NameError 
137135
138-     type_dict  =  {
139-         "txt"  : [read_file (path_1 ), b"text/plain" ],
140-         "html"  : [read_file (path_1 ), b"text/html" ],
141-         "ico"  : [read_file (path_1 ), b"image/ico" ],
142-         "jpg"  : [read_file (path_1 ), b"image/jpg" ],
143-     }
144-     
145-     content  =  type_dict [file_type [- 1 ]][0 ]
146-     mime_type  =  type_dict [file_type [- 1 ]][1 ]
147-     # TODO: Fill in the appropriate content and mime_type give the path. 
148-     # See the assignment guidelines for help on "mapping mime-types", though 
149-     # you might need to create a special case for handling make_time.py 
150- 
151-     # If the path is "make_time.py", then you may OPTIONALLY return the 
152-     # result of executing `make_time.py`. But you need only return the 
153-     # CONTENTS of `make_time.py`. 
154- 
155136    return  content , mime_type 
156137
157138
@@ -183,14 +164,9 @@ def server(log_buffer=sys.stderr):
183164
184165                try :
185166                    path  =  parse_request (request )
186-                     # TODO: Use response_path to retrieve the content and the mimetype, 
187-                     # based on the request path. 
167+ 
188168                    content , mime_type  =  response_path (path )
189-                     # TODO; If parse_request raised a NotImplementedError, then let 
190-                     # response be a method_not_allowed response. If response_path raised 
191-                     # a NameError, then let response be a not_found response. Else, 
192-                     # use the content and mimetype from response_path to build a  
193-                     # response_ok. 
169+ 
194170                    response  =  response_ok (
195171                        body = content ,
196172                        mimetype = mime_type 
@@ -217,5 +193,3 @@ def server(log_buffer=sys.stderr):
217193if  __name__  ==  '__main__' :
218194    server ()
219195    sys .exit (0 )
220- 
221- 
0 commit comments