From 436f26b4eb1b38089396374876908fdb06d3c015 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Wed, 2 Mar 2016 21:37:50 +0100 Subject: Added framework project 1 --- project1/proj1_s4498062/webhttp/resource.py | 86 +++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 project1/proj1_s4498062/webhttp/resource.py (limited to 'project1/proj1_s4498062/webhttp/resource.py') diff --git a/project1/proj1_s4498062/webhttp/resource.py b/project1/proj1_s4498062/webhttp/resource.py new file mode 100644 index 0000000..dc20067 --- /dev/null +++ b/project1/proj1_s4498062/webhttp/resource.py @@ -0,0 +1,86 @@ +"""Resources + +This module contains a handler class for resources. +""" + +import os +import mimetype +import urlparse + + +class FileExistError(Exception): + """Exception which is raised when file does not exist""" + pass + + +class FileAccessError(Exception): + """Exception which is raised when file exists, but cannot be accessed""" + pass + + +class Resource: + """Class for representing a Resource (file)""" + + def __init__(self, uri): + """Initialize the resource" + + Raises: + FileExistError: if resource does not exist + FileAccessError: if resource exists, but cannot be accessed + + Args: + uri (str): Uniform Resource Identifier + """ + self.uri = uri + out = urlparse.urlparse(uri) + self.path = os.path.join("content", out.path.lstrip("/")) + if os.path.isdir(self.path): + self.path = os.path.join(self.path, "index.html") + if not os.path.isfile(self.path): + raise FileExistError + if not os.access(self.path, os.R_OK): + raise FileAccessError + + def generate_etag(self): + """Generate the ETag for the resource + + Returns: + str: ETag for the resource + """ + stat = os.stat(self.path) + etag = "" + return etag + + def get_content(self): + """Get the contents of the resource + + Returns: + str: Contents of the resource + """ + return open(self.path).read() + + def get_content_type(self): + """Get the content type, i.e "text/html" + + Returns: + str: type of content in the resource + """ + mimetype = mimetypes.guess_type(self.path) + return mimetype[0] + + def get_content_encoding(self): + """Get the content encoding, i.e "gzip" + + Returns: + str: encoding used for the resource + """ + mimetype = mimetypes.guess_type(self.path) + return mimetype[1] + + def get_content_length(self): + """Get the length of the resource + + Returns: + int: length of resource in bytes + """ + return os.path.getsize(self.path) -- cgit v1.2.3