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