+ * Returns : N/A
+ *
+ *********************************************************************/
+static void set_privoxy_variables(const struct client_state *csp)
+{
+ int i;
+ struct {
+ const char *name;
+ const char *value;
+ } env[] = {
+ { "PRIVOXY_URL", csp->http->url },
+ { "PRIVOXY_PATH", csp->http->path },
+ { "PRIVOXY_HOST", csp->http->host },
+ { "PRIVOXY_ORIGIN", csp->ip_addr_str },
+ { "PRIVOXY_LISTEN_ADDRESS", csp->listen_addr_str },
+ };
+
+ for (i = 0; i < SZ(env); i++)
+ {
+ if (setenv(env[i].name, env[i].value, 1))
+ {
+ log_error(LOG_LEVEL_ERROR, "Failed to set %s=%s: %E",
+ env[i].name, env[i].value);
+ }
+ }
+}
+
+
+/*********************************************************************
+ *
+ * Function : execute_external_filter
+ *
+ * Description : Pipe content into external filter and return the output
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : name = Name of the external filter to execute
+ * 3 : content = The original content to filter
+ * 4 : size = The size of the content buffer
+ *
+ * Returns : a pointer to the (newly allocated) modified buffer.
+ * or NULL if there were no hits or something went wrong
+ *
+ *********************************************************************/
+static char *execute_external_filter(const struct client_state *csp,
+ const char *name, char *content, size_t *size)
+{
+ char cmd[200];
+ char file_name[FILENAME_MAX];
+ FILE *fp;
+ char *filter_output;
+ int fd;
+ int ret;
+ size_t new_size;
+ const char *external_filter;
+
+ if (csp->config->temporary_directory == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "No temporary-directory configured. Can't execute filter: %s",
+ name);
+ return NULL;
+ }
+
+ external_filter = get_external_filter(csp, name);
+
+ if (sizeof(file_name) < snprintf(file_name, sizeof(file_name),
+ "%s/privoxy-XXXXXXXX", csp->config->temporary_directory))
+ {
+ log_error(LOG_LEVEL_ERROR, "temporary-directory path too long");
+ return NULL;
+ }
+
+ fd = mkstemp(file_name);
+ if (fd == -1)
+ {
+ log_error(LOG_LEVEL_ERROR, "mkstemp() failed to create %s: %E", file_name);
+ return NULL;
+ }
+
+ fp = fdopen(fd, "w");
+ if (fp == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR, "fdopen() failed: %E");
+ unlink(file_name);
+ return NULL;
+ }
+
+ /*
+ * The size may be zero if a previous filter discarded everything.
+ *
+ * This isn't necessary unintentional, so we just don't try
+ * to fwrite() nothing and let the user deal with the rest.
+ */
+ if ((*size != 0) && fwrite(content, *size, 1, fp) != 1)
+ {
+ log_error(LOG_LEVEL_ERROR, "fwrite(..., %lu, 1, ..) failed: %E", *size);
+ unlink(file_name);
+ fclose(fp);
+ return NULL;
+ }
+ fclose(fp);
+
+ if (sizeof(cmd) < snprintf(cmd, sizeof(cmd), "%s < %s", external_filter, file_name))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "temporary-directory or external filter path too long");
+ unlink(file_name);
+ return NULL;
+ }
+
+ log_error(LOG_LEVEL_RE_FILTER, "Executing '%s': %s", name, cmd);
+
+ /*
+ * The locking is necessary to prevent other threads
+ * from overwriting the environment variables before
+ * the popen fork. Afterwards this no longer matters.
+ */
+ privoxy_mutex_lock(&external_filter_mutex);
+ set_privoxy_variables(csp);
+ fp = popen(cmd, "r");
+ privoxy_mutex_unlock(&external_filter_mutex);
+ if (fp == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR, "popen(\"%s\", \"r\") failed: %E", cmd);
+ unlink(file_name);
+ return NULL;
+ }
+
+ /* Allocate at least one byte */
+ filter_output = malloc_or_die(*size + 1);
+
+ new_size = 0;
+ while (!feof(fp) && !ferror(fp))
+ {
+ size_t len;
+ /* Could be bigger ... */
+ enum { READ_LENGTH = 2048 };
+
+ if (new_size + READ_LENGTH >= *size)
+ {
+ char *p;
+
+ /* Could be considered wasteful if the content is 'large'. */
+ *size += (*size >= READ_LENGTH) ? *size : READ_LENGTH;
+
+ p = realloc(filter_output, *size);
+ if (p == NULL)
+ {
+ log_error(LOG_LEVEL_ERROR, "Out of memory while reading "
+ "external filter output. Using what we got so far.");
+ break;
+ }
+ filter_output = p;
+ }
+ assert(new_size + READ_LENGTH < *size);
+ len = fread(&filter_output[new_size], 1, READ_LENGTH, fp);
+ if (len > 0)
+ {
+ new_size += len;
+ }
+ }
+
+ ret = pclose(fp);
+ if (ret == -1)
+ {
+ log_error(LOG_LEVEL_ERROR, "Executing %s failed: %E", cmd);
+ }
+ else
+ {
+ log_error(LOG_LEVEL_RE_FILTER,
+ "Executing '%s' resulted in return value %d. "
+ "Read %lu of up to %lu bytes.", name, (ret >> 8), new_size, *size);
+ }
+
+ unlink(file_name);
+ *size = new_size;
+
+ return filter_output;
+
+}
+#endif /* def FEATURE_EXTERNAL_FILTERS */
+
+
+/*********************************************************************
+ *
+ * Function : pcrs_filter_request_body
+ *
+ * Description : Execute all text substitutions from all applying
+ * +client_body_filter actions on the given text buffer.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : data = Target data
+ * 3 : data_len = Target data len
+ *
+ * Returns : a pointer to the (newly allocated) modified buffer.
+ * or NULL if there were no hits or something went wrong
+ *
+ *********************************************************************/
+static char *pcrs_filter_request_body(const struct client_state *csp, const char *data, size_t *data_len)
+{
+ return pcrs_filter_impl(csp, FALSE, data, data_len);
+}
+
+
+/*********************************************************************
+ *
+ * Function : gif_deanimate_response
+ *
+ * Description : Deanimate the GIF image that has been accumulated in
+ * csp->iob->buf, set csp->content_length to the modified
+ * size and raise the CSP_FLAG_MODIFIED flag.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : a pointer to the (newly allocated) modified buffer.
+ * or NULL in case something went wrong.
+ *
+ *********************************************************************/
+#ifdef FUZZ
+char *gif_deanimate_response(struct client_state *csp)
+#else
+static char *gif_deanimate_response(struct client_state *csp)
+#endif
+{
+ struct binbuffer *in, *out;
+ char *p;
+ size_t size;
+
+ size = (size_t)(csp->iob->eod - csp->iob->cur);
+
+ in = zalloc_or_die(sizeof(*in));
+ out = zalloc_or_die(sizeof(*out));
+
+ in->buffer = csp->iob->cur;
+ in->size = size;
+
+ if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
+ {
+ log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
+ freez(in);
+ buf_free(out);
+ return(NULL);
+ }
+ else
+ {
+ if ((int)size == out->offset)
+ {
+ log_error(LOG_LEVEL_DEANIMATE, "GIF not changed.");
+ }
+ else
+ {
+ log_error(LOG_LEVEL_DEANIMATE,
+ "Success! GIF shrunk from %lu bytes to %lu.", size, out->offset);
+ }
+ csp->content_length = out->offset;
+ csp->flags |= CSP_FLAG_MODIFIED;
+ p = out->buffer;
+ freez(in);
+ freez(out);
+ return(p);
+ }
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_filter_function
+ *
+ * Description : Decides which content filter function has
+ * to be applied (if any). Only considers functions
+ * for internal filters which are mutually-exclusive.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : The content filter function to run, or
+ * NULL if no content filter is active
+ *
+ *********************************************************************/
+static filter_function_ptr get_filter_function(const struct client_state *csp)
+{
+ filter_function_ptr filter_function = NULL;
+
+ /*
+ * Choose the applying filter function based on
+ * the content type and action settings.
+ */
+ if ((csp->content_type & CT_TEXT) &&
+ (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER])))
+ {
+ filter_function = pcrs_filter_response_body;
+ }
+ else if ((csp->content_type & CT_GIF) &&
+ (csp->action->flags & ACTION_DEANIMATE))
+ {
+ filter_function = gif_deanimate_response;
+ }
+
+ return filter_function;
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_bytes_to_next_chunk_start
+ *
+ * Description : Returns the number of bytes to the start of the
+ * next chunk in the buffer.
+ *
+ * Parameters :
+ * 1 : buffer = Pointer to the text buffer
+ * 2 : size = Number of bytes in the buffer.
+ * 3 : offset = Where to expect the beginning of the next chunk.
+ *
+ * Returns : -1 if the size can't be determined or data is missing,
+ * otherwise the number of bytes to the start of the next chunk
+ * or 0 if the last chunk has been fully buffered.
+ *
+ *********************************************************************/
+static int get_bytes_to_next_chunk_start(char *buffer, size_t size, size_t offset)
+{
+ char *chunk_start;
+ char *p;
+ unsigned int chunk_size = 0;
+ int bytes_to_skip;
+
+ if (size <= offset || size < 5)
+ {
+ /*
+ * Not enough bytes bufferd to figure
+ * out the size of the next chunk.
+ */
+ return -1;
+ }
+
+ chunk_start = buffer + offset;
+
+ p = strstr(chunk_start, "\r\n");
+ if (NULL == p)
+ {
+ /*
+ * The line with the chunk-size hasn't been completely received
+ * yet (or is invalid).
+ */
+ log_error(LOG_LEVEL_RE_FILTER,
+ "Not enough or invalid data in buffer in chunk size line.");
+ return -1;
+ }
+
+ if (sscanf(chunk_start, "%x", &chunk_size) != 1)
+ {
+ /* XXX: Write test case to trigger this. */
+ log_error(LOG_LEVEL_ERROR, "Failed to parse chunk size. "
+ "Size: %lu, offset: %lu. Chunk size start: %N", size, offset,
+ (size - offset), chunk_start);
+ return -1;
+ }
+
+ /*
+ * To get to the start of the next chunk size we have to skip
+ * the line with the current chunk size followed by "\r\n" followd
+ * by the actual data and another "\r\n" following the data.
+ */
+ bytes_to_skip = (int)(p - chunk_start) + 2 + (int)chunk_size + 2;
+
+ if (bytes_to_skip <= 0)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Failed to figure out chunk offset. %u and %d seem dubious.",
+ chunk_size, bytes_to_skip);
+ return -1;
+ }
+ if (chunk_size == 0)
+ {
+ if (bytes_to_skip <= (size - offset))
+ {
+ return 0;
+ }
+ else
+ {
+ log_error(LOG_LEVEL_INFO,
+ "Last chunk detected but we're still missing data.");
+ return -1;
+ }
+ }
+
+ return bytes_to_skip;
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_bytes_missing_from_chunked_data
+ *
+ * Description : Figures out how many bytes of data we need to get
+ * to the start of the next chunk of data (XXX: terminology).
+ * Due to the nature of chunk-encoded data we can only see
+ * how many data is missing according to the last chunk size
+ * buffered.
+ *
+ * Parameters :
+ * 1 : buffer = Pointer to the text buffer
+ * 2 : size = Number of bytes in the buffer.
+ * 3 : offset = Where to expect the beginning of the next chunk.
+ *
+ * Returns : -1 if the data can't be parsed (yet),
+ * 0 if the buffer is complete or a
+ * number of bytes that is missing.
+ *
+ *********************************************************************/
+int get_bytes_missing_from_chunked_data(char *buffer, size_t size, size_t offset)
+{
+ int ret = -1;
+ int last_valid_offset = -1;
+
+ if (size < offset || size < 5)
+ {
+ /* Not enough data buffered yet */
+ return -1;
+ }
+
+ do
+ {
+ ret = get_bytes_to_next_chunk_start(buffer, size, offset);
+ if (ret == -1)
+ {
+ return last_valid_offset;
+ }
+ if (ret == 0)
+ {
+ return 0;
+ }
+ if (offset != 0)
+ {
+ last_valid_offset = (int)offset;
+ }
+ offset += (size_t)ret;
+ } while (offset < size);
+
+ return (int)offset;
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : chunked_data_is_complete
+ *
+ * Description : Detects if a buffer with chunk-encoded data looks
+ * complete.
+ *
+ * Parameters :
+ * 1 : buffer = Pointer to the text buffer
+ * 2 : size = Number of bytes in the buffer.
+ * 3 : offset = Where to expect the beginning of the
+ * first complete chunk.
+ *
+ * Returns : TRUE if it looks like the data is complete,
+ * FALSE otherwise.