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 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
19 * @brief URL support functions
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
36 * See <a href="http://tools.ietf.org/html/rfc3875">RFC 3875</a>.
38 char *infer_url(int include_path_info
) {
39 const char *scheme
= "http", *server
, *script
, *e
, *request_uri
, *path_info
;
43 /* mod_ssl sets HTTPS=on if the scheme is https */
44 if((e
= getenv("HTTPS")) && !strcmp(e
, "on"))
47 /* Figure out the server. 'MUST' be set and we don't cope if it
49 if(!(server
= getenv("SERVER_NAME")))
50 disorder_fatal(0, "SERVER_NAME is not set");
51 server
= xstrdup(server
);
53 /* Figure out the port. 'MUST' be set but we cope if it is not. */
54 if((e
= getenv("SERVER_PORT")))
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
);
66 script
= xstrdup(request_uri
);
69 if(!(script
= getenv("SCRIPT_NAME")))
70 disorder_fatal(0, "SCRIPT_NAME is not set");
71 /* SCRIPT_NAME may be "" */
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
));
81 disorder_fatal(0, "SCRIPT_NAME does not start with a '/'");
82 script
= xstrdup(script
);
85 byte_xasprintf(&url
, "%s://%s%s",
86 scheme
, server
, script
);
88 byte_xasprintf(&url
, "%s://%s:%d%s",
89 scheme
, server
, port
, script
);
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
98 * NB that URLs with usernames and passwords are NOT currently supported.
100 int parse_url(const char *url
, struct url
*parsed
) {
105 for(s
= url
; *s
&& *s
!= '/' && *s
!= ':'; ++s
)
108 parsed
->scheme
= xstrndup(url
, s
- url
);
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. */
118 for(s
= url
; *s
&& *s
!= '/' && *s
!= ':'; ++s
)
120 parsed
->host
= xstrndup(url
, s
- url
);
122 /* We have host:port[/...] */
125 n
= strtol(s
, (char **)&url
, 10);
128 if(n
< 0 || n
> 65535)
132 /* We just have host[/...] */
139 for(s
= url
; *s
&& *s
!= '?'; ++s
)
141 if(!(parsed
->path
= urldecodestring(url
, s
- url
)))
147 parsed
->query
= xstrdup(url
+ 1);