comparison uriparser/src/UriResolve.c @ 60:a2be1eba7adb

uriparser: import 0.8.5, close #878 @10m
author David Demelier <markand@malikania.fr>
date Fri, 13 Jul 2018 10:50:43 +0200
parents
children
comparison
equal deleted inserted replaced
59:e5880f2798a1 60:a2be1eba7adb
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 * * Redistributions of source code must retain the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer.
15 *
16 * * 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 * * Neither the name of the <ORGANIZATION> nor the names of its
22 * 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 THE
30 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * 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
63
64 #ifndef URI_DOXYGEN
65 # include <uriparser/Uri.h>
66 # include "UriCommon.h"
67 #endif
68
69
70
71 /* Appends a relative URI to an absolute. The last path segment of
72 * the absolute URI is replaced. */
73 static URI_INLINE UriBool URI_FUNC(MergePath)(URI_TYPE(Uri) * absWork,
74 const URI_TYPE(Uri) * relAppend) {
75 URI_TYPE(PathSegment) * sourceWalker;
76 URI_TYPE(PathSegment) * destPrev;
77 if (relAppend->pathHead == NULL) {
78 return URI_TRUE;
79 }
80
81 /* Replace last segment ("" if trailing slash) with first of append chain */
82 if (absWork->pathHead == NULL) {
83 URI_TYPE(PathSegment) * const dup = malloc(sizeof(URI_TYPE(PathSegment)));
84 if (dup == NULL) {
85 return URI_FALSE; /* Raises malloc error */
86 }
87 dup->next = NULL;
88 absWork->pathHead = dup;
89 absWork->pathTail = dup;
90 }
91 absWork->pathTail->text.first = relAppend->pathHead->text.first;
92 absWork->pathTail->text.afterLast = relAppend->pathHead->text.afterLast;
93
94 /* Append all the others */
95 sourceWalker = relAppend->pathHead->next;
96 if (sourceWalker == NULL) {
97 return URI_TRUE;
98 }
99 destPrev = absWork->pathTail;
100
101 for (;;) {
102 URI_TYPE(PathSegment) * const dup = malloc(sizeof(URI_TYPE(PathSegment)));
103 if (dup == NULL) {
104 destPrev->next = NULL;
105 absWork->pathTail = destPrev;
106 return URI_FALSE; /* Raises malloc error */
107 }
108 dup->text = sourceWalker->text;
109 destPrev->next = dup;
110
111 if (sourceWalker->next == NULL) {
112 absWork->pathTail = dup;
113 absWork->pathTail->next = NULL;
114 break;
115 }
116 destPrev = dup;
117 sourceWalker = sourceWalker->next;
118 }
119
120 return URI_TRUE;
121 }
122
123
124 static int URI_FUNC(ResolveAbsolutePathFlag)(URI_TYPE(Uri) * absWork) {
125 if (absWork == NULL) {
126 return URI_ERROR_NULL;
127 }
128
129 if (URI_FUNC(IsHostSet)(absWork) && absWork->absolutePath) {
130 /* Empty segment needed, instead? */
131 if (absWork->pathHead == NULL) {
132 URI_TYPE(PathSegment) * const segment = malloc(sizeof(URI_TYPE(PathSegment)));
133 if (segment == NULL) {
134 return URI_ERROR_MALLOC;
135 }
136 segment->text.first = URI_FUNC(SafeToPointTo);
137 segment->text.afterLast = URI_FUNC(SafeToPointTo);
138 segment->next = NULL;
139
140 absWork->pathHead = segment;
141 absWork->pathTail = segment;
142 }
143
144 absWork->absolutePath = URI_FALSE;
145 }
146
147 return URI_SUCCESS;
148 }
149
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 UriBool relSourceHasScheme;
156
157 if (absDest == NULL) {
158 return URI_ERROR_NULL;
159 }
160 URI_FUNC(ResetUri)(absDest);
161
162 if ((relSource == NULL) || (absBase == NULL)) {
163 return URI_ERROR_NULL;
164 }
165
166 /* absBase absolute? */
167 if (absBase->scheme.first == NULL) {
168 return URI_ERROR_ADDBASE_REL_BASE;
169 }
170
171 /* [00/32] -- A non-strict parser may ignore a scheme in the reference */
172 /* [00/32] -- if it is identical to the base URI's scheme. */
173 /* [00/32] if ((not strict) and (R.scheme == Base.scheme)) then */
174 relSourceHasScheme = (relSource->scheme.first != NULL) ? URI_TRUE : URI_FALSE;
175 if ((options & URI_RESOLVE_IDENTICAL_SCHEME_COMPAT)
176 && (absBase->scheme.first != NULL)
177 && (relSource->scheme.first != NULL)
178 && (0 == URI_FUNC(CompareRange)(&(absBase->scheme), &(relSource->scheme)))) {
179 /* [00/32] undefine(R.scheme); */
180 relSourceHasScheme = URI_FALSE;
181 /* [00/32] endif; */
182 }
183
184 /* [01/32] if defined(R.scheme) then */
185 if (relSourceHasScheme) {
186 /* [02/32] T.scheme = R.scheme; */
187 absDest->scheme = relSource->scheme;
188 /* [03/32] T.authority = R.authority; */
189 if (!URI_FUNC(CopyAuthority)(absDest, relSource)) {
190 return URI_ERROR_MALLOC;
191 }
192 /* [04/32] T.path = remove_dot_segments(R.path); */
193 if (!URI_FUNC(CopyPath)(absDest, relSource)) {
194 return URI_ERROR_MALLOC;
195 }
196 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest)) {
197 return URI_ERROR_MALLOC;
198 }
199 /* [05/32] T.query = R.query; */
200 absDest->query = relSource->query;
201 /* [06/32] else */
202 } else {
203 /* [07/32] if defined(R.authority) then */
204 if (URI_FUNC(IsHostSet)(relSource)) {
205 /* [08/32] T.authority = R.authority; */
206 if (!URI_FUNC(CopyAuthority)(absDest, relSource)) {
207 return URI_ERROR_MALLOC;
208 }
209 /* [09/32] T.path = remove_dot_segments(R.path); */
210 if (!URI_FUNC(CopyPath)(absDest, relSource)) {
211 return URI_ERROR_MALLOC;
212 }
213 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest)) {
214 return URI_ERROR_MALLOC;
215 }
216 /* [10/32] T.query = R.query; */
217 absDest->query = relSource->query;
218 /* [11/32] else */
219 } else {
220 /* [28/32] T.authority = Base.authority; */
221 if (!URI_FUNC(CopyAuthority)(absDest, absBase)) {
222 return URI_ERROR_MALLOC;
223 }
224 /* [12/32] if (R.path == "") then */
225 if (relSource->pathHead == NULL && !relSource->absolutePath) {
226 /* [13/32] T.path = Base.path; */
227 if (!URI_FUNC(CopyPath)(absDest, absBase)) {
228 return URI_ERROR_MALLOC;
229 }
230 /* [14/32] if defined(R.query) then */
231 if (relSource->query.first != NULL) {
232 /* [15/32] T.query = R.query; */
233 absDest->query = relSource->query;
234 /* [16/32] else */
235 } else {
236 /* [17/32] T.query = Base.query; */
237 absDest->query = absBase->query;
238 /* [18/32] endif; */
239 }
240 /* [19/32] else */
241 } else {
242 /* [20/32] if (R.path starts-with "/") then */
243 if (relSource->absolutePath) {
244 int res;
245 /* [21/32] T.path = remove_dot_segments(R.path); */
246 if (!URI_FUNC(CopyPath)(absDest, relSource)) {
247 return URI_ERROR_MALLOC;
248 }
249 res = URI_FUNC(ResolveAbsolutePathFlag)(absDest);
250 if (res != URI_SUCCESS) {
251 return res;
252 }
253 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest)) {
254 return URI_ERROR_MALLOC;
255 }
256 /* [22/32] else */
257 } else {
258 /* [23/32] T.path = merge(Base.path, R.path); */
259 if (!URI_FUNC(CopyPath)(absDest, absBase)) {
260 return URI_ERROR_MALLOC;
261 }
262 if (!URI_FUNC(MergePath)(absDest, relSource)) {
263 return URI_ERROR_MALLOC;
264 }
265 /* [24/32] T.path = remove_dot_segments(T.path); */
266 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest)) {
267 return URI_ERROR_MALLOC;
268 }
269
270 if (!URI_FUNC(FixAmbiguity)(absDest)) {
271 return URI_ERROR_MALLOC;
272 }
273 /* [25/32] endif; */
274 }
275 /* [26/32] T.query = R.query; */
276 absDest->query = relSource->query;
277 /* [27/32] endif; */
278 }
279 URI_FUNC(FixEmptyTrailSegment)(absDest);
280 /* [29/32] endif; */
281 }
282 /* [30/32] T.scheme = Base.scheme; */
283 absDest->scheme = absBase->scheme;
284 /* [31/32] endif; */
285 }
286 /* [32/32] T.fragment = R.fragment; */
287 absDest->fragment = relSource->fragment;
288
289 return URI_SUCCESS;
290
291 }
292
293
294
295 int URI_FUNC(AddBaseUri)(URI_TYPE(Uri) * absDest,
296 const URI_TYPE(Uri) * relSource, const URI_TYPE(Uri) * absBase) {
297 const int res = URI_FUNC(AddBaseUriImpl)(absDest, relSource, absBase, URI_RESOLVE_STRICTLY);
298 if ((res != URI_SUCCESS) && (absDest != NULL)) {
299 URI_FUNC(FreeUriMembers)(absDest);
300 }
301 return res;
302 }
303
304
305
306 int URI_FUNC(AddBaseUriEx)(URI_TYPE(Uri) * absDest,
307 const URI_TYPE(Uri) * relSource, const URI_TYPE(Uri) * absBase,
308 UriResolutionOptions options) {
309 const int res = URI_FUNC(AddBaseUriImpl)(absDest, relSource, absBase, options);
310 if ((res != URI_SUCCESS) && (absDest != NULL)) {
311 URI_FUNC(FreeUriMembers)(absDest);
312 }
313 return res;
314 }
315
316
317
318 #endif