Example #1
0
    def get_header_as_datetime(self, header, required=False, obs_date=False):
        """Return an HTTP header with HTTP-Date values as a datetime.

        Args:
            name (str): Header name, case-insensitive (e.g., 'Date')
            required (bool, optional): Set to ``True`` to raise
                ``HTTPBadRequest`` instead of returning gracefully when the
                header is not found (default ``False``).
            obs_date (bool, optional): Support obs-date formats according to
                RFC 7231, e.g.: "Sunday, 06-Nov-94 08:49:37 GMT"
                (default ``False``).

        Returns:
            datetime: The value of the specified header if it exists,
                or ``None`` if the header is not found and is not required.

        Raises:
            HTTPBadRequest: The header was not found in the request, but
                it was required.
            HttpInvalidHeader: The header contained a malformed/invalid value.
        """

        try:
            http_date = self.get_header(header, required=required)
            return util.http_date_to_dt(http_date, obs_date=obs_date)
        except TypeError:
            # When the header does not exist and isn't required
            return None
        except ValueError:
            msg = "It must be formatted according to RFC 7231, " "Section 7.1.1.1"
            raise HTTPInvalidHeader(msg, header)
Example #2
0
    def get_header_as_datetime(self, header, required=False, obs_date=False):
        """Return an HTTP header with HTTP-Date values as a datetime.

        Args:
            name (str): Header name, case-insensitive (e.g., 'Date')
            required (bool, optional): Set to ``True`` to raise
                ``HTTPBadRequest`` instead of returning gracefully when the
                header is not found (default ``False``).
            obs_date (bool, optional): Support obs-date formats according to
                RFC 7231, e.g.: "Sunday, 06-Nov-94 08:49:37 GMT"
                (default ``False``).

        Returns:
            datetime: The value of the specified header if it exists,
            or ``None`` if the header is not found and is not required.

        Raises:
            HTTPBadRequest: The header was not found in the request, but
                it was required.
            HttpInvalidHeader: The header contained a malformed/invalid value.
        """

        try:
            http_date = self.get_header(header, required=required)
            return util.http_date_to_dt(http_date, obs_date=obs_date)
        except TypeError:
            # When the header does not exist and isn't required
            return None
        except ValueError:
            msg = ('It must be formatted according to RFC 7231, '
                   'Section 7.1.1.1')
            raise errors.HTTPInvalidHeader(msg, header)
Example #3
0
 def date(self):
     http_date = self._get_header_by_wsgi_name('HTTP_DATE')
     try:
         return util.http_date_to_dt(http_date)
     except ValueError:
         msg = ('The value could not be parsed. It must be formatted '
                'according to RFC 1123.')
         raise InvalidHeader(msg, http_date, 'date')
Example #4
0
 def date(self):
     http_date = self._get_header_by_wsgi_name('HTTP_DATE')
     try:
         return util.http_date_to_dt(http_date)
     except ValueError:
         msg = ('The value could not be parsed. It must be formatted '
                'according to RFC 1123.')
         raise InvalidHeader(msg, http_date, 'date')
Example #5
0
    def date(self):
        try:
            http_date = self.env['HTTP_DATE']
        except KeyError:
            return None

        try:
            return util.http_date_to_dt(http_date)
        except ValueError:
            msg = ('It must be formatted according to RFC 1123.')
            raise HTTPInvalidHeader(msg, 'Date')
Example #6
0
    def date(self):
        try:
            http_date = self.env['HTTP_DATE']
        except KeyError:
            return None

        try:
            return util.http_date_to_dt(http_date)
        except ValueError:
            msg = ('It must be formatted according to RFC 1123.')
            raise HTTPInvalidHeader(msg, 'Date')
Example #7
0
def test_response_unset_cookie(client):
    resp = falcon.Response()
    resp.unset_cookie('bad')
    resp.set_cookie('bad', 'cookie', max_age=300)
    resp.unset_cookie('bad')

    morsels = list(resp._cookies.values())
    len(morsels) == 1

    bad_cookie = morsels[0]
    assert bad_cookie['expires'] == -1

    output = bad_cookie.OutputString()
    assert 'bad=;' in output or 'bad="";' in output

    match = re.search('expires=([^;]+)', output)
    assert match

    expiration = http_date_to_dt(match.group(1), obs_date=True)
    assert expiration < datetime.utcnow()
Example #8
0
    def test_response_unset_cookie(self):
        resp = falcon.Response()
        resp.unset_cookie('bad')
        resp.set_cookie('bad', 'cookie', max_age=300)
        resp.unset_cookie('bad')

        morsels = list(resp._cookies.values())
        self.assertEqual(len(morsels), 1)

        bad_cookie = morsels[0]
        self.assertEqual(bad_cookie['expires'], -1)

        output = bad_cookie.OutputString()
        self.assertTrue('bad=;' in output or 'bad="";' in output)

        match = re.search('expires=([^;]+)', output)
        self.assertIsNotNone(match)

        expiration = http_date_to_dt(match.group(1), obs_date=True)
        self.assertThat(expiration, LessThan(datetime.utcnow()))
Example #9
0
    def test_response_unset_cookie(self):
        resp = falcon.Response()
        resp.unset_cookie("bad")
        resp.set_cookie("bad", "cookie", max_age=300)
        resp.unset_cookie("bad")

        morsels = list(resp._cookies.values())
        self.assertEqual(len(morsels), 1)

        bad_cookie = morsels[0]
        self.assertEqual(bad_cookie['expires'], -1)

        output = bad_cookie.OutputString()
        self.assertTrue('bad=;' in output or 'bad="";' in output)

        match = re.search('expires=([^;]+)', output)
        self.assertIsNotNone(match)

        expiration = http_date_to_dt(match.group(1), obs_date=True)
        self.assertThat(expiration, LessThan(datetime.utcnow()))
Example #10
0
def test_response_unset_cookie(client):
    resp = falcon.Response()
    resp.unset_cookie('bad')
    resp.set_cookie('bad', 'cookie', max_age=300)
    resp.unset_cookie('bad')

    morsels = list(resp._cookies.values())
    len(morsels) == 1

    bad_cookie = morsels[0]
    bad_cookie['expires'] == -1

    output = bad_cookie.OutputString()
    assert 'bad=;' in output or 'bad="";' in output

    match = re.search('expires=([^;]+)', output)
    assert match

    expiration = http_date_to_dt(match.group(1), obs_date=True)
    assert expiration < datetime.utcnow()
Example #11
0
    def date(self):
        """Value of the Date header, converted to a datetime instance.

        Returns:
            An instance of datetime.datetime representing the value of
            the Date header, or None if the Date header is not present
            in the request.

        Raises:
            HTTPBadRequest: The date value could not be parsed, likely
                because it does not confrom to RFC 1123.

        """

        http_date = self._get_header_by_wsgi_name("DATE")
        try:
            return util.http_date_to_dt(http_date)
        except ValueError:
            msg = "The value of the Date header could not be parsed. It " "must be formatted according to RFC 1123."
            raise InvalidHeaderValueError(msg)
Example #12
0
    def date(self):
        """Value of the Date header, converted to a datetime instance.

        Returns:
            An instance of datetime.datetime representing the value of
            the Date header, or None if the Date header is not present
            in the request.

        Raises:
            HTTPBadRequest: The date value could not be parsed, likely
                because it does not confrom to RFC 1123.

        """

        http_date = self._get_header_by_wsgi_name('DATE')
        try:
            return util.http_date_to_dt(http_date)
        except ValueError:
            msg = ('The value of the Date header could not be parsed. It '
                   'must be formatted according to RFC 1123.')
            raise InvalidHeaderValueError(msg)
Example #13
0
    def expires(self):
        if self._expires:
            return http_date_to_dt(self._expires, obs_date=True)

        return None
Example #14
0
    def expires(self):
        if self._expires:
            return http_date_to_dt(self._expires, obs_date=True)

        return None
Example #15
0
    def expires(self) -> Optional[dt.datetime]:
        if self._expires:  # type: ignore[attr-defined]
            return http_date_to_dt(self._expires,
                                   obs_date=True)  # type: ignore[attr-defined]

        return None