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 | ||
05b75f8d | 24 | #include "common.h" |
36bde473 | 25 | |
36bde473 | 26 | #include <errno.h> |
27 | ||
28 | #include "mem.h" | |
29 | #include "log.h" | |
30 | #include "printf.h" | |
31 | #include "url.h" | |
32 | #include "kvp.h" | |
33 | ||
34 | /** @brief Infer the for the web interface | |
cc5b0a8e | 35 | * @param include_path_info 1 to include post-script path, else 0 |
36bde473 | 36 | * @return Inferred URL |
37 | * | |
38 | * See <a href="http://tools.ietf.org/html/rfc3875">RFC 3875</a>. | |
39 | */ | |
cc5b0a8e RK |
40 | char *infer_url(int include_path_info) { |
41 | const char *scheme = "http", *server, *script, *e, *request_uri, *path_info; | |
36bde473 | 42 | char *url; |
43 | int port; | |
3c35e8a0 RK |
44 | |
45 | /* mod_ssl sets HTTPS=on if the scheme is https */ | |
46 | if((e = getenv("HTTPS")) && !strcmp(e, "on")) | |
47 | scheme = "https"; | |
36bde473 | 48 | |
49 | /* Figure out the server. 'MUST' be set and we don't cope if it | |
50 | * is not. */ | |
51 | if(!(server = getenv("SERVER_NAME"))) | |
52 | fatal(0, "SERVER_NAME is not set"); | |
53 | server = xstrdup(server); | |
54 | ||
55 | /* Figure out the port. 'MUST' be set but we cope if it is not. */ | |
56 | if((e = getenv("SERVER_PORT"))) | |
57 | port = atoi(e); | |
58 | else | |
59 | port = 80; | |
60 | ||
0688055d | 61 | /* Figure out path to ourselves. */ |
cc5b0a8e | 62 | if(include_path_info && (request_uri = getenv("REQUEST_URI"))) { |
0688055d | 63 | /* REQUEST_URI is an Apache extexnsion. If it's available it results in |
64 | * more accurate self-referencing URLs. */ | |
65 | if((e = strchr(request_uri, '?'))) | |
66 | script = xstrndup(request_uri, e - request_uri); | |
67 | else | |
68 | script = xstrdup(request_uri); | |
69 | } else { | |
70 | /* RFC3875 s4.1.13 */ | |
71 | if(!(script = getenv("SCRIPT_NAME"))) | |
72 | fatal(0, "SCRIPT_NAME is not set"); | |
73 | /* SCRIPT_NAME may be "" */ | |
74 | if(!*script) | |
75 | script = "/"; | |
76 | /* SCRIPT_NAME is not URL-encoded */ | |
77 | script = urlencodestring(script); | |
cc5b0a8e RK |
78 | if(include_path_info && (path_info = getenv("PATH_INFO"))) |
79 | byte_xasprintf((char **)&script, "%s%s", | |
80 | script, urlencodestring(path_info)); | |
0688055d | 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 | */ |