From 804ae3b864e1fe47ea38ac1a2283019387c33ac0 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Sat, 5 Mar 2016 10:25:03 +0100 Subject: Step 11: content encoding (only gzip for the moment) --- project1/proj1_s4498062/webhttp/resource.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 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 8fefc9c..9dd948c 100644 --- a/project1/proj1_s4498062/webhttp/resource.py +++ b/project1/proj1_s4498062/webhttp/resource.py @@ -4,15 +4,19 @@ This module contains a handler class for resources. """ import binascii +import gzip import hashlib import mimetypes import os import re +import StringIO import urlparse from config import config +import encodings import regexes as r + class FileExistError(Exception): """Exception which is raised when file does not exist""" pass @@ -36,6 +40,8 @@ class Resource: Args: uri (str): Uniform Resource Identifier """ + if uri == None: + raise FileExistError self.uri = uri out = urlparse.urlparse(uri) self.path = os.path.join("content", out.path.lstrip("/")) @@ -69,13 +75,14 @@ class Resource: return etag == '*' or \ any([tag == my_etag for tag in re.split(r.ETagSplit, etag)]) - def get_content(self): + def get_content(self, encoding=encodings.IDENTITY): """Get the contents of the resource Returns: str: Contents of the resource """ - return open(self.path).read() + content = open(self.path).read() + return encodings.encode(encoding, content) def get_content_type(self): """Get the content type, i.e "text/html" -- cgit v1.2.3