From: Richard Kettlewell
ID | +Description | +
---|---|
#21 | +CGI should use PATH_INFO more sensibly | +
Sorry, PATH_INFO not supported.
\n"); + printf("Sorry, is PATH_INFO not supported." + "Try here instead.
\n", + cgi_sgmlquote(infer_url(0/*!include_path_info*/))); exit(0); } /* Parse CGI arguments */ @@ -53,7 +55,7 @@ int main(int argc, char **argv) { /* Figure out our URL. This can still be overridden from the config file if * necessary but it shouldn't be necessary in ordinary installations. */ if(!config->url) - config->url = infer_url(); + config->url = infer_url(1/*include_path_info*/); /* Pick up the cookie, if there is one */ dcgi_get_cookie(); /* Register expansions */ diff --git a/lib/url.c b/lib/url.c index b777282..e05e6bf 100644 --- a/lib/url.c +++ b/lib/url.c @@ -32,12 +32,13 @@ #include "kvp.h" /** @brief Infer the for the web interface + * @param include_path_info 1 to include post-script path, else 0 * @return Inferred URL * * See RFC 3875. */ -char *infer_url(void) { - const char *scheme = "http", *server, *script, *e, *request_uri; +char *infer_url(int include_path_info) { + const char *scheme = "http", *server, *script, *e, *request_uri, *path_info; char *url; int port; @@ -58,7 +59,7 @@ char *infer_url(void) { port = 80; /* Figure out path to ourselves. */ - if((request_uri = getenv("REQUEST_URI"))) { + if(include_path_info && (request_uri = getenv("REQUEST_URI"))) { /* REQUEST_URI is an Apache extexnsion. If it's available it results in * more accurate self-referencing URLs. */ if((e = strchr(request_uri, '?'))) @@ -74,6 +75,9 @@ char *infer_url(void) { script = "/"; /* SCRIPT_NAME is not URL-encoded */ script = urlencodestring(script); + if(include_path_info && (path_info = getenv("PATH_INFO"))) + byte_xasprintf((char **)&script, "%s%s", + script, urlencodestring(path_info)); } if(script[0] != '/') fatal(0, "SCRIPT_NAME does not start with a '/'"); diff --git a/lib/url.h b/lib/url.h index 3331456..3546e77 100644 --- a/lib/url.h +++ b/lib/url.h @@ -68,7 +68,7 @@ struct url { char *query; }; -char *infer_url(void); +char *infer_url(int include_path_info); int parse_url(const char *url, struct url *parsed); #endif /* URL_H */ diff --git a/libtests/t-url.c b/libtests/t-url.c index dcff4bf..4bc7474 100644 --- a/libtests/t-url.c +++ b/libtests/t-url.c @@ -50,22 +50,22 @@ static void test_url(void) { setenv("SERVER_NAME", "www.anjou.terraraq.org.uk", 1); setenv("SERVER_PORT", "80", 1); setenv("SCRIPT_NAME", "/~richard/env.cgi", 1); - check_string(infer_url(), + check_string(infer_url(1), "http://www.anjou.terraraq.org.uk/~richard/env.cgi"); setenv("HTTPS", "on", 1); - check_string(infer_url(), + check_string(infer_url(1), "https://www.anjou.terraraq.org.uk/~richard/env.cgi"); setenv("QUERY_STRING", "foo", 1); - check_string(infer_url(), + check_string(infer_url(1), "https://www.anjou.terraraq.org.uk/~richard/env.cgi"); setenv("REQUEST_URI", "/~richard/env%2ecgi", 1); - check_string(infer_url(), + check_string(infer_url(1), "https://www.anjou.terraraq.org.uk/~richard/env%2ecgi"); setenv("REQUEST_URI", "/~richard/env%2ecgi?foo", 1); - check_string(infer_url(), + check_string(infer_url(1), "https://www.anjou.terraraq.org.uk/~richard/env%2ecgi"); setenv("SERVER_PORT", "8080", 1); - check_string(infer_url(), + check_string(infer_url(1), "https://www.anjou.terraraq.org.uk:8080/~richard/env%2ecgi"); }