This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
http-url.c
156 lines (141 loc) · 3.43 KB
/
http-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
/*
http-url.c - URL parsing routines
Copyright (C) 2008 siliconforks.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include "http-server.h"
#include <ctype.h>
#include <string.h>
#include "util.h"
int URL_parse_host_and_port(const char * s, char ** host, uint16_t * port) {
char * colon = strchr(s, ':');
if (colon == NULL) {
*host = xstrdup(s);
*port = 80;
}
else {
if (*(colon + 1) == '\0') {
*port = 80;
}
else {
char * end;
unsigned long p = strtoul(colon + 1, &end, 10);
if (*end == '\0') {
if (p > UINT16_MAX) {
return -1;
}
else {
*port = p;
}
}
else {
return -1;
}
}
*host = xstrndup(s, colon - s);
}
return 0;
}
int URL_parse_abs_path_and_query(const char * s, char ** abs_path, char ** query) {
if (*s == '\0') {
*abs_path = xstrdup("/");
*query = NULL;
}
else if (*s == '?') {
*abs_path = xstrdup("/");
*query = xstrdup(s + 1);
}
else if (*s == '/') {
char * question = strchr(s, '?');
if (question == NULL) {
*abs_path = xstrdup(s);
*query = NULL;
}
else {
*abs_path = xstrndup(s, question - s);
*query = xstrdup(question + 1);
}
}
else {
return -1;
}
return 0;
}
int URL_parse(const char * url, char ** host, uint16_t * port, char ** abs_path, char ** query) {
/* check for invalid characters */
for (const char * p = url; *p != '\0'; p++) {
switch (*p) {
case ';':
case '/':
case '?':
case ':':
case '@':
case '&':
case '=':
case '+':
case '$':
case ',':
case '-':
case '_':
case '.':
case '!':
case '~':
case '*':
case '\'':
case '(':
case ')':
case '%':
break;
default:
if (! isalnum(*p)) {
return -1;
}
break;
}
}
int result;
if (strncasecmp(url, "http://", 7) == 0) {
/* absoluteURI */
/* advance past the http:// */
const char * authority_start = url + 7;
/* look for a slash or question mark */
const char * p;
for (p = authority_start; *p != '/' && *p != '?' && *p != '\0'; p++) {
;
}
char * host_and_port = xstrndup(authority_start, p - authority_start);
result = URL_parse_host_and_port(host_and_port, host, port);
free(host_and_port);
if (result != 0) {
return result;
}
result = URL_parse_abs_path_and_query(p, abs_path, query);
if (result != 0) {
free(*host);
*host = NULL;
return result;
}
}
else {
/* abs_path */
*host = NULL;
*port = 80;
result = URL_parse_abs_path_and_query(url, abs_path, query);
if (result != 0) {
return result;
}
}
result = 0;
return result;
}