summaryrefslogtreecommitdiff
path: root/project1/proj1_s4498062/webhttp/resource.py
diff options
context:
space:
mode:
authorCamil Staps2016-03-04 23:09:53 +0100
committerCamil Staps2016-03-04 23:10:12 +0100
commit5372ebb8be5a5e6669129b6dc78fa6f333b6e186 (patch)
tree37e96eeb9749a3f9cfae44082d72514f49785156 /project1/proj1_s4498062/webhttp/resource.py
parentProject one until step 8 in the readme (diff)
Project 1: Config system; ETags; error pages
Diffstat (limited to 'project1/proj1_s4498062/webhttp/resource.py')
-rw-r--r--project1/proj1_s4498062/webhttp/resource.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/project1/proj1_s4498062/webhttp/resource.py b/project1/proj1_s4498062/webhttp/resource.py
index cf2b725..8fefc9c 100644
--- a/project1/proj1_s4498062/webhttp/resource.py
+++ b/project1/proj1_s4498062/webhttp/resource.py
@@ -3,10 +3,15 @@
This module contains a handler class for resources.
"""
-import os
+import binascii
+import hashlib
import mimetypes
+import os
+import re
import urlparse
+from config import config
+import regexes as r
class FileExistError(Exception):
"""Exception which is raised when file does not exist"""
@@ -37,7 +42,7 @@ class Resource:
if not os.path.exists(self.path):
raise FileExistError
if os.path.isdir(self.path):
- self.path = os.path.join(self.path, "index.html")
+ self.path = os.path.join(self.path, config('index'))
if not os.path.exists(self.path):
raise FileAccessError
if not os.path.isfile(self.path):
@@ -52,9 +57,18 @@ class Resource:
str: ETag for the resource
"""
stat = os.stat(self.path)
- etag = ""
+ m = hashlib.md5()
+ m.update(str(stat))
+ etag = binascii.hexlify(m.digest())
return etag
+ def etag_match(self, etag):
+ if etag == None:
+ return False
+ my_etag = self.generate_etag()
+ return etag == '*' or \
+ any([tag == my_etag for tag in re.split(r.ETagSplit, etag)])
+
def get_content(self):
"""Get the contents of the resource