summaryrefslogtreecommitdiff
path: root/src/mod_cgi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mod_cgi.c')
-rw-r--r--src/mod_cgi.c67
1 files changed, 50 insertions, 17 deletions
diff --git a/src/mod_cgi.c b/src/mod_cgi.c
index 82ae78b..3b032f1 100644
--- a/src/mod_cgi.c
+++ b/src/mod_cgi.c
@@ -822,15 +822,27 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
);
cgi_env_add(&env, CONST_STR_LEN("SERVER_PORT"), buf, strlen(buf));
+ switch (srv_sock->addr.plain.sa_family) {
#ifdef HAVE_IPV6
- s = inet_ntop(srv_sock->addr.plain.sa_family,
- srv_sock->addr.plain.sa_family == AF_INET6 ?
- (const void *) &(srv_sock->addr.ipv6.sin6_addr) :
- (const void *) &(srv_sock->addr.ipv4.sin_addr),
- b2, sizeof(b2)-1);
+ case AF_INET6:
+ s = inet_ntop(srv_sock->addr.plain.sa_family,
+ (const void *) &(srv_sock->addr.ipv6.sin6_addr),
+ b2, sizeof(b2)-1);
+ break;
+ case AF_INET:
+ s = inet_ntop(srv_sock->addr.plain.sa_family,
+ (const void *) &(srv_sock->addr.ipv4.sin_addr),
+ b2, sizeof(b2)-1);
+ break;
#else
- s = inet_ntoa(srv_sock->addr.ipv4.sin_addr);
+ case AF_INET:
+ s = inet_ntoa(srv_sock->addr.ipv4.sin_addr);
+ break;
#endif
+ default:
+ s = "";
+ break;
+ }
cgi_env_add(&env, CONST_STR_LEN("SERVER_ADDR"), s, strlen(s));
s = get_http_method_name(con->request.http_method);
@@ -848,15 +860,27 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
}
+ switch (con->dst_addr.plain.sa_family) {
#ifdef HAVE_IPV6
- s = inet_ntop(con->dst_addr.plain.sa_family,
- con->dst_addr.plain.sa_family == AF_INET6 ?
- (const void *) &(con->dst_addr.ipv6.sin6_addr) :
- (const void *) &(con->dst_addr.ipv4.sin_addr),
- b2, sizeof(b2)-1);
+ case AF_INET6:
+ s = inet_ntop(con->dst_addr.plain.sa_family,
+ (const void *) &(con->dst_addr.ipv6.sin6_addr),
+ b2, sizeof(b2)-1);
+ break;
+ case AF_INET:
+ s = inet_ntop(con->dst_addr.plain.sa_family,
+ (const void *) &(con->dst_addr.ipv4.sin_addr),
+ b2, sizeof(b2)-1);
+ break;
#else
- s = inet_ntoa(con->dst_addr.ipv4.sin_addr);
+ case AF_INET:
+ s = inet_ntoa(con->dst_addr.ipv4.sin_addr);
+ break;
#endif
+ default:
+ s = "";
+ break;
+ }
cgi_env_add(&env, CONST_STR_LEN("REMOTE_ADDR"), s, strlen(s));
LI_ltostr(buf,
@@ -912,7 +936,7 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
buffer_reset(p->tmp_buf);
if (0 != strcasecmp(ds->key->ptr, "CONTENT-TYPE")) {
- buffer_copy_string(p->tmp_buf, "HTTP_");
+ buffer_copy_string_len(p->tmp_buf, CONST_STR_LEN("HTTP_"));
p->tmp_buf->used--; /* strip \0 after HTTP_ */
}
@@ -949,7 +973,7 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
for (j = 0; j < ds->key->used - 1; j++) {
p->tmp_buf->ptr[p->tmp_buf->used++] =
- isalpha((unsigned char)ds->key->ptr[j]) ?
+ light_isalnum((unsigned char)ds->key->ptr[j]) ?
toupper((unsigned char)ds->key->ptr[j]) : '_';
}
p->tmp_buf->ptr[p->tmp_buf->used++] = '\0';
@@ -987,6 +1011,8 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
*c = '/';
}
+ openDevNull(STDERR_FILENO);
+
/* we don't need the client socket */
for (i = 3; i < 256; i++) {
if (i != srv->errorlog_fd) close(i);
@@ -995,7 +1021,7 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
/* exec the cgi */
execve(args[0], args, env.ptr);
- log_error_write(srv, __FILE__, __LINE__, "sss", "CGI failed:", strerror(errno), args[0]);
+ /* log_error_write(srv, __FILE__, __LINE__, "sss", "CGI failed:", strerror(errno), args[0]); */
/* */
SEGFAULT();
@@ -1058,8 +1084,9 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
switch(errno) {
case ENOSPC:
con->http_status = 507;
-
break;
+ case EINTR:
+ continue;
default:
con->http_status = 403;
break;
@@ -1071,8 +1098,9 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
switch(errno) {
case ENOSPC:
con->http_status = 507;
-
break;
+ case EINTR:
+ continue;
default:
con->http_status = 403;
break;
@@ -1087,6 +1115,8 @@ static int cgi_create_env(server *srv, connection *con, plugin_data *p, buffer *
c->offset += r;
cq->bytes_out += r;
} else {
+ log_error_write(srv, __FILE__, __LINE__, "ss", "write() failed due to: ", strerror(errno));
+ con->http_status = 500;
break;
}
chunkqueue_remove_finished_chunks(cq);
@@ -1174,6 +1204,8 @@ URIHANDLER_FUNC(cgi_is_handled) {
plugin_data *p = p_d;
buffer *fn = con->physical.path;
+ if (con->mode != DIRECT) return HANDLER_GO_ON;
+
if (fn->used == 0) return HANDLER_GO_ON;
mod_cgi_patch_connection(srv, con, p);
@@ -1189,6 +1221,7 @@ URIHANDLER_FUNC(cgi_is_handled) {
if (0 == strncmp(fn->ptr + s_len - ct_len, ds->key->ptr, ct_len)) {
if (cgi_create_env(srv, con, p, ds->value)) {
+ con->mode = DIRECT;
con->http_status = 500;
buffer_reset(con->physical.path);