-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.c
386 lines (320 loc) · 10.5 KB
/
url.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "url.h"
/* 224.0.0.0-224.0.0.255: "Reserved for special 'well-known' multicast addresses."
* 224.0.1.0-238.255.255.255: "Globally-scoped (Internet-wide) multicast addresses."
* 239.0.0.0-239.255.255.255: "Administratively-scoped (local) multicast addresses."
*/
static char *URL_REG_PATTERN_UDP_MCAST_GROUP =
"2(2[4-9]|3[0-9])"
"(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))"
"(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))"
"(\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))";
static inline void prepchr(char *s, const char c) {
memmove(s+1, s, strlen(s));
s[0] = c;
}
static inline char* buf_tail(URL *it) { return &it->buf[it->buf_len]; }
static inline void path_ensure_start_slash(URL *it) {
if (it->scheme == URL_SCHEME_FILE) { // add missing /
if ((it->got_path) && /* path was parsed */
(url_path(it)[0] != URL_SEPARATOR_PATH) && /* missing / */
(sizeof(it->buf) > it->buf_len+1)) /* have enought space */
prepchr(&it->buf[it->pos_path], URL_SEPARATOR_PATH);
}
}
static inline void split_host_port(const char *buf, const size_t bufsz, char *host, uint8_t *got_host, uint16_t *port) {
char *token = NULL;
char port_raw[6] = { 0 };
/* /221.1.1.1:5500 */
if (!bufsz) return;
*got_host = 1;
token = strchr(buf, URL_SEPARATOR_PORT);
if (token) { /* host + port */
strncpy(host, buf, token-buf);
strncpy(
port_raw,
token+1, /* port buffer start */
/* port buffer size = */
bufsz /* total-buffer-size */
- (token-buf) /* - (host-buffer-size) */
);
*port = (uint16_t)atoi(port_raw);
return;
}
*port = 0;
strncpy(host, buf, bufsz);
}
void url_parse(URL *it, const char *raw) {
char copy[300];
char *cursor = NULL,
*token = NULL;
uint8_t got_host = 0;
size_t bufsz = 0;
regex_t reg = { 0 }; /* UDP mcast regexp */
memset(it, 0, sizeof(*it));
cursor = strcpy(copy, raw);
it->scheme = URL_SCHEME_UNKNOWN;
if ((token=strstr(cursor, URL_SEPARATOR_SCHEME))) {
it->scheme = url_scheme_parse(
cursor, /* scheme buffer */
token-cursor /* scheme buffer size */
);
cursor = token + strlen(URL_SEPARATOR_SCHEME);
}
if ((token=strchr(cursor, URL_SEPARATOR_USERINFO))) {
bufsz = token-cursor;
strncpy(
buf_tail(it),
cursor, /* userinfo buffer */
bufsz /* userinfo buffer size */
);
it->pos_userinfo = it->buf_len;
it->buf_len += (uint16_t)bufsz + 1;
it->got_user_info = 1;
cursor = token + 1;
}
if (it->scheme != URL_SCHEME_FILE) {
if ((token=strchr(cursor, URL_SEPARATOR_PATH))) { /* got some data after host:[port]? */
bufsz = token-cursor;
/* ensure host != "." && host != ".." */
if (!(((bufsz == 1) &&
(cursor[0] == URL_RELATIVE_PATH_START)) || /* '.' */
((bufsz == 2) &&
(cursor[0] == URL_RELATIVE_PATH_START) &&
(cursor[1] == URL_RELATIVE_PATH_START)))) { /* ".." */
split_host_port(
cursor, /* host[+port] buffer */
bufsz, /* host[+port] buffer size */
buf_tail(it),
&got_host,
&it->port
);
if (got_host) {
it->pos_host = it->buf_len;
it->buf_len += strlen(&it->buf[it->pos_host]) + 1;
it->got_host = 1;
}
cursor = token;
}
} else {
bufsz = strlen(cursor);
split_host_port(cursor, bufsz, buf_tail(it), &got_host, &it->port);
if (got_host) {
it->pos_host = it->buf_len;
it->buf_len += strlen(&it->buf[it->pos_host]) + 1;
it->got_host = 1;
}
cursor = NULL;
}
}
if (cursor) {
if ((token=strchr(cursor, URL_SEPARATOR_QUERY))) {
bufsz = token-cursor;
strncpy(
buf_tail(it),
cursor, /* path buffer */
bufsz /* path buffer size */
);
it->pos_path = it->buf_len;
it->buf_len += (uint16_t)bufsz + 1;
it->got_path = 1;
path_ensure_start_slash(it);
cursor = token + 1;
} else {
if ((token=strchr(cursor, URL_SEPARATOR_FRAGMENT))) {
bufsz = token-cursor;
strncpy(
buf_tail(it),
cursor, /* path buffer */
bufsz /* path buffer size */
);
it->pos_path = it->buf_len;
it->buf_len += (uint16_t)bufsz + 1;
it->got_path = 1;
path_ensure_start_slash(it);
cursor = token + 1;
strcpy(buf_tail(it), cursor);
it->pos_fragment = it->buf_len;
it->buf_len += (uint16_t)strlen(cursor) + 1;
it->got_fragment = 1;
} else {
strcpy(buf_tail(it), cursor);
it->pos_path = it->buf_len;
it->buf_len += (uint16_t)strlen(cursor) + 1;
it->got_path = 1;
path_ensure_start_slash(it);
}
cursor = NULL;
}
}
if (cursor) {
if ((token=strchr(cursor, URL_SEPARATOR_FRAGMENT))) {
bufsz = token-cursor;
strncpy(
buf_tail(it),
cursor, /* query buffer */
bufsz /* query buffer size */
);
it->pos_query = it->buf_len;
it->buf_len += (uint16_t)bufsz + 1;
it->got_query = 1;
cursor = token + 1;
strcpy(buf_tail(it), cursor);
it->pos_fragment = it->buf_len;
it->buf_len += (uint16_t)strlen(cursor) + 1;
it->got_fragment = 1;
} else {
strcpy(buf_tail(it), cursor);
it->pos_query = it->buf_len;
it->buf_len += (uint16_t)strlen(cursor) + 1;
it->got_query = 1;
cursor = NULL;
}
}
{ /* post-processing: guess missing from context */
if (it->scheme == URL_SCHEME_UNKNOWN) { /* guess scheme from context */
if ((it->got_host) && /* host provided */
(url_host(it)[0] == '2'))
it->scheme = URL_SCHEME_UDP;
if ((!it->got_host) && /* no host provided */
(it->got_path) && /* path provided */
(
(url_path(it)[0] == '/') ||
(url_path(it)[0] == '.')
))
it->scheme = URL_SCHEME_FILE;
}
if ((it->scheme == URL_SCHEME_HTTP) &&
(!it->port))
it->port = URL_DEFAULT_HTTP_PORT;
if ((it->scheme == URL_SCHEME_HTTPS) &&
(!it->port))
it->port = URL_DEFAULT_HTTPS_PORT;
if ((it->scheme == URL_SCHEME_SSH) &&
(!it->port))
it->port = URL_DEFAULT_SSH_PORT;
if ((it->scheme == URL_SCHEME_UDP) &&
(it->got_host)) {
// must-compile
if (!regcomp(®, URL_REG_PATTERN_UDP_MCAST_GROUP, REG_EXTENDED|REG_ICASE)) {
// must-exec
if (!regexec(®, url_host(it), (size_t)0, NULL, 0))
it->flags |= URL_FLAG_MULTICAST;
regfree(®);
}
}
if (it->got_path) {
cursor = (char*)url_path(it);
token = strrchr(cursor, URL_PATH_EXT_START);
if ((token) &&
(strlen(token) != 1)) /* /foo/bar/buz. */ {
bufsz = token-cursor;
it->got_ext = 1;
it->pos_ext = it->pos_path + (uint16_t)bufsz + 1;
}
}
}
}
const char* url_user_info(URL *it) { return it->got_user_info ? &it->buf[it->pos_userinfo] : ""; }
const char* url_host(URL *it) { return it->got_host ? &it->buf[it->pos_host] : ""; }
const char* url_path(URL *it) { return it->got_path ? &it->buf[it->pos_path] : ""; }
const char* url_ext(URL *it) { return it->got_ext ? &it->buf[it->pos_ext] : ""; }
const char* url_query(URL *it) { return it->got_query ? &it->buf[it->pos_query] : ""; }
const char* url_fragment(URL *it) { return it->got_fragment ? &it->buf[it->pos_fragment] : ""; }
const URLScheme url_scheme(URL *it) { return it->scheme; }
const URLProtocol url_protocol(URL *it) { return (URLProtocol)it->scheme; }
void url_sprint(URL *it, char *buf, size_t bufsz) {
snprintf(buf+strlen(buf), bufsz-strlen(buf), "%s%s", url_scheme_string(it->scheme), URL_SEPARATOR_SCHEME);
if (it->got_user_info)
snprintf(buf+strlen(buf), bufsz-strlen(buf), "%s%c", url_user_info(it), URL_SEPARATOR_USERINFO);
if (it->got_host)
snprintf(buf+strlen(buf), bufsz-strlen(buf), "%s", url_host(it));
if (it->port)
snprintf(buf+strlen(buf), bufsz-strlen(buf), "%c%d", URL_SEPARATOR_PORT, it->port);
if (it->got_path)
snprintf(buf+strlen(buf), bufsz-strlen(buf), "%s", url_path(it));
if (it->got_query)
snprintf(buf+strlen(buf), bufsz-strlen(buf), "%c%s", URL_SEPARATOR_QUERY, url_query(it));
if (it->got_fragment)
snprintf(buf+strlen(buf), bufsz-strlen(buf), "%c%s", URL_SEPARATOR_FRAGMENT, url_fragment(it));
}
void url_sprint_json(URL *it, char *buf, size_t bufsz) {
snprintf(buf, bufsz, "{"
"\"scheme\": \"%s\""
", \"userinfo\": \"%s\""
", \"host\": \"%s\""
", \"port\": %d"
", \"path\": \"%s\""
", \"ext\": \"%s\""
", \"query\": \"%s\""
", \"fragment\": \"%s\""
", \"multicast?\": %d"
"}",
url_scheme_string(it->scheme),
url_user_info(it),
url_host(it),
it->port,
url_path(it),
url_ext(it),
url_query(it),
url_fragment(it),
it->flags & URL_FLAG_MULTICAST
);
}
URLScheme url_scheme_parse(char *buf, size_t bufsz) {
int i = 0;
char *cursor = buf;
for (i = 0; i < bufsz; i++) {
*cursor = tolower(*cursor);
cursor++;
}
if (!strncmp(buf, "udp", bufsz)) {
return URL_SCHEME_UDP;
} else if (!strncmp(buf, "rtmp", bufsz)) {
return URL_SCHEME_RTMP;
} else if (!strncmp(buf, "http", bufsz)) {
return URL_SCHEME_HTTP;
} else if (!strncmp(buf, "https", bufsz)) {
return URL_SCHEME_HTTPS;
} else if (!strncmp(buf, "ws", bufsz)) {
return URL_SCHEME_WS;
} else if (!strncmp(buf, "wss", bufsz)) {
return URL_SCHEME_WSS;
} else if (!strncmp(buf, "rtp", bufsz)) {
return URL_SCHEME_RTP;
} else if (!strncmp(buf, "file", bufsz)) {
return URL_SCHEME_FILE;
} else if (!strncmp(buf, "ssh", bufsz)) {
return URL_SCHEME_SSH;
}
return URL_SCHEME_UNKNOWN;
}
const char *url_scheme_descr(URLScheme it) {
switch (it) {
case URL_SCHEME_UDP: return URL_SCHEME_UDP_DESCR;
case URL_SCHEME_RTMP: return URL_SCHEME_RTMP_DESCR;
case URL_SCHEME_HTTP: return URL_SCHEME_HTTP_DESCR;
case URL_SCHEME_HTTPS: return URL_SCHEME_HTTPS_DESCR;
case URL_SCHEME_WS: return URL_SCHEME_WS_DESCR;
case URL_SCHEME_WSS: return URL_SCHEME_WSS_DESCR;
case URL_SCHEME_RTP: return URL_SCHEME_RTP_DESCR;
case URL_SCHEME_FILE: return URL_SCHEME_FILE_DESCR;
case URL_SCHEME_SSH: return URL_SCHEME_SSH_DESCR;
case URL_SCHEME_UNKNOWN: return URL_SCHEME_UNKNOWN_DESCR;
}
return URL_SCHEME_UNKNOWN_DESCR;
}
const char *url_scheme_string(URLScheme it) {
switch (it) {
case URL_SCHEME_UDP: return URL_SCHEME_UDP_STR;
case URL_SCHEME_RTMP: return URL_SCHEME_RTMP_STR;
case URL_SCHEME_HTTP: return URL_SCHEME_HTTP_STR;
case URL_SCHEME_HTTPS: return URL_SCHEME_HTTPS_STR;
case URL_SCHEME_WS: return URL_SCHEME_WS_STR;
case URL_SCHEME_WSS: return URL_SCHEME_WSS_STR;
case URL_SCHEME_RTP: return URL_SCHEME_RTP_STR;
case URL_SCHEME_FILE: return URL_SCHEME_FILE_STR;
case URL_SCHEME_SSH: return URL_SCHEME_SSH_STR;
case URL_SCHEME_UNKNOWN: return URL_SCHEME_UNKNOWN_STR;
}
return URL_SCHEME_UNKNOWN_STR;
}