2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <libubox/blobmsg.h>
21 #include <libubox/blobmsg_json.h>
22 #include <libubox/avl.h>
23 #include <libubox/avl-cmp.h>
30 static const struct uhttpd_ops
*ops
;
31 static struct config
*_conf
;
34 static struct ubus_context
*ctx
;
35 static struct blob_buf buf
;
37 #define UH_UBUS_MAX_POST_SIZE 65536
38 #define UH_UBUS_DEFAULT_SID "00000000000000000000000000000000"
48 static const struct blobmsg_policy rpc_policy
[__RPC_MAX
] = {
49 [RPC_JSONRPC
] = { .name
= "jsonrpc", .type
= BLOBMSG_TYPE_STRING
},
50 [RPC_METHOD
] = { .name
= "method", .type
= BLOBMSG_TYPE_STRING
},
51 [RPC_PARAMS
] = { .name
= "params", .type
= BLOBMSG_TYPE_UNSPEC
},
52 [RPC_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_UNSPEC
},
60 static const struct blobmsg_policy ses_policy
[__SES_MAX
] = {
61 [SES_ACCESS
] = { .name
= "access", .type
= BLOBMSG_TYPE_BOOL
},
70 struct blob_attr
*data
;
71 struct blob_attr
*params
;
96 } json_errors
[__ERROR_MAX
] = {
97 [ERROR_PARSE
] = { -32700, "Parse error" },
98 [ERROR_REQUEST
] = { -32600, "Invalid request" },
99 [ERROR_METHOD
] = { -32601, "Method not found" },
100 [ERROR_PARAMS
] = { -32602, "Invalid parameters" },
101 [ERROR_INTERNAL
] = { -32603, "Internal error" },
102 [ERROR_OBJECT
] = { -32000, "Object not found" },
103 [ERROR_SESSION
] = { -32001, "Session not found" },
104 [ERROR_ACCESS
] = { -32002, "Access denied" },
105 [ERROR_TIMEOUT
] = { -32003, "ubus request timed out" },
110 HDR_ACCESS_CONTROL_REQUEST_METHOD
,
111 HDR_ACCESS_CONTROL_REQUEST_HEADERS
,
115 static void __uh_ubus_next_batched_request(struct uloop_timeout
*timeout
);
117 static void uh_ubus_next_batched_request(struct client
*cl
)
119 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
121 du
->timeout
.cb
= __uh_ubus_next_batched_request
;
122 uloop_timeout_set(&du
->timeout
, 1);
125 static void uh_ubus_add_cors_headers(struct client
*cl
)
127 struct blob_attr
*tb
[__HDR_MAX
];
128 static const struct blobmsg_policy hdr_policy
[__HDR_MAX
] = {
129 [HDR_ORIGIN
] = { "origin", BLOBMSG_TYPE_STRING
},
130 [HDR_ACCESS_CONTROL_REQUEST_METHOD
] = { "access-control-request-method", BLOBMSG_TYPE_STRING
},
131 [HDR_ACCESS_CONTROL_REQUEST_HEADERS
] = { "access-control-request-headers", BLOBMSG_TYPE_STRING
},
134 blobmsg_parse(hdr_policy
, __HDR_MAX
, tb
, blob_data(cl
->hdr
.head
), blob_len(cl
->hdr
.head
));
139 if (tb
[HDR_ACCESS_CONTROL_REQUEST_METHOD
])
141 char *hdr
= (char *) blobmsg_data(tb
[HDR_ACCESS_CONTROL_REQUEST_METHOD
]);
143 if (strcmp(hdr
, "POST") && strcmp(hdr
, "OPTIONS"))
147 ustream_printf(cl
->us
, "Access-Control-Allow-Origin: %s\r\n",
148 blobmsg_get_string(tb
[HDR_ORIGIN
]));
150 if (tb
[HDR_ACCESS_CONTROL_REQUEST_HEADERS
])
151 ustream_printf(cl
->us
, "Access-Control-Allow-Headers: %s\r\n",
152 blobmsg_get_string(tb
[HDR_ACCESS_CONTROL_REQUEST_HEADERS
]));
154 ustream_printf(cl
->us
, "Access-Control-Allow-Methods: POST, OPTIONS\r\n");
155 ustream_printf(cl
->us
, "Access-Control-Allow-Credentials: true\r\n");
158 static void uh_ubus_send_header(struct client
*cl
, int code
, const char *summary
, const char *content_type
)
160 ops
->http_header(cl
, code
, summary
);
163 uh_ubus_add_cors_headers(cl
);
165 ustream_printf(cl
->us
, "Content-Type: %s\r\n", content_type
);
167 if (cl
->request
.method
== UH_HTTP_MSG_OPTIONS
)
168 ustream_printf(cl
->us
, "Content-Length: 0\r\n");
170 ustream_printf(cl
->us
, "\r\n");
173 static void uh_ubus_send_response(struct client
*cl
, struct blob_buf
*buf
)
175 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
176 const char *sep
= "";
179 if (du
->array
&& du
->array_idx
> 1)
182 str
= blobmsg_format_json(buf
->head
, true);
183 ops
->chunk_printf(cl
, "%s%s", sep
, str
);
186 du
->jsobj_cur
= NULL
;
188 uh_ubus_next_batched_request(cl
);
190 return ops
->request_done(cl
);
193 static void uh_ubus_init_json_rpc_response(struct client
*cl
, struct blob_buf
*buf
)
195 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
196 struct json_object
*obj
= du
->jsobj_cur
, *obj2
= NULL
;
198 blobmsg_add_string(buf
, "jsonrpc", "2.0");
201 json_object_object_get_ex(obj
, "id", &obj2
);
204 blobmsg_add_json_element(buf
, "id", obj2
);
206 blobmsg_add_field(buf
, BLOBMSG_TYPE_UNSPEC
, "id", NULL
, 0);
209 static void uh_ubus_json_rpc_error(struct client
*cl
, enum rpc_error type
)
213 blob_buf_init(&buf
, 0);
215 uh_ubus_init_json_rpc_response(cl
, &buf
);
216 c
= blobmsg_open_table(&buf
, "error");
217 blobmsg_add_u32(&buf
, "code", json_errors
[type
].code
);
218 blobmsg_add_string(&buf
, "message", json_errors
[type
].msg
);
219 blobmsg_close_table(&buf
, c
);
220 uh_ubus_send_response(cl
, &buf
);
223 static void uh_ubus_error(struct client
*cl
, int code
, const char *message
)
225 blob_buf_init(&buf
, 0);
227 blobmsg_add_u32(&buf
, "code", code
);
228 blobmsg_add_string(&buf
, "message", message
);
229 uh_ubus_send_response(cl
, &buf
);
232 static void uh_ubus_posix_error(struct client
*cl
, int err
)
234 uh_ubus_error(cl
, -err
, strerror(err
));
237 static void uh_ubus_ubus_error(struct client
*cl
, int err
)
239 uh_ubus_error(cl
, err
, ubus_strerror(err
));
242 /* GET requests handling */
244 static void uh_ubus_list_cb(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
);
246 static void uh_ubus_handle_get_list(struct client
*cl
, const char *path
)
248 static struct blob_buf tmp
;
249 struct list_data data
= { .verbose
= true, .add_object
= !path
, .buf
= &tmp
};
250 struct blob_attr
*cur
;
254 blob_buf_init(&tmp
, 0);
256 err
= ubus_lookup(ctx
, path
, uh_ubus_list_cb
, &data
);
258 uh_ubus_send_header(cl
, 500, "Ubus Protocol Error", "application/json");
259 uh_ubus_ubus_error(cl
, err
);
263 blob_buf_init(&buf
, 0);
264 blob_for_each_attr(cur
, tmp
.head
, rem
)
265 blobmsg_add_blob(&buf
, cur
);
267 uh_ubus_send_header(cl
, 200, "OK", "application/json");
268 uh_ubus_send_response(cl
, &buf
);
271 static int uh_ubus_subscription_notification_cb(struct ubus_context
*ctx
,
272 struct ubus_object
*obj
,
273 struct ubus_request_data
*req
,
275 struct blob_attr
*msg
)
277 struct ubus_subscriber
*s
;
278 struct dispatch_ubus
*du
;
282 s
= container_of(obj
, struct ubus_subscriber
, obj
);
283 du
= container_of(s
, struct dispatch_ubus
, sub
);
284 cl
= container_of(du
, struct client
, dispatch
.ubus
);
286 json
= blobmsg_format_json(msg
, true);
288 ops
->chunk_printf(cl
, "event: %s\ndata: %s\n\n", method
, json
);
295 static void uh_ubus_subscription_notification_remove_cb(struct ubus_context
*ctx
, struct ubus_subscriber
*s
, uint32_t id
)
297 struct dispatch_ubus
*du
;
300 du
= container_of(s
, struct dispatch_ubus
, sub
);
301 cl
= container_of(du
, struct client
, dispatch
.ubus
);
303 ops
->request_done(cl
);
306 static void uh_ubus_handle_get_subscribe(struct client
*cl
, const char *sid
, const char *path
)
308 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
312 /* TODO: add ACL support */
313 if (!conf
.ubus_noauth
) {
314 uh_ubus_send_header(cl
, 200, "OK", "application/json");
315 uh_ubus_posix_error(cl
, EACCES
);
319 du
->sub
.cb
= uh_ubus_subscription_notification_cb
;
320 du
->sub
.remove_cb
= uh_ubus_subscription_notification_remove_cb
;
324 err
= ubus_register_subscriber(ctx
, &du
->sub
);
328 err
= ubus_lookup_id(ctx
, path
, &id
);
332 err
= ubus_subscribe(ctx
, &du
->sub
, id
);
336 uh_ubus_send_header(cl
, 200, "OK", "text/event-stream");
338 if (conf
.events_retry
)
339 ops
->chunk_printf(cl
, "retry: %d\n", conf
.events_retry
);
344 ubus_unregister_subscriber(ctx
, &du
->sub
);
348 uh_ubus_send_header(cl
, 200, "OK", "application/json");
349 uh_ubus_ubus_error(cl
, err
);
353 static void uh_ubus_handle_get(struct client
*cl
)
355 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
356 const char *url
= du
->url_path
;
358 url
+= strlen(conf
.ubus_prefix
);
360 if (!strcmp(url
, "/list") || !strncmp(url
, "/list/", strlen("/list/"))) {
361 url
+= strlen("/list");
363 uh_ubus_handle_get_list(cl
, *url
? url
+ 1 : NULL
);
364 } else if (!strncmp(url
, "/subscribe/", strlen("/subscribe/"))) {
365 url
+= strlen("/subscribe");
367 uh_ubus_handle_get_subscribe(cl
, NULL
, url
+ 1);
369 ops
->http_header(cl
, 404, "Not Found");
370 ustream_printf(cl
->us
, "\r\n");
371 ops
->request_done(cl
);
375 /* POST requests handling */
378 uh_ubus_request_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
380 struct dispatch_ubus
*du
= container_of(req
, struct dispatch_ubus
, req
);
381 struct blob_attr
*cur
;
384 blob_for_each_attr(cur
, msg
, len
)
385 blobmsg_add_blob(&du
->buf
, cur
);
389 uh_ubus_request_cb(struct ubus_request
*req
, int ret
)
391 struct dispatch_ubus
*du
= container_of(req
, struct dispatch_ubus
, req
);
392 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
393 struct blob_attr
*cur
;
397 blob_buf_init(&buf
, 0);
399 uloop_timeout_cancel(&du
->timeout
);
401 /* Legacy format always uses "result" array - even for errors and empty
406 uh_ubus_init_json_rpc_response(cl
, &buf
);
407 r
= blobmsg_open_array(&buf
, "result");
408 blobmsg_add_u32(&buf
, "", ret
);
409 c
= blobmsg_open_table(&buf
, NULL
);
410 blob_for_each_attr(cur
, du
->buf
.head
, rem
)
411 blobmsg_add_blob(&buf
, cur
);
412 blobmsg_close_table(&buf
, c
);
413 blobmsg_close_array(&buf
, r
);
414 uh_ubus_send_response(cl
, &buf
);
421 uh_ubus_init_json_rpc_response(cl
, &buf
);
422 c
= blobmsg_open_table(&buf
, "error");
423 blobmsg_add_u32(&buf
, "code", ret
);
424 blobmsg_add_string(&buf
, "message", ubus_strerror(ret
));
425 blobmsg_close_table(&buf
, c
);
426 uh_ubus_send_response(cl
, &buf
);
428 uh_ubus_init_json_rpc_response(cl
, &buf
);
429 if (blob_len(du
->buf
.head
)) {
430 r
= blobmsg_open_table(&buf
, "result");
431 blob_for_each_attr(cur
, du
->buf
.head
, rem
)
432 blobmsg_add_blob(&buf
, cur
);
433 blobmsg_close_table(&buf
, r
);
435 blobmsg_add_field(&buf
, BLOBMSG_TYPE_UNSPEC
, "result", NULL
, 0);
437 uh_ubus_send_response(cl
, &buf
);
443 uh_ubus_timeout_cb(struct uloop_timeout
*timeout
)
445 struct dispatch_ubus
*du
= container_of(timeout
, struct dispatch_ubus
, timeout
);
446 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
448 ubus_abort_request(ctx
, &du
->req
);
449 uh_ubus_json_rpc_error(cl
, ERROR_TIMEOUT
);
452 static void uh_ubus_close_fds(struct client
*cl
)
454 if (ctx
->sock
.fd
< 0)
461 static void uh_ubus_request_free(struct client
*cl
)
463 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
465 blob_buf_free(&du
->buf
);
466 uloop_timeout_cancel(&du
->timeout
);
469 json_object_put(du
->jsobj
);
472 json_tokener_free(du
->jstok
);
475 ubus_abort_request(ctx
, &du
->req
);
481 static void uh_ubus_single_error(struct client
*cl
, enum rpc_error type
)
483 uh_ubus_send_header(cl
, 200, "OK", "application/json");
484 uh_ubus_json_rpc_error(cl
, type
);
485 ops
->request_done(cl
);
488 static void uh_ubus_send_request(struct client
*cl
, const char *sid
, struct blob_attr
*args
)
490 struct dispatch
*d
= &cl
->dispatch
;
491 struct dispatch_ubus
*du
= &d
->ubus
;
492 struct blob_attr
*cur
;
493 static struct blob_buf req
;
496 blob_buf_init(&req
, 0);
497 blobmsg_for_each_attr(cur
, args
, rem
) {
498 if (!strcmp(blobmsg_name(cur
), "ubus_rpc_session"))
499 return uh_ubus_json_rpc_error(cl
, ERROR_PARAMS
);
500 blobmsg_add_blob(&req
, cur
);
503 blobmsg_add_string(&req
, "ubus_rpc_session", sid
);
505 blob_buf_init(&du
->buf
, 0);
506 memset(&du
->req
, 0, sizeof(du
->req
));
507 ret
= ubus_invoke_async(ctx
, du
->obj
, du
->func
, req
.head
, &du
->req
);
509 return uh_ubus_json_rpc_error(cl
, ERROR_INTERNAL
);
511 du
->req
.data_cb
= uh_ubus_request_data_cb
;
512 du
->req
.complete_cb
= uh_ubus_request_cb
;
513 ubus_complete_request_async(ctx
, &du
->req
);
515 du
->timeout
.cb
= uh_ubus_timeout_cb
;
516 uloop_timeout_set(&du
->timeout
, conf
.script_timeout
* 1000);
518 du
->req_pending
= true;
521 static void uh_ubus_list_cb(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
523 struct blob_attr
*sig
, *attr
;
524 struct list_data
*data
= priv
;
528 if (!data
->verbose
) {
529 blobmsg_add_string(data
->buf
, NULL
, obj
->path
);
536 if (data
->add_object
)
537 o
= blobmsg_open_table(data
->buf
, obj
->path
);
538 blob_for_each_attr(sig
, obj
->signature
, rem
) {
539 t
= blobmsg_open_table(data
->buf
, blobmsg_name(sig
));
540 rem2
= blobmsg_data_len(sig
);
541 __blob_for_each_attr(attr
, blobmsg_data(sig
), rem2
) {
542 if (blob_id(attr
) != BLOBMSG_TYPE_INT32
)
545 switch (blobmsg_get_u32(attr
)) {
546 case BLOBMSG_TYPE_INT8
:
547 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "boolean");
549 case BLOBMSG_TYPE_INT32
:
550 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "number");
552 case BLOBMSG_TYPE_STRING
:
553 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "string");
555 case BLOBMSG_TYPE_ARRAY
:
556 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "array");
558 case BLOBMSG_TYPE_TABLE
:
559 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "object");
562 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "unknown");
566 blobmsg_close_table(data
->buf
, t
);
568 if (data
->add_object
)
569 blobmsg_close_table(data
->buf
, o
);
572 static void uh_ubus_send_list(struct client
*cl
, struct blob_attr
*params
)
574 struct blob_attr
*cur
, *dup
;
575 struct list_data data
= { .buf
= &cl
->dispatch
.ubus
.buf
, .verbose
= false, .add_object
= true };
579 blob_buf_init(data
.buf
, 0);
583 if (!params
|| blob_id(params
) != BLOBMSG_TYPE_ARRAY
) {
584 r
= blobmsg_open_array(data
.buf
, "result");
585 ubus_lookup(ctx
, NULL
, uh_ubus_list_cb
, &data
);
586 blobmsg_close_array(data
.buf
, r
);
589 r
= blobmsg_open_table(data
.buf
, "result");
590 dup
= blob_memdup(params
);
593 rem
= blobmsg_data_len(dup
);
595 __blob_for_each_attr(cur
, blobmsg_data(dup
), rem
)
596 ubus_lookup(ctx
, blobmsg_data(cur
), uh_ubus_list_cb
, &data
);
599 blobmsg_close_table(data
.buf
, r
);
604 blob_buf_init(&buf
, 0);
605 uh_ubus_init_json_rpc_response(cl
, &buf
);
606 blobmsg_add_blob(&buf
, blob_data(data
.buf
->head
));
607 uh_ubus_send_response(cl
, &buf
);
610 static bool parse_json_rpc(struct rpc_data
*d
, struct blob_attr
*data
)
612 struct blob_attr
*tb
[__RPC_MAX
];
613 struct blob_attr
*cur
;
615 blobmsg_parse(rpc_policy
, __RPC_MAX
, tb
, blob_data(data
), blob_len(data
));
617 cur
= tb
[RPC_JSONRPC
];
618 if (!cur
|| strcmp(blobmsg_data(cur
), "2.0") != 0)
621 cur
= tb
[RPC_METHOD
];
626 d
->method
= blobmsg_data(cur
);
628 cur
= tb
[RPC_PARAMS
];
632 d
->params
= blob_memdup(cur
);
639 static void parse_call_params(struct rpc_data
*d
)
641 const struct blobmsg_policy data_policy
[] = {
642 { .type
= BLOBMSG_TYPE_STRING
},
643 { .type
= BLOBMSG_TYPE_STRING
},
644 { .type
= BLOBMSG_TYPE_STRING
},
645 { .type
= BLOBMSG_TYPE_TABLE
},
647 struct blob_attr
*tb
[4];
649 if (!d
->params
|| blobmsg_type(d
->params
) != BLOBMSG_TYPE_ARRAY
)
652 blobmsg_parse_array(data_policy
, ARRAY_SIZE(data_policy
), tb
,
653 blobmsg_data(d
->params
), blobmsg_data_len(d
->params
));
656 d
->sid
= blobmsg_data(tb
[0]);
658 if (conf
.ubus_noauth
&& (!d
->sid
|| !*d
->sid
))
659 d
->sid
= UH_UBUS_DEFAULT_SID
;
662 d
->object
= blobmsg_data(tb
[1]);
665 d
->function
= blobmsg_data(tb
[2]);
670 static void uh_ubus_init_batch(struct client
*cl
)
672 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
675 uh_ubus_send_header(cl
, 200, "OK", "application/json");
676 ops
->chunk_printf(cl
, "[");
679 static void uh_ubus_complete_batch(struct client
*cl
)
681 ops
->chunk_printf(cl
, "]");
682 ops
->request_done(cl
);
685 static void uh_ubus_allowed_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
687 struct blob_attr
*tb
[__SES_MAX
];
688 bool *allow
= (bool *)req
->priv
;
693 blobmsg_parse(ses_policy
, __SES_MAX
, tb
, blob_data(msg
), blob_len(msg
));
696 *allow
= blobmsg_get_bool(tb
[SES_ACCESS
]);
699 static bool uh_ubus_allowed(const char *sid
, const char *obj
, const char *fun
)
703 static struct blob_buf req
;
705 if (ubus_lookup_id(ctx
, "session", &id
))
708 blob_buf_init(&req
, 0);
709 blobmsg_add_string(&req
, "ubus_rpc_session", sid
);
710 blobmsg_add_string(&req
, "object", obj
);
711 blobmsg_add_string(&req
, "function", fun
);
713 ubus_invoke(ctx
, id
, "access", req
.head
, uh_ubus_allowed_cb
, &allow
, conf
.script_timeout
* 500);
718 static void uh_ubus_handle_request_object(struct client
*cl
, struct json_object
*obj
)
720 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
721 struct rpc_data data
= {};
722 enum rpc_error err
= ERROR_PARSE
;
723 static struct blob_buf req
;
727 if (json_object_get_type(obj
) != json_type_object
)
731 blob_buf_init(&req
, 0);
732 if (!blobmsg_add_object(&req
, obj
))
735 if (!parse_json_rpc(&data
, req
.head
))
738 if (!strcmp(data
.method
, "call")) {
739 parse_call_params(&data
);
741 if (!data
.sid
|| !data
.object
|| !data
.function
|| !data
.data
)
744 du
->func
= data
.function
;
745 if (ubus_lookup_id(ctx
, data
.object
, &du
->obj
)) {
750 if (!conf
.ubus_noauth
&& !uh_ubus_allowed(data
.sid
, data
.object
, data
.function
)) {
755 uh_ubus_send_request(cl
, data
.sid
, data
.data
);
758 else if (!strcmp(data
.method
, "list")) {
759 uh_ubus_send_list(cl
, data
.params
);
768 uh_ubus_json_rpc_error(cl
, err
);
776 static void __uh_ubus_next_batched_request(struct uloop_timeout
*timeout
)
778 struct dispatch_ubus
*du
= container_of(timeout
, struct dispatch_ubus
, timeout
);
779 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
780 struct json_object
*obj
= du
->jsobj
;
783 len
= json_object_array_length(obj
);
784 if (du
->array_idx
>= len
)
785 return uh_ubus_complete_batch(cl
);
787 obj
= json_object_array_get_idx(obj
, du
->array_idx
++);
788 uh_ubus_handle_request_object(cl
, obj
);
791 static void uh_ubus_data_done(struct client
*cl
)
793 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
794 struct json_object
*obj
= du
->jsobj
;
796 switch (obj
? json_object_get_type(obj
) : json_type_null
) {
797 case json_type_object
:
798 uh_ubus_send_header(cl
, 200, "OK", "application/json");
799 return uh_ubus_handle_request_object(cl
, obj
);
800 case json_type_array
:
801 uh_ubus_init_batch(cl
);
802 return uh_ubus_next_batched_request(cl
);
804 return uh_ubus_single_error(cl
, ERROR_PARSE
);
808 static void uh_ubus_call(struct client
*cl
, const char *path
, const char *sid
)
810 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
811 struct json_object
*obj
= du
->jsobj
;
812 struct rpc_data data
= {};
813 enum rpc_error err
= ERROR_PARSE
;
814 static struct blob_buf req
;
818 if (!obj
|| json_object_get_type(obj
) != json_type_object
)
821 uh_ubus_send_header(cl
, 200, "OK", "application/json");
824 blob_buf_init(&req
, 0);
825 if (!blobmsg_add_object(&req
, obj
))
828 if (!parse_json_rpc(&data
, req
.head
))
831 du
->func
= data
.method
;
832 if (ubus_lookup_id(ctx
, path
, &du
->obj
)) {
837 if (!conf
.ubus_noauth
&& !uh_ubus_allowed(sid
, path
, data
.method
)) {
842 uh_ubus_send_request(cl
, sid
, data
.params
);
846 uh_ubus_json_rpc_error(cl
, err
);
859 static void uh_ubus_handle_post(struct client
*cl
)
861 static const struct blobmsg_policy hdr_policy
[__HDR_UBUS_MAX
] = {
862 [HDR_AUTHORIZATION
] = { "authorization", BLOBMSG_TYPE_STRING
},
864 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
865 struct blob_attr
*tb
[__HDR_UBUS_MAX
];
866 const char *url
= du
->url_path
;
869 /* Treat both: /foo AND /foo/ as legacy requests. */
870 if (ops
->path_match(conf
.ubus_prefix
, url
) && strlen(url
) - strlen(conf
.ubus_prefix
) <= 1) {
872 uh_ubus_data_done(cl
);
876 blobmsg_parse(hdr_policy
, __HDR_UBUS_MAX
, tb
, blob_data(cl
->hdr
.head
), blob_len(cl
->hdr
.head
));
878 auth
= UH_UBUS_DEFAULT_SID
;
879 if (tb
[HDR_AUTHORIZATION
]) {
880 const char *tmp
= blobmsg_get_string(tb
[HDR_AUTHORIZATION
]);
882 if (!strncasecmp(tmp
, "Bearer ", 7))
886 url
+= strlen(conf
.ubus_prefix
);
888 if (!strncmp(url
, "/call/", strlen("/call/"))) {
889 url
+= strlen("/call/");
891 uh_ubus_call(cl
, url
, auth
);
893 ops
->http_header(cl
, 404, "Not Found");
894 ustream_printf(cl
->us
, "\r\n");
895 ops
->request_done(cl
);
899 static int uh_ubus_data_send(struct client
*cl
, const char *data
, int len
)
901 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
903 if (du
->jsobj
|| !du
->jstok
)
907 if (du
->post_len
> UH_UBUS_MAX_POST_SIZE
)
910 du
->jsobj
= json_tokener_parse_ex(du
->jstok
, data
, len
);
914 uh_ubus_single_error(cl
, ERROR_PARSE
);
918 static void uh_ubus_handle_request(struct client
*cl
, char *url
, struct path_info
*pi
)
920 struct dispatch
*d
= &cl
->dispatch
;
921 struct dispatch_ubus
*du
= &d
->ubus
;
924 du
->url_path
= strdup(url
);
926 ops
->client_error(cl
, 500, "Internal Server Error", "Failed to allocate resources");
929 chr
= strchr(du
->url_path
, '?');
935 switch (cl
->request
.method
)
937 case UH_HTTP_MSG_GET
:
938 uh_ubus_handle_get(cl
);
940 case UH_HTTP_MSG_POST
:
941 d
->data_send
= uh_ubus_data_send
;
942 d
->data_done
= uh_ubus_handle_post
;
943 d
->close_fds
= uh_ubus_close_fds
;
944 d
->free
= uh_ubus_request_free
;
945 du
->jstok
= json_tokener_new();
948 case UH_HTTP_MSG_OPTIONS
:
949 uh_ubus_send_header(cl
, 200, "OK", "application/json");
950 ops
->request_done(cl
);
954 ops
->client_error(cl
, 400, "Bad Request", "Invalid Request");
962 uh_ubus_check_url(const char *url
)
964 return ops
->path_match(conf
.ubus_prefix
, url
);
970 static struct dispatch_handler ubus_dispatch
= {
971 .check_url
= uh_ubus_check_url
,
972 .handle_request
= uh_ubus_handle_request
,
975 ctx
= ubus_connect(conf
.ubus_socket
);
977 fprintf(stderr
, "Unable to connect to ubus socket\n");
981 ops
->dispatch_add(&ubus_dispatch
);
988 static int uh_ubus_plugin_init(const struct uhttpd_ops
*o
, struct config
*c
)
992 return uh_ubus_init();
995 static void uh_ubus_post_init(void)
1000 struct uhttpd_plugin uhttpd_plugin
= {
1001 .init
= uh_ubus_plugin_init
,
1002 .post_init
= uh_ubus_post_init
,