Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/rfc3986/misc.py: 84%
31 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:04 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:04 +0000
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
22from . import abnf_regexp
24# These are enumerated for the named tuple used as a superclass of
25# URIReference
26URI_COMPONENTS = ["scheme", "authority", "path", "query", "fragment"]
28important_characters = {
29 "generic_delimiters": abnf_regexp.GENERIC_DELIMITERS,
30 "sub_delimiters": abnf_regexp.SUB_DELIMITERS,
31 # We need to escape the '*' in this case
32 "re_sub_delimiters": abnf_regexp.SUB_DELIMITERS_RE,
33 "unreserved_chars": abnf_regexp.UNRESERVED_CHARS,
34 # We need to escape the '-' in this case:
35 "re_unreserved": abnf_regexp.UNRESERVED_RE,
36}
38# For details about delimiters and reserved characters, see:
39# http://tools.ietf.org/html/rfc3986#section-2.2
40GENERIC_DELIMITERS = abnf_regexp.GENERIC_DELIMITERS_SET
41SUB_DELIMITERS = abnf_regexp.SUB_DELIMITERS_SET
42RESERVED_CHARS = abnf_regexp.RESERVED_CHARS_SET
43# For details about unreserved characters, see:
44# http://tools.ietf.org/html/rfc3986#section-2.3
45UNRESERVED_CHARS = abnf_regexp.UNRESERVED_CHARS_SET
46NON_PCT_ENCODED = abnf_regexp.NON_PCT_ENCODED_SET
48URI_MATCHER = re.compile(abnf_regexp.URL_PARSING_RE)
50SUBAUTHORITY_MATCHER = re.compile(
51 (
52 "^(?:(?P<userinfo>{})@)?" # userinfo
53 "(?P<host>{})" # host
54 "(?::(?P<port>{}))?$" # port
55 ).format(
56 abnf_regexp.USERINFO_RE, abnf_regexp.HOST_PATTERN, abnf_regexp.PORT_RE
57 )
58)
61HOST_MATCHER = re.compile("^" + abnf_regexp.HOST_RE + "$")
62IPv4_MATCHER = re.compile("^" + abnf_regexp.IPv4_RE + "$")
63IPv6_MATCHER = re.compile(r"^\[" + abnf_regexp.IPv6_ADDRZ_RFC4007_RE + r"\]$")
65# Used by host validator
66IPv6_NO_RFC4007_MATCHER = re.compile(r"^\[%s\]$" % (abnf_regexp.IPv6_ADDRZ_RE))
68# Matcher used to validate path components
69PATH_MATCHER = re.compile(abnf_regexp.PATH_RE)
72# ##################################
73# Query and Fragment Matcher Section
74# ##################################
76QUERY_MATCHER = re.compile(abnf_regexp.QUERY_RE)
78FRAGMENT_MATCHER = QUERY_MATCHER
80# Scheme validation, see: http://tools.ietf.org/html/rfc3986#section-3.1
81SCHEME_MATCHER = re.compile(f"^{abnf_regexp.SCHEME_RE}$")
83RELATIVE_REF_MATCHER = re.compile(
84 r"^%s(\?%s)?(#%s)?$"
85 % (
86 abnf_regexp.RELATIVE_PART_RE,
87 abnf_regexp.QUERY_RE,
88 abnf_regexp.FRAGMENT_RE,
89 )
90)
92# See http://tools.ietf.org/html/rfc3986#section-4.3
93ABSOLUTE_URI_MATCHER = re.compile(
94 r"^%s:%s(\?%s)?$"
95 % (
96 abnf_regexp.COMPONENT_PATTERN_DICT["scheme"],
97 abnf_regexp.HIER_PART_RE,
98 abnf_regexp.QUERY_RE[1:-1],
99 )
100)
102# ###############
103# IRIs / RFC 3987
104# ###############
106IRI_MATCHER = re.compile(abnf_regexp.URL_PARSING_RE, re.UNICODE)
108ISUBAUTHORITY_MATCHER = re.compile(
109 (
110 "^(?:(?P<userinfo>{})@)?" # iuserinfo
111 "(?P<host>{})" # ihost
112 ":?(?P<port>{})?$" # port
113 ).format(
114 abnf_regexp.IUSERINFO_RE, abnf_regexp.IHOST_RE, abnf_regexp.PORT_RE
115 ),
116 re.UNICODE,
117)
120# Path merger as defined in http://tools.ietf.org/html/rfc3986#section-5.2.3
121def merge_paths(base_uri, relative_path):
122 """Merge a base URI's path with a relative URI's path."""
123 if base_uri.path is None and base_uri.authority is not None:
124 return "/" + relative_path
125 else:
126 path = base_uri.path or ""
127 index = path.rfind("/")
128 return path[:index] + "/" + relative_path
131UseExisting = object()