parse status header code from cgi scripts
[project/uhttpd.git] / proc.c
1 /*
2 * uhttpd - Tiny single-threaded httpd
3 *
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 */
19
20 #include <libubox/blobmsg.h>
21 #include "uhttpd.h"
22
23 #define __headers \
24 __header(accept) \
25 __header(accept_charset) \
26 __header(accept_encoding) \
27 __header(accept_language) \
28 __header(authorization) \
29 __header(connection) \
30 __header(cookie) \
31 __header(host) \
32 __header(referer) \
33 __header(user_agent) \
34 __header(content_type) \
35 __header(content_length)
36
37 #undef __header
38 #define __header __enum_header
39 enum client_hdr {
40 __headers
41 __HDR_MAX,
42 };
43
44 #undef __header
45 #define __header __blobmsg_header
46 static const struct blobmsg_policy hdr_policy[__HDR_MAX] = {
47 __headers
48 };
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_REFERER", HDR_referer },
63 { "HTTP_USER_AGENT", HDR_user_agent },
64 { "CONTENT_TYPE", HDR_content_type },
65 { "CONTENT_LENGTH", HDR_content_length },
66 };
67
68 enum extra_vars {
69 /* no update needed */
70 _VAR_GW,
71 _VAR_SOFTWARE,
72
73 /* updated by uh_get_process_vars */
74 VAR_SCRIPT_NAME,
75 VAR_SCRIPT_FILE,
76 VAR_DOCROOT,
77 VAR_QUERY,
78 VAR_REQUEST,
79 VAR_PROTO,
80 VAR_METHOD,
81 VAR_PATH_INFO,
82 VAR_USER,
83 VAR_REDIRECT,
84
85 __VAR_MAX,
86 };
87
88 static struct env_var extra_vars[] = {
89 [_VAR_GW] = { "GATEWAY_INTERFACE", "CGI/1.1" },
90 [_VAR_SOFTWARE] = { "SERVER_SOFTWARE", "uhttpd" },
91 [VAR_SCRIPT_NAME] = { "SCRIPT_NAME" },
92 [VAR_SCRIPT_FILE] = { "SCRIPT_FILENAME" },
93 [VAR_DOCROOT] = { "DOCUMENT_ROOT" },
94 [VAR_QUERY] = { "QUERY_STRING" },
95 [VAR_REQUEST] = { "REQUEST_URI" },
96 [VAR_PROTO] = { "SERVER_PROTOCOL" },
97 [VAR_METHOD] = { "REQUEST_METHOD" },
98 [VAR_PATH_INFO] = { "PATH_INFO" },
99 [VAR_USER] = { "REMOTE_USER" },
100 [VAR_REDIRECT] = { "REDIRECT_STATUS" },
101 };
102
103 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi)
104 {
105 struct http_request *req = &cl->request;
106 struct blob_attr *data = cl->hdr.head;
107 struct env_var *vars = (void *) uh_buf;
108 struct blob_attr *tb[__HDR_MAX];
109 static char buf[4];
110 int len;
111 int i;
112
113 len = ARRAY_SIZE(proc_header_env);
114 len += ARRAY_SIZE(extra_vars);
115 len *= sizeof(struct env_var);
116
117 BUILD_BUG_ON(sizeof(uh_buf) < len);
118
119 extra_vars[VAR_SCRIPT_NAME].value = pi->name;
120 extra_vars[VAR_SCRIPT_FILE].value = pi->phys;
121 extra_vars[VAR_DOCROOT].value = pi->root;
122 extra_vars[VAR_QUERY].value = pi->query ? pi->query : "";
123 extra_vars[VAR_REQUEST].value = req->url;
124 extra_vars[VAR_PROTO].value = http_versions[req->version];
125 extra_vars[VAR_METHOD].value = http_methods[req->method];
126 extra_vars[VAR_PATH_INFO].value = pi->info;
127 extra_vars[VAR_USER].value = req->realm ? req->realm->user : NULL;
128
129 snprintf(buf, sizeof(buf), "%d", req->redirect_status);
130 extra_vars[VAR_REDIRECT].value = buf;
131
132 blobmsg_parse(hdr_policy, __HDR_MAX, tb, blob_data(data), blob_len(data));
133 for (i = 0; i < ARRAY_SIZE(proc_header_env); i++) {
134 struct blob_attr *cur;
135
136 cur = tb[proc_header_env[i].idx];
137 vars[i].name = proc_header_env[i].name;
138 vars[i].value = cur ? blobmsg_data(cur) : "";
139 }
140
141 memcpy(&vars[i], extra_vars, sizeof(extra_vars));
142 i += ARRAY_SIZE(extra_vars);
143 vars[i].name = NULL;
144 vars[i].value = NULL;
145
146 return vars;
147 }
148
149 static void proc_close_fds(struct client *cl)
150 {
151 close(cl->dispatch.proc.r.sfd.fd.fd);
152 }
153
154 static void proc_handle_close(struct relay *r, int ret)
155 {
156 if (r->header_cb) {
157 uh_client_error(r->cl, 502, "Bad Gateway",
158 "The process did not produce any response");
159 return;
160 }
161
162 uh_request_done(r->cl);
163 }
164
165 static void proc_handle_header(struct relay *r, const char *name, const char *val)
166 {
167 static char status_buf[64];
168 struct client *cl = r->cl;
169 char *sep;
170 char buf[4];
171
172 if (!strcmp(name, "Status")) {
173 sep = strchr(val, ' ');
174 if (sep != val + 3)
175 return;
176
177 memcpy(buf, val, 3);
178 buf[3] = 0;
179 snprintf(status_buf, sizeof(status_buf), "%s", sep + 1);
180 cl->dispatch.proc.status_msg = status_buf;
181 cl->dispatch.proc.status_code = atoi(buf);
182 return;
183 }
184
185 blobmsg_add_string(&cl->dispatch.proc.hdr, name, val);
186 }
187
188 static void proc_handle_header_end(struct relay *r)
189 {
190 struct client *cl = r->cl;
191 struct blob_attr *cur;
192 int rem;
193
194 uh_http_header(cl, cl->dispatch.proc.status_code, cl->dispatch.proc.status_msg);
195 blob_for_each_attr(cur, cl->dispatch.proc.hdr.head, rem)
196 ustream_printf(cl->us, "%s: %s\r\n", blobmsg_name(cur), blobmsg_data(cur));
197
198 ustream_printf(cl->us, "\r\n");
199 }
200
201 static void proc_free(struct client *cl)
202 {
203 uh_relay_free(&cl->dispatch.proc.r);
204 }
205
206 bool uh_create_process(struct client *cl, struct path_info *pi,
207 void (*cb)(struct client *cl, struct path_info *pi, int fd))
208 {
209 int fds[2];
210 int pid;
211
212 blob_buf_init(&cl->dispatch.proc.hdr, 0);
213 cl->dispatch.proc.status_code = 200;
214 cl->dispatch.proc.status_msg = "OK";
215
216 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
217 return false;
218
219 pid = fork();
220 if (pid < 0) {
221 close(fds[0]);
222 close(fds[1]);
223 return false;
224 }
225
226 if (!pid) {
227 close(fds[0]);
228 uh_close_fds();
229 cb(cl, pi, fds[1]);
230 exit(0);
231 }
232
233 close(fds[1]);
234 uh_relay_open(cl, &cl->dispatch.proc.r, fds[0], pid);
235 cl->dispatch.free = proc_free;
236 cl->dispatch.close_fds = proc_close_fds;
237 cl->dispatch.proc.r.header_cb = proc_handle_header;
238 cl->dispatch.proc.r.header_end = proc_handle_header_end;
239 cl->dispatch.proc.r.close = proc_handle_close;
240
241 return true;
242 }