MCPcopy Create free account
hub / github.com/python-websockets/websockets / read_headers

Function read_headers

src/websockets/legacy/http.py:146–183  ·  view source on GitHub ↗

Read HTTP headers from ``stream``. Non-ASCII characters are represented with surrogate escapes.

(stream: asyncio.StreamReader)

Source from the content-addressed store, hash-verified

144
145
146async def read_headers(stream: asyncio.StreamReader) -> Headers:
147 """
148 Read HTTP headers from ``stream``.
149
150 Non-ASCII characters are represented with surrogate escapes.
151
152 """
153 # https://datatracker.ietf.org/doc/html/rfc7230#section-3.2
154
155 # We don't attempt to support obsolete line folding.
156
157 headers = Headers()
158 for _ in range(MAX_NUM_HEADERS + 1):
159 try:
160 line = await read_line(stream)
161 except EOFError as exc:
162 raise EOFError("connection closed while reading HTTP headers") from exc
163 if line == b"":
164 break
165
166 try:
167 raw_name, raw_value = line.split(b":", 1)
168 except ValueError: # not enough values to unpack (expected 2, got 1)
169 raise ValueError(f"invalid HTTP header line: {d(line)}") from None
170 if not _token_re.fullmatch(raw_name):
171 raise ValueError(f"invalid HTTP header name: {d(raw_name)}")
172 raw_value = raw_value.strip(b" \t")
173 if not _value_re.fullmatch(raw_value):
174 raise ValueError(f"invalid HTTP header value: {d(raw_value)}")
175
176 name = raw_name.decode("ascii") # guaranteed to be ASCII at this point
177 value = raw_value.decode("ascii", "surrogateescape")
178 headers[name] = value
179
180 else:
181 raise SecurityError("too many HTTP headers")
182
183 return headers
184
185
186async def read_line(stream: asyncio.StreamReader) -> bytes:

Callers 7

test_header_nameMethod · 0.90
test_header_valueMethod · 0.90
test_headers_limitMethod · 0.90
test_line_limitMethod · 0.90
test_line_endingMethod · 0.90
read_requestFunction · 0.85
read_responseFunction · 0.85

Calls 5

HeadersClass · 0.85
read_lineFunction · 0.85
SecurityErrorClass · 0.85
dFunction · 0.70
decodeMethod · 0.45

Tested by 5

test_header_nameMethod · 0.72
test_header_valueMethod · 0.72
test_headers_limitMethod · 0.72
test_line_limitMethod · 0.72
test_line_endingMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…