11import socket
22import sys
33import traceback
4+ import logging
5+ import os
6+ import mimetypes
7+
8+ mimetypes .add_type ("image/jpeg" , '.jpeg' )
9+ mimetypes .add_type ("image/jpeg" , '.jpg' )
10+
11+ logging .basicConfig (level = logging .INFO )
412
513def response_ok (body = b"This is a minimal response" , mimetype = b"text/plain" ):
614 """
@@ -19,21 +27,27 @@ def response_ok(body=b"This is a minimal response", mimetype=b"text/plain"):
1927 '''
2028 """
2129
22- # TODO: Implement response_ok
23- return b""
30+ return b"\r \n " .join ([
31+ b"HTTP/1.1 200 OK" ,
32+ b"Content-Type: " + mimetype ,
33+ b"" ,
34+ body ,
35+ ])
2436
2537def response_method_not_allowed ():
2638 """Returns a 405 Method Not Allowed response"""
2739
28- # TODO: Implement response_method_not_allowed
29- return b""
40+ return b"\r \n " .join ([
41+ b"HTTP/1.1 405 Method Not Allowed" ,
42+ b"" ,
43+ b"You can't do that on this server!"
44+ ])
3045
3146
3247def response_not_found ():
3348 """Returns a 404 Not Found response"""
3449
35- # TODO: Implement response_not_found
36- return b""
50+ return b"HTTP/1.1 404 Not Found"
3751
3852
3953def parse_request (request ):
@@ -44,8 +58,12 @@ def parse_request(request):
4458 NotImplementedError if the method of the request is not GET.
4559 """
4660
47- # TODO: implement parse_request
48- return ""
61+ method , path , version = request .split ("\r \n " )[0 ].split (" " )
62+
63+ if method != "GET" :
64+ raise NotImplementedError
65+ return path
66+
4967
5068def response_path (path ):
5169 """
@@ -75,22 +93,36 @@ def response_path(path):
7593
7694 """
7795
78- # TODO: Raise a NameError if the requested content is not present
79- # under webroot.
8096
81- # TODO: Fill in the appropriate content and mime_type give the path.
82- # See the assignment guidelines for help on "mapping mime-types", though
83- # you might need to create a special case for handling make_time.py
84- #
85- # If the path is "make_time.py", then you may OPTIONALLY return the
86- # result of executing `make_time.py`. But you need only return the
87- # CONTENTS of `make_time.py`.
97+ head = os .path .split (path )[0 ]
98+ tail = os .path .split (path )[1 ]
99+ logging .debug ("the head is: {}" .format (head ))
100+ logging .debug ("the tail is: {}" .format (tail ))
101+ file_path = os .path .join ('webroot' + head , tail )
102+ logging .debug ("the file_path is: {}" .format (file_path ))
103+ if os .path .isfile (file_path ) is True :
104+ logging .debug ("File is found" )
105+ with open (file_path , "rb" ) as f :
106+ content = f .read ()
107+ tail = os .path .split (path )[1 ]
108+ print ('File_name: {}' .format (tail ))
109+ mime_type = mimetypes .guess_type (tail )[0 ].encode ()
110+ print (mime_type )
111+ return content , mime_type
112+
113+ elif os .path .isdir (file_path ):
114+ logging .debug ("Directory is found" )
115+ mime_type = b"text/plain"
116+ file_list = os .listdir (file_path ) #list of files
117+ file_string = ',' .join (file_list ) #string of files
118+ content = file_string .encode () #encoded string
119+ logging .debug ("Contents in Directory: {}" .format (content ))
120+ return content , mime_type
121+
122+ else :
123+ logging .debug ("Nothing is found" )
124+ raise NameError
88125
89- content = b"not implemented"
90- mime_type = b"not implemented"
91-
92- return content , mime_type
93-
94126
95127def server (log_buffer = sys .stderr ):
96128 address = ('127.0.0.1' , 10000 )
@@ -114,24 +146,29 @@ def server(log_buffer=sys.stderr):
114146
115147 if '\r \n \r \n ' in request :
116148 break
117-
118149
119150 print ("Request received:\n {}\n \n " .format (request ))
120151
121- # TODO: Use parse_request to retrieve the path from the request.
122-
123- # TODO: Use response_path to retrieve the content and the mimetype,
124- # based on the request path.
125-
126- # TODO; If parse_request raised a NotImplementedError, then let
127- # response be a method_not_allowed response. If response_path raised
128- # a NameError, then let response be a not_found response. Else,
129- # use the content and mimetype from response_path to build a
130- # response_ok.
131- response = response_ok (
132- body = b"Welcome to my web server" ,
133- mimetype = b"text/plain"
134- )
152+ try :
153+ # Use parse_request to retrieve the path from the request.
154+ path = parse_request (request )
155+ # Use response_path to retrieve the content and the mimetype,
156+ # based on the request path.
157+ content , mime_type = response_path (path )
158+ response = response_ok (
159+ body = content ,
160+ mimetype = mime_type )
161+
162+ # If parse_request raised a NotImplementedError, then let
163+ # response be a method_not_allowed response.
164+ except NotImplementedError :
165+ response = response_method_not_allowed ()
166+
167+ # If response_path raised a NameError, then let response
168+ # be a not_found response. Else,use the content and
169+ # mimetype from response_path to build a response_ok.
170+ except NameError :
171+ response = response_not_found ()
135172
136173 conn .sendall (response )
137174 except :
0 commit comments