summary refs log tree commit diff stats
path: root/yuri.c
blob: 89c2a2fd051b5e4add1fb65d63a78ff0231ddf78 (plain) (blame)
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
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "yuri.h"

struct uri *
uri_new(void)
{
	struct uri *ret;

	ret = malloc(sizeof(*ret));
	if (ret == NULL)
		return NULL;
	memset(ret, 0, sizeof(*ret));
	return ret;
}

int
uri_set_scheme(struct uri *uri, const char *scheme)
{
	const char *newscheme;

	newscheme = strdup(scheme);
	if (newscheme == NULL)
		return -1;
	free(uri->scheme);
	uri->scheme = newscheme;

	return 0;
}

int
uri_set_authority(struct uri *uri, int type, const char *user, const char *host, int port)
{
	const char *newuser;
	const char *newhost;

	newhost = strdup(host);
	if (newhost == NULL)
		return -1;

	if (user) {
		newuser = strdup(user);
		if (newuser == NULL) {
			free(newhost);
			return -1;
		}
	} else {
		newuser = NULL;
	}

	free(uri->authority.user);
	free(uri->authority.host);

	uri->authority.user = newuser;
	uri->authority.host = newhost;
	uri->authority.port = port;
	uri->authority.type = type;

	return 0;
}

int
uri_set_authority_type(struct uri *u, int type)
{
	u->authority.type = type;
	return 0;
}

int
uri_set_authority_user(struct uri *u, const char *user)
{
	const char *newuser;

	newuser = strdup(user);
	if (newuser == NULL)
		return -1;
	free(u->authority.user);
	u->authority.user = newuser;

	return 0;
}

int
uri_set_authority_host(struct uri *u, const char *host)
{
	const char *newhost;

	newhost = strdup(host);
	if (newhost == NULL)
		return -1;
	free(u->authority.host);
	u->authority.host = newhost;

	return 0;
}

int
uri_set_authority_port(struct uri *u, int port)
{
	u->authority.port = port;
	return 0;
}

int
uri_append_path(struct uri *uri, const char *item)
{
	char **path;
	int npath;

	if (uri->npath == 0)
		npath = 1;
	else
		npath = uri->npath + 1;

	path = realloc(uri->path, sizeof(*uri->path)*npath);
	if (path == NULL)
		return -1;
	uri->path = path;
	uri->path[npath-1] = strdup(item);
	if (uri->path[npath-1] == NULL)
		return -1;
	uri->npath = npath;

	return 0;
}

int
uri_set_query(struct uri *uri, const char *query)
{
	const char *newquery;

	newquery = strdup(query);
	if (newquery == NULL)
		return -1;
	free(uri->query);
	uri->query = newquery;

	return 0;
}

int
uri_set_fragment(struct uri *uri, const char *fragment)
{
	const char *newfragment;

	newfragment = strdup(fragment);
	if (newfragment == NULL)
		return -1;
	free(uri->fragment);
	uri->fragment = newfragment;

	return 0;
}

void
uri_unset_scheme(struct uri *u)
{
	free(u->scheme);
	u->scheme = NULL;
}

void
uri_unset_authority(struct uri *u)
{
	uri_unset_authority_type(u);
	uri_unset_authority_user(u);
	uri_unset_authority_host(u);
	uri_unset_authority_port(u);
}

void
uri_unset_authority_type(struct uri *u)
{
	u->authority.type = 0;
}

void
uri_unset_authority_user(struct uri *u)
{
	free(u->authority.user);
	u->authority.user = NULL;
}

void
uri_unset_authority_host(struct uri *u)
{
	free(u->authority.host);
	u->authority.host = NULL;
}

void
uri_unset_authority_port(struct uri *u)
{
	u->authority.port = 0;
}

void
uri_unset_path(struct uri *u)
{
	int i;
	for (i = 0; i < u->npath; i++)
		free(u->path[i]);
	free(u->path);
	u->path = NULL;
	u->npath = 0;
}

void
uri_unset_query(struct uri *u)
{
	free(u->query);
	u->query = NULL;
}

void
uri_unset_fragment(struct uri *u)
{
	free(u->fragment);
	u->fragment = NULL;
}

const char *
uri_get_scheme(struct uri *u)
{
	return u->scheme;
}

int
uri_get_authority_type(struct uri *u)
{
	return u->authority.type;
}

const char *
uri_get_authority_user(struct uri *u)
{
	return u->authority.user;
}

const char *
uri_get_authority_host(struct uri *u)
{
	return u->authority.host;
}

int
uri_get_authority_port(struct uri *u)
{
	return u->authority.port;
}

const char **
uri_get_path(struct uri *u)
{
	return u->path;
}

/*
 * TODO: cleanup
 */
char *
uri_string_path(struct uri *u)
{
	struct uri *temp;
	char *ret;

	temp = uri_new();
	if (temp == NULL)
		return NULL;
	temp->path = u->path;
	temp->npath = u->npath;

	ret = uri_encode(temp);
	free(temp);
	if (ret == NULL)
		return NULL;

	return ret;
}

int
uri_get_npath(struct uri *u)
{
	return u->npath;
}

const char *
uri_get_query(struct uri *u)
{
	return u->query;
}

const char *
uri_get_fragment(struct uri *u)
{
	return u->fragment;
}

void
uri_free(struct uri *u)
{
	int i;
	if (!u)
		return;
	free(u->scheme);
	free(u->authority.user);
	free(u->authority.host);
	for (i = 0; i < u->npath; i++)
		free(u->path[i]);
	free(u->path);
	free(u->query);
	free(u->fragment);
	free(u);
}