summaryrefslogtreecommitdiff
path: root/project1/proj1_s4498062/webhttp/composer.py
diff options
context:
space:
mode:
Diffstat (limited to 'project1/proj1_s4498062/webhttp/composer.py')
-rw-r--r--project1/proj1_s4498062/webhttp/composer.py24
1 files changed, 18 insertions, 6 deletions
diff --git a/project1/proj1_s4498062/webhttp/composer.py b/project1/proj1_s4498062/webhttp/composer.py
index dfac7a1..5123d31 100644
--- a/project1/proj1_s4498062/webhttp/composer.py
+++ b/project1/proj1_s4498062/webhttp/composer.py
@@ -7,8 +7,8 @@ HTTP requests from a client.
import time
import webhttp.message
-import webhttp.resource
-
+from webhttp.resource import Resource, FileExistError, FileAccessError
+import webhttp.weblogging as logging
class ResponseComposer:
"""Class that composes a HTTP response to a HTTP request"""
@@ -34,10 +34,22 @@ class ResponseComposer:
response = webhttp.message.Response()
# Stub code
- response.code = 200
- response.set_header("Content-Length", 4)
- response.set_header("Connection", "close")
- response.body = "Test"
+ try:
+ resource = Resource(request.uri)
+ response.code = 200
+ response.set_header('Content-Length', resource.get_content_length())
+ response.set_header('Connection', 'close')
+ response.body = resource.get_content()
+ except FileExistError:
+ response.code = 404
+ response.set_header("Content-Length", 9)
+ response.set_header("Connection", "close")
+ response.body = "Not found"
+ except FileAccessError:
+ response.code = 403
+ response.set_header("Content-Length", 13)
+ response.set_header("Connection", "close")
+ response.body = "Access denied"
return response