/[jscoverage]/trunk/http-url.c
ViewVC logotype

Annotation of /trunk/http-url.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 505 - (hide annotations)
Sat Jan 2 04:55:54 2010 UTC (9 years, 8 months ago) by siliconforks
File MIME type: text/plain
File size: 3179 byte(s)
Update copyright year.

1 siliconforks 116 /*
2     http-url.c - URL parsing routines
3 siliconforks 505 Copyright (C) 2008, 2009, 2010 siliconforks.com
4 siliconforks 116
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 siliconforks 254 #include <ctype.h>
25 siliconforks 116 #include <string.h>
26    
27     #include "util.h"
28    
29     int URL_parse_host_and_port(const char * s, char ** host, uint16_t * port) {
30     char * colon = strchr(s, ':');
31     if (colon == NULL) {
32     *host = xstrdup(s);
33     *port = 80;
34     }
35     else {
36     if (*(colon + 1) == '\0') {
37     *port = 80;
38     }
39     else {
40     char * end;
41     unsigned long p = strtoul(colon + 1, &end, 10);
42     if (*end == '\0') {
43     if (p > UINT16_MAX) {
44     return -1;
45     }
46     else {
47     *port = p;
48     }
49     }
50     else {
51     return -1;
52     }
53     }
54     *host = xstrndup(s, colon - s);
55     }
56     return 0;
57     }
58    
59     int URL_parse_abs_path_and_query(const char * s, char ** abs_path, char ** query) {
60     if (*s == '\0') {
61     *abs_path = xstrdup("/");
62     *query = NULL;
63     }
64     else if (*s == '?') {
65     *abs_path = xstrdup("/");
66     *query = xstrdup(s + 1);
67     }
68     else if (*s == '/') {
69     char * question = strchr(s, '?');
70     if (question == NULL) {
71     *abs_path = xstrdup(s);
72     *query = NULL;
73     }
74     else {
75     *abs_path = xstrndup(s, question - s);
76     *query = xstrdup(question + 1);
77     }
78     }
79     else {
80     return -1;
81     }
82     return 0;
83     }
84    
85 siliconforks 254 int URL_parse(const char * url, char ** host, uint16_t * port, char ** abs_path, char ** query) {
86     /* check for invalid characters */
87     for (const char * p = url; *p != '\0'; p++) {
88 siliconforks 456 if (*p <= 32 || *p >= 127) {
89     return -1;
90 siliconforks 254 }
91     }
92 siliconforks 116
93     int result;
94     if (strncasecmp(url, "http://", 7) == 0) {
95     /* absoluteURI */
96    
97     /* advance past the http:// */
98     const char * authority_start = url + 7;
99    
100     /* look for a slash or question mark */
101     const char * p;
102     for (p = authority_start; *p != '/' && *p != '?' && *p != '\0'; p++) {
103     ;
104     }
105    
106     char * host_and_port = xstrndup(authority_start, p - authority_start);
107     result = URL_parse_host_and_port(host_and_port, host, port);
108     free(host_and_port);
109     if (result != 0) {
110     return result;
111     }
112    
113     result = URL_parse_abs_path_and_query(p, abs_path, query);
114     if (result != 0) {
115     free(*host);
116     *host = NULL;
117     return result;
118     }
119     }
120     else {
121     /* abs_path */
122     *host = NULL;
123     *port = 80;
124     result = URL_parse_abs_path_and_query(url, abs_path, query);
125     if (result != 0) {
126     return result;
127     }
128     }
129     result = 0;
130     return result;
131     }

  ViewVC Help
Powered by ViewVC 1.1.24