-
Notifications
You must be signed in to change notification settings - Fork 15
/
parse-uri.c
205 lines (180 loc) · 5.71 KB
/
parse-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
/*
strview_t demo to extract scheme (http/ftp/etc..), host, user, path and port from the following URI formats:
The function which does this:
* Is less than 50 lines.
* Requires no buffers.
* Doesn't modify the given uri.
* Doesn't even copy any data from the given uri. (we already HAVE the data, we don't need to copy it).
Possible combinations of URI components:
scheme:
scheme:path
scheme:path/path
scheme://host
scheme://host/path
scheme://host/path/path
scheme://user@host
scheme://user@host/path
scheme://user@host/path/path
scheme://host:<port>
scheme://host:<port>/path
scheme://host:<port>/path/path
scheme://user@host:<port>
scheme://user@host:<port>/path
scheme://user@host:<port>/path/path
host
host/path
host/path/path
user@host
user@host/path
user@host/path/path
host:<port>
host:<port>/path
host:<port>/path/path
user@host:<port>
user@host:<port>/path
user@host:<port>/path/path
*/
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include "strview.h"
//********************************************************************************************************
// Configurable defines
//********************************************************************************************************
//********************************************************************************************************
// Local defines
//********************************************************************************************************
struct uri_struct
{
strview_t scheme;
strview_t host;
strview_t path;
strview_t user;
strview_t port;
};
//********************************************************************************************************
// Public variables
//********************************************************************************************************
//********************************************************************************************************
// Private variables
//********************************************************************************************************
#define NUMBER_OF_DEMO_STRINGS 27
const char* demo_string[NUMBER_OF_DEMO_STRINGS] =
{
"http:",
"http:home",
"http:home/fred",
"http://github.com",
"http://github.com/path",
"http://github.com/home/fred",
"http://[email protected]",
"http://[email protected]/path",
"http://[email protected]/home/fred",
"http://github.com:80",
"http://github.com:80/path",
"http://github.com:80/home/fred",
"http://[email protected]:80",
"http://[email protected]:80/path",
"http://[email protected]:80/home/fred",
"github.com",
"github.com/path",
"github.com/home/fred",
"[email protected]/path",
"[email protected]/home/fred",
"github.com:80",
"github.com:80/path",
"github.com:80/home/fred",
"[email protected]:80",
"[email protected]:80/path",
"[email protected]:80/home/fred"
};
//********************************************************************************************************
// Private prototypes
//********************************************************************************************************
static struct uri_struct parse_uri(strview_t uri);
static void show_uri(struct uri_struct uri);
//********************************************************************************************************
// Public functions
//********************************************************************************************************
int main(int argc, const char* argv[])
{
int i = 0;
if(argc > 1)
show_uri(parse_uri(cstr(argv[1])));
else
{
while(i != NUMBER_OF_DEMO_STRINGS)
{
printf("[%s]\n", demo_string[i]);
show_uri(parse_uri(cstr(demo_string[i])));
i++;
};
printf("To run with a custom string use %s <string>\n", argv[0]);
};
return 0;
}
//********************************************************************************************************
// Private functions
//********************************************************************************************************
static struct uri_struct parse_uri(strview_t uri)
{
struct uri_struct result = {0};
strview_t tmp;
if(strview_is_valid(strview_find_first(uri, "://")))
{
result.scheme = strview_split_first_delim(&uri, ":");
uri = strview_sub(uri, 2, INT_MAX);
};
result.user = strview_split_first_delim(&uri, "@");
if(!strview_is_valid(uri))
strview_swap(&result.user, &uri);
if(!strview_is_valid(strview_find_first(uri, ":")))
{
tmp = strview_split_first_delim(&uri, "/");
if(strview_is_valid(uri))
{
result.path = uri;
result.host = tmp;
}
else
result.host = tmp;
}
else
{
tmp = strview_split_first_delim(&uri, ":");
if(!strview_is_valid(uri))
strview_swap(&tmp, &uri);
if(uri.size && isdigit(uri.data[0]))
{
result.host = tmp;
tmp = strview_split_first_delim(&uri, "/");
result.port = tmp;
if(strview_is_valid(uri))
result.path = uri;
}
else
{
result.scheme = tmp;
if(uri.size)
result.path = uri;
};
};
return result;
}
static void show_uri(struct uri_struct uri)
{
if(strview_is_valid(uri.scheme))
printf("scheme = \"%"PRIstr"\"\n", PRIstrarg(uri.scheme));
if(strview_is_valid(uri.host))
printf("host = \"%"PRIstr"\"\n", PRIstrarg(uri.host));
if(strview_is_valid(uri.path))
printf("path = \"%"PRIstr"\"\n", PRIstrarg(uri.path));
if(strview_is_valid(uri.user))
printf("user = \"%"PRIstr"\"\n", PRIstrarg(uri.user));
if(strview_is_valid(uri.port))
printf("port = \"%"PRIstr"\"\n", PRIstrarg(uri.port));
printf("\n");
}