/src/php-src/ext/uri/uriparser/src/UriSetHostCommon.c
Line | Count | Source |
1 | | /* |
2 | | * uriparser - RFC 3986 URI parsing library |
3 | | * |
4 | | * Copyright (C) 2025, Sebastian Pipping <sebastian@pipping.org> |
5 | | * All rights reserved. |
6 | | * |
7 | | * Redistribution and use in source and binary forms, with or without |
8 | | * modification, are permitted provided that the following conditions |
9 | | * are met: |
10 | | * |
11 | | * 1. Redistributions of source code must retain the above |
12 | | * copyright notice, this list of conditions and the following |
13 | | * disclaimer. |
14 | | * |
15 | | * 2. Redistributions in binary form must reproduce the above |
16 | | * copyright notice, this list of conditions and the following |
17 | | * disclaimer in the documentation and/or other materials |
18 | | * provided with the distribution. |
19 | | * |
20 | | * 3. Neither the name of the copyright holder nor the names of |
21 | | * its contributors may be used to endorse or promote products |
22 | | * derived from this software without specific prior written |
23 | | * permission. |
24 | | * |
25 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
26 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
27 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
28 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
29 | | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
30 | | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
31 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
32 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
33 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
34 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
35 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
36 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
37 | | */ |
38 | | |
39 | | /** |
40 | | * @file UriSetHostCommon.c |
41 | | * Holds code used by multiple SetHost* functions. |
42 | | * NOTE: This source file includes itself twice. |
43 | | */ |
44 | | |
45 | | /* What encodings are enabled? */ |
46 | | #include <uriparser/UriDefsConfig.h> |
47 | | #if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE)) |
48 | | /* Include SELF twice */ |
49 | | # ifdef URI_ENABLE_ANSI |
50 | | # define URI_PASS_ANSI 1 |
51 | | # include "UriSetHostCommon.c" |
52 | | # undef URI_PASS_ANSI |
53 | | # endif |
54 | | # ifdef URI_ENABLE_UNICODE |
55 | | # define URI_PASS_UNICODE 1 |
56 | | # include "UriSetHostCommon.c" |
57 | | # undef URI_PASS_UNICODE |
58 | | # endif |
59 | | #else |
60 | | # ifdef URI_PASS_ANSI |
61 | | # include <uriparser/UriDefsAnsi.h> |
62 | | # else |
63 | | # include <uriparser/UriDefsUnicode.h> |
64 | | # include <wchar.h> |
65 | | # endif |
66 | | |
67 | | # ifndef URI_DOXYGEN |
68 | | # include <uriparser/Uri.h> |
69 | | # include <uriparser/UriIp4.h> |
70 | | # include "UriCommon.h" |
71 | | # include "UriMemory.h" |
72 | | # include "UriSetHostBase.h" |
73 | | # include "UriSetHostCommon.h" |
74 | | # endif |
75 | | |
76 | | # include <assert.h> |
77 | | |
78 | | int URI_FUNC(InternalSetHostMm)(URI_TYPE(Uri) * uri, UriHostType hostType, |
79 | | const URI_CHAR * first, const URI_CHAR * afterLast, |
80 | 0 | UriMemoryManager * memory) { |
81 | | /* Superficial input validation (before making any changes) */ |
82 | 0 | if ((uri == NULL) || ((first == NULL) != (afterLast == NULL))) { |
83 | 0 | return URI_ERROR_NULL; |
84 | 0 | } |
85 | | |
86 | 0 | URI_CHECK_MEMORY_MANAGER(memory); /* may return */ |
87 | | |
88 | | /* The RFC 3986 grammar reads: |
89 | | * authority = [ userinfo "@" ] host [ ":" port ] |
90 | | * So no user info or port without a host. */ |
91 | 0 | if (first == NULL) { |
92 | 0 | if (uri->userInfo.first != NULL) { |
93 | 0 | return URI_ERROR_SETHOST_USERINFO_SET; |
94 | 0 | } else if (uri->portText.first != NULL) { |
95 | 0 | return URI_ERROR_SETHOST_PORT_SET; |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | | /* Syntax-check the new value */ |
100 | 0 | if (first != NULL) { |
101 | 0 | switch (hostType) { |
102 | 0 | case URI_HOST_TYPE_IP4: |
103 | 0 | if (URI_FUNC(IsWellFormedHostIp4)(first, afterLast) == URI_FALSE) { |
104 | 0 | return URI_ERROR_SYNTAX; |
105 | 0 | } |
106 | 0 | break; |
107 | 0 | case URI_HOST_TYPE_IP6: { |
108 | 0 | const int res = URI_FUNC(IsWellFormedHostIp6Mm)(first, afterLast, memory); |
109 | 0 | assert((res == URI_SUCCESS) || (res == URI_ERROR_SYNTAX) |
110 | 0 | || (res == URI_ERROR_MALLOC)); |
111 | 0 | if (res != URI_SUCCESS) { |
112 | 0 | return res; |
113 | 0 | } |
114 | 0 | } break; |
115 | 0 | case URI_HOST_TYPE_IPFUTURE: { |
116 | 0 | const int res = |
117 | 0 | URI_FUNC(IsWellFormedHostIpFutureMm)(first, afterLast, memory); |
118 | 0 | assert((res == URI_SUCCESS) || (res == URI_ERROR_SYNTAX) |
119 | 0 | || (res == URI_ERROR_MALLOC)); |
120 | 0 | if (res != URI_SUCCESS) { |
121 | 0 | return res; |
122 | 0 | } |
123 | 0 | } break; |
124 | 0 | case URI_HOST_TYPE_REGNAME: |
125 | 0 | if (URI_FUNC(IsWellFormedHostRegName)(first, afterLast) == URI_FALSE) { |
126 | 0 | return URI_ERROR_SYNTAX; |
127 | 0 | } |
128 | 0 | break; |
129 | 0 | default: |
130 | 0 | assert(0 && "Unsupported URI host type"); |
131 | 0 | } |
132 | 0 | } |
133 | | |
134 | | /* Clear old value */ |
135 | 0 | const UriBool hadHostBefore = URI_FUNC(HasHost)(uri); |
136 | 0 | if (uri->hostData.ipFuture.first != NULL) { |
137 | | /* NOTE: .hostData.ipFuture holds the very same range pointers |
138 | | * as .hostText; we must not free memory twice. */ |
139 | 0 | uri->hostText.first = NULL; |
140 | 0 | uri->hostText.afterLast = NULL; |
141 | |
|
142 | 0 | if ((uri->owner == URI_TRUE) |
143 | 0 | && (uri->hostData.ipFuture.first != uri->hostData.ipFuture.afterLast)) { |
144 | 0 | memory->free(memory, (URI_CHAR *)uri->hostData.ipFuture.first); |
145 | 0 | } |
146 | 0 | uri->hostData.ipFuture.first = NULL; |
147 | 0 | uri->hostData.ipFuture.afterLast = NULL; |
148 | 0 | } else if (uri->hostText.first != NULL) { |
149 | 0 | if ((uri->owner == URI_TRUE) |
150 | 0 | && (uri->hostText.first != uri->hostText.afterLast)) { |
151 | 0 | memory->free(memory, (URI_CHAR *)uri->hostText.first); |
152 | 0 | } |
153 | 0 | uri->hostText.first = NULL; |
154 | 0 | uri->hostText.afterLast = NULL; |
155 | 0 | } |
156 | |
|
157 | 0 | if (uri->hostData.ip4 != NULL) { |
158 | 0 | memory->free(memory, uri->hostData.ip4); |
159 | 0 | uri->hostData.ip4 = NULL; |
160 | 0 | } else if (uri->hostData.ip6 != NULL) { |
161 | 0 | memory->free(memory, uri->hostData.ip6); |
162 | 0 | uri->hostData.ip6 = NULL; |
163 | 0 | } |
164 | | |
165 | | /* Already done setting? */ |
166 | 0 | if (first == NULL) { |
167 | | /* Yes, but disambiguate as needed */ |
168 | 0 | if (hadHostBefore == URI_TRUE) { |
169 | 0 | if (uri->pathHead != NULL) { |
170 | 0 | uri->absolutePath = URI_TRUE; |
171 | 0 | } |
172 | |
|
173 | 0 | const UriBool success = |
174 | 0 | URI_FUNC(EnsureThatPathIsNotMistakenForHost)(uri, memory); |
175 | 0 | return (success == URI_TRUE) ? URI_SUCCESS : URI_ERROR_MALLOC; |
176 | 0 | } |
177 | | |
178 | 0 | return URI_SUCCESS; |
179 | 0 | } |
180 | | |
181 | 0 | assert(first != NULL); |
182 | | |
183 | | /* Ensure owned */ |
184 | 0 | if (uri->owner == URI_FALSE) { |
185 | 0 | const int res = URI_FUNC(MakeOwnerMm)(uri, memory); |
186 | 0 | if (res != URI_SUCCESS) { |
187 | 0 | return res; |
188 | 0 | } |
189 | 0 | } |
190 | | |
191 | 0 | assert(uri->owner == URI_TRUE); |
192 | | |
193 | | /* Apply new value; NOTE that .hostText is set for all four host types */ |
194 | 0 | URI_TYPE(TextRange) sourceRange; |
195 | 0 | sourceRange.first = first; |
196 | 0 | sourceRange.afterLast = afterLast; |
197 | |
|
198 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&uri->hostText, &sourceRange, memory) == URI_FALSE) { |
199 | 0 | return URI_ERROR_MALLOC; |
200 | 0 | } |
201 | | |
202 | 0 | uri->absolutePath = URI_FALSE; /* always URI_FALSE for URIs with host */ |
203 | | |
204 | | /* Fill .hostData as needed */ |
205 | 0 | switch (hostType) { |
206 | 0 | case URI_HOST_TYPE_IP4: { |
207 | 0 | uri->hostData.ip4 = memory->malloc(memory, sizeof(UriIp4)); |
208 | 0 | if (uri->hostData.ip4 == NULL) { |
209 | 0 | return URI_ERROR_MALLOC; |
210 | 0 | } |
211 | | |
212 | 0 | const int res = |
213 | 0 | URI_FUNC(ParseIpFourAddress)(uri->hostData.ip4->data, first, afterLast); |
214 | | # if defined(NDEBUG) |
215 | | (void)res; /* i.e. mark as unused */ |
216 | | # else |
217 | 0 | assert(res == URI_SUCCESS); /* because checked for well-formedness earlier */ |
218 | 0 | # endif |
219 | 0 | } break; |
220 | 0 | case URI_HOST_TYPE_IP6: { |
221 | 0 | uri->hostData.ip6 = memory->malloc(memory, sizeof(UriIp6)); |
222 | 0 | if (uri->hostData.ip6 == NULL) { |
223 | 0 | return URI_ERROR_MALLOC; |
224 | 0 | } |
225 | | |
226 | 0 | const int res = |
227 | 0 | URI_FUNC(ParseIpSixAddressMm)(uri->hostData.ip6, first, afterLast, memory); |
228 | 0 | assert((res == URI_SUCCESS) |
229 | 0 | || (res == URI_ERROR_MALLOC)); /* because checked for |
230 | | well-formedness earlier */ |
231 | 0 | if (res != URI_SUCCESS) { |
232 | 0 | return res; |
233 | 0 | } |
234 | 0 | } break; |
235 | 0 | case URI_HOST_TYPE_IPFUTURE: |
236 | 0 | uri->hostData.ipFuture.first = uri->hostText.first; |
237 | 0 | uri->hostData.ipFuture.afterLast = uri->hostText.afterLast; |
238 | 0 | break; |
239 | 0 | case URI_HOST_TYPE_REGNAME: |
240 | 0 | break; |
241 | 0 | default: |
242 | 0 | assert(0 && "Unsupported URI host type"); |
243 | 0 | } |
244 | | |
245 | 0 | return URI_SUCCESS; |
246 | 0 | } Unexecuted instantiation: uriInternalSetHostMmA Unexecuted instantiation: uriInternalSetHostMmW |
247 | | |
248 | | #endif |