MCPcopy
hub / github.com/pallets/werkzeug / make_conditional

Method make_conditional

src/werkzeug/wrappers/response.py:708–783  ·  view source on GitHub ↗

Make the response conditional to the request. This method works best if an etag was defined for the response already. The `add_etag` method can be used to do that. If called without etag just the date header is set. This does nothing if the request method in the r

(
        self,
        request_or_environ: WSGIEnvironment | Request,
        accept_ranges: bool | str = False,
        complete_length: int | None = None,
    )

Source from the content-addressed store, hash-verified

706 return True
707
708 def make_conditional(
709 self,
710 request_or_environ: WSGIEnvironment | Request,
711 accept_ranges: bool | str = False,
712 complete_length: int | None = None,
713 ) -> Response:
714 """Make the response conditional to the request. This method works
715 best if an etag was defined for the response already. The `add_etag`
716 method can be used to do that. If called without etag just the date
717 header is set.
718
719 This does nothing if the request method in the request or environ is
720 anything but GET or HEAD.
721
722 For optimal performance when handling range requests, it's recommended
723 that your response data object implements `seekable`, `seek` and `tell`
724 methods as described by :py:class:`io.IOBase`. Objects returned by
725 :meth:`~werkzeug.wsgi.wrap_file` automatically implement those methods.
726
727 It does not remove the body of the response because that's something
728 the :meth:`__call__` function does for us automatically.
729
730 Returns self so that you can do ``return resp.make_conditional(req)``
731 but modifies the object in-place.
732
733 :param request_or_environ: a request object or WSGI environment to be
734 used to make the response conditional
735 against.
736 :param accept_ranges: This parameter dictates the value of
737 `Accept-Ranges` header. If ``False`` (default),
738 the header is not set. If ``True``, it will be set
739 to ``"bytes"``. If it's a string, it will use this
740 value.
741 :param complete_length: Will be used only in valid Range Requests.
742 It will set `Content-Range` complete length
743 value and compute `Content-Length` real value.
744 This parameter is mandatory for successful
745 Range Requests completion.
746 :raises: :class:`~werkzeug.exceptions.RequestedRangeNotSatisfiable`
747 if `Range` header could not be parsed or satisfied.
748
749 .. versionchangedd: 3.2
750 Adds the ``Accept-Ranges`` header if ``accept_ranges`` is passed,
751 even if this is not a satisfiable range request.
752
753 .. versionchanged:: 2.0
754 Range processing is skipped if length is 0 instead of
755 raising a 416 Range Not Satisfiable error.
756 """
757 environ = _get_environ(request_or_environ)
758 if environ["REQUEST_METHOD"] in ("GET", "HEAD"):
759 # if the date is not in the headers, add it now. We however
760 # will not override an already existing header. Unfortunately
761 # this header will be overridden by many WSGI servers including
762 # wsgiref.
763 if "date" not in self.headers:
764 self.headers["Date"] = http_date()
765 is206 = self._process_range_request(environ, complete_length, accept_ranges)

Calls 7

_get_environFunction · 0.85
http_dateFunction · 0.85
parse_etagsFunction · 0.85
is_resource_modifiedFunction · 0.50
getMethod · 0.45