Skip to content

Commit

Permalink
Bugfix: Include Etag in 304 Not Modified responses, allow all HTTPPre…
Browse files Browse the repository at this point in the history
…amble kwargs to be passed to HTTPResponse subclasses
  • Loading branch information
alekstorm committed Mar 12, 2012
1 parent 831cc7d commit 5f428ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
9 changes: 5 additions & 4 deletions vortex/resources.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time import time
import uuid import uuid


from vortex import HTTPStream, Resource, authenticate, http_date, signed_cookie, xsrf from vortex import SAFE_METHODS, HTTPStream, Resource, authenticate, http_date, signed_cookie, xsrf
from vortex.responses import * from vortex.responses import *


class DictResource(Resource): class DictResource(Resource):
Expand Down Expand Up @@ -77,20 +77,21 @@ def get(self, request, **kwargs):


# Don't send the result if the content has not been modified since the If-Modified-Since # Don't send the result if the content has not been modified since the If-Modified-Since
modified = self.os.stat(self.path).st_mtime modified = self.os.stat(self.path).st_mtime
if 'If-Modified-Since' in request.headers and time.mktime(email.utils.parsedate(request.headers['If-Modified-Since'])) >= modified:
return HTTPNotModifiedResponse()


headers = { headers = {
'Etag': '"%s"' % hashlib.sha1('\0'.join([self.path, str(modified)])).hexdigest(), 'Etag': '"%s"' % hashlib.sha1('\0'.join([self.path, str(modified)])).hexdigest(),
'Last-Modified': http_date(modified), 'Last-Modified': http_date(modified),
} }


if 'If-Modified-Since' in request.headers and time.mktime(email.utils.parsedate(request.headers['If-Modified-Since'])) >= modified:
return HTTPNotModifiedResponse(headers=headers)

inm = request.headers.get('If-None-Match', None) inm = request.headers.get('If-None-Match', None)
if inm: if inm:
if request.method not in SAFE_METHODS: if request.method not in SAFE_METHODS:
return HTTPPreconditionFailedResponse() return HTTPPreconditionFailedResponse()
elif inm.find(headers['Etag']) != -1 or inm == '*': elif inm.find(headers['Etag']) != -1 or inm == '*':
return HTTPNotModifiedResponse() return HTTPNotModifiedResponse(headers=headers)


mimetype = mimetypes.guess_type(self.path)[0] mimetype = mimetypes.guess_type(self.path)[0]
if mimetype: if mimetype:
Expand Down
44 changes: 22 additions & 22 deletions vortex/responses.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,55 +3,55 @@
from vortex import HTTPPreamble, HTTPResponse from vortex import HTTPPreamble, HTTPResponse


class HTTPCreatedResponse(HTTPResponse): class HTTPCreatedResponse(HTTPResponse):
def __init__(self, cookies=None): def __init__(self, **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.CREATED, cookies=cookies)) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.CREATED, **kwargs))




class HTTPNoContentResponse(HTTPResponse): class HTTPNoContentResponse(HTTPResponse):
def __init__(self, cookies=None): def __init__(self, **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NO_CONTENT, cookies=cookies)) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NO_CONTENT, **kwargs))




class HTTPFoundResponse(HTTPResponse): class HTTPFoundResponse(HTTPResponse):
def __init__(self, location, body='', cookies=None): def __init__(self, location, body='', **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.FOUND, headers={'Location': location}, cookies=cookies), body=body) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.FOUND, headers={'Location': location}, **kwargs), body=body)




class HTTPNotModifiedResponse(HTTPResponse): class HTTPNotModifiedResponse(HTTPResponse):
def __init__(self, body='', cookies=None): def __init__(self, body='', **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_MODIFIED, cookies=cookies), body=body) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_MODIFIED, **kwargs), body=body)




class HTTPNotFoundResponse(HTTPResponse): class HTTPNotFoundResponse(HTTPResponse):
def __init__(self, body='', cookies=None): def __init__(self, body='', **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_FOUND, cookies=cookies), body=body) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_FOUND, **kwargs), body=body)




class HTTPBadRequestResponse(HTTPResponse): class HTTPBadRequestResponse(HTTPResponse):
def __init__(self, body='', cookies=None): def __init__(self, body='', **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.BAD_REQUEST, cookies=cookies), body=body) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.BAD_REQUEST, **kwargs), body=body)




class HTTPUnauthorizedResponse(HTTPResponse): class HTTPUnauthorizedResponse(HTTPResponse):
def __init__(self, body='', cookies=None): def __init__(self, body='', **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.UNAUTHORIZED, cookies=cookies), body=body) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.UNAUTHORIZED, **kwargs), body=body)




class HTTPForbiddenResponse(HTTPResponse): class HTTPForbiddenResponse(HTTPResponse):
def __init__(self, body='', cookies=None): def __init__(self, body='', **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.FORBIDDEN, cookies=cookies), body=body) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.FORBIDDEN, **kwargs), body=body)




class HTTPMethodNotAllowedResponse(HTTPResponse): class HTTPMethodNotAllowedResponse(HTTPResponse):
def __init__(self, allowed, body='', cookies=None): def __init__(self, allowed, body='', **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.METHOD_NOT_ALLOWED, headers={'Allowed': allowed}, cookies=cookies), body=body) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.METHOD_NOT_ALLOWED, headers={'Allowed': allowed}, **kwargs), body=body)




class HTTPNotImplementedResponse(HTTPResponse): class HTTPNotImplementedResponse(HTTPResponse):
def __init__(self, body='', cookies=None): def __init__(self, body='', **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_IMPLEMENTED, cookies=cookies), body=body) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.NOT_IMPLEMENTED, **kwargs), body=body)




class HTTPInternalServerErrorResponse(HTTPResponse): class HTTPInternalServerErrorResponse(HTTPResponse):
def __init__(self, body='', cookies=None): def __init__(self, body='', **kwargs):
HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.INTERNAL_SERVER_ERROR, cookies=cookies), body=body) HTTPResponse.__init__(self, HTTPPreamble(status_code=httplib.INTERNAL_SERVER_ERROR, **kwargs), body=body)

0 comments on commit 5f428ed

Please sign in to comment.