Exemplo n.º 1
0
 def __init__(self, version, header_entries, body, trailer_entries=None):
     super(Message, self).__init__()
     self.version = (HTTPVersion(force_unicode(version))
                     if version is not None else None)
     self.header_entries = [HeaderEntry(k, v)
                            for k, v in header_entries]
     self.body = bytes(body) if okay(body) else body
     self.trailer_entries = [HeaderEntry(k, v)
                             for k, v in trailer_entries or []]
     self.rebuild_headers()
     self.annotations = {}
Exemplo n.º 2
0
 def __init__(self, version, header_entries, body, trailer_entries=None,
              remark=None):
     super(Message, self).__init__()
     self.version = (HTTPVersion(force_unicode(version))
                     if version is not None else None)
     self.header_entries = [HeaderEntry(k, v)
                            for k, v in header_entries]
     self.body = bytes(body) if okay(body) else body
     self.trailer_entries = [HeaderEntry(k, v)
                             for k, v in trailer_entries or []]
     self.rebuild_headers()
     self.annotations = {}
     self.remark = remark
Exemplo n.º 3
0
def _check_protocol_id(complain, encoded_id):
    # Since there is only one correct way to encode
    # an ALPN protocol ID into an RFC 7838 ``protocol-id``,
    # we just compute it and compare to what's in the message.
    decoded_id = pct_decode(force_bytes(encoded_id))
    correct_encoded_id = u''
    for c in iterbytes(decoded_id):
        if (tchar - '%').match(c):
            correct_encoded_id += force_unicode(c)
        else:
            correct_encoded_id += pct_encode(c, safe='').upper()
    if encoded_id != correct_encoded_id:
        complain(1256, actual=encoded_id, correct=correct_encoded_id)
    return decoded_id
Exemplo n.º 4
0
def _check_protocol_id(complain, encoded_id):
    # Since there is only one correct way to encode
    # an ALPN protocol ID into an RFC 7838 ``protocol-id``,
    # we just compute it and compare to what's in the message.
    decoded_id = pct_decode(force_bytes(encoded_id))
    correct_encoded_id = u''
    for c in iterbytes(decoded_id):
        if (tchar - '%').match(c):
            correct_encoded_id += force_unicode(c)
        else:
            correct_encoded_id += pct_encode(c, safe='').upper()
    if encoded_id != correct_encoded_id:
        complain(1256, actual=encoded_id, correct=correct_encoded_id)
    return decoded_id
Exemplo n.º 5
0
    def __init__(self, scheme, method, target, version, header_entries,
                 body, trailer_entries=None):
        # pylint: disable=redefined-outer-name
        """
        :param scheme:
            The scheme of the request URI, as a Unicode string
            (usually ``u'http'`` or ``u'https'``),
            or `None` if unknown (this disables some checks).

        :param method:
            The request method, as a Unicode string.

        :param target:
            The request target, as a Unicode string.
            It must be in one of the four forms `defined by RFC 7230`__.
            (For HTTP/2, it can be `reconstructed from pseudo-headers`__.)

            __ https://tools.ietf.org/html/rfc7230#section-5.3
            __ https://tools.ietf.org/html/rfc7540#section-8.1.2.3

        :param version:
            The request's protocol version, as a Unicode string,
            or `None` if unknown (this disables some checks).

            For requests sent over HTTP/1.x connections,
            this should be the HTTP version sent in the `request line`__,
            such as ``u'HTTP/1.0'`` or ``u'HTTP/1.1'``.

            __ https://tools.ietf.org/html/rfc7230#section-3.1.1

            For requests sent over HTTP/2 connections,
            this should be ``u'HTTP/2'``.

        :param header_entries:
            A list of the request's headers (may be empty).
            It must **not** include HTTP/2 `pseudo-headers`__.

            __ https://tools.ietf.org/html/rfc7540#section-8.1.2.1

            Every item of the list must be a ``(name, value)`` pair.

            `name` must be a Unicode string.

            `value` may be a byte string or a Unicode string.
            If it is Unicode, HTTPolice will assume that it has been decoded
            from ISO-8859-1 (the historic encoding of HTTP),
            and will encode it back into ISO-8859-1 before any processing.

        :param body:
            The request's payload body, as a **byte string**,
            or `None` if unknown (this disables some checks).

            If the request has no payload (like a GET request),
            this should be the empty string ``b''``.

            This must be the payload body as `defined by RFC 7230`__:
            **after** removing any ``Transfer-Encoding`` (like ``chunked``),
            but **before** removing any ``Content-Encoding`` (like ``gzip``).

            __ https://tools.ietf.org/html/rfc7230#section-3.3

        :param trailer_entries:
            A list of headers from the request's trailer part
            (as found in `chunked coding`__ or `HTTP/2`__),
            or `None` if there is no trailer part.

            __ https://tools.ietf.org/html/rfc7230#section-4.1.2
            __ https://tools.ietf.org/html/rfc7540#section-8.1

            The format is the same as for `header_entries`.

        """
        super(Request, self).__init__(version, header_entries, body,
                                      trailer_entries)
        self.scheme = force_unicode(scheme) if scheme is not None else None
        self.method = Method(force_unicode(method))
        self.target = force_unicode(target)
Exemplo n.º 6
0
def _header_name_from_cgi(cgi_name):
    prefix = 'HTTP_'
    if cgi_name.startswith(prefix):
        cgi_name = cgi_name[len(prefix):]
    return force_unicode('-'.join(p.title() for p in cgi_name.split('_')))
Exemplo n.º 7
0
    def __init__(self,
                 scheme,
                 method,
                 target,
                 version,
                 header_entries,
                 body,
                 trailer_entries=None,
                 remark=None):
        # pylint: disable=redefined-outer-name
        """
        :param scheme:
            The scheme of the request URI, as a Unicode string
            (usually ``u'http'`` or ``u'https'``),
            or `None` if unknown (this disables some checks).

        :param method:
            The request method, as a Unicode string.

        :param target:
            The request target, as a Unicode string.
            It must be in one of the four forms `defined by RFC 7230`__.
            (For HTTP/2, it can be `reconstructed from pseudo-headers`__.)

            __ https://tools.ietf.org/html/rfc7230#section-5.3
            __ https://tools.ietf.org/html/rfc7540#section-8.1.2.3

        :param version:
            The request's protocol version, as a Unicode string,
            or `None` if unknown (this disables some checks).

            For requests sent over HTTP/1.x connections,
            this should be the HTTP version sent in the `request line`__,
            such as ``u'HTTP/1.0'`` or ``u'HTTP/1.1'``.

            __ https://tools.ietf.org/html/rfc7230#section-3.1.1

            For requests sent over HTTP/2 connections,
            this should be ``u'HTTP/2'``.

        :param header_entries:
            A list of the request's headers (may be empty).
            It must **not** include HTTP/2 `pseudo-headers`__.

            __ https://tools.ietf.org/html/rfc7540#section-8.1.2.1

            Every item of the list must be a ``(name, value)`` pair.

            `name` must be a Unicode string.

            `value` may be a byte string or a Unicode string.
            If it is Unicode, HTTPolice will assume that it has been decoded
            from ISO-8859-1 (the historic encoding of HTTP),
            and will encode it back into ISO-8859-1 before any processing.

        :param body:
            The request's payload body, as a **byte string**,
            or `None` if unknown (this disables some checks).

            If the request has no payload (like a GET request),
            this should be the empty string ``b''``.

            This must be the payload body as `defined by RFC 7230`__:
            **after** removing any ``Transfer-Encoding`` (like ``chunked``),
            but **before** removing any ``Content-Encoding`` (like ``gzip``).

            __ https://tools.ietf.org/html/rfc7230#section-3.3

        :param trailer_entries:
            A list of headers from the request's trailer part
            (as found in `chunked coding`__ or `HTTP/2`__),
            or `None` if there is no trailer part.

            __ https://tools.ietf.org/html/rfc7230#section-4.1.2
            __ https://tools.ietf.org/html/rfc7540#section-8.1

            The format is the same as for `header_entries`.

        :param remark:
            If not `None`, this Unicode string will be shown
            above the request in HTML reports
            (when the appropriate option is enabled).
            For example, it can be used to identify the source of the data:
            ``u'from somefile.dat, offset 1337'``.

        """
        super(Request, self).__init__(version, header_entries, body,
                                      trailer_entries, remark)
        self.scheme = force_unicode(scheme) if scheme is not None else None
        self.method = Method(force_unicode(method))
        self.target = force_unicode(target)
Exemplo n.º 8
0
 def __new__(cls, name, value):
     return super(HeaderEntry, cls).__new__(cls,
                                            FieldName(force_unicode(name)),
                                            force_bytes(value))
Exemplo n.º 9
0
def _header_name_from_cgi(cgi_name):
    prefix = 'HTTP_'
    if cgi_name.startswith(prefix):
        cgi_name = cgi_name[len(prefix):]
    return force_unicode('-'.join(p.title() for p in cgi_name.split('_')))
Exemplo n.º 10
0
 def __new__(cls, name, value):
     return super(HeaderEntry, cls).__new__(cls,
                                            FieldName(force_unicode(name)),
                                            force_bytes(value))
Exemplo n.º 11
0
 def __str__(self):  # pragma: no cover
     if self.inner is None:
         return u'(?)'
     else:
         return force_unicode(self.inner)
Exemplo n.º 12
0
    def __init__(self, version, status, reason, header_entries,
                 body, trailer_entries=None):
        """
        :param version:
            The response's protocol version, as a Unicode string,
            or `None` if unknown (this disables some checks).

            For responses sent over HTTP/1.x connections,
            this should be the HTTP version sent in the `status line`__,
            such as ``u'HTTP/1.0'`` or ``u'HTTP/1.1'``.

            __ https://tools.ietf.org/html/rfc7230#section-3.1.2

            For responses sent over HTTP/2 connections,
            this should be ``u'HTTP/2'``.

        :param status:
            The response's status code, as an integer.

        :param reason:
            The response's reason phrase (such as "OK" or "Not Found"),
            as a Unicode string, or `None` if unknown (as in HTTP/2).

        :param header_entries:
            A list of the response's headers (may be empty).
            It must **not** include HTTP/2 `pseudo-headers`__.

            __ https://tools.ietf.org/html/rfc7540#section-8.1.2.1

            Every item of the list must be a ``(name, value)`` pair.

            `name` must be a Unicode string.

            `value` may be a byte string or a Unicode string.
            If it is Unicode, HTTPolice will assume that it has been decoded
            from ISO-8859-1 (the historic encoding of HTTP),
            and will encode it back into ISO-8859-1 before any processing.

        :param body:
            The response's payload body, as a **byte string**,
            or `None` if unknown (this disables some checks).

            If the response has no payload (like 204 or 304 responses),
            this should be the empty string ``b''``.

            This must be the payload body as `defined by RFC 7230`__:
            **after** removing any ``Transfer-Encoding`` (like ``chunked``),
            but **before** removing any ``Content-Encoding`` (like ``gzip``).

            __ https://tools.ietf.org/html/rfc7230#section-3.3

        :param trailer_entries:
            A list of headers from the response's trailer part
            (as found in `chunked coding`__ or `HTTP/2`__),
            or `None` if there is no trailer part.

            __ https://tools.ietf.org/html/rfc7230#section-4.1.2
            __ https://tools.ietf.org/html/rfc7540#section-8.1

            The format is the same as for `header_entries`.

        """
        super(Response, self).__init__(version, header_entries,
                                       body, trailer_entries)
        self.status = StatusCode(status)
        self.reason = force_unicode(reason) if reason is not None else None
        self.request = None
Exemplo n.º 13
0
    def __init__(self,
                 version,
                 status,
                 reason,
                 header_entries,
                 body,
                 trailer_entries=None,
                 remark=None):
        """
        :param version:
            The response's protocol version, as a Unicode string,
            or `None` if unknown (this disables some checks).

            For responses sent over HTTP/1.x connections,
            this should be the HTTP version sent in the `status line`__,
            such as ``u'HTTP/1.0'`` or ``u'HTTP/1.1'``.

            __ https://tools.ietf.org/html/rfc7230#section-3.1.2

            For responses sent over HTTP/2 connections,
            this should be ``u'HTTP/2'``.

        :param status:
            The response's status code, as an integer.

        :param reason:
            The response's reason phrase (such as "OK" or "Not Found"),
            as a Unicode string, or `None` if unknown (as in HTTP/2).

        :param header_entries:
            A list of the response's headers (may be empty).
            It must **not** include HTTP/2 `pseudo-headers`__.

            __ https://tools.ietf.org/html/rfc7540#section-8.1.2.1

            Every item of the list must be a ``(name, value)`` pair.

            `name` must be a Unicode string.

            `value` may be a byte string or a Unicode string.
            If it is Unicode, HTTPolice will assume that it has been decoded
            from ISO-8859-1 (the historic encoding of HTTP),
            and will encode it back into ISO-8859-1 before any processing.

        :param body:
            The response's payload body, as a **byte string**,
            or `None` if unknown (this disables some checks).

            If the response has no payload (like 204 or 304 responses),
            this should be the empty string ``b''``.

            This must be the payload body as `defined by RFC 7230`__:
            **after** removing any ``Transfer-Encoding`` (like ``chunked``),
            but **before** removing any ``Content-Encoding`` (like ``gzip``).

            __ https://tools.ietf.org/html/rfc7230#section-3.3

        :param trailer_entries:
            A list of headers from the response's trailer part
            (as found in `chunked coding`__ or `HTTP/2`__),
            or `None` if there is no trailer part.

            __ https://tools.ietf.org/html/rfc7230#section-4.1.2
            __ https://tools.ietf.org/html/rfc7540#section-8.1

            The format is the same as for `header_entries`.

        :param remark:
            If not `None`, this Unicode string will be shown
            above this response in HTML reports
            (when the appropriate option is enabled).
            For example, it can be used to identify the source of the data:
            ``u'from somefile.dat, offset 1337'``.

        """
        super(Response, self).__init__(version, header_entries, body,
                                       trailer_entries, remark)
        self.status = StatusCode(status)
        self.reason = force_unicode(reason) if reason is not None else None
        self.request = None
Exemplo n.º 14
0
 def __str__(self):                                  # pragma: no cover
     if self.inner is None:
         return u'(?)'
     return force_unicode(self.inner)