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"
|
|
|
|
#include "http_chunk.h"
|
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <sys/types.h>
|
2009-10-11 14:31:42 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef __WIN32
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <winsock2.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
#else
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <sys/socket.h>
|
|
|
|
# include <sys/wait.h>
|
|
|
|
# include <sys/mman.h>
|
|
|
|
# include <netinet/in.h>
|
|
|
|
# include <arpa/inet.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fdevent.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-12-20 12:47:39 +00:00
|
|
|
#include <fcntl.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2005-08-19 11:14:04 +00:00
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
# include <sys/filio.h>
|
|
|
|
#endif
|
|
|
|
|
2009-04-10 17:35:19 +00:00
|
|
|
#include "version.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
enum {EOL_UNSET, EOL_N, EOL_RN};
|
|
|
|
|
|
|
|
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 {
|
|
|
|
pid_t *ptr;
|
|
|
|
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;
|
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
|
|
|
buffer *tmp_buf;
|
|
|
|
buffer *parse_response;
|
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;
|
|
|
|
int fde_ndx; /* 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;
|
|
|
|
buffer *response_header;
|
|
|
|
} 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));
|
|
|
|
|
|
|
|
assert(hctx);
|
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();
|
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);
|
|
|
|
buffer_free(hctx->response_header);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(hctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {FDEVENT_HANDLED_UNSET, FDEVENT_HANDLED_FINISHED, FDEVENT_HANDLED_NOT_FINISHED, FDEVENT_HANDLED_ERROR};
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
assert(p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->tmp_buf = buffer_init();
|
|
|
|
p->parse_response = buffer_init();
|
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
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
array_free(s->cgi);
|
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
|
|
|
buffer_free(p->tmp_buf);
|
|
|
|
buffer_free(p->parse_response);
|
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 */
|
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
|
|
|
|
2005-08-16 13:07:46 +00:00
|
|
|
p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_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++) {
|
|
|
|
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));
|
2005-02-20 14:27:00 +00:00
|
|
|
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;
|
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);
|
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
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int cgi_pid_add(server *srv, plugin_data *p, pid_t pid) {
|
|
|
|
int m = -1;
|
|
|
|
size_t i;
|
|
|
|
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);
|
|
|
|
|
|
|
|
for (i = 0; i < r->used; i++) {
|
|
|
|
if (r->ptr[i] > m) m = r->ptr[i];
|
|
|
|
}
|
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);
|
|
|
|
} else if (r->used == r->size) {
|
|
|
|
r->size += 16;
|
|
|
|
r->ptr = realloc(r->ptr, sizeof(*r->ptr) * r->size);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
r->ptr[r->used++] = pid;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cgi_pid_del(server *srv, plugin_data *p, pid_t pid) {
|
|
|
|
size_t i;
|
|
|
|
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);
|
|
|
|
|
|
|
|
for (i = 0; i < r->used; i++) {
|
|
|
|
if (r->ptr[i] == pid) break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (i != r->used) {
|
|
|
|
/* found */
|
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--;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
static int cgi_response_parse(server *srv, connection *con, plugin_data *p, buffer *in) {
|
2005-02-20 14:27:00 +00:00
|
|
|
char *ns;
|
|
|
|
const char *s;
|
|
|
|
int line = 0;
|
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
|
|
|
buffer_copy_string_buffer(p->parse_response, in);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
for (s = p->parse_response->ptr;
|
2007-08-17 21:04:20 +00:00
|
|
|
NULL != (ns = strchr(s, '\n'));
|
|
|
|
s = ns + 1, line++) {
|
2005-02-20 14:27:00 +00:00
|
|
|
const char *key, *value;
|
|
|
|
int key_len;
|
|
|
|
data_string *ds;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
/* strip the \n */
|
2005-02-20 14:27:00 +00:00
|
|
|
ns[0] = '\0';
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
if (ns > s && ns[-1] == '\r') ns[-1] = '\0';
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
if (line == 0 &&
|
2005-02-20 14:27:00 +00:00
|
|
|
0 == strncmp(s, "HTTP/1.", 7)) {
|
|
|
|
/* non-parsed header ... we parse them anyway */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if ((s[7] == '1' ||
|
|
|
|
s[7] == '0') &&
|
|
|
|
s[8] == ' ') {
|
|
|
|
int status;
|
|
|
|
/* after the space should be a status code for us */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
status = strtol(s+9, NULL, 10);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-18 11:35:27 +00:00
|
|
|
if (status >= 100 &&
|
|
|
|
status < 1000) {
|
2005-02-20 14:27:00 +00:00
|
|
|
/* we expected 3 digits and didn't got them */
|
|
|
|
con->parsed_response |= HTTP_STATUS;
|
|
|
|
con->http_status = status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-08-17 21:04:20 +00:00
|
|
|
/* parse the headers */
|
2005-02-20 14:27:00 +00:00
|
|
|
key = s;
|
|
|
|
if (NULL == (value = strchr(s, ':'))) {
|
|
|
|
/* we expect: "<key>: <value>\r\n" */
|
|
|
|
continue;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
key_len = value - key;
|
|
|
|
value += 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* skip LWS */
|
|
|
|
while (*value == ' ' || *value == '\t') value++;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
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-02-20 14:27:00 +00:00
|
|
|
array_insert_unique(con->response.headers, (data_unset *)ds);
|
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 6:
|
|
|
|
if (0 == strncasecmp(key, "Status", key_len)) {
|
|
|
|
con->http_status = strtol(value, NULL, 10);
|
|
|
|
con->parsed_response |= HTTP_STATUS;
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
con->response.keep_alive = (0 == strcasecmp(value, "Keep-Alive")) ? 1 : 0;
|
|
|
|
con->parsed_response |= HTTP_CONNECTION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
if (0 == strncasecmp(key, "Content-Length", key_len)) {
|
|
|
|
con->response.content_length = strtol(value, NULL, 10);
|
|
|
|
con->parsed_response |= HTTP_CONTENT_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* CGI/1.1 rev 03 - 7.2.1.2 */
|
|
|
|
if ((con->parsed_response & HTTP_LOCATION) &&
|
|
|
|
!(con->parsed_response & HTTP_STATUS)) {
|
|
|
|
con->http_status = 302;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int cgi_demux_response(server *srv, handler_ctx *hctx) {
|
|
|
|
plugin_data *p = hctx->plugin_data;
|
|
|
|
connection *con = hctx->remote_conn;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
while(1) {
|
|
|
|
int n;
|
2011-03-13 17:44:39 +00:00
|
|
|
int toread;
|
|
|
|
|
|
|
|
#if defined(__WIN32)
|
|
|
|
buffer_prepare_copy(hctx->response, 4 * 1024);
|
|
|
|
#else
|
|
|
|
if (ioctl(con->fd, FIONREAD, &toread) || toread == 0 || toread <= 4*1024) {
|
|
|
|
buffer_prepare_copy(hctx->response, 4 * 1024);
|
|
|
|
} else {
|
|
|
|
if (toread > MAX_READ_LIMIT) toread = MAX_READ_LIMIT;
|
|
|
|
buffer_prepare_copy(hctx->response, toread + 1);
|
|
|
|
}
|
|
|
|
#endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (-1 == (n = read(hctx->fd, hctx->response->ptr, hctx->response->size - 1))) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR) {
|
|
|
|
/* would block, wait for signal */
|
|
|
|
return FDEVENT_HANDLED_NOT_FINISHED;
|
|
|
|
}
|
|
|
|
/* error */
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sdd", strerror(errno), con->fd, hctx->fd);
|
|
|
|
return FDEVENT_HANDLED_ERROR;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (n == 0) {
|
|
|
|
/* read finished */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->file_finished = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* send final chunk */
|
|
|
|
http_chunk_append_mem(srv, con, NULL, 0);
|
|
|
|
joblist_append(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return FDEVENT_HANDLED_FINISHED;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
hctx->response->ptr[n] = '\0';
|
|
|
|
hctx->response->used = n+1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* split header from body */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->file_started == 0) {
|
2007-08-17 21:04:20 +00:00
|
|
|
int is_header = 0;
|
|
|
|
int is_header_end = 0;
|
|
|
|
size_t last_eol = 0;
|
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_append_string_buffer(hctx->response_header, hctx->response);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
/**
|
|
|
|
* we have to handle a few cases:
|
|
|
|
*
|
|
|
|
* nph:
|
|
|
|
*
|
|
|
|
* HTTP/1.0 200 Ok\n
|
|
|
|
* Header: Value\n
|
|
|
|
* \n
|
|
|
|
*
|
|
|
|
* CGI:
|
|
|
|
* Header: Value\n
|
|
|
|
* Status: 200\n
|
|
|
|
* \n
|
|
|
|
*
|
|
|
|
* and different mixes of \n and \r\n combinations
|
|
|
|
*
|
|
|
|
* Some users also forget about CGI and just send a response and hope
|
|
|
|
* we handle it. No headers, no header-content seperator
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* nph (non-parsed headers) */
|
2007-08-17 21:04:20 +00:00
|
|
|
if (0 == strncmp(hctx->response_header->ptr, "HTTP/1.", 7)) is_header = 1;
|
|
|
|
|
|
|
|
for (i = 0; !is_header_end && i < hctx->response_header->used - 1; i++) {
|
|
|
|
char c = hctx->response_header->ptr[i];
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case ':':
|
|
|
|
/* we found a colon
|
|
|
|
*
|
|
|
|
* looks like we have a normal header
|
|
|
|
*/
|
|
|
|
is_header = 1;
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
/* EOL */
|
|
|
|
if (is_header == 0) {
|
|
|
|
/* we got a EOL but we don't seem to got a HTTP header */
|
|
|
|
|
|
|
|
is_header_end = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
/**
|
|
|
|
* check if we saw a \n(\r)?\n sequence
|
|
|
|
*/
|
|
|
|
if (last_eol > 0 &&
|
|
|
|
((i - last_eol == 1) ||
|
|
|
|
(i - last_eol == 2 && hctx->response_header->ptr[i - 1] == '\r'))) {
|
|
|
|
is_header_end = 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
last_eol = i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
break;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
if (is_header_end) {
|
|
|
|
if (!is_header) {
|
2005-02-20 14:27:00 +00:00
|
|
|
/* no header, but a body */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->request.http_version == HTTP_VERSION_1_1) {
|
|
|
|
con->response.transfer_encoding = HTTP_TRANSFER_ENCODING_CHUNKED;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
http_chunk_append_mem(srv, con, hctx->response_header->ptr, hctx->response_header->used);
|
|
|
|
joblist_append(srv, con);
|
|
|
|
} else {
|
2007-08-17 21:04:20 +00:00
|
|
|
const char *bstart;
|
|
|
|
size_t blen;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i still points to the char after the terminating EOL EOL
|
|
|
|
*
|
|
|
|
* put it on the last \n again
|
|
|
|
*/
|
|
|
|
i--;
|
|
|
|
|
|
|
|
/* the body starts after the EOL */
|
|
|
|
bstart = hctx->response_header->ptr + (i + 1);
|
|
|
|
blen = (hctx->response_header->used - 1) - (i + 1);
|
|
|
|
|
|
|
|
/* string the last \r?\n */
|
|
|
|
if (i > 0 && (hctx->response_header->ptr[i - 1] == '\r')) {
|
|
|
|
i--;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
hctx->response_header->ptr[i] = '\0';
|
|
|
|
hctx->response_header->used = i + 1; /* the string + \0 */
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* parse the response header */
|
2007-08-17 21:04:20 +00:00
|
|
|
cgi_response_parse(srv, con, p, hctx->response_header);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* enable chunked-transfer-encoding */
|
|
|
|
if (con->request.http_version == HTTP_VERSION_1_1 &&
|
|
|
|
!(con->parsed_response & HTTP_CONTENT_LENGTH)) {
|
|
|
|
con->response.transfer_encoding = HTTP_TRANSFER_ENCODING_CHUNKED;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-08-17 21:04:20 +00:00
|
|
|
if (blen > 0) {
|
|
|
|
http_chunk_append_mem(srv, con, bstart, blen + 1);
|
2005-02-20 14:27:00 +00:00
|
|
|
joblist_append(srv, con);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->file_started = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_chunk_append_mem(srv, con, hctx->response->ptr, hctx->response->used);
|
|
|
|
joblist_append(srv, con);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
#if 0
|
2005-02-20 14:27:00 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ddss", con->fd, hctx->fd, connection_get_state(con->state), b->ptr);
|
|
|
|
#endif
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return FDEVENT_HANDLED_NOT_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static handler_t cgi_connection_close(server *srv, handler_ctx *hctx) {
|
|
|
|
int status;
|
|
|
|
pid_t pid;
|
|
|
|
plugin_data *p;
|
|
|
|
connection *con;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (NULL == hctx) return HANDLER_GO_ON;
|
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 (con->mode != p->id) return HANDLER_GO_ON;
|
|
|
|
|
|
|
|
#ifndef __WIN32
|
2006-10-04 13:26:23 +00:00
|
|
|
|
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);
|
|
|
|
fdevent_unregister(srv->ev, hctx->fd);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-04 15:19:50 +00:00
|
|
|
if (close(hctx->fd)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sds", "cgi close failed ", hctx->fd, strerror(errno));
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-04 15:19:50 +00:00
|
|
|
hctx->fd = -1;
|
|
|
|
hctx->fde_ndx = -1;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
pid = 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
|
|
|
/* is this a good idea ? */
|
|
|
|
cgi_handler_ctx_free(hctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* if waitpid hasn't been called by response.c yet, do it here */
|
|
|
|
if (pid) {
|
|
|
|
/* check if the CGI-script is already gone */
|
|
|
|
switch(waitpid(pid, &status, WNOHANG)) {
|
|
|
|
case 0:
|
|
|
|
/* not finished yet */
|
|
|
|
#if 0
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd", "(debug) child isn't done yet, pid:", pid);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
/* */
|
|
|
|
if (errno == EINTR) break;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* errno == ECHILD happens if _subrequest catches the process-status before
|
2005-02-20 14:27:00 +00:00
|
|
|
* we have read the response of the cgi process
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* -> catch status
|
|
|
|
* -> WAIT_FOR_EVENT
|
|
|
|
* -> read response
|
|
|
|
* -> we get here with waitpid == ECHILD
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-02-08 12:01:58 +00:00
|
|
|
if (errno == ECHILD) return HANDLER_GO_ON;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "waitpid failed: ", strerror(errno));
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
default:
|
2005-04-20 18:07:58 +00:00
|
|
|
/* Send an error if we haven't sent any data yet */
|
|
|
|
if (0 == con->file_started) {
|
|
|
|
connection_set_state(srv, con, CON_STATE_HANDLE_REQUEST);
|
|
|
|
con->http_status = 500;
|
|
|
|
con->mode = DIRECT;
|
2010-07-04 10:37:34 +00:00
|
|
|
} else {
|
|
|
|
con->file_finished = 1;
|
2005-04-20 18:07:58 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
#if 0
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd", "(debug) cgi exited fine, pid:", pid);
|
|
|
|
#endif
|
2006-02-08 12:01:58 +00:00
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
} else {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sd", "cgi died, pid:", pid);
|
2006-02-08 12:01:58 +00:00
|
|
|
return 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
|
|
|
kill(pid, SIGTERM);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* cgi-script is still alive, queue the PID for removal */
|
|
|
|
cgi_pid_add(srv, p, pid);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
#endif
|
2006-02-08 12:01:58 +00:00
|
|
|
return HANDLER_GO_ON;
|
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;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return cgi_connection_close(srv, con->plugin_ctx[p->id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-06 21:57:15 +00:00
|
|
|
static handler_t cgi_handle_fdevent(server *srv, void *ctx, int revents) {
|
2005-02-20 14:27:00 +00:00
|
|
|
handler_ctx *hctx = ctx;
|
|
|
|
connection *con = hctx->remote_conn;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
joblist_append(srv, con);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (hctx->fd == -1) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ddss", con->fd, hctx->fd, connection_get_state(con->state), "invalid cgi-fd");
|
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
|
|
|
if (revents & FDEVENT_IN) {
|
|
|
|
switch (cgi_demux_response(srv, hctx)) {
|
|
|
|
case FDEVENT_HANDLED_NOT_FINISHED:
|
|
|
|
break;
|
|
|
|
case FDEVENT_HANDLED_FINISHED:
|
|
|
|
/* we are done */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#if 0
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ddss", con->fd, hctx->fd, connection_get_state(con->state), "finished");
|
|
|
|
#endif
|
2005-05-20 13:30:53 +00:00
|
|
|
cgi_connection_close(srv, hctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/* if we get a IN|HUP and have read everything don't exec the close twice */
|
2005-06-04 15:19:50 +00:00
|
|
|
return HANDLER_FINISHED;
|
2005-02-20 14:27:00 +00:00
|
|
|
case FDEVENT_HANDLED_ERROR:
|
2010-07-04 10:37:34 +00:00
|
|
|
/* Send an error if we haven't sent any data yet */
|
|
|
|
if (0 == con->file_started) {
|
|
|
|
connection_set_state(srv, con, CON_STATE_HANDLE_REQUEST);
|
|
|
|
con->http_status = 500;
|
|
|
|
con->mode = DIRECT;
|
|
|
|
} else {
|
|
|
|
con->file_finished = 1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "demuxer failed: ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (revents & FDEVENT_OUT) {
|
|
|
|
/* nothing to do */
|
|
|
|
}
|
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) {
|
|
|
|
/* check if we still have a unfinished header package which is a body in reality */
|
|
|
|
if (con->file_started == 0 &&
|
|
|
|
hctx->response_header->used) {
|
|
|
|
con->file_started = 1;
|
|
|
|
http_chunk_append_mem(srv, con, hctx->response_header->ptr, hctx->response_header->used);
|
|
|
|
joblist_append(srv, con);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (con->file_finished == 0) {
|
|
|
|
http_chunk_append_mem(srv, con, NULL, 0);
|
|
|
|
joblist_append(srv, con);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
con->file_finished = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (chunkqueue_is_empty(con->write_queue)) {
|
|
|
|
/* there is nothing left to write */
|
|
|
|
connection_set_state(srv, con, CON_STATE_RESPONSE_END);
|
|
|
|
} else {
|
|
|
|
/* used the write-handler to finish the request on demand */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
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
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sddd", "got HUP from cgi", con->fd, hctx->fd, revents);
|
|
|
|
# endif
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* rtsigs didn't liked the close */
|
|
|
|
cgi_connection_close(srv, hctx);
|
|
|
|
} else if (revents & FDEVENT_ERR) {
|
|
|
|
con->file_finished = 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* kill all connections to the cgi process */
|
|
|
|
cgi_connection_close(srv, hctx);
|
|
|
|
#if 1
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "cgi-FDEVENT_ERR");
|
2006-10-04 13:26:23 +00:00
|
|
|
#endif
|
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
|
|
|
return HANDLER_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-01 11:34:30 +00:00
|
|
|
static int cgi_env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
|
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);
|
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));
|
|
|
|
} else if (env->size == env->used) {
|
|
|
|
env->size += 16;
|
|
|
|
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *cgi_handler) {
|
|
|
|
pid_t pid;
|
2006-10-04 13:26:23 +00:00
|
|
|
|