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"""
21import re
22import typing as t
24from . import abnf_regexp
27class URIReferenceBase(t.NamedTuple):
28 """The namedtuple used as a superclass of URIReference and IRIReference."""
30 scheme: t.Optional[str]
31 authority: t.Optional[str]
32 path: t.Optional[str]
33 query: t.Optional[str]
34 fragment: t.Optional[str]
37important_characters = {
38 "generic_delimiters": abnf_regexp.GENERIC_DELIMITERS,
39 "sub_delimiters": abnf_regexp.SUB_DELIMITERS,
40 # We need to escape the '*' in this case
41 "re_sub_delimiters": abnf_regexp.SUB_DELIMITERS_RE,
42 "unreserved_chars": abnf_regexp.UNRESERVED_CHARS,
43 # We need to escape the '-' in this case:
44 "re_unreserved": abnf_regexp.UNRESERVED_RE,
45}
47# For details about delimiters and reserved characters, see:
48# http://tools.ietf.org/html/rfc3986#section-2.2
49GENERIC_DELIMITERS = abnf_regexp.GENERIC_DELIMITERS_SET
50SUB_DELIMITERS = abnf_regexp.SUB_DELIMITERS_SET
51RESERVED_CHARS = abnf_regexp.RESERVED_CHARS_SET
52# For details about unreserved characters, see:
53# http://tools.ietf.org/html/rfc3986#section-2.3
54UNRESERVED_CHARS = abnf_regexp.UNRESERVED_CHARS_SET
55NON_PCT_ENCODED = abnf_regexp.NON_PCT_ENCODED_SET
57URI_MATCHER = re.compile(abnf_regexp.URL_PARSING_RE)
59SUBAUTHORITY_MATCHER = re.compile(
60 (
61 "^(?:(?P<userinfo>{})@)?" # userinfo
62 "(?P<host>{})" # host
63 "(?::(?P<port>{}))?$" # port
64 ).format(
65 abnf_regexp.USERINFO_RE, abnf_regexp.HOST_PATTERN, abnf_regexp.PORT_RE
66 )
67)
70HOST_MATCHER = re.compile("^" + abnf_regexp.HOST_RE + "$")
71IPv4_MATCHER = re.compile("^" + abnf_regexp.IPv4_RE + "$")
72IPv6_MATCHER = re.compile(r"^\[" + abnf_regexp.IPv6_ADDRZ_RFC4007_RE + r"\]$")
74# Used by host validator
75IPv6_NO_RFC4007_MATCHER = re.compile(r"^\[%s\]$" % (abnf_regexp.IPv6_ADDRZ_RE))
77# Matcher used to validate path components
78PATH_MATCHER = re.compile(abnf_regexp.PATH_RE)
81# ##################################
82# Query and Fragment Matcher Section
83# ##################################
85QUERY_MATCHER = re.compile(abnf_regexp.QUERY_RE)
87FRAGMENT_MATCHER = QUERY_MATCHER
89# Scheme validation, see: http://tools.ietf.org/html/rfc3986#section-3.1
90SCHEME_MATCHER = re.compile(f"^{abnf_regexp.SCHEME_RE}$")
92RELATIVE_REF_MATCHER = re.compile(
93 r"^%s(\?%s)?(#%s)?$"
94 % (
95 abnf_regexp.RELATIVE_PART_RE,
96 abnf_regexp.QUERY_RE,
97 abnf_regexp.FRAGMENT_RE,
98 )
99)
101# See http://tools.ietf.org/html/rfc3986#section-4.3
102ABSOLUTE_URI_MATCHER = re.compile(
103 r"^%s:%s(\?%s)?$"
104 % (
105 abnf_regexp.COMPONENT_PATTERN_DICT["scheme"],
106 abnf_regexp.HIER_PART_RE,
107 abnf_regexp.QUERY_RE[1:-1],
108 )
109)
111# ###############
112# IRIs / RFC 3987
113# ###############
115IRI_MATCHER = re.compile(abnf_regexp.URL_PARSING_RE, re.UNICODE)
117ISUBAUTHORITY_MATCHER = re.compile(
118 (
119 "^(?:(?P<userinfo>{})@)?" # iuserinfo
120 "(?P<host>{})" # ihost
121 ":?(?P<port>{})?$" # port
122 ).format(
123 abnf_regexp.IUSERINFO_RE, abnf_regexp.IHOST_RE, abnf_regexp.PORT_RE
124 ),
125 re.UNICODE,
126)
129# Path merger as defined in http://tools.ietf.org/html/rfc3986#section-5.2.3
130def merge_paths(base_uri: URIReferenceBase, relative_path: str) -> str:
131 """Merge a base URI's path with a relative URI's path."""
132 if base_uri.path is None and base_uri.authority is not None:
133 return "/" + relative_path
134 else:
135 path = base_uri.path or ""
136 index = path.rfind("/")
137 return path[:index] + "/" + relative_path
140UseExisting: t.Final[t.Any] = object()