Repository navigation
Expand file tree
/
Copy pathurlparser.c
More file actions
125 lines (104 loc) · 2.96 KB
/
Copy pathurlparser.c
File metadata and controls
125 lines (104 loc) · 2.96 KB
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
#ifdef __ORCAC__
# pragma noroot
#endif
#include "urlparser.h"
#include <string.h>
/*
* Parse the URL and break it up into its component parts.
*
* This handles http URLs and other schemes with the same syntax.
*
* It can also handle relative URLs containing an absolute-path reference
* (i.e. starting with /) and URL-like addresses without the scheme prefix.
* In these cases, the portions of the URL not provided will be NULL.
*
* This modifies the original string passed in, breaking it up into components.
*/
URLParts ParseURL(char *url) {
char *sep, sep2;
URLParts urlParts = {0};
sep = strrchr(url, '#');
if (sep) {
*sep = '\0';
urlParts.fragment = sep + 1;
}
/* Detect relative URL (absolute-path reference only) */
if (url[0] == '/') {
if (url[1] != '/') {
urlParts.path = url + 1;
return urlParts;
} else {
urlParts.errorFound = 1;
return urlParts;
}
}
sep = strchr(url, ':');
if (sep) {
urlParts.scheme = url;
*sep = '\0';
url = sep + 1;
}
if (urlParts.scheme != NULL) {
if (strncmp(url, "//", 2) == 0) {
url += 2;
} else {
urlParts.errorFound = 1;
return urlParts;
}
}
urlParts.path = strchr(url, '/');
if (urlParts.path == NULL) {
urlParts.path = "";
} else {
*urlParts.path = '\0';
urlParts.path++;
}
sep = strchr(url, '@');
if (sep) {
*sep = '\0';
urlParts.username = url;
urlParts.password = strchr(url, ':');
if (urlParts.password != NULL) {
*urlParts.password = '\0';
urlParts.password++;
}
url = sep + 1;
}
urlParts.host = url;
/* Handle IPv6 address syntax */
if (*url == '[') {
sep = strchr(url, ']');
if (sep) {
url = sep + 1;
} else {
urlParts.errorFound = 1;
return urlParts;
}
}
sep = strchr(url, ':');
if (sep) {
*sep = '\0';
urlParts.port = sep + 1;
}
return urlParts;
}
#ifdef URLPARSER_TEST
#include <stdio.h>
int main(int argc, char **argv)
{
URLParts urlParts;
if (argc < 2)
return 1;
urlParts = ParseURL(argv[1]);
printf("scheme: %s\n", urlParts.scheme ? urlParts.scheme : "(NULL)");
printf("username: %s\n", urlParts.username ? urlParts.username : "(NULL)");
printf("password: %s\n", urlParts.password ? urlParts.password : "(NULL)");
printf("host: %s\n", urlParts.host ? urlParts.host : "(NULL)");
printf("port: %s\n", urlParts.port ? urlParts.port : "(NULL)");
printf("path: %s\n", urlParts.path ? urlParts.path : "(NULL)");
printf("fragment: %s\n", urlParts.fragment ? urlParts.fragment : "(NULL)");
if (urlParts.errorFound) {
printf("Error found\n");
}
}
#endif