forked from nevali/liburi
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparse.c
More file actions
354 lines (335 loc) · 8.34 KB
/
parse.c
File metadata and controls
354 lines (335 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright 2015-2017 BBC
*/
/*
* Copyright 2012 Mo McRoberts.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "p_liburi.h"
static const char *uri_schemeend_(const char *str);
static int uri_parse_nonhier_(URI *restrict dest, const char *uristr);
static char *uri_range_copy_(const UriTextRangeA *range);
static int uri_range_set_(UriTextRangeA *restrict range, const char *restrict src);
static int uri_path_copy_ref_(UriPathSegmentA **head, UriPathSegmentA **tail, const UriPathSegmentA *src);
/* Create a URI from a 7-bit ASCII string, which we consider to be the
* native form.
* Use uri_create_str(), uri_create_wstr(), or uri_create_ustr() if the
* source string is not plain ASCII.
*/
URI *
uri_create_ascii(const char *restrict str, const URI *restrict base)
{
URI *uri;
UriParserStateA state;
const char *t;
uri = uri_create_();
if(!uri)
{
return NULL;
}
/* Deal with non-hierarchical URIs properly:
* Scan the string for the end of the scheme, If the character immediately
* following the colon is not a slash, we consider the URI
* non-hierarchical and parse it accordingly.
*/
t = uri_schemeend_(str);
if(t && t[0] && t[1] != '/' && t[1] != '\\')
{
/* A scheme is present and the first character after the colon
* is not slash
*/
uri->hier = 0;
if(uri_parse_nonhier_(uri, str))
{
uri_destroy(uri);
return NULL;
}
}
else
{
uri->hier = 1;
state.uri = &(uri->uri);
if(uriParseUriA(&state, str) != URI_SUCCESS)
{
uri_destroy(uri);
return NULL;
}
}
if(uri_postparse_(uri))
{
uri_destroy(uri);
return NULL;
}
if(uri_rebase(uri, base))
{
uri_destroy(uri);
return NULL;
}
return uri;
}
/* Internal: find the first character after the URI scheme; this function will
* therefore return either NULL, or a pointer to a colon.
*/
static const char *
uri_schemeend_(const char *str)
{
for(; str && *str; str++)
{
if(*str == ':')
{
return str;
}
/* These characters cannot appear within a scheme, and so
* indicate that the scheme is absent.
*/
if(*str == '@' || *str == '/' || *str == '%' || *str == '\\')
{
break;
}
}
return NULL;
}
/* Internal: parse a non-hierarchical URI
* This is a URI in the form:
*
* scheme ':' authority ':' namespace-specific + '?' query + '#' + fragment
*
* The namespace-specific segment (NSS) is considered to be an opaque string.
*/
static int
uri_parse_nonhier_(URI *restrict dest, const char *uristr)
{
#warning Handling non-hierarchical URIs is not yet supported
errno = EPERM;
return -1;
}
/* Internal: perform post-parsing normalisation and manipulation of a URI */
int
uri_postparse_(URI *uri)
{
uriNormalizeSyntaxA(&(uri->uri));
/* Copy the UriUriA text ranges into new buffers, then set the
* ranges to point back to our new buffers. This means that the uriparser
* APIs will still work on our UriUriA object, but we can manipulate the
* components as needed.
*
* The UriUriA's owner flag will be set to false, indicating that with the
* exception of the ip4 and ip6 structs within its hostData, it does not
* own any of the memory its text ranges point at.
*/
uri->scheme = uri_range_copy_(&(uri->uri.scheme));
if(uri->scheme)
{
uri->absolute = 1;
}
uri->auth = uri_range_copy_(&(uri->uri.userInfo));
uri->hoststr = uri_range_copy_(&(uri->uri.hostText));
uri->portstr = uri_range_copy_(&(uri->uri.portText));
uri->query = uri_range_copy_(&(uri->uri.query));
uri->fragment = uri_range_copy_(&(uri->uri.fragment));
/* Copy the path data */
if(uri_path_copy_(uri, uri->uri.pathHead))
{
return -1;
}
/* Copy the host data */
if((uri->hostdata.ipFuture.first = uri_range_copy_(&(uri->uri.hostData.ipFuture))))
{
uri->hostdata.ipFuture.afterLast = strchr(uri->hostdata.ipFuture.first, 0);
}
if(uri_hostdata_copy_(&(uri->hostdata), &(uri->uri.hostData)))
{
return -1;
}
/* Parse the port number, if present */
if(uri->portstr)
{
uri->port = atoi(uri->portstr);
if(uri->port < 1 || uri->port > 65535)
{
uri->port = 0;
}
}
return uri_postparse_set_(uri);
}
/* Replace the members of a UriUriA with pointers to our own buffers
* excepting ip4 and ip6 within the hostData member, which is always
* duplicated.
*/
int
uri_postparse_set_(URI *uri)
{
uriFreeUriMembersA(&(uri->uri));
uri->uri.owner = URI_FALSE;
uri_range_set_(&(uri->uri.scheme), uri->scheme);
uri_range_set_(&(uri->uri.userInfo), uri->auth);
uri_range_set_(&(uri->uri.hostText), uri->hoststr);
uri_range_set_(&(uri->uri.portText), uri->portstr);
uri_range_set_(&(uri->uri.query), uri->query);
uri_range_set_(&(uri->uri.fragment), uri->fragment);
uri_range_set_(&(uri->uri.hostData.ipFuture), uri->hostdata.ipFuture.first);
uri_hostdata_copy_(&(uri->uri.hostData), &(uri->hostdata));
uri_path_copy_ref_(&(uri->uri.pathHead), &(uri->uri.pathTail), uri->pathfirst);
uri->uri.absolutePath = (uri->pathabs ? URI_TRUE : URI_FALSE);
return 0;
}
/* Internal: copy a UriHostData struct, allocating memory as needed */
int
uri_hostdata_copy_(struct UriHostDataStructA *restrict dest, const struct UriHostDataStructA *restrict src)
{
if(src->ip4)
{
dest->ip4 = (UriIp4 *) calloc(1, sizeof(UriIp4));
if(!dest->ip4)
{
return -1;
}
memcpy(dest->ip4, src->ip4, sizeof(UriIp4));
}
if(src->ip6)
{
dest->ip6 = (UriIp6 *) calloc(1, sizeof(UriIp6));
if(!dest->ip6)
{
return -1;
}
memcpy(dest->ip6, src->ip6, sizeof(UriIp6));
}
return 0;
}
/* Internal: copy a UriPath into our URI object */
int
uri_path_copy_(URI *uri, const UriPathSegmentA *head)
{
UriPathSegmentA *seg, *prev;
prev = NULL;
uri->pathabs = (int) uri->uri.absolutePath;
for(; head; head = head->next)
{
seg = (UriPathSegmentA *) calloc(1, sizeof(UriPathSegmentA));
if(!seg)
{
return -1;
}
seg->text.first = uri_range_copy_(&(head->text));
if(seg->text.first)
{
seg->text.afterLast = strchr(seg->text.first, 0);
}
if(prev)
{
prev->next = seg;
}
else
{
uri->pathfirst = seg;
}
prev = seg;
}
uri->pathlast = prev;
uri->pathcur = uri->pathfirst;
return 0;
}
/* Internal: create a new chain of path segments referencing the
* strings in the source segment; this is used because even if
* UriUriA::owner is false, the segments themselves will still be
* freed by uriFreeUriMembersA().
*/
static int
uri_path_copy_ref_(UriPathSegmentA **head, UriPathSegmentA **tail, const UriPathSegmentA *src)
{
UriPathSegmentA *seg, *prev;
prev = NULL;
for(; src; src = src->next)
{
seg = (UriPathSegmentA *) calloc(1, sizeof(UriPathSegmentA));
if(!seg)
{
return -1;
}
seg->text.first = src->text.first;
seg->text.afterLast = src->text.afterLast;
if(prev)
{
prev->next = seg;
}
else
{
*head = seg;
}
prev = seg;
}
*tail = prev;
return 0;
}
/* Internal: copy a UriTextRange to a newly-allocated buffer */
static char *
uri_range_copy_(const UriTextRangeA *range)
{
size_t l;
char *buf;
if(range->first && range->afterLast)
{
l = range->afterLast - range->first + 1;
}
else if(range->first)
{
l = strlen(range->first) + 1;
}
else
{
errno = 0;
return NULL;
}
if(!l)
{
errno = 0;
return NULL;
}
buf = (char *) malloc(l);
if(!buf)
{
return NULL;
}
strncpy(buf, range->first, l);
buf[l - 1] = 0;
return buf;
}
/* Internal: free a UriTextRange if it's owned by the UriUri, and then
* set it to point to the supplied null-terminated string. Note that
* uri->owner must be set to URI_FALSE after calling this or else
* uriFreeUriMembersA() will free heap blocks it doesn't own.
* src may be NULL. owner must be uri->owner.
*/
static int
uri_range_set_(UriTextRangeA *range, const char *src)
{
/* Set the range to point to the entire length of src (provided src
* is non-NULL)
*/
range->first = src;
if(src)
{
range->afterLast = strchr(src, 0);
}
else
{
range->afterLast = NULL;
}
return 0;
}