| 1 | /* |
| 2 | * uhttpd - Tiny single-threaded httpd - Main header |
| 3 | * |
| 4 | * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org> |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #ifndef _UHTTPD_ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <signal.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/socket.h> |
| 29 | #include <sys/select.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <netinet/in.h> |
| 32 | #include <netinet/tcp.h> |
| 33 | #include <arpa/inet.h> |
| 34 | #include <linux/limits.h> |
| 35 | #include <netdb.h> |
| 36 | #include <ctype.h> |
| 37 | #include <errno.h> |
| 38 | #include <dlfcn.h> |
| 39 | |
| 40 | #include <libubox/list.h> |
| 41 | #include <libubox/uloop.h> |
| 42 | |
| 43 | |
| 44 | #ifdef HAVE_LUA |
| 45 | #include <lua.h> |
| 46 | #endif |
| 47 | |
| 48 | #ifdef HAVE_TLS |
| 49 | #include <openssl/ssl.h> |
| 50 | #endif |
| 51 | |
| 52 | /* uClibc... */ |
| 53 | #ifndef SOL_TCP |
| 54 | #define SOL_TCP 6 |
| 55 | #endif |
| 56 | |
| 57 | #ifdef DEBUG |
| 58 | #define D(...) fprintf(stderr, __VA_ARGS__) |
| 59 | #else |
| 60 | #define D(...) |
| 61 | #endif |
| 62 | |
| 63 | |
| 64 | #define UH_LIMIT_MSGHEAD 4096 |
| 65 | #define UH_LIMIT_HEADERS 64 |
| 66 | #define UH_LIMIT_CLIENTS 64 |
| 67 | |
| 68 | |
| 69 | struct listener; |
| 70 | struct client; |
| 71 | struct interpreter; |
| 72 | struct http_request; |
| 73 | struct uh_ubus_state; |
| 74 | |
| 75 | struct config { |
| 76 | char docroot[PATH_MAX]; |
| 77 | char *realm; |
| 78 | char *file; |
| 79 | char *index_file; |
| 80 | char *error_handler; |
| 81 | int no_symlinks; |
| 82 | int no_dirlists; |
| 83 | int network_timeout; |
| 84 | int rfc1918_filter; |
| 85 | int tcp_keepalive; |
| 86 | int max_requests; |
| 87 | #ifdef HAVE_CGI |
| 88 | char *cgi_prefix; |
| 89 | #endif |
| 90 | #ifdef HAVE_LUA |
| 91 | char *lua_prefix; |
| 92 | char *lua_handler; |
| 93 | lua_State *lua_state; |
| 94 | lua_State * (*lua_init) (const struct config *conf); |
| 95 | void (*lua_close) (lua_State *L); |
| 96 | bool (*lua_request) (struct client *cl, lua_State *L); |
| 97 | #endif |
| 98 | #ifdef HAVE_UBUS |
| 99 | char *ubus_prefix; |
| 100 | char *ubus_socket; |
| 101 | void *ubus_state; |
| 102 | struct uh_ubus_state * (*ubus_init) (const struct config *conf); |
| 103 | void (*ubus_close) (struct uh_ubus_state *state); |
| 104 | bool (*ubus_request) (struct client *cl, struct uh_ubus_state *state); |
| 105 | #endif |
| 106 | #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS) |
| 107 | int script_timeout; |
| 108 | #endif |
| 109 | #ifdef HAVE_TLS |
| 110 | char *cert; |
| 111 | char *key; |
| 112 | SSL_CTX *tls; |
| 113 | SSL_CTX * (*tls_init) (void); |
| 114 | int (*tls_cert) (SSL_CTX *c, const char *file); |
| 115 | int (*tls_key) (SSL_CTX *c, const char *file); |
| 116 | void (*tls_free) (struct listener *l); |
| 117 | int (*tls_accept) (struct client *c); |
| 118 | void (*tls_close) (struct client *c); |
| 119 | int (*tls_recv) (struct client *c, char *buf, int len); |
| 120 | int (*tls_send) (struct client *c, const char *buf, int len); |
| 121 | #endif |
| 122 | }; |
| 123 | |
| 124 | enum http_method { |
| 125 | UH_HTTP_MSG_GET, |
| 126 | UH_HTTP_MSG_POST, |
| 127 | UH_HTTP_MSG_HEAD, |
| 128 | }; |
| 129 | |
| 130 | extern const char *http_methods[]; |
| 131 | |
| 132 | enum http_version { |
| 133 | UH_HTTP_VER_0_9, |
| 134 | UH_HTTP_VER_1_0, |
| 135 | UH_HTTP_VER_1_1, |
| 136 | }; |
| 137 | |
| 138 | extern const char *http_versions[]; |
| 139 | |
| 140 | struct http_request { |
| 141 | enum http_method method; |
| 142 | enum http_version version; |
| 143 | int redirect_status; |
| 144 | char *url; |
| 145 | char *headers[UH_LIMIT_HEADERS]; |
| 146 | struct auth_realm *realm; |
| 147 | }; |
| 148 | |
| 149 | struct http_response { |
| 150 | int statuscode; |
| 151 | char *statusmsg; |
| 152 | char *headers[UH_LIMIT_HEADERS]; |
| 153 | }; |
| 154 | |
| 155 | struct listener { |
| 156 | struct uloop_fd fd; |
| 157 | int socket; |
| 158 | int n_clients; |
| 159 | struct sockaddr_in6 addr; |
| 160 | struct config *conf; |
| 161 | #ifdef HAVE_TLS |
| 162 | SSL_CTX *tls; |
| 163 | #endif |
| 164 | struct listener *next; |
| 165 | }; |
| 166 | |
| 167 | struct client { |
| 168 | #ifdef HAVE_TLS |
| 169 | SSL *tls; |
| 170 | #endif |
| 171 | struct uloop_fd fd; |
| 172 | struct uloop_fd rpipe; |
| 173 | struct uloop_fd wpipe; |
| 174 | struct uloop_process proc; |
| 175 | struct uloop_timeout timeout; |
| 176 | bool (*cb)(struct client *); |
| 177 | void *priv; |
| 178 | bool dispatched; |
| 179 | struct { |
| 180 | char buf[UH_LIMIT_MSGHEAD]; |
| 181 | char *ptr; |
| 182 | int len; |
| 183 | } httpbuf; |
| 184 | struct listener *server; |
| 185 | struct http_request request; |
| 186 | struct http_response response; |
| 187 | struct sockaddr_in6 servaddr; |
| 188 | struct sockaddr_in6 peeraddr; |
| 189 | struct client *next; |
| 190 | }; |
| 191 | |
| 192 | struct client_light { |
| 193 | #ifdef HAVE_TLS |
| 194 | SSL *tls; |
| 195 | #endif |
| 196 | struct uloop_fd fd; |
| 197 | }; |
| 198 | |
| 199 | struct auth_realm { |
| 200 | char path[PATH_MAX]; |
| 201 | char user[32]; |
| 202 | char pass[128]; |
| 203 | struct auth_realm *next; |
| 204 | }; |
| 205 | |
| 206 | #ifdef HAVE_CGI |
| 207 | struct interpreter { |
| 208 | char path[PATH_MAX]; |
| 209 | char extn[32]; |
| 210 | struct interpreter *next; |
| 211 | }; |
| 212 | #endif |
| 213 | |
| 214 | #endif |
| 215 | |