def check_precondition_headers(viewfunc, request):
    """View decorator to check X-If-[Unm|M]odified-Since-Version headers.

    This decorator checks pre-validated X-If-Modified-Since-Version and
    X-If-Unmodified-Since-Version headers against the actual last-modified
    version of the target resource.  If the preconditions are not met then
    it raises the appropriate error response.

    In addition, and retreived value for the last-modified version will be
    stored in the response headers for return to the client.  This may save
    having to look it up again when the response is being rendered.
    """
    if "if_modified_since" in request.validated:
        version = get_resource_version(request)
        request.response.headers["X-Last-Modified-Version"] = str(version)
        if version <= request.validated["if_modified_since"]:
            raise HTTPNotModified(headers={
                "X-Last-Modified-Version": str(version),
            })

    if "if_unmodified_since" in request.validated:
        version = get_resource_version(request)
        request.response.headers["X-Last-Modified-Version"] = str(version)
        if version > request.validated["if_unmodified_since"]:
            raise HTTPPreconditionFailed(headers={
                "X-Last-Modified-Version": str(version),
            })

    return viewfunc(request)
示例#2
0
def check_precondition_headers(viewfunc, request):
    """View decorator to check X-If-[Unm|M]odified-Since-Version headers.

    This decorator checks pre-validated X-If-Modified-Since-Version and
    X-If-Unmodified-Since-Version headers against the actual last-modified
    version of the target resource.  If the preconditions are not met then
    it raises the appropriate error response.

    In addition, and retreived value for the last-modified version will be
    stored in the response headers for return to the client.  This may save
    having to look it up again when the response is being rendered.
    """
    if "if_modified_since" in request.validated:
        version = get_resource_version(request)
        request.response.headers["X-Last-Modified-Version"] = str(version)
        if version <= request.validated["if_modified_since"]:
            raise HTTPNotModified(headers={
                "X-Last-Modified-Version": str(version),
            })

    if "if_unmodified_since" in request.validated:
        version = get_resource_version(request)
        request.response.headers["X-Last-Modified-Version"] = str(version)
        if version > request.validated["if_unmodified_since"]:
            raise HTTPPreconditionFailed(
                headers={
                    "X-Last-Modified-Version": str(version),
                })

    return viewfunc(request)
 def adjust_response(self, value, request, response):
     # Ensure that every response reports the last-modified version.
     # In most cases this will have already been set when we looked it
     # up during processing of the request.
     if "X-Last-Modified-Version" not in response.headers:
         version = get_resource_version(request)
         response.headers["X-Last-Modified-Version"] = str(version)
示例#4
0
 def adjust_response(self, value, request, response):
     # Ensure that every response reports the last-modified version.
     # In most cases this will have already been set when we looked it
     # up during processing of the request.
     if "X-Last-Modified-Version" not in response.headers:
         version = get_resource_version(request)
         response.headers["X-Last-Modified-Version"] = str(version)