-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_url.c
More file actions
140 lines (124 loc) · 3.02 KB
/
Copy pathparse_url.c
File metadata and controls
140 lines (124 loc) · 3.02 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include "parse_url.h"
//TODO: user/pass - what's the f. sintax, because i do not remember it...
// free url_parts_t struct on error... more testing pls
// No Regexp, no sscanf :)
url_parts_t* parse_url(char *url) {
char *p, *n;
int port;
url_parts_t *url_parts;
if(!url)
return NULL;
if(! (p = strstr(url, "://"))) {
printf("Invalid scheme in url [%s] - use http://...\n", url);
return NULL;
}
url_parts = (url_parts_t *) calloc(1, sizeof(url_parts_t));
if(url_parts == NULL) {
perror("Out of memory\n");
return NULL;
}
url_parts->scheme = strndup(url, p-url);
p += 3;
n = p;
for(;*n && *n != ':' && *n != '/' && *n != '?' && *n != '#'; n++); // until end, or : or / or ? or #
if(n == p) {
printf("Invalid host - empty host\n");
free(url_parts->scheme);
free(url_parts);
return NULL;
}
url_parts->host = strndup(p, n - p);
if(*n == ':') {
for(p = ++n, port = 0; *n && *n != '/' && *n != '?' && *n != '#'; n++) {
if(*n < '0' || *n > '9') {
free(url_parts->scheme);
free(url_parts->host);
free(url_parts);
printf("Invalid port - should contain only digits\n");
return NULL;
}
port = port * 10 + *n - '0';
if(port > 65535 || port == 0) {
free(url_parts->scheme);
free(url_parts->host);
free(url_parts);
printf("Invalid port - should be between 0 and 65535\n");
return NULL;
}
}
url_parts->port = (unsigned short) port;
}
if(*n == '/') {
for(p = n; *n && *n != '?' && *n != '#'; n++); // until end, or ? or #
url_parts->path = strndup(p, n - p);
p = n;
}
if(*n == '?') {
for(p = ++n; *n && *n != '#'; n++);
url_parts->query_string = strndup(p, n - p);
}
if(*n == '#') {
for(p = ++n; *n; n++);
url_parts->fragment = strndup(p, n - p);
}
if(!url_parts->port) {
//struct servent *serv;
//serv = getservbyname(url_parts->scheme, "tcp");
//printf("[%i]\n", serv->s_port);
if(strcmp(url_parts->scheme, "http") == 0) {
url_parts->port = 80;
}
}
if(!url_parts->path) {
url_parts->path = strdup("/");
}
return url_parts;
}
#if 0
char *make_url(struct url_parts_t url_parts) {
}
#endif
void free_url_parts(url_parts_t *url_parts)
{
free(url_parts->scheme);
free(url_parts->host);
free(url_parts->path);
if(url_parts->user) {
free(url_parts->user);
}
if(url_parts->pass) {
free(url_parts->pass);
}
if(url_parts->query_string) {
free(url_parts->query_string);
}
if(url_parts->fragment) {
free(url_parts->fragment);
}
free(url_parts);
}
url_parts_t *url_parts_dup(url_parts_t *parts)
{
url_parts_t *p;
if(!(p = calloc(1, sizeof(*p)))) {
perror("calloc");
return NULL;
}
p->scheme = strdup(parts->scheme);
p->host = strdup(parts->host);
p->path = strdup(parts->path);
if(parts->user)
p->user = strdup(parts->user);
if(parts->pass)
p->pass = strdup(parts->pass);
if(parts->query_string)
p->query_string = strdup(parts->query_string);
if(parts->fragment)
p->fragment = strdup(parts->fragment);
p->port = parts->port;
return p;
}