summaryrefslogtreecommitdiffstats
path: root/include/uri.h
blob: 80df641f16068ecb60c6871d9585903390ea2773 (plain)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// uri.h - Minimal URI (de)serializer

// Ethan Werner
//
// URI Parser and Serializer
//
// The uri_t structure is intended to be used as a black box. The primary use
// of the header as a whole is to enable the efficient creation of
// related URIs.
//
// Usage Notes
//  The internally held strings should remain opaque and only interacted with
//  through the given functions
//
//  Any string interacting with a uri_t is copied upon both entry and
//  exit, thus, all strings except those must be (de)allocated by the user.
//
// Thread safety: Not thread-safe. External synchronization required.
// Limitations: uri_parse only accepts absolute URIs with a scheme. Relative
//   URIs (e.g., "/path/to/resource") will cause uri_parse to return NULL.


#ifndef URI_H
#define URI_H

typedef enum
{
	BUILD,
	SCHEME,
	USERINFO,
	HOST,
	PORT,
	PATH,
	QUERY,
	FRAGMENT,
	URI_END
} uri_element_t;

typedef struct uri_t
{
	char *data[URI_END];
} uri_t;

uri_t *uri_init(void);
void uri_free(uri_t *);
void uri_set(uri_t *, uri_element_t, char const *);
char *uri_get(uri_t *);
uri_t *uri_parse(char const *);
char *uri_remove(uri_t *, uri_element_t);
void uri_print(uri_t const *);
void uri_print_elements(uri_t const *);
void uri_build(uri_t *);

#endif // URI_H


#ifdef URI_IMPLEMENTATION

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>

static void uri_copy_string(char **ptr, char const *source);

uri_t *uri_init(void)
{
	uri_t *u = calloc(1, sizeof(uri_t));
	if (!u)
		return NULL;
	return u;
}

void uri_free(uri_t *u)
{
	if (u == NULL)
		return;
	for(int i = 0; i < URI_END; i++)
	{
		if(u->data[i])
			free(u->data[i]);
	}
	free(u);
}

static void uri_copy_string(char **ptr, char const *source)
{
	if (!ptr)
		return;
	if (*ptr)
		free(*ptr);
	if(source) {
		size_t n = strlen(source);
		if (n == SIZE_MAX)
			return;
		*ptr = malloc(n + 1);
		if (!*ptr)
			return;
		memcpy(*ptr, source, n);
		(*ptr)[n] = '\0';
	}
	else {
		*ptr = NULL;
	}
}

void uri_set(uri_t *u, uri_element_t e, char const *data)
{
	if (u == NULL)
		return;
	if ((int)e < 0 || (int)e >= URI_END)
		return;
	uri_copy_string(&u->data[e], data);
}

char *uri_get(uri_t *u)
{
	if (u == NULL)
		return NULL;
	uri_build(u);
	char *c = NULL;
	uri_copy_string(&c, u->data[BUILD]);
	return c;
}

char *uri_remove(uri_t *u, uri_element_t e)
{
	if (u == NULL)
		return NULL;
	if ((int)e < 0 || (int)e >= URI_END)
		return NULL;
	char *data = u->data[e];
	u->data[e] = NULL;
	return data;
}

void uri_build(uri_t *u)
{
	if (u == NULL)
		return;
	size_t n = 0;

	if(u->data[BUILD])
		free(u->data[BUILD]);
	u->data[BUILD] = NULL;

	// Length of built string
	{
		size_t s;
		if(u->data[SCHEME])
		{
			s = strlen(u->data[SCHEME]) + 1;
			if (n > SIZE_MAX - s) { u->data[BUILD] = NULL; return; }
			n += s;
		}
		if(u->data[USERINFO])
		{
			s = strlen(u->data[USERINFO]) + 1;
			if (n > SIZE_MAX - s) { u->data[BUILD] = NULL; return; }
			n += s;
		}
		if(u->data[HOST])
		{
			s = strlen(u->data[HOST]) + 2;
			if (n > SIZE_MAX - s) { u->data[BUILD] = NULL; return; }
			n += s;
		}
		if(u->data[PORT])
		{
			s = strlen(u->data[PORT]) + 1;
			if (n > SIZE_MAX - s) { u->data[BUILD] = NULL; return; }
			n += s;
		}
		if(u->data[PATH])
		{
			s = strlen(u->data[PATH]);
			if (n > SIZE_MAX - s) { u->data[BUILD] = NULL; return; }
			n += s;
		}
		if(u->data[QUERY])
		{
			s = strlen(u->data[QUERY]) + 1;
			if (n > SIZE_MAX - s) { u->data[BUILD] = NULL; return; }
			n += s;
		}
		if(u->data[FRAGMENT])
		{
			s = strlen(u->data[FRAGMENT]) + 1;
			if (n > SIZE_MAX - s) { u->data[BUILD] = NULL; return; }
			n += s;
		}
	}

	if (n == SIZE_MAX)
		return;
	char *build = malloc(n + 1);
	if (!build)
		return;
	build[0] = '\0';

	size_t pos = 0;
	// Copy strings to BUILD
	if(u->data[SCHEME])
	{
		size_t len = strlen(u->data[SCHEME]);
		memcpy(build + pos, u->data[SCHEME], len);
		pos += len;
		build[pos++] = ':';
	}

	if(u->data[HOST])
	{
		memcpy(build + pos, "//", 2);
		pos += 2;

		if(u->data[USERINFO])
		{
			size_t len = strlen(u->data[USERINFO]);
			memcpy(build + pos, u->data[USERINFO], len);
			pos += len;
			build[pos++] = '@';
		}

		size_t len = strlen(u->data[HOST]);
		memcpy(build + pos, u->data[HOST], len);
		pos += len;

		if(u->data[PORT])
		{
			build[pos++] = ':';
			len = strlen(u->data[PORT]);
			memcpy(build + pos, u->data[PORT], len);
			pos += len;
		}
	}
	if(u->data[PATH])
	{
		size_t len = strlen(u->data[PATH]);
		memcpy(build + pos, u->data[PATH], len);
		pos += len;
	}
	if(u->data[QUERY])
	{
		build[pos++] = '?';
		size_t len = strlen(u->data[QUERY]);
		memcpy(build + pos, u->data[QUERY], len);
		pos += len;
	}
	if(u->data[FRAGMENT])
	{
		build[pos++] = '#';
		size_t len = strlen(u->data[FRAGMENT]);
		memcpy(build + pos, u->data[FRAGMENT], len);
		pos += len;
	}

	build[pos] = '\0';
	u->data[BUILD] = build;
}

static int uri_set_range(uri_t *u, uri_element_t e, char const *from, char const *to)
{
	if (u == NULL || from == NULL || to == NULL)
		return 0;
	if ((int)e < 0 || (int)e >= URI_END)
		return 0;
	if (to <= from)
		return 0;
	size_t len = (size_t)(to - from);
	if (len == SIZE_MAX)
		return 0;
	char *s = malloc(len + 1);
	if (!s)
		return 0;
	s[len] = 0;
	memcpy(s, from, len);

	if(u->data[e])
		free(u->data[e]);

	u->data[e] = s;
	return 1;
}

uri_t *uri_parse(char const *uri_s)
{
	if (uri_s == NULL)
		return NULL;
	uri_t *uri = calloc(1, sizeof(uri_t));
	if (!uri)
		return NULL;

	char const *current = uri_s;
	char const *working;

	// SCHEME
	working = strchr(current, ':');

	if(!working)
	{
		uri_free(uri);
		return NULL;
	}

 if (!uri_set_range(uri, SCHEME, current, working)) {
    uri_free(uri);
    return NULL;
  }
  current = ++working;

  // AUTHORITY
  if(working[0] == '/' && working[1] == '/')
  {
    current = working += 2;

    char const *at = strchr(working, '@');
    if (at) {
      if (!uri_set_range(uri, USERINFO, current, at)) {
        uri_free(uri);
        return NULL;
      }
      current = at + 1;

      char const *host_end = strpbrk(current, ":/");
      if (!host_end)
        host_end = current + strlen(current);

      if (!uri_set_range(uri, HOST, current, host_end)) {
        uri_free(uri);
        return NULL;
      }
      working = host_end;
    } else {
      while(*working && *working != ':' && *working != '/')
        working++;

      if (!uri_set_range(uri, HOST, current, working)) {
        uri_free(uri);
        return NULL;
      }
    }

    if(!*working)
    {
      uri_build(uri);
      return uri;
    }

    // PORT
    if(*working == ':')
    {
      current = ++working;

      while(*working && *working != '/')
        working++;

      if (!uri_set_range(uri, PORT, current, working)) {
        uri_free(uri);
        return NULL;
      }
    }

    if(!*working)
    {
      uri_build(uri);
      return uri;
    }

    current = working;

    while(*working && *working != '?' && *working != '#')
      working++;
  }
  else
  {
    // No authority section - scan for query/fragment delimiters
    while(*working && *working != '?' && *working != '#')
      working++;
  }

  // PATH
  if (!uri_set_range(uri, PATH, current, working)) {
    uri_free(uri);
    return NULL;
  }

  if(!*working)
  {
    uri_build(uri);
    return uri;
  }

  current = working;

  // QUERY
  if(*working == '?')
  {
    char const *query_start = ++working;

    while(*working && *working != '#')
      working++;

    if (!uri_set_range(uri, QUERY, query_start, working)) {
      uri_free(uri);
      return NULL;
    }

    current = working;
  }

  // FRAGMENT
  if(*working == '#')
  {
    char const *frag_start = ++working;

    while(*working)
      working++;

    if (!uri_set_range(uri, FRAGMENT, frag_start, working)) {
      uri_free(uri);
      return NULL;
    }
  }

  uri_build(uri);

  return uri;
}

void uri_print(uri_t const *u)
{
	if (u == NULL)
		return;
	if(u->data[BUILD])
		fputs(u->data[BUILD], stdout);
}

void uri_print_elements(uri_t const *u)
{
	if (u == NULL)
		return;
	for(int i = 0; i < URI_END; i++)
	{
		printf("%d", i);

		if(u->data[i])
		{
			printf(" - %s", u->data[i]);
		}

		putchar('\n');
	}
}

#endif // URI_IMPLEMENTATION