summaryrefslogtreecommitdiffstats
path: root/plugins/uriparser/UriResolve.c
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2017-05-06 14:36:16 (GMT)
committerSven Nierlein <sven@nierlein.de>2018-10-22 14:30:31 (GMT)
commita739a5614834178ba88d04972b4f4ba9c560e1df (patch)
treee9ebee27575d064000e9e210dba2b36c914e8544 /plugins/uriparser/UriResolve.c
parent28a4c7076a3115d30aeb3b3b5e5bde715692e4a7 (diff)
downloadmonitoring-plugins-a739a5614834178ba88d04972b4f4ba9c560e1df.tar.gz
some rework:
- added old style 'redir' function and options along to a new libcurl internal 'follow' parameter 'curl' - moved picohttpparser to it's own subdirectory - added uriparser to be used instead of the home-grown parser in 'redir'
Diffstat (limited to 'plugins/uriparser/UriResolve.c')
-rw-r--r--plugins/uriparser/UriResolve.c316
1 files changed, 316 insertions, 0 deletions
diff --git a/plugins/uriparser/UriResolve.c b/plugins/uriparser/UriResolve.c
new file mode 100644
index 0000000..3660b6b
--- /dev/null
+++ b/plugins/uriparser/UriResolve.c
@@ -0,0 +1,316 @@
1/*
2 * uriparser - RFC 3986 URI parsing library
3 *
4 * Copyright (C) 2007, Weijia Song <songweijia@gmail.com>
5 * Copyright (C) 2007, Sebastian Pipping <webmaster@hartwork.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. */
73static 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
124static 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
151static int URI_FUNC(AddBaseUriImpl)(URI_TYPE(Uri) * absDest,
152 const URI_TYPE(Uri) * relSource,
153 const URI_TYPE(Uri) * absBase,
154 UriResolutionOptions options) {
155 if (absDest == NULL) {
156 return URI_ERROR_NULL;
157 }
158 URI_FUNC(ResetUri)(absDest);
159
160 if ((relSource == NULL) || (absBase == NULL)) {
161 return URI_ERROR_NULL;
162 }
163
164 /* absBase absolute? */
165 if (absBase->scheme.first == NULL) {
166 return URI_ERROR_ADDBASE_REL_BASE;
167 }
168
169 /* [00/32] -- A non-strict parser may ignore a scheme in the reference */
170 /* [00/32] -- if it is identical to the base URI's scheme. */
171 /* [00/32] if ((not strict) and (R.scheme == Base.scheme)) then */
172 UriBool relSourceHasScheme = (relSource->scheme.first != NULL) ? URI_TRUE : URI_FALSE;
173 if ((options & URI_RESOLVE_IDENTICAL_SCHEME_COMPAT)
174 && (absBase->scheme.first != NULL)
175 && (relSource->scheme.first != NULL)
176 && (0 == URI_FUNC(CompareRange)(&(absBase->scheme), &(relSource->scheme)))) {
177 /* [00/32] undefine(R.scheme); */
178 relSourceHasScheme = URI_FALSE;
179 /* [00/32] endif; */
180 }
181
182 /* [01/32] if defined(R.scheme) then */
183 if (relSourceHasScheme) {
184 /* [02/32] T.scheme = R.scheme; */
185 absDest->scheme = relSource->scheme;
186 /* [03/32] T.authority = R.authority; */
187 if (!URI_FUNC(CopyAuthority)(absDest, relSource)) {
188 return URI_ERROR_MALLOC;
189 }
190 /* [04/32] T.path = remove_dot_segments(R.path); */
191 if (!URI_FUNC(CopyPath)(absDest, relSource)) {
192 return URI_ERROR_MALLOC;
193 }
194 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest)) {
195 return URI_ERROR_MALLOC;
196 }
197 /* [05/32] T.query = R.query; */
198 absDest->query = relSource->query;
199 /* [06/32] else */
200 } else {
201 /* [07/32] if defined(R.authority) then */
202 if (URI_FUNC(IsHostSet)(relSource)) {
203 /* [08/32] T.authority = R.authority; */
204 if (!URI_FUNC(CopyAuthority)(absDest, relSource)) {
205 return URI_ERROR_MALLOC;
206 }
207 /* [09/32] T.path = remove_dot_segments(R.path); */
208 if (!URI_FUNC(CopyPath)(absDest, relSource)) {
209 return URI_ERROR_MALLOC;
210 }
211 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest)) {
212 return URI_ERROR_MALLOC;
213 }
214 /* [10/32] T.query = R.query; */
215 absDest->query = relSource->query;
216 /* [11/32] else */
217 } else {
218 /* [28/32] T.authority = Base.authority; */
219 if (!URI_FUNC(CopyAuthority)(absDest, absBase)) {
220 return URI_ERROR_MALLOC;
221 }
222 /* [12/32] if (R.path == "") then */
223 if (relSource->pathHead == NULL && !relSource->absolutePath) {
224 /* [13/32] T.path = Base.path; */
225 if (!URI_FUNC(CopyPath)(absDest, absBase)) {
226 return URI_ERROR_MALLOC;
227 }
228 /* [14/32] if defined(R.query) then */
229 if (relSource->query.first != NULL) {
230 /* [15/32] T.query = R.query; */
231 absDest->query = relSource->query;
232 /* [16/32] else */
233 } else {
234 /* [17/32] T.query = Base.query; */
235 absDest->query = absBase->query;
236 /* [18/32] endif; */
237 }
238 /* [19/32] else */
239 } else {
240 /* [20/32] if (R.path starts-with "/") then */
241 if (relSource->absolutePath) {
242 int res;
243 /* [21/32] T.path = remove_dot_segments(R.path); */
244 if (!URI_FUNC(CopyPath)(absDest, relSource)) {
245 return URI_ERROR_MALLOC;
246 }
247 res = URI_FUNC(ResolveAbsolutePathFlag)(absDest);
248 if (res != URI_SUCCESS) {
249 return res;
250 }
251 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest)) {
252 return URI_ERROR_MALLOC;
253 }
254 /* [22/32] else */
255 } else {
256 /* [23/32] T.path = merge(Base.path, R.path); */
257 if (!URI_FUNC(CopyPath)(absDest, absBase)) {
258 return URI_ERROR_MALLOC;
259 }
260 if (!URI_FUNC(MergePath)(absDest, relSource)) {
261 return URI_ERROR_MALLOC;
262 }
263 /* [24/32] T.path = remove_dot_segments(T.path); */
264 if (!URI_FUNC(RemoveDotSegmentsAbsolute)(absDest)) {
265 return URI_ERROR_MALLOC;
266 }
267
268 if (!URI_FUNC(FixAmbiguity)(absDest)) {
269 return URI_ERROR_MALLOC;
270 }
271 /* [25/32] endif; */
272 }
273 /* [26/32] T.query = R.query; */
274 absDest->query = relSource->query;
275 /* [27/32] endif; */
276 }
277 URI_FUNC(FixEmptyTrailSegment)(absDest);
278 /* [29/32] endif; */
279 }
280 /* [30/32] T.scheme = Base.scheme; */
281 absDest->scheme = absBase->scheme;
282 /* [31/32] endif; */
283 }
284 /* [32/32] T.fragment = R.fragment; */
285 absDest->fragment = relSource->fragment;
286
287 return URI_SUCCESS;
288
289}
290
291
292
293int URI_FUNC(AddBaseUri)(URI_TYPE(Uri) * absDest,
294 const URI_TYPE(Uri) * relSource, const URI_TYPE(Uri) * absBase) {
295 const int res = URI_FUNC(AddBaseUriImpl)(absDest, relSource, absBase, URI_RESOLVE_STRICTLY);
296 if ((res != URI_SUCCESS) && (absDest != NULL)) {
297 URI_FUNC(FreeUriMembers)(absDest);
298 }
299 return res;
300}
301
302
303
304int URI_FUNC(AddBaseUriEx)(URI_TYPE(Uri) * absDest,
305 const URI_TYPE(Uri) * relSource, const URI_TYPE(Uri) * absBase,
306 UriResolutionOptions options) {
307 const int res = URI_FUNC(AddBaseUriImpl)(absDest, relSource, absBase, options);
308 if ((res != URI_SUCCESS) && (absDest != NULL)) {
309 URI_FUNC(FreeUriMembers)(absDest);
310 }
311 return res;
312}
313
314
315
316#endif