-
Notifications
You must be signed in to change notification settings - Fork 1
/
uri.c
371 lines (338 loc) · 10.1 KB
/
uri.c
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
#include "pch.h"
#include "uri.h"
struct uri
uri_parse(const char *uri_unsafe, int uri_len)
{
// Quick fix for strcspn reading past uri_len...
// Perhaps we should write a strncspn function or something
char *uri = alloca(uri_len + 1);
strncpy(uri, uri_unsafe, uri_len);
uri[uri_len] = '\0';
struct uri result;
memset(&result, 0, sizeof(struct uri));
if (uri_len <= 0) return result;
// Find protocol:
int colon_pos = strcspn(uri, ":");
size_t protocol_name_len = 0;
if (colon_pos < uri_len)
{
strncpy(result.protocol_str, uri, (size_t)colon_pos);
result.protocol = lookup_protocol(result.protocol_str, colon_pos);
switch(PROTOCOL_INFOS[result.protocol].prefix_type)
{
default:
case PROTOCOL_PREFIX_NORMAL:
protocol_name_len = (size_t)colon_pos + strlen("://");
break;
case PROTOCOL_PREFIX_NO_SLASHES:
protocol_name_len = (size_t)colon_pos + strlen(":");
break;
}
}
// Get optional port (0 if none provided)
colon_pos = strcspn(uri + protocol_name_len, ":") + protocol_name_len;
if (colon_pos < (uri_len - 1))
{
result.port = atoi(uri + colon_pos + 1);
}
else
{
colon_pos = -1;
}
// Find where path would start
// Will be end of string if there's no path
int path_pos = 0;
if (protocol_name_len > 0)
{
for (path_pos = protocol_name_len;
path_pos < uri_len && uri[path_pos] != '/';
++path_pos);
}
// Use path position instead of colon (if no port was given)
if (colon_pos < 0) colon_pos = path_pos;
// Get hostname for non-relative URIs
if (path_pos > 0 && protocol_name_len > 0)
{
// There's a port in the string, so the hostname is between the colon
// and the protocol name sections
size_t start_pos = protocol_name_len;
strncpy(result.hostname,
uri + start_pos, (size_t)colon_pos - start_pos);
}
// Get query location
int query_pos = strcspn(uri + path_pos, "?") + path_pos;
// Find fragment location if there is one. Note we don't care about the
// fragment at all as we have no use for it; we only detect it to make sure
// it doesn't appear in path, query, etc.
int frag_pos = strcspn(uri + path_pos, "#") + path_pos;
int frag_len = max(uri_len - frag_pos, 0);
int query_len = max(uri_len - query_pos - frag_len, 0);
if (query_pos + 1 < uri_len)
{
int query_len_tmp = query_len;
if (frag_pos < uri_len) --query_len_tmp;
strncpy(result.query, uri + query_pos + 1, query_len_tmp);
}
// Adjust path length
int path_len = max(uri_len - frag_len - query_len - path_pos, 0);
// Gopher-specific: it is common these days to prefix paths with the
// resource item type,
// e.g. gopher://foo.bar/0/a_text_file.txt
// so we have to accomodate for this for some links to
// work correctly
if (result.protocol == PROTOCOL_GOPHER)
{
enum gopher_item_type item = GOPHER_ITEM_UNSUPPORTED;
if (path_len >= 3 &&
uri[path_pos] == '/' &&
(item = gopher_item_lookup(uri[path_pos + 1]))
!= GOPHER_ITEM_UNSUPPORTED &&
uri[path_pos + 2] == '/')
{
path_pos += 2;
path_len -= 2;
}
result.gopher_item = item;
}
// Get path
if (path_pos < uri_len)
{
strncpy(result.path, uri + path_pos, path_len);
}
else
{
// Empty path
result.path[0] = '/';
result.path[1] = '\0';
}
#if 0
printf("Parsed URI: '%s'\n"
" Protocol: %s\n"
" Host : %s\n"
" Port : %d\n"
" Path : %s\n"
" Query : %s\n",
uri, PROTOCOL_INFOS[result.protocol].name,
result.hostname, result.port, result.path, result.query);
#endif
return result;
}
/* Convert URI to string (returns size of result) */
size_t
uri_str(
const struct uri *restrict const uri,
char *restrict buf,
size_t buf_size,
enum uri_string_flags flags)
{
if (!uri) return 0;
// Get scheme string
char scheme[PROTOCOL_NAME_MAX + strlen("://")];
scheme[0] = '\0';
if (!(uri->protocol == PROTOCOL_NONE ||
uri->protocol == PROTOCOL_UNKNOWN ||
flags & URI_FLAGS_NO_PROTOCOL_BIT))
{
strcpy(scheme, PROTOCOL_INFOS[uri->protocol].name);
switch(PROTOCOL_INFOS[uri->protocol].prefix_type)
{
default:
case PROTOCOL_PREFIX_NORMAL:
strcat(scheme, "://");
break;
case PROTOCOL_PREFIX_NO_SLASHES:
strcat(scheme, ":");
break;
}
}
else if (uri->protocol == PROTOCOL_UNKNOWN && *uri->protocol_str)
{
strcpy(scheme, uri->protocol_str);
strcat(scheme, "://");
}
int path_len = strlen(uri->path);
if (flags & URI_FLAGS_NO_TRAILING_SLASH_BIT &&
uri->path[path_len - 1] == '/')
{
path_len = max(path_len - 1, 0);
}
// Apply gopher item prefix
char gopher_item_prefix[12];
if (uri->protocol == PROTOCOL_GOPHER &&
!(flags & URI_FLAGS_NO_GOPHER_ITEM_BIT) &&
uri->gopher_item != GOPHER_ITEM_UNSUPPORTED)
{
if (flags & URI_FLAGS_FANCY_BIT)
{
gopher_item_prefix[0] = '/';
gopher_item_prefix[1] = '\x1b';
gopher_item_prefix[2] = '[';
gopher_item_prefix[3] = '3';
gopher_item_prefix[4] = '3';
gopher_item_prefix[5] = 'm';
gopher_item_prefix[6] = GOPHER_ITEM_IDS[uri->gopher_item];
gopher_item_prefix[7] = '\x1b';
gopher_item_prefix[8] = '[';
gopher_item_prefix[9] = '0';
gopher_item_prefix[10] = 'm';
gopher_item_prefix[11] = '\0';
}
else
{
gopher_item_prefix[0] = '/';
gopher_item_prefix[1] = GOPHER_ITEM_IDS[uri->gopher_item];
gopher_item_prefix[2] = '\0';
}
}
else
{
gopher_item_prefix[0] = '\0';
}
// Get query string
char query[URI_QUERY_MAX];
query[0] = '\0';
if (*uri->query && !(flags & URI_FLAGS_NO_QUERY_BIT))
{
query[0] = '?';
query[1] = '\0';
strncat(query, uri->query, URI_QUERY_MAX - 2);
}
const char *fmt;
if (!uri->port ||
flags & URI_FLAGS_NO_PORT_BIT)
{
fmt = "%s" // Scheme
"%s" // Hostname
"%s" // Gopher item prefix
"%.*s" // Path
"%s"; // Query
if (flags & URI_FLAGS_FANCY_BIT)
{
// Fancy mode escapes
fmt = "\x1b[2m%s\x1b[0m"
"%s"
"%s"
"\x1b[2m%.*s%s\x1b[0m";
}
// Print without port
return snprintf(buf, buf_size, fmt,
scheme,
uri->hostname,
gopher_item_prefix,
path_len,
uri->path,
query);
}
else
{
fmt = "%s" // Scheme
"%s" // Hostname
":%d" // Port
"%s" // Gopher item prefix
"%.*s" // Path
"%s"; // Query
if (flags & URI_FLAGS_FANCY_BIT)
{
// Fancy mode escapes
fmt = "\x1b[2m%s\x1b[0m"
"%s"
":%d"
"%s"
"\x1b[2m%.*s%s\x1b[0m";
}
// Print with port
return snprintf(buf, buf_size,
fmt,
scheme,
uri->hostname,
uri->port,
gopher_item_prefix,
path_len,
uri->path,
query);
}
}
/* Make a relative URI absolute */
void
uri_abs(struct uri *restrict base, struct uri *restrict rel)
{
if (rel->protocol != PROTOCOL_NONE ||
rel->hostname[0] != '\0')
{
// Not relative
return;
}
rel->protocol = base->protocol;
rel->port = base->port;
strncpy(rel->hostname, base->hostname, URI_HOSTNAME_MAX);
char *dir_end = NULL;
if (*rel->path != '/')
{
// The path is in same directory as current document
size_t base_len = strlen(base->path);
dir_end = base->path + base_len;
for (; dir_end > base->path && *dir_end != '/'; --dir_end);
// Temporarily modify the base directory so that the paths are appended
// correctly (only if the base path isn't itself a directory)
if (dir_end < base->path + base_len - 1) *dir_end = '\0';
else dir_end = NULL;
}
// Append and normalise paths
char old_path[URI_PATH_MAX];
strncpy(old_path, rel->path, URI_PATH_MAX);
path_normalise(base->path, old_path, rel->path);
if (dir_end)
{
// Restore trailing slash
*dir_end = '/';
}
}
/* Set the query component of a URI in a properly encoded form */
void
uri_set_query(struct uri *restrict uri, const char *restrict q)
{
//strncpy(uri->query, q, URI_QUERY_MAX);
uri_encode(uri->query, q, URI_QUERY_MAX);
}
/* Encode URI according to RFC 3986 */
void
uri_encode(
char *restrict o,
const char *restrict s,
size_t n)
{
static bool rfc3986[256];
// Initialise encoder table
static bool is_uri_encoder_init = false;
if (!is_uri_encoder_init)
{
for (int i = 0; i < sizeof(rfc3986); ++i)
{
// Flags all characters that need to be percent-encoded
rfc3986[i] = isalnum(i) ||
i == '~' ||
i == '-' ||
i == '.' ||
i == '_';
}
is_uri_encoder_init = true;
}
char *end = o + n;
for (;
*s && o < end;
++s)
{
// If this character is not flagged in the table; then just copy it
// directly
if (rfc3986[(unsigned char)*s])
{
*o++ = *s;
continue;
}
// Percent-encode the character
o += snprintf(o, end - o, "%%%02X", *s);
}
// Set null-terminator
if (o >= end) *end = '\0';
else *o = '\0';
}