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 <ucode/compiler.h>
22 #include <ucode/lib.h>
30 #define UH_UCODE_CB "handle_request"
32 static const struct uhttpd_ops
*ops
;
33 static struct config
*_conf
;
36 static struct ucode_prefix
*current_prefix
;
39 uh_ucode_recv(uc_vm_t
*vm
, size_t nargs
)
41 static struct pollfd pfd
= { .fd
= STDIN_FILENO
, .events
= POLLIN
};
42 int data_len
= 0, len
= BUFSIZ
, rlen
, r
;
43 uc_value_t
*v
= uc_fn_arg(0);
46 if (ucv_type(v
) == UC_INTEGER
) {
47 len
= ucv_int64_get(v
);
50 uc_vm_raise_exception(vm
, EXCEPTION_TYPE
, "Argument not an integer");
55 buf
= ucv_stringbuf_new();
58 rlen
= (len
< BUFSIZ
) ? len
: BUFSIZ
;
60 if (printbuf_memset(buf
, -1, 0, rlen
)) {
61 uc_vm_raise_exception(vm
, EXCEPTION_RUNTIME
, "Out of memory");
68 r
= read(STDIN_FILENO
, buf
->buf
+ buf
->bpos
, rlen
);
71 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
) {
75 if (pfd
.revents
& POLLIN
)
97 /* add final guard \0 but do not count it */
98 if (printbuf_memset(buf
, -1, 0, 1)) {
99 uc_vm_raise_exception(vm
, EXCEPTION_RUNTIME
, "Out of memory");
107 return ucv_stringbuf_finish(buf
);
116 uh_ucode_send(uc_vm_t
*vm
, size_t nargs
)
123 for (arridx
= 0; arridx
< nargs
; arridx
++) {
124 val
= uc_fn_arg(arridx
);
126 if (ucv_type(val
) == UC_STRING
) {
127 len
+= write(STDOUT_FILENO
, ucv_string_get(val
), ucv_string_length(val
));
129 else if (val
!= NULL
) {
130 p
= ucv_to_string(vm
, val
);
131 len
+= p
? write(STDOUT_FILENO
, p
, strlen(p
)) : 0;
136 return ucv_int64_new(len
);
140 uh_ucode_strconvert(uc_vm_t
*vm
, size_t nargs
, int (*convert
)(char *, int, const char *, int))
142 uc_value_t
*val
= uc_fn_arg(0);
143 static char out_buf
[4096];
147 if (ucv_type(val
) == UC_STRING
) {
148 out_len
= convert(out_buf
, sizeof(out_buf
),
149 ucv_string_get(val
), ucv_string_length(val
));
151 else if (val
!= NULL
) {
152 p
= ucv_to_string(vm
, val
);
153 out_len
= p
? convert(out_buf
, sizeof(out_buf
), p
, strlen(p
)) : 0;
164 error
= "buffer overflow";
166 error
= "malformed string";
168 uc_vm_raise_exception(vm
, EXCEPTION_RUNTIME
,
169 "%s on URL conversion\n", error
);
174 return ucv_string_new_length(out_buf
, out_len
);
178 uh_ucode_urldecode(uc_vm_t
*vm
, size_t nargs
)
180 return uh_ucode_strconvert(vm
, nargs
, ops
->urldecode
);
184 uh_ucode_urlencode(uc_vm_t
*vm
, size_t nargs
)
186 return uh_ucode_strconvert(vm
, nargs
, ops
->urlencode
);
189 static uc_parse_config_t config
= {
190 .strict_declarations
= false,
191 .lstrip_blocks
= true,
196 uh_ucode_exception(uc_vm_t
*vm
, uc_exception_t
*ex
)
200 if (ex
->type
== EXCEPTION_EXIT
)
203 printf("Status: 500 Internal Server Error\r\n\r\n"
204 "Exception while executing ucode program %s:\n",
205 current_prefix
->handler
);
208 case EXCEPTION_SYNTAX
: printf("Syntax error"); break;
209 case EXCEPTION_RUNTIME
: printf("Runtime error"); break;
210 case EXCEPTION_TYPE
: printf("Type error"); break;
211 case EXCEPTION_REFERENCE
: printf("Reference error"); break;
212 default: printf("Error");
215 printf(": %s\n", ex
->message
);
217 ctx
= ucv_object_get(ucv_array_get(ex
->stacktrace
, 0), "context", NULL
);
220 printf("%s\n", ucv_string_get(ctx
));
224 uh_ucode_state_init(struct ucode_prefix
*ucode
)
226 char *syntax_error
= NULL
;
227 uc_vm_t
*vm
= &ucode
->ctx
;
228 uc_program_t
*handler
;
229 uc_vm_status_t status
;
234 uc_search_path_init(&config
.module_search_path
);
235 uc_vm_init(vm
, &config
);
236 uc_stdlib_load(uc_vm_scope_get(vm
));
238 /* build uhttpd api table */
239 v
= ucv_object_new(vm
);
241 ucv_object_add(v
, "send", ucv_cfunction_new("send", uh_ucode_send
));
242 ucv_object_add(v
, "sendc", ucv_get(ucv_object_get(v
, "send", NULL
)));
243 ucv_object_add(v
, "recv", ucv_cfunction_new("recv", uh_ucode_recv
));
244 ucv_object_add(v
, "urldecode", ucv_cfunction_new("urldecode", uh_ucode_urldecode
));
245 ucv_object_add(v
, "urlencode", ucv_cfunction_new("urlencode", uh_ucode_urlencode
));
246 ucv_object_add(v
, "docroot", ucv_string_new(conf
.docroot
));
248 ucv_object_add(uc_vm_scope_get(vm
), "uhttpd", v
);
250 src
= uc_source_new_file(ucode
->handler
);
253 fprintf(stderr
, "Error: Unable to open ucode handler: %s\n",
259 handler
= uc_compile(&config
, src
, &syntax_error
);
264 fprintf(stderr
, "Error: Unable to compile ucode handler: %s\n",
272 vm
->output
= fopen("/dev/null", "w");
275 fprintf(stderr
, "Error: Unable to open /dev/null for writing: %s\n",
281 status
= uc_vm_execute(vm
, handler
, &v
);
282 exitcode
= (int)ucv_int64_get(v
);
284 uc_program_put(handler
);
292 fprintf(stderr
, "Error: The ucode handler invoked exit(%d)\n", exitcode
);
293 exit(exitcode
? exitcode
: 1);
296 fprintf(stderr
, "Error: Compilation error while executing ucode handler\n");
300 fprintf(stderr
, "Error: Runtime error while executing ucode handler\n");
304 v
= ucv_object_get(uc_vm_scope_get(vm
), UH_UCODE_CB
, NULL
);
306 if (!ucv_is_callable(v
)) {
307 fprintf(stderr
, "Error: The ucode handler declares no " UH_UCODE_CB
"() callback.\n");
311 uc_vm_exception_handler_set(vm
, uh_ucode_exception
);
321 ucode_main(struct client
*cl
, struct path_info
*pi
, char *url
)
323 uc_vm_t
*vm
= ¤t_prefix
->ctx
;
324 uc_value_t
*req
, *hdr
, *res
;
325 int path_len
, prefix_len
;
326 struct blob_attr
*cur
;
331 /* new env table for this request */
332 req
= ucv_object_new(vm
);
334 prefix_len
= strlen(pi
->name
);
335 path_len
= strlen(url
);
336 str
= strchr(url
, '?');
342 path_len
= str
- url
;
345 if (prefix_len
> 0 && pi
->name
[prefix_len
- 1] == '/')
348 if (path_len
> prefix_len
) {
349 ucv_object_add(req
, "PATH_INFO",
350 ucv_string_new_length(url
+ prefix_len
, path_len
- prefix_len
));
353 for (var
= ops
->get_process_vars(cl
, pi
); var
->name
; var
++) {
357 ucv_object_add(req
, var
->name
, ucv_string_new(var
->value
));
360 ucv_object_add(req
, "HTTP_VERSION",
361 ucv_double_new(0.9 + (cl
->request
.version
/ 10.0)));
363 hdr
= ucv_object_new(vm
);
365 blob_for_each_attr(cur
, cl
->hdr
.head
, rem
)
366 ucv_object_add(hdr
, blobmsg_name(cur
), ucv_string_new(blobmsg_data(cur
)));
368 ucv_object_add(req
, "headers", hdr
);
370 res
= uc_vm_invoke(vm
, UH_UCODE_CB
, 1, req
);
379 ucode_handle_request(struct client
*cl
, char *url
, struct path_info
*pi
)
381 struct ucode_prefix
*p
;
382 static struct path_info _pi
;
384 list_for_each_entry(p
, &conf
.ucode_prefix
, list
) {
385 if (!ops
->path_match(p
->prefix
, url
))
389 pi
->name
= p
->prefix
;
390 pi
->phys
= p
->handler
;
394 if (!ops
->create_process(cl
, pi
, url
, ucode_main
)) {
395 ops
->client_error(cl
, 500, "Internal Server Error",
396 "Failed to create CGI process: %s",
403 ops
->client_error(cl
, 500, "Internal Server Error",
404 "Failed to lookup matching handler");
408 check_ucode_url(const char *url
)
410 struct ucode_prefix
*p
;
412 list_for_each_entry(p
, &conf
.ucode_prefix
, list
)
413 if (ops
->path_match(p
->prefix
, url
))
419 static struct dispatch_handler ucode_dispatch
= {
421 .check_url
= check_ucode_url
,
422 .handle_request
= ucode_handle_request
,
426 ucode_plugin_init(const struct uhttpd_ops
*o
, struct config
*c
)
428 struct ucode_prefix
*p
;
433 list_for_each_entry(p
, &conf
.ucode_prefix
, list
)
434 uh_ucode_state_init(p
);
436 ops
->dispatch_add(&ucode_dispatch
);
440 struct uhttpd_plugin uhttpd_plugin
= {
441 .init
= ucode_plugin_init
,