2005-02-20 14:27:00 +00:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "server.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "connections.h"
|
|
|
|
#include "fdevent.h"
|
|
|
|
|
|
|
|
#include "request.h"
|
|
|
|
#include "response.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "http_chunk.h"
|
2005-08-08 08:22:06 +00:00
|
|
|
#include "stat_cache.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "joblist.h"
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2005-08-09 06:42:33 +00:00
|
|
|
#include "inet_ntop_cache.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef USE_OPENSSL
|
2006-10-04 13:26:23 +00:00
|
|
|
# include <openssl/ssl.h>
|
|
|
|
# include <openssl/err.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
# include <sys/filio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "sys-socket.h"
|
|
|
|
|
2005-09-02 17:07:00 +00:00
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
|
|
|
} plugin_data;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
static connection *connections_get_new_connection(server *srv) {
|
|
|
|
connections *conns = srv->conns;
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (conns->size == 0) {
|
|
|
|
conns->size = 128;
|
|
|
|
conns->ptr = NULL;
|
|
|
|
conns->ptr = malloc(sizeof(*conns->ptr) * conns->size);
|
|
|
|
for (i = 0; i < conns->size; i++) {
|
|
|
|
conns->ptr[i] = connection_init(srv);
|
|
|
|
}
|
|
|
|
} else if (conns->size == conns->used) {
|
|
|
|
conns->size += 128;
|
|
|
|
conns->ptr = realloc(conns->ptr, sizeof(*conns->ptr) * conns->size);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = conns->used; i < conns->size; i++) {
|
|
|
|
conns->ptr[i] = connection_init(srv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connection_reset(srv, conns->ptr[conns->used]);
|
2006-10-04 13:26:23 +00:00
|
|
|
#if 0
|
2005-02-20 14:27:00 +00:00
|
|
|
fprintf(stderr, "%s.%d: add: ", __FILE__, __LINE__);
|
|
|
|
for (i = 0; i < conns->used + 1; i++) {
|
|
|
|
fprintf(stderr, "%d ", conns->ptr[i]->fd);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2006-10-04 13:26:23 +00:00
|
|
|
#endif
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
conns->ptr[conns->used]->ndx = conns->used;
|
|
|
|
return conns->ptr[conns->used++];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int connection_del(server *srv, connection *con) {
|
|
|
|
size_t i;
|
|
|
|
connections *conns = srv->conns;
|
|
|
|
connection *temp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con == NULL) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (-1 == con->ndx) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2010-04-28 19:08:11 +00:00
|
|
|
buffer_reset(con->uri.authority);
|
|
|
|
buffer_reset(con->uri.path);
|
|
|
|
buffer_reset(con->uri.query);
|
|
|
|
buffer_reset(con->request.orig_uri);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
i = con->ndx;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* not last element */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (i != conns->used - 1) {
|
|
|
|
temp = conns->ptr[i];
|
|
|
|
conns->ptr[i] = conns->ptr[conns->used - 1];
|
|
|
|
conns->ptr[conns->used - 1] = temp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
conns->ptr[i]->ndx = i;
|
|
|
|
conns->ptr[conns->used - 1]->ndx = -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
conns->used--;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->ndx = -1;
|
|
|
|
#if 0
|
|
|
|
fprintf(stderr, "%s.%d: del: (%d)", __FILE__, __LINE__, conns->used);
|
|
|
|
for (i = 0; i < conns->used; i++) {
|
|
|
|
fprintf(stderr, "%d ", conns->ptr[i]->fd);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
2006-10-04 13:26:23 +00:00
|
|
|
#endif
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int connection_close(server *srv, connection *con) {
|
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
server_socket *srv_sock = con->srv_socket;
|
|
|
|
#endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
if (srv_sock->is_ssl) {
|
|
|
|
if (con->ssl) SSL_free(con->ssl);
|
|
|
|
con->ssl = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fdevent_event_del(srv->ev, &(con->fde_ndx), con->fd);
|
|
|
|
fdevent_unregister(srv->ev, con->fd);
|
|
|
|
#ifdef __WIN32
|
|
|
|
if (closesocket(con->fd)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sds",
|
|
|
|
"(warning) close:", con->fd, strerror(errno));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (close(con->fd)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sds",
|
|
|
|
"(warning) close:", con->fd, strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
srv->cur_fds--;
|
|
|
|
#if 0
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd",
|
|
|
|
"closed()", con->fd);
|
|
|
|
#endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection_del(srv, con);
|
|
|
|
connection_set_state(srv, con, CON_STATE_CONNECT);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-01 10:53:52 +00:00
|
|
|
#if 0
|
2005-02-20 14:27:00 +00:00
|
|
|
static void dump_packet(const unsigned char *data, size_t len) {
|
|
|
|
size_t i, j;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (len == 0) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (i % 16 == 0) fprintf(stderr, " ");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fprintf(stderr, "%02x ", data[i]);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if ((i + 1) % 16 == 0) {
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
for (j = 0; j <= i % 16; j++) {
|
|
|
|
unsigned char c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (i-15+j >= len) break;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = data[i-15+j];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fprintf(stderr, "%c", c > 32 && c < 128 ? c : '.');
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (len % 16 != 0) {
|
|
|
|
for (j = i % 16; j < 16; j++) {
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fprintf(stderr, " ");
|
|
|
|
for (j = i & ~0xf; j < len; j++) {
|
|
|
|
unsigned char c;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
c = data[j];
|
|
|
|
fprintf(stderr, "%c", c > 32 && c < 128 ? c : '.');
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
#endif
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2006-09-20 14:03:55 +00:00
|
|
|
static int connection_handle_read_ssl(server *srv, connection *con) {
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef USE_OPENSSL
|
2010-02-01 23:28:50 +00:00
|
|
|
int r, ssl_err, len, count = 0, read_offset, toread;
|
2006-09-21 08:04:32 +00:00
|
|
|
buffer *b = NULL;
|
2006-09-20 14:03:55 +00:00
|
|
|
|
|
|
|
if (!con->conf.is_ssl) return -1;
|
|
|
|
|
2008-03-26 11:51:06 +00:00
|
|
|
ERR_clear_error();
|
2006-09-20 14:03:55 +00:00
|
|
|
do {
|
2010-02-01 23:28:50 +00:00
|
|
|
if (NULL != con->read_queue->last) {
|
|
|
|
b = con->read_queue->last->mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == b || b->size - b->used < 1024) {
|
|
|
|
b = chunkqueue_get_append_buffer(con->read_queue);
|
|
|
|
len = SSL_pending(con->ssl);
|
|
|
|
if (len < 4*1024) len = 4*1024; /* always alloc >= 4k buffer */
|
|
|
|
buffer_prepare_copy(b, len + 1);
|
2006-09-01 09:53:44 +00:00
|
|
|
|
2006-09-21 08:04:32 +00:00
|
|
|
/* overwrite everything with 0 */
|
|
|
|
memset(b->ptr, 0, b->size);
|
2006-09-01 09:53:44 +00:00
|
|
|
}
|
2006-09-20 14:03:55 +00:00
|
|
|
|
2010-02-01 23:28:50 +00:00
|
|
|
read_offset = (b->used > 0) ? b->used - 1 : 0;
|
|
|
|
toread = b->size - 1 - read_offset;
|
|
|
|
|
|
|
|
len = SSL_read(con->ssl, b->ptr + read_offset, toread);
|
2006-09-20 14:03:55 +00:00
|
|
|
|
|
|
|
if (len > 0) {
|
2010-02-01 23:28:50 +00:00
|
|
|
if (b->used > 0) b->used--;
|
|
|
|
b->used += len;
|
2006-09-20 14:03:55 +00:00
|
|
|
b->ptr[b->used++] = '\0';
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2010-02-01 23:28:50 +00:00
|
|
|
con->bytes_read += len;
|
2006-09-21 08:04:32 +00:00
|
|
|
|
2009-04-26 18:29:09 +00:00
|
|
|
count += len;
|
2006-09-20 14:03:55 +00:00
|
|
|
}
|
2010-02-01 23:28:50 +00:00
|
|
|
} while (len == toread && count < MAX_READ_LIMIT);
|
2006-09-20 14:03:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (len < 0) {
|
2009-06-03 16:44:37 +00:00
|
|
|
int oerrno = errno;
|
2006-09-20 14:03:55 +00:00
|
|
|
switch ((r = SSL_get_error(con->ssl, len))) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
con->is_readable = 0;
|
2006-09-21 08:04:32 +00:00
|
|
|
|
2010-02-01 23:28:50 +00:00
|
|
|
/* the manual says we have to call SSL_read with the same arguments next time.
|
|
|
|
* we ignore this restriction; no one has complained about it in 1.5 yet, so it probably works anyway.
|
|
|
|
*/
|
2006-09-21 08:04:32 +00:00
|
|
|
|
2006-09-20 14:03:55 +00:00
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
/**
|
|
|
|
* man SSL_get_error()
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2006-09-20 14:03:55 +00:00
|
|
|
* SSL_ERROR_SYSCALL
|
2006-10-04 13:26:23 +00:00
|
|
|
* Some I/O error occurred. The OpenSSL error queue may contain more
|
2006-09-20 14:03:55 +00:00
|
|
|
* information on the error. If the error queue is empty (i.e.
|
2006-10-04 13:26:23 +00:00
|
|
|
* ERR_get_error() returns 0), ret can be used to find out more about
|
2006-09-20 14:03:55 +00:00
|
|
|
* the error: If ret == 0, an EOF was observed that violates the
|
2006-10-04 13:26:23 +00:00
|
|
|
* protocol. If ret == -1, the underlying BIO reported an I/O error
|
2006-09-20 14:03:55 +00:00
|
|
|
* (for socket I/O on Unix systems, consult errno for details).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
while((ssl_err = ERR_get_error())) {
|
|
|
|
/* get all errors from the error-queue */
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sds", "SSL:",
|
2006-09-20 14:03:55 +00:00
|
|
|
r, ERR_error_string(ssl_err, NULL));
|
|
|
|
}
|
|
|
|
|
2009-06-03 16:44:37 +00:00
|
|
|
switch(oerrno) {
|
2006-09-20 14:03:55 +00:00
|
|
|
default:
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sddds", "SSL:",
|
2009-06-03 16:44:37 +00:00
|
|
|
len, r, oerrno,
|
|
|
|
strerror(oerrno));
|
2006-09-20 14:03:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-20 14:03:55 +00:00
|
|
|
break;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
/* clean shutdown on the remote side */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-20 14:03:55 +00:00
|
|
|
if (r == 0) {
|
|
|
|
/* FIXME: later */
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-20 14:03:55 +00:00
|
|
|
/* fall thourgh */
|
|
|
|
default:
|
|
|
|
while((ssl_err = ERR_get_error())) {
|
2008-08-19 17:40:42 +00:00
|
|
|
switch (ERR_GET_REASON(ssl_err)) {
|
|
|
|
case SSL_R_SSL_HANDSHAKE_FAILURE:
|
|
|
|
case SSL_R_TLSV1_ALERT_UNKNOWN_CA:
|
|
|
|
case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN:
|
|
|
|
case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE:
|
|
|
|
if (!con->conf.log_ssl_noise) continue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2006-09-20 14:03:55 +00:00
|
|
|
/* get all errors from the error-queue */
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sds", "SSL:",
|
2008-08-19 17:40:42 +00:00
|
|
|
r, ERR_error_string(ssl_err, NULL));
|
2006-09-20 14:03:55 +00:00
|
|
|
}
|
|
|
|
break;
|
2005-09-26 08:56:39 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-20 14:03:55 +00:00
|
|
|
connection_set_state(srv, con, CON_STATE_ERROR);
|
2006-09-21 08:04:32 +00:00
|
|
|
|
2006-09-20 14:03:55 +00:00
|
|
|
return -1;
|
|
|
|
} else if (len == 0) {
|
|
|
|
con->is_readable = 0;
|
|
|
|
/* the other end close the connection -> KEEP-ALIVE */
|
2005-09-26 08:56:39 +00:00
|
|
|
|
2006-09-20 14:03:55 +00:00
|
|
|
return -2;
|
2010-07-04 07:45:13 +00:00
|
|
|
} else {
|
|
|
|
joblist_append(srv, con);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-09-20 14:03:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
2008-04-29 21:01:00 +00:00
|
|
|
UNUSED(srv);
|
|
|
|
UNUSED(con);
|
2006-09-20 14:03:55 +00:00
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-07-04 07:45:13 +00:00
|
|
|
/* 0: everything ok, -1: error, -2: con closed */
|
2006-09-20 14:03:55 +00:00
|
|
|
static int connection_handle_read(server *srv, connection *con) {
|
|
|
|
int len;
|
|
|
|
buffer *b;
|
2010-02-01 23:28:50 +00:00
|
|
|
int toread, read_offset;
|
2006-09-20 14:03:55 +00:00
|
|
|
|
|
|
|
if (con->conf.is_ssl) {
|
|
|
|
return connection_handle_read_ssl(srv, con);
|
|
|
|
}
|
|
|
|
|
2010-02-01 23:28:50 +00:00
|
|
|
b = (NULL != con->read_queue->last) ? con->read_queue->last->mem : NULL;
|
|
|
|
|
|
|
|
/* default size for chunks is 4kb; only use bigger chunks if FIONREAD tells
|
|
|
|
* us more than 4kb is available
|
|
|
|
* if FIONREAD doesn't signal a big chunk we fill the previous buffer
|
|
|
|
* if it has >= 1kb free
|
|
|
|
*/
|
2006-09-20 14:03:55 +00:00
|
|
|
#if defined(__WIN32)
|
2010-02-01 23:28:50 +00:00
|
|
|
if (NULL == b || b->size - b->used < 1024) {
|
2008-09-30 14:28:12 +00:00
|
|
|
b = chunkqueue_get_append_buffer(con->read_queue);
|
|
|
|
buffer_prepare_copy(b, 4 * 1024);
|
2010-02-01 23:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
read_offset = (b->used == 0) ? 0 : b->used - 1;
|
|
|
|
len = recv(con->fd, b->ptr + read_offset, b->size - 1 - read_offset, 0);
|
|
|
|
#else
|
|
|
|
if (ioctl(con->fd, FIONREAD, &toread) || toread == 0 || toread <= 4*1024) {
|
|
|
|
if (NULL == b || b->size - b->used < 1024) {
|
|
|
|
b = chunkqueue_get_append_buffer(con->read_queue);
|
|
|
|
buffer_prepare_copy(b, 4 * 1024);
|
|
|
|
}
|
2008-09-30 14:28:12 +00:00
|
|
|
} else {
|
2009-04-26 18:29:09 +00:00
|
|
|
if (toread > MAX_READ_LIMIT) toread = MAX_READ_LIMIT;
|
2008-09-30 14:28:12 +00:00
|
|
|
b = chunkqueue_get_append_buffer(con->read_queue);
|
|
|
|
buffer_prepare_copy(b, toread + 1);
|
2005-09-26 08:56:39 +00:00
|
|
|
}
|
2010-02-01 23:28:50 +00:00
|
|
|
|
|
|
|
read_offset = (b->used == 0) ? 0 : b->used - 1;
|
|
|
|
len = read(con->fd, b->ptr + read_offset, b->size - 1 - read_offset);
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (len < 0) {
|
|
|
|
con->is_readable = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (errno == EAGAIN) return 0;
|
|
|
|
if (errno == EINTR) {
|
|
|
|
/* we have been interrupted before we could read */
|
|
|
|
con->is_readable = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (errno != ECONNRESET) {
|
|
|
|
/* expected for keep-alive */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssd", "connection closed - read failed: ", strerror(errno), errno);
|
|
|
|
}
|
2006-09-20 14:03:55 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection_set_state(srv, con, CON_STATE_ERROR);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
} else if (len == 0) {
|
|
|
|
con->is_readable = 0;
|
|
|
|
/* the other end close the connection -> KEEP-ALIVE */
|
2005-11-17 14:08:58 +00:00
|
|
|
|
|
|
|
/* pipelining */
|
|
|
|
|
|
|
|
return -2;
|
2005-02-20 14:27:00 +00:00
|
|
|
} else if ((size_t)len < b->size - 1) {
|
|
|
|
/* we got less then expected, wait for the next fd-event */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->is_readable = 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2010-02-01 23:28:50 +00:00
|
|
|
if (b->used > 0) b->used--;
|
|
|
|
b->used += len;
|
2005-02-20 14:27:00 +00:00
|
|
|
b->ptr[b->used++] = '\0';
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->bytes_read += len;
|
|
|
|
#if 0
|
|
|
|
dump_packet(b->ptr, len);
|
|
|
|
#endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int connection_handle_write_prepare(server *srv, connection *con) {
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->mode == DIRECT) {
|
|
|
|
/* static files */
|
|
|
|
switch(con->request.http_method) {
|
|
|
|
case HTTP_METHOD_GET:
|
|
|
|
case HTTP_METHOD_POST:
|
|
|
|
case HTTP_METHOD_HEAD:
|
|
|
|
case HTTP_METHOD_PUT:
|
2005-08-19 13:42:01 +00:00
|
|
|
case HTTP_METHOD_MKCOL:
|
|
|
|
case HTTP_METHOD_DELETE:
|
2005-08-20 19:10:44 +00:00
|
|
|
case HTTP_METHOD_COPY:
|
|
|
|
case HTTP_METHOD_MOVE:
|
|
|
|
case HTTP_METHOD_PROPFIND:
|
|
|
|
case HTTP_METHOD_PROPPATCH:
|
2006-09-28 07:25:38 +00:00
|
|
|
case HTTP_METHOD_LOCK:
|
|
|
|
case HTTP_METHOD_UNLOCK:
|
2005-08-15 09:55:23 +00:00
|
|
|
break;
|
2005-08-19 08:37:52 +00:00
|
|
|
case HTTP_METHOD_OPTIONS:
|
2006-01-03 13:59:46 +00:00
|
|
|
/*
|
|
|
|
* 400 is coming from the request-parser BEFORE uri.path is set
|
2006-10-04 13:26:23 +00:00
|
|
|
* 403 is from the response handler when noone else catched it
|
|
|
|
*
|
2006-01-03 13:59:46 +00:00
|
|
|
* */
|
2008-02-26 16:20:48 +00:00
|
|
|
if ((!con->http_status || con->http_status == 200) && con->uri.path->used &&
|
2006-01-03 13:59:46 +00:00
|
|
|
con->uri.path->ptr[0] != '*') {
|
2005-08-19 08:37:52 +00:00
|
|
|
response_header_insert(srv, con, CONST_STR_LEN("Allow"), CONST_STR_LEN("OPTIONS, GET, HEAD, POST"));
|
|
|
|
|
2008-02-27 12:15:38 +00:00
|
|
|
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
|
|
|
|
con->parsed_response &= ~HTTP_CONTENT_LENGTH;
|
|
|
|
|
2005-08-19 08:37:52 +00:00
|
|
|
con->http_status = 200;
|
|
|
|
con->file_finished = 1;
|
|
|
|
|
|
|
|
chunkqueue_reset(con->write_queue);
|
|
|
|
}
|
|
|
|
break;
|
2005-08-15 09:55:23 +00:00
|
|
|
default:
|
|
|
|
switch(con->http_status) {
|
|
|
|
case 400: /* bad request */
|
2006-01-08 18:41:08 +00:00
|
|
|
case 414: /* overload request header */
|
2005-08-15 09:55:23 +00:00
|
|
|
case 505: /* unknown protocol */
|
2005-08-19 00:05:52 +00:00
|
|
|
case 207: /* this was webdav */
|
2005-08-15 09:55:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
con->http_status = 501;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->http_status == 0) {
|
|
|
|
con->http_status = 403;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
switch(con->http_status) {
|
2008-04-23 19:10:42 +00:00
|
|
|
case 204: /* class: header only */
|
|
|
|
case 205:
|
|
|
|
case 304:
|
|
|
|
/* disable chunked encoding again as we have no body */
|
|
|
|
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
|
|
|
|
con->parsed_response &= ~HTTP_CONTENT_LENGTH;
|
|
|
|
chunkqueue_reset(con->write_queue);
|
|
|
|
|
|
|
|
con->file_finished = 1;
|
|
|
|
break;
|
|
|
|
default: /* class: header + body */
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->mode != DIRECT) break;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-04-23 19:10:42 +00:00
|
|
|
/* only custom body for 4xx and 5xx */
|
|
|
|
if (con->http_status < 400 || con->http_status >= 600) break;
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
con->file_finished = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_reset(con->physical.path);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
/* try to send static errorfile */
|
|
|
|
if (!buffer_is_empty(con->conf.errorfile_prefix)) {
|
|
|
|
stat_cache_entry *sce = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_copy_string_buffer(con->physical.path, con->conf.errorfile_prefix);
|
2008-04-23 19:10:42 +00:00
|
|
|
buffer_append_long(con->physical.path, con->http_status);
|
|
|
|
buffer_append_string_len(con->physical.path, CONST_STR_LEN(".html"));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (HANDLER_ERROR != stat_cache_get_entry(srv, con, con->physical.path, &sce)) {
|
|
|
|
con->file_finished = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
http_chunk_append_file(srv, con, con->physical.path, 0, sce->st.st_size);
|
2005-11-22 11:23:03 +00:00
|
|
|
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_BUF_LEN(sce->content_type));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
if (!con->file_finished) {
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer *b;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_reset(con->physical.path);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->file_finished = 1;
|
2005-08-15 09:55:23 +00:00
|
|
|
b = chunkqueue_get_append_buffer(con->write_queue);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
/* build default error-page */
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_copy_string_len(b, CONST_STR_LEN(
|
2005-08-15 09:55:23 +00:00
|
|
|
"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"
|
|
|
|
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
|
|
|
|
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
|
|
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"
|
|
|
|
" <head>\n"
|
2008-07-30 19:38:32 +00:00
|
|
|
" <title>"));
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_append_long(b, con->http_status);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(" - "));
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_append_string(b, get_http_status_name(con->http_status));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(
|
2005-08-15 09:55:23 +00:00
|
|
|
"</title>\n"
|
|
|
|
" </head>\n"
|
|
|
|
" <body>\n"
|
2008-07-30 19:38:32 +00:00
|
|
|
" <h1>"));
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_append_long(b, con->http_status);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(" - "));
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_append_string(b, get_http_status_name(con->http_status));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("</h1>\n"
|
2005-08-15 09:55:23 +00:00
|
|
|
" </body>\n"
|
|
|
|
"</html>\n"
|
2008-07-30 19:38:32 +00:00
|
|
|
));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/html"));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2005-08-15 09:55:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->file_finished) {
|
2006-10-04 13:26:23 +00:00
|
|
|
/* we have all the content and chunked encoding is not used, set a content-length */
|
|
|
|
|
|
|
|
if ((!(con->parsed_response & HTTP_CONTENT_LENGTH)) &&
|
2006-10-07 17:47:49 +00:00
|
|
|
(con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) == 0) {
|
|
|
|
off_t qlen = chunkqueue_length(con->write_queue);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-18 10:40:20 +00:00
|
|
|
/**
|
|
|
|
* The Content-Length header only can be sent if we have content:
|
|
|
|
* - HEAD doesn't have a content-body (but have a content-length)
|
|
|
|
* - 1xx, 204 and 304 don't have a content-body (RFC 2616 Section 4.3)
|
|
|
|
*
|
|
|
|
* Otherwise generate a Content-Length header as chunked encoding is not
|
|
|
|
* available
|
|
|
|
*/
|
|
|
|
if ((con->http_status >= 100 && con->http_status < 200) ||
|
|
|
|
con->http_status == 204 ||
|
|
|
|
con->http_status == 304) {
|
2008-02-27 12:15:38 +00:00
|
|
|
data_string *ds;
|
2007-08-18 10:40:20 +00:00
|
|
|
/* no Content-Body, no Content-Length */
|
2008-02-27 12:15:38 +00:00
|
|
|
if (NULL != (ds = (data_string*) array_get_element(con->response.headers, "Content-Length"))) {
|
2008-04-23 13:10:41 +00:00
|
|
|
buffer_reset(ds->value); /* Headers with empty values are ignored for output */
|
2008-02-27 12:15:38 +00:00
|
|
|
}
|
2008-02-27 18:35:42 +00:00
|
|
|
} else if (qlen > 0 || con->request.http_method != HTTP_METHOD_HEAD) {
|
2007-09-06 11:24:20 +00:00
|
|
|
/* qlen = 0 is important for Redirects (301, ...) as they MAY have
|
|
|
|
* a content. Browsers are waiting for a Content otherwise
|
|
|
|
*/
|
2008-02-27 18:35:42 +00:00
|
|
|
buffer_copy_off_t(srv->tmp_buf, qlen);
|
2006-10-07 17:47:49 +00:00
|
|
|
|
|
|
|
response_header_overwrite(srv, con, CONST_STR_LEN("Content-Length"), CONST_BUF_LEN(srv->tmp_buf));
|
|
|
|
}
|
2005-08-21 17:52:06 +00:00
|
|
|
}
|
2005-08-15 09:55:23 +00:00
|
|
|
} else {
|
2007-08-17 15:37:45 +00:00
|
|
|
/**
|
|
|
|
* the file isn't finished yet, but we have all headers
|
|
|
|
*
|
|
|
|
* to get keep-alive we either need:
|
|
|
|
* - Content-Length: ... (HTTP/1.0 and HTTP/1.0) or
|
|
|
|
* - Transfer-Encoding: chunked (HTTP/1.1)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (((con->parsed_response & HTTP_CONTENT_LENGTH) == 0) &&
|
2005-08-15 09:55:23 +00:00
|
|
|
((con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) == 0)) {
|
|
|
|
con->keep_alive = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 15:37:45 +00:00
|
|
|
/**
|
|
|
|
* if the backend sent a Connection: close, follow the wish
|
|
|
|
*
|
|
|
|
* NOTE: if the backend sent Connection: Keep-Alive, but no Content-Length, we
|
|
|
|
* will close the connection. That's fine. We can always decide the close
|
|
|
|
* the connection
|
|
|
|
*
|
|
|
|
* FIXME: to be nice we should remove the Connection: ...
|
|
|
|
*/
|
|
|
|
if (con->parsed_response & HTTP_CONNECTION) {
|
2005-02-20 14:27:00 +00:00
|
|
|
/* a subrequest disable keep-alive although the client wanted it */
|
|
|
|
if (con->keep_alive && !con->response.keep_alive) {
|
|
|
|
con->keep_alive = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->request.http_method == HTTP_METHOD_HEAD) {
|
2006-10-05 10:23:27 +00:00
|
|
|
/**
|
|
|
|
* a HEAD request has the same as a GET
|
|
|
|
* without the content
|
|
|
|
*/
|
2008-02-27 12:15:38 +00:00
|
|
|
con->file_finished = 1;
|
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
chunkqueue_reset(con->write_queue);
|
2006-10-05 10:23:27 +00:00
|
|
|
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
|
2005-08-15 09:55:23 +00:00
|
|
|
}
|
2005-08-19 08:37:52 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
http_response_write_header(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int connection_handle_write(server *srv, connection *con) {
|
|
|
|
switch(network_write_chunkqueue(srv, con, con->write_queue)) {
|
|
|
|
case 0:
|
|
|
|
if (con->file_finished) {
|
|
|
|
connection_set_state(srv, con, CON_STATE_RESPONSE_END);
|
|
|
|
joblist_append(srv, con);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case -1: /* error on our side */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd",
|
|
|
|
"connection closed: write failed on fd", con->fd);
|
|
|
|
connection_set_state(srv, con, CON_STATE_ERROR);
|
|
|
|
joblist_append(srv, con);
|
|
|
|
break;
|
|
|
|
case -2: /* remote close */
|
|
|
|
connection_set_state(srv, con, CON_STATE_ERROR);
|
|
|
|
joblist_append(srv, con);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
con->is_writable = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* not finished yet -> WRITE */
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
connection *connection_init(server *srv) {
|
|
|
|
connection *con;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
UNUSED(srv);
|
|
|
|
|
|
|
|
con = calloc(1, sizeof(*con));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->fd = 0;
|
|
|
|