Example #1
0
    def check_last_modified(self, date):
        """
        This function will check if one of a given request's last modified
        headers are set, and, if so, will check it against the date provided.
        If the request specifies an equivalent or newer resource, this function
        will call halt() to abort the current request with a 304 Not Modified
        status.
        """
        if date is None:
            return

        # Python's time functions are stupid. We do everything with unix times.
        timestamp = time.mktime(date.timetuple())
        self.response.last_modified = timestamp

        # We don't do anything if there's an ETag.
        if self.request.if_none_match is not MatchNoneEtag:
            return

        if (self.response.status_int == 200 and
                self.request.if_modified_since is not None):
            time_val = time.mktime(self.request.if_modified_since.timetuple())
            if time_val >= timestamp:
                halt(code=304)

        if ((self.response.is_success or self.response.status_int == 412) and
                self.request.if_unmodified_since is not None):
            time_val = time.mktime(
                self.request.if_unmodified_since.timetuple())
            if time_val < timestamp:
                halt(code=412)
Example #2
0
    def check_last_modified(self, date):
        """
        This function will check if one of a given request's last modified
        headers are set, and, if so, will check it against the date provided.
        If the request specifies an equivalent or newer resource, this function
        will call halt() to abort the current request with a 304 Not Modified
        status.
        """
        if date is None:
            return

        # Python's time functions are stupid. We do everything with unix times.
        timestamp = time.mktime(date.timetuple())
        self.response.last_modified = timestamp

        # We don't do anything if there's an ETag.
        if self.request.if_none_match is not MatchNoneEtag:
            return

        if (self.response.status_int == 200
                and self.request.if_modified_since is not None):
            time_val = time.mktime(self.request.if_modified_since.timetuple())
            if time_val >= timestamp:
                halt(code=304)

        if ((self.response.is_success or self.response.status_int == 412)
                and self.request.if_unmodified_since is not None):
            time_val = time.mktime(
                self.request.if_unmodified_since.timetuple())
            if time_val < timestamp:
                halt(code=412)
Example #3
0
    def check_etag(self, etag, new_resource=False, weak=False):
        """
        As per check_if_modified(), except checks the ETag header instead.
        """
        self.response.etag = (etag, not weak)

        # We assume the request is a new resource if it is a POST.
        new_resource = new_resource or self.request.method == "POST"

        # An etag will match a 'If-*-Match' header in two cases:
        #  - If it's not a new resource, and the header specifies 'anything'
        #    (i.e. '*')
        #  - Otherwise, if it's an exact match.
        def etag_matches(value):
            if value is MatchAnyEtag:
                return not new_resource
            return etag in value

        if self.response.is_success or self.response.status_int == 304:
            if (self.request.if_none_match is not MatchNoneEtag and
                    etag_matches(self.request.if_none_match)):
                if self.request.is_safe:
                    halt(code=304)
                else:
                    halt(code=412)
            elif (self.request.if_match is not MatchAnyEtag and
                    not etag_matches(self.request.if_match)):
                halt(code=412)
Example #4
0
    def check_etag(self, etag, new_resource=False, weak=False):
        """
        As per check_if_modified(), except checks the ETag header instead.
        """
        self.response.etag = (etag, not weak)

        # We assume the request is a new resource if it is a POST.
        new_resource = new_resource or self.request.method == "POST"

        # An etag will match a 'If-*-Match' header in two cases:
        #  - If it's not a new resource, and the header specifies 'anything'
        #    (i.e. '*')
        #  - Otherwise, if it's an exact match.
        def etag_matches(value):
            if value is MatchAnyEtag:
                return not new_resource
            return etag in value

        if self.response.is_success or self.response.status_int == 304:
            if (self.request.if_none_match is not MatchNoneEtag
                    and etag_matches(self.request.if_none_match)):
                if self.request.is_safe:
                    halt(code=304)
                else:
                    halt(code=412)
            elif (self.request.if_match is not MatchAnyEtag
                  and not etag_matches(self.request.if_match)):
                halt(code=412)