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 <libubox/blobmsg.h>
25 __header(accept_charset) \
26 __header(accept_encoding) \
27 __header(accept_language) \
28 __header(authorization) \
29 __header(connection) \
33 __header(user_agent) \
34 __header(content_type) \
35 __header(content_length)
38 #define __header __enum_header
45 #define __header __blobmsg_header
46 static const struct blobmsg_policy hdr_policy
[__HDR_MAX
] = {
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
},
69 /* no update needed */
73 /* updated by uh_get_process_vars */
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" },
103 struct env_var
*uh_get_process_vars(struct client
*cl
, struct path_info
*pi
)
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
];
113 len
= ARRAY_SIZE(proc_header_env
);
114 len
+= ARRAY_SIZE(extra_vars
);
115 len
*= sizeof(struct env_var
);
117 BUILD_BUG_ON(sizeof(uh_buf
) < len
);
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
;
129 snprintf(buf
, sizeof(buf
), "%d", req
->redirect_status
);
130 extra_vars
[VAR_REDIRECT
].value
= buf
;
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
;
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
) : "";
141 memcpy(&vars
[i
], extra_vars
, sizeof(extra_vars
));
142 i
+= ARRAY_SIZE(extra_vars
);
144 vars
[i
].value
= NULL
;
149 static void proc_close_fds(struct client
*cl
)
151 close(cl
->dispatch
.proc
.r
.sfd
.fd
.fd
);
154 static void proc_handle_close(struct relay
*r
, int ret
)
157 uh_client_error(r
->cl
, 502, "Bad Gateway",
158 "The process did not produce any response");
162 uh_request_done(r
->cl
);
165 static void proc_handle_header(struct relay
*r
, const char *name
, const char *val
)
167 static char status_buf
[64];
168 struct client
*cl
= r
->cl
;
172 if (!strcmp(name
, "Status")) {
173 sep
= strchr(val
, ' ');
179 snprintf(status_buf
, sizeof(status_buf
), "%s", sep
+ 1);
180 cl
->dispatch
.proc
.status_msg
= status_buf
;
184 blobmsg_add_string(&cl
->dispatch
.proc
.hdr
, name
, val
);
187 static void proc_handle_header_end(struct relay
*r
)
189 struct client
*cl
= r
->cl
;
190 struct blob_attr
*cur
;
193 uh_http_header(cl
, cl
->dispatch
.proc
.status_code
, cl
->dispatch
.proc
.status_msg
);
194 blob_for_each_attr(cur
, cl
->dispatch
.proc
.hdr
.head
, rem
)
195 ustream_printf(cl
->us
, "%s: %s\r\n", blobmsg_name(cur
), blobmsg_data(cur
));
197 ustream_printf(cl
->us
, "\r\n");
200 static void proc_free(struct client
*cl
)
202 uh_relay_free(&cl
->dispatch
.proc
.r
);
205 bool uh_create_process(struct client
*cl
, struct path_info
*pi
,
206 void (*cb
)(struct client
*cl
, struct path_info
*pi
, int fd
))
211 blob_buf_init(&cl
->dispatch
.proc
.hdr
, 0);
212 cl
->dispatch
.proc
.status_code
= 200;
213 cl
->dispatch
.proc
.status_msg
= "OK";
215 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fds
))
233 uh_relay_open(cl
, &cl
->dispatch
.proc
.r
, fds
[0], pid
);
234 cl
->dispatch
.free
= proc_free
;
235 cl
->dispatch
.close_fds
= proc_close_fds
;
236 cl
->dispatch
.proc
.r
.header_cb
= proc_handle_header
;
237 cl
->dispatch
.proc
.r
.header_end
= proc_handle_header_end
;
238 cl
->dispatch
.proc
.r
.close
= proc_handle_close
;