• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/uhttpd/uhttpd.h

  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 #ifndef __UHTTPD_H
 21 #define __UHTTPD_H
 22 
 23 #include <netinet/in.h>
 24 #include <limits.h>
 25 #include <dirent.h>
 26 
 27 #include <libubox/list.h>
 28 #include <libubox/uloop.h>
 29 #include <libubox/ustream.h>
 30 #include <libubox/blob.h>
 31 #include <libubox/utils.h>
 32 #ifdef HAVE_UBUS
 33 #include <libubus.h>
 34 #include <json-c/json.h>
 35 #endif
 36 #ifdef HAVE_TLS
 37 #include <libubox/ustream-ssl.h>
 38 #endif
 39 
 40 #include "utils.h"
 41 
 42 #define UH_LIMIT_CLIENTS        64
 43 
 44 #define __enum_header(_name, _val) HDR_##_name,
 45 #define __blobmsg_header(_name, _val) [HDR_##_name] = { .name = #_val, .type = BLOBMSG_TYPE_STRING },
 46 
 47 struct client;
 48 
 49 struct alias {
 50         struct list_head list;
 51         char *alias;
 52         char *path;
 53 };
 54 
 55 struct lua_prefix {
 56         struct list_head list;
 57         const char *handler;
 58         const char *prefix;
 59         void *ctx;
 60 };
 61 
 62 struct config {
 63         const char *docroot;
 64         const char *realm;
 65         const char *file;
 66         const char *error_handler;
 67         const char *cgi_prefix;
 68         const char *cgi_docroot_path;
 69         const char *cgi_path;
 70         const char *ubus_prefix;
 71         const char *ubus_socket;
 72         int no_symlinks;
 73         int no_dirlists;
 74         int network_timeout;
 75         int rfc1918_filter;
 76         int tls_redirect;
 77         int tcp_keepalive;
 78         int max_script_requests;
 79         int max_connections;
 80         int http_keepalive;
 81         int script_timeout;
 82         int ubus_noauth;
 83         int ubus_cors;
 84         int cgi_prefix_len;
 85         int events_retry;
 86         struct list_head cgi_alias;
 87         struct list_head lua_prefix;
 88 };
 89 
 90 struct auth_realm {
 91         struct list_head list;
 92         const char *path;
 93         const char *user;
 94         const char *pass;
 95 };
 96 
 97 enum http_method {
 98         UH_HTTP_MSG_GET,
 99         UH_HTTP_MSG_POST,
100         UH_HTTP_MSG_HEAD,
101         UH_HTTP_MSG_OPTIONS,
102         UH_HTTP_MSG_PUT,
103         UH_HTTP_MSG_PATCH,
104         UH_HTTP_MSG_DELETE,
105 };
106 
107 enum http_version {
108         UH_HTTP_VER_0_9,
109         UH_HTTP_VER_1_0,
110         UH_HTTP_VER_1_1,
111 };
112 
113 enum http_user_agent {
114         UH_UA_UNKNOWN,
115         UH_UA_GECKO,
116         UH_UA_CHROME,
117         UH_UA_SAFARI,
118         UH_UA_MSIE,
119         UH_UA_KONQUEROR,
120         UH_UA_OPERA,
121         UH_UA_MSIE_OLD,
122         UH_UA_MSIE_NEW,
123 };
124 
125 struct http_request {
126         enum http_method method;
127         enum http_version version;
128         enum http_user_agent ua;
129         int redirect_status;
130         int content_length;
131         bool expect_cont;
132         bool connection_close;
133         bool disable_chunked;
134         uint8_t transfer_chunked;
135         const struct auth_realm *realm;
136 };
137 
138 enum client_state {
139         CLIENT_STATE_INIT,
140         CLIENT_STATE_HEADER,
141         CLIENT_STATE_DATA,
142         CLIENT_STATE_DONE,
143         CLIENT_STATE_CLOSE,
144         CLIENT_STATE_CLEANUP,
145 };
146 
147 struct interpreter {
148         struct list_head list;
149         const char *path;
150         const char *ext;
151 };
152 
153 struct path_info {
154         const char *root;
155         const char *phys;
156         const char *name;
157         const char *info;
158         const char *query;
159         bool redirected;
160         struct stat stat;
161         const struct interpreter *ip;
162 };
163 
164 struct env_var {
165         const char *name;
166         const char *value;
167 };
168 
169 struct relay {
170         struct ustream_fd sfd;
171         struct uloop_process proc;
172         struct uloop_timeout timeout;
173         struct client *cl;
174 
175         bool process_done;
176         bool error;
177         bool skip_data;
178 
179         int ret;
180         int header_ofs;
181 
182         void (*header_cb)(struct relay *r, const char *name, const char *value);
183         void (*header_end)(struct relay *r);
184         void (*close)(struct relay *r, int ret);
185 };
186 
187 struct dispatch_proc {
188         struct uloop_timeout timeout;
189         struct blob_buf hdr;
190         struct uloop_fd wrfd;
191         struct relay r;
192         int status_code;
193         char *status_msg;
194 };
195 
196 struct dispatch_handler {
197         struct list_head list;
198         bool script;
199 
200         bool (*check_url)(const char *url);
201         bool (*check_path)(struct path_info *pi, const char *url);
202         void (*handle_request)(struct client *cl, char *url, struct path_info *pi);
203 };
204 
205 #ifdef HAVE_UBUS
206 struct dispatch_ubus {
207         struct ubus_request req;
208 
209         struct uloop_timeout timeout;
210         struct json_tokener *jstok;
211         struct json_object *jsobj;
212         struct json_object *jsobj_cur;
213         char *url_path;
214         int post_len;
215 
216         uint32_t obj;
217         const char *func;
218 
219         struct blob_buf buf;
220         bool req_pending;
221         bool array;
222         int array_idx;
223         bool legacy; /* Got legacy request => use legacy reply */
224 
225         struct ubus_subscriber sub;
226 };
227 #endif
228 
229 struct dispatch {
230         int (*data_send)(struct client *cl, const char *data, int len);
231         void (*data_done)(struct client *cl);
232         void (*write_cb)(struct client *cl);
233         void (*close_fds)(struct client *cl);
234         void (*free)(struct client *cl);
235 
236         void *req_data;
237         void (*req_free)(struct client *cl);
238 
239         bool data_blocked;
240         bool no_cache;
241 
242         union {
243                 struct {
244                         struct blob_attr **hdr;
245                         int fd;
246                 } file;
247                 struct dispatch_proc proc;
248 #ifdef HAVE_UBUS
249                 struct dispatch_ubus ubus;
250 #endif
251         };
252 };
253 
254 struct client {
255         struct list_head list;
256         int refcount;
257         int id;
258 
259         struct ustream *us;
260         struct ustream_fd sfd;
261 #ifdef HAVE_TLS
262         struct ustream_ssl ssl;
263 #endif
264         struct uloop_timeout timeout;
265         int requests;
266 
267         enum client_state state;
268         bool tls;
269 
270         int http_code;
271         struct http_request request;
272         struct uh_addr srv_addr, peer_addr;
273 
274         struct blob_buf hdr;
275         struct blob_buf hdr_response;
276         struct dispatch dispatch;
277 };
278 
279 extern char uh_buf[4096];
280 extern int n_clients;
281 extern struct config conf;
282 extern const char * const http_versions[];
283 extern const char * const http_methods[];
284 extern struct dispatch_handler cgi_dispatch;
285 
286 void uh_index_add(const char *filename);
287 
288 bool uh_accept_client(int fd, bool tls);
289 
290 void uh_unblock_listeners(void);
291 void uh_setup_listeners(void);
292 int uh_socket_bind(const char *host, const char *port, bool tls);
293 
294 int uh_first_tls_port(int family);
295 
296 bool uh_use_chunked(struct client *cl);
297 void uh_chunk_write(struct client *cl, const void *data, int len);
298 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
299 
300 void __printf(2, 3)
301 uh_chunk_printf(struct client *cl, const char *format, ...);
302 
303 void uh_chunk_eof(struct client *cl);
304 void uh_request_done(struct client *cl);
305 
306 void uh_http_header(struct client *cl, int code, const char *summary);
307 void __printf(4, 5)
308 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
309 
310 void uh_handle_request(struct client *cl);
311 void client_poll_post_data(struct client *cl);
312 void uh_client_read_cb(struct client *cl);
313 void uh_client_notify_state(struct client *cl);
314 
315 void uh_auth_add(const char *path, const char *user, const char *pass);
316 bool uh_auth_check(struct client *cl, const char *path, const char *auth,
317                    char **uptr, char **pptr);
318 
319 void uh_close_listen_fds(void);
320 void uh_close_fds(void);
321 
322 void uh_interpreter_add(const char *ext, const char *path);
323 void uh_dispatch_add(struct dispatch_handler *d);
324 
325 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
326 void uh_relay_close(struct relay *r, int ret);
327 void uh_relay_free(struct relay *r);
328 void uh_relay_kill(struct client *cl, struct relay *r);
329 
330 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
331 bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
332                        void (*cb)(struct client *cl, struct path_info *pi, char *url));
333 
334 int uh_plugin_init(const char *name);
335 void uh_plugin_post_init(void);
336 
337 int uh_handler_add(const char *file);
338 int uh_handler_run(struct client *cl, char **url, bool fallback);
339 
340 struct path_info *uh_path_lookup(struct client *cl, const char *url);
341 
342 static inline void uh_client_ref(struct client *cl)
343 {
344         cl->refcount++;
345 }
346 
347 static inline void uh_client_unref(struct client *cl)
348 {
349         if (--cl->refcount)
350                 return;
351 
352         if (cl->state == CLIENT_STATE_CLEANUP)
353                 ustream_state_change(cl->us);
354 }
355 
356 #endif
357 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt