MCPcopy Index your code
hub / github.com/python-websockets/websockets / parse_list

Function parse_list

src/websockets/headers.py:165–228  ·  view source on GitHub ↗

Parse a comma-separated list from ``header`` at the given position. This is appropriate for parsing values with the following grammar: 1#item ``parse_item`` parses one item. ``header`` is assumed not to start or end with whitespace. (This function is designed for pa

(
    parse_item: Callable[[str, int, str], tuple[T, int]],
    header: str,
    pos: int,
    header_name: str,
)

Source from the content-addressed store, hash-verified

163
164
165def parse_list(
166 parse_item: Callable[[str, int, str], tuple[T, int]],
167 header: str,
168 pos: int,
169 header_name: str,
170) -> list[T]:
171 """
172 Parse a comma-separated list from ``header`` at the given position.
173
174 This is appropriate for parsing values with the following grammar:
175
176 1#item
177
178 ``parse_item`` parses one item.
179
180 ``header`` is assumed not to start or end with whitespace.
181
182 (This function is designed for parsing an entire header value and
183 :func:`~websockets.http.read_headers` strips whitespace from values.)
184
185 Return a list of items.
186
187 Raises:
188 InvalidHeaderFormat: On invalid inputs.
189
190 """
191 # Per https://datatracker.ietf.org/doc/html/rfc7230#section-7, "a recipient
192 # MUST parse and ignore a reasonable number of empty list elements";
193 # hence while loops that remove extra delimiters.
194
195 # Remove extra delimiters before the first item.
196 while peek_ahead(header, pos) == ",":
197 pos = parse_OWS(header, pos + 1)
198
199 items = []
200 while True:
201 # Loop invariant: a item starts at pos in header.
202 item, pos = parse_item(header, pos, header_name)
203 items.append(item)
204 pos = parse_OWS(header, pos)
205
206 # We may have reached the end of the header.
207 if pos == len(header):
208 break
209
210 # There must be a delimiter after each element except the last one.
211 if peek_ahead(header, pos) == ",":
212 pos = parse_OWS(header, pos + 1)
213 else:
214 raise InvalidHeaderFormat(header_name, "expected comma", header, pos)
215
216 # Remove extra delimiters before the next item.
217 while peek_ahead(header, pos) == ",":
218 pos = parse_OWS(header, pos + 1)
219
220 # We may have reached the end of the header.
221 if pos == len(header):
222 break

Callers 4

parse_connectionFunction · 0.85
parse_upgradeFunction · 0.85
parse_extensionFunction · 0.85
parse_subprotocolFunction · 0.85

Calls 4

peek_aheadFunction · 0.85
parse_OWSFunction · 0.85
InvalidHeaderFormatClass · 0.85
appendMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…