proc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * uhttpd - Tiny single-threaded httpd
  3. *
  4. * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
  5. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  6. *
  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.
  10. *
  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.
  18. */
  19. #include <arpa/inet.h>
  20. #include <libubox/blobmsg.h>
  21. #include "uhttpd.h"
  22. #define __headers \
  23. __header(accept, accept) \
  24. __header(accept_charset, accept-charset) \
  25. __header(accept_encoding, accept-encoding) \
  26. __header(accept_language, accept-language) \
  27. __header(authorization, authorization) \
  28. __header(connection, connection) \
  29. __header(cookie, cookie) \
  30. __header(host, host) \
  31. __header(origin, origin) \
  32. __header(referer, referer) \
  33. __header(user_agent, user-agent) \
  34. __header(content_type, content-type) \
  35. __header(content_length, content-length) \
  36. __header(x_http_method_override, x-http-method-override) \
  37. __header(http_auth_user, http-auth-user) \
  38. __header(http_auth_pass, http-auth-pass)
  39. #undef __header
  40. #define __header __enum_header
  41. enum client_hdr {
  42. __headers
  43. __HDR_MAX,
  44. };
  45. #undef __header
  46. #define __header __blobmsg_header
  47. static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
  48. __headers
  49. };
  50. static const struct {
  51. const char *name;
  52. int idx;
  53. } proc_header_env[] = {
  54. { "HTTP_ACCEPT", HDR_accept },
  55. { "HTTP_ACCEPT_CHARSET", HDR_accept_charset },
  56. { "HTTP_ACCEPT_ENCODING", HDR_accept_encoding },
  57. { "HTTP_ACCEPT_LANGUAGE", HDR_accept_language },
  58. { "HTTP_AUTHORIZATION", HDR_authorization },
  59. { "HTTP_CONNECTION", HDR_connection },
  60. { "HTTP_COOKIE", HDR_cookie },
  61. { "HTTP_HOST", HDR_host },
  62. { "HTTP_ORIGIN", HDR_origin },
  63. { "HTTP_REFERER", HDR_referer },
  64. { "HTTP_USER_AGENT", HDR_user_agent },
  65. { "HTTP_X_HTTP_METHOD_OVERRIDE", HDR_x_http_method_override },
  66. { "HTTP_AUTH_USER", HDR_http_auth_user },
  67. { "HTTP_AUTH_PASS", HDR_http_auth_pass },
  68. { "CONTENT_TYPE", HDR_content_type },
  69. { "CONTENT_LENGTH", HDR_content_length },
  70. };
  71. enum extra_vars {
  72. /* no update needed */
  73. _VAR_GW,
  74. _VAR_SOFTWARE,
  75. /* updated by uh_get_process_vars */
  76. VAR_SCRIPT_NAME,
  77. VAR_SCRIPT_FILE,
  78. VAR_DOCROOT,
  79. VAR_QUERY,
  80. VAR_REQUEST,
  81. VAR_PROTO,
  82. VAR_METHOD,
  83. VAR_PATH_INFO,
  84. VAR_USER,
  85. VAR_HTTPS,
  86. VAR_REDIRECT,
  87. VAR_SERVER_NAME,
  88. VAR_SERVER_ADDR,
  89. VAR_SERVER_PORT,
  90. VAR_REMOTE_NAME,
  91. VAR_REMOTE_ADDR,
  92. VAR_REMOTE_PORT,
  93. __VAR_MAX,
  94. };
  95. static char local_addr[INET6_ADDRSTRLEN], remote_addr[INET6_ADDRSTRLEN];
  96. static char local_port[6], remote_port[6];
  97. static char redirect_status[4];
  98. static struct env_var extra_vars[] = {
  99. [_VAR_GW] = { "GATEWAY_INTERFACE", "CGI/1.1" },
  100. [_VAR_SOFTWARE] = { "SERVER_SOFTWARE", "uhttpd" },
  101. [VAR_SCRIPT_NAME] = { "SCRIPT_NAME" },
  102. [VAR_SCRIPT_FILE] = { "SCRIPT_FILENAME" },
  103. [VAR_DOCROOT] = { "DOCUMENT_ROOT" },
  104. [VAR_QUERY] = { "QUERY_STRING" },
  105. [VAR_REQUEST] = { "REQUEST_URI" },
  106. [VAR_PROTO] = { "SERVER_PROTOCOL" },
  107. [VAR_METHOD] = { "REQUEST_METHOD" },
  108. [VAR_PATH_INFO] = { "PATH_INFO" },
  109. [VAR_USER] = { "REMOTE_USER" },
  110. [VAR_HTTPS] = { "HTTPS" },
  111. [VAR_REDIRECT] = { "REDIRECT_STATUS", redirect_status },
  112. [VAR_SERVER_NAME] = { "SERVER_NAME", local_addr },
  113. [VAR_SERVER_ADDR] = { "SERVER_ADDR", local_addr },
  114. [VAR_SERVER_PORT] = { "SERVER_PORT", local_port },
  115. [VAR_REMOTE_NAME] = { "REMOTE_HOST", remote_addr },
  116. [VAR_REMOTE_ADDR] = { "REMOTE_ADDR", remote_addr },
  117. [VAR_REMOTE_PORT] = { "REMOTE_PORT", remote_port },
  118. };
  119. struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi)
  120. {
  121. struct http_request *req = &cl->request;
  122. struct blob_attr *data = cl->hdr.head;
  123. struct env_var *vars = (void *) uh_buf;
  124. struct blob_attr *tb[__HDR_MAX];
  125. const char *url;
  126. int len;
  127. int i;
  128. url = blobmsg_data(blob_data(cl->hdr.head));
  129. len = ARRAY_SIZE(proc_header_env);
  130. len += ARRAY_SIZE(extra_vars);
  131. len *= sizeof(struct env_var);
  132. BUILD_BUG_ON(sizeof(uh_buf) < len);
  133. extra_vars[VAR_SCRIPT_NAME].value = pi->name;
  134. extra_vars[VAR_SCRIPT_FILE].value = pi->phys;
  135. extra_vars[VAR_DOCROOT].value = pi->root;
  136. extra_vars[VAR_QUERY].value = pi->query ? pi->query : "";
  137. extra_vars[VAR_REQUEST].value = url;
  138. extra_vars[VAR_PROTO].value = http_versions[req->version];
  139. extra_vars[VAR_METHOD].value = http_methods[req->method];
  140. extra_vars[VAR_PATH_INFO].value = pi->info;
  141. extra_vars[VAR_USER].value = req->realm ? req->realm->user : NULL;
  142. extra_vars[VAR_HTTPS].value = cl->tls ? "on" : NULL;
  143. snprintf(redirect_status, sizeof(redirect_status),
  144. "%d", req->redirect_status);
  145. inet_ntop(cl->srv_addr.family, &cl->srv_addr.in, local_addr, sizeof(local_addr));
  146. snprintf(local_port, sizeof(local_port), "%d", cl->srv_addr.port);
  147. inet_ntop(cl->peer_addr.family, &cl->peer_addr.in, remote_addr, sizeof(remote_addr));
  148. snprintf(remote_port, sizeof(remote_port), "%d", cl->peer_addr.port);
  149. blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(data), blob_len(data));
  150. for (i = 0; i < ARRAY_SIZE(proc_header_env); i++) {
  151. struct blob_attr *cur;
  152. cur = tb[proc_header_env[i].idx];
  153. vars[i].name = proc_header_env[i].name;
  154. vars[i].value = cur ? blobmsg_data(cur) : NULL;
  155. }
  156. memcpy(&vars[i], extra_vars, sizeof(extra_vars));
  157. i += ARRAY_SIZE(extra_vars);
  158. vars[i].name = NULL;
  159. vars[i].value = NULL;
  160. return vars;
  161. }
  162. static void proc_close_fds(struct client *cl)
  163. {
  164. struct dispatch_proc *p = &cl->dispatch.proc;
  165. close(p->r.sfd.fd.fd);
  166. if (p->wrfd.fd >= 0)
  167. close(p->wrfd.fd);
  168. }
  169. static void proc_handle_close(struct relay *r, int ret)
  170. {
  171. if (r->header_cb) {
  172. uh_client_error(r->cl, 502, "Bad Gateway",
  173. "The process did not produce any response");
  174. return;
  175. }
  176. uh_request_done(r->cl);
  177. }
  178. static void proc_handle_header(struct relay *r, const char *name, const char *val)
  179. {
  180. static char status_buf[64];
  181. struct client *cl = r->cl;
  182. char *sep;
  183. char buf[4];
  184. if (!strcmp(name, "Status")) {
  185. sep = strchr(val, ' ');
  186. if (sep != val + 3)
  187. return;
  188. memcpy(buf, val, 3);
  189. buf[3] = 0;
  190. snprintf(status_buf, sizeof(status_buf), "%s", sep + 1);
  191. cl->dispatch.proc.status_msg = status_buf;
  192. cl->dispatch.proc.status_code = atoi(buf);
  193. return;
  194. }
  195. blobmsg_add_string(&cl->dispatch.proc.hdr, name, val);
  196. }
  197. static void proc_handle_header_end(struct relay *r)
  198. {
  199. struct client *cl = r->cl;
  200. struct blob_attr *cur;
  201. int rem;
  202. uh_http_header(cl, cl->dispatch.proc.status_code, cl->dispatch.proc.status_msg);
  203. blob_for_each_attr(cur, cl->dispatch.proc.hdr.head, rem)
  204. ustream_printf(cl->us, "%s: %s\r\n", blobmsg_name(cur),
  205. blobmsg_get_string(cur));
  206. ustream_printf(cl->us, "\r\n");
  207. if (cl->request.method == UH_HTTP_MSG_HEAD)
  208. r->skip_data = true;
  209. }
  210. static void proc_write_close(struct client *cl)
  211. {
  212. struct dispatch_proc *p = &cl->dispatch.proc;
  213. if (p->wrfd.fd < 0)
  214. return;
  215. uloop_fd_delete(&p->wrfd);
  216. close(p->wrfd.fd);
  217. p->wrfd.fd = -1;
  218. }
  219. static void proc_free(struct client *cl)
  220. {
  221. struct dispatch_proc *p = &cl->dispatch.proc;
  222. uloop_timeout_cancel(&p->timeout);
  223. blob_buf_free(&p->hdr);
  224. proc_write_close(cl);
  225. uh_relay_free(&p->r);
  226. }
  227. static void proc_write_cb(struct uloop_fd *fd, unsigned int events)
  228. {
  229. struct client *cl = container_of(fd, struct client, dispatch.proc.wrfd);
  230. client_poll_post_data(cl);
  231. }
  232. static void proc_relay_write_cb(struct client *cl)
  233. {
  234. struct dispatch_proc *p = &cl->dispatch.proc;
  235. if (ustream_pending_data(cl->us, true))
  236. return;
  237. ustream_set_read_blocked(&p->r.sfd.stream, false);
  238. p->r.sfd.stream.notify_read(&p->r.sfd.stream, 0);
  239. }
  240. static int proc_data_send(struct client *cl, const char *data, int len)
  241. {
  242. struct dispatch_proc *p = &cl->dispatch.proc;
  243. int retlen = 0;
  244. int ret;
  245. while (len) {
  246. ret = write(p->wrfd.fd, data, len);
  247. if (ret < 0) {
  248. if (errno == EINTR)
  249. continue;
  250. if (errno == EAGAIN || errno == EWOULDBLOCK)
  251. break;
  252. /* consume all data */
  253. ret = len;
  254. }
  255. if (!ret)
  256. break;
  257. retlen += ret;
  258. len -= ret;
  259. data += ret;
  260. }
  261. if (len)
  262. uloop_fd_add(&p->wrfd, ULOOP_WRITE);
  263. else
  264. uloop_fd_delete(&p->wrfd);
  265. return retlen;
  266. }
  267. static void proc_timeout_cb(struct uloop_timeout *timeout)
  268. {
  269. struct dispatch_proc *proc = container_of(timeout, struct dispatch_proc, timeout);
  270. struct client *cl = container_of(proc, struct client, dispatch.proc);
  271. uh_relay_kill(cl, &proc->r);
  272. }
  273. bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
  274. void (*cb)(struct client *cl, struct path_info *pi, char *url))
  275. {
  276. struct dispatch *d = &cl->dispatch;
  277. struct dispatch_proc *proc = &d->proc;
  278. int rfd[2], wfd[2];
  279. int pid;
  280. blob_buf_init(&proc->hdr, 0);
  281. proc->status_code = 200;
  282. proc->status_msg = "OK";
  283. if (pipe(rfd))
  284. return false;
  285. if (pipe(wfd))
  286. goto close_rfd;
  287. pid = fork();
  288. if (pid < 0)
  289. goto close_wfd;
  290. if (!pid) {
  291. close(0);
  292. close(1);
  293. dup2(rfd[1], 1);
  294. dup2(wfd[0], 0);
  295. close(rfd[0]);
  296. close(rfd[1]);
  297. close(wfd[0]);
  298. close(wfd[1]);
  299. uh_close_fds();
  300. cb(cl, pi, url);
  301. exit(0);
  302. }
  303. close(rfd[1]);
  304. close(wfd[0]);
  305. proc->wrfd.fd = wfd[1];
  306. uh_relay_open(cl, &proc->r, rfd[0], pid);
  307. d->free = proc_free;
  308. d->close_fds = proc_close_fds;
  309. d->data_send = proc_data_send;
  310. d->data_done = proc_write_close;
  311. d->write_cb = proc_relay_write_cb;
  312. proc->r.header_cb = proc_handle_header;
  313. proc->r.header_end = proc_handle_header_end;
  314. proc->r.close = proc_handle_close;
  315. proc->wrfd.cb = proc_write_cb;
  316. proc->timeout.cb = proc_timeout_cb;
  317. if (conf.script_timeout > 0)
  318. uloop_timeout_set(&proc->timeout, conf.script_timeout * 1000);
  319. return true;
  320. close_wfd:
  321. close(wfd[0]);
  322. close(wfd[1]);
  323. close_rfd:
  324. close(rfd[0]);
  325. close(rfd[1]);
  326. return false;
  327. }