diff options
author | Camil Staps | 2016-03-05 10:25:03 +0100 |
---|---|---|
committer | Camil Staps | 2016-03-05 10:25:03 +0100 |
commit | 804ae3b864e1fe47ea38ac1a2283019387c33ac0 (patch) | |
tree | 043122a7497dd162b1bcecc548eb723307d99f5e /project1/proj1_s4498062/webhttp/resource.py | |
parent | Project 1: Config system; ETags; error pages (diff) |
Step 11: content encoding (only gzip for the moment)
Diffstat (limited to 'project1/proj1_s4498062/webhttp/resource.py')
-rw-r--r-- | project1/proj1_s4498062/webhttp/resource.py | 11 |
1 files changed, 9 insertions, 2 deletions
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"
|