python-hyper / h11

A pure-Python, bring-your-own-I/O implementation of HTTP/1.1
https://h11.readthedocs.io/
MIT License
490 stars 62 forks source link

Increase/Decrease status code ranges #134

Closed kujaomega closed 2 years ago

kujaomega commented 3 years ago

Thank you for all the development.

I have seen in h11/h11/_events.py / the handled status codes ranges are between [200, 600).

@dataclass(init=False, frozen=True)
class Response(_ResponseBase):
    """The beginning of an HTTP response.
    Fields:
    .. attribute:: status_code
       The status code of this response, as an integer. For an
       :class:`Response`, this is always in the range [200,
       600).
    .. attribute:: headers
       Request headers, represented as a list of (name, value) pairs. See
       :ref:`the header normalization rules <headers-format>` for details.
    .. attribute:: http_version
       The HTTP protocol version, represented as a byte string like
       ``b"1.1"``. See :ref:`the HTTP version normalization rules
       <http_version-format>` for details.
    .. attribute:: reason
       The reason phrase of this response, as a byte string. For example:
       ``b"OK"``, or ``b"Not Found"``.
    """

    def __post_init__(self) -> None:
        if not (200 <= self.status_code < 600):
            raise LocalProtocolError(
                "Response status_code should be in range [200, 600), not {}".format(
                    self.status_code
                )
            )

As this is the standard to follow, it's great to have it, but there are some http servers that don't follow the standard. What you think about adding the possibility to setup the status code ranges or increasing them for possible exceptional cases?

tomchristie commented 3 years ago

Given this observation... https://github.com/python-hyper/h11/issues/95#issuecomment-887892220

I think it'd be a fair change to expand the allowed range to support non-conforming XXX values.

$ curl --http1.1 --head https://www.linkedin.com/company/linkedin
HTTP/1.1 999 Request denied
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Length: 1529
Content-Type: text/html
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: trkCode=bf; Max-Age=5
Set-Cookie: trkInfo=AQEQu_fWUsOvWQAAAXsbrvEY6x_26G7cumSaPiDWve6_jGqzHEXzwMY8OZ8ZS_cqymL_WdCFz_hWkw_cEkQnGzBHcuZshMBT6Qpb71ALo51X26uz4ME_ik9uaysDlHsksXp9QU8=; Max-Age=5
Set-Cookie: rtc=AQGWPBJ4fVNkmQAAAXsbrvEYHs6svtFHGKh-_F7jCpvN1TMs49bCzdDAUjYVFSNwm9qFzLcGt6rISQDzO7RrTpq3VDKxvTBOKCU6Ed0UCY6A8juKSwg6xpA_nfn2s9eKKwAkoaamrh-at6PNEKKEsRAlHBMVQaLFpmkbFjI_w2EcIlM9nw5inH2Smq963e0qruVHMdYhG_qjKXgG; Max-Age=120; path=/; domain=.linkedin.com
X-Li-Fabric: prod-lor1
X-Li-Pop: afd-prod-lor1
X-Li-Proto: http/1.1
X-LI-UUID: tYdxOCq7mBaAQANv8ioAAA==
X-Cache: CONFIG_NOCACHE
X-MSEdge-Ref: Ref A: FFE6F8566CE84CBEA5FC515947EE7836 Ref B: LON04EDGE1114 Ref C: 2021-08-06T13:37:35Z
Date: Fri, 06 Aug 2021 13:37:35 GMT

Granted the server is non-conforming here, but it's a big-name site, and we need to be robust to in the wild behaviours, right?

I can't take a call on this for any other maintainers point of view, but if anyone's keen on putting together a pull request for this I think it'd be worth a go.

SariNusier commented 2 years ago

Hi all,

I'm wondering if at least we can include status code 100 because I'm not too sure how else to handle an expect: 100-continue request if we can't return a repsonse with status code 100.

This is part of the http1.1 protocol [1] as far as I understand it, so do you think it's valid to add it? Starlette also has this as a return code [2], but if I try to respond with it, I get an exception thrown from h11.

Edit: I just noticed that h11 does support InformationalResponse, so maybe this is a a problem in uvicorn rather than h11 🤔.

Any opinions/suggestions? Thanks!

[1] https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3 [2] https://github.com/encode/starlette/blob/master/starlette/status.py#L8

tyteen4a03 commented 2 years ago

Bumping this issue - would like to see support for 100 - 199 codes.

njsmith commented 2 years ago

@tyteen4a03 As far as I know, h11 has always had full support for 100-199 status codes. What are you missing?

tyteen4a03 commented 2 years ago

@tyteen4a03 As far as I know, h11 has always had full support for 100-199 status codes. What are you missing?

When trying to create a response with 100 status, I get Response status_code should be in range [200, 1000), not 103.

njsmith commented 2 years ago

@tyteen4a03 ah, that's because h11 models 1xx status codes as InformationalResponse objects, not Response objects, because they act in a special way in the HTTP/1.1 state machine.