2 * This file is part of DisOrder
3 * Copyright (C) 2007, 2008 Richard Kettlewell
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.
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.
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
21 * @brief URL support functions
38 /** @brief Infer the for the web interface
39 * @return Inferred URL
41 * See <a href="http://tools.ietf.org/html/rfc3875">RFC 3875</a>.
43 char *infer_url(void) {
44 const char *scheme = "http", *server, *script, *e, *request_uri;
48 /* mod_ssl sets HTTPS=on if the scheme is https */
49 if((e = getenv("HTTPS")) && !strcmp(e, "on"))
52 /* Figure out the server. 'MUST' be set and we don't cope if it
54 if(!(server = getenv("SERVER_NAME")))
55 fatal(0, "SERVER_NAME is not set");
56 server = xstrdup(server);
58 /* Figure out the port. 'MUST' be set but we cope if it is not. */
59 if((e = getenv("SERVER_PORT")))
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);
71 script = xstrdup(request_uri);
74 if(!(script = getenv("SCRIPT_NAME")))
75 fatal(0, "SCRIPT_NAME is not set");
76 /* SCRIPT_NAME may be "" */
79 /* SCRIPT_NAME is not URL-encoded */
80 script = urlencodestring(script);
83 fatal(0, "SCRIPT_NAME does not start with a '/'");
84 script = xstrdup(script);
87 byte_xasprintf(&url, "%s://%s%s",
88 scheme, server, script);
90 byte_xasprintf(&url, "%s://%s:%d%s",
91 scheme, server, port, script);
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
100 * NB that URLs with usernames and passwords are NOT currently supported.
102 int parse_url(const char *url, struct url *parsed) {
107 for(s = url; *s && *s != '/' && *s != ':'; ++s)
110 parsed->scheme = xstrndup(url, s - url);
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. */
120 for(s = url; *s && *s != '/' && *s != ':'; ++s)
122 parsed->host = xstrndup(url, s - url);
124 /* We have host:port[/...] */
127 n = strtol(s, (char **)&url, 10);
130 if(n < 0 || n > 65535)
134 /* We just have host[/...] */
141 for(s = url; *s && *s != '?'; ++s)
143 if(!(parsed->path = urldecodestring(url, s - url)))
149 parsed->query = xstrdup(url + 1);