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