Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/rfc3986/misc.py: 84%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (c) 2014 Rackspace
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11# implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""
15Module containing compiled regular expressions and constants.
17This module contains important constants, patterns, and compiled regular
18expressions for parsing and validating URIs and their components.
19"""
20import re
21import typing as t
23from . import abnf_regexp
26class URIReferenceBase(t.NamedTuple):
27 """The namedtuple used as a superclass of URIReference and IRIReference."""
29 scheme: t.Optional[str]
30 authority: t.Optional[str]
31 path: t.Optional[str]
32 query: t.Optional[str]
33 fragment: t.Optional[str]
36important_characters = {
37 "generic_delimiters": abnf_regexp.GENERIC_DELIMITERS,
38 "sub_delimiters": abnf_regexp.SUB_DELIMITERS,
39 # We need to escape the '*' in this case
40 "re_sub_delimiters": abnf_regexp.SUB_DELIMITERS_RE,
41 "unreserved_chars": abnf_regexp.UNRESERVED_CHARS,
42 # We need to escape the '-' in this case:
43 "re_unreserved": abnf_regexp.UNRESERVED_RE,
44}
46# For details about delimiters and reserved characters, see:
47# http://tools.ietf.org/html/rfc3986#section-2.2
48GENERIC_DELIMITERS = abnf_regexp.GENERIC_DELIMITERS_SET
49SUB_DELIMITERS = abnf_regexp.SUB_DELIMITERS_SET
50RESERVED_CHARS = abnf_regexp.RESERVED_CHARS_SET
51# For details about unreserved characters, see:
52# http://tools.ietf.org/html/rfc3986#section-2.3
53UNRESERVED_CHARS = abnf_regexp.UNRESERVED_CHARS_SET
54NON_PCT_ENCODED = abnf_regexp.NON_PCT_ENCODED_SET
56URI_MATCHER = re.compile(abnf_regexp.URL_PARSING_RE)
58SUBAUTHORITY_MATCHER = re.compile(
59 (
60 "^(?:(?P<userinfo>{})@)?" # userinfo
61 "(?P<host>{})" # host
62 "(?::(?P<port>{}))?$" # port
63 ).format(
64 abnf_regexp.USERINFO_RE, abnf_regexp.HOST_PATTERN, abnf_regexp.PORT_RE
65 )
66)
69HOST_MATCHER = re.compile("^" + abnf_regexp.HOST_RE + "$")
70IPv4_MATCHER = re.compile("^" + abnf_regexp.IPv4_RE + "$")
71IPv6_MATCHER = re.compile(r"^\[" + abnf_regexp.IPv6_ADDRZ_RFC4007_RE + r"\]$")
73# Used by host validator
74IPv6_NO_RFC4007_MATCHER = re.compile(r"^\[%s\]$" % (abnf_regexp.IPv6_ADDRZ_RE))
76# Matcher used to validate path components
77PATH_MATCHER = re.compile(abnf_regexp.PATH_RE)
80# ##################################
81# Query and Fragment Matcher Section
82# ##################################
84QUERY_MATCHER = re.compile(abnf_regexp.QUERY_RE)
86FRAGMENT_MATCHER = QUERY_MATCHER
88# Scheme validation, see: http://tools.ietf.org/html/rfc3986#section-3.1
89SCHEME_MATCHER = re.compile(f"^{abnf_regexp.SCHEME_RE}$")
91RELATIVE_REF_MATCHER = re.compile(
92 r"^%s(\?%s)?(#%s)?$"
93 % (
94 abnf_regexp.RELATIVE_PART_RE,
95 abnf_regexp.QUERY_RE,
96 abnf_regexp.FRAGMENT_RE,
97 )
98)
100# See http://tools.ietf.org/html/rfc3986#section-4.3
101ABSOLUTE_URI_MATCHER = re.compile(
102 r"^%s:%s(\?%s)?$"
103 % (
104 abnf_regexp.COMPONENT_PATTERN_DICT["scheme"],
105 abnf_regexp.HIER_PART_RE,
106 abnf_regexp.QUERY_RE[1:-1],
107 )
108)
110# ###############
111# IRIs / RFC 3987
112# ###############
114IRI_MATCHER = re.compile(abnf_regexp.URL_PARSING_RE, re.UNICODE)
116ISUBAUTHORITY_MATCHER = re.compile(
117 (
118 "^(?:(?P<userinfo>{})@)?" # iuserinfo
119 "(?P<host>{})" # ihost
120 ":?(?P<port>{})?$" # port
121 ).format(
122 abnf_regexp.IUSERINFO_RE, abnf_regexp.IHOST_RE, abnf_regexp.PORT_RE
123 ),
124 re.UNICODE,
125)
128# Path merger as defined in http://tools.ietf.org/html/rfc3986#section-5.2.3
129def merge_paths(base_uri: URIReferenceBase, relative_path: str) -> str:
130 """Merge a base URI's path with a relative URI's path."""
131 if base_uri.path is None and base_uri.authority is not None:
132 return "/" + relative_path
133 else:
134 path = base_uri.path or ""
135 index = path.rfind("/")
136 return path[:index] + "/" + relative_path
139UseExisting: t.Final[t.Any] = object()