Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/opentelemetry/util/re.py: 52%
31 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
1# Copyright The OpenTelemetry Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15from logging import getLogger
16from re import compile, split
17from typing import Dict, List, Mapping
18from urllib.parse import unquote
20from deprecated import deprecated
22_logger = getLogger(__name__)
24# The following regexes reference this spec: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specifying-headers-via-environment-variables
26# Optional whitespace
27_OWS = r"[ \t]*"
28# A key contains printable US-ASCII characters except: SP and "(),/:;<=>?@[\]{}
29_KEY_FORMAT = (
30 r"[\x21\x23-\x27\x2a\x2b\x2d\x2e\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+"
31)
32# A value contains a URL-encoded UTF-8 string. The encoded form can contain any
33# printable US-ASCII characters (0x20-0x7f) other than SP, DEL, and ",;/
34_VALUE_FORMAT = r"[\x21\x23-\x2b\x2d-\x3a\x3c-\x5b\x5d-\x7e]*"
35# A key-value is key=value, with optional whitespace surrounding key and value
36_KEY_VALUE_FORMAT = rf"{_OWS}{_KEY_FORMAT}{_OWS}={_OWS}{_VALUE_FORMAT}{_OWS}"
38_HEADER_PATTERN = compile(_KEY_VALUE_FORMAT)
39_DELIMITER_PATTERN = compile(r"[ \t]*,[ \t]*")
41_BAGGAGE_PROPERTY_FORMAT = rf"{_KEY_VALUE_FORMAT}|{_OWS}{_KEY_FORMAT}{_OWS}"
44# pylint: disable=invalid-name
47@deprecated(version="1.15.0", reason="You should use parse_env_headers") # type: ignore
48def parse_headers(s: str) -> Mapping[str, str]:
49 return parse_env_headers(s)
52def parse_env_headers(s: str) -> Mapping[str, str]:
53 """
54 Parse ``s``, which is a ``str`` instance containing HTTP headers encoded
55 for use in ENV variables per the W3C Baggage HTTP header format at
56 https://www.w3.org/TR/baggage/#baggage-http-header-format, except that
57 additional semi-colon delimited metadata is not supported.
58 """
59 headers: Dict[str, str] = {}
60 headers_list: List[str] = split(_DELIMITER_PATTERN, s)
61 for header in headers_list:
62 if not header: # empty string
63 continue
64 match = _HEADER_PATTERN.fullmatch(header.strip())
65 if not match:
66 _logger.warning(
67 "Header format invalid! Header values in environment variables must be "
68 "URL encoded per the OpenTelemetry Protocol Exporter specification: %s",
69 header,
70 )
71 continue
72 # value may contain any number of `=`
73 name, value = match.string.split("=", 1)
74 name = unquote(name).strip().lower()
75 value = unquote(value).strip()
76 headers[name] = value
78 return headers