This is automatically called right before the response is started and returns headers modified for the given environment. It returns a copy of the headers from the response with some modifications applied if necessary. For example the location header (if present) is
(self, environ: WSGIEnvironment)
| 436 | self.add_etag() |
| 437 | |
| 438 | def get_wsgi_headers(self, environ: WSGIEnvironment) -> Headers: |
| 439 | """This is automatically called right before the response is started |
| 440 | and returns headers modified for the given environment. It returns a |
| 441 | copy of the headers from the response with some modifications applied |
| 442 | if necessary. |
| 443 | |
| 444 | For example the location header (if present) is joined with the root |
| 445 | URL of the environment. Also the content length is automatically set |
| 446 | to zero here for certain status codes. |
| 447 | |
| 448 | .. versionchanged:: 0.6 |
| 449 | Previously that function was called `fix_headers` and modified |
| 450 | the response object in place. Also since 0.6, IRIs in location |
| 451 | and content-location headers are handled properly. |
| 452 | |
| 453 | Also starting with 0.6, Werkzeug will attempt to set the content |
| 454 | length if it is able to figure it out on its own. This is the |
| 455 | case if all the strings in the response iterable are already |
| 456 | encoded and the iterable is buffered. |
| 457 | |
| 458 | :param environ: the WSGI environment of the request. |
| 459 | :return: returns a new :class:`~werkzeug.datastructures.Headers` |
| 460 | object. |
| 461 | """ |
| 462 | headers = Headers(self.headers) |
| 463 | location: str | None = None |
| 464 | content_location: str | None = None |
| 465 | content_length: str | int | None = None |
| 466 | status = self.status_code |
| 467 | |
| 468 | # iterate over the headers to find all values in one go. Because |
| 469 | # get_wsgi_headers is used each response that gives us a tiny |
| 470 | # speedup. |
| 471 | for key, value in headers: |
| 472 | ikey = key.lower() |
| 473 | if ikey == "location": |
| 474 | location = value |
| 475 | elif ikey == "content-location": |
| 476 | content_location = value |
| 477 | elif ikey == "content-length": |
| 478 | content_length = value |
| 479 | |
| 480 | if location is not None: |
| 481 | location = iri_to_uri(location) |
| 482 | |
| 483 | if self.autocorrect_location_header: |
| 484 | # Make the location header an absolute URL. |
| 485 | current_url = get_current_url(environ, strip_querystring=True) |
| 486 | current_url = iri_to_uri(current_url) |
| 487 | location = urljoin(current_url, location) |
| 488 | |
| 489 | headers["Location"] = location |
| 490 | |
| 491 | # make sure the content location is a URL |
| 492 | if content_location is not None: |
| 493 | headers["Content-Location"] = iri_to_uri(content_location) |
| 494 | |
| 495 | if 100 <= status < 200 or status == 204: |