From 5372ebb8be5a5e6669129b6dc78fa6f333b6e186 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Fri, 4 Mar 2016 23:09:53 +0100 Subject: Project 1: Config system; ETags; error pages --- project1/proj1_s4498062/webhttp/resource.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'project1/proj1_s4498062/webhttp/resource.py') 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 -- cgit v1.2.3