-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.c
204 lines (171 loc) · 3.86 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
#include "url.h"
static void
_parse_url_scheme(const char *link, char **scheme)
{
size_t schemelen;
const char *p;
p = strstr(link, "://");
if (p == NULL) {
fprintf(stderr, "Invalid URL.\n");
exit(EXIT_FAILURE);
} else {
schemelen = p - link;
*scheme = (char *)realloc(*scheme, sizeof(char) * (schemelen + 1));
if (*scheme == NULL) {
fprintf(stderr, "Unable to allocate scheme for URL.\n");
exit(EXIT_FAILURE);
}
memcpy(*scheme, link, schemelen);
(*scheme)[schemelen] = '\0';
if (strncmp(*scheme, "gemini", schemelen) != 0) {
fprintf(stderr, "Invalid URL.\n");
exit(EXIT_FAILURE);
}
}
}
static void
_parse_url_authority(const char *link, char **authority)
{
size_t authoritylen;
const char *p;
const char *q;
p = strstr(link, "://");
if (p == NULL) {
fprintf(stderr, "Invalid URL.\n");
exit(EXIT_FAILURE);
} else {
/* Skip past "://" to beginning of authority. */
p += 3;
}
q = strchr(p, '/');
if (q == NULL) {
q = &link[strlen(link)];
}
authoritylen = q - p;
*authority = (char *)realloc(*authority, sizeof(char) * (authoritylen + 1));
if (*authority == NULL) {
fprintf(stderr, "Unable to allocate authority for URL.\n");
exit(EXIT_FAILURE);
}
memcpy(*authority, p, authoritylen);
(*authority)[authoritylen] = '\0';
}
static void
_parse_url_path(const char *link, char **path)
{
size_t pathlen;
const char *p;
const char *q;
p = strchr(strstr(link, "://") + 3, '/');
if (p == NULL) {
/* No path provided after authority. */
*path = strdup("/");
return;
}
q = strchr(link, '?');
if (q == NULL) {
/* Remainder of string is path. */
q = &link[strlen(link)];
}
pathlen = q - p;
*path = (char *)realloc(*path, sizeof(char) * (pathlen + 1));
if (*path == NULL) {
fprintf(stderr, "Unable to allocate path for URL.\n");
exit(EXIT_FAILURE);
}
memcpy(*path, p, pathlen);
(*path)[pathlen] = '\0';
}
static void
_parse_url_query(const char *link, char **query)
{
size_t querylen;
const char *p;
p = strchr(link, '?');
if (p == NULL) {
*query = NULL;
return;
}
querylen = strlen(p);
*query = (char *)realloc(*query, sizeof(char *) * (querylen + 1));
if (*query == NULL) {
fprintf(stderr, "Unable to allocate query for URL.\n");
exit(EXIT_FAILURE);
}
memcpy(*query, p, querylen);
(*query)[querylen] = '\0';
}
static char *
_percent_encode_string(char *s)
{
char *encoded;
size_t encodedlen;
size_t n_spaces = 0;
for (size_t i = 0; i < strlen(s); ++i) {
if (s[i] == ' ') {
++n_spaces;
}
}
encodedlen = strlen(s) + (n_spaces * 3);
encoded = (char *)calloc(encodedlen + 1, sizeof(char));
if (encoded == NULL) {
fprintf(stderr, "Unable to allocate query.\n");
exit(EXIT_FAILURE);
}
for (size_t i = 0, j = 0; i < encodedlen; ++i, ++j) {
if (s[j] == ' ') {
i += 2;
strcat(encoded, "%20");
} else {
encoded[i] = s[j];
}
}
encoded[encodedlen] = '\0';
return encoded;
}
struct url *
create_url()
{
struct url *url = malloc(sizeof(struct url));
if (url == NULL) {
fprintf(stderr, "Unable to allocate URL.\n");
exit(EXIT_FAILURE);
}
url->scheme = NULL;
url->authority = NULL;
url->path = NULL;
url->query = NULL;
return url;
}
void
parse_url(const char *link, struct url *url)
{
_parse_url_scheme(link, &url->scheme);
_parse_url_authority(link, &url->authority);
_parse_url_path(link, &url->path);
_parse_url_query(link, &url->query);
}
void
free_url(struct url *url)
{
free(url->scheme);
free(url->authority);
free(url->path);
free(url->query);
free(url);
}
void
append_query(char *s, struct url *url)
{
assert(url->query == NULL);
char *encoded = _percent_encode_string(s);
size_t encodedlen = strlen(encoded);
url->query = (char *)calloc(encodedlen + 1, sizeof(char));
if (url->query == NULL) {
fprintf(stderr, "Unable to allocate query.\n");
exit(EXIT_FAILURE);
}
url->query[0] = '?';
strncat(url->query, encoded, encodedlen - 1);
free(encoded);
}