-const char jcc_rcs[] = "$Id: jcc.c,v 1.371 2011/10/23 11:24:33 fabiankeil Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.416 2012/11/24 14:01:25 fabiankeil Exp $";
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/jcc.c,v $
* Purpose : Main file. Contains main() method, main loop, and
* the main connection-handling function.
*
- * Copyright : Written by and Copyright (C) 2001-2010 the
+ * Copyright : Written by and Copyright (C) 2001-2012 the
* Privoxy team. http://www.privoxy.org/
*
* Based on the Internet Junkbuster originally written
privoxy_mutex_t rand_mutex;
#endif /* ndef HAVE_RANDOM */
-#ifdef HAVE_STRTOK
-privoxy_mutex_t strtok_mutex;
-#endif /* def HAVE_STRTOK */
-
#endif /* def MUTEX_LOCKS_AVAILABLE */
#if defined(unix)
case SIGINT:
log_error(LOG_LEVEL_INFO, "exiting by signal %d .. bye", the_signal);
#if defined(unix)
- if(pidfile)
+ if (pidfile)
{
unlink(pidfile);
}
}
+/*********************************************************************
+ *
+ * Function : log_applied_actions
+ *
+ * Description : Logs the applied actions if LOG_LEVEL_ACTIONS is
+ * enabled.
+ *
+ * Parameters :
+ * 1 : actions = Current action spec to log
+ *
+ * Returns : Nothing.
+ *
+ *********************************************************************/
+static void log_applied_actions(const struct current_action_spec *actions)
+{
+ /*
+ * The conversion to text requires lots of memory allocations so
+ * we only do the conversion if the user is actually interested.
+ */
+ if (debug_level_is_enabled(LOG_LEVEL_ACTIONS))
+ {
+ char *actions_as_text = actions_to_line_of_text(actions);
+ log_error(LOG_LEVEL_ACTIONS, "%s", actions_as_text);
+ freez(actions_as_text);
+ }
+}
+
+
/*********************************************************************
*
* Function : send_crunch_response
status_code[3] = '\0';
/* Log that the request was crunched and why. */
+ log_applied_actions(csp->action);
log_error(LOG_LEVEL_CRUNCH, "%s: %s", crunch_reason(rsp), http->url);
log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" %s %u",
csp->ip_addr_str, http->ocmd, status_code, rsp->content_length);
* Downgrade http version from 1.1 to 1.0
* if +downgrade action applies.
*/
- if ( (csp->action->flags & ACTION_DOWNGRADE)
+ if ((csp->action->flags & ACTION_DOWNGRADE)
&& (!strcmpic(http->ver, "HTTP/1.1")))
{
freez(http->ver);
- http->ver = strdup("HTTP/1.0");
-
- if (http->ver == NULL)
- {
- log_error(LOG_LEVEL_FATAL, "Out of memory downgrading HTTP version");
- }
+ http->ver = strdup_or_die("HTTP/1.0");
}
/*
else
{
/* XXX: ocmd is a misleading name */
- http->ocmd = strdup(http->cmd);
- if (http->ocmd == NULL)
- {
- log_error(LOG_LEVEL_FATAL,
- "Out of memory copying rewritten HTTP request line");
- }
+ http->ocmd = strdup_or_die(http->cmd);
}
return err;
*/
csp->expected_content_length = 0;
content_length_known = TRUE;
+ csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
}
if (csp->http->status == 204 || csp->http->status == 304)
*/
csp->expected_content_length = 0;
content_length_known = TRUE;
+ csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
}
return (content_length_known && ((0 == csp->expected_content_length)
assert(NULL != http->host);
server_connection->sfd = sfd;
- server_connection->host = strdup(http->host);
- if (NULL == server_connection->host)
- {
- log_error(LOG_LEVEL_FATAL, "Out of memory saving socket.");
- }
+ server_connection->host = strdup_or_die(http->host);
server_connection->port = http->port;
assert(NULL != fwd);
server_connection->forwarder_type = fwd->type;
if (NULL != fwd->gateway_host)
{
- server_connection->gateway_host = strdup(fwd->gateway_host);
- if (NULL == server_connection->gateway_host)
- {
- log_error(LOG_LEVEL_FATAL, "Out of memory saving gateway_host.");
- }
+ server_connection->gateway_host = strdup_or_die(fwd->gateway_host);
}
else
{
if (NULL != fwd->forward_host)
{
- server_connection->forward_host = strdup(fwd->forward_host);
- if (NULL == server_connection->forward_host)
- {
- log_error(LOG_LEVEL_FATAL, "Out of memory saving forward_host.");
- }
+ server_connection->forward_host = strdup_or_die(fwd->forward_host);
}
else
{
* we do.
*
* Data that doesn't belong to the current request is
- * thrown away to let the client retry on a clean socket.
- *
- * XXX: This is a hack until we can deal with multiple
- * pipelined requests at the same time.
- *
+ * either thrown away to let the client retry on a clean
+ * socket, or stashed to be dealt with after the current
+ * request is served.
*
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
static void verify_request_length(struct client_state *csp)
{
unsigned long long buffered_request_bytes =
- (unsigned long long)(csp->iob->eod - csp->iob->cur);
+ (unsigned long long)(csp->client_iob->eod - csp->client_iob->cur);
if ((csp->expected_client_content_length != 0)
&& (buffered_request_bytes != 0))
}
else
{
- assert(csp->iob->eod > csp->iob->cur + csp->expected_client_content_length);
- csp->iob->eod = csp->iob->cur + csp->expected_client_content_length;
+ assert(csp->client_iob->eod > csp->client_iob->cur + csp->expected_client_content_length);
+ csp->client_iob->eod = csp->client_iob->cur + csp->expected_client_content_length;
log_error(LOG_LEVEL_CONNECT, "Reducing expected bytes to 0. "
"Marking the server socket tainted after throwing %llu bytes away.",
buffered_request_bytes - csp->expected_client_content_length);
}
if (!(csp->flags & CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ)
- && ((csp->iob->cur[0] != '\0') || (csp->expected_client_content_length != 0)))
+ && ((csp->client_iob->cur < csp->client_iob->eod)
+ || (csp->expected_client_content_length != 0)))
{
- csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
if (strcmpic(csp->http->gpc, "GET")
&& strcmpic(csp->http->gpc, "HEAD")
&& strcmpic(csp->http->gpc, "TRACE")
{
/* XXX: this is an incomplete hack */
csp->flags &= ~CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
- log_error(LOG_LEVEL_CONNECT,
- "There might be a request body. The connection will not be kept alive.");
+ log_error(LOG_LEVEL_CONNECT, "There better be a request body.");
}
else
{
- /* XXX: and so is this */
csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
- log_error(LOG_LEVEL_CONNECT,
- "Possible pipeline attempt detected. The connection will not "
- "be kept alive and we will only serve the first request.");
- /* Nuke the pipelined requests from orbit, just to be sure. */
- csp->iob->buf[0] = '\0';
- csp->iob->eod = csp->iob->cur = csp->iob->buf;
+
+ if ((csp->config->feature_flags & RUNTIME_FEATURE_TOLERATE_PIPELINING) == 0)
+ {
+ csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
+ log_error(LOG_LEVEL_CONNECT,
+ "Possible pipeline attempt detected. The connection will not "
+ "be kept alive and we will only serve the first request.");
+ /* Nuke the pipelined requests from orbit, just to be sure. */
+ clear_iob(csp->client_iob);
+ }
+ else
+ {
+ /*
+ * Keep the pipelined data around for now, we'll deal with
+ * it once we're done serving the current request.
+ */
+ csp->flags |= CSP_FLAG_PIPELINED_REQUEST_WAITING;
+ assert(csp->client_iob->eod >= csp->client_iob->cur);
+ log_error(LOG_LEVEL_CONNECT, "Complete client request followed by "
+ "%d bytes of pipelined data received.",
+ (int)(csp->client_iob->eod - csp->client_iob->cur));
+ }
}
}
else
* actually been reused.
*/
if ((csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE)
- && !(csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED))
+ && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
{
log_error(LOG_LEVEL_CONNECT,
"Marking the server socket %d tainted.",
memset(buf, 0, sizeof(buf));
+ if ((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) != 0)
+ {
+ /*
+ * If there are multiple pipelined requests waiting,
+ * the flag will be set again once the next request
+ * has been parsed.
+ */
+ csp->flags &= ~CSP_FLAG_PIPELINED_REQUEST_WAITING;
+
+ request_line = get_header(csp->client_iob);
+ if ((NULL != request_line) && ('\0' != *request_line))
+ {
+ return request_line;
+ }
+ else
+ {
+ log_error(LOG_LEVEL_CONNECT, "No complete request line "
+ "received yet. Continuing reading from %d.", csp->cfd);
+ }
+ }
+
do
{
if (!data_is_available(csp->cfd, csp->config->socket_timeout))
{
- log_error(LOG_LEVEL_CONNECT,
- "Stopped waiting for the request line. Timeout: %d.",
- csp->config->socket_timeout);
- write_socket(csp->cfd, CLIENT_CONNECTION_TIMEOUT_RESPONSE,
- strlen(CLIENT_CONNECTION_TIMEOUT_RESPONSE));
+ if (socket_is_still_alive(csp->cfd))
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "No request line on socket %d received in time. Timeout: %d.",
+ csp->cfd, csp->config->socket_timeout);
+ write_socket(csp->cfd, CLIENT_CONNECTION_TIMEOUT_RESPONSE,
+ strlen(CLIENT_CONNECTION_TIMEOUT_RESPONSE));
+ }
+ else
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "The client side of the connection on socket %d got "
+ "closed without sending a complete request line.", csp->cfd);
+ }
return NULL;
}
* If there is no memory left for buffering the
* request, there is nothing we can do but hang up
*/
- if (add_to_iob(csp, buf, len))
+ if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
{
return NULL;
}
- request_line = get_header(csp->iob);
+ request_line = get_header(csp->client_iob);
} while ((NULL != request_line) && ('\0' == *request_line));
* Description : Read the client's request (more precisely the
* client headers) and answer it if necessary.
*
- * Note that since we're not using select() we could get
- * blocked here if a client connected, then didn't say
- * anything!
- *
* Parameters :
* 1 : csp = Current client state (buffers, headers, etc...)
*
struct list header_list;
struct list *headers = &header_list;
+ /* We don't care if the arriving data is a valid HTTP request or not. */
+ csp->requests_received_total++;
+
http = csp->http;
memset(buf, 0, sizeof(buf));
init_list(headers);
for (;;)
{
- p = get_header(csp->iob);
+ p = get_header(csp->client_iob);
if (p == NULL)
{
return JB_ERR_PARSE;
}
- if (add_to_iob(csp, buf, len))
+ if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
{
/*
* If there is no memory left for buffering the
/*
* Save a copy of the original request for logging
*/
- http->ocmd = strdup(http->cmd);
- if (http->ocmd == NULL)
- {
- log_error(LOG_LEVEL_FATAL,
- "Out of memory copying HTTP request line");
- }
+ http->ocmd = strdup_or_die(http->cmd);
enlist(csp->headers, http->cmd);
/* Append the previously read headers */
/* Assume persistence until further notice */
csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
}
+
+ if (csp->http->ssl == 0)
+ {
+ csp->expected_client_content_length = get_expected_content_length(csp->headers);
+ verify_request_length(csp);
+ }
#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
err = sed(csp, FILTER_CLIENT_HEADERS);
return JB_ERR_PARSE;
}
-#ifdef FEATURE_CONNECTION_KEEP_ALIVE
- if (csp->http->ssl == 0)
- {
- verify_request_length(csp);
- }
-#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
-
return JB_ERR_OK;
}
struct http_response *rsp;
struct timeval timeout;
#ifdef FEATURE_CONNECTION_KEEP_ALIVE
- int watch_client_socket = 1;
+ int watch_client_socket;
#endif
memset(buf, 0, sizeof(buf));
return;
}
+ log_applied_actions(csp->action);
log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
if (fwd->forward_host)
&& connection_destination_matches(&csp->server_connection, http, fwd))
{
log_error(LOG_LEVEL_CONNECT,
- "Reusing server socket %u. Opened for %s.",
- csp->server_connection.sfd, csp->server_connection.host);
+ "Reusing server socket %d connected to %s. Total requests: %u.",
+ csp->server_connection.sfd, csp->server_connection.host,
+ csp->server_connection.requests_sent_total);
}
else
{
if (csp->server_connection.sfd != JB_INVALID_SOCKET)
{
- log_error(LOG_LEVEL_CONNECT,
- "Closing server socket %u. Opened for %s.",
- csp->server_connection.sfd, csp->server_connection.host);
- close_socket(csp->server_connection.sfd);
+#ifdef FEATURE_CONNECTION_SHARING
+ if (csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
+ {
+ remember_connection(&csp->server_connection);
+ }
+ else
+#endif /* def FEATURE_CONNECTION_SHARING */
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Closing server socket %d connected to %s. Total requests: %u.",
+ csp->server_connection.sfd, csp->server_connection.host,
+ csp->server_connection.requests_sent_total);
+ close_socket(csp->server_connection.sfd);
+ }
mark_connection_closed(&csp->server_connection);
}
#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
}
#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+ csp->server_connection.requests_sent_total++;
+
if (fwd->forward_host || (http->ssl == 0))
{
+ int write_failure;
hdr = list_to_text(csp->headers);
if (hdr == NULL)
{
* Write the client's (modified) header to the server
* (along with anything else that may be in the buffer)
*/
- if (write_socket(csp->server_connection.sfd, hdr, strlen(hdr))
- || (flush_socket(csp->server_connection.sfd, csp->iob) < 0))
+ write_failure = 0 != write_socket(csp->server_connection.sfd, hdr, strlen(hdr));
+ freez(hdr);
+
+ if (write_failure)
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Failed sending request headers to: %s: %E", http->hostport);
+ }
+ else if (((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) == 0)
+ && (flush_socket(csp->server_connection.sfd, csp->client_iob) < 0))
{
+ write_failure = 1;
log_error(LOG_LEVEL_CONNECT,
- "write header to: %s failed: %E", http->hostport);
+ "Failed sending request body to: %s: %E", http->hostport);
+ }
+ if (write_failure)
+ {
rsp = error_response(csp, "connect-failed");
if (rsp)
{
send_crunch_response(csp, rsp);
}
-
- freez(hdr);
return;
}
- freez(hdr);
}
else
{
{
return;
}
- IOB_RESET(csp);
+ clear_iob(csp->client_iob);
}
log_error(LOG_LEVEL_CONNECT, "to %s successful", http->hostport);
server_body = 0;
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+ watch_client_socket = 0 == (csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING);
+#endif
+
for (;;)
{
#ifdef __OS2__
*/
watch_client_socket = 0;
log_error(LOG_LEVEL_CONNECT,
- "Stopping to watch the client socket. "
- "There's already another request waiting.");
+ "Stopping to watch the client socket %d. "
+ "There's already another request waiting.",
+ csp->cfd);
continue;
}
/*
}
#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
- fflush(NULL);
len = read_socket(csp->server_connection.sfd, buf, sizeof(buf) - 1);
if (len < 0)
* has been reached, switch to non-filtering mode, i.e. make & write the
* header, flush the iob and buf, and get out of the way.
*/
- if (add_to_iob(csp, buf, len))
+ if (add_to_iob(csp->iob, csp->config->buffer_limit, buf, len))
{
size_t hdrlen;
long flushed;
* Buffer up the data we just read. If that fails, there's
* little we can do but send our static out-of-memory page.
*/
- if (add_to_iob(csp, buf, len))
+ if (add_to_iob(csp->iob, csp->config->buffer_limit, buf, len))
{
log_error(LOG_LEVEL_ERROR, "Out of memory while looking for end of server headers.");
rsp = cgi_error_memory();
* we can parse the headers we just continue here.
*/
log_error(LOG_LEVEL_CONNECT,
- "Continuing buffering headers. Bytes most recently read: %d.",
- len);
+ "Continuing buffering server headers from socket %d. "
+ "Bytes most recently read: %d.", csp->cfd, len);
continue;
}
}
*********************************************************************/
static void prepare_csp_for_next_request(struct client_state *csp)
{
+ unsigned int toggled_on_flag_set = (0 != (csp->flags & CSP_FLAG_TOGGLED_ON));
+
csp->content_type = 0;
csp->content_length = 0;
csp->expected_content_length = 0;
csp->expected_client_content_length = 0;
list_remove_all(csp->headers);
- freez(csp->iob->buf);
- memset(csp->iob, 0, sizeof(csp->iob));
+ clear_iob(csp->iob);
freez(csp->error_message);
free_http_request(csp->http);
destroy_list(csp->headers);
csp->fwd = NULL;
}
/* XXX: Store per-connection flags someplace else. */
- csp->flags &= CSP_FLAG_TOGGLED_ON;
- csp->flags |= CSP_FLAG_ACTIVE;
- csp->flags |= CSP_FLAG_REUSED_CLIENT_CONNECTION;
+ csp->flags = (CSP_FLAG_ACTIVE | CSP_FLAG_REUSED_CLIENT_CONNECTION);
+ if (toggled_on_flag_set)
+ {
+ csp->flags |= CSP_FLAG_TOGGLED_ON;
+ }
+
+ if (csp->client_iob->eod > csp->client_iob->cur)
+ {
+ long bytes_to_shift = csp->client_iob->cur - csp->client_iob->buf;
+ size_t data_length = (size_t)(csp->client_iob->eod - csp->client_iob->cur);
+
+ assert(bytes_to_shift > 0);
+ assert(data_length > 0);
+
+ log_error(LOG_LEVEL_CONNECT, "Shifting %d pipelined bytes by %d bytes",
+ data_length, bytes_to_shift);
+ memmove(csp->client_iob->buf, csp->client_iob->cur, data_length);
+ csp->client_iob->cur = csp->client_iob->buf;
+ assert(csp->client_iob->eod == csp->client_iob->buf + bytes_to_shift + data_length);
+ csp->client_iob->eod = csp->client_iob->buf + data_length;
+ memset(csp->client_iob->eod, '\0', (size_t)bytes_to_shift);
+
+ csp->flags |= CSP_FLAG_PIPELINED_REQUEST_WAITING;
+ }
+ else
+ {
+ /*
+ * We mainly care about resetting client_iob->cur so we don't
+ * waste buffer space at the beginning and don't mess up the
+ * request restoration done by cgi_show_request().
+ *
+ * Freeing the buffer itself isn't technically necessary,
+ * but makes debugging more convenient.
+ */
+ clear_iob(csp->client_iob);
+ }
}
#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
static void serve(struct client_state *csp)
#endif /* def AMIGA */
{
+ int config_file_change_detected = 0; /* Only used for debugging */
#ifdef FEATURE_CONNECTION_KEEP_ALIVE
#ifdef FEATURE_CONNECTION_SHARING
static int monitor_thread_running = 0;
#endif /* def FEATURE_CONNECTION_SHARING */
int continue_chatting = 0;
+ log_error(LOG_LEVEL_CONNECT, "Accepted connection from %s on socket %d",
+ csp->ip_addr_str, csp->cfd);
+
do
{
unsigned int latency;
- int config_file_change_detected = 0; /* Only used for debugging */
chat(csp);
latency = (unsigned)(csp->server_connection.response_received -
csp->server_connection.request_sent) / 2;
+ if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
+ && (csp->flags & CSP_FLAG_CRUNCHED)
+ && (csp->expected_client_content_length != 0))
+ {
+ csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
+ log_error(LOG_LEVEL_CONNECT,
+ "Tainting client socket %d due to unread data.", csp->cfd);
+ }
+
continue_chatting = (csp->config->feature_flags
& RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
&& !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
- && ((csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE)
- || (csp->flags & CSP_FLAG_CRUNCHED))
&& (csp->cfd != JB_INVALID_SOCKET)
- && ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
- || (csp->config->feature_flags &
- RUNTIME_FEATURE_CONNECTION_SHARING));
+ && (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
+ && ((csp->flags & CSP_FLAG_SERVER_CONTENT_LENGTH_SET)
+ || (csp->flags & CSP_FLAG_CHUNKED));
- if (continue_chatting && !(csp->flags & CSP_FLAG_CRUNCHED))
+ if (!(csp->flags & CSP_FLAG_CRUNCHED)
+ && (csp->server_connection.sfd != JB_INVALID_SOCKET))
{
- continue_chatting = (csp->server_connection.sfd != JB_INVALID_SOCKET)
- && socket_is_still_alive(csp->server_connection.sfd);
- if (continue_chatting)
+ if (!(csp->flags & CSP_FLAG_SERVER_KEEP_ALIVE_TIMEOUT_SET))
{
- if (!(csp->flags & CSP_FLAG_SERVER_KEEP_ALIVE_TIMEOUT_SET))
+ csp->server_connection.keep_alive_timeout = csp->config->default_server_timeout;
+ }
+ if (!(csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE)
+ || (csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
+ || !socket_is_still_alive(csp->server_connection.sfd)
+ || !(latency < csp->server_connection.keep_alive_timeout))
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Closing server socket %d connected to %s. "
+ "Keep-alive %u. Tainted: %u. Socket alive %u. Timeout: %u.",
+ csp->server_connection.sfd, csp->server_connection.host,
+ 0 != (csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE),
+ 0 != (csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED),
+ socket_is_still_alive(csp->server_connection.sfd),
+ csp->server_connection.keep_alive_timeout);
+#ifdef FEATURE_CONNECTION_SHARING
+ if (csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
{
- csp->server_connection.keep_alive_timeout = csp->config->default_server_timeout;
- log_error(LOG_LEVEL_CONNECT,
- "The server didn't specify how long the connection will stay open. "
- "Assumed timeout is: %u.", csp->server_connection.keep_alive_timeout);
+ forget_connection(csp->server_connection.sfd);
}
- continue_chatting = (latency < csp->server_connection.keep_alive_timeout);
+#endif /* def FEATURE_CONNECTION_SHARING */
+ close_socket(csp->server_connection.sfd);
+ mark_connection_closed(&csp->server_connection);
}
}
if (continue_chatting)
{
- unsigned int client_timeout = 1; /* XXX: Use something else here? */
+ if (((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) != 0)
+ && socket_is_still_alive(csp->cfd))
+ {
+ log_error(LOG_LEVEL_CONNECT, "Client request %d has been "
+ "pipelined on socket %d and the socket is still alive.",
+ csp->requests_received_total+1, csp->cfd);
+ prepare_csp_for_next_request(csp);
+ continue;
+ }
if (0 != (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE))
{
if (csp->server_connection.sfd != JB_INVALID_SOCKET)
{
- client_timeout = (unsigned)csp->server_connection.keep_alive_timeout - latency;
log_error(LOG_LEVEL_CONNECT,
"Waiting for the next client request on socket %d. "
"Keeping the server socket %d to %s open.",
"No server socket to keep open.", csp->cfd);
}
}
+
if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
- && data_is_available(csp->cfd, (int)client_timeout)
+ && data_is_available(csp->cfd, (int)csp->config->keep_alive_timeout)
&& socket_is_still_alive(csp->cfd))
{
log_error(LOG_LEVEL_CONNECT,
- "Client request arrived in time on socket %d.", csp->cfd);
+ "Client request %u arrived in time on socket %d.",
+ csp->requests_received_total+1, csp->cfd);
prepare_csp_for_next_request(csp);
}
else
{
- if (0 != (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE))
- {
- log_error(LOG_LEVEL_CONNECT,
- "No additional client request received in time on socket %d.",
- csp->cfd);
- }
#ifdef FEATURE_CONNECTION_SHARING
if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
+ && (csp->server_connection.sfd != JB_INVALID_SOCKET)
&& (socket_is_still_alive(csp->server_connection.sfd)))
{
time_t time_open = time(NULL) - csp->server_connection.timestamp;
remember_connection(&csp->server_connection);
csp->server_connection.sfd = JB_INVALID_SOCKET;
- close_socket(csp->cfd);
+ drain_and_close_socket(csp->cfd);
csp->cfd = JB_INVALID_SOCKET;
privoxy_mutex_lock(&connection_reuse_mutex);
if (!monitor_thread_running)
else if (csp->server_connection.sfd != JB_INVALID_SOCKET)
{
log_error(LOG_LEVEL_CONNECT,
- "The connection on server socket %d to %s isn't reusable. Closing. "
- "Server connection: keep-alive %u, tainted: %u, socket alive %u. "
- "Client connection: socket alive: %u. Server timeout: %u. "
+ "Closing server socket %d connected to %s. Keep-alive: %u. "
+ "Tainted: %u. Socket alive: %u. Timeout: %u. "
"Configuration file change detected: %u",
csp->server_connection.sfd, csp->server_connection.host,
0 != (csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE),
0 != (csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED),
socket_is_still_alive(csp->server_connection.sfd),
- socket_is_still_alive(csp->cfd),
csp->server_connection.keep_alive_timeout,
config_file_change_detected);
}
if (csp->cfd != JB_INVALID_SOCKET)
{
- close_socket(csp->cfd);
+ log_error(LOG_LEVEL_CONNECT, "Closing client socket %d. "
+ "Keep-alive: %u. Socket alive: %u. Data available: %u. "
+ "Configuration file change detected: %u. Requests received: %u.",
+ csp->cfd, 0 != (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE),
+ socket_is_still_alive(csp->cfd), data_is_available(csp->cfd, 0),
+ config_file_change_detected, csp->requests_received_total);
+ drain_and_close_socket(csp->cfd);
}
csp->flags &= ~CSP_FLAG_ACTIVE;
static void usage(const char *myname)
{
printf("Privoxy version " VERSION " (" HOME_PAGE_URL ")\n"
- "Usage: %s "
+ "Usage: %s [--config-test] "
#if defined(unix)
"[--chroot] "
#endif /* defined(unix) */
privoxy_mutex_init(&rand_mutex);
#endif /* ndef HAVE_RANDOM */
-#ifdef HAVE_STRTOK
- privoxy_mutex_init(&strtok_mutex);
-#endif /* def HAVE_STRTOK */
#endif /* def MUTEX_LOCKS_AVAILABLE */
}
#endif
{
int argc_pos = 0;
+ int do_config_test = 0;
unsigned int random_seed;
#ifdef unix
struct passwd *pw = NULL;
const char *pName = argv[argc_pos] + 9;
if (*pName == ':')
pName++;
- exit( (install_service(pName)) ? 0 : 1 );
+ exit((install_service(pName)) ? 0 : 1);
}
else if (strncmp(argv[argc_pos], "--uninstall", 11) == 0)
{
pName++;
exit((uninstall_service(pName)) ? 0 : 1);
}
- else if (strcmp(argv[argc_pos], "--service" ) == 0)
+ else if (strcmp(argv[argc_pos], "--service") == 0)
{
bRunAsService = TRUE;
w32_set_service_cwd();
usage(argv[0]);
}
- else if(strcmp(argv[argc_pos], "--version") == 0)
+ else if (strcmp(argv[argc_pos], "--version") == 0)
{
printf("Privoxy version " VERSION " (" HOME_PAGE_URL ")\n");
exit(0);
#if defined(unix)
- else if (strcmp(argv[argc_pos], "--no-daemon" ) == 0)
+ else if (strcmp(argv[argc_pos], "--no-daemon") == 0)
{
set_debug_level(LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO);
daemon_mode = 0;
}
- else if (strcmp(argv[argc_pos], "--pidfile" ) == 0)
+ else if (strcmp(argv[argc_pos], "--pidfile") == 0)
{
if (++argc_pos == argc) usage(argv[0]);
- pidfile = strdup(argv[argc_pos]);
+ pidfile = strdup_or_die(argv[argc_pos]);
}
- else if (strcmp(argv[argc_pos], "--user" ) == 0)
+ else if (strcmp(argv[argc_pos], "--user") == 0)
{
char *user_arg;
char *group_name;
if (++argc_pos == argc) usage(argv[argc_pos]);
- user_arg = strdup(argv[argc_pos]);
- if (NULL == user_arg)
- {
- log_error(LOG_LEVEL_FATAL,
- "Out of memory splitting --user argument '%s'.", argv[argc_pos]);
- }
+ user_arg = strdup_or_die(argv[argc_pos]);
group_name = strchr(user_arg, '.');
if (NULL != group_name)
{
freez(user_arg);
}
- else if (strcmp(argv[argc_pos], "--pre-chroot-nslookup" ) == 0)
+ else if (strcmp(argv[argc_pos], "--pre-chroot-nslookup") == 0)
{
if (++argc_pos == argc) usage(argv[0]);
- pre_chroot_nslookup_to_load_resolver = strdup(argv[argc_pos]);
+ pre_chroot_nslookup_to_load_resolver = strdup_or_die(argv[argc_pos]);
}
- else if (strcmp(argv[argc_pos], "--chroot" ) == 0)
+ else if (strcmp(argv[argc_pos], "--chroot") == 0)
{
do_chroot = 1;
}
#endif /* defined(unix) */
+ else if (strcmp(argv[argc_pos], "--config-test") == 0)
+ {
+ do_config_test = 1;
+ }
+
else if (argc_pos + 1 != argc)
{
/*
show_version(Argv[0]);
#if defined(unix)
- if ( *configfile != '/' )
+ if (*configfile != '/')
{
char cwd[BUFFER_SIZE];
char *abs_file;
if (NULL == getcwd(cwd, sizeof(cwd)))
{
perror("failed to get current working directory");
- exit( 1 );
+ exit(1);
}
+ basedir = strdup_or_die(cwd);
/* XXX: why + 5? */
abs_file_size = strlen(cwd) + strlen(configfile) + 5;
- basedir = strdup(cwd);
-
- if (NULL == basedir ||
- NULL == (abs_file = malloc(abs_file_size)))
- {
- perror("malloc failed");
- exit( 1 );
- }
+ abs_file = malloc_or_die(abs_file_size);
strlcpy(abs_file, basedir, abs_file_size);
- strlcat(abs_file, "/", abs_file_size );
+ strlcat(abs_file, "/", abs_file_size);
strlcat(abs_file, configfile, abs_file_size);
configfile = abs_file;
}
# endif /* def _WIN_CONSOLE */
#endif /* def _WIN32 */
+ if (do_config_test)
+ {
+ exit(NULL == load_config());
+ }
/* Initialize the CGI subsystem */
cgi_init_error_messages();
int fd;
pid_t pid = fork();
- if ( pid < 0 ) /* error */
+ if (pid < 0) /* error */
{
perror("fork");
- exit( 3 );
+ exit(3);
}
- else if ( pid != 0 ) /* parent */
+ else if (pid != 0) /* parent */
{
int status;
pid_t wpid;
* must check for errors
* child died due to missing files aso
*/
- sleep( 1 );
- wpid = waitpid( pid, &status, WNOHANG );
- if ( wpid != 0 )
+ sleep(1);
+ wpid = waitpid(pid, &status, WNOHANG);
+ if (wpid != 0)
{
- exit( 1 );
+ exit(1);
}
- exit( 0 );
+ exit(0);
}
/* child */
log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
#ifdef AMIGA
- if(!childs)
+ if (!childs)
{
exit(1);
}
freez(csp_list);
continue;
}
- else
- {
- log_error(LOG_LEVEL_CONNECT,
- "accepted connection from %s on socket %d",
- csp->ip_addr_str, csp->cfd);
- }
csp->flags |= CSP_FLAG_ACTIVE;
csp->server_connection.sfd = JB_INVALID_SOCKET;
NP_StackSize, 200*1024,
TAG_DONE);
#endif
- if(0 != child_id)
+ if (0 != child_id)
{
childs++;
((struct Task *)child_id)->tc_UserData = csp;
int child_status;
#if !defined(_WIN32) && !defined(__CYGWIN__)
- wait( &child_status );
+ wait(&child_status);
/*
* Evaluate child's return code: If the child has