summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Crispin <blogic@openwrt.org>2015-10-07 16:21:10 +0200
committerJohn Crispin <blogic@openwrt.org>2015-10-17 16:47:02 +0200
commit3614ae6eb7b6101fa5d0f14ff0d3a6eb4970c3da (patch)
tree3469adf10cbc166479bb4e27790aa9e5fc331bce
parent7ed2edc40dd6d0171266f3bfbc96466e1d25e3cd (diff)
add a -y parameter for cgi-bin redirects
this allows an alias entry inside the root folder point at a cgi-bin script -y foo=bar will redirect /foo to /cgi-bin/bar Signed-off-by: John Crispin <blogic@openwrt.org>
-rw-r--r--file.c42
-rw-r--r--main.c21
-rw-r--r--uhttpd.h8
3 files changed, 69 insertions, 2 deletions
diff --git a/file.c b/file.c
index 9946d7c..6b5020f 100644
--- a/file.c
+++ b/file.c
@@ -33,6 +33,8 @@
#include "uhttpd.h"
#include "mimetypes.h"
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+
static LIST_HEAD(index_files);
static LIST_HEAD(dispatch_handlers);
static LIST_HEAD(pending_requests);
@@ -815,6 +817,44 @@ static bool __handle_file_request(struct client *cl, char *url)
return true;
}
+static char *uh_handle_alias(char *old_url)
+{
+ struct alias *alias;
+ static char *new_url;
+ static int url_len;
+
+ if (!list_empty(&conf.cgi_alias)) list_for_each_entry(alias, &conf.cgi_alias, list) {
+ int old_len;
+ int new_len;
+ int path_len = 0;
+
+ if (uh_path_match(alias->alias, old_url))
+ continue;
+
+ if (alias->path)
+ path_len = strlen(alias->path);
+
+ old_len = strlen(old_url) + 1;
+ new_len = old_len + MAX(conf.cgi_prefix_len, path_len);
+
+ if (new_len > url_len) {
+ new_url = realloc(new_url, new_len);
+ url_len = new_len;
+ }
+
+ *new_url = '\0';
+
+ if (alias->path)
+ strcpy(new_url, alias->path);
+ else if (conf.cgi_prefix)
+ strcpy(new_url, conf.cgi_prefix);
+ strcat(new_url, old_url);
+
+ return new_url;
+ }
+ return old_url;
+}
+
void uh_handle_request(struct client *cl)
{
struct http_request *req = &cl->request;
@@ -822,6 +862,8 @@ void uh_handle_request(struct client *cl)
char *url = blobmsg_data(blob_data(cl->hdr.head));
char *error_handler;
+ url = uh_handle_alias(url);
+
req->redirect_status = 200;
d = dispatch_find(url, NULL);
if (d)
diff --git a/main.c b/main.c
index ed47486..ae54b69 100644
--- a/main.c
+++ b/main.c
@@ -155,6 +155,7 @@ static int usage(const char *name)
" -X Enable CORS HTTP headers on JSON-RPC api\n"
#endif
" -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
+ " -y alias[=path] URL alias handle\n"
" -i .ext=path Use interpreter at path for files with the given extension\n"
" -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
" -T seconds Network timeout in seconds, default is 30\n"
@@ -177,6 +178,7 @@ static void init_defaults_pre(void)
conf.realm = "Protected Area";
conf.cgi_prefix = "/cgi-bin";
conf.cgi_path = "/sbin:/usr/sbin:/bin:/usr/bin";
+ INIT_LIST_HEAD(&conf.cgi_alias);
}
static void init_defaults_post(void)
@@ -188,9 +190,11 @@ static void init_defaults_post(void)
if (conf.cgi_prefix) {
char *str = malloc(strlen(conf.docroot) + strlen(conf.cgi_prefix) + 1);
+
strcpy(str, conf.docroot);
strcat(str, conf.cgi_prefix);
conf.cgi_docroot_path = str;
+ conf.cgi_prefix_len = strlen(conf.cgi_prefix);
};
}
@@ -211,12 +215,12 @@ static void fixup_prefix(char *str)
int main(int argc, char **argv)
{
+ struct alias *alias;
bool nofork = false;
char *port;
int opt, ch;
int cur_fd;
int bound = 0;
-
#ifdef HAVE_TLS
int n_tls = 0;
const char *tls_key = NULL, *tls_crt = NULL;
@@ -228,7 +232,7 @@ int main(int argc, char **argv)
init_defaults_pre();
signal(SIGPIPE, SIG_IGN);
- while ((ch = getopt(argc, argv, "afqSDRXC:K:E:I:p:s:h:c:l:L:d:r:m:n:N:x:i:t:k:T:A:u:U:")) != -1) {
+ while ((ch = getopt(argc, argv, "afqSDRXC:K:E:I:p:s:h:c:l:L:d:r:m:n:N:x:y:i:t:k:T:A:u:U:")) != -1) {
switch(ch) {
#ifdef HAVE_TLS
case 'C':
@@ -312,6 +316,19 @@ int main(int argc, char **argv)
conf.cgi_prefix = optarg;
break;
+ case 'y':
+ alias = calloc(1, sizeof(*alias));
+ if (!alias) {
+ fprintf(stderr, "Error: failed to allocate alias\n");
+ exit(1);
+ }
+ alias->alias = strdup(optarg);
+ alias->path = strchr(alias->alias, '=');
+ if (alias->path)
+ *alias->path++ = 0;
+ list_add(&alias->list, &conf.cgi_alias);
+ break;
+
case 'i':
optarg = strdup(optarg);
port = strchr(optarg, '=');
diff --git a/uhttpd.h b/uhttpd.h
index be9ee7f..897d4b3 100644
--- a/uhttpd.h
+++ b/uhttpd.h
@@ -46,6 +46,12 @@
struct client;
+struct alias {
+ struct list_head list;
+ char *alias;
+ char *path;
+};
+
struct config {
const char *docroot;
const char *realm;
@@ -70,6 +76,8 @@ struct config {
int script_timeout;
int ubus_noauth;
int ubus_cors;
+ int cgi_prefix_len;
+ struct list_head cgi_alias;
};
struct auth_realm {