/src/php-src/ext/uri/uriparser/src/UriResolve.c
Line | Count | Source |
1 | | /* |
2 | | * uriparser - RFC 3986 URI parsing library |
3 | | * |
4 | | * Copyright (C) 2007, Weijia Song <songweijia@gmail.com> |
5 | | * Copyright (C) 2007, Sebastian Pipping <sebastian@pipping.org> |
6 | | * All rights reserved. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions |
10 | | * are met: |
11 | | * |
12 | | * 1. Redistributions of source code must retain the above |
13 | | * copyright notice, this list of conditions and the following |
14 | | * disclaimer. |
15 | | * |
16 | | * 2. Redistributions in binary form must reproduce the above |
17 | | * copyright notice, this list of conditions and the following |
18 | | * disclaimer in the documentation and/or other materials |
19 | | * provided with the distribution. |
20 | | * |
21 | | * 3. Neither the name of the copyright holder nor the names of |
22 | | * its contributors may be used to endorse or promote products |
23 | | * derived from this software without specific prior written |
24 | | * permission. |
25 | | * |
26 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
27 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
28 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
29 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
30 | | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
31 | | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
32 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
33 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
34 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
35 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
36 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
37 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
38 | | */ |
39 | | |
40 | | /* What encodings are enabled? */ |
41 | | #include <uriparser/UriDefsConfig.h> |
42 | | #if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE)) |
43 | | /* Include SELF twice */ |
44 | | # ifdef URI_ENABLE_ANSI |
45 | | # define URI_PASS_ANSI 1 |
46 | | # include "UriResolve.c" |
47 | | # undef URI_PASS_ANSI |
48 | | # endif |
49 | | # ifdef URI_ENABLE_UNICODE |
50 | | # define URI_PASS_UNICODE 1 |
51 | | # include "UriResolve.c" |
52 | | # undef URI_PASS_UNICODE |
53 | | # endif |
54 | | #else |
55 | | # ifdef URI_PASS_ANSI |
56 | | # include <uriparser/UriDefsAnsi.h> |
57 | | # else |
58 | | # include <uriparser/UriDefsUnicode.h> |
59 | | # include <wchar.h> |
60 | | # endif |
61 | | |
62 | | # ifndef URI_DOXYGEN |
63 | | # include <uriparser/Uri.h> |
64 | | # include "UriCommon.h" |
65 | | # include "UriMemory.h" |
66 | | # endif |
67 | | |
68 | | /* Appends a relative URI to an absolute. The last path segment of |
69 | | * the absolute URI is replaced. */ |
70 | | static URI_INLINE UriBool URI_FUNC(MergePath)(URI_TYPE(Uri) * absWork, |
71 | | const URI_TYPE(Uri) * relAppend, |
72 | 0 | UriMemoryManager * memory) { |
73 | 0 | URI_TYPE(PathSegment) * sourceWalker; |
74 | 0 | URI_TYPE(PathSegment) * destPrev; |
75 | 0 | if (relAppend->pathHead == NULL) { |
76 | 0 | return URI_TRUE; |
77 | 0 | } |
78 | | |
79 | | /* Replace last segment ("" if trailing slash) with first of append chain */ |
80 | 0 | if (absWork->pathHead == NULL) { |
81 | 0 | URI_TYPE(PathSegment) * const dup = |
82 | 0 | memory->malloc(memory, sizeof(URI_TYPE(PathSegment))); |
83 | 0 | if (dup == NULL) { |
84 | 0 | return URI_FALSE; /* Raises malloc error */ |
85 | 0 | } |
86 | 0 | dup->next = NULL; |
87 | 0 | absWork->pathHead = dup; |
88 | 0 | absWork->pathTail = dup; |
89 | 0 | } |
90 | 0 | absWork->pathTail->text.first = relAppend->pathHead->text.first; |
91 | 0 | absWork->pathTail->text.afterLast = relAppend->pathHead->text.afterLast; |
92 | | |
93 | | /* Append all the others */ |
94 | 0 | sourceWalker = relAppend->pathHead->next; |
95 | 0 | if (sourceWalker == NULL) { |
96 | 0 | return URI_TRUE; |
97 | 0 | } |
98 | 0 | destPrev = absWork->pathTail; |
99 | |
|
100 | 0 | for (;;) { |
101 | 0 | URI_TYPE(PathSegment) * const dup = |
102 | 0 | memory->malloc(memory, sizeof(URI_TYPE(PathSegment))); |
103 | 0 | if (dup == NULL) { |
104 | 0 | destPrev->next = NULL; |
105 | 0 | absWork->pathTail = destPrev; |
106 | 0 | return URI_FALSE; /* Raises malloc error */ |
107 | 0 | } |
108 | 0 | dup->text = sourceWalker->text; |
109 | 0 | destPrev->next = dup; |
110 | |
|
111 | 0 | if (sourceWalker->next == NULL) { |
112 | 0 | absWork->pathTail = dup; |
113 | 0 | absWork->pathTail->next = NULL; |
114 | 0 | break; |
115 | 0 | } |
116 | 0 | destPrev = dup; |
117 | 0 | sourceWalker = sourceWalker->next; |
118 | 0 | } |
119 | | |
120 | 0 | return URI_TRUE; |
121 | 0 | } Unexecuted instantiation: UriResolve.c:uriMergePathA Unexecuted instantiation: UriResolve.c:uriMergePathW |
122 | | |
123 | | static int URI_FUNC(ResolveAbsolutePathFlag)(URI_TYPE(Uri) * absWork, |
124 | 0 | UriMemoryManager * memory) { |
125 | 0 | if (absWork == NULL) { |
126 | 0 | return URI_ERROR_NULL; |
127 | 0 | } |
128 | | |
129 | 0 | if (URI_FUNC(HasHost)(absWork) && absWork->absolutePath) { |
130 | | /* Empty segment needed, instead? */ |
131 | 0 | if (absWork->pathHead == NULL) { |
132 | 0 | URI_TYPE(PathSegment) * const segment = |
133 | 0 | memory->malloc(memory, sizeof(URI_TYPE(PathSegment))); |
134 | 0 | if (segment == NULL) { |
135 | 0 | return URI_ERROR_MALLOC; |
136 | 0 | } |
137 | 0 | segment->text.first = URI_FUNC(SafeToPointTo); |
138 | 0 | segment->text.afterLast = URI_FUNC(SafeToPointTo); |
139 | 0 | segment->next = NULL; |
140 | |
|
141 | 0 | absWork->pathHead = segment; |
142 | 0 | absWork->pathTail = segment; |
143 | 0 | } |
144 | | |
145 | 0 | absWork->absolutePath = URI_FALSE; |
146 | 0 | } |
147 | | |
148 | 0 | return URI_SUCCESS; |
149 | 0 | } Unexecuted instantiation: UriResolve.c:uriResolveAbsolutePathFlagA Unexecuted instantiation: UriResolve.c:uriResolveAbsolutePathFlagW |
150 | | |
151 | | static int URI_FUNC(AddBaseUriImpl)(URI_TYPE(Uri) * absDest, |
152 | | const URI_TYPE(Uri) * relSource, |
153 | | const URI_TYPE(Uri) * absBase, |
154 | | UriResolutionOptions options, |
155 | 0 | UriMemoryManager * memory) { |
156 | 0 | UriBool relSourceHasScheme; |
157 | |
|
158 | 0 | if (absDest == NULL) { |
159 | 0 | return URI_ERROR_NULL; |
160 | 0 | } |
161 | 0 | URI_FUNC(ResetUri)(absDest); |
162 | |
|
163 | 0 | if ((relSource == NULL) || (absBase == NULL)) { |
164 | 0 | return URI_ERROR_NULL; |
165 | 0 | } |
166 | | |
167 | | /* absBase absolute? */ |
168 | 0 | if (absBase->scheme.first == NULL) { |
169 | 0 | return URI_ERROR_ADDBASE_REL_BASE; |
170 | 0 | } |
171 | | |
172 | | /* NOTE: The curly brackets here force deeper indent (and that's all) */ |
173 | 0 | { |
174 | 0 | { |
175 | 0 | { |
176 | | /* clang-format off */ |
177 | | /* [00/32] -- A non-strict parser may ignore a scheme in the reference */ |
178 | | /* [00/32] -- if it is identical to the base URI's scheme. */ |
179 | | /* [00/32] if ((not strict) and (R.scheme == Base.scheme)) then */ |
180 | | /* clang-format on */ |
181 | 0 | relSourceHasScheme = |
182 | 0 | (relSource->scheme.first != NULL) ? URI_TRUE : URI_FALSE; |
183 | 0 | if ((options & URI_RESOLVE_IDENTICAL_SCHEME_COMPAT) |
184 | 0 | && (absBase->scheme.first != NULL) |
185 | 0 | && (relSource->scheme.first != NULL) |
186 | 0 | && (URI_FUNC(RangeEquals)(&(absBase->scheme), |
187 | 0 | &(relSource->scheme)))) { |
188 | | /* clang-format off */ |
189 | | /* [00/32] undefine(R.scheme); */ |
190 | | /* clang-format on */ |
191 | 0 | relSourceHasScheme = URI_FALSE; |
192 | | /* clang-format off */ |
193 | | /* [00/32] endif; */ |
194 | | /* clang-format on */ |
195 | 0 | } |
196 | | |
197 | | /* clang-format off */ |
198 | | /* [01/32] if defined(R.scheme) then */ |
199 | | /* clang-format on */ |
200 | 0 | if (relSourceHasScheme) { |
201 | | /* clang-format off */ |
202 | | /* [02/32] T.scheme = R.scheme; */ |
203 | | /* clang-format on */ |
204 | 0 | absDest->scheme = relSource->scheme; |
205 | | /* clang-format off */ |
206 | | /* [03/32] T.authority = R.authority; */ |
207 | | /* clang-format on */ |
208 | 0 | if (!URI_FUNC(CopyAuthority)(absDest, relSource, memory)) { |
209 | 0 | return URI_ERROR_MALLOC; |
210 | 0 | } |
211 | | /* clang-format off */ |
212 | | /* [04/32] T.path = remove_dot_segments(R.path); */ |
213 | | /* clang-format on */ |
214 | 0 | if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) { |
215 | 0 | return URI_ERROR_MALLOC; |
216 | 0 | } |
217 | 0 | if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, memory)) { |
218 | 0 | return URI_ERROR_MALLOC; |
219 | 0 | } |
220 | | /* clang-format off */ |
221 | | /* [05/32] T.query = R.query; */ |
222 | | /* clang-format on */ |
223 | 0 | absDest->query = relSource->query; |
224 | | /* clang-format off */ |
225 | | /* [06/32] else */ |
226 | | /* clang-format on */ |
227 | 0 | } else { |
228 | | /* clang-format off */ |
229 | | /* [07/32] if defined(R.authority) then */ |
230 | | /* clang-format on */ |
231 | 0 | if (URI_FUNC(HasHost)(relSource)) { |
232 | | /* clang-format off */ |
233 | | /* [08/32] T.authority = R.authority; */ |
234 | | /* clang-format on */ |
235 | 0 | if (!URI_FUNC(CopyAuthority)(absDest, relSource, memory)) { |
236 | 0 | return URI_ERROR_MALLOC; |
237 | 0 | } |
238 | | /* clang-format off */ |
239 | | /* [09/32] T.path = remove_dot_segments(R.path); */ |
240 | | /* clang-format on */ |
241 | 0 | if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) { |
242 | 0 | return URI_ERROR_MALLOC; |
243 | 0 | } |
244 | 0 | if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, memory)) { |
245 | 0 | return URI_ERROR_MALLOC; |
246 | 0 | } |
247 | | /* clang-format off */ |
248 | | /* [10/32] T.query = R.query; */ |
249 | | /* clang-format on */ |
250 | 0 | absDest->query = relSource->query; |
251 | | /* clang-format off */ |
252 | | /* [11/32] else */ |
253 | | /* clang-format on */ |
254 | 0 | } else { |
255 | | /* clang-format off */ |
256 | | /* [28/32] T.authority = Base.authority; */ |
257 | | /* clang-format on */ |
258 | 0 | if (!URI_FUNC(CopyAuthority)(absDest, absBase, memory)) { |
259 | 0 | return URI_ERROR_MALLOC; |
260 | 0 | } |
261 | | /* clang-format off */ |
262 | | /* [12/32] if (R.path == "") then */ |
263 | | /* clang-format on */ |
264 | 0 | if (relSource->pathHead == NULL && !relSource->absolutePath) { |
265 | | /* clang-format off */ |
266 | | /* [13/32] T.path = Base.path; */ |
267 | | /* clang-format on */ |
268 | 0 | if (!URI_FUNC(CopyPath)(absDest, absBase, memory)) { |
269 | 0 | return URI_ERROR_MALLOC; |
270 | 0 | } |
271 | | /* clang-format off */ |
272 | | /* [14/32] if defined(R.query) then */ |
273 | | /* clang-format on */ |
274 | 0 | if (relSource->query.first != NULL) { |
275 | | /* clang-format off */ |
276 | | /* [15/32] T.query = R.query; */ |
277 | | /* clang-format on */ |
278 | 0 | absDest->query = relSource->query; |
279 | | /* clang-format off */ |
280 | | /* [16/32] else */ |
281 | | /* clang-format on */ |
282 | 0 | } else { |
283 | | /* clang-format off */ |
284 | | /* [17/32] T.query = Base.query; */ |
285 | | /* clang-format on */ |
286 | 0 | absDest->query = absBase->query; |
287 | | /* clang-format off */ |
288 | | /* [18/32] endif; */ |
289 | | /* clang-format on */ |
290 | 0 | } |
291 | | /* clang-format off */ |
292 | | /* [19/32] else */ |
293 | | /* clang-format on */ |
294 | 0 | } else { |
295 | | /* clang-format off */ |
296 | | /* [20/32] if (R.path starts-with "/") then */ |
297 | | /* clang-format on */ |
298 | 0 | if (relSource->absolutePath) { |
299 | 0 | int res; |
300 | | /* clang-format off */ |
301 | | /* [21/32] T.path = remove_dot_segments(R.path); */ |
302 | | /* clang-format on */ |
303 | 0 | if (!URI_FUNC(CopyPath)(absDest, relSource, memory)) { |
304 | 0 | return URI_ERROR_MALLOC; |
305 | 0 | } |
306 | 0 | res = URI_FUNC(ResolveAbsolutePathFlag)(absDest, memory); |
307 | 0 | if (res != URI_SUCCESS) { |
308 | 0 | return res; |
309 | 0 | } |
310 | 0 | if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, |
311 | 0 | memory)) { |
312 | 0 | return URI_ERROR_MALLOC; |
313 | 0 | } |
314 | | /* clang-format off */ |
315 | | /* [22/32] else */ |
316 | | /* clang-format on */ |
317 | 0 | } else { |
318 | | /* clang-format off */ |
319 | | /* [23/32] T.path = merge(Base.path, R.path); */ |
320 | | /* clang-format on */ |
321 | 0 | if (!URI_FUNC(CopyPath)(absDest, absBase, memory)) { |
322 | 0 | return URI_ERROR_MALLOC; |
323 | 0 | } |
324 | 0 | if (!URI_FUNC(MergePath)(absDest, relSource, memory)) { |
325 | 0 | return URI_ERROR_MALLOC; |
326 | 0 | } |
327 | | /* clang-format off */ |
328 | | /* [24/32] T.path = remove_dot_segments(T.path); */ |
329 | | /* clang-format on */ |
330 | 0 | if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest, |
331 | 0 | memory)) { |
332 | 0 | return URI_ERROR_MALLOC; |
333 | 0 | } |
334 | | |
335 | 0 | if (!URI_FUNC(FixAmbiguity)(absDest, memory)) { |
336 | 0 | return URI_ERROR_MALLOC; |
337 | 0 | } |
338 | | /* clang-format off */ |
339 | | /* [25/32] endif; */ |
340 | 0 | } |
341 | | /* clang-format off */ |
342 | | /* [26/32] T.query = R.query; */ |
343 | | /* clang-format on */ |
344 | 0 | absDest->query = relSource->query; |
345 | | /* clang-format off */ |
346 | | /* [27/32] endif; */ |
347 | | /* clang-format on */ |
348 | 0 | } |
349 | 0 | URI_FUNC(FixEmptyTrailSegment)(absDest, memory); |
350 | | /* clang-format off */ |
351 | | /* [29/32] endif; */ |
352 | | /* clang-format on */ |
353 | 0 | } |
354 | | /* clang-format off */ |
355 | | /* [30/32] T.scheme = Base.scheme; */ |
356 | | /* clang-format on */ |
357 | 0 | absDest->scheme = absBase->scheme; |
358 | | /* clang-format off */ |
359 | | /* [31/32] endif; */ |
360 | | /* clang-format on */ |
361 | 0 | } |
362 | | /* clang-format off */ |
363 | | /* [32/32] T.fragment = R.fragment; */ |
364 | | /* clang-format on */ |
365 | 0 | absDest->fragment = relSource->fragment; |
366 | 0 | } |
367 | 0 | } |
368 | 0 | } |
369 | 0 | return URI_SUCCESS; |
370 | 0 | } Unexecuted instantiation: UriResolve.c:uriAddBaseUriImplA Unexecuted instantiation: UriResolve.c:uriAddBaseUriImplW |
371 | | |
372 | | int URI_FUNC(AddBaseUri)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource, |
373 | 0 | const URI_TYPE(Uri) * absBase) { |
374 | 0 | const UriResolutionOptions options = URI_RESOLVE_STRICTLY; |
375 | 0 | return URI_FUNC(AddBaseUriEx)(absDest, relSource, absBase, options); |
376 | 0 | } Unexecuted instantiation: uriAddBaseUriA Unexecuted instantiation: uriAddBaseUriW |
377 | | |
378 | | int URI_FUNC(AddBaseUriEx)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource, |
379 | 0 | const URI_TYPE(Uri) * absBase, UriResolutionOptions options) { |
380 | 0 | return URI_FUNC(AddBaseUriExMm)(absDest, relSource, absBase, options, NULL); |
381 | 0 | } Unexecuted instantiation: uriAddBaseUriExA Unexecuted instantiation: uriAddBaseUriExW |
382 | | |
383 | | int URI_FUNC(AddBaseUriExMm)(URI_TYPE(Uri) * absDest, const URI_TYPE(Uri) * relSource, |
384 | | const URI_TYPE(Uri) * absBase, UriResolutionOptions options, |
385 | 0 | UriMemoryManager * memory) { |
386 | 0 | int res; |
387 | |
|
388 | 0 | URI_CHECK_MEMORY_MANAGER(memory); /* may return */ |
389 | | |
390 | 0 | res = URI_FUNC(AddBaseUriImpl)(absDest, relSource, absBase, options, memory); |
391 | 0 | if ((res != URI_SUCCESS) && (absDest != NULL)) { |
392 | 0 | URI_FUNC(FreeUriMembersMm)(absDest, memory); |
393 | 0 | } |
394 | 0 | return res; |
395 | 0 | } Unexecuted instantiation: uriAddBaseUriExMmA Unexecuted instantiation: uriAddBaseUriExMmW |
396 | | |
397 | | #endif |