summaryrefslogtreecommitdiffstats
path: root/include/uri.h
blob: a00400dce2c23667b9f0f5c5b012ad3eb16bf0dd (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
// 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.


#ifndef URI_H
#define URI_H


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



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


#endif // URI_H


#ifdef URI_IMPLEMENTATION


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


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


#define copy_string( ptr, source ) {       \
	if( source )                           \
	{                                      \
		size_t n = strlen( source );       \
		ptr = malloc( n + 1 );             \
		ptr[n] = '\0';                     \
		strcpy( ptr, source );             \
	}                                      \
	else                                   \
	{                                      \
		ptr = NULL;                        \
	}                                      \
}


void uri_set( uri_t *u, uri_element_t e, char const *data )
{
	char *c;
	copy_string( c, data );

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

	u->data[e] = c;
}


char * uri_get( uri_t *u )
{
	uri_build( u );
	char *c;
	copy_string( c, u->data[BUILD] );

	return c;
}


char * uri_remove( uri_t *u, uri_element_t e )
{
	char *data = u->data[e];
	uri_set( u, e, NULL );

	return data;
}

// TODO: Account for / when path is missing in query and fragment
void uri_build( uri_t *u )
{
	int n = 0;

    // TODO: Convert to a for loop
	if( u->data[BUILD] )
		free( u->data[BUILD] );

	// Length of built string
	if( u->data[SCHEME] )
		n += strlen( u->data[SCHEME] ) + 1; // ":"

	if( u->data[USERINFO] )
		n += strlen( u->data[USERINFO] ) + 1; // "@"

	if( u->data[HOST] )
		n += strlen( u->data[HOST] ) + 2; // "//"

	if( u->data[PORT] )
		n += strlen( u->data[PORT] ) + 1; // ":"

	if( u->data[PATH] )
		n += strlen( u->data[PATH] );

	if( u->data[QUERY] )
		n += strlen( u->data[QUERY] ) + 1; // ?

	if( u->data[FRAGMENT] )
		n += strlen( u->data[FRAGMENT] ) + 1; // #

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

	// Copy strings to BUILD
	strcpy( build, u->data[SCHEME] );
	strcat( build, ":" );

	if( u->data[HOST] )
	{
		strcat( build, "//" );

		if( u->data[USERINFO] )
		{
			strcat( build, u->data[USERINFO] );
			strcat( build, "@" );
		}

		strcat( build, u->data[HOST] );

		if( u->data[PORT] )
		{
			strcat( build, ":" );
			strcat( build, u->data[PORT] );
		}
	}
	if( u->data[PATH] )
	{
		strcat( build, u->data[PATH] );
	}
	if( u->data[QUERY] )
	{
		strcat( build, "?" );
		strcat( build, u->data[QUERY] );
	}
	if( u->data[FRAGMENT] )
	{
		strcat( build, "#" );
		strcat( build, u->data[FRAGMENT] );
	}

	u->data[BUILD] = build;
}


char * copy_substring( char const *from, char const *to )
{
	char *s = ( char * ) malloc( to - from + 1 );
	s[to - from] = 0;
	strncpy( s, from, to - from );

	return s;
}



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

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

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

	if( !working )
		return NULL;

	token = copy_substring( current, working );
	uri_set( uri, SCHEME, token );
	current = ++working;

	// AUTHORITY
	if( ( *( working ) == '/' ) && ( *( working + 1 ) == '/' ) )
	{
		current = working += 2;

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

		// USERINFO
		if( *working == '@' )
		{
			token = copy_substring( current, working );
			uri_set( uri, USERINFO, token );

			current = ++working;

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

		// HOST
		token = copy_substring( current, working );
		uri_set( uri, HOST, token );

		if( !( *working ) )
			return uri;

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

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

			token = copy_substring( current, working );
			uri_set( uri, PORT, token );
		}


		if( !( *working ) )
			return uri;

		current = working;

		while( *working && *working != '?' && *working != '#' )
			working++;
	}
	else
	{
		while( *working && *working != '/' )
			working++;
	}

	// PATH
	token = copy_substring( current, working );
	uri_set( uri, PATH, token );

	if( !( *working ) )
		return uri;

	current = working;

	// QUERY
	if( *working == '?' )
	{
		while( *working && *working != '#' )
			working++;

		token = copy_substring( current, working );
		uri_set( uri, QUERY, token );

		current = working;
	}

	// FRAGMENT
	if( *working == '#' )
	{
		while( *working )
			working++;

		token = copy_substring( current, working );
		uri_set( uri, FRAGMENT, token );
	}

	uri_build( uri );

	return uri;
}


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


void uri_print_elements( uri_t const *u )
{
	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