Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Bugfix: Include Etag in 304 Not Modified responses, allow all HTTPPre…

…amble kwargs to be passed to HTTPResponse subclasses
  • Loading branch information...
commit 5f428ed4d83d8efdf64da721297a9263c14d1993 1 parent 831cc7d
Alek Storm authored March 11, 2012
9  vortex/resources.py
@@ -7,7 +7,7 @@
7 7
 import time
8 8
 import uuid
9 9
 
10  
-from vortex import HTTPStream, Resource, authenticate, http_date, signed_cookie, xsrf
  10
+from vortex import SAFE_METHODS, HTTPStream, Resource, authenticate, http_date, signed_cookie, xsrf
11 11
 from vortex.responses import *
12 12
 
13 13
 class DictResource(Resource):
@@ -77,20 +77,21 @@ def get(self, request, **kwargs):
77 77
 
78 78
         # Don't send the result if the content has not been modified since the If-Modified-Since
79 79
         modified = self.os.stat(self.path).st_mtime
80  
-        if 'If-Modified-Since' in request.headers and time.mktime(email.utils.parsedate(request.headers['If-Modified-Since'])) >= modified:
81  
-            return HTTPNotModifiedResponse()
82 80
 
83 81
         headers = {
84 82
             'Etag': '"%s"' % hashlib.sha1('\0'.join([self.path, str(modified)])).hexdigest(),
85 83
             'Last-Modified': http_date(modified),
86 84
         }
87 85
 
  86
+        if 'If-Modified-Since' in request.headers and time.mktime(email.utils.parsedate(request.headers['If-Modified-Since'])) >= modified:
  87
+            return HTTPNotModifiedResponse(headers=headers)
  88
+
88 89
         inm = request.headers.get('If-None-Match', None)
89 90
         if inm:
90 91
             if request.method not in SAFE_METHODS:
91 92
                 return HTTPPreconditionFailedResponse()
92 93
             elif inm.find(headers['Etag']) != -1 or inm == '*':
93  
-                return HTTPNotModifiedResponse()
  94
+                return HTTPNotModifiedResponse(headers=headers)
94 95
 
95 96
         mimetype = mimetypes.guess_type(self.path)[0]
96 97
         if mimetype:
44  vortex/responses.py
@@ -3,55 +3,55 @@
3 3
 from vortex import HTTPPreamble, HTTPResponse
4 4
 
5 5
 class HTTPCreatedResponse(HTTPResponse):
6  
-    def __init__(self, cookies=None):
7  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.CREATED, cookies=cookies))
  6
+    def __init__(self, **kwargs):
  7
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.CREATED, **kwargs))
8 8
 
9 9
 
10 10
 class HTTPNoContentResponse(HTTPResponse):
11  
-    def __init__(self, cookies=None):
12  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NO_CONTENT, cookies=cookies))
  11
+    def __init__(self, **kwargs):
  12
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NO_CONTENT, **kwargs))
13 13
 
14 14
 
15 15
 class HTTPFoundResponse(HTTPResponse):
16  
-    def __init__(self, location, body='', cookies=None):
17  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.FOUND, headers={'Location': location}, cookies=cookies), body=body)
  16
+    def __init__(self, location, body='', **kwargs):
  17
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.FOUND, headers={'Location': location}, **kwargs), body=body)
18 18
 
19 19
 
20 20
 class HTTPNotModifiedResponse(HTTPResponse):
21  
-    def __init__(self, body='', cookies=None):
22  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_MODIFIED, cookies=cookies), body=body)
  21
+    def __init__(self, body='', **kwargs):
  22
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_MODIFIED, **kwargs), body=body)
23 23
 
24 24
 
25 25
 class HTTPNotFoundResponse(HTTPResponse):
26  
-    def __init__(self, body='', cookies=None):
27  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_FOUND, cookies=cookies), body=body)
  26
+    def __init__(self, body='', **kwargs):
  27
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_FOUND, **kwargs), body=body)
28 28
 
29 29
 
30 30
 class HTTPBadRequestResponse(HTTPResponse):
31  
-    def __init__(self, body='', cookies=None):
32  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.BAD_REQUEST, cookies=cookies), body=body)
  31
+    def __init__(self, body='', **kwargs):
  32
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.BAD_REQUEST, **kwargs), body=body)
33 33
 
34 34
 
35 35
 class HTTPUnauthorizedResponse(HTTPResponse):
36  
-    def __init__(self, body='', cookies=None):
37  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.UNAUTHORIZED, cookies=cookies), body=body)
  36
+    def __init__(self, body='', **kwargs):
  37
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.UNAUTHORIZED, **kwargs), body=body)
38 38
 
39 39
 
40 40
 class HTTPForbiddenResponse(HTTPResponse):
41  
-    def __init__(self, body='', cookies=None):
42  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.FORBIDDEN, cookies=cookies), body=body)
  41
+    def __init__(self, body='', **kwargs):
  42
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.FORBIDDEN, **kwargs), body=body)
43 43
 
44 44
 
45 45
 class HTTPMethodNotAllowedResponse(HTTPResponse):
46  
-    def __init__(self, allowed, body='', cookies=None):
47  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.METHOD_NOT_ALLOWED, headers={'Allowed': allowed}, cookies=cookies), body=body)
  46
+    def __init__(self, allowed, body='', **kwargs):
  47
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.METHOD_NOT_ALLOWED, headers={'Allowed': allowed}, **kwargs), body=body)
48 48
 
49 49
 
50 50
 class HTTPNotImplementedResponse(HTTPResponse):
51  
-    def __init__(self, body='', cookies=None):
52  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_IMPLEMENTED, cookies=cookies), body=body)
  51
+    def __init__(self, body='', **kwargs):
  52
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_IMPLEMENTED, **kwargs), body=body)
53 53
 
54 54
 
55 55
 class HTTPInternalServerErrorResponse(HTTPResponse):
56  
-    def __init__(self, body='', cookies=None):
57  
-        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.INTERNAL_SERVER_ERROR, cookies=cookies), body=body)
  56
+    def __init__(self, body='', **kwargs):
  57
+        HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.INTERNAL_SERVER_ERROR, **kwargs), body=body)

0 notes on commit 5f428ed

Please sign in to comment.
Something went wrong with that request. Please try again.