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

Function quote_header_value

src/werkzeug/http.py:138–171  ·  view source on GitHub ↗

Add double quotes around a header value. If the header contains only ASCII token characters, it will be returned unchanged. If the header contains ``"`` or ``\\`` characters, they will be escaped with an additional ``\\`` character. This is the reverse of :func:`unquote_header_value`.

(value: t.Any, allow_token: bool = True)

Source from the content-addressed store, hash-verified

136
137
138def quote_header_value(value: t.Any, allow_token: bool = True) -> str:
139 """Add double quotes around a header value. If the header contains only ASCII token
140 characters, it will be returned unchanged. If the header contains ``"`` or ``\\``
141 characters, they will be escaped with an additional ``\\`` character.
142
143 This is the reverse of :func:`unquote_header_value`.
144
145 :param value: The value to quote. Will be converted to a string.
146 :param allow_token: Disable to quote the value even if it only has token characters.
147
148 .. versionchanged:: 3.0
149 Passing bytes is not supported.
150
151 .. versionchanged:: 3.0
152 The ``extra_chars`` parameter is removed.
153
154 .. versionchanged:: 2.3
155 The value is quoted if it is the empty string.
156
157 .. versionadded:: 0.5
158 """
159 value_str = str(value)
160
161 if not value_str:
162 return '""'
163
164 if allow_token:
165 token_chars = _token_chars
166
167 if token_chars.issuperset(value_str):
168 return value_str
169
170 value_str = value_str.replace("\\", "\\\\").replace('"', '\\"')
171 return f'"{value_str}"'
172
173
174_unslash_re = re.compile(r"\\(.)", re.A)

Callers 3

dump_options_headerFunction · 0.85
dump_headerFunction · 0.85
to_headerMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected