proc: expose HTTP Origin header in process environment
[project/uhttpd.git] / handler.c
index 56720fb..04e71e0 100644 (file)
--- a/handler.c
+++ b/handler.c
@@ -41,19 +41,30 @@ static void
 handle_redirect(struct json_script_ctx *ctx, struct blob_attr *data)
 {
        struct client *cl = cur_client;
-       static struct blobmsg_policy policy = {
-                .type = BLOBMSG_TYPE_STRING,
+       static struct blobmsg_policy policy[3] = {
+                { .type = BLOBMSG_TYPE_STRING },
+                { .type = BLOBMSG_TYPE_INT32 },
+                { .type = BLOBMSG_TYPE_STRING },
        };
-       struct blob_attr *tb;
+       struct blob_attr *tb[3];
+       const char *status = "Found";
+       int code = 302;
 
-       blobmsg_parse_array(&policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
-       if (!tb)
+       blobmsg_parse_array(policy, ARRAY_SIZE(policy), tb, blobmsg_data(data), blobmsg_data_len(data));
+       if (!tb[0])
                return;
 
-       uh_http_header(cl, 302, "Found");
-       ustream_printf(cl->us, "Content-Length: 0\r\n");
+       if (tb[1]) {
+               code = blobmsg_get_u32(tb[1]);
+               if (tb[2])
+                       status = blobmsg_get_string(tb[2]);
+       }
+
+       uh_http_header(cl, code, status);
+       if (!uh_use_chunked(cl))
+               ustream_printf(cl->us, "Content-Length: 0\r\n");
        ustream_printf(cl->us, "Location: %s\r\n\r\n",
-                      blobmsg_get_string(tb));
+                      blobmsg_get_string(tb[0]));
        uh_request_done(cl);
        *cur_url = NULL;
 
@@ -92,6 +103,32 @@ handle_set_uri(struct json_script_ctx *ctx, struct blob_attr *data)
 }
 
 static void
+handle_add_header(struct json_script_ctx *ctx, struct blob_attr *data)
+{
+       struct client *cl = cur_client;
+       static struct blobmsg_policy policy[2] = {
+                { .type = BLOBMSG_TYPE_STRING },
+                { .type = BLOBMSG_TYPE_STRING },
+       };
+       struct blob_attr *tb[2];
+
+       blobmsg_parse_array(policy, ARRAY_SIZE(tb), tb, blobmsg_data(data), blobmsg_data_len(data));
+       if (!tb[0] || !tb[1])
+               return;
+
+       blobmsg_add_string(&cl->hdr_response, blobmsg_get_string(tb[0]),
+                          blobmsg_get_string(tb[1]));
+}
+
+static void
+handle_no_cache(struct json_script_ctx *ctx, struct blob_attr *data)
+{
+       struct client *cl = cur_client;
+
+       cl->dispatch.no_cache = true;
+}
+
+static void
 handle_command(struct json_script_ctx *ctx, const char *name,
               struct blob_attr *data, struct blob_attr *vars)
 {
@@ -100,7 +137,9 @@ handle_command(struct json_script_ctx *ctx, const char *name,
                void (*func)(struct json_script_ctx *ctx, struct blob_attr *data);
        } cmds[] = {
                { "redirect", handle_redirect },
-               { "set_uri", handle_set_uri }
+               { "rewrite", handle_set_uri },
+               { "add-header", handle_add_header },
+               { "no-cache", handle_no_cache },
        };
        int i;