2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <arpa/inet.h>
21 #include <libubox/blobmsg.h>
26 __header(accept_charset) \
27 __header(accept_encoding) \
28 __header(accept_language) \
29 __header(authorization) \
30 __header(connection) \
34 __header(user_agent) \
35 __header(content_type) \
36 __header(content_length)
39 #define __header __enum_header
46 #define __header __blobmsg_header
47 static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
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 },
70 /* no update needed */
74 /* updated by uh_get_process_vars */
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];
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 },
120 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi)
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];
129 len = ARRAY_SIZE(proc_header_env);
130 len += ARRAY_SIZE(extra_vars);
131 len *= sizeof(struct env_var);
133 BUILD_BUG_ON(sizeof(uh_buf) < len);
135 extra_vars[VAR_SCRIPT_NAME].value = pi->name;
136 extra_vars[VAR_SCRIPT_FILE].value = pi->phys;
137 extra_vars[VAR_DOCROOT].value = pi->root;
138 extra_vars[VAR_QUERY].value = pi->query ? pi->query : "";
139 extra_vars[VAR_REQUEST].value = req->url;
140 extra_vars[VAR_PROTO].value = http_versions[req->version];
141 extra_vars[VAR_METHOD].value = http_methods[req->method];
142 extra_vars[VAR_PATH_INFO].value = pi->info;
143 extra_vars[VAR_USER].value = req->realm ? req->realm->user : NULL;
145 snprintf(redirect_status, sizeof(redirect_status),
146 "%d", req->redirect_status);
147 inet_ntop(cl->srv_addr.family, &cl->srv_addr.in, local_addr, sizeof(local_addr));
148 inet_ntop(cl->peer_addr.family, &cl->peer_addr.in, remote_addr, sizeof(remote_addr));
150 blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(data), blob_len(data));
151 for (i = 0; i < ARRAY_SIZE(proc_header_env); i++) {
152 struct blob_attr *cur;
154 cur = tb[proc_header_env[i].idx];
155 vars[i].name = proc_header_env[i].name;
156 vars[i].value = cur ? blobmsg_data(cur) : "";
159 memcpy(&vars[i], extra_vars, sizeof(extra_vars));
160 i += ARRAY_SIZE(extra_vars);
162 vars[i].value = NULL;
167 static void proc_close_fds(struct client *cl)
169 close(cl->dispatch.proc.r.sfd.fd.fd);
172 static void proc_handle_close(struct relay *r, int ret)
175 uh_client_error(r->cl, 502, "Bad Gateway",
176 "The process did not produce any response");
180 uh_request_done(r->cl);
183 static void proc_handle_header(struct relay *r, const char *name, const char *val)
185 static char status_buf[64];
186 struct client *cl = r->cl;
190 if (!strcmp(name, "Status")) {
191 sep = strchr(val, ' ');
197 snprintf(status_buf, sizeof(status_buf), "%s", sep + 1);
198 cl->dispatch.proc.status_msg = status_buf;
199 cl->dispatch.proc.status_code = atoi(buf);
203 blobmsg_add_string(&cl->dispatch.proc.hdr, name, val);
206 static void proc_handle_header_end(struct relay *r)
208 struct client *cl = r->cl;
209 struct blob_attr *cur;
212 uh_http_header(cl, cl->dispatch.proc.status_code, cl->dispatch.proc.status_msg);
213 blob_for_each_attr(cur, cl->dispatch.proc.hdr.head, rem)
214 ustream_printf(cl->us, "%s: %s\r\n", blobmsg_name(cur), blobmsg_data(cur));
216 ustream_printf(cl->us, "\r\n");
219 static void proc_free(struct client *cl)
221 uh_relay_free(&cl->dispatch.proc.r);
224 static void proc_write_close(struct client *cl)
226 shutdown(cl->dispatch.proc.r.sfd.fd.fd, SHUT_WR);
229 static void proc_relay_write_cb(struct ustream *us, int bytes)
231 struct client *cl = container_of(us, struct client, dispatch.proc.r.sfd.stream);
233 if (ustream_pending_data(us, true))
236 proc_write_close(cl);
239 static void proc_data_send(struct client *cl, const char *data, int len)
241 struct ustream *us = &cl->dispatch.proc.r.sfd.stream;
243 ustream_write(us, data, len, false);
246 static void proc_data_done(struct client *cl)
248 struct ustream *us = &cl->dispatch.proc.r.sfd.stream;
250 if (ustream_pending_data(us, true)) {
251 us->notify_write = proc_relay_write_cb;
255 proc_write_close(cl);
258 bool uh_create_process(struct client *cl, struct path_info *pi,
259 void (*cb)(struct client *cl, struct path_info *pi, int fd))
261 struct dispatch *d = &cl->dispatch;
265 blob_buf_init(&cl->dispatch.proc.hdr, 0);
266 d->proc.status_code = 200;
267 d->proc.status_msg = "OK";
269 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
287 uh_relay_open(cl, &cl->dispatch.proc.r, fds[0], pid);
289 d->close_fds = proc_close_fds;
290 d->data_send = proc_data_send;
291 d->data_done = proc_data_done;
292 d->proc.r.header_cb = proc_handle_header;
293 d->proc.r.header_end = proc_handle_header_end;
294 d->proc.r.close = proc_handle_close;