2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "server.h"
|
|
|
|
#include "stat_cache.h"
|
|
|
|
#include "keyvalue.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "connections.h"
|
|
|
|
#include "joblist.h"
|
2016-04-22 03:57:14 +00:00
|
|
|
#include "response.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
#include "http_chunk.h"
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <sys/types.h>
|
2016-02-10 19:33:57 +00:00
|
|
|
#include "sys-mmap.h"
|
2017-03-24 01:37:17 +00:00
|
|
|
#include "sys-socket.h"
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <sys/wait.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fdevent.h>
|
|
|
|
|
2005-12-20 12:47:39 +00:00
|
|
|
#include <fcntl.h>
|
2017-07-31 02:28:13 +00:00
|
|
|
#include <signal.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2016-12-23 05:31:23 +00:00
|
|
|
static int pipe_cloexec(int pipefd[2]) {
|
|
|
|
#ifdef HAVE_PIPE2
|
|
|
|
if (0 == pipe2(pipefd, O_CLOEXEC)) return 0;
|
|
|
|
#endif
|
|
|
|
return 0 == pipe(pipefd)
|
|
|
|
#ifdef FD_CLOEXEC
|
|
|
|
&& 0 == fcntl(pipefd[0], F_SETFD, FD_CLOEXEC)
|
|
|
|
&& 0 == fcntl(pipefd[1], F_SETFD, FD_CLOEXEC)
|
|
|
|
#endif
|
|
|
|
? 0
|
|
|
|
: -1;
|
|
|
|
}
|
2013-12-24 04:28:44 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
typedef struct {
|
|
|
|
char **ptr;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t size;
|
|
|
|
size_t used;
|
|
|
|
} char_array;
|
|
|
|
|
|
|
|
typedef struct {
|
2017-09-10 19:28:51 +00:00
|
|
|
struct { pid_t pid; void *ctx; } *ptr;
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t used;
|
|
|
|
size_t size;
|
|
|
|
} buffer_pid_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
array *cgi;
|
2009-07-04 20:23:00 +00:00
|
|
|
unsigned short execute_x_only;
|
2017-02-26 22:49:47 +00:00
|
|
|
unsigned short local_redir;
|
2016-04-22 03:57:14 +00:00
|
|
|
unsigned short xsendfile_allow;
|
2017-05-13 22:37:23 +00:00
|
|
|
unsigned short upgrade;
|
2016-04-22 03:57:14 +00:00
|
|
|
array *xsendfile_docroot;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
|
|
|
buffer_pid_t cgi_pid;
|
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
|
|
|
|
|
|
|
plugin_config conf;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
pid_t pid;
|
|
|
|
int fd;
|
2016-04-12 04:21:03 +00:00
|
|
|
int fdtocgi;
|
2005-02-20 14:27:00 +00:00
|
|
|
int fde_ndx; /* index into the fd-event buffer */
|
2016-04-12 04:21:03 +00:00
|
|
|
int fde_ndx_tocgi; /* index into the fd-event buffer */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
connection *remote_conn; /* dumb pointer */
|
|
|
|
plugin_data *plugin_data; /* dumb pointer */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer *response;
|
2016-12-16 16:06:29 +00:00
|
|
|
buffer *cgi_handler; /* dumb pointer */
|
2017-03-18 04:10:48 +00:00
|
|
|
http_response_opts opts;
|
2016-11-28 17:39:37 +00:00
|
|
|
plugin_config conf;
|
2005-02-20 14:27:00 +00:00
|
|
|
} handler_ctx;
|
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static handler_ctx * cgi_handler_ctx_init(void) {
|
2005-02-20 14:27:00 +00:00
|
|
|
handler_ctx *hctx = calloc(1, sizeof(*hctx));
|
|
|
|
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(hctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
hctx->response = buffer_init();
|
2016-04-07 00:46:43 +00:00
|
|
|
hctx->fd = -1;
|
2016-04-12 04:21:03 +00:00
|
|
|
hctx->fdtocgi = -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return hctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cgi_handler_ctx_free(handler_ctx *hctx) {
|
|
|
|
buffer_free(hctx->response);
|
|
|
|
free(hctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
INIT_FUNC(mod_cgi_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));
|
|
|
|
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FREE_FUNC(mod_cgi_free) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
buffer_pid_t *r = &(p->cgi_pid);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
UNUSED(srv);
|
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;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
array_free(s->cgi);
|
2016-04-22 03:57:14 +00:00
|
|
|
array_free(s->xsendfile_docroot);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
free(p->config_storage);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
if (r->ptr) free(r->ptr);
|
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_fastcgi_set_defaults) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
size_t i = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
config_values_t cv[] = {
|
2005-02-20 14:27:00 +00:00
|
|
|
{ "cgi.assign", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
2009-07-04 20:23:00 +00:00
|
|
|
{ "cgi.execute-x-only", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
|
2016-05-07 05:18:34 +00:00
|
|
|
{ "cgi.x-sendfile", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
|
|
|
|
{ "cgi.x-sendfile-docroot", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
|
2017-02-26 22:49:47 +00:00
|
|
|
{ "cgi.local-redir", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
|
2017-05-13 22:37:23 +00:00
|
|
|
{ "cgi.upgrade", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
|
2005-02-20 14:27:00 +00:00
|
|
|
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!p) return HANDLER_ERROR;
|
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 *));
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(p->config_storage);
|
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-08-16 13:07:46 +00:00
|
|
|
s = calloc(1, sizeof(plugin_config));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(s);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
s->cgi = array_init();
|
2009-07-04 20:23:00 +00:00
|
|
|
s->execute_x_only = 0;
|
2017-02-26 22:49:47 +00:00
|
|
|
s->local_redir = 0;
|
2016-04-22 03:57:14 +00:00
|
|
|
s->xsendfile_allow= 0;
|
|
|
|
s->xsendfile_docroot = array_init();
|
2017-05-13 22:37:23 +00:00
|
|
|
s->upgrade = 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
cv[0].destination = s->cgi;
|
2009-07-04 20:23:00 +00:00
|
|
|
cv[1].destination = &(s->execute_x_only);
|
2016-04-22 03:57:14 +00:00
|
|
|
cv[2].destination = &(s->xsendfile_allow);
|
|
|
|
cv[3].destination = s->xsendfile_docroot;
|
2017-02-26 22:49:47 +00:00
|
|
|
cv[4].destination = &(s->local_redir);
|
2017-05-13 22:37:23 +00:00
|
|
|
cv[5].destination = &(s->upgrade);
|
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;
|
|
|
|
}
|
2016-04-22 03:57:14 +00:00
|
|
|
|
2017-03-05 20:39:45 +00:00
|
|
|
if (!array_is_kvstring(s->cgi)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"unexpected value for cgi.assign; expected list of \"ext\" => \"exepath\"");
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-04-22 03:57:14 +00:00
|
|
|
if (s->xsendfile_docroot->used) {
|
|
|
|
size_t j;
|
|
|
|
for (j = 0; j < s->xsendfile_docroot->used; ++j) {
|
|
|
|
data_string *ds = (data_string *)s->xsendfile_docroot->data[j];
|
|
|
|
if (ds->type != TYPE_STRING) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s",
|
|
|
|
"unexpected type for key cgi.x-sendfile-docroot; expected: cgi.x-sendfile-docroot = ( \"/allowed/path\", ... )");
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
if (ds->value->ptr[0] != '/') {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "SBs",
|
|
|
|
"cgi.x-sendfile-docroot paths must begin with '/'; invalid: \"", ds->value, "\"");
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
buffer_path_simplify(ds->value, ds->value);
|
|
|
|
buffer_append_slash(ds->value);
|
|
|
|
}
|
|
|
|
}
|
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 HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-10 19:28:51 +00:00
|
|
|
static void cgi_pid_add(plugin_data *p, pid_t pid, void *ctx) {
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_pid_t *r = &(p->cgi_pid);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (r->size == 0) {
|
|
|
|
r->size = 16;
|
|
|
|
r->ptr = malloc(sizeof(*r->ptr) * r->size);
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(r->ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
} else if (r->used == r->size) {
|
|
|
|
r->size += 16;
|
|
|
|
r->ptr = realloc(r->ptr, sizeof(*r->ptr) * r->size);
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(r->ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-09-10 19:28:51 +00:00
|
|
|
r->ptr[r->used].pid = pid;
|
|
|
|
r->ptr[r->used].ctx = ctx;
|
|
|
|
++r->used;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-09-10 19:28:51 +00:00
|
|
|
static void cgi_pid_kill(plugin_data *p, pid_t pid) {
|
|
|
|
buffer_pid_t *r = &(p->cgi_pid);
|
|
|
|
for (size_t i = 0; i < r->used; ++i) {
|
|
|
|
if (r->ptr[i].pid == pid) {
|
|
|
|
r->ptr[i].ctx = NULL;
|
|
|
|
kill(pid, SIGTERM);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 19:28:51 +00:00
|
|
|
static void cgi_pid_del(plugin_data *p, size_t i) {
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_pid_t *r = &(p->cgi_pid);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (i != r->used - 1) {
|
|
|
|
r->ptr[i] = r->ptr[r->used - 1];
|
|
|
|
}
|
|
|
|
r->used--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-12 04:21:03 +00:00
|
|
|
static void cgi_connection_close_fdtocgi(server *srv, handler_ctx *hctx) {
|
|
|
|
/*(closes only hctx->fdtocgi)*/
|
|
|
|
fdevent_event_del(srv->ev, &(hctx->fde_ndx_tocgi), hctx->fdtocgi);
|
2017-10-21 16:20:27 +00:00
|
|
|
/*fdevent_unregister(srv->ev, hctx->fdtocgi);*//*(handled below)*/
|
2016-08-24 19:30:11 +00:00
|
|
|
fdevent_sched_close(srv->ev, hctx->fdtocgi, 0);
|
2016-04-12 04:21:03 +00:00
|
|
|
hctx->fdtocgi = -1;
|
|
|
|
}
|
|
|
|
|
2016-04-07 00:46:43 +00:00
|
|
|
static void cgi_connection_close(server *srv, handler_ctx *hctx) {
|
2016-03-04 18:54:26 +00:00
|
|
|
plugin_data *p = hctx->plugin_data;
|
|
|
|
connection *con = hctx->remote_conn;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
/* the connection to the browser went away, but we still have a connection
|
2006-10-04 13:26:23 +00:00
|
|
|
* to the CGI script
|
2005-02-20 14:27:00 +00:00
|
|
|
*
|
|
|
|
* close cgi-connection
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-04 15:19:50 +00:00
|
|
|
if (hctx->fd != -1) {
|
|
|
|
/* close connection to the cgi-script */
|
|
|
|
fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
|
2017-10-21 16:20:27 +00:00
|
|
|
/*fdevent_unregister(srv->ev, hctx->fd);*//*(handled below)*/
|
2016-08-24 19:30:11 +00:00
|
|
|
fdevent_sched_close(srv->ev, hctx->fd, 0);
|
2016-04-12 04:21:03 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-04-12 04:21:03 +00:00
|
|
|
if (hctx->fdtocgi != -1) {
|
|
|
|
cgi_connection_close_fdtocgi(srv, hctx); /*(closes only hctx->fdtocgi)*/
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2017-09-10 19:28:51 +00:00
|
|
|
if (hctx->pid > 0) {
|
|
|
|
cgi_pid_kill(p, hctx->pid);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->plugin_ctx[p->id] = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
cgi_handler_ctx_free(hctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-05-27 04:24:33 +00:00
|
|
|
/* finish response (if not already con->file_started, con->file_finished) */
|
|
|
|
if (con->mode == p->id) {
|
|
|
|
http_response_backend_done(srv, con);
|
2016-03-04 18:54:24 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static handler_t cgi_connection_close_callback(server *srv, connection *con, void *p_d) {
|
|
|
|
plugin_data *p = p_d;
|
2016-03-04 18:54:26 +00:00
|
|
|
handler_ctx *hctx = con->plugin_ctx[p->id];
|
2016-04-07 00:46:43 +00:00
|
|
|
if (hctx) cgi_connection_close(srv, hctx);
|
2016-03-04 18:54:26 +00:00
|
|
|
|
2016-04-07 00:46:43 +00:00
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-12 04:21:03 +00:00
|
|
|
static int cgi_write_request(server *srv, handler_ctx *hctx, int fd);
|
|
|
|
|
|
|
|
|
|
|
|
static handler_t cgi_handle_fdevent_send (server *srv, void *ctx, int revents) {
|
|
|
|
handler_ctx *hctx = ctx;
|
|
|
|
connection *con = hctx->remote_conn;
|
|
|
|
|
|
|
|
/*(joblist only actually necessary here in mod_cgi fdevent send if returning HANDLER_ERROR)*/
|
|
|
|
joblist_append(srv, con);
|
|
|
|
|
|
|
|
if (revents & FDEVENT_OUT) {
|
|
|
|
if (0 != cgi_write_request(srv, hctx, hctx->fdtocgi)) {
|
|
|
|
cgi_connection_close(srv, hctx);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
/* more request body to be sent to CGI */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (revents & FDEVENT_HUP) {
|
|
|
|
/* skip sending remaining data to CGI */
|
2016-06-10 04:04:10 +00:00
|
|
|
if (con->request.content_length) {
|
|
|
|
chunkqueue *cq = con->request_content_queue;
|
|
|
|
chunkqueue_mark_written(cq, chunkqueue_length(cq));
|
|
|
|
if (cq->bytes_in != (off_t)con->request.content_length) {
|
|
|
|
con->keep_alive = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 04:21:03 +00:00
|
|
|
|
|
|
|
cgi_connection_close_fdtocgi(srv, hctx); /*(closes only hctx->fdtocgi)*/
|
|
|
|
} else if (revents & FDEVENT_ERR) {
|
|
|
|
/* kill all connections to the cgi process */
|
|
|
|
#if 1
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "cgi-FDEVENT_ERR");
|
|
|
|
#endif
|
|
|
|
cgi_connection_close(srv, hctx);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
[core] shared code for socket backends
common codebase for socket backends, based off mod_fastcgi with
some features added for mod_proxy
(mostly intended to reduce code duplication and enhance code isolation)
mod_fastcgi and mod_scgi can now use fastcgi.balance and scgi.balance
for similar behavior as proxy.balance, but the balancing is per-host
and not per-proc. proxy.balance is also per-host and not per-proc.
mod_proxy and mod_scgi can now use proxy.map-extensions and
scgi.map-extensions, similar to fastcgi.map-extensions.
mod_fastcgi behavior change (affects only mod_status):
- statistics tags have been renamed from "fastcgi.*" to "gw.*"
"fastcgi.backend.*" -> "gw.backend.*"
"fastcgi.active-requests" -> "gw.active-requests"
("fastcgi.requests" remains "fastcgi.requests")
("proxy.requests" is new)
("scgi.requests" is new)
mod_scgi behavior change (likely minor):
- removed scgi_proclist_sort_down() and scgi_proclist_sort_up().
procs now chosen based on load as measured by num socket connnections
Note:
modules using gw_backend.[ch] are currently still independent modules.
If it had been written as a single module with fastcgi, scgi, proxy
implementations, then there would have been a chance of breaking some
existing user configurations where module ordering made a difference
for which module handled a given request, though for most people, this
would have made no difference.
Details about mod_fastcgi code transformations:
unsigned int debug -> int debug
fastcgi_env member removed from plugin_config
renamed "fcgi" and "fastcgi" to "gw", and "FCGI" to "GW"
reorganize routines for high-level and lower-level interfaces
some lower-level internal interfaces changed to use host,proc,debug
args rather than knowing about higher-level (app) hctx and plugin_data
tabs->spaces and reformatting
2017-07-14 05:29:18 +00:00
|
|
|
static handler_t cgi_response_headers(server *srv, connection *con, struct http_response_opts_t *opts) {
|
2017-05-13 22:37:23 +00:00
|
|
|
/* response headers just completed */
|
[core] shared code for socket backends
common codebase for socket backends, based off mod_fastcgi with
some features added for mod_proxy
(mostly intended to reduce code duplication and enhance code isolation)
mod_fastcgi and mod_scgi can now use fastcgi.balance and scgi.balance
for similar behavior as proxy.balance, but the balancing is per-host
and not per-proc. proxy.balance is also per-host and not per-proc.
mod_proxy and mod_scgi can now use proxy.map-extensions and
scgi.map-extensions, similar to fastcgi.map-extensions.
mod_fastcgi behavior change (affects only mod_status):
- statistics tags have been renamed from "fastcgi.*" to "gw.*"
"fastcgi.backend.*" -> "gw.backend.*"
"fastcgi.active-requests" -> "gw.active-requests"
("fastcgi.requests" remains "fastcgi.requests")
("proxy.requests" is new)
("scgi.requests" is new)
mod_scgi behavior change (likely minor):
- removed scgi_proclist_sort_down() and scgi_proclist_sort_up().
procs now chosen based on load as measured by num socket connnections
Note:
modules using gw_backend.[ch] are currently still independent modules.
If it had been written as a single module with fastcgi, scgi, proxy
implementations, then there would have been a chance of breaking some
existing user configurations where module ordering made a difference
for which module handled a given request, though for most people, this
would have made no difference.
Details about mod_fastcgi code transformations:
unsigned int debug -> int debug
fastcgi_env member removed from plugin_config
renamed "fcgi" and "fastcgi" to "gw", and "FCGI" to "GW"
reorganize routines for high-level and lower-level interfaces
some lower-level internal interfaces changed to use host,proc,debug
args rather than knowing about higher-level (app) hctx and plugin_data
tabs->spaces and reformatting
2017-07-14 05:29:18 +00:00
|
|
|
handler_ctx *hctx = (handler_ctx *)opts->pdata;
|
2017-05-13 22:37:23 +00:00
|
|
|
|
|
|
|
if (con->parsed_response & HTTP_UPGRADE) {
|
|
|
|
if (hctx->conf.upgrade && con->http_status == 101) {
|
|
|
|
/* 101 Switching Protocols; transition to transparent proxy */
|
|
|
|
http_response_upgrade_read_body_unknown(srv, con);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
con->parsed_response &= ~HTTP_UPGRADE;
|
|
|
|
#if 0
|
|
|
|
/* preserve prior questionable behavior; likely broken behavior
|
|
|
|
* anyway if backend thinks connection is being upgraded but client
|
|
|
|
* does not receive Connection: upgrade */
|
|
|
|
response_header_overwrite(srv, con, CONST_STR_LEN("Upgrade"),
|
|
|
|
CONST_STR_LEN(""));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hctx->conf.upgrade && !(con->parsed_response & HTTP_UPGRADE)) {
|
|
|
|
chunkqueue *cq = con->request_content_queue;
|
|
|
|
hctx->conf.upgrade = 0;
|
|
|
|
if (cq->bytes_out == (off_t)con->request.content_length) {
|
|
|
|
cgi_connection_close_fdtocgi(srv, hctx); /*(closes hctx->fdtocgi)*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[core] shared code for socket backends
common codebase for socket backends, based off mod_fastcgi with
some features added for mod_proxy
(mostly intended to reduce code duplication and enhance code isolation)
mod_fastcgi and mod_scgi can now use fastcgi.balance and scgi.balance
for similar behavior as proxy.balance, but the balancing is per-host
and not per-proc. proxy.balance is also per-host and not per-proc.
mod_proxy and mod_scgi can now use proxy.map-extensions and
scgi.map-extensions, similar to fastcgi.map-extensions.
mod_fastcgi behavior change (affects only mod_status):
- statistics tags have been renamed from "fastcgi.*" to "gw.*"
"fastcgi.backend.*" -> "gw.backend.*"
"fastcgi.active-requests" -> "gw.active-requests"
("fastcgi.requests" remains "fastcgi.requests")
("proxy.requests" is new)
("scgi.requests" is new)
mod_scgi behavior change (likely minor):
- removed scgi_proclist_sort_down() and scgi_proclist_sort_up().
procs now chosen based on load as measured by num socket connnections
Note:
modules using gw_backend.[ch] are currently still independent modules.
If it had been written as a single module with fastcgi, scgi, proxy
implementations, then there would have been a chance of breaking some
existing user configurations where module ordering made a difference
for which module handled a given request, though for most people, this
would have made no difference.
Details about mod_fastcgi code transformations:
unsigned int debug -> int debug
fastcgi_env member removed from plugin_config
renamed "fcgi" and "fastcgi" to "gw", and "FCGI" to "GW"
reorganize routines for high-level and lower-level interfaces
some lower-level internal interfaces changed to use host,proc,debug
args rather than knowing about higher-level (app) hctx and plugin_data
tabs->spaces and reformatting
2017-07-14 05:29:18 +00:00
|
|
|
return HANDLER_GO_ON;
|
2017-05-13 22:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-11 03:34:55 +00:00
|
|
|
static int cgi_recv_response(server *srv, handler_ctx *hctx) {
|
[core] shared code for socket backends
common codebase for socket backends, based off mod_fastcgi with
some features added for mod_proxy
(mostly intended to reduce code duplication and enhance code isolation)
mod_fastcgi and mod_scgi can now use fastcgi.balance and scgi.balance
for similar behavior as proxy.balance, but the balancing is per-host
and not per-proc. proxy.balance is also per-host and not per-proc.
mod_proxy and mod_scgi can now use proxy.map-extensions and
scgi.map-extensions, similar to fastcgi.map-extensions.
mod_fastcgi behavior change (affects only mod_status):
- statistics tags have been renamed from "fastcgi.*" to "gw.*"
"fastcgi.backend.*" -> "gw.backend.*"
"fastcgi.active-requests" -> "gw.active-requests"
("fastcgi.requests" remains "fastcgi.requests")
("proxy.requests" is new)
("scgi.requests" is new)
mod_scgi behavior change (likely minor):
- removed scgi_proclist_sort_down() and scgi_proclist_sort_up().
procs now chosen based on load as measured by num socket connnections
Note:
modules using gw_backend.[ch] are currently still independent modules.
If it had been written as a single module with fastcgi, scgi, proxy
implementations, then there would have been a chance of breaking some
existing user configurations where module ordering made a difference
for which module handled a given request, though for most people, this
would have made no difference.
Details about mod_fastcgi code transformations:
unsigned int debug -> int debug
fastcgi_env member removed from plugin_config
renamed "fcgi" and "fastcgi" to "gw", and "FCGI" to "GW"
reorganize routines for high-level and lower-level interfaces
some lower-level internal interfaces changed to use host,proc,debug
args rather than knowing about higher-level (app) hctx and plugin_data
tabs->spaces and reformatting
2017-07-14 05:29:18 +00:00
|
|
|
switch (http_response_read(srv, hctx->remote_conn, &hctx->opts,
|
|
|
|
hctx->response, hctx->fd, &hctx->fde_ndx)) {
|
2017-03-18 04:10:48 +00:00
|
|
|
default:
|
|
|
|
return HANDLER_GO_ON;
|
|
|
|
case HANDLER_ERROR:
|
|
|
|
http_response_backend_error(srv, hctx->remote_conn);
|
|
|
|
/* fall through */
|
|
|
|
case HANDLER_FINISHED:
|
2005-05-20 13:30:53 +00:00
|
|
|
cgi_connection_close(srv, hctx);
|
2005-06-04 15:19:50 +00:00
|
|
|
return HANDLER_FINISHED;
|
2017-03-18 04:10:48 +00:00
|
|
|
case HANDLER_COMEBACK:
|
|
|
|
/* hctx->conf.local_redir */
|
|
|
|
connection_response_reset(srv, hctx->remote_conn); /*(includes con->http_status = 0)*/
|
|
|
|
plugins_call_connection_reset(srv, hctx->remote_conn);
|
2017-02-20 19:47:13 +00:00
|
|
|
/*cgi_connection_close(srv, hctx);*//*(already cleaned up and hctx is now invalid)*/
|
2016-07-14 18:29:02 +00:00
|
|
|
return HANDLER_COMEBACK;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2016-06-11 03:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static handler_t cgi_handle_fdevent(server *srv, void *ctx, int revents) {
|
|
|
|
handler_ctx *hctx = ctx;
|
|
|
|
connection *con = hctx->remote_conn;
|
|
|
|
|
|
|
|
joblist_append(srv, con);
|
|
|
|
|
|
|
|
if (revents & FDEVENT_IN) {
|
|
|
|
handler_t rc = cgi_recv_response(srv, hctx);/*(might invalidate hctx)*/
|
|
|
|
if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* perhaps this issue is already handled */
|
|
|
|
if (revents & FDEVENT_HUP) {
|
2016-06-14 04:48:28 +00:00
|
|
|
if (con->file_started) {
|
|
|
|
/* drain any remaining data from kernel pipe buffers
|
|
|
|
* even if (con->conf.stream_response_body
|
|
|
|
* & FDEVENT_STREAM_RESPONSE_BUFMIN)
|
|
|
|
* since event loop will spin on fd FDEVENT_HUP event
|
|
|
|
* until unregistered. */
|
|
|
|
handler_t rc;
|
|
|
|
do {
|
|
|
|
rc = cgi_recv_response(srv,hctx);/*(might invalidate hctx)*/
|
|
|
|
} while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
|
2016-07-14 18:29:02 +00:00
|
|
|
return rc; /* HANDLER_FINISHED or HANDLER_COMEBACK or HANDLER_ERROR */
|
2017-03-18 04:10:48 +00:00
|
|
|
} else if (!buffer_string_is_empty(hctx->response)) {
|
2016-06-14 04:48:28 +00:00
|
|
|
/* unfinished header package which is a body in reality */
|
2005-02-20 14:27:00 +00:00
|
|
|
con->file_started = 1;
|
2017-03-18 04:10:48 +00:00
|
|
|
if (0 != http_chunk_append_buffer(srv, con, hctx->response)) {
|
2016-05-25 20:45:09 +00:00
|
|
|
cgi_connection_close(srv, hctx);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2017-02-22 16:58:21 +00:00
|
|
|
if (0 == con->http_status) con->http_status = 200; /* OK */
|
2016-06-14 04:48:28 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
cgi_connection_close(srv, hctx);
|
|
|
|
} else if (revents & FDEVENT_ERR) {
|
|
|
|
/* kill all connections to the cgi process */
|
|
|
|
cgi_connection_close(srv, hctx);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
consistent, shared code to create CGI env
consolidated from CGI, FastCGI, SCGI, SSI
Note: due to prior inconsistencies between the code in mod_cgi,
mod_fastcgi, mod_scgi, and mod_ssi, there are some minor behavior
changes.
CONTENT_LENGTH is now always set, even if 0
(though CONTENT_LENGTH is never set for FASTCGI_AUTHORIZER)
PATH_INFO is created only if present, not if empty.
(mod_fastcgi and mod_ssi previously set PATH_INFO="" (blank value))
PATH_TRANSLATED is now set if PATH_INFO is present
(previously missing from mod_cgi and mod_ssi)
mod_ssi now sets DOCUMENT_ROOT to con->physical.basedir, like others
(previously, mod_ssi set DOCUMENT_ROOT to con->physical.doc_root,
which matched con->physical.basedir unless mod_alias changed basedir)
mod_ssi now sets REQUEST_URI to con->request.orig_uri, like others
(previously, mod_ssi set REQUEST_URI to con->request.uri, which
matched con->request.orig_uri except after redirects, error docs)
2016-10-10 17:37:36 +00:00
|
|
|
static int cgi_env_add(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
|
|
|
|
char_array *env = venv;
|
2005-02-20 14:27:00 +00:00
|
|
|
char *dst;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!key || !val) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-04-28 18:26:23 +00:00
|
|
|
dst = malloc(key_len + val_len + 2);
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(dst);
|
2005-02-20 14:27:00 +00:00
|
|
|
memcpy(dst, key, key_len);
|
|
|
|
dst[key_len] = '=';
|
2009-04-28 18:26:23 +00:00
|
|
|
memcpy(dst + key_len + 1, val, val_len);
|
|
|
|
dst[key_len + 1 + val_len] = '\0';
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (env->size == 0) {
|
|
|
|
env->size = 16;
|
|
|
|
env->ptr = malloc(env->size * sizeof(*env->ptr));
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(env->ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
} else if (env->size == env->used) {
|
|
|
|
env->size += 16;
|
|
|
|
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(env->ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
env->ptr[env->used++] = dst;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-05 05:22:33 +00:00
|
|
|
/*(improved from network_write_mmap.c)*/
|
|
|
|
static off_t mmap_align_offset(off_t start) {
|
|
|
|
static off_t pagemask = 0;
|
|
|
|
if (0 == pagemask) {
|
|
|
|
long pagesize = sysconf(_SC_PAGESIZE);
|
|
|
|
if (-1 == pagesize) pagesize = 4096;
|
|
|
|
pagemask = ~((off_t)pagesize - 1); /* pagesize always power-of-2 */
|
|
|
|
}
|
|
|
|
return (start & pagemask);
|
|
|
|
}
|
|
|
|
|
2015-08-23 11:53:48 +00:00
|
|
|
/* returns: 0: continue, -1: fatal error, -2: connection reset */
|
|
|
|
/* similar to network_write_file_chunk_mmap, but doesn't use send on windows (because we're on pipes),
|
|
|
|
* also mmaps and sends complete chunk instead of only small parts - the files
|
|
|
|
* are supposed to be temp files with reasonable chunk sizes.
|
|
|
|
*
|
|
|
|
* Also always use mmap; the files are "trusted", as we created them.
|
|
|
|
*/
|
2016-04-12 04:21:03 +00:00
|
|
|
static ssize_t cgi_write_file_chunk_mmap(server *srv, connection *con, int fd, chunkqueue *cq) {
|
2015-08-23 11:53:48 +00:00
|
|
|
chunk* const c = cq->first;
|
|
|
|
off_t offset, toSend, file_end;
|
|
|
|
ssize_t r;
|
|
|
|
size_t mmap_offset, mmap_avail;
|
2017-03-18 04:21:25 +00:00
|
|
|
char *data = NULL;
|
2015-08-23 11:53:48 +00:00
|
|
|
|
|
|
|
force_assert(NULL != c);
|
|
|
|
force_assert(FILE_CHUNK == c->type);
|
|
|
|
force_assert(c->offset >= 0 && c->offset <= c->file.length);
|
|
|
|
|
|
|
|
offset = c->file.start + c->offset;
|
|
|
|
toSend = c->file.length - c->offset;
|
|
|
|
file_end = c->file.start + c->file.length; /* offset to file end in this chunk */
|
|
|
|
|
|
|
|
if (0 == toSend) {
|
|
|
|
chunkqueue_remove_finished_chunks(cq);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-21 11:00:32 +00:00
|
|
|
/*(simplified from chunk.c:chunkqueue_open_file_chunk())*/
|
2016-12-05 05:22:33 +00:00
|
|
|
UNUSED(con);
|
|
|
|
if (-1 == c->file.fd) {
|
|
|
|
if (-1 == (c->file.fd = fdevent_open_cloexec(c->file.name->ptr, O_RDONLY, 0))) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssb", "open failed:", strerror(errno), c->file.name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-08-23 11:53:48 +00:00
|
|
|
|
|
|
|
/* (re)mmap the buffer if range is not covered completely */
|
|
|
|
if (MAP_FAILED == c->file.mmap.start
|
|
|
|
|| offset < c->file.mmap.offset
|
|
|
|
|| file_end > (off_t)(c->file.mmap.offset + c->file.mmap.length)) {
|
|
|
|
|
|
|
|
if (MAP_FAILED != c->file.mmap.start) {
|
|
|
|
munmap(c->file.mmap.start, c->file.mmap.length);
|
|
|
|
c->file.mmap.start = MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
2015-08-23 12:59:07 +00:00
|
|
|
c->file.mmap.offset = mmap_align_offset(offset);
|
|
|
|
c->file.mmap.length = file_end - c->file.mmap.offset;
|
2015-08-23 11:53:48 +00:00
|
|
|
|
2016-02-10 19:33:57 +00:00
|
|
|
if (MAP_FAILED == (c->file.mmap.start = mmap(NULL, c->file.mmap.length, PROT_READ, MAP_PRIVATE, c->file.fd, c->file.mmap.offset))) {
|
2016-04-18 03:37:40 +00:00
|
|
|
if (toSend > 65536) toSend = 65536;
|
|
|
|
data = malloc(toSend);
|
|
|
|
force_assert(data);
|
|
|
|
if (-1 == lseek(c->file.fd, offset, SEEK_SET)
|
|
|
|
|| 0 >= (toSend = read(c->file.fd, data, toSend))) {
|
|
|
|
if (-1 == toSend) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssbdo", "lseek/read failed:",
|
|
|
|
strerror(errno), c->file.name, c->file.fd, offset);
|
|
|
|
} else { /*(0 == toSend)*/
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbdo", "unexpected EOF (input truncated?):",
|
|
|
|
c->file.name, c->file.fd, offset);
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-08-23 11:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:37:40 +00:00
|
|
|
if (MAP_FAILED != c->file.mmap.start) {
|
|
|
|
force_assert(offset >= c->file.mmap.offset);
|
|
|
|
mmap_offset = offset - c->file.mmap.offset;
|
|
|
|
force_assert(c->file.mmap.length > mmap_offset);
|
|
|
|
mmap_avail = c->file.mmap.length - mmap_offset;
|
|
|
|
force_assert(toSend <= (off_t) mmap_avail);
|
|
|
|
|
|
|
|
data = c->file.mmap.start + mmap_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = write(fd, data, toSend);
|
2015-08-23 11:53:48 +00:00
|
|
|
|
2016-04-18 03:37:40 +00:00
|
|
|
if (MAP_FAILED == c->file.mmap.start) free(data);
|
2015-08-23 11:53:48 +00:00
|
|
|
|
2016-04-18 03:37:40 +00:00
|
|
|
if (r < 0) {
|
2015-08-23 11:53:48 +00:00
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
case EINTR:
|
|
|
|
return 0;
|
|
|
|
case EPIPE:
|
|
|
|
case ECONNRESET:
|
|
|
|
return -2;
|
|
|
|
default:
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssd",
|
|
|
|
"write failed:", strerror(errno), fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r >= 0) {
|
|
|
|
chunkqueue_mark_written(cq, r);
|
|
|
|
}
|
|
|
|
|
2016-04-12 04:21:03 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cgi_write_request(server *srv, handler_ctx *hctx, int fd) {
|
|
|
|
connection *con = hctx->remote_conn;
|
|
|
|
chunkqueue *cq = con->request_content_queue;
|
|
|
|
chunk *c;
|
|
|
|
|
|
|
|
/* old comment: windows doesn't support select() on pipes - wouldn't be easy to fix for all platforms.
|
|
|
|
* solution: if this is still a problem on windows, then substitute
|
|
|
|
* socketpair() for pipe() and closesocket() for close() on windows.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (c = cq->first; c; c = cq->first) {
|
|
|
|
ssize_t r = -1;
|
|
|
|
|
|
|
|
switch(c->type) {
|
|
|
|
case FILE_CHUNK:
|
|
|
|
r = cgi_write_file_chunk_mmap(srv, con, fd, cq);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MEM_CHUNK:
|
|
|
|
if ((r = write(fd, c->mem->ptr + c->offset, buffer_string_length(c->mem) - c->offset)) < 0) {
|
|
|
|
switch(errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
case EINTR:
|
|
|
|
/* ignore and try again */
|
|
|
|
r = 0;
|
|
|
|
break;
|
|
|
|
case EPIPE:
|
|
|
|
case ECONNRESET:
|
|
|
|
/* connection closed */
|
|
|
|
r = -2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* fatal error */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "write failed due to: ", strerror(errno));
|
|
|
|
r = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (r > 0) {
|
|
|
|
chunkqueue_mark_written(cq, r);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == r) break; /*(might block)*/
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case -1:
|
|
|
|
/* fatal error */
|
|
|
|
return -1;
|
|
|
|
case -2:
|
|
|
|
/* connection reset */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "failed to send post data to cgi, connection closed by CGI");
|
|
|
|
/* skip all remaining data */
|
|
|
|
chunkqueue_mark_written(cq, chunkqueue_length(cq));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:37:23 +00:00
|
|
|
if (cq->bytes_out == (off_t)con->request.content_length && !hctx->conf.upgrade) {
|
2016-04-12 04:21:03 +00:00
|
|
|
/* sent all request body input */
|
|
|
|
/* close connection to the cgi-script */
|
2016-06-10 04:04:10 +00:00
|
|
|
if (-1 == hctx->fdtocgi) { /*(received request body sent in initial send to pipe buffer)*/
|
2016-11-24 14:31:52 +00:00
|
|
|
--srv->cur_fds;
|
2016-04-12 04:21:03 +00:00
|
|
|
if (close(fd)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sds", "cgi stdin close failed ", fd, strerror(errno));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cgi_connection_close_fdtocgi(srv, hctx); /*(closes only hctx->fdtocgi)*/
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-10 04:04:10 +00:00
|
|
|
off_t cqlen = cq->bytes_in - cq->bytes_out;
|
2016-12-16 16:06:29 +00:00
|
|
|
if (cq->bytes_in != con->request.content_length && cqlen < 65536 - 16384) {
|
2016-06-10 04:04:10 +00:00
|
|
|
/*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
|
|
|
|
if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
|
|
|
|
con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
|
|
|
|
con->is_readable = 1; /* trigger optimistic read from client */
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 04:21:03 +00:00
|
|
|
if (-1 == hctx->fdtocgi) { /*(not registered yet)*/
|
|
|
|
hctx->fdtocgi = fd;
|
|
|
|
hctx->fde_ndx_tocgi = -1;
|
|
|
|
fdevent_register(srv->ev, hctx->fdtocgi, cgi_handle_fdevent_send, hctx);
|
2016-06-10 04:04:10 +00:00
|
|
|
}
|
|
|
|
if (0 == cqlen) { /*(chunkqueue_is_empty(cq))*/
|
|
|
|
if ((fdevent_event_get_interest(srv->ev, hctx->fdtocgi) & FDEVENT_OUT)) {
|
|
|
|
fdevent_event_set(srv->ev, &(hctx->fde_ndx_tocgi), hctx->fdtocgi, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* more request body remains to be sent to CGI so register for fdevents */
|
2016-04-12 04:21:03 +00:00
|
|
|
fdevent_event_set(srv->ev, &(hctx->fde_ndx_tocgi), hctx->fdtocgi, FDEVENT_OUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 11:53:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|