From: rjk@greenend.org.uk <> Date: Fri, 11 Jan 2008 12:08:55 +0000 (+0000) Subject: Use REQUEST_URI if available, for more consistent self-referring URLs. X-Git-Tag: 3.0~64 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/commitdiff_plain/0688055d6045c4a9916bd7c2b328cc98139905da?ds=sidebyside Use REQUEST_URI if available, for more consistent self-referring URLs. Handle SCRIPT_NAME="", per the RFC. --- diff --git a/lib/url.c b/lib/url.c index 784ee9f..2579ec1 100644 --- a/lib/url.c +++ b/lib/url.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "mem.h" #include "log.h" @@ -40,7 +41,7 @@ * See RFC 3875. */ char *infer_url(void) { - const char *scheme = "http", *server, *script, *e; + const char *scheme = "http", *server, *script, *e, *request_uri; char *url; int port; @@ -56,9 +57,24 @@ char *infer_url(void) { else port = 80; - /* Figure out path to ourselves */ - if(!(script = getenv("SCRIPT_NAME"))) - fatal(0, "SCRIPT_NAME is not set"); + /* Figure out path to ourselves. */ + if((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, '?'))) + script = xstrndup(request_uri, e - request_uri); + else + script = xstrdup(request_uri); + } else { + /* RFC3875 s4.1.13 */ + if(!(script = getenv("SCRIPT_NAME"))) + fatal(0, "SCRIPT_NAME is not set"); + /* SCRIPT_NAME may be "" */ + if(!*script) + script = "/"; + /* SCRIPT_NAME is not URL-encoded */ + script = urlencodestring(script); + } if(script[0] != '/') fatal(0, "SCRIPT_NAME does not start with a '/'"); script = xstrdup(script);