| 1 | #include <sys/stat.h> |
|---|
| 2 | |
|---|
| 3 | #include <stdlib.h> |
|---|
| 4 | #include <stdio.h> |
|---|
| 5 | #include <errno.h> |
|---|
| 6 | #include <string.h> |
|---|
| 7 | #include <fcntl.h> |
|---|
| 8 | #include <assert.h> |
|---|
| 9 | |
|---|
| 10 | #include "settings.h" |
|---|
| 11 | |
|---|
| 12 | #include "server.h" |
|---|
| 13 | #include "connections.h" |
|---|
| 14 | #include "fdevent.h" |
|---|
| 15 | #include "log.h" |
|---|
| 16 | |
|---|
| 17 | #include "request.h" |
|---|
| 18 | #include "response.h" |
|---|
| 19 | #include "network.h" |
|---|
| 20 | #include "stat_cache.h" |
|---|
| 21 | #include "joblist.h" |
|---|
| 22 | |
|---|
| 23 | #include "plugin.h" |
|---|
| 24 | |
|---|
| 25 | #include "inet_ntop_cache.h" |
|---|
| 26 | #include "configfile.h" |
|---|
| 27 | #include "http_req.h" |
|---|
| 28 | |
|---|
| 29 | #ifdef USE_OPENSSL |
|---|
| 30 | # include <openssl/ssl.h> |
|---|
| 31 | # include <openssl/err.h> |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #ifdef HAVE_SYS_FILIO_H |
|---|
| 35 | # include <sys/filio.h> |
|---|
| 36 | #endif |
|---|
| 37 | |
|---|
| 38 | #include "sys-socket.h" |
|---|
| 39 | #include "sys-files.h" |
|---|
| 40 | |
|---|
| 41 | typedef struct { |
|---|
| 42 | Â Â Â Â PLUGIN_DATA; |
|---|
| 43 | }Â plugin_data; |
|---|
| 44 | |
|---|
| 45 | static connection *connections_get_new_connection(server *srv) { |
|---|
| 46 | Â Â Â Â connections *conns =Â srv->conns; |
|---|
| 47 | Â Â Â Â size_t i; |
|---|
| 48 | |
|---|
| 49 |     if (conns->size == 0) { |
|---|
| 50 | Â Â Â Â Â Â Â Â conns->size =Â 128; |
|---|
| 51 | Â Â Â Â Â Â Â Â conns->ptr =Â NULL; |
|---|
| 52 | Â Â Â Â Â Â Â Â conns->ptr =Â malloc(sizeof(*conns->ptr)Â *Â conns->size); |
|---|
| 53 |         for (i = 0; i < conns->size; i++) { |
|---|
| 54 | Â Â Â Â Â Â Â Â Â Â Â Â conns->ptr[i]Â =Â connection_init(srv); |
|---|
| 55 | Â Â Â Â Â Â Â Â } |
|---|
| 56 |     } else if (conns->size == conns->used) { |
|---|
| 57 | Â Â Â Â Â Â Â Â conns->size +=Â 128; |
|---|
| 58 |         conns->ptr = realloc(conns->ptr, sizeof(*conns->ptr) * conns->size); |
|---|
| 59 | |
|---|
| 60 |         for (i = conns->used; i < conns->size; i++) { |
|---|
| 61 | Â Â Â Â Â Â Â Â Â Â Â Â conns->ptr[i]Â =Â connection_init(srv); |
|---|
| 62 | Â Â Â Â Â Â Â Â } |
|---|
| 63 | Â Â Â Â } |
|---|
| 64 | |
|---|
| 65 |     connection_reset(srv, conns->ptr[conns->used]); |
|---|
| 66 | |
|---|
| 67 | Â Â Â Â conns->ptr[conns->used]->ndx =Â conns->used; |
|---|
| 68 |     return conns->ptr[conns->used++]; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | static int connection_del(server *srv, connection *con) { |
|---|
| 72 | Â Â Â Â size_t i; |
|---|
| 73 | Â Â Â Â connections *conns =Â srv->conns; |
|---|
| 74 | Â Â Â Â connection *temp; |
|---|
| 75 | |
|---|
| 76 |     if (con == NULL) return -1; |
|---|
| 77 | |
|---|
| 78 |     if (-1 == con->ndx) return -1; |
|---|
| 79 | |
|---|
| 80 | Â Â Â Â i =Â con->ndx; |
|---|
| 81 | |
|---|
| 82 | Â Â Â Â /* not last element */ |
|---|
| 83 | |
|---|
| 84 |     if (i != conns->used - 1) { |
|---|
| 85 | Â Â Â Â Â Â Â Â temp =Â conns->ptr[i]; |
|---|
| 86 | Â Â Â Â Â Â Â Â conns->ptr[i]Â =Â conns->ptr[conns->used -Â 1]; |
|---|
| 87 | Â Â Â Â Â Â Â Â conns->ptr[conns->used -Â 1]Â =Â temp; |
|---|
| 88 | |
|---|
| 89 | Â Â Â Â Â Â Â Â conns->ptr[i]->ndx =Â i; |
|---|
| 90 | Â Â Â Â Â Â Â Â conns->ptr[conns->used -Â 1]->ndx =Â -1; |
|---|
| 91 | Â Â Â Â } |
|---|
| 92 | |
|---|
| 93 | Â Â Â Â conns->used--; |
|---|
| 94 | |
|---|
| 95 | Â Â Â Â con->ndx =Â -1; |
|---|
| 96 | |
|---|
| 97 |     return 0; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | int connection_close(server *srv, connection *con) { |
|---|
| 101 | #ifdef USE_OPENSSL |
|---|
| 102 | Â Â Â Â /* should be in iosocket_close() */ |
|---|
| 103 | |
|---|
| 104 |     if (con->sock->ssl) { |
|---|
| 105 |         int ret, ssl_r; |
|---|
| 106 |         unsigned long err; |
|---|
| 107 | |
|---|
| 108 | Â Â Â Â Â Â Â Â ERR_clear_error(); |
|---|
| 109 |         switch (ret = SSL_shutdown(con->sock->ssl)) { |
|---|
| 110 |         case 1: |
|---|
| 111 | Â Â Â Â Â Â Â Â Â Â Â Â /* done */ |
|---|
| 112 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 113 |         case 0: |
|---|
| 114 | Â Â Â Â Â Â Â Â Â Â Â Â /* wait for fd-event |
|---|
| 115 | Â Â Â Â Â Â Â Â Â Â Â Â Â * |
|---|
| 116 | Â Â Â Â Â Â Â Â Â Â Â Â Â * FIXME: wait for fdevent and call SSL_shutdown again |
|---|
| 117 | Â Â Â Â Â Â Â Â Â Â Â Â Â * (But it is not that important as we close the underlying connection anyway) |
|---|
| 118 | Â Â Â Â Â Â Â Â Â Â Â Â Â */ |
|---|
| 119 | |
|---|
| 120 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 121 | Â Â Â Â Â Â Â Â default: |
|---|
| 122 |             switch ((ssl_r = SSL_get_error(con->sock->ssl, ret))) { |
|---|
| 123 |             case SSL_ERROR_WANT_WRITE: |
|---|
| 124 |             case SSL_ERROR_WANT_READ: |
|---|
| 125 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 126 |             case SSL_ERROR_SYSCALL: |
|---|
| 127 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* perhaps we have error waiting in our error-queue */ |
|---|
| 128 |                 if (0 != (err = ERR_get_error())) { |
|---|
| 129 |                     do { |
|---|
| 130 |                         ERROR("SSL_shutdown failed (%i, %i): %s", ssl_r, ret, ERR_error_string(err, NULL)); |
|---|
| 131 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }Â while((err =Â ERR_get_error())); |
|---|
| 132 |                 } else { |
|---|
| 133 |                     ERROR("SSL_shutdown failed (%i, %i, %i): %s", ssl_r, ret, errno, strerror(errno)); |
|---|
| 134 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 135 | |
|---|
| 136 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 137 | Â Â Â Â Â Â Â Â Â Â Â Â default: |
|---|
| 138 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â while((err =Â ERR_get_error()))Â { |
|---|
| 139 |                     ERROR("SSL_shutdown failed (%i, %i): %s", ssl_r, ret, ERR_error_string(err, NULL)); |
|---|
| 140 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 141 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 142 | Â Â Â Â Â Â Â Â } |
|---|
| 143 | |
|---|
| 144 | Â Â Â Â Â Â Â Â SSL_free(con->sock->ssl); |
|---|
| 145 | Â Â Â Â Â Â Â Â ERR_clear_error(); |
|---|
| 146 | Â Â Â Â Â Â Â Â con->sock->ssl =Â NULL; |
|---|
| 147 | Â Â Â Â } |
|---|
| 148 | #endif |
|---|
| 149 | |
|---|
| 150 |     fdevent_event_del(srv->ev, con->sock); |
|---|
| 151 |     fdevent_unregister(srv->ev, con->sock); |
|---|
| 152 | |
|---|
| 153 |     if (closesocket(con->sock->fd)) { |
|---|
| 154 |         log_error_write(srv, __FILE__, __LINE__, "sds", |
|---|
| 155 |                 "(warning) close:", con->sock->fd, strerror(errno)); |
|---|
| 156 | Â Â Â Â } |
|---|
| 157 | |
|---|
| 158 |     connection_del(srv, con); |
|---|
| 159 |     connection_set_state(srv, con, CON_STATE_CONNECT); |
|---|
| 160 | |
|---|
| 161 |     return 0; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | #if 0 |
|---|
| 165 | static void dump_packet(const unsigned char *data, size_t len) { |
|---|
| 166 | Â Â Â Â size_t i, j; |
|---|
| 167 | |
|---|
| 168 | Â Â Â Â if (len == 0) return; |
|---|
| 169 | |
|---|
| 170 | Â Â Â Â for (i = 0; i < len; i++) { |
|---|
| 171 | Â Â Â Â Â Â Â Â if (i % 16 == 0) fprintf(stderr, "Â "); |
|---|
| 172 | |
|---|
| 173 | Â Â Â Â Â Â Â Â fprintf(stderr, "%02x ", data[i]); |
|---|
| 174 | |
|---|
| 175 | Â Â Â Â Â Â Â Â if ((i + 1) % 16 == 0) { |
|---|
| 176 | Â Â Â Â Â Â Â Â Â Â Â Â fprintf(stderr, "Â "); |
|---|
| 177 | Â Â Â Â Â Â Â Â Â Â Â Â for (j = 0; j <= i % 16; j++) { |
|---|
| 178 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned char c; |
|---|
| 179 | |
|---|
| 180 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (i-15+j >= len) break; |
|---|
| 181 | |
|---|
| 182 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â c = data[i-15+j]; |
|---|
| 183 | |
|---|
| 184 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â fprintf(stderr, "%c", c > 32 && c < 128 ? c : '.'); |
|---|
| 185 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 186 | |
|---|
| 187 | Â Â Â Â Â Â Â Â Â Â Â Â fprintf(stderr, "\n"); |
|---|
| 188 | Â Â Â Â Â Â Â Â } |
|---|
| 189 | Â Â Â Â } |
|---|
| 190 | |
|---|
| 191 | Â Â Â Â if (len % 16 != 0) { |
|---|
| 192 | Â Â Â Â Â Â Â Â for (j = i % 16; j < 16; j++) { |
|---|
| 193 | Â Â Â Â Â Â Â Â Â Â Â Â fprintf(stderr, "Â Â "); |
|---|
| 194 | Â Â Â Â Â Â Â Â } |
|---|
| 195 | |
|---|
| 196 | Â Â Â Â Â Â Â Â fprintf(stderr, "Â "); |
|---|
| 197 | Â Â Â Â Â Â Â Â for (j = i & ~0xf; j < len; j++) { |
|---|
| 198 | Â Â Â Â Â Â Â Â Â Â Â Â unsigned char c; |
|---|
| 199 | |
|---|
| 200 | Â Â Â Â Â Â Â Â Â Â Â Â c = data[j]; |
|---|
| 201 | Â Â Â Â Â Â Â Â Â Â Â Â fprintf(stderr, "%c", c > 32 && c < 128 ? c : '.'); |
|---|
| 202 | Â Â Â Â Â Â Â Â } |
|---|
| 203 | Â Â Â Â Â Â Â Â fprintf(stderr, "\n"); |
|---|
| 204 | Â Â Â Â } |
|---|
| 205 | } |
|---|
| 206 | #endif |
|---|
| 207 | |
|---|
| 208 | static int connection_handle_response_header(server *srv, connection *con) { |
|---|
| 209 |     int no_response_body = 0; |
|---|
| 210 | |
|---|
| 211 |     if (con->mode == DIRECT) { |
|---|
| 212 | Â Â Â Â Â Â Â Â /* static files */ |
|---|
| 213 | Â Â Â Â Â Â Â Â switch(con->request.http_method)Â { |
|---|
| 214 |         case HTTP_METHOD_GET: |
|---|
| 215 |         case HTTP_METHOD_POST: |
|---|
| 216 |         case HTTP_METHOD_HEAD: |
|---|
| 217 | Â Â Â Â Â Â Â Â Â Â Â Â /* webdav */ |
|---|
| 218 |         case HTTP_METHOD_PUT: |
|---|
| 219 |         case HTTP_METHOD_MKCOL: |
|---|
| 220 |         case HTTP_METHOD_DELETE: |
|---|
| 221 |         case HTTP_METHOD_COPY: |
|---|
| 222 |         case HTTP_METHOD_MOVE: |
|---|
| 223 |         case HTTP_METHOD_PROPFIND: |
|---|
| 224 |         case HTTP_METHOD_PROPPATCH: |
|---|
| 225 |         case HTTP_METHOD_LOCK: |
|---|
| 226 |         case HTTP_METHOD_UNLOCK: |
|---|
| 227 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 228 |         case HTTP_METHOD_OPTIONS: |
|---|
| 229 | Â Â Â Â Â Â Â Â Â Â Â Â /* |
|---|
| 230 | Â Â Â Â Â Â Â Â Â Â Â Â Â * 400 is coming from the request-parser BEFORE uri.path is set |
|---|
| 231 | Â Â Â Â Â Â Â Â Â Â Â Â Â * 403 is from the response handler when noone else catched it |
|---|
| 232 | Â Â Â Â Â Â Â Â Â Â Â Â Â * |
|---|
| 233 | Â Â Â Â Â Â Â Â Â Â Â Â Â * */ |
|---|
| 234 |             if ((!con->http_status || con->http_status == 200) && |
|---|
| 235 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â con->uri.path->used &&Â con->uri.path->ptr[0]Â !=Â '*')Â { |
|---|
| 236 |                 response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST")); |
|---|
| 237 | |
|---|
| 238 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* trash the content */ |
|---|
| 239 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â no_response_body =Â 1; |
|---|
| 240 | |
|---|
| 241 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â con->http_status =Â 200; |
|---|
| 242 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 243 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 244 | Â Â Â Â Â Â Â Â default: |
|---|
| 245 | Â Â Â Â Â Â Â Â Â Â Â Â switch(con->http_status)Â { |
|---|
| 246 |             case 400: /* bad request */ |
|---|
| 247 |             case 414: /* overload request header */ |
|---|
| 248 |             case 505: /* unknown protocol */ |
|---|
| 249 |             case 207: /* this was webdav */ |
|---|
| 250 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 251 | Â Â Â Â Â Â Â Â Â Â Â Â default: |
|---|
| 252 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â con->http_status =Â 501; |
|---|
| 253 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 254 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 255 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 256 | Â Â Â Â Â Â Â Â } |
|---|
| 257 | Â Â Â Â } |
|---|
| 258 | |
|---|
| 259 |     if (con->http_status == 0) { |
|---|
| 260 |         TRACE("%s", "no status, setting 403"); |
|---|
| 261 | Â Â Â Â Â Â Â Â con->http_status =Â 403; |
|---|
| 262 | Â Â Â Â } |
|---|
| 263 | |
|---|
| 264 | Â Â Â Â switch(con->http_status)Â { |
|---|
| 265 |     case 400: /* class: header + custom body */ |
|---|
| 266 |     case 401: |
|---|
| 267 |     case 403: |
|---|
| 268 |     case 404: |
|---|
| 269 |     case 408: |
|---|
| 270 |     case 409: |
|---|
| 271 |     case 410: |
|---|
| 272 |     case 411: |
|---|
| 273 |     case 416: |
|---|
| 274 |     case 423: |
|---|
| 275 |     case 500: |
|---|
| 276 |     case 501: |
|---|
| 277 |     case 502: |
|---|
| 278 |     case 503: |
|---|
| 279 |     case 504: |
|---|
| 280 |     case 505: |
|---|
| 281 |     case 509: |
|---|
| 282 |         if (con->mode != DIRECT) break; |
|---|
| 283 | |
|---|
| 284 | Â Â Â Â Â Â Â Â con->send->is_closed =Â 0; |
|---|
| 285 | |
|---|
| 286 | Â Â Â Â Â Â Â Â buffer_reset(con->physical.path); |
|---|
| 287 | |
|---|
| 288 | Â Â Â Â Â Â Â Â /* try to send static errorfile */ |
|---|
| 289 |         if (!buffer_is_empty(con->conf.errorfile_prefix)) { |
|---|
| 290 | Â Â Â Â Â Â Â Â Â Â Â Â stat_cache_entry *sce =Â NULL; |
|---|
| 291 | |
|---|
| 292 |             buffer_copy_string_buffer(con->physical.path, con->conf.errorfile_prefix); |
|---|
| 293 |             buffer_append_string(con->physical.path, get_http_status_body_name(con->http_status)); |
|---|
| 294 | |
|---|
| 295 |             if (HANDLER_ERROR != stat_cache_get_entry(srv, con, con->physical.path, &sce)) { |
|---|
| 296 |                 chunkqueue_append_file(con->send, con->physical.path, 0, sce->st.st_size); |
|---|
| 297 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â con->send->bytes_in +=Â sce->st.st_size; |
|---|
| 298 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â con->send->is_closed =Â 1; |
|---|
| 299 |                 response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type)); |
|---|
| 300 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 301 | Â Â Â Â Â Â Â Â } |
|---|
| 302 | |
|---|
| 303 |         if (!con->send->is_closed) { |
|---|
| 304 | Â Â Â Â Â Â Â Â Â Â Â Â buffer *b; |
|---|
| 305 | |
|---|
| 306 | Â Â Â Â Â Â Â Â Â Â Â Â buffer_reset(con->physical.path); |
|---|
| 307 | |
|---|
| 308 | Â Â Â Â Â Â Â Â Â Â Â Â con->send->is_closed =Â 1; |
|---|
| 309 | Â Â Â Â Â Â Â Â Â Â Â Â b =Â chunkqueue_get_append_buffer(con->send); |
|---|
| 310 | |
|---|
| 311 | Â Â Â Â Â Â Â Â Â Â Â Â /* build default error-page */ |
|---|
| 312 |             buffer_copy_string_len(b, CONST_STR_LEN( |
|---|
| 313 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "<?xml version=\"1.0\"Â encoding=\"iso-8859-1\"?>\n" |
|---|
| 314 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" |
|---|
| 315 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Â Â Â Â Â \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" |
|---|
| 316 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "<html xmlns=\"http://www.w3.org/1999/xhtml\"Â xml:lang=\"en\"Â lang=\"en\">\n" |
|---|
| 317 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â " <head>\n" |
|---|
| 318 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Â <title>")); |
|---|
| 319 |             buffer_append_long(b, con->http_status); |
|---|
| 320 |             buffer_append_string_len(b, CONST_STR_LEN(" - ")); |
|---|
| 321 |             buffer_append_string(b, get_http_status_name(con->http_status)); |
|---|
| 322 | |
|---|
| 323 | Â Â Â Â Â Â Â Â Â Â Â Â buffer_append_string(b, |
|---|
| 324 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "</title>\n" |
|---|
| 325 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â " </head>\n" |
|---|
| 326 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â " <body>\n" |
|---|
| 327 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Â <h1>"); |
|---|
| 328 |             buffer_append_long(b, con->http_status); |
|---|
| 329 |             buffer_append_string_len(b, CONST_STR_LEN(" - ")); |
|---|
| 330 |             buffer_append_string(b, get_http_status_name(con->http_status)); |
|---|
| 331 | |
|---|
| 332 | Â Â Â Â Â Â Â Â Â Â Â Â buffer_append_string(b,"</h1>\n" |
|---|
| 333 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â " </body>\n" |
|---|
| 334 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "</html>\n" |
|---|
| 335 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ); |
|---|
| 336 | |
|---|
| 337 | Â Â Â Â Â Â Â Â Â Â Â Â con->send->bytes_in +=Â b->used -Â 1; |
|---|
| 338 | |
|---|
| 339 |             response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html")); |
|---|
| 340 | Â Â Â Â Â Â Â Â } |
|---|
| 341 | Â Â Â Â Â Â Â Â /* fall through */ |
|---|
| 342 |     case 207: |
|---|
| 343 |     case 200: /* class: header + body */ |
|---|
| 344 |     case 201: |
|---|
| 345 |     case 301: |
|---|
| 346 |     case 302: |
|---|
| 347 |     case 303: |
|---|
| 348 | Â Â Â Â Â Â Â Â break; |
|---|
| 349 | |
|---|
| 350 |     case 206: /* write_queue is already prepared */ |
|---|
| 351 | Â Â Â Â Â Â Â Â break; |
|---|
| 352 |     case 205: /* class: header only */ |
|---|
| 353 |     case 304: |
|---|
| 354 | Â Â Â Â default: |
|---|
| 355 |         if (con->mode == DIRECT) { |
|---|
| 356 | Â Â Â Â Â Â Â Â Â Â Â Â /* only if we have handled the request internally |
|---|
| 357 | Â Â Â Â Â Â Â Â Â Â Â Â Â * we will see no response-content |
|---|
| 358 | Â Â Â Â Â Â Â Â Â Â Â Â Â * |
|---|
| 359 | Â Â Â Â Â Â Â Â Â Â Â Â Â * if it was a fastcgi request we will see a END_REQUEST packet |
|---|
| 360 | Â Â Â Â Â Â Â Â Â Â Â Â Â * after the header was parsed. |
|---|
| 361 | Â Â Â Â Â Â Â Â Â Â Â Â Â * |
|---|
| 362 | Â Â Â Â Â Â Â Â Â Â Â Â Â *Â */ |
|---|
| 363 | Â Â Â Â Â Â Â Â Â Â Â Â no_response_body =Â 1; |
|---|
| 364 | Â Â Â Â Â Â Â Â } |
|---|
| 365 | Â Â Â Â Â Â Â Â break; |
|---|
| 366 | Â Â Â Â } |
|---|
| 367 | |
|---|
| 368 |     if (no_response_body) { |
|---|
| 369 | Â Â Â Â Â Â Â Â /* disable chunked encoding again as we have no body */ |
|---|
| 370 | Â Â Â Â Â Â Â Â con->response.transfer_encoding &=Â ~HTTP_TRANSFER_ENCODING_CHUNKED; |
|---|
| 371 | Â Â Â Â Â Â Â Â chunkqueue_reset(con->send); |
|---|
| 372 | |
|---|
| 373 | Â Â Â Â Â Â Â Â con->send->is_closed =Â 1; |
|---|
| 374 | Â Â Â Â } |
|---|
| 375 | |
|---|
| 376 |     return 0; |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | connection *connection_init(server *srv)Â { |
|---|
| 380 | Â Â Â Â connection *con; |
|---|
| 381 | |
|---|
| 382 | Â Â Â Â UNUSED(srv); |
|---|
| 383 | |
|---|
| 384 |     con = calloc(1, sizeof(*con)); |
|---|
| 385 | |
|---|
| 386 | Â Â Â Â con->sock =Â iosocket_init(); |
|---|
| 387 | Â Â Â Â con->ndx =Â -1; |
|---|
| 388 | Â Â Â Â con->bytes_written =Â 0; |
|---|
| 389 | Â Â Â Â con->bytes_read =Â 0; |
|---|
| 390 | Â Â Â Â con->bytes_header =Â 0; |
|---|
| 391 | Â Â Â Â con->loops_per_request =Â 0; |
|---|
| 392 | |
|---|
| 393 | #define CLEAN(x) \ |
|---|
| 394 | Â Â Â Â con->x = buffer_init(); |
|---|
| 395 | |
|---|
| 396 | Â Â Â Â CLEAN(request.uri); |
|---|
| 397 | Â Â Â Â CLEAN(request.request); |
|---|
| 398 | Â Â Â Â CLEAN(request.pathinfo); |
|---|
| 399 | Â Â Â Â CLEAN(request.http_host); |
|---|
| 400 | |
|---|
| 401 | Â Â Â Â CLEAN(request.orig_uri); |
|---|
| 402 | |
|---|
| 403 | Â Â Â Â CLEAN(uri.scheme); |
|---|
| 404 | Â Â Â Â CLEAN(uri.authority); |
|---|
| 405 | Â Â Â Â CLEAN(uri.path); |
|---|
| 406 | Â Â Â Â CLEAN(uri.path_raw); |
|---|
| 407 | Â Â Â Â CLEAN(uri.query); |
|---|
| 408 | |
|---|
| 409 | Â Â Â Â CLEAN(physical.doc_root); |
|---|
| 410 | Â Â Â Â CLEAN(physical.path); |
|---|
| 411 | Â Â Â Â CLEAN(physical.basedir); |
|---|
| 412 | Â Â Â Â CLEAN(physical.rel_path); |
|---|
| 413 | Â Â Â Â CLEAN(physical.etag); |
|---|
| 414 | Â Â Â Â CLEAN(parse_request); |
|---|
| 415 | |
|---|
| 416 | Â Â Â Â CLEAN(authed_user); |
|---|
| 417 | Â Â Â Â CLEAN(server_name); |
|---|
| 418 | Â Â Â Â CLEAN(error_handler); |
|---|
| 419 | Â Â Â Â CLEAN(dst_addr_buf); |
|---|
| 420 | |
|---|
| 421 | #undef CLEAN |
|---|
| 422 | Â Â Â Â con->send_filters =Â filter_chain_init(); |
|---|
| 423 | Â Â Â Â /* send is the chunkqueue of the first send filter */ |
|---|
| 424 | Â Â Â Â con->send =Â con->send_filters->first->cq; |
|---|
| 425 | Â Â Â Â con->recv =Â chunkqueue_init(); |
|---|
| 426 |     chunkqueue_set_tempdirs(con->recv, srv->srvconf.upload_tempdirs); |
|---|
| 427 | |
|---|
| 428 | Â Â Â Â con->send_raw =Â chunkqueue_init(); |
|---|
| 429 | Â Â Â Â con->recv_raw =Â chunkqueue_init(); |
|---|
| 430 | |
|---|
| 431 |     con->request.headers   = array_init(); |
|---|
| 432 |     con->response.headers   = array_init(); |
|---|
| 433 |     con->environment   = array_init(); |
|---|
| 434 | |
|---|
| 435 | Â Â Â Â con->http_req =Â http_request_init(); |
|---|
| 436 | |
|---|
| 437 | Â Â Â Â /* init plugin specific connection structures */ |
|---|
| 438 | |
|---|
| 439 |     con->plugin_ctx = calloc(1, (srv->plugins.used + 1) * sizeof(void *)); |
|---|
| 440 | |
|---|
| 441 |     con->cond_cache = calloc(srv->config_context->used, sizeof(cond_cache_t)); |
|---|
| 442 |     config_setup_connection(srv, con); |
|---|
| 443 | |
|---|
| 444 |     return con; |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | void connections_free(server *srv) { |
|---|
| 448 | Â Â Â Â connections *conns =Â srv->conns; |
|---|
| 449 | Â Â Â Â size_t i; |
|---|
| 450 | |
|---|
| 451 |     for (i = 0; i < conns->size; i++) { |
|---|
| 452 | Â Â Â Â Â Â Â Â connection *con =Â conns->ptr[i]; |
|---|
| 453 | |
|---|
| 454 |         connection_reset(srv, con); |
|---|
| 455 | Â Â Â Â Â Â Â Â iosocket_free(con->sock); |
|---|
| 456 | |
|---|
| 457 | Â Â Â Â Â Â Â Â filter_chain_free(con->send_filters); |
|---|
| 458 | Â Â Â Â Â Â Â Â con->send =Â NULL; |
|---|
| 459 | Â Â Â Â Â Â Â Â chunkqueue_free(con->recv); |
|---|
| 460 | Â Â Â Â Â Â Â Â chunkqueue_free(con->send_raw); |
|---|
| 461 | Â Â Â Â Â Â Â Â chunkqueue_free(con->recv_raw); |
|---|
| 462 | Â Â Â Â Â Â Â Â array_free(con->request.headers); |
|---|
| 463 | Â Â Â Â Â Â Â Â array_free(con->response.headers); |
|---|
| 464 | Â Â Â Â Â Â Â Â array_free(con->environment); |
|---|
| 465 | |
|---|
| 466 | #define CLEAN(x) \ |
|---|
| 467 | Â Â Â Â buffer_free(con->x); |
|---|
| 468 | |
|---|
| 469 | Â Â Â Â Â Â Â Â CLEAN(request.uri); |
|---|
| 470 | Â Â Â Â Â Â Â Â CLEAN(request.request); |
|---|
| 471 | Â Â Â Â Â Â Â Â CLEAN(request.pathinfo); |
|---|
| 472 | Â Â Â Â Â Â Â Â CLEAN(request.http_host); |
|---|
| 473 | |
|---|
| 474 | Â Â Â Â Â Â Â Â CLEAN(request.orig_uri); |
|---|
| 475 | |
|---|
| 476 | Â Â Â Â Â Â Â Â CLEAN(uri.scheme); |
|---|
| 477 | Â Â Â Â Â Â Â Â CLEAN(uri.authority); |
|---|
| 478 | Â Â Â Â Â Â Â Â CLEAN(uri.path); |
|---|
| 479 | Â Â Â Â Â Â Â Â CLEAN(uri.path_raw); |
|---|
| 480 | Â Â Â Â Â Â Â Â CLEAN(uri.query); |
|---|
| 481 | |
|---|
| 482 | Â Â Â Â Â Â Â Â CLEAN(physical.doc_root); |
|---|
| 483 | Â Â Â Â Â Â Â Â CLEAN(physical.path); |
|---|
| 484 | Â Â Â Â Â Â Â Â CLEAN(physical.basedir); |
|---|
| 485 | Â Â Â Â Â Â Â Â CLEAN(physical.etag); |
|---|
| 486 | Â Â Â Â Â Â Â Â CLEAN(physical.rel_path); |
|---|
| 487 | Â Â Â Â Â Â Â Â CLEAN(parse_request); |
|---|
| 488 | |
|---|
| 489 | Â Â Â Â Â Â Â Â CLEAN(authed_user); |
|---|
| 490 | Â Â Â Â Â Â Â Â CLEAN(server_name); |
|---|
| 491 | Â Â Â Â Â Â Â Â CLEAN(error_handler); |
|---|
| 492 | Â Â Â Â Â Â Â Â CLEAN(dst_addr_buf); |
|---|
| 493 | #undef CLEAN |
|---|
| 494 | Â Â Â Â Â Â Â Â free(con->plugin_ctx); |
|---|
| 495 | Â Â Â Â Â Â Â Â free(con->cond_cache); |
|---|
| 496 | |
|---|
| 497 | Â Â Â Â Â Â Â Â http_request_free(con->http_req); |
|---|
| 498 | |
|---|
| 499 | Â Â Â Â Â Â Â Â free(con); |
|---|
| 500 | Â Â Â Â } |
|---|
| 501 | |
|---|
| 502 | Â Â Â Â free(conns->ptr); |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | int connection_reset(server *srv, connection *con) { |
|---|
| 507 | Â Â Â Â size_t i; |
|---|
| 508 | |
|---|
| 509 |     plugins_call_connection_reset(srv, con); |
|---|
| 510 | |
|---|
| 511 | Â Â Â Â con->is_readable =Â 1; |
|---|
| 512 | Â Â Â Â con->is_writable =Â 1; |
|---|
| 513 | Â Â Â Â con->http_status =Â 0; |
|---|
| 514 | Â Â Â Â con->file_started =Â 0; |
|---|
| 515 | Â Â Â Â con->got_response =Â 0; |
|---|
| 516 | |
|---|
| 517 | Â Â Â Â con->bytes_written =Â 0; |
|---|
| 518 | Â Â Â Â con->bytes_written_cur_second =Â 0; |
|---|
| 519 | Â Â Â Â con->bytes_read =Â 0; |
|---|
| 520 | Â Â Â Â con->bytes_header =Â 0; |
|---|
| 521 | Â Â Â Â con->loops_per_request =Â 0; |
|---|
| 522 | |
|---|
| 523 | Â Â Â Â con->request.http_method =Â HTTP_METHOD_UNSET; |
|---|
| 524 | Â Â Â Â con->request.http_version =Â HTTP_VERSION_UNSET; |
|---|
| 525 | Â Â Â Â con->request.content_length =Â -1; |
|---|
| 526 | |
|---|
| 527 | Â Â Â Â con->response.keep_alive =Â 0; |
|---|
| 528 | Â Â Â Â con->response.content_length =Â -1; |
|---|
| 529 | Â Â Â Â con->response.transfer_encoding =Â 0; |
|---|
| 530 | |
|---|
| 531 | Â Â Â Â con->mode =Â DIRECT; |
|---|
| 532 | |
|---|
| 533 | #define CLEAN(x) \ |
|---|
| 534 | Â Â Â Â if (con->x) buffer_reset(con->x); |
|---|
| 535 | |
|---|
| 536 | Â Â Â Â CLEAN(request.uri); |
|---|
| 537 | Â Â Â Â CLEAN(request.pathinfo); |
|---|
| 538 | Â Â Â Â CLEAN(request.request); |
|---|
| 539 | Â Â Â Â CLEAN(request.http_host); |
|---|
| 540 | |
|---|
| 541 | Â Â Â Â CLEAN(request.orig_uri); |
|---|
| 542 | |
|---|
| 543 | Â Â Â Â CLEAN(uri.scheme); |
|---|
| 544 | Â Â Â Â CLEAN(uri.authority); |
|---|
| 545 | Â Â Â Â CLEAN(uri.path); |
|---|
| 546 | Â Â Â Â CLEAN(uri.path_raw); |
|---|
| 547 | Â Â Â Â CLEAN(uri.query); |
|---|
| 548 | |
|---|
| 549 | Â Â Â Â CLEAN(physical.doc_root); |
|---|
| 550 | Â Â Â Â CLEAN(physical.path); |
|---|
| 551 | Â Â Â Â CLEAN(physical.basedir); |
|---|
| 552 | Â Â Â Â CLEAN(physical.rel_path); |
|---|
| 553 | Â Â Â Â CLEAN(physical.etag); |
|---|
| 554 | |
|---|
| 555 | Â Â Â Â CLEAN(parse_request); |
|---|
| 556 | |
|---|
| 557 | Â Â Â Â CLEAN(authed_user); |
|---|
| 558 | Â Â Â Â CLEAN(server_name); |
|---|
| 559 | Â Â Â Â CLEAN(error_handler); |
|---|
| 560 | #undef CLEAN |
|---|
| 561 | |
|---|
| 562 | #define CLEAN(x) \ |
|---|
| 563 | Â Â Â Â if (con->x) con->x->used = 0; |
|---|
| 564 | |
|---|
| 565 | #undef CLEAN |
|---|
| 566 | |
|---|
| 567 | Â Â Â Â array_reset(con->request.headers); |
|---|
| 568 | Â Â Â Â array_reset(con->response.headers); |
|---|
| 569 | Â Â Â Â array_reset(con->environment); |
|---|
| 570 | |
|---|
| 571 | Â Â Â Â filter_chain_reset(con->send_filters); |
|---|
| 572 | Â Â Â Â con->send =Â con->send_filters->first->cq; |
|---|
| 573 | Â Â Â Â chunkqueue_reset(con->recv); |
|---|
| 574 | Â Â Â Â chunkqueue_reset(con->send_raw); |
|---|
| 575 | |
|---|
| 576 | Â Â Â Â http_request_reset(con->http_req); |
|---|
| 577 | |
|---|
| 578 | Â Â Â Â /* the plugins should cleanup themself */ |
|---|
| 579 |     for (i = 0; i < srv->plugins.used; i++) { |
|---|
| 580 | Â Â Â Â Â Â Â Â plugin *p =Â ((plugin **)(srv->plugins.ptr))[i]; |
|---|
| 581 | Â Â Â Â Â Â Â Â plugin_data *pd =Â p->data; |
|---|
| 582 | |
|---|
| 583 |         if (!pd) continue; |
|---|
| 584 | |
|---|
| 585 |         if (con->plugin_ctx[pd->id] != NULL) { |
|---|
| 586 |             log_error_write(srv, __FILE__, __LINE__, "sb", "missing cleanup in", p->name); |
|---|
| 587 | Â Â Â Â Â Â Â Â } |
|---|
| 588 | |
|---|
| 589 | Â Â Â Â Â Â Â Â con->plugin_ctx[pd->id]Â =Â NULL; |
|---|
| 590 | Â Â Â Â } |
|---|
| 591 | |
|---|
| 592 |     config_cond_cache_reset(srv, con); |
|---|
| 593 | |
|---|
| 594 | Â Â Â Â con->header_len =Â 0; |
|---|
| 595 | Â Â Â Â con->in_error_handler =Â 0; |
|---|
| 596 | |
|---|
| 597 |     config_setup_connection(srv, con); |
|---|
| 598 | |
|---|
| 599 |     return 0; |
|---|
| 600 | } |
|---|
| 601 | |
|---|
| 602 | /** |
|---|
| 603 | Â * handle all header and content read |
|---|
| 604 | Â * |
|---|
| 605 | Â * we get called by the state-engine and by the fdevent-handler |
|---|
| 606 | Â */ |
|---|
| 607 | handler_t connection_handle_read_request_header(server *srv, connection *con) { |
|---|
| 608 | Â Â Â Â /* let's see if we need more data later */ |
|---|
| 609 |     fdevent_event_del(srv->ev, con->sock); |
|---|
| 610 | |
|---|
| 611 | Â Â Â Â con->read_idle_ts =Â srv->cur_ts;Â /* start a read-call() */ |
|---|
| 612 | |
|---|
| 613 | Â Â Â Â /* read from the network */ |
|---|
| 614 |     switch (network_read(srv, con, con->sock, con->recv_raw)) { |
|---|
| 615 |     case NETWORK_STATUS_SUCCESS: |
|---|
| 616 | Â Â Â Â Â Â Â Â /* we read everything from the socket, do we have a full header ? */ |
|---|
| 617 | Â Â Â Â Â Â Â Â break; |
|---|
| 618 |     case NETWORK_STATUS_WAIT_FOR_EVENT: |
|---|
| 619 |         fdevent_event_add(srv->ev, con->sock, FDEVENT_IN); |
|---|
| 620 |         return HANDLER_WAIT_FOR_EVENT; |
|---|
| 621 |     case NETWORK_STATUS_CONNECTION_CLOSE: |
|---|
| 622 | Â Â Â Â Â Â Â Â /* the connection went away before we got something back */ |
|---|
| 623 |         connection_set_state(srv, con, CON_STATE_CLOSE); |
|---|
| 624 | |
|---|
| 625 |         return HANDLER_GO_ON; |
|---|
| 626 | Â Â Â Â default: |
|---|
| 627 |         ERROR("++ %s", "oops, something went wrong while reading"); |
|---|
| 628 |         return HANDLER_ERROR; |
|---|
| 629 | Â Â Â Â } |
|---|
| 630 | |
|---|
| 631 |     switch (http_request_parse_cq(con->recv_raw, con->http_req)) { |
|---|
| 632 |     case PARSE_ERROR: |
|---|
| 633 | Â Â Â Â Â Â Â Â con->http_status =Â 400;Â /* the header is broken */ |
|---|
| 634 | Â Â Â Â Â Â Â Â con->send->is_closed =Â 1;Â /* we have nothing to send */ |
|---|
| 635 | |
|---|
| 636 | Â Â Â Â Â Â Â Â chunkqueue_remove_finished_chunks(con->recv_raw); |
|---|
| 637 | |
|---|
| 638 |         return HANDLER_FINISHED; |
|---|
| 639 |     case PARSE_NEED_MORE: |
|---|
| 640 | Â Â Â Â Â Â Â Â /* we need more */ |
|---|
| 641 |         fdevent_event_add(srv->ev, con->sock, FDEVENT_IN); |
|---|
| 642 | |
|---|
| 643 |         return HANDLER_WAIT_FOR_EVENT; |
|---|
| 644 |     case PARSE_SUCCESS: |
|---|
| 645 | Â Â Â Â Â Â Â Â chunkqueue_remove_finished_chunks(con->recv_raw); |
|---|
| 646 | Â Â Â Â Â Â Â Â break; |
|---|
| 647 | Â Â Â Â default: |
|---|
| 648 | Â Â Â Â Â Â Â Â chunkqueue_remove_finished_chunks(con->recv_raw); |
|---|
| 649 |         TRACE("%s", "(error)"); |
|---|
| 650 |         return HANDLER_ERROR; |
|---|
| 651 | Â Â Â Â } |
|---|
| 652 | |
|---|
| 653 |     return HANDLER_GO_ON; |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | /* decode the HTTP/1.1 chunk encoding */ |
|---|
| 657 | |
|---|
| 658 | handler_t connection_handle_read_request_content(server *srv, connection *con) { |
|---|
| 659 | Â Â Â Â /* read data from the socket and push it to the backend */ |
|---|
| 660 | |
|---|
| 661 | Â Â Â Â chunkqueue *in =Â con->recv_raw; |
|---|
| 662 | Â Â Â Â chunkqueue *out =Â con->recv;Â /* the pure content */ |
|---|
| 663 | Â Â Â Â chunk *c; |
|---|
| 664 | |
|---|
| 665 | Â Â Â Â /* let's see if we need more data later */ |
|---|
| 666 |     fdevent_event_del(srv->ev, con->sock); |
|---|
| 667 | |
|---|
| 668 | Â Â Â Â con->read_idle_ts =Â srv->cur_ts;Â /* start a read-call() */ |
|---|
| 669 | |
|---|
| 670 |     if (con->request.content_length == -1) return HANDLER_GO_ON; |
|---|
| 671 | |
|---|
| 672 | Â Â Â Â /* if the content was short enough, it might be read already */ |
|---|
| 673 |     if (in->first && |
|---|
| 674 | Â Â Â Â Â Â chunkqueue_length(in)Â -Â in->first->offset >Â 0)Â { |
|---|
| 675 | |
|---|
| 676 | Â Â Â Â Â Â Â Â /* |
|---|
| 677 | Â Â Â Â Â Â Â Â Â * looks like the request-header also had some content for us |
|---|
| 678 | Â Â Â Â Â Â Â Â Â */ |
|---|
| 679 | |
|---|
| 680 |     } else { |
|---|
| 681 | Â Â Â Â Â Â Â Â /* read from the network */ |
|---|
| 682 |         switch (network_read(srv, con, con->sock, in)) { |
|---|
| 683 |         case NETWORK_STATUS_SUCCESS: |
|---|
| 684 | Â Â Â Â Â Â Â Â Â Â Â Â /* we have data */ |
|---|
| 685 | Â Â Â Â Â Â Â Â Â Â Â Â break; |
|---|
| 686 |         case NETWORK_STATUS_WAIT_FOR_EVENT: |
|---|
| 687 |             fdevent_event_add(srv->ev, con->sock, FDEVENT_IN); |
|---|
| 688 |             return HANDLER_WAIT_FOR_EVENT; |
|---|
| 689 |         case NETWORK_STATUS_CONNECTION_CLOSE: |
|---|
| 690 | Â Â Â Â Â Â Â Â Â Â Â Â /* the connection went away before we got something back */ |
|---|
| 691 |             connection_set_state(srv, con, CON_STATE_CLOSE); |
|---|
| 692 | |
|---|
| 693 |             return HANDLER_GO_ON; |
|---|
| 694 | Â Â Â Â Â Â Â Â default: |
|---|
| 695 |             ERROR("++ %s", "oops, something went wrong while reading"); |
|---|
| 696 |             return HANDLER_ERROR; |
|---|
| 697 | Â Â Â Â Â Â Â Â } |
|---|
| 698 | Â Â Â Â } |
|---|
| 699 | |
|---|
| 700 | Â Â Â Â /* how much data do we want to extract ? */ |
|---|
| 701 |     for (c = in->first; c && (out->bytes_in != con->request.content_length); c = c->next) { |
|---|
| 702 |         off_t weWant, weHave, toRead; |
|---|
| 703 | |
|---|
| 704 | Â Â Â Â Â Â Â Â weWant =Â con->request.content_length -Â out->bytes_in; |
|---|
| 705 | |
|---|
| 706 |         if (c->mem->used == 0) continue; |
|---|
| 707 | |
|---|
| 708 | Â Â Â Â Â Â Â Â weHave =Â c->mem->used -Â c->offset -Â 1; |
|---|
| 709 | |
|---|
| 710 |         toRead = weHave > weWant ? weWant : weHave; |
|---|
| 711 | |
|---|
| 712 | Â Â Â Â Â Â Â Â /* the new way, copy everything into a chunkqueue whcih might use tempfiles */ |
|---|
| 713 |         if (con->request.content_length > 64 * 1024) { |
|---|
| 714 | Â Â Â Â Â Â Â Â Â Â Â Â chunk *dst_c =Â NULL; |
|---|
| 715 | Â Â Â Â Â Â Â Â Â Â Â Â /* copy everything to max 1Mb sized tempfiles */ |
|---|
| 716 | |
|---|
| 717 | Â Â Â Â Â Â Â Â Â Â Â Â /* |
|---|
| 718 | Â Â Â Â Â Â Â Â Â Â Â Â Â * if the last chunk is |
|---|
| 719 | Â Â Â Â Â Â Â Â Â Â Â Â Â * - smaller than 1Mb (size < 1Mb) |
|---|
| 720 | Â Â Â Â Â Â Â Â Â Â Â Â Â * - not read yet (offset == 0) |
|---|
| 721 | Â Â Â Â Â Â Â Â Â Â Â Â Â * -> append to it |
|---|
| 722 | Â Â Â Â Â Â Â Â Â Â Â Â Â * otherwise |
|---|
| 723 | Â Â Â Â Â Â Â Â Â Â Â Â Â * -> create a new chunk |
|---|
| 724 | Â Â Â Â Â Â Â Â Â Â Â Â Â * |
|---|
| 725 | Â Â Â Â Â Â Â Â Â Â Â Â Â * */ |
|---|
| 726 | |
|---|
| 727 |             if (out->last && |
|---|
| 728 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â out->last->type ==Â FILE_CHUNK && |
|---|
| 729 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â out->last->file.is_temp && |
|---|
| 730 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â out->last->offset ==Â 0)Â { |
|---|
| 731 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* ok, take the last chunk for our job */ |
|---|
| 732 | |
|---|
| 733 |                 if (out->last->file.length < 1 * 1024 * 1024) { |
|---|
| 734 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dst_c =Â out->last; |
|---|
| 735 | |
|---|
| 736 |                     if (dst_c->file.fd == -1) { |
|---|
| 737 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* this should not happen as we cache the fd, but you never know */ |
|---|
| 738 |                         dst_c->file.fd = open(dst_c->file.name->ptr, O_WRONLY | O_APPEND); |
|---|
| 739 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 740 |                 } else { |
|---|
| 741 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* the chunk is too large now, close it */ |
|---|
| 742 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dst_c =Â out->last; |
|---|
| 743 | |
|---|
| 744 |                     if (dst_c->file.fd != -1) { |
|---|
| 745 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â close(dst_c->file.fd); |
|---|
| 746 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dst_c->file.fd =Â -1; |
|---|
| 747 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 748 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dst_c =Â chunkqueue_get_append_tempfile(out); |
|---|
| 749 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 750 |             } else { |
|---|
| 751 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dst_c =Â chunkqueue_get_append_tempfile(out); |
|---|
| 752 | Â Â Â Â Â Â Â Â Â Â Â Â } |
|---|
| 753 | |
|---|
| 754 | Â Â Â Â Â Â Â Â Â Â Â Â /* we have a chunk, let's write to it */ |
|---|
| 755 | |
|---|
| 756 |             if (dst_c->file.fd == -1) { |
|---|
| 757 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â /* we don't have file to write to, |
|---|
| 758 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â * EACCES might be one reason. |
|---|
| 759 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â * |
|---|
| 760 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â * Instead of sending 500 we send 413 and say the request is too large |
|---|
| 761 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â *Â */ |
|---|
| 762 | |
|---|
| 763 |                 log_error_write(srv, __FILE__, __LINE__, "sbs", |
|---|
| 764 | Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "denying upload as opening to temp-file for upload failed:", |
|---|
| 765 |                         dst_c->file.name, strerror(errno)); |
|---|
| 766 | |
|---|
| 767 | Â Â Â Â Â Â Â Â Â Â |
|---|