Commit | Line | Data |
---|---|---|
36bde473 | 1 | /* |
2 | * This file is part of DisOrder | |
5aff007d | 3 | * Copyright (C) 2007, 2008 Richard Kettlewell |
36bde473 | 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, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
20 | /** @file lib/url.c | |
21 | * @brief URL support functions | |
22 | */ | |
23 | ||
24 | #include <config.h> | |
25 | #include "types.h" | |
26 | ||
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <errno.h> | |
0688055d | 30 | #include <string.h> |
36bde473 | 31 | |
32 | #include "mem.h" | |
33 | #include "log.h" | |
34 | #include "printf.h" | |
35 | #include "url.h" | |
36 | #include "kvp.h" | |
37 | ||
38 | /** @brief Infer the for the web interface | |
39 | * @return Inferred URL | |
40 | * | |
41 | * See <a href="http://tools.ietf.org/html/rfc3875">RFC 3875</a>. | |
42 | */ | |
43 | char *infer_url(void) { | |
0688055d | 44 | const char *scheme = "http", *server, *script, *e, *request_uri; |
36bde473 | 45 | char *url; |
46 | int port; | |
3c35e8a0 RK |
47 | |
48 | /* mod_ssl sets HTTPS=on if the scheme is https */ | |
49 | if((e = getenv("HTTPS")) && !strcmp(e, "on")) | |
50 | scheme = "https"; | |
36bde473 | 51 | |
52 | /* Figure out the server. 'MUST' be set and we don't cope if it | |
53 | * is not. */ | |
54 | if(!(server = getenv("SERVER_NAME"))) | |
55 | fatal(0, "SERVER_NAME is not set"); | |
56 | server = xstrdup(server); | |
57 | ||
58 | /* Figure out the port. 'MUST' be set but we cope if it is not. */ | |
59 | if((e = getenv("SERVER_PORT"))) | |
60 | port = atoi(e); | |
61 | else | |
62 | port = 80; | |
63 | ||
0688055d | 64 | /* Figure out path to ourselves. */ |
65 | if((request_uri = getenv("REQUEST_URI"))) { | |
66 | /* REQUEST_URI is an Apache extexnsion. If it's available it results in | |
67 | * more accurate self-referencing URLs. */ | |
68 | if((e = strchr(request_uri, '?'))) | |
69 | script = xstrndup(request_uri, e - request_uri); | |
70 | else | |
71 | script = xstrdup(request_uri); | |
72 | } else { | |
73 | /* RFC3875 s4.1.13 */ | |
74 | if(!(script = getenv("SCRIPT_NAME"))) | |
75 | fatal(0, "SCRIPT_NAME is not set"); | |
76 | /* SCRIPT_NAME may be "" */ | |
77 | if(!*script) | |
78 | script = "/"; | |
79 | /* SCRIPT_NAME is not URL-encoded */ | |
80 | script = urlencodestring(script); | |
81 | } | |
36bde473 | 82 | if(script[0] != '/') |
83 | fatal(0, "SCRIPT_NAME does not start with a '/'"); | |
84 | script = xstrdup(script); | |
85 | ||
86 | if(port == 80) | |
87 | byte_xasprintf(&url, "%s://%s%s", | |
88 | scheme, server, script); | |
89 | else | |
90 | byte_xasprintf(&url, "%s://%s:%d%s", | |
91 | scheme, server, port, script); | |
92 | return url; | |
93 | } | |
94 | ||
95 | /** @brief Parse a URL | |
96 | * @param url URL to parsed | |
97 | * @param parsed Where to store parsed URL data | |
98 | * @return 0 on success, non-0 on error | |
99 | * | |
100 | * NB that URLs with usernames and passwords are NOT currently supported. | |
101 | */ | |
102 | int parse_url(const char *url, struct url *parsed) { | |
103 | const char *s; | |
104 | long n; | |
105 | ||
106 | /* The scheme */ | |
107 | for(s = url; *s && *s != '/' && *s != ':'; ++s) | |
108 | ; | |
109 | if(*s == ':') { | |
110 | parsed->scheme = xstrndup(url, s - url); | |
111 | url = s + 1; | |
112 | } else | |
113 | parsed->scheme = 0; | |
114 | ||
115 | /* The host and port */ | |
116 | if(*url == '/' && url[1] == '/') { | |
117 | /* //user:password@host:port, but we don't support the | |
118 | * user:password@ part. */ | |
119 | url += 2; | |
120 | for(s = url; *s && *s != '/' && *s != ':'; ++s) | |
121 | ; | |
122 | parsed->host = xstrndup(url, s - url); | |
123 | if(*s == ':') { | |
124 | /* We have host:port[/...] */ | |
125 | ++s; | |
126 | errno = 0; | |
127 | n = strtol(s, (char **)&url, 10); | |
128 | if(errno) | |
129 | return -1; | |
130 | if(n < 0 || n > 65535) | |
131 | return -1; | |
132 | parsed->port = n; | |
133 | } else { | |
134 | /* We just have host[/...] */ | |
135 | url = s; | |
136 | parsed->port = -1; | |
137 | } | |
138 | } | |
139 | ||
140 | /* The path */ | |
141 | for(s = url; *s && *s != '?'; ++s) | |
142 | ; | |
143 | if(!(parsed->path = urldecodestring(url, s - url))) | |
144 | return -1; | |
145 | url = s; | |
146 | ||
147 | /* The query */ | |
148 | if(*url == '?') | |
149 | parsed->query = xstrdup(url + 1); | |
150 | else | |
151 | parsed->query = 0; | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
156 | /* | |
157 | Local Variables: | |
158 | c-basic-offset:2 | |
159 | comment-column:40 | |
160 | fill-column:79 | |
161 | indent-tabs-mode:nil | |
162 | End: | |
163 | */ |