2005-02-20 14:27:00 +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>
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
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
|
|
|
|
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
|
2006-09-20 14:03:55 +00:00
|
|
|
int r, ssl_err, len;
|
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;
|
|
|
|
|
|
|
|
/* don't resize the buffer if we were in SSL_ERROR_WANT_* */
|
|
|
|
|
|
|
|
do {
|
2006-09-21 08:04:32 +00:00
|
|
|
if (!con->ssl_error_want_reuse_buffer) {
|
|
|
|
b = buffer_init();
|
2006-09-01 09:53:44 +00:00
|
|
|
buffer_prepare_copy(b, SSL_pending(con->ssl) + (16 * 1024)); /* the pending bytes + 16kb */
|
|
|
|
|
2006-09-21 08:04:32 +00:00
|
|
|
/* overwrite everything with 0 */
|
|
|
|
memset(b->ptr, 0, b->size);
|
|
|
|
} else {
|
|
|
|
b = con->ssl_error_want_reuse_buffer;
|
2006-09-01 09:53:44 +00:00
|
|
|
}
|
2006-09-20 14:03:55 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
len = SSL_read(con->ssl, b->ptr, b->size - 1);
|
2006-09-21 08:04:32 +00:00
|
|
|
con->ssl_error_want_reuse_buffer = NULL; /* reuse it only once */
|
2006-09-20 14:03:55 +00:00
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
b->used = len;
|
|
|
|
b->ptr[b->used++] = '\0';
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-09-21 08:04:32 +00:00
|
|
|
/* we move the buffer to the chunk-queue, no need to free it */
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
chunkqueue_append_buffer_weak(con->read_queue, b);
|
2006-09-20 14:03:55 +00:00
|
|
|
con->bytes_read += len;
|
2006-09-21 08:04:32 +00:00
|
|
|
b = NULL;
|
2006-09-20 14:03:55 +00:00
|
|
|
}
|
|
|
|
} while (len > 0);
|
|
|
|
|
|
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
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
|
|
|
con->ssl_error_want_reuse_buffer = b;
|
|
|
|
|
|
|
|
b = NULL;
|
|
|
|
|
|
|
|
/* we have to steal the buffer from the queue-queue */
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(errno) {
|
|
|
|
default:
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sddds", "SSL:",
|
2006-09-20 14:03:55 +00:00
|
|
|
len, r, errno,
|
|
|
|
strerror(errno));
|
|
|
|
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())) {
|
|
|
|
/* 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));
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
buffer_free(b);
|
2006-10-04 13:26:23 +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
|
|
|
/* pipelining */
|
2006-09-21 08:04:32 +00:00
|
|
|
buffer_free(b);
|
2006-09-20 14:03:55 +00:00
|
|
|
|
|
|
|
return -2;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-09-20 14:03:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int connection_handle_read(server *srv, connection *con) {
|
|
|
|
int len;
|
|
|
|
buffer *b;
|
|
|
|
int toread;
|
|
|
|
|
|
|
|
if (con->conf.is_ssl) {
|
|
|
|
return connection_handle_read_ssl(srv, con);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__WIN32)
|
2006-09-01 09:53:44 +00:00
|
|
|
b = chunkqueue_get_append_buffer(con->read_queue);
|
|
|
|
buffer_prepare_copy(b, 4 * 1024);
|
2005-02-20 14:27:00 +00:00
|
|
|
len = recv(con->fd, b->ptr, b->size - 1, 0);
|
|
|
|
#else
|
2005-09-26 08:56:39 +00:00
|
|
|
if (ioctl(con->fd, FIONREAD, &toread)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd",
|
2005-09-26 08:56:39 +00:00
|
|
|
"unexpected end-of-file:",
|
|
|
|
con->fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-09-01 09:53:44 +00:00
|
|
|
b = chunkqueue_get_append_buffer(con->read_queue);
|
2006-10-04 07:00:35 +00:00
|
|
|
buffer_prepare_copy(b, toread + 1);
|
2005-09-26 08:56:39 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
len = read(con->fd, b->ptr, b->size - 1);
|
|
|
|
#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
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
b->used = len;
|
|
|
|
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
|
|
|
* */
|
|
|
|
if (con->uri.path->used &&
|
|
|
|
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"));
|
|
|
|
|
|
|
|
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) {
|
|
|
|
case 400: /* class: header + custom body */
|
|
|
|
case 401:
|
|
|
|
case 403:
|
|
|
|
case 404:
|
|
|
|
case 408:
|
2006-09-28 07:25:38 +00:00
|
|
|
case 409:
|
2005-08-15 09:55:23 +00:00
|
|
|
case 411:
|
|
|
|
case 416:
|
2006-03-06 00:47:29 +00:00
|
|
|
case 423:
|
2005-08-15 09:55:23 +00:00
|
|
|
case 500:
|
|
|
|
case 501:
|
|
|
|
case 503:
|
2006-10-04 13:26:23 +00:00
|
|
|
case 505:
|
2005-08-15 09:55:23 +00:00
|
|
|
if (con->mode != DIRECT) break;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
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);
|
|
|
|
buffer_append_string(con->physical.path, get_http_status_body_name(con->http_status));
|
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 */
|
2006-10-04 13:26:23 +00:00
|
|
|
buffer_copy_string(b,
|
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"
|
|
|
|
" <title>");
|
|
|
|
buffer_append_long(b, con->http_status);
|
|
|
|
buffer_append_string(b, " - ");
|
|
|
|
buffer_append_string(b, get_http_status_name(con->http_status));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
buffer_append_string(b,
|
|
|
|
"</title>\n"
|
|
|
|
" </head>\n"
|
|
|
|
" <body>\n"
|
|
|
|
" <h1>");
|
|
|
|
buffer_append_long(b, con->http_status);
|
|
|
|
buffer_append_string(b, " - ");
|
|
|
|
buffer_append_string(b, get_http_status_name(con->http_status));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
buffer_append_string(b,"</h1>\n"
|
2005-08-15 09:55:23 +00:00
|
|
|
" </body>\n"
|
|
|
|
"</html>\n"
|
|
|
|
);
|
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
|
|
|
/* fall through */
|
2005-08-19 00:05:52 +00:00
|
|
|
case 207:
|
2005-08-15 09:55:23 +00:00
|
|
|
case 200: /* class: header + body */
|
2006-03-02 23:31:40 +00:00
|
|
|
case 201:
|
2006-02-08 12:31:09 +00:00
|
|
|
case 301:
|
2005-09-21 10:45:26 +00:00
|
|
|
case 302:
|
2007-02-20 17:25:12 +00:00
|
|
|
case 303:
|
2005-08-19 08:37:52 +00:00
|
|
|
break;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
case 206: /* write_queue is already prepared */
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
2005-08-15 09:55:23 +00:00
|
|
|
case 205: /* class: header only */
|
|
|
|
case 304:
|
2005-02-20 14:27:00 +00:00
|
|
|
default:
|
2005-08-15 09:55:23 +00:00
|
|
|
/* disable chunked encoding again as we have no body */
|
|
|
|
con->response.transfer_encoding &= ~HTTP_TRANSFER_ENCODING_CHUNKED;
|
|
|
|
chunkqueue_reset(con->write_queue);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-15 09:55:23 +00:00
|
|
|
con->file_finished = 1;
|
|
|
|
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
|
|
|
|
2006-10-07 17:47:49 +00:00
|
|
|
/* if we have no content for a GET/PORT request, send Content-Length: 0
|
|
|
|
* if it is a HEAD request, don't generate a Content-Length as
|
|
|
|
* the backend might have already cut it off */
|
|
|
|
if (qlen > 0 || con->request.http_method != HTTP_METHOD_HEAD) {
|
|
|
|
buffer_copy_off_t(srv->tmp_buf, chunkqueue_length(con->write_queue));
|
|
|
|
|
|
|
|
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 {
|
|
|
|
/* disable keep-alive if size-info for the body is missing */
|
|
|
|
if ((con->parsed_response & HTTP_CONTENT_LENGTH) &&
|
|
|
|
((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
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (0 == (con->parsed_response & HTTP_CONNECTION)) {
|
2005-08-15 09:55:23 +00:00
|
|
|
/* (f)cgi did'nt send Connection: header
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* shall we ?
|
|
|
|
*/
|
|
|
|
if (((con->response.transfer_encoding & HTTP_TRANSFER_ENCODING_CHUNKED) == 0) &&
|
|
|
|
(con->parsed_response & HTTP_CONTENT_LENGTH) == 0) {
|
|
|
|
/* without content_length, no keep-alive */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->keep_alive = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* 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-02-20 14:27:00 +00:00
|
|
|
/* FIXME: we have to drop the Connection: Header from the subrequest */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
*/
|
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;
|
|
|
|
con->ndx = -1;
|
|
|
|
con->fde_ndx = -1;
|
|
|
|
con->bytes_written = 0;
|
|
|
|
con->bytes_read = 0;
|
|
|
|
con->bytes_header = 0;
|
2005-07-23 20:42:34 +00:00
|
|
|
con->loops_per_request = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#define CLEAN(x) \
|
|
|
|
con->x = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(request.uri);
|
|
|
|
CLEAN(request.request_line);
|
|
|
|
CLEAN(request.request);
|
|
|
|
CLEAN(request.pathinfo);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(request.orig_uri);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(uri.scheme);
|
|
|
|
CLEAN(uri.authority);
|
|
|
|
CLEAN(uri.path);
|
|
|
|
CLEAN(uri.path_raw);
|
|
|
|
CLEAN(uri.query);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(physical.doc_root);
|
|
|
|
CLEAN(physical.path);
|
2005-08-08 09:42:27 +00:00
|
|
|
CLEAN(physical.basedir);
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(physical.rel_path);
|
|
|
|
CLEAN(physical.etag);
|
|
|
|
CLEAN(parse_request);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CLEAN(authed_user);
|
|
|
|
CLEAN(server_name);
|
|
|
|
CLEAN(error_handler);
|
2005-08-25 20:09:46 +00:00
|
|
|
CLEAN(dst_addr_buf);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#undef CLEAN
|
|
|
|
con->write_queue = chunkqueue_init();
|
|
|
|
con->read_queue = chunkqueue_init();
|
2005-09-26 08:56:39 +00:00
|
|
|
con->request_content_queue = chunkqueue_init();
|
2005-11-01 07:50:08 +00:00
|
|
|
chunkqueue_set_tempdirs(con->request_content_queue, srv->srvconf.upload_tempdirs);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->request.headers = array_init();
|
|
|
|
con->response.headers = array_init();
|
|
|
|
con->environment = array_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* init plugin specific connection structures */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-17 14:08:58 +00:00
|
|
|
con->plugin_ctx = calloc(1, (srv->plugins.used + 1) * sizeof(void *));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-08-09 06:42:33 +00:00
|
|
|
con->cond_cache = calloc(srv->config_context->used, sizeof(cond_cache_t));
|
2005-02-20 14:27:00 +00:00
|
|
|
config_setup_connection(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return con;
|
|
|
|
}
|
|
|
|
|
|
|
|
void connections_free(server *srv) {
|
|
|
|
connections *conns = srv->conns;
|
2006-10-04 13:26:23 +00:00
|
|
|
size_t i;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < conns->size; i++) {
|
|
|
|
connection *con = conns->ptr[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection_reset(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
chunkqueue_free(con->write_queue);
|
|
|
|
chunkqueue_free(con->read_queue);
|
2005-09-26 08:56:39 +00:00
|
|
|
chunkqueue_free(con->request_content_queue);
|
2005-02-20 14:27:00 +00:00
|
|
|
array_free(con->request.headers);
|
|
|
|
array_free(con->response.headers);
|
|
|
|
array_free(con->environment);
|
|
|
|
|
|
|
|
#define CLEAN(x) \
|
|
|
|
buffer_free(con->x);
|
2006-10-04 13:26:23 +00:00
|
|