_iter_ (): 143 yield _native ( x ) 144 145 def get_all ( self, name : str | bytes ) -> list : 146 """ 147 Like `Headers.get`, but does not fold multiple headers into a single one. ![]() _delitem_ ( key ) 140 141 def _iter_ ( self ) -> Iterator : 142 for x in super (). fields ) + b " \r\n " 134 else : 135 return b "" 136 137 def _delitem_ ( self, key : str | bytes ) -> None : 138 key = _always_bytes ( key ) 139 super (). lower () 130 131 def _bytes_ ( self ) -> bytes : 132 if self. join ( values ) 125 126 127 def _kconv ( key ) -> str : 128 # Headers are case-insensitive 129 return key. ] 120 121 122 def _reduce_values ( values ) -> str : 123 # Headers can be folded 124 return ", ". fields : 108 if not isinstance ( key, bytes ) or not isinstance ( value, bytes ): 109 raise TypeError ( "Header fields must be bytes." ) 110 111 # content_type -> content-type 112 self. _init_ ( fields ) 106 107 for key, value in self. 101 102 If ``**headers`` contains multiple keys that have equal ``.lower()`` representations, 103 the behavior is undefined. 99 For convenience, underscores in header names will be transformed to dashes - 100 this behaviour does not extend to other methods. ![]() Will overwrite existing values from `fields`. 98 - *\\*\\*headers:* Additional headers to set. 53 54 Create headers with keyword arguments: 55 > h = Headers(host="", content_type="application/xml") 56 57 Headers mostly behave like a normal dict: 58 > h 59 "" 60 61 Headers are case insensitive: 62 > h 63 "" 64 65 Headers can also be created from a list of raw (header_name, header_value) byte tuples: 66 > h = Headers() 71 72 Multiple headers are folded into a single header as per RFC 7230: 73 > h 74 "text/html, application/xml" 75 76 Setting a header removes all existing headers with the same name: 77 > h = "application/text" 78 > h 79 "application/text" 80 81 `bytes(h)` returns an HTTP/1 header block: 82 > print(bytes(h)) 83 Host: 84 Accept: application/text 85 86 For full control, the raw header fields can be accessed: 87 > h.fields 88 89 Caveats: 90 - For use with the "Set-Cookie" and "Cookie" headers, either use `okies` or see `Headers.get_all`. MultiDict ): # type: ignore 50 """ 51 Header class which allows both convenient access to individual headers as well as 52 direct access to the underlying raw data. ![]() always_bytes ( x, "utf-8", "surrogateescape" ) 46 47 48 # This cannot be easily typed with mypy yet, so we just specify MultiDict without concrete types. decode ( "utf-8", "surrogateescape" ) 42 43 44 def _always_bytes ( x : str | bytes ) -> bytes : 45 return strutils. ![]() 40 def _native ( x : bytes ) -> str : 41 return x. 1 import binascii 2 import json 3 import os 4 import time 5 import urllib.parse 6 import warnings 7 from collections.abc import Callable 8 from collections.abc import Iterable 9 from collections.abc import Iterator 10 from collections.abc import Mapping 11 from collections.abc import Sequence 12 from dataclasses import dataclass 13 from dataclasses import fields 14 from email.utils import formatdate 15 from email.utils import mktime_tz 16 from email.utils import parsedate_tz 17 from typing import Any 18 from typing import cast 19 20 from mitmproxy import flow 21 from retypes import multidict 22 from retypes import serializable 23 from import encoding 24 from import cookies 25 from import multipart 26 from import status_codes 27 from import url 28 from import assemble_content_type 29 from import infer_content_encoding 30 from import parse_content_type 31 from mitmproxy.utils import human 32 from mitmproxy.utils import strutils 33 from mitmproxy.utils import typecheck 34 from import always_bytes 35 from import always_str 36 from mitmproxy.websocket import WebSocketData 37 38 39 # While headers _should_ be ASCII, it's not uncommon for certain headers to be utf-8 encoded.
0 Comments
Leave a Reply. |