2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <libubox/blobmsg.h>
22 #include <libubox/blobmsg_json.h>
23 #include <libubox/avl.h>
24 #include <libubox/avl-cmp.h>
30 #include "ubus-session.h"
32 static const struct uhttpd_ops
*ops
;
33 static struct config
*_conf
;
36 static struct ubus_context
*ctx
;
37 static struct blob_buf buf
;
39 #define UH_UBUS_MAX_POST_SIZE 4096
49 static const struct blobmsg_policy rpc_policy
[__RPC_MAX
] = {
50 [RPC_JSONRPC
] = { .name
= "jsonrpc", .type
= BLOBMSG_TYPE_STRING
},
51 [RPC_METHOD
] = { .name
= "method", .type
= BLOBMSG_TYPE_STRING
},
52 [RPC_PARAMS
] = { .name
= "params", .type
= BLOBMSG_TYPE_ARRAY
},
53 [RPC_ID
] = { .name
= "id", .type
= BLOBMSG_TYPE_UNSPEC
},
61 struct blob_attr
*data
;
80 } json_errors
[__ERROR_MAX
] = {
81 [ERROR_PARSE
] = { -32700, "Parse error" },
82 [ERROR_REQUEST
] = { -32600, "Invalid request" },
83 [ERROR_METHOD
] = { -32601, "Method not found" },
84 [ERROR_PARAMS
] = { -32602, "Invalid parameters" },
85 [ERROR_INTERNAL
] = { -32603, "Internal error" },
86 [ERROR_OBJECT
] = { -32000, "Object not found" },
87 [ERROR_SESSION
] = { -32001, "Session not found" },
88 [ERROR_ACCESS
] = { -32002, "Access denied" },
89 [ERROR_TIMEOUT
] = { -32003, "ubus request timed out" },
92 static void __uh_ubus_next_batched_request(struct uloop_timeout
*timeout
);
94 static void uh_ubus_next_batched_request(struct client
*cl
)
96 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
98 du
->timeout
.cb
= __uh_ubus_next_batched_request
;
99 uloop_timeout_set(&du
->timeout
, 1);
102 static void uh_ubus_send_header(struct client
*cl
)
104 ops
->http_header(cl
, 200, "OK");
105 ustream_printf(cl
->us
, "Content-Type: application/json\r\n\r\n");
108 static void uh_ubus_send_response(struct client
*cl
)
110 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
111 const char *sep
= "";
114 if (du
->array
&& du
->array_idx
> 1)
117 str
= blobmsg_format_json_indent(buf
.head
, true, du
->array
);
118 ops
->chunk_printf(cl
, "%s%s", sep
, str
);
121 du
->jsobj_cur
= NULL
;
123 uh_ubus_next_batched_request(cl
);
125 ops
->chunk_printf(cl
, "\n");
126 return ops
->request_done(cl
);
130 static void uh_ubus_init_response(struct client
*cl
)
132 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
133 struct json_object
*obj
= du
->jsobj_cur
;
135 blob_buf_init(&buf
, 0);
136 blobmsg_add_string(&buf
, "jsonrpc", "2.0");
139 obj
= json_object_object_get(obj
, "id");
142 blobmsg_add_json_element(&buf
, "id", obj
);
144 blobmsg_add_field(&buf
, BLOBMSG_TYPE_UNSPEC
, "id", NULL
, 0);
147 static void uh_ubus_json_error(struct client
*cl
, enum rpc_error type
)
151 uh_ubus_init_response(cl
);
152 c
= blobmsg_open_table(&buf
, "error");
153 blobmsg_add_u32(&buf
, "code", json_errors
[type
].code
);
154 blobmsg_add_string(&buf
, "message", json_errors
[type
].msg
);
155 blobmsg_close_table(&buf
, c
);
156 uh_ubus_send_response(cl
);
160 uh_ubus_request_data_cb(struct ubus_request
*req
, int type
, struct blob_attr
*msg
)
162 struct dispatch_ubus
*du
= container_of(req
, struct dispatch_ubus
, req
);
164 blobmsg_add_field(&du
->buf
, BLOBMSG_TYPE_TABLE
, "", blob_data(msg
), blob_len(msg
));
168 uh_ubus_request_cb(struct ubus_request
*req
, int ret
)
170 struct dispatch_ubus
*du
= container_of(req
, struct dispatch_ubus
, req
);
171 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
172 struct blob_attr
*cur
;
176 uloop_timeout_cancel(&du
->timeout
);
177 uh_ubus_init_response(cl
);
178 r
= blobmsg_open_array(&buf
, "result");
179 blobmsg_add_u32(&buf
, "", ret
);
180 blob_for_each_attr(cur
, du
->buf
.head
, rem
)
181 blobmsg_add_blob(&buf
, cur
);
182 blobmsg_close_array(&buf
, r
);
183 uh_ubus_send_response(cl
);
187 uh_ubus_timeout_cb(struct uloop_timeout
*timeout
)
189 struct dispatch_ubus
*du
= container_of(timeout
, struct dispatch_ubus
, timeout
);
190 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
192 ubus_abort_request(ctx
, &du
->req
);
193 uh_ubus_json_error(cl
, ERROR_TIMEOUT
);
196 static void uh_ubus_close_fds(struct client
*cl
)
198 if (ctx
->sock
.fd
< 0)
205 static void uh_ubus_request_free(struct client
*cl
)
207 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
209 blob_buf_free(&du
->buf
);
210 uloop_timeout_cancel(&cl
->timeout
);
213 json_object_put(du
->jsobj
);
216 json_tokener_free(du
->jstok
);
219 ubus_abort_request(ctx
, &du
->req
);
222 static void uh_ubus_single_error(struct client
*cl
, enum rpc_error type
)
224 uh_ubus_send_header(cl
);
225 uh_ubus_json_error(cl
, type
);
226 ops
->request_done(cl
);
229 static void uh_ubus_send_request(struct client
*cl
, json_object
*obj
)
231 struct dispatch
*d
= &cl
->dispatch
;
232 struct dispatch_ubus
*du
= &d
->ubus
;
235 blob_buf_init(&du
->buf
, 0);
236 memset(&du
->req
, 0, sizeof(du
->req
));
237 ret
= ubus_invoke_async(ctx
, du
->obj
, du
->func
, buf
.head
, &du
->req
);
239 return uh_ubus_json_error(cl
, ERROR_INTERNAL
);
241 du
->req
.data_cb
= uh_ubus_request_data_cb
;
242 du
->req
.complete_cb
= uh_ubus_request_cb
;
243 ubus_complete_request_async(ctx
, &du
->req
);
245 du
->timeout
.cb
= uh_ubus_timeout_cb
;
246 uloop_timeout_set(&du
->timeout
, conf
.script_timeout
);
248 du
->req_pending
= true;
251 static bool parse_json_rpc(struct rpc_data
*d
, struct blob_attr
*data
)
253 const struct blobmsg_policy data_policy
[] = {
254 { .type
= BLOBMSG_TYPE_STRING
},
255 { .type
= BLOBMSG_TYPE_STRING
},
256 { .type
= BLOBMSG_TYPE_TABLE
},
258 struct blob_attr
*tb
[__RPC_MAX
];
259 struct blob_attr
*tb2
[3];
260 struct blob_attr
*cur
;
262 blobmsg_parse(rpc_policy
, __RPC_MAX
, tb
, blob_data(data
), blob_len(data
));
264 cur
= tb
[RPC_JSONRPC
];
265 if (!cur
|| strcmp(blobmsg_data(cur
), "2.0") != 0)
268 cur
= tb
[RPC_METHOD
];
273 d
->method
= blobmsg_data(cur
);
275 cur
= tb
[RPC_PARAMS
];
279 blobmsg_parse_array(data_policy
, ARRAY_SIZE(data_policy
), tb2
,
280 blobmsg_data(cur
), blobmsg_data_len(cur
));
282 if (!tb2
[0] || !tb2
[1] || !tb2
[2])
285 d
->object
= blobmsg_data(tb2
[0]);
286 d
->function
= blobmsg_data(tb2
[1]);
291 static void uh_ubus_init_batch(struct client
*cl
)
293 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
296 uh_ubus_send_header(cl
);
297 ops
->chunk_printf(cl
, "[\n\t");
300 static void uh_ubus_complete_batch(struct client
*cl
)
302 ops
->chunk_printf(cl
, "\n]\n");
303 ops
->request_done(cl
);
306 static void uh_ubus_handle_request_object(struct client
*cl
, struct json_object
*obj
)
308 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
309 struct uh_ubus_session
*ses
;
310 struct rpc_data data
= {};
311 enum rpc_error err
= ERROR_PARSE
;
313 if (json_object_get_type(obj
) != json_type_object
)
317 blob_buf_init(&buf
, 0);
318 if (!blobmsg_add_object(&buf
, obj
))
321 if (!parse_json_rpc(&data
, buf
.head
))
324 if (strcmp(data
.method
, "call") != 0) {
329 ses
= uh_ubus_session_get(du
->sid
);
335 if (!uh_ubus_session_acl_allowed(ses
, data
.object
, data
.function
)) {
340 du
->func
= data
.function
;
341 if (ubus_lookup_id(ctx
, data
.object
, &du
->obj
)) {
346 uh_ubus_send_request(cl
, obj
);
350 uh_ubus_json_error(cl
, err
);
353 static void __uh_ubus_next_batched_request(struct uloop_timeout
*timeout
)
355 struct dispatch_ubus
*du
= container_of(timeout
, struct dispatch_ubus
, timeout
);
356 struct client
*cl
= container_of(du
, struct client
, dispatch
.ubus
);
357 struct json_object
*obj
= du
->jsobj
;
360 len
= json_object_array_length(obj
);
361 if (du
->array_idx
>= len
)
362 return uh_ubus_complete_batch(cl
);
364 obj
= json_object_array_get_idx(obj
, du
->array_idx
++);
365 uh_ubus_handle_request_object(cl
, obj
);
368 static void uh_ubus_data_done(struct client
*cl
)
370 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
371 struct json_object
*obj
= du
->jsobj
;
373 switch (obj
? json_object_get_type(obj
) : json_type_null
) {
374 case json_type_object
:
375 uh_ubus_send_header(cl
);
376 return uh_ubus_handle_request_object(cl
, obj
);
377 case json_type_array
:
378 uh_ubus_init_batch(cl
);
379 if (json_object_array_length(obj
) > 0)
380 return uh_ubus_next_batched_request(cl
);
383 return uh_ubus_single_error(cl
, ERROR_PARSE
);
387 static int uh_ubus_data_send(struct client
*cl
, const char *data
, int len
)
389 struct dispatch_ubus
*du
= &cl
->dispatch
.ubus
;
391 if (du
->jsobj
|| !du
->jstok
)
395 if (du
->post_len
> UH_UBUS_MAX_POST_SIZE
)
398 du
->jsobj
= json_tokener_parse_ex(du
->jstok
, data
, len
);
402 uh_ubus_single_error(cl
, ERROR_PARSE
);
406 static void uh_ubus_handle_request(struct client
*cl
, char *url
, struct path_info
*pi
)
408 struct dispatch
*d
= &cl
->dispatch
;
411 blob_buf_init(&buf
, 0);
413 url
+= strlen(conf
.ubus_prefix
);
417 sep
= strchr(url
, '/');
422 if (strlen(sid
) != 32 ||
423 cl
->request
.method
!= UH_HTTP_MSG_POST
)
424 return ops
->client_error(cl
, 400, "Bad Request", "Invalid Request");
426 d
->close_fds
= uh_ubus_close_fds
;
427 d
->free
= uh_ubus_request_free
;
428 d
->data_send
= uh_ubus_data_send
;
429 d
->data_done
= uh_ubus_data_done
;
430 d
->ubus
.jstok
= json_tokener_new();
435 uh_ubus_check_url(const char *url
)
437 return ops
->path_match(conf
.ubus_prefix
, url
);
443 static struct dispatch_handler ubus_dispatch
= {
444 .check_url
= uh_ubus_check_url
,
445 .handle_request
= uh_ubus_handle_request
,
448 ctx
= ubus_connect(conf
.ubus_socket
);
450 fprintf(stderr
, "Unable to connect to ubus socket\n");
454 ops
->dispatch_add(&ubus_dispatch
);
455 if (ubus_session_api_init(ctx
)) {
456 fprintf(stderr
, "Unable to initialize ubus session API\n");
465 static int uh_ubus_plugin_init(const struct uhttpd_ops
*o
, struct config
*c
)
469 return uh_ubus_init();
472 static void uh_ubus_post_init(void)
477 const struct uhttpd_plugin uhttpd_plugin
= {
478 .init
= uh_ubus_plugin_init
,
479 .post_init
= uh_ubus_post_init
,