forked from nevali/liburi
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuri.c
More file actions
163 lines (149 loc) · 3.18 KB
/
uri.c
File metadata and controls
163 lines (149 loc) · 3.18 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
/* 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"
/* Create a new URI from an existing URI. If base is provided, the
* new URI will be rebased against it.
*/
URI *
uri_create_uri(const URI *restrict source, const URI *restrict base)
{
URI *uri;
uri = uri_dup_(source);
if(!uri)
{
return NULL;
}
if(base)
{
if(uri_rebase(uri, base))
{
uri_destroy(uri);
return NULL;
}
}
return uri;
}
/* Destroy a URI object */
int
uri_destroy(URI *uri)
{
if(uri)
{
uri_reset_(uri);
free(uri);
}
return 0;
}
/* Return 1 if a URI is absolute, 0 otherwise */
int
uri_absolute(const URI *uri)
{
return (uri->absolute ? 1 : 0);
}
/* Internal: allocate a new URI object */
URI *
uri_create_(void)
{
URI *p;
p = (URI *) calloc(1, sizeof(URI));
if(!p)
{
return NULL;
}
return p;
}
/* Internal: duplicate an existing URI object */
URI *
uri_dup_(const URI *src)
{
URI *p;
p = (URI *) calloc(1, sizeof(URI));
if(!p)
{
return NULL;
}
/* Copy of each of the string properties */
#define URI_COPYSTR_(dest, src, member) \
if((src)->member) \
{ \
(dest)->member = strdup((src)->member); \
if(!(dest)->member) \
{ \
uri_destroy((dest)); \
return NULL; \
} \
}
URI_COPYSTR_(p, src, scheme);
URI_COPYSTR_(p, src, auth);
URI_COPYSTR_(p, src, user);
URI_COPYSTR_(p, src, password);
URI_COPYSTR_(p, src, hoststr);
URI_COPYSTR_(p, src, portstr);
URI_COPYSTR_(p, src, authority);
URI_COPYSTR_(p, src, nss);
URI_COPYSTR_(p, src, query);
URI_COPYSTR_(p, src, fragment);
URI_COPYSTR_(p, src, composed);
#undef URI_COPYSTR_
/* Copy the host info */
uri_hostdata_copy_(&(p->hostdata), &(src->hostdata));
p->port = src->port;
/* Copy the path */
uri_path_copy_(p, src->pathfirst);
/* Copy the flags */
p->pathabs = src->pathabs;
/* Now set the UriUri members to point to the new strings */
uri_postparse_set_(p);
return p;
}
/* Internal: safely free the contents of a URI object, so that they can be
* replaced
*/
int
uri_reset_(URI *uri)
{
UriPathSegmentA *seg, *next;
uriFreeUriMembersA(&(uri->uri));
free(uri->scheme);
free(uri->auth);
free(uri->user);
free(uri->password);
free(uri->hoststr);
free(uri->hostdata.ip4);
free(uri->hostdata.ip6);
free((char *) uri->hostdata.ipFuture.first);
free(uri->portstr);
free(uri->authority);
free(uri->nss);
for(seg = uri->pathfirst; seg; seg = next)
{
next = seg->next;
free((char *) seg->text.first);
free(seg);
}
free(uri->query);
free(uri->fragment);
free(uri->composed);
memset(uri, 0, sizeof(URI));
return 0;
}