2
0

handler.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * uhttpd - Tiny single-threaded httpd
  3. *
  4. * Copyright (C) 2015 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <libubox/blobmsg.h>
  19. #include <libubox/blobmsg_json.h>
  20. #include <libubox/json_script.h>
  21. #include "uhttpd.h"
  22. struct handler {
  23. struct list_head list;
  24. struct json_script_file *request;
  25. struct json_script_file *fallback;
  26. };
  27. static LIST_HEAD(handlers);
  28. static struct json_script_ctx handler_ctx;
  29. static struct env_var *cur_vars;
  30. static struct blob_buf b;
  31. static int handler_ret;
  32. static struct client *cur_client;
  33. static char **cur_url;
  34. static void
  35. handle_redirect(struct json_script_ctx *ctx, struct blob_attr *data)
  36. {
  37. struct client *cl = cur_client;
  38. static struct blobmsg_policy policy[3] = {
  39. { .type = BLOBMSG_TYPE_STRING },
  40. { .type = BLOBMSG_TYPE_INT32 },
  41. { .type = BLOBMSG_TYPE_STRING },
  42. };
  43. struct blob_attr *tb[3];
  44. const char *status = "Found";
  45. int code = 302;
  46. blobmsg_parse_array(policy, ARRAY_SIZE(policy), tb, blobmsg_data(data), blobmsg_data_len(data));
  47. if (!tb[0])
  48. return;
  49. if (tb[1]) {
  50. code = blobmsg_get_u32(tb[1]);
  51. if (tb[2])
  52. status = blobmsg_get_string(tb[2]);
  53. }
  54. uh_http_header(cl, code, status);
  55. if (!uh_use_chunked(cl))
  56. ustream_printf(cl->us, "Content-Length: 0\r\n");
  57. ustream_printf(cl->us, "Location: %s\r\n\r\n",
  58. blobmsg_get_string(tb[0]));
  59. uh_request_done(cl);
  60. *cur_url = NULL;
  61. handler_ret = 1;
  62. json_script_abort(ctx);
  63. }
  64. static void
  65. handle_set_uri(struct json_script_ctx *ctx, struct blob_attr *data)
  66. {
  67. struct client *cl = cur_client;
  68. static struct blobmsg_policy policy = {
  69. .type = BLOBMSG_TYPE_STRING,
  70. };
  71. struct blob_attr *tb;
  72. struct blob_attr *old_url = blob_data(cl->hdr.head);
  73. blobmsg_parse_array(&policy, 1, &tb, blobmsg_data(data), blobmsg_data_len(data));
  74. if (!tb)
  75. return;
  76. blob_buf_init(&b, 0);
  77. blob_put_raw(&b, blob_next(old_url), blob_len(cl->hdr.head) - blob_pad_len(old_url));
  78. /* replace URL in client header cache */
  79. blob_buf_init(&cl->hdr, 0);
  80. blobmsg_add_string(&cl->hdr, "URL", blobmsg_get_string(tb));
  81. blob_put_raw(&cl->hdr, blob_data(b.head), blob_len(b.head));
  82. *cur_url = blobmsg_data(blob_data(cl->hdr.head));
  83. cur_vars = NULL;
  84. blob_buf_init(&b, 0);
  85. handler_ret = 1;
  86. json_script_abort(ctx);
  87. }
  88. static void
  89. handle_add_header(struct json_script_ctx *ctx, struct blob_attr *data)
  90. {
  91. struct client *cl = cur_client;
  92. static struct blobmsg_policy policy[2] = {
  93. { .type = BLOBMSG_TYPE_STRING },
  94. { .type = BLOBMSG_TYPE_STRING },
  95. };
  96. struct blob_attr *tb[2];
  97. blobmsg_parse_array(policy, ARRAY_SIZE(tb), tb, blobmsg_data(data), blobmsg_data_len(data));
  98. if (!tb[0] || !tb[1])
  99. return;
  100. blobmsg_add_string(&cl->hdr_response, blobmsg_get_string(tb[0]),
  101. blobmsg_get_string(tb[1]));
  102. }
  103. static void
  104. handle_no_cache(struct json_script_ctx *ctx, struct blob_attr *data)
  105. {
  106. struct client *cl = cur_client;
  107. cl->dispatch.no_cache = true;
  108. }
  109. static void
  110. handle_command(struct json_script_ctx *ctx, const char *name,
  111. struct blob_attr *data, struct blob_attr *vars)
  112. {
  113. static const struct {
  114. const char *name;
  115. void (*func)(struct json_script_ctx *ctx, struct blob_attr *data);
  116. } cmds[] = {
  117. { "redirect", handle_redirect },
  118. { "rewrite", handle_set_uri },
  119. { "add-header", handle_add_header },
  120. { "no-cache", handle_no_cache },
  121. };
  122. int i;
  123. for (i = 0; i < ARRAY_SIZE(cmds); i++) {
  124. if (!strcmp(cmds[i].name, name)) {
  125. cmds[i].func(ctx, data);
  126. return;
  127. }
  128. }
  129. }
  130. static const char *
  131. handle_var(struct json_script_ctx *ctx, const char *name,
  132. struct blob_attr *vars)
  133. {
  134. struct client *cl = cur_client;
  135. struct env_var *cur;
  136. static struct path_info empty_path;
  137. if (!cur_vars) {
  138. struct path_info *p = uh_path_lookup(cl, *cur_url);
  139. if (!p)
  140. p = &empty_path;
  141. cur_vars = uh_get_process_vars(cl, p);
  142. }
  143. for (cur = cur_vars; cur->name; cur++) {
  144. if (!strcmp(cur->name, name))
  145. return cur->value;
  146. }
  147. return NULL;
  148. }
  149. static void
  150. handler_init(void)
  151. {
  152. if (handler_ctx.handle_command)
  153. return;
  154. json_script_init(&handler_ctx);
  155. handler_ctx.handle_command = handle_command;
  156. handler_ctx.handle_var = handle_var;
  157. }
  158. static bool set_handler(struct json_script_file **dest, struct blob_attr *data)
  159. {
  160. if (!data)
  161. return true;
  162. *dest = json_script_file_from_blobmsg(NULL, blobmsg_data(data), blobmsg_data_len(data));
  163. return *dest;
  164. }
  165. int uh_handler_add(const char *file)
  166. {
  167. enum {
  168. H_REQUEST,
  169. H_FALLBACK,
  170. __H_MAX,
  171. };
  172. struct blobmsg_policy policy[__H_MAX] = {
  173. [H_REQUEST] = { "request", BLOBMSG_TYPE_ARRAY },
  174. [H_FALLBACK] = { "fallback", BLOBMSG_TYPE_ARRAY },
  175. };
  176. struct blob_attr *tb[__H_MAX];
  177. struct handler *h;
  178. handler_init();
  179. blob_buf_init(&b, 0);
  180. if (!blobmsg_add_json_from_file(&b, file))
  181. return -1;
  182. blobmsg_parse(policy, __H_MAX, tb, blob_data(b.head), blob_len(b.head));
  183. if (!tb[H_REQUEST] && !tb[H_FALLBACK])
  184. return -1;
  185. h = calloc(1, sizeof(*h));
  186. if (!set_handler(&h->request, tb[H_REQUEST]) ||
  187. !set_handler(&h->fallback, tb[H_FALLBACK])) {
  188. free(h->request);
  189. free(h->fallback);
  190. free(h);
  191. return -1;
  192. }
  193. list_add_tail(&h->list, &handlers);
  194. return 0;
  195. }
  196. int uh_handler_run(struct client *cl, char **url, bool fallback)
  197. {
  198. struct json_script_file *f;
  199. struct handler *h;
  200. cur_client = cl;
  201. cur_url = url;
  202. cur_vars = NULL;
  203. handler_ret = 0;
  204. list_for_each_entry(h, &handlers, list) {
  205. f = fallback ? h->fallback : h->request;
  206. if (!f)
  207. continue;
  208. blob_buf_init(&b, 0);
  209. json_script_run_file(&handler_ctx, f, b.head);
  210. if (handler_ctx.abort)
  211. break;
  212. }
  213. return handler_ret;
  214. }