relay: do forward data if the http request type was HEAD
[project/uhttpd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 3c11246..39dca35 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -127,19 +127,17 @@ static void uh_ubus_send_response(struct client *cl)
        char *str;
 
        if (du->array && du->array_idx > 1)
-               sep = ", ";
+               sep = ",";
 
-       str = blobmsg_format_json_indent(buf.head, true, du->array);
+       str = blobmsg_format_json(buf.head, true);
        ops->chunk_printf(cl, "%s%s", sep, str);
        free(str);
 
        du->jsobj_cur = NULL;
        if (du->array)
                uh_ubus_next_batched_request(cl);
-       else {
-               ops->chunk_printf(cl, "\n");
+       else
                return ops->request_done(cl);
-       }
 }
 
 static void uh_ubus_init_response(struct client *cl)
@@ -250,8 +248,11 @@ static void uh_ubus_send_request(struct client *cl, json_object *obj, const char
        int ret, rem;
 
        blob_buf_init(&req, 0);
-       blobmsg_for_each_attr(cur, args, rem)
+       blobmsg_for_each_attr(cur, args, rem) {
+               if (!strcmp(blobmsg_name(cur), "ubus_rpc_session"))
+                       return uh_ubus_json_error(cl, ERROR_PARAMS);
                blobmsg_add_blob(&req, cur);
+       }
 
        blobmsg_add_string(&req, "ubus_rpc_session", sid);
 
@@ -329,6 +330,8 @@ static void uh_ubus_send_list(struct client *cl, json_object *obj, struct blob_a
 
        blob_buf_init(data.buf, 0);
 
+       uh_client_ref(cl);
+
        if (!params || blob_id(params) != BLOBMSG_TYPE_ARRAY) {
                r = blobmsg_open_array(data.buf, "result");
                ubus_lookup(ctx, NULL, uh_ubus_list_cb, &data);
@@ -348,6 +351,8 @@ static void uh_ubus_send_list(struct client *cl, json_object *obj, struct blob_a
                blobmsg_close_table(data.buf, r);
        }
 
+       uh_client_unref(cl);
+
        uh_ubus_init_response(cl);
        blobmsg_add_blob(&buf, blob_data(data.buf->head));
        uh_ubus_send_response(cl);
@@ -412,12 +417,12 @@ static void uh_ubus_init_batch(struct client *cl)
 
        du->array = true;
        uh_ubus_send_header(cl);
-       ops->chunk_printf(cl, "[\n\t");
+       ops->chunk_printf(cl, "[");
 }
 
 static void uh_ubus_complete_batch(struct client *cl)
 {
-       ops->chunk_printf(cl, "\n]\n");
+       ops->chunk_printf(cl, "]");
        ops->request_done(cl);
 }
 
@@ -445,7 +450,7 @@ static bool uh_ubus_allowed(const char *sid, const char *obj, const char *fun)
                return false;
 
        blob_buf_init(&req, 0);
-       blobmsg_add_string(&req, "sid", sid);
+       blobmsg_add_string(&req, "ubus_rpc_session", sid);
        blobmsg_add_string(&req, "object", obj);
        blobmsg_add_string(&req, "function", fun);
 
@@ -460,6 +465,8 @@ static void uh_ubus_handle_request_object(struct client *cl, struct json_object
        struct rpc_data data = {};
        enum rpc_error err = ERROR_PARSE;
 
+       uh_client_ref(cl);
+
        if (json_object_get_type(obj) != json_type_object)
                goto error;
 
@@ -503,6 +510,8 @@ error:
 out:
        if (data.params)
                free(data.params);
+
+       uh_client_unref(cl);
 }
 
 static void __uh_ubus_next_batched_request(struct uloop_timeout *timeout)