Add LOG_LEVEL_ACTIONS to log the applying actions
[privoxy.git] / jcc.c
diff --git a/jcc.c b/jcc.c
index 636ebc0..8f35b32 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.367 2011/10/16 12:40:34 fabiankeil Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.384 2012/07/27 17:31:10 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -338,7 +338,7 @@ static void sig_handler(int the_signal)
       case SIGINT:
          log_error(LOG_LEVEL_INFO, "exiting by signal %d .. bye", the_signal);
 #if defined(unix)
-         if(pidfile)
+         if (pidfile)
          {
             unlink(pidfile);
          }
@@ -661,6 +661,34 @@ static const char *crunch_reason(const struct http_response *rsp)
 }
 
 
+/*********************************************************************
+ *
+ * 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
@@ -705,6 +733,7 @@ static void send_crunch_response(const struct client_state *csp, struct http_res
       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);
@@ -823,16 +852,11 @@ static void build_request_line(struct client_state *csp, const struct forward_sp
     * 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");
    }
 
    /*
@@ -891,12 +915,7 @@ static jb_err change_request_destination(struct client_state *csp)
    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;
@@ -1003,11 +1022,7 @@ void save_connection_destination(jb_socket sfd,
    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);
@@ -1020,11 +1035,7 @@ void save_connection_destination(jb_socket sfd,
    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
    {
@@ -1034,11 +1045,7 @@ void save_connection_destination(jb_socket sfd,
 
    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
    {
@@ -1193,11 +1200,20 @@ static char *get_request_line(struct client_state *csp)
    {
       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;
       }
 
@@ -1230,10 +1246,6 @@ static char *get_request_line(struct client_state *csp)
  * 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...)
  *
@@ -1400,12 +1412,7 @@ static jb_err receive_client_request(struct client_state *csp)
    /*
     * 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 */
@@ -1624,6 +1631,7 @@ static void chat(struct client_state *csp)
       return;
    }
 
+   log_applied_actions(csp->action);
    log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
 
    if (fwd->forward_host)
@@ -1865,8 +1873,9 @@ static void chat(struct client_state *csp)
                 */
                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;
             }
             /*
@@ -2444,6 +2453,8 @@ static void chat(struct client_state *csp)
  *********************************************************************/
 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;
@@ -2462,9 +2473,11 @@ static void prepare_csp_for_next_request(struct client_state *csp)
       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;
+   }
 }
 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
 
@@ -2515,7 +2528,9 @@ static void serve(struct client_state *csp)
          && ((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->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
+             || (csp->config->feature_flags &
+                RUNTIME_FEATURE_CONNECTION_SHARING));
 
       if (continue_chatting && !(csp->flags & CSP_FLAG_CRUNCHED))
       {
@@ -2542,22 +2557,24 @@ static void serve(struct client_state *csp)
 
       if (continue_chatting)
       {
-         unsigned int client_timeout;
+         unsigned int client_timeout = 1; /* XXX: Use something else here? */
 
-         if (csp->server_connection.sfd != JB_INVALID_SOCKET)
+         if (0 != (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE))
          {
-            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.",
-               csp->cfd, csp->server_connection.sfd, csp->server_connection.host);
-         }
-         else
-         {
-            client_timeout = 1; /* XXX: Use something else here? */
-            log_error(LOG_LEVEL_CONNECT,
-               "Waiting for the next client request on socket %d. "
-               "No server socket to keep open.", csp->cfd);
+            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.",
+                  csp->cfd, csp->server_connection.sfd, csp->server_connection.host);
+            }
+            else
+            {
+               log_error(LOG_LEVEL_CONNECT,
+                  "Waiting for the next client request on socket %d. "
+                  "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)
@@ -2569,9 +2586,12 @@ static void serve(struct client_state *csp)
          }
          else
          {
-            log_error(LOG_LEVEL_CONNECT,
-               "No additional client request received in time on socket %d.",
-                csp->cfd);
+            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)
                && (socket_is_still_alive(csp->server_connection.sfd)))
@@ -2640,6 +2660,10 @@ static void serve(struct client_state *csp)
 
    if (csp->cfd != JB_INVALID_SOCKET)
    {
+      log_error(LOG_LEVEL_CONNECT, "Closing client socket %d. "
+         "Keep-alive: %u, Socket alive: %u. Data available: %u.",
+         csp->cfd, 0 != (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE),
+         socket_is_still_alive(csp->cfd), data_is_available(csp->cfd, 0));
       close_socket(csp->cfd);
    }
 
@@ -2840,6 +2864,7 @@ static void initialize_mutexes(void)
 #ifndef HAVE_RANDOM
    privoxy_mutex_init(&rand_mutex);
 #endif /* ndef HAVE_RANDOM */
+
 #endif /* def MUTEX_LOCKS_AVAILABLE */
 }
 
@@ -2914,7 +2939,7 @@ int main(int argc, char **argv)
          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)
       {
@@ -2923,7 +2948,7 @@ int main(int argc, char **argv)
             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();
@@ -2940,7 +2965,7 @@ int main(int argc, char **argv)
          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);
@@ -2948,31 +2973,26 @@ int main(int argc, char **argv)
 
 #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)
          {
@@ -2997,13 +3017,13 @@ int main(int argc, char **argv)
          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;
       }
@@ -3030,7 +3050,7 @@ int main(int argc, char **argv)
    show_version(Argv[0]);
 
 #if defined(unix)
-   if ( *configfile != '/' )
+   if (*configfile != '/')
    {
       char cwd[BUFFER_SIZE];
       char *abs_file;
@@ -3040,21 +3060,15 @@ int main(int argc, char **argv)
       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;
    }
@@ -3088,10 +3102,9 @@ int main(int argc, char **argv)
 #if !defined(_WIN32) && !defined(__OS2__) && !defined(AMIGA)
 {
    int idx;
-   const int catched_signals[] = { SIGTERM, SIGINT, SIGHUP, 0 };
-   const int ignored_signals[] = { SIGPIPE, 0 };
+   const int catched_signals[] = { SIGTERM, SIGINT, SIGHUP };
 
-   for (idx = 0; catched_signals[idx] != 0; idx++)
+   for (idx = 0; idx < SZ(catched_signals); idx++)
    {
 #ifdef sun /* FIXME: Is it safe to check for HAVE_SIGSET instead? */
       if (sigset(catched_signals[idx], sig_handler) == SIG_ERR)
@@ -3103,12 +3116,9 @@ int main(int argc, char **argv)
       }
    }
 
-   for (idx = 0; ignored_signals[idx] != 0; idx++)
+   if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
    {
-      if (signal(ignored_signals[idx], SIG_IGN) == SIG_ERR)
-      {
-         log_error(LOG_LEVEL_FATAL, "Can't set ignore-handler for signal %d: %E", ignored_signals[idx]);
-      }
+      log_error(LOG_LEVEL_FATAL, "Can't set ignore-handler for SIGPIPE: %E");
    }
 
 }
@@ -3138,12 +3148,12 @@ int main(int argc, char **argv)
       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;
@@ -3151,13 +3161,13 @@ int main(int argc, char **argv)
           * 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 */
 
@@ -3537,7 +3547,7 @@ static void listen_loop(void)
          log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
 
 #ifdef AMIGA
-         if(!childs)
+         if (!childs)
          {
             exit(1);
          }
@@ -3696,7 +3706,7 @@ static void listen_loop(void)
                                            NP_StackSize, 200*1024,
                                            TAG_DONE);
 #endif
-         if(0 != child_id)
+         if (0 != child_id)
          {
             childs++;
             ((struct Task *)child_id)->tc_UserData = csp;
@@ -3750,7 +3760,7 @@ static void listen_loop(void)
             int child_status;
 #if !defined(_WIN32) && !defined(__CYGWIN__)
 
-            wait( &child_status );
+            wait(&child_status);
 
             /*
              * Evaluate child's return code: If the child has