2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "server.h"
|
|
|
|
#include "keyvalue.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include "http_chunk.h"
|
|
|
|
#include "fdevent.h"
|
|
|
|
#include "connections.h"
|
|
|
|
#include "response.h"
|
|
|
|
#include "joblist.h"
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
#include "inet_ntop_cache.h"
|
2005-05-08 06:25:17 +00:00
|
|
|
#include "crc32.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "sys-socket.h"
|
|
|
|
|
|
|
|
#define data_proxy data_fastcgi
|
|
|
|
#define data_proxy_init data_fastcgi_init
|
|
|
|
|
|
|
|
#define PROXY_RETRY_TIMEOUT 60
|
|
|
|
|
|
|
|
/**
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* the proxy module is based on the fastcgi module
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* 28.06.2004 Jan Kneschke The first release
|
|
|
|
* 01.07.2004 Evgeny Rodichev Several bugfixes and cleanups
|
|
|
|
* - co-ordinate up- and downstream flows correctly (proxy_demux_response
|
|
|
|
* and proxy_handle_fdevent)
|
|
|
|
* - correctly transfer upstream http_response_status;
|
|
|
|
* - some unused structures removed.
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* TODO: - delay upstream read if write_queue is too large
|
|
|
|
* (to prevent memory eating, like in apache). Shoud be
|
|
|
|
* configurable).
|
|
|
|
* - persistent connection with upstream servers
|
|
|
|
* - HTTP/1.1
|
|
|
|
*/
|
2005-05-08 06:25:17 +00:00
|
|
|
typedef enum {
|
|
|
|
PROXY_BALANCE_UNSET,
|
|
|
|
PROXY_BALANCE_FAIR,
|
|
|
|
PROXY_BALANCE_HASH,
|
|
|
|
PROXY_BALANCE_RR
|
|
|
|
} proxy_balance_t;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
array *extensions;
|
2007-08-17 22:17:00 +00:00
|
|
|
unsigned short debug;
|
2005-05-31 08:23:33 +00:00
|
|
|
|
|
|
|
proxy_balance_t balance;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer *parse_response;
|
2005-05-08 06:25:17 +00:00
|
|
|
buffer *balance_buf;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config **config_storage;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config conf;
|
|
|
|
} plugin_data;
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
typedef enum {
|
|
|
|
PROXY_STATE_INIT,
|
|
|
|
PROXY_STATE_CONNECT,
|
|
|
|
PROXY_STATE_PREPARE_WRITE,
|
|
|
|
PROXY_STATE_WRITE,
|
2016-04-07 00:46:43 +00:00
|
|
|
PROXY_STATE_READ
|
2005-05-08 06:25:17 +00:00
|
|
|
} proxy_connection_state_t;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
enum { PROXY_STDOUT, PROXY_END_REQUEST };
|
2005-05-08 06:25:17 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
typedef struct {
|
|
|
|
proxy_connection_state_t state;
|
|
|
|
time_t state_timestamp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
data_proxy *host;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer *response;
|
|
|
|
buffer *response_header;
|
|
|
|
|
2005-09-26 08:56:39 +00:00
|
|
|
chunkqueue *wb;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
int fd; /* fd to the proxy process */
|
|
|
|
int fde_ndx; /* index into the fd-event buffer */
|
|
|
|
|
|
|
|
size_t path_info_offset; /* start of path_info in uri.path */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection *remote_conn; /* dump pointer */
|
|
|
|
plugin_data *plugin_data; /* dump pointer */
|
|
|
|
} handler_ctx;
|
|
|
|
|
|
|
|
|
|
|
|
/* ok, we need a prototype */
|
2010-08-06 21:57:15 +00:00
|
|
|
static handler_t proxy_handle_fdevent(server *srv, void *ctx, int revents);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static handler_ctx * handler_ctx_init(void) {
|
2005-02-20 14:27:00 +00:00
|
|
|
handler_ctx * hctx;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
hctx = calloc(1, sizeof(*hctx));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
hctx->state = PROXY_STATE_INIT;
|
|
|
|
hctx->host = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
hctx->response = buffer_init();
|
|
|
|
hctx->response_header = buffer_init();
|
|
|
|
|
2005-09-26 08:56:39 +00:00
|
|
|
hctx->wb = chunkqueue_init();
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
hctx->fd = -1;
|
|
|
|
hctx->fde_ndx = -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return hctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handler_ctx_free(handler_ctx *hctx) {
|
|
|
|
buffer_free(hctx->response);
|
|
|
|
buffer_free(hctx->response_header);
|
2005-09-26 08:56:39 +00:00
|
|
|
chunkqueue_free(hctx->wb);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(hctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT_FUNC(mod_proxy_init) {
|
|
|
|
plugin_data *p;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p = calloc(1, sizeof(*p));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->parse_response = buffer_init();
|
2005-05-08 06:25:17 +00:00
|
|
|
p->balance_buf = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FREE_FUNC(mod_proxy_free) {
|
|
|
|
plugin_data *p = p_d;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
UNUSED(srv);
|
|
|
|
|
|
|
|
buffer_free(p->parse_response);
|
2005-05-08 06:25:17 +00:00
|
|
|
buffer_free(p->balance_buf);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (p->config_storage) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
|
|
|
plugin_config *s = p->config_storage[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-05-14 09:38:33 +00:00
|
|
|
if (NULL == s) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-05-14 09:38:33 +00:00
|
|
|
array_free(s->extensions);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-05-14 09:38:33 +00:00
|
|
|
free(s);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
free(p->config_storage);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
SETDEFAULTS_FUNC(mod_proxy_set_defaults) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
data_unset *du;
|
|
|
|
size_t i = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
config_values_t cv[] = {
|
2005-02-20 14:27:00 +00:00
|
|
|
{ "proxy.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
|
|
|
{ "proxy.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
|
2005-05-31 08:23:33 +00:00
|
|
|
{ "proxy.balance", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
|
2005-02-20 14:27:00 +00:00
|
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2013-11-13 11:43:26 +00:00
|
|
|
p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < srv->config_context->used; i++) {
|
2015-11-07 12:51:11 +00:00
|
|
|
data_config const* config = (data_config const*)srv->config_context->data[i];
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config *s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
s = malloc(sizeof(plugin_config));
|
|
|
|
s->extensions = array_init();
|
|
|
|
s->debug = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
cv[0].destination = s->extensions;
|
|
|
|
cv[1].destination = &(s->debug);
|
2005-05-31 08:23:33 +00:00
|
|
|
cv[2].destination = p->balance_buf;
|
|
|
|
|
|
|
|
buffer_reset(p->balance_buf);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->config_storage[i] = s;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-11-07 12:51:11 +00:00
|
|
|
if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (buffer_string_is_empty(p->balance_buf)) {
|
2005-05-31 08:23:33 +00:00
|
|
|
s->balance = PROXY_BALANCE_FAIR;
|
|
|
|
} else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("fair"))) {
|
|
|
|
s->balance = PROXY_BALANCE_FAIR;
|
|
|
|
} else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("round-robin"))) {
|
|
|
|
s->balance = PROXY_BALANCE_RR;
|
|
|
|
} else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("hash"))) {
|
|
|
|
s->balance = PROXY_BALANCE_HASH;
|
|
|
|
} else {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb",
|
2005-05-31 08:23:33 +00:00
|
|
|
"proxy.balance has to be one of: fair, round-robin, hash, but not:", p->balance_buf);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:51:11 +00:00
|
|
|
if (NULL != (du = array_get_element(config->value, "proxy.server"))) {
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t j;
|
|
|
|
data_array *da = (data_array *)du;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (du->type != TYPE_ARRAY) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sss",
|
2016-04-19 19:34:07 +00:00
|
|
|
"unexpected type for key: ", "proxy.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/*
|
2005-02-20 14:27:00 +00:00
|
|
|
* proxy.server = ( "<ext>" => ...,
|
|
|
|
* "<ext>" => ... )
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (j = 0; j < da->value->used; j++) {
|
|
|
|
data_array *da_ext = (data_array *)da->value->data[j];
|
|
|
|
size_t n;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (da_ext->type != TYPE_ARRAY) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sssbs",
|
|
|
|
"unexpected type for key: ", "proxy.server",
|
2016-04-19 19:34:07 +00:00
|
|
|
"[", da->value->data[j]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* proxy.server = ( "<ext>" =>
|
|
|
|
* ( "<host>" => ( ... ),
|
2005-02-20 14:27:00 +00:00
|
|
|
* "<host>" => ( ... )
|
2006-10-04 13:26:23 +00:00
|
|
|
* ),
|
2005-02-20 14:27:00 +00:00
|
|
|
* "<ext>" => ... )
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (n = 0; n < da_ext->value->used; n++) {
|
|
|
|
data_array *da_host = (data_array *)da_ext->value->data[n];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
data_proxy *df;
|
|
|
|
data_array *dfa;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
config_values_t pcv[] = {
|
2005-02-20 14:27:00 +00:00
|
|
|
{ "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
|
|
|
{ "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
|
|
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (da_host->type != TYPE_ARRAY) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssSBS",
|
|
|
|
"unexpected type for key:",
|
|
|
|
"proxy.server",
|
2016-04-19 19:34:07 +00:00
|
|
|
"[", da_ext->value->data[n]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
df = data_proxy_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-05 09:05:10 +00:00
|
|
|
df->port = 80;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(df->key, da_host->key);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
pcv[0].destination = df->host;
|
|
|
|
pcv[1].destination = &(df->port);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-11-07 12:51:11 +00:00
|
|
|
if (0 != config_insert_values_internal(srv, da_host->value, pcv, T_CONFIG_SCOPE_CONNECTION)) {
|
2014-02-14 21:06:19 +00:00
|
|
|
df->free((data_unset*) df);
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (buffer_string_is_empty(df->host)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbbbs",
|
|
|
|
"missing key (string):",
|
2005-02-20 14:27:00 +00:00
|
|
|
da->key,
|
|
|
|
da_ext->key,
|
|
|
|
da_host->key,
|
|
|
|
"host");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2014-02-14 21:06:19 +00:00
|
|
|
df->free((data_unset*) df);
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* if extension already exists, take it */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (NULL == (dfa = (data_array *)array_get_element(s->extensions, da_ext->key->ptr))) {
|
|
|
|
dfa = data_array_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(dfa->key, da_ext->key);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
array_insert_unique(dfa->value, (data_unset *)df);
|
|
|
|
array_insert_unique(s->extensions, (data_unset *)dfa);
|
|
|
|
} else {
|
|
|
|
array_insert_unique(dfa->value, (data_unset *)df);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void proxy_connection_close(server *srv, handler_ctx *hctx) {
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_data *p;
|
2005-08-08 08:22:06 +00:00
|
|
|
connection *con;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p = hctx->plugin_data;
|
|
|
|
con = hctx->remote_conn;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (hctx->fd != -1) {
|
2005-08-08 08:22:06 +00:00
|
|
|
fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
|
|
|
|
fdevent_unregister(srv->ev, hctx->fd);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
close(hctx->fd);
|
|
|
|
srv->cur_fds--;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2010-04-07 15:34:51 +00:00
|
|
|
if (hctx->host) {
|
|
|
|
hctx->host->usage--;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
handler_ctx_free(hctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
con->plugin_ctx[p->id] = NULL;
|
2016-04-07 00:46:43 +00:00
|
|
|
|
|
|
|
/* finish response (if not already finished) */
|
2016-04-07 23:47:49 +00:00
|
|
|
if (con->mode == p->id
|
|
|
|
&& (con->state == CON_STATE_HANDLE_REQUEST || con->state == CON_STATE_READ_POST)) {
|
2016-04-07 00:46:43 +00:00
|
|
|
/* (not CON_STATE_ERROR and not CON_STATE_RESPONSE_END,
|
|
|
|
* i.e. not called from proxy_connection_reset()) */
|
|
|
|
|
|
|
|
/* Send an error if we haven't sent any data yet */
|
|
|
|
if (0 == con->file_started) {
|
|
|
|
con->http_status = 500;
|
|
|
|
con->mode = DIRECT;
|
|
|
|
}
|
|
|
|
else if (!con->file_finished) {
|
|
|
|
http_chunk_close(srv, con);
|
|
|
|
con->file_finished = 1;
|
|
|
|
}
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int proxy_establish_connection(server *srv, handler_ctx *hctx) {
|
|
|
|
struct sockaddr *proxy_addr;
|
|
|
|
struct sockaddr_in proxy_addr_in;
|
2015-07-11 11:20:18 +00:00
|
|
|
#if defined(HAVE_SYS_UN_H)
|
|
|
|
struct sockaddr_un proxy_addr_un;
|
|
|
|
#endif
|
2009-04-26 21:02:16 +00:00
|
|
|
#if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
|
|
|
|
struct sockaddr_in6 proxy_addr_in6;
|
|
|
|
#endif
|
2005-02-20 14:27:00 +00:00
|
|
|
socklen_t servlen;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_data *p = hctx->plugin_data;
|
|
|
|
data_proxy *host= hctx->host;
|
|
|
|
int proxy_fd = hctx->fd;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
2015-07-11 11:20:18 +00:00
|
|
|
#if defined(HAVE_SYS_UN_H)
|
|
|
|
if (strstr(host->host->ptr, "/")) {
|
2015-08-30 10:16:28 +00:00
|
|
|
if (buffer_string_length(host->host) + 1 > sizeof(proxy_addr_un.sun_path)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sB",
|
|
|
|
"ERROR: Unix Domain socket filename too long:",
|
|
|
|
host->host);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-11 11:20:18 +00:00
|
|
|
memset(&proxy_addr_un, 0, sizeof(proxy_addr_un));
|
|
|
|
proxy_addr_un.sun_family = AF_UNIX;
|
|
|
|
strcpy(proxy_addr_un.sun_path, host->host->ptr);
|
|
|
|
servlen = sizeof(proxy_addr_un);
|
|
|
|
proxy_addr = (struct sockaddr *) &proxy_addr_un;
|
|
|
|
} else
|
|
|
|
#endif
|
2009-04-26 21:02:16 +00:00
|
|
|
#if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
|
|
|
|
if (strstr(host->host->ptr, ":")) {
|
|
|
|
memset(&proxy_addr_in6, 0, sizeof(proxy_addr_in6));
|
|
|
|
proxy_addr_in6.sin6_family = AF_INET6;
|
|
|
|
inet_pton(AF_INET6, host->host->ptr, (char *) &proxy_addr_in6.sin6_addr);
|
|
|
|
proxy_addr_in6.sin6_port = htons(host->port);
|
|
|
|
servlen = sizeof(proxy_addr_in6);
|
|
|
|
proxy_addr = (struct sockaddr *) &proxy_addr_in6;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
memset(&proxy_addr_in, 0, sizeof(proxy_addr_in));
|
|
|
|
proxy_addr_in.sin_family = AF_INET;
|
|
|
|
proxy_addr_in.sin_addr.s_addr = inet_addr(host->host->ptr);
|
|
|
|
proxy_addr_in.sin_port = htons(host->port);
|
|
|
|
servlen = sizeof(proxy_addr_in);
|
|
|
|
proxy_addr = (struct sockaddr *) &proxy_addr_in;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (-1 == connect(proxy_fd, proxy_addr, servlen)) {
|
|
|
|
if (errno == EINPROGRESS || errno == EALREADY) {
|
|
|
|
if (p->conf.debug) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd",
|
2005-02-20 14:27:00 +00:00
|
|
|
"connect delayed:", proxy_fd);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-03-05 16:30:29 +00:00
|
|
|
return 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
} else {
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sdsd",
|
2005-02-20 14:27:00 +00:00
|
|
|
"connect failed:", proxy_fd, strerror(errno), errno);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p->conf.debug) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd",
|
2005-02-20 14:27:00 +00:00
|
|
|
"connect succeeded: ", proxy_fd);
|
|
|
|
}
|
2005-05-31 08:23:33 +00:00
|
|
|
|
2005-03-05 16:30:29 +00:00
|
|
|
return 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void proxy_set_header(connection *con, const char *key, const char *value) {
|
2015-07-11 11:20:18 +00:00
|
|
|
data_string *ds_dst;
|
2005-11-18 12:38:18 +00:00
|
|
|
|
2015-07-11 11:20:18 +00:00
|
|
|
if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
|
|
|
|
ds_dst = data_string_init();
|
|
|
|
}
|
2005-11-18 12:38:18 +00:00
|
|
|
|
2015-07-11 11:20:18 +00:00
|
|
|
buffer_copy_string(ds_dst->key, key);
|
|
|
|
buffer_copy_string(ds_dst->value, value);
|
|
|
|
array_insert_unique(con->request.headers, (data_unset *)ds_dst);
|
2005-11-18 12:38:18 +00:00
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void proxy_append_header(connection *con, const char *key, const char *value) {
|
2015-07-11 11:20:18 +00:00
|
|
|
data_string *ds_dst;
|
2005-11-18 12:38:18 +00:00
|
|
|
|
2015-07-11 11:20:18 +00:00
|
|
|
if (NULL == (ds_dst = (data_string *)array_get_unused_element(con->request.headers, TYPE_STRING))) {
|
|
|
|
ds_dst = data_string_init();
|
|
|
|
}
|
2005-11-18 12:38:18 +00:00
|
|
|
|
2015-07-11 11:20:18 +00:00
|
|
|
buffer_copy_string(ds_dst->key, key);
|
|
|
|
buffer_append_string(ds_dst->value, value);
|
|
|
|
array_insert_unique(con->request.headers, (data_unset *)ds_dst);
|
2005-11-18 12:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
static int proxy_create_env(server *srv, handler_ctx *hctx) {
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection *con = hctx->remote_conn;
|
2005-09-26 08:56:39 +00:00
|
|
|
buffer *b;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* build header */
|
2005-09-26 08:56:39 +00:00
|
|
|
|
2015-02-08 19:10:36 +00:00
|
|
|
b = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* request line */
|
2005-09-26 08:56:39 +00:00
|
|
|
buffer_copy_string(b, get_http_method_name(con->request.http_method));
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(" "));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-26 08:56:39 +00:00
|
|
|
buffer_append_string_buffer(b, con->request.uri);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(" HTTP/1.0\r\n"));
|
2005-09-26 09:16:35 +00:00
|
|
|
|
2005-11-18 12:38:18 +00:00
|
|
|
proxy_append_header(con, "X-Forwarded-For", (char *)inet_ntop_cache_get_ip(srv, &(con->dst_addr)));
|
2006-10-04 13:26:23 +00:00
|
|
|
/* http_host is NOT is just a pointer to a buffer
|
2006-01-03 12:51:46 +00:00
|
|
|
* which is NULL if it is not set */
|
2015-02-08 12:37:10 +00:00
|
|
|
if (!buffer_string_is_empty(con->request.http_host)) {
|
2005-12-05 13:30:11 +00:00
|
|
|
proxy_set_header(con, "X-Host", con->request.http_host->ptr);
|
|
|
|
}
|
2013-07-31 20:23:21 +00:00
|
|
|
proxy_set_header(con, "X-Forwarded-Proto", con->uri.scheme->ptr);
|
2005-11-18 12:38:18 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* request header */
|
|
|
|
for (i = 0; i < con->request.headers->used; i++) {
|
|
|
|
data_string *ds;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
ds = (data_string *)con->request.headers->data[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
if (!buffer_is_empty(ds->value) && !buffer_is_empty(ds->key)) {
|
2016-03-04 19:23:16 +00:00
|
|
|
if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Connection"))) continue;
|
|
|
|
if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Proxy-Connection"))) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-26 08:56:39 +00:00
|
|
|
buffer_append_string_buffer(b, ds->key);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(": "));
|
2005-09-26 08:56:39 +00:00
|
|
|
buffer_append_string_buffer(b, ds->value);
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("\r\n"));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-03-04 19:23:16 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("Connection: close\r\n\r\n"));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:36 +00:00
|
|
|
chunkqueue_append_buffer(hctx->wb, b);
|
|
|
|
buffer_free(b);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* body */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-02 17:34:06 +00:00
|
|
|
if (con->request.content_length) {
|
2005-09-26 08:56:39 +00:00
|
|
|
chunkqueue *req_cq = con->request_content_queue;
|
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
chunkqueue_steal(hctx->wb, req_cq, req_cq->bytes_in);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int proxy_set_state(server *srv, handler_ctx *hctx, proxy_connection_state_t state) {
|
|
|
|
hctx->state = state;
|
|
|
|
hctx->state_timestamp = srv->cur_ts;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int proxy_response_parse(server *srv, connection *con, plugin_data *p, buffer *in) {
|
|
|
|
char *s, *ns;
|
|
|
|
int http_response_status = -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
UNUSED(srv);
|
|
|
|
|
2016-03-26 11:32:13 +00:00
|
|
|
/* [\r]\n -> [\0]\0 */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(p->parse_response, in);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-03-26 11:32:13 +00:00
|
|
|
for (s = p->parse_response->ptr; NULL != (ns = strchr(s, '\n')); s = ns + 1) {
|
2005-02-20 14:27:00 +00:00
|
|
|
char *key, *value;
|
|
|
|
int key_len;
|
|
|
|
data_string *ds;
|
2005-11-18 12:38:18 +00:00
|
|
|
int copy_header;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
ns[0] = '\0';
|
2016-03-26 11:32:13 +00:00
|
|
|
if (s != ns && ns[-1] == '\r') ns[-1] = '\0';
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
if (-1 == http_response_status) {
|
|
|
|
/* The first line of a Response message is the Status-Line */
|
|
|
|
|
|
|
|
for (key=s; *key && *key != ' '; key++);
|
|
|
|
|
|
|
|
if (*key) {
|
|
|
|
http_response_status = (int) strtol(key, NULL, 10);
|
2016-03-26 10:58:49 +00:00
|
|
|
if (http_response_status < 100 || http_response_status >= 1000) http_response_status = 502;
|
2005-02-20 14:27:00 +00:00
|
|
|
} else {
|
|
|
|
http_response_status = 502;
|
|
|
|
}
|
|
|
|
|
|
|
|
con->http_status = http_response_status;
|
|
|
|
con->parsed_response |= HTTP_STATUS;
|
|
|
|
continue;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (NULL == (value = strchr(s, ':'))) {
|
|
|
|
/* now we expect: "<key>: <value>\n" */
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
key = s;
|
|
|
|
key_len = value - key;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
value++;
|
|
|
|
/* strip WS */
|
|
|
|
while (*value == ' ' || *value == '\t') value++;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-18 12:38:18 +00:00
|
|
|
copy_header = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
switch(key_len) {
|
|
|
|
case 4:
|
|
|
|
if (0 == strncasecmp(key, "Date", key_len)) {
|
|
|
|
con->parsed_response |= HTTP_DATE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
if (0 == strncasecmp(key, "Location", key_len)) {
|
|
|
|
con->parsed_response |= HTTP_LOCATION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
if (0 == strncasecmp(key, "Connection", key_len)) {
|
2005-11-18 12:38:18 +00:00
|
|
|
copy_header = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
if (0 == strncasecmp(key, "Content-Length", key_len)) {
|
2016-03-26 10:58:49 +00:00
|
|
|
con->response.content_length = strtoul(value, NULL, 10);
|
2005-02-20 14:27:00 +00:00
|
|
|
con->parsed_response |= HTTP_CONTENT_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2005-11-18 12:38:18 +00:00
|
|
|
|
|
|
|
if (copy_header) {
|
|
|
|
if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
|
|
|
|
ds = data_response_init();
|
|
|
|
}
|
|
|
|
buffer_copy_string_len(ds->key, key, key_len);
|
|
|
|
buffer_copy_string(ds->value, value);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-11-18 12:38:18 +00:00
|
|
|
array_insert_unique(con->response.headers, (data_unset *)ds);
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int proxy_demux_response(server *srv, handler_ctx *hctx) {
|
|
|
|
int fin = 0;
|
|
|
|
int b;
|
|
|
|
ssize_t r;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_data *p = hctx->plugin_data;
|
|
|
|
connection *con = hctx->remote_conn;
|
|
|
|
int proxy_fd = hctx->fd;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* check how much we have to read */
|
|
|
|
if (ioctl(hctx->fd, FIONREAD, &b)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd",
|
|