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
,
120 static const char *uh_ubus_get_auth(const struct blob_attr
*attr
)
122 static const struct blobmsg_policy hdr_policy
[__HDR_UBUS_MAX
] = {
123 [HDR_AUTHORIZATION
] = { "authorization", BLOBMSG_TYPE_STRING
},
125 struct blob_attr
*tb
[__HDR_UBUS_MAX
];
127 blobmsg_parse(hdr_policy
, __HDR_UBUS_MAX
, tb
, blob_data(attr
), blob_len(attr
));
129 if (tb
[HDR_AUTHORIZATION
]) {
130 const char *tmp
= blobmsg_get_string(tb
[HDR_AUTHORIZATION
]);
132 if (!strncasecmp(tmp
, "Bearer ", 7))
136 return UH_UBUS_DEFAULT_SID
;
139 static void __uh_ubus_next_batched_request(struct uloop_timeout
*timeout
);
141 static void uh_ubus_next_batched_request(struct client
*cl
)
143 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
145 du
->timeout
.cb
= __uh_ubus_next_batched_request
;
146 uloop_timeout_set(&du
->timeout
, 1);
149 static void uh_ubus_add_cors_headers(struct client
*cl
)
151 struct blob_attr
*tb
[__HDR_MAX
];
152 static const struct blobmsg_policy hdr_policy
[__HDR_MAX
] = {
153 [HDR_ORIGIN
] = { "origin", BLOBMSG_TYPE_STRING
},
154 [HDR_ACCESS_CONTROL_REQUEST_METHOD
] = { "access-control-request-method", BLOBMSG_TYPE_STRING
},
155 [HDR_ACCESS_CONTROL_REQUEST_HEADERS
] = { "access-control-request-headers", BLOBMSG_TYPE_STRING
},
158 blobmsg_parse(hdr_policy
, __HDR_MAX
, tb
, blob_data(cl
->hdr
.head
), blob_len(cl
->hdr
.head
));
163 if (tb
[HDR_ACCESS_CONTROL_REQUEST_METHOD
])
165 char *hdr
= (char *) blobmsg_data(tb
[HDR_ACCESS_CONTROL_REQUEST_METHOD
]);
167 if (strcmp(hdr
, "GET") && strcmp(hdr
, "POST") && strcmp(hdr
, "OPTIONS"))
171 ustream_printf(cl
->us
, "Access-Control-Allow-Origin: %s\r\n",
172 blobmsg_get_string(tb
[HDR_ORIGIN
]));
174 if (tb
[HDR_ACCESS_CONTROL_REQUEST_HEADERS
])
175 ustream_printf(cl
->us
, "Access-Control-Allow-Headers: %s\r\n",
176 blobmsg_get_string(tb
[HDR_ACCESS_CONTROL_REQUEST_HEADERS
]));
178 ustream_printf(cl
->us
, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
179 ustream_printf(cl
->us
, "Access-Control-Allow-Credentials: true\r\n");
182 static void uh_ubus_send_header(struct client
*cl
, int code
, const char *summary
, const char *content_type
)
184 ops
->http_header(cl
, code
, summary
);
187 uh_ubus_add_cors_headers(cl
);
189 ustream_printf(cl
->us
, "Content-Type: %s\r\n", content_type
);
191 if (cl
->request
.method
== UH_HTTP_MSG_OPTIONS
)
192 ustream_printf(cl
->us
, "Content-Length: 0\r\n");
194 ustream_printf(cl
->us
, "\r\n");
197 static void uh_ubus_send_response(struct client
*cl
, struct blob_buf
*buf
)
199 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
200 const char *sep
= "";
203 if (du
->array
&& du
->array_idx
> 1)
206 str
= blobmsg_format_json(buf
->head
, true);
207 ops
->chunk_printf(cl
, "%s%s", sep
, str
);
210 du
->jsobj_cur
= NULL
;
212 uh_ubus_next_batched_request(cl
);
214 return ops
->request_done(cl
);
217 static void uh_ubus_init_json_rpc_response(struct client
*cl
, struct blob_buf
*buf
)
219 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
220 struct json_object
*obj
= du
->jsobj_cur
, *obj2
= NULL
;
222 blobmsg_add_string(buf
, "jsonrpc", "2.0");
225 json_object_object_get_ex(obj
, "id", &obj2
);
228 blobmsg_add_json_element(buf
, "id", obj2
);
230 blobmsg_add_field(buf
, BLOBMSG_TYPE_UNSPEC
, "id", NULL
, 0);
233 static void uh_ubus_json_rpc_error(struct client
*cl
, enum rpc_error type
)
237 blob_buf_init(&buf
, 0);
239 uh_ubus_init_json_rpc_response(cl
, &buf
);
240 c
= blobmsg_open_table(&buf
, "error");
241 blobmsg_add_u32(&buf
, "code", json_errors
[type
].code
);
242 blobmsg_add_string(&buf
, "message", json_errors
[type
].msg
);
243 blobmsg_close_table(&buf
, c
);
244 uh_ubus_send_response(cl
, &buf
);
247 static void uh_ubus_error(struct client
*cl
, int code
, const char *message
)
249 blob_buf_init(&buf
, 0);
251 blobmsg_add_u32(&buf
, "code", code
);
252 blobmsg_add_string(&buf
, "message", message
);
253 uh_ubus_send_response(cl
, &buf
);
256 static void uh_ubus_posix_error(struct client
*cl
, int err
)
258 uh_ubus_error(cl
, -err
, strerror(err
));
261 static void uh_ubus_ubus_error(struct client
*cl
, int err
)
263 uh_ubus_error(cl
, err
, ubus_strerror(err
));
266 static void uh_ubus_allowed_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
268 struct blob_attr
*tb
[__SES_MAX
];
269 bool *allow
= (bool *)req
->priv
;
274 blobmsg_parse(ses_policy
, __SES_MAX
, tb
, blob_data(msg
), blob_len(msg
));
277 *allow
= blobmsg_get_bool(tb
[SES_ACCESS
]);
280 static bool uh_ubus_allowed(const char *sid
, const char *obj
, const char *fun
)
284 static struct blob_buf req
;
286 if (ubus_lookup_id(ctx
, "session", &id
))
289 blob_buf_init(&req
, 0);
290 blobmsg_add_string(&req
, "ubus_rpc_session", sid
);
291 blobmsg_add_string(&req
, "object", obj
);
292 blobmsg_add_string(&req
, "function", fun
);
294 ubus_invoke(ctx
, id
, "access", req
.head
, uh_ubus_allowed_cb
, &allow
, conf
.script_timeout
* 500);
299 /* GET requests handling */
301 static void uh_ubus_list_cb(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
);
303 static void uh_ubus_handle_get_list(struct client
*cl
, const char *path
)
305 static struct blob_buf tmp
;
306 struct list_data data
= { .verbose
= true, .add_object
= !path
, .buf
= &tmp
};
307 struct blob_attr
*cur
;
311 blob_buf_init(&tmp
, 0);
313 err
= ubus_lookup(ctx
, path
, uh_ubus_list_cb
, &data
);
315 uh_ubus_send_header(cl
, 500, "Ubus Protocol Error", "application/json");
316 uh_ubus_ubus_error(cl
, err
);
320 blob_buf_init(&buf
, 0);
321 blob_for_each_attr(cur
, tmp
.head
, rem
)
322 blobmsg_add_blob(&buf
, cur
);
324 uh_ubus_send_header(cl
, 200, "OK", "application/json");
325 uh_ubus_send_response(cl
, &buf
);
328 static int uh_ubus_subscription_notification_cb(struct ubus_context
*ctx
,
329 struct ubus_object
*obj
,
330 struct ubus_request_data
*req
,
332 struct blob_attr
*msg
)
334 struct ubus_subscriber
*s
;
335 struct dispatch_ubus
*du
;
339 s
= container_of(obj
, struct ubus_subscriber
, obj
);
340 du
= container_of(s
, struct dispatch_ubus
, sub
);
341 cl
= container_of(du
, struct client
, dispatch
.ubus
);
343 json
= blobmsg_format_json(msg
, true);
345 ops
->chunk_printf(cl
, "event: %s\ndata: %s\n\n", method
, json
);
352 static void uh_ubus_subscription_notification_remove_cb(struct ubus_context
*ctx
, struct ubus_subscriber
*s
, uint32_t id
)
354 struct dispatch_ubus
*du
;
357 du
= container_of(s
, struct dispatch_ubus
, sub
);
358 cl
= container_of(du
, struct client
, dispatch
.ubus
);
360 ubus_unregister_subscriber(ctx
, &du
->sub
);
362 ops
->request_done(cl
);
365 static void uh_ubus_handle_get_subscribe(struct client
*cl
, const char *path
)
367 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
372 sid
= uh_ubus_get_auth(cl
->hdr
.head
);
374 if (!conf
.ubus_noauth
&& !uh_ubus_allowed(sid
, path
, ":subscribe")) {
375 uh_ubus_send_header(cl
, 200, "OK", "application/json");
376 uh_ubus_posix_error(cl
, EACCES
);
380 du
->sub
.cb
= uh_ubus_subscription_notification_cb
;
381 du
->sub
.remove_cb
= uh_ubus_subscription_notification_remove_cb
;
385 err
= ubus_register_subscriber(ctx
, &du
->sub
);
389 err
= ubus_lookup_id(ctx
, path
, &id
);
393 err
= ubus_subscribe(ctx
, &du
->sub
, id
);
397 uh_ubus_send_header(cl
, 200, "OK", "text/event-stream");
399 if (conf
.events_retry
)
400 ops
->chunk_printf(cl
, "retry: %d\n", conf
.events_retry
);
405 ubus_unregister_subscriber(ctx
, &du
->sub
);
409 uh_ubus_send_header(cl
, 200, "OK", "application/json");
410 uh_ubus_ubus_error(cl
, err
);
414 static void uh_ubus_handle_get(struct client
*cl
)
416 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
417 const char *url
= du
->url_path
;
419 url
+= strlen(conf
.ubus_prefix
);
421 if (!strcmp(url
, "/list") || !strncmp(url
, "/list/", strlen("/list/"))) {
422 url
+= strlen("/list");
424 uh_ubus_handle_get_list(cl
, *url
? url
+ 1 : NULL
);
425 } else if (!strncmp(url
, "/subscribe/", strlen("/subscribe/"))) {
426 url
+= strlen("/subscribe");
428 uh_ubus_handle_get_subscribe(cl
, url
+ 1);
430 ops
->http_header(cl
, 404, "Not Found");
431 ustream_printf(cl
->us
, "\r\n");
432 ops
->request_done(cl
);
436 /* POST requests handling */
439 uh_ubus_request_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
441 struct dispatch_ubus
*du
= container_of(req
, struct dispatch_ubus
, req
);
442 struct blob_attr
*cur
;
445 blob_for_each_attr(cur
, msg
, len
)
446 blobmsg_add_blob(&du
->buf
, cur
);
450 uh_ubus_request_cb(struct ubus_request
*req
, int ret
)
452 struct dispatch_ubus
*du
= container_of(req
, struct dispatch_ubus
, req
);
453 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
454 struct blob_attr
*cur
;
458 blob_buf_init(&buf
, 0);
460 uloop_timeout_cancel(&du
->timeout
);
462 /* Legacy format always uses "result" array - even for errors and empty
467 uh_ubus_init_json_rpc_response(cl
, &buf
);
468 r
= blobmsg_open_array(&buf
, "result");
469 blobmsg_add_u32(&buf
, "", ret
);
471 if (blob_len(du
->buf
.head
)) {
472 c
= blobmsg_open_table(&buf
, NULL
);
473 blob_for_each_attr(cur
, du
->buf
.head
, rem
)
474 blobmsg_add_blob(&buf
, cur
);
475 blobmsg_close_table(&buf
, c
);
478 blobmsg_close_array(&buf
, r
);
479 uh_ubus_send_response(cl
, &buf
);
486 uh_ubus_init_json_rpc_response(cl
, &buf
);
487 c
= blobmsg_open_table(&buf
, "error");
488 blobmsg_add_u32(&buf
, "code", ret
);
489 blobmsg_add_string(&buf
, "message", ubus_strerror(ret
));
490 blobmsg_close_table(&buf
, c
);
491 uh_ubus_send_response(cl
, &buf
);
493 uh_ubus_init_json_rpc_response(cl
, &buf
);
494 if (blob_len(du
->buf
.head
)) {
495 r
= blobmsg_open_table(&buf
, "result");
496 blob_for_each_attr(cur
, du
->buf
.head
, rem
)
497 blobmsg_add_blob(&buf
, cur
);
498 blobmsg_close_table(&buf
, r
);
500 blobmsg_add_field(&buf
, BLOBMSG_TYPE_UNSPEC
, "result", NULL
, 0);
502 uh_ubus_send_response(cl
, &buf
);
508 uh_ubus_timeout_cb(struct uloop_timeout
*timeout
)
510 struct dispatch_ubus
*du
= container_of(timeout
, struct dispatch_ubus
, timeout
);
511 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
513 ubus_abort_request(ctx
, &du
->req
);
514 uh_ubus_json_rpc_error(cl
, ERROR_TIMEOUT
);
517 static void uh_ubus_close_fds(struct client
*cl
)
519 if (ctx
->sock
.fd
< 0)
526 static void uh_ubus_request_free(struct client
*cl
)
528 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
530 blob_buf_free(&du
->buf
);
531 uloop_timeout_cancel(&du
->timeout
);
534 json_object_put(du
->jsobj
);
537 json_tokener_free(du
->jstok
);
540 ubus_abort_request(ctx
, &du
->req
);
546 static void uh_ubus_single_error(struct client
*cl
, enum rpc_error type
)
548 uh_ubus_send_header(cl
, 200, "OK", "application/json");
549 uh_ubus_json_rpc_error(cl
, type
);
550 ops
->request_done(cl
);
553 static void uh_ubus_send_request(struct client
*cl
, const char *sid
, struct blob_attr
*args
)
555 struct dispatch
*d
= &cl
->dispatch
;
556 struct dispatch_ubus
*du
= &d
->ubus
;
557 struct blob_attr
*cur
;
558 static struct blob_buf req
;
561 blob_buf_init(&req
, 0);
562 blobmsg_for_each_attr(cur
, args
, rem
) {
563 if (!strcmp(blobmsg_name(cur
), "ubus_rpc_session"))
564 return uh_ubus_json_rpc_error(cl
, ERROR_PARAMS
);
565 blobmsg_add_blob(&req
, cur
);
568 blobmsg_add_string(&req
, "ubus_rpc_session", sid
);
570 blob_buf_init(&du
->buf
, 0);
571 memset(&du
->req
, 0, sizeof(du
->req
));
572 ret
= ubus_invoke_async(ctx
, du
->obj
, du
->func
, req
.head
, &du
->req
);
574 return uh_ubus_json_rpc_error(cl
, ERROR_INTERNAL
);
576 du
->req
.data_cb
= uh_ubus_request_data_cb
;
577 du
->req
.complete_cb
= uh_ubus_request_cb
;
578 ubus_complete_request_async(ctx
, &du
->req
);
580 du
->timeout
.cb
= uh_ubus_timeout_cb
;
581 uloop_timeout_set(&du
->timeout
, conf
.script_timeout
* 1000);
583 du
->req_pending
= true;
586 static void uh_ubus_list_cb(struct ubus_context
*ctx
, struct ubus_object_data
*obj
, void *priv
)
588 struct blob_attr
*sig
, *attr
;
589 struct list_data
*data
= priv
;
593 if (!data
->verbose
) {
594 blobmsg_add_string(data
->buf
, NULL
, obj
->path
);
601 if (data
->add_object
) {
602 o
= blobmsg_open_table(data
->buf
, obj
->path
);
607 blob_for_each_attr(sig
, obj
->signature
, rem
) {
608 t
= blobmsg_open_table(data
->buf
, blobmsg_name(sig
));
609 rem2
= blobmsg_data_len(sig
);
610 __blob_for_each_attr(attr
, blobmsg_data(sig
), rem2
) {
611 if (blob_id(attr
) != BLOBMSG_TYPE_INT32
)
614 switch (blobmsg_get_u32(attr
)) {
615 case BLOBMSG_TYPE_INT8
:
616 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "boolean");
618 case BLOBMSG_TYPE_INT32
:
619 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "number");
621 case BLOBMSG_TYPE_STRING
:
622 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "string");
624 case BLOBMSG_TYPE_ARRAY
:
625 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "array");
627 case BLOBMSG_TYPE_TABLE
:
628 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "object");
631 blobmsg_add_string(data
->buf
, blobmsg_name(attr
), "unknown");
635 blobmsg_close_table(data
->buf
, t
);
638 if (data
->add_object
)
639 blobmsg_close_table(data
->buf
, o
);
642 static void uh_ubus_send_list(struct client
*cl
, struct blob_attr
*params
)
644 struct blob_attr
*cur
, *dup
;
645 struct list_data data
= { .buf
= &cl
->dispatch
.ubus
.buf
, .verbose
= false, .add_object
= true };
649 blob_buf_init(data
.buf
, 0);
653 if (!params
|| blob_id(params
) != BLOBMSG_TYPE_ARRAY
) {
654 r
= blobmsg_open_array(data
.buf
, "result");
655 ubus_lookup(ctx
, NULL
, uh_ubus_list_cb
, &data
);
656 blobmsg_close_array(data
.buf
, r
);
659 r
= blobmsg_open_table(data
.buf
, "result");
660 dup
= blob_memdup(params
);
663 rem
= blobmsg_data_len(dup
);
665 __blob_for_each_attr(cur
, blobmsg_data(dup
), rem
)
666 ubus_lookup(ctx
, blobmsg_data(cur
), uh_ubus_list_cb
, &data
);
669 blobmsg_close_table(data
.buf
, r
);
674 blob_buf_init(&buf
, 0);
675 uh_ubus_init_json_rpc_response(cl
, &buf
);
676 blobmsg_add_blob(&buf
, blob_data(data
.buf
->head
));
677 uh_ubus_send_response(cl
, &buf
);
680 static bool parse_json_rpc(struct rpc_data
*d
, struct blob_attr
*data
)
682 struct blob_attr
*tb
[__RPC_MAX
];
683 struct blob_attr
*cur
;
685 blobmsg_parse(rpc_policy
, __RPC_MAX
, tb
, blob_data(data
), blob_len(data
));
687 cur
= tb
[RPC_JSONRPC
];
688 if (!cur
|| strcmp(blobmsg_data(cur
), "2.0") != 0)
691 cur
= tb
[RPC_METHOD
];
696 d
->method
= blobmsg_data(cur
);
698 cur
= tb
[RPC_PARAMS
];
702 d
->params
= blob_memdup(cur
);
709 static void parse_call_params(struct rpc_data
*d
)
711 const struct blobmsg_policy data_policy
[] = {
712 { .type
= BLOBMSG_TYPE_STRING
},
713 { .type
= BLOBMSG_TYPE_STRING
},
714 { .type
= BLOBMSG_TYPE_STRING
},
715 { .type
= BLOBMSG_TYPE_TABLE
},
717 struct blob_attr
*tb
[4];
719 if (!d
->params
|| blobmsg_type(d
->params
) != BLOBMSG_TYPE_ARRAY
)
722 blobmsg_parse_array(data_policy
, ARRAY_SIZE(data_policy
), tb
,
723 blobmsg_data(d
->params
), blobmsg_data_len(d
->params
));
726 d
->sid
= blobmsg_data(tb
[0]);
728 if (conf
.ubus_noauth
&& (!d
->sid
|| !*d
->sid
))
729 d
->sid
= UH_UBUS_DEFAULT_SID
;
732 d
->object
= blobmsg_data(tb
[1]);
735 d
->function
= blobmsg_data(tb
[2]);
740 static void uh_ubus_init_batch(struct client
*cl
)
742 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
745 uh_ubus_send_header(cl
, 200, "OK", "application/json");
746 ops
->chunk_printf(cl
, "[");
749 static void uh_ubus_complete_batch(struct client
*cl
)
751 ops
->chunk_printf(cl
, "]");
752 ops
->request_done(cl
);
755 static void uh_ubus_handle_request_object(struct client
*cl
, struct json_object
*obj
)
757 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
758 struct rpc_data data
= {};
759 enum rpc_error err
= ERROR_PARSE
;
760 static struct blob_buf req
;
764 if (json_object_get_type(obj
) != json_type_object
)
768 blob_buf_init(&req
, 0);
769 if (!blobmsg_add_object(&req
, obj
))
772 if (!parse_json_rpc(&data
, req
.head
))
775 if (!strcmp(data
.method
, "call")) {
776 parse_call_params(&data
);
778 if (!data
.sid
|| !data
.object
|| !data
.function
|| !data
.data
)
781 du
->func
= data
.function
;
782 if (ubus_lookup_id(ctx
, data
.object
, &du
->obj
)) {
787 if (!conf
.ubus_noauth
&& !uh_ubus_allowed(data
.sid
, data
.object
, data
.function
)) {
792 uh_ubus_send_request(cl
, data
.sid
, data
.data
);
795 else if (!strcmp(data
.method
, "list")) {
796 uh_ubus_send_list(cl
, data
.params
);
805 uh_ubus_json_rpc_error(cl
, err
);
813 static void __uh_ubus_next_batched_request(struct uloop_timeout
*timeout
)
815 struct dispatch_ubus
*du
= container_of(timeout
, struct dispatch_ubus
, timeout
);
816 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
817 struct json_object
*obj
= du
->jsobj
;
820 len
= json_object_array_length(obj
);
821 if (du
->array_idx
>= len
)
822 return uh_ubus_complete_batch(cl
);
824 obj
= json_object_array_get_idx(obj
, du
->array_idx
++);
825 uh_ubus_handle_request_object(cl
, obj
);
828 static void uh_ubus_data_done(struct client
*cl
)
830 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
831 struct json_object
*obj
= du
->jsobj
;
833 switch (obj
? json_object_get_type(obj
) : json_type_null
) {
834 case json_type_object
:
835 uh_ubus_send_header(cl
, 200, "OK", "application/json");
836 return uh_ubus_handle_request_object(cl
, obj
);
837 case json_type_array
:
838 uh_ubus_init_batch(cl
);
839 return uh_ubus_next_batched_request(cl
);
841 return uh_ubus_single_error(cl
, ERROR_PARSE
);
845 static void uh_ubus_call(struct client
*cl
, const char *path
, const char *sid
)
847 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
848 struct json_object
*obj
= du
->jsobj
;
849 struct rpc_data data
= {};
850 enum rpc_error err
= ERROR_PARSE
;
851 static struct blob_buf req
;
855 if (!obj
|| json_object_get_type(obj
) != json_type_object
)
858 uh_ubus_send_header(cl
, 200, "OK", "application/json");
861 blob_buf_init(&req
, 0);
862 if (!blobmsg_add_object(&req
, obj
))
865 if (!parse_json_rpc(&data
, req
.head
))
868 du
->func
= data
.method
;
869 if (ubus_lookup_id(ctx
, path
, &du
->obj
)) {
874 if (!conf
.ubus_noauth
&& !uh_ubus_allowed(sid
, path
, data
.method
)) {
879 uh_ubus_send_request(cl
, sid
, data
.params
);
883 uh_ubus_json_rpc_error(cl
, err
);
891 static void uh_ubus_handle_post(struct client
*cl
)
893 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
894 const char *url
= du
->url_path
;
897 /* Treat both: /foo AND /foo/ as legacy requests. */
898 if (ops
->path_match(conf
.ubus_prefix
, url
) && strlen(url
) - strlen(conf
.ubus_prefix
) <= 1) {
900 uh_ubus_data_done(cl
);
904 auth
= uh_ubus_get_auth(cl
->hdr
.head
);
906 url
+= strlen(conf
.ubus_prefix
);
908 if (!strncmp(url
, "/call/", strlen("/call/"))) {
909 url
+= strlen("/call/");
911 uh_ubus_call(cl
, url
, auth
);
913 ops
->http_header(cl
, 404, "Not Found");
914 ustream_printf(cl
->us
, "\r\n");
915 ops
->request_done(cl
);
919 static int uh_ubus_data_send(struct client
*cl
, const char *data
, int len
)
921 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
923 if (du
->jsobj
|| !du
->jstok
)
927 if (du
->post_len
> UH_UBUS_MAX_POST_SIZE
)
930 du
->jsobj
= json_tokener_parse_ex(du
->jstok
, data
, len
);
934 uh_ubus_single_error(cl
, ERROR_PARSE
);
938 static void uh_ubus_handle_request(struct client
*cl
, char *url
, struct path_info
*pi
)
940 struct dispatch
*d
= &cl
->dispatch
;
941 struct dispatch_ubus
*du
= &d
->ubus
;
944 du
->url_path
= strdup(url
);
946 ops
->client_error(cl
, 500, "Internal Server Error", "Failed to allocate resources");
949 chr
= strchr(du
->url_path
, '?');
955 switch (cl
->request
.method
)
957 case UH_HTTP_MSG_GET
:
958 uh_ubus_handle_get(cl
);
960 case UH_HTTP_MSG_POST
:
961 d
->data_send
= uh_ubus_data_send
;
962 d
->data_done
= uh_ubus_handle_post
;
963 d
->close_fds
= uh_ubus_close_fds
;
964 d
->free
= uh_ubus_request_free
;
965 du
->jstok
= json_tokener_new();
968 case UH_HTTP_MSG_OPTIONS
:
969 uh_ubus_send_header(cl
, 200, "OK", "application/json");
970 ops
->request_done(cl
);
974 ops
->client_error(cl
, 400, "Bad Request", "Invalid Request");
982 uh_ubus_check_url(const char *url
)
984 return ops
->path_match(conf
.ubus_prefix
, url
);
990 static struct dispatch_handler ubus_dispatch
= {
991 .check_url
= uh_ubus_check_url
,
992 .handle_request
= uh_ubus_handle_request
,
995 ctx
= ubus_connect(conf
.ubus_socket
);
997 fprintf(stderr
, "Unable to connect to ubus socket\n");
1001 ops
->dispatch_add(&ubus_dispatch
);
1008 static int uh_ubus_plugin_init(const struct uhttpd_ops
*o
, struct config
*c
)
1012 return uh_ubus_init();
1015 static void uh_ubus_post_init(void)
1017 ubus_add_uloop(ctx
);
1020 struct uhttpd_plugin uhttpd_plugin
= {
1021 .init
= uh_ubus_plugin_init
,
1022 .post_init
= uh_ubus_post_init
,