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