(
self,
status_code: int,
headers: httputil.HTTPHeaders,
chunk: bytes,
finishing: bool,
)
| 3124 | return ctype.startswith("text/") or ctype in self.CONTENT_TYPES |
| 3125 | |
| 3126 | def transform_first_chunk( |
| 3127 | self, |
| 3128 | status_code: int, |
| 3129 | headers: httputil.HTTPHeaders, |
| 3130 | chunk: bytes, |
| 3131 | finishing: bool, |
| 3132 | ) -> Tuple[int, httputil.HTTPHeaders, bytes]: |
| 3133 | # TODO: can/should this type be inherited from the superclass? |
| 3134 | if "Vary" in headers: |
| 3135 | headers["Vary"] += ", Accept-Encoding" |
| 3136 | else: |
| 3137 | headers["Vary"] = "Accept-Encoding" |
| 3138 | if self._gzipping: |
| 3139 | ctype = _unicode(headers.get("Content-Type", "")).split(";")[0] |
| 3140 | self._gzipping = ( |
| 3141 | self._compressible_type(ctype) |
| 3142 | and (not finishing or len(chunk) >= self.MIN_LENGTH) |
| 3143 | and ("Content-Encoding" not in headers) |
| 3144 | ) |
| 3145 | if self._gzipping: |
| 3146 | headers["Content-Encoding"] = "gzip" |
| 3147 | self._gzip_value = BytesIO() |
| 3148 | self._gzip_file = gzip.GzipFile( |
| 3149 | mode="w", fileobj=self._gzip_value, compresslevel=self.GZIP_LEVEL |
| 3150 | ) |
| 3151 | chunk = self.transform_chunk(chunk, finishing) |
| 3152 | if "Content-Length" in headers: |
| 3153 | # The original content length is no longer correct. |
| 3154 | # If this is the last (and only) chunk, we can set the new |
| 3155 | # content-length; otherwise we remove it and fall back to |
| 3156 | # chunked encoding. |
| 3157 | if finishing: |
| 3158 | headers["Content-Length"] = str(len(chunk)) |
| 3159 | else: |
| 3160 | del headers["Content-Length"] |
| 3161 | return status_code, headers, chunk |
| 3162 | |
| 3163 | def transform_chunk(self, chunk: bytes, finishing: bool) -> bytes: |
| 3164 | if self._gzipping: |
nothing calls this directly
no test coverage detected