Skip to content

Commit 7aaef5a

Browse files
committed
Repking Lesson 3 Assignment
1 parent f7f0dac commit 7aaef5a

File tree

3 files changed

+102
-38
lines changed

3 files changed

+102
-38
lines changed

http_server.py

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import socket
22
import sys
33
import 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

513
def 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

2537
def 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

3247
def 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

3953
def 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

5068
def 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

95127
def 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:

tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import subprocess
33
import http.client
44
import os
5+
import logging
56

67

78
class WebTestCase(unittest.TestCase):

unit-tests.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
class TestCase(unittest.TestCase):
77

8+
#@unittest.skip("Skipping")
89
def test_response_ok(self):
910
mimetype = b"image/bmp"
1011
body = b"foo"
@@ -53,10 +54,22 @@ def test_response_path_file(self):
5354
content, mime_type = http_server.response_path(path)
5455

5556
self.assertEqual(b"text/html", mime_type)
56-
57+
5758
with open(os.path.join("webroot", "a_web_page.html"), "rb") as f:
5859
self.assertEqual(f.read(), content)
5960

61+
def test_response_path_image(self):
62+
file = 'images/Sample_Scene_Balls.jpg'
63+
web_path = '/' + file
64+
65+
content, mime_type = http_server.response_path(web_path)
66+
67+
68+
self.assertEqual(b"image/jpeg", mime_type)
69+
70+
with open(os.path.join("webroot", file), "rb") as f:
71+
self.assertEqual(f.read(), content)
72+
6073
def test_response_path_dir(self):
6174
path = "/"
6275

@@ -67,6 +80,19 @@ def test_response_path_dir(self):
6780
self.assertIn(b"sample.txt", content)
6881
self.assertIn(b"a_web_page.html", content)
6982

83+
def test_images_index(self):
84+
"""
85+
A call to /images/ yields a list of files in the images directory
86+
"""
87+
88+
path = "/images"
89+
90+
content, mime_type = http_server.response_path(path)
91+
92+
self.assertIn(b"JPEG_example.jpg", content)
93+
self.assertIn(b"sample_1.png", content)
94+
self.assertIn(b"Sample_Scene_Balls.jpg", content)
95+
7096
def test_response_path_not_found(self):
7197
path = "/foo/bar/baz/doesnt/exist"
7298

0 commit comments

Comments
 (0)