def test_request_pyi(): request = Request("GET", b"/", [(b"cookie", b"foo=aaa")]) request.cookies["foo"] == "aaa" request.get_cookie("foo") == "aaa" request.get_first_header(b"cookie") == b"foo=aaa" request.set_cookie("lorem", "ipsum") request.get_cookie("lorem") == "ipsum"
def is_requested_range_actual(request: Request, info: FileInfo) -> bool: if_range = request.get_first_header(b"if-range") if not if_range: return True return if_range == info.etag.encode() or if_range == info.modified_time.encode()
def _get_requested_range(request: Request) -> Optional[Range]: # http://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7233.html#rfc.section.3.1 # A server must ignore a Range header field received with a request method # other than GET if request.method != "GET": return None # NB: only the first Range request header is taken into consideration; # if the HTTP contains several Range headers, only the first is used range_header = request.get_first_header(b"range") if not range_header: return None try: value = Range.parse(range_header) except InvalidRangeValue: raise BadRequest("Invalid Range header") else: # An origin server must ignore a Range header field that contains # a range unit it does not understand. if value.unit != "bytes": return None return value