Beispiel #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')

        Keyword Args:
            required (bool): raise HTTPBadRequest (default False)

            obs_date (bool): 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)
            if http_date is not None:
                return to_gmt(http_date, src=TimezoneGMT())
        except TypeError:
            return None
        except ValueError:
            msg = "It must be formatted according to RFC 7231, Section 7.1.1.1"
            raise HTTPInvalidHeader(header, msg)
Beispiel #2
0
 def last_modified(self):
     last_modified = self.get_header('last-modified')
     if last_modified is not None:
         return to_gmt(last_modified, src=TimezoneGMT())
Beispiel #3
0
 def expires(self):
     expires = self.get_header('expires')
     if expires is not None:
         return to_gmt(expires, src=TimezoneGMT())
Beispiel #4
0
# THE POSSIBILITY OF SUCH DAMAGE.
from io import BytesIO
from collections import OrderedDict
from http.cookies import SimpleCookie, CookieError

from luxon import __identity__
from luxon import constants as const
from luxon.utils.encoding import is_ascii
from luxon.utils.timezone import TimezoneGMT, to_gmt, format_http_datetime
from luxon.core.handlers.wsgi.redirects import Redirects
from luxon.structs.cidict import CiDict
from luxon.utils.encoding import if_unicode_to_bytes
from luxon.utils.http import (parse_cache_control_header, ETags)
from luxon.utils import js

GMT_TIMEZONE = TimezoneGMT()


class Response(Redirects):
    """Represents an HTTP response to a client request.

    Args:
        env (dict): A WSGI environment dict passed in from the server.
            As per PEP-3333.
        start_response (function): callback function supplied by the server
            which takes the HTTP status and headers as arguments.

    Attributes:
        status (int): HTTP status code. (e.g. '200') Default 200.
    """
    _DEFAULT_CONTENT_TYPE = const.APPLICATION_JSON