1 |
/* |
2 |
http-url.c - URL parsing routines |
3 |
Copyright (C) 2008 siliconforks.com |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include <config.h> |
21 |
|
22 |
#include "http-server.h" |
23 |
|
24 |
#include <string.h> |
25 |
|
26 |
#include "util.h" |
27 |
|
28 |
int URL_parse_host_and_port(const char * s, char ** host, uint16_t * port) { |
29 |
char * colon = strchr(s, ':'); |
30 |
if (colon == NULL) { |
31 |
*host = xstrdup(s); |
32 |
*port = 80; |
33 |
} |
34 |
else { |
35 |
if (*(colon + 1) == '\0') { |
36 |
*port = 80; |
37 |
} |
38 |
else { |
39 |
char * end; |
40 |
unsigned long p = strtoul(colon + 1, &end, 10); |
41 |
if (*end == '\0') { |
42 |
if (p > UINT16_MAX) { |
43 |
return -1; |
44 |
} |
45 |
else { |
46 |
*port = p; |
47 |
} |
48 |
} |
49 |
else { |
50 |
return -1; |
51 |
} |
52 |
} |
53 |
*host = xstrndup(s, colon - s); |
54 |
} |
55 |
return 0; |
56 |
} |
57 |
|
58 |
int URL_parse_abs_path_and_query(const char * s, char ** abs_path, char ** query) { |
59 |
if (*s == '\0') { |
60 |
*abs_path = xstrdup("/"); |
61 |
*query = NULL; |
62 |
} |
63 |
else if (*s == '?') { |
64 |
*abs_path = xstrdup("/"); |
65 |
*query = xstrdup(s + 1); |
66 |
} |
67 |
else if (*s == '/') { |
68 |
char * question = strchr(s, '?'); |
69 |
if (question == NULL) { |
70 |
*abs_path = xstrdup(s); |
71 |
*query = NULL; |
72 |
} |
73 |
else { |
74 |
*abs_path = xstrndup(s, question - s); |
75 |
*query = xstrdup(question + 1); |
76 |
} |
77 |
} |
78 |
else { |
79 |
return -1; |
80 |
} |
81 |
return 0; |
82 |
} |
83 |
|
84 |
|
85 |
int URL_parse(const char * url, char ** host, uint16_t * port, char ** abs_path, char ** query) { |
86 |
int result; |
87 |
if (strncasecmp(url, "http://", 7) == 0) { |
88 |
/* absoluteURI */ |
89 |
|
90 |
/* advance past the http:// */ |
91 |
const char * authority_start = url + 7; |
92 |
|
93 |
/* look for a slash or question mark */ |
94 |
const char * p; |
95 |
for (p = authority_start; *p != '/' && *p != '?' && *p != '\0'; p++) { |
96 |
; |
97 |
} |
98 |
|
99 |
char * host_and_port = xstrndup(authority_start, p - authority_start); |
100 |
result = URL_parse_host_and_port(host_and_port, host, port); |
101 |
free(host_and_port); |
102 |
if (result != 0) { |
103 |
return result; |
104 |
} |
105 |
|
106 |
result = URL_parse_abs_path_and_query(p, abs_path, query); |
107 |
if (result != 0) { |
108 |
free(*host); |
109 |
*host = NULL; |
110 |
return result; |
111 |
} |
112 |
} |
113 |
else { |
114 |
/* abs_path */ |
115 |
*host = NULL; |
116 |
*port = 80; |
117 |
result = URL_parse_abs_path_and_query(url, abs_path, query); |
118 |
if (result != 0) { |
119 |
return result; |
120 |
} |
121 |
} |
122 |
result = 0; |
123 |
return result; |
124 |
} |