cardinal_pythonlib.pyramid.requests
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Functions to operate on Pyramid requests.
- cardinal_pythonlib.pyramid.requests.decompress_request(request: Request) None [source]
Reverses anything specified in
Content-Encoding
, modifying the request in place.
- cardinal_pythonlib.pyramid.requests.gen_accept_encoding_definitions(accept_encoding: str) Generator[str, None, None] [source]
For a given HTTP
Accept-Encoding
field value, generate encoding definitions. An example might be:from cardinal_pythonlib.pyramid.compression import * accept_encoding = "br;q=1.0, gzip;q=0.8, *;q=0.1" print(list(gen_encoding_definitions(accept_encoding)))
which gives
['br;q=1.0', 'gzip;q=0.8', '*;q=0.1']
- cardinal_pythonlib.pyramid.requests.gen_accept_encodings(accept_encoding: str) Generator[str, None, None] [source]
For a given HTTP
Accept-Encoding
field value, generate encodings. An example might be:from cardinal_pythonlib.pyramid.compression import * accept_encoding = "br;q=1.0, gzip;q=0.8, *;q=0.1" print(list(gen_encodings(accept_encoding)))
which gives
['br', 'gzip', '*']
- cardinal_pythonlib.pyramid.requests.gen_content_encodings(request: Request) Generator[str, None, None] [source]
Generates content encodings in the order they are specified – that is, the order in which they were applied.
- cardinal_pythonlib.pyramid.requests.gen_content_encodings_reversed(request: Request) Generator[str, None, None] [source]
Generates content encodings in reverse order – that is, in the order required to reverse them.
- cardinal_pythonlib.pyramid.requests.request_accepts_gzip(request: Request) bool [source]
Does the request specify an
Accept-Encoding
header that includesgzip
?Note:
Field names in HTTP headers are case-insensitive (e.g. “accept-encoding” is fine).
WebOb request headers are in a case-insensitive dictionary; see https://docs.pylonsproject.org/projects/webob/en/stable/. (Easily verified by altering the key being checked; it works.)
However, the value is a Python string so is case-sensitive.
But the HTTP standard doesn’t say that field values are case-insensitive; see https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2.
So we’ll do a case-sensitive check for “gzip”.
But there is also a bit of other syntax possible; see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding.