Privoxy is going to shut down after the next request.
\n" + "\n" + "\n"; + assert(csp); assert(rsp); assert(parameters); @@ -325,12 +193,16 @@ jb_err cgi_die (struct client_state *csp, /* quit */ g_terminate = 1; - /* - * I don't really care what gets sent back to the browser. - * Take the easy option - "out of memory" page. - */ + csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE; + + rsp->content_length = 0; + rsp->head_length = 0; + rsp->is_static = 0; + + rsp->body = strdup_or_die(body); + rsp->status = strdup_or_die(status); - return JB_ERR_MEMORY; + return JB_ERR_OK; } #endif /* def FEATURE_GRACEFUL_TERMINATION */ @@ -341,7 +213,7 @@ jb_err cgi_die (struct client_state *csp, * * Description : Show the client's request and what sed() would have * made of it. - * + * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) * 2 : rsp = http_response data structure for output @@ -350,7 +222,7 @@ jb_err cgi_die (struct client_state *csp, * CGI Parameters : none * * Returns : JB_ERR_OK on success - * JB_ERR_MEMORY on out-of-memory error. + * JB_ERR_MEMORY on out-of-memory error. * *********************************************************************/ jb_err cgi_show_request(struct client_state *csp, @@ -368,11 +240,11 @@ jb_err cgi_show_request(struct client_state *csp, { return JB_ERR_MEMORY; } - + /* * Repair the damage done to the IOB by get_header() */ - for (p = csp->iob->buf; p < csp->iob->eod; p++) + for (p = csp->client_iob->buf; p < csp->client_iob->cur; p++) { if (*p == '\0') *p = '\n'; } @@ -382,14 +254,14 @@ jb_err cgi_show_request(struct client_state *csp, * be sending to the server if this wasn't a CGI call */ - if (map(exports, "client-request", 1, html_encode(csp->iob->buf), 0)) + if (map(exports, "client-request", 1, html_encode(csp->client_iob->buf), 0)) { free_map(exports); return JB_ERR_MEMORY; } - if (map(exports, "processed-request", 1, html_encode_and_free_original( - sed(client_patterns, add_client_headers, csp)), 0)) + if (map(exports, "processed-request", 1, + html_encode_and_free_original(list_to_text(csp->headers)), 0)) { free_map(exports); return JB_ERR_MEMORY; @@ -403,7 +275,7 @@ jb_err cgi_show_request(struct client_state *csp, * * Function : cgi_send_banner * - * Description : CGI function that returns a banner. + * Description : CGI function that returns a banner. * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) @@ -414,10 +286,11 @@ jb_err cgi_show_request(struct client_state *csp, * type : Selects the type of banner between "trans", "logo", * and "auto". Defaults to "logo" if absent or invalid. * "auto" means to select as if we were image-blocking. - * (Only the first character really counts). + * (Only the first character really counts; b and t are + * equivalent). * * Returns : JB_ERR_OK on success - * JB_ERR_MEMORY on out-of-memory error. + * JB_ERR_MEMORY on out-of-memory error. * *********************************************************************/ jb_err cgi_send_banner(struct client_state *csp, @@ -426,20 +299,24 @@ jb_err cgi_send_banner(struct client_state *csp, { char imagetype = lookup(parameters, "type")[0]; - if (imagetype == 'a') /* auto */ + /* + * If type is auto, then determine the right thing + * to do from the set-image-blocker action + */ + if (imagetype == 'a') { - /* Default to pattern */ + /* + * Default to pattern + */ imagetype = 'p'; + #ifdef FEATURE_IMAGE_BLOCKING if ((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0) { static const char prefix1[] = CGI_PREFIX "send-banner?type="; static const char prefix2[] = "http://" CGI_SITE_1_HOST "/send-banner?type="; + const char *p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER]; - /* determine HOW images should be blocked */ - const char * p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER]; - - /* and handle accordingly: */ if (p == NULL) { /* Use default - nothing to do here. */ @@ -452,6 +329,11 @@ jb_err cgi_send_banner(struct client_state *csp, { imagetype = 'p'; } + + /* + * If the action is to call this CGI, determine + * the argument: + */ else if (0 == strncmpic(p, prefix1, sizeof(prefix1) - 1)) { imagetype = p[sizeof(prefix1) - 1]; @@ -460,21 +342,90 @@ jb_err cgi_send_banner(struct client_state *csp, { imagetype = p[sizeof(prefix2) - 1]; } + + /* + * Everything else must (should) be a URL to + * redirect to. + */ + else + { + imagetype = 'r'; + } } #endif /* def FEATURE_IMAGE_BLOCKING */ } - - if ((imagetype == 'b') || (imagetype == 't')) /* blank / transparent */ - { - rsp->body = bindup(image_blank_data, image_blank_length); - rsp->content_length = image_blank_length; + /* + * Now imagetype is either the non-auto type we were called with, + * or it was auto and has since been determined. In any case, we + * can proceed to actually answering the request by sending a redirect + * or an image as appropriate: + */ + if (imagetype == 'r') + { + rsp->status = strdup_or_die("302 Local Redirect from Privoxy"); + if (enlist_unique_header(rsp->headers, "Location", + csp->action->string[ACTION_STRING_IMAGE_BLOCKER])) + { + return JB_ERR_MEMORY; + } } - else /* pattern */ + else { - rsp->body = bindup(image_pattern_data, image_pattern_length); - rsp->content_length = image_pattern_length; - } + if ((imagetype == 'b') || (imagetype == 't')) + { + rsp->body = bindup(image_blank_data, image_blank_length); + rsp->content_length = image_blank_length; + } + else + { + rsp->body = bindup(image_pattern_data, image_pattern_length); + rsp->content_length = image_pattern_length; + } + + if (rsp->body == NULL) + { + return JB_ERR_MEMORY; + } + if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE)) + { + return JB_ERR_MEMORY; + } + + rsp->is_static = 1; + } + + return JB_ERR_OK; + +} + + +/********************************************************************* + * + * Function : cgi_transparent_image + * + * Description : CGI function that sends a 1x1 transparent image. + * + * Parameters : + * 1 : csp = Current client state (buffers, headers, etc...) + * 2 : rsp = http_response data structure for output + * 3 : parameters = map of cgi parameters + * + * CGI Parameters : None + * + * Returns : JB_ERR_OK on success + * JB_ERR_MEMORY on out-of-memory error. + * + *********************************************************************/ +jb_err cgi_transparent_image(struct client_state *csp, + struct http_response *rsp, + const struct map *parameters) +{ + (void)csp; + (void)parameters; + + rsp->body = bindup(image_blank_data, image_blank_length); + rsp->content_length = image_blank_length; if (rsp->body == NULL) { @@ -495,9 +446,9 @@ jb_err cgi_send_banner(struct client_state *csp, /********************************************************************* * - * Function : cgi_transparent_image + * Function : cgi_send_default_favicon * - * Description : CGI function that sends a 1x1 transparent image. + * Description : CGI function that sends the standard favicon. * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) @@ -507,22 +458,103 @@ jb_err cgi_send_banner(struct client_state *csp, * CGI Parameters : None * * Returns : JB_ERR_OK on success - * JB_ERR_MEMORY on out-of-memory error. + * JB_ERR_MEMORY on out-of-memory error. * *********************************************************************/ -jb_err cgi_transparent_image(struct client_state *csp, - struct http_response *rsp, - const struct map *parameters) +jb_err cgi_send_default_favicon(struct client_state *csp, + struct http_response *rsp, + const struct map *parameters) { - rsp->body = bindup(image_blank_data, image_blank_length); - rsp->content_length = image_blank_length; + static const char default_favicon_data[] = + "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260" + "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000" + "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000" + "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000" + "\000\000\377\377\377\000\377\000\052\000\017\360\000\000\077" + "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000" + "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000" + "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036" + "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360" + "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000" + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360" + "\017\000\000"; + static const size_t favicon_length = sizeof(default_favicon_data) - 1; + + (void)csp; + (void)parameters; + + rsp->body = bindup(default_favicon_data, favicon_length); + rsp->content_length = favicon_length; if (rsp->body == NULL) { return JB_ERR_MEMORY; } - if (enlist(rsp->headers, "Content-Type: " BUILTIN_IMAGE_MIMETYPE)) + if (enlist(rsp->headers, "Content-Type: image/x-icon")) + { + return JB_ERR_MEMORY; + } + + rsp->is_static = 1; + + return JB_ERR_OK; + +} + + +/********************************************************************* + * + * Function : cgi_send_error_favicon + * + * Description : CGI function that sends the favicon for error pages. + * + * Parameters : + * 1 : csp = Current client state (buffers, headers, etc...) + * 2 : rsp = http_response data structure for output + * 3 : parameters = map of cgi parameters + * + * CGI Parameters : None + * + * Returns : JB_ERR_OK on success + * JB_ERR_MEMORY on out-of-memory error. + * + *********************************************************************/ +jb_err cgi_send_error_favicon(struct client_state *csp, + struct http_response *rsp, + const struct map *parameters) +{ + static const char error_favicon_data[] = + "\000\000\001\000\001\000\020\020\002\000\000\000\000\000\260" + "\000\000\000\026\000\000\000\050\000\000\000\020\000\000\000" + "\040\000\000\000\001\000\001\000\000\000\000\000\100\000\000" + "\000\000\000\000\000\000\000\000\000\002\000\000\000\000\000" + "\000\000\377\377\377\000\000\000\377\000\017\360\000\000\077" + "\374\000\000\161\376\000\000\161\376\000\000\361\377\000\000" + "\361\377\000\000\360\017\000\000\360\007\000\000\361\307\000" + "\000\361\307\000\000\361\307\000\000\360\007\000\000\160\036" + "\000\000\177\376\000\000\077\374\000\000\017\360\000\000\360" + "\017\000\000\300\003\000\000\200\001\000\000\200\001\000\000" + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + "\000\000\200\001\000\000\200\001\000\000\300\003\000\000\360" + "\017\000\000"; + static const size_t favicon_length = sizeof(error_favicon_data) - 1; + + (void)csp; + (void)parameters; + + rsp->body = bindup(error_favicon_data, favicon_length); + rsp->content_length = favicon_length; + + if (rsp->body == NULL) + { + return JB_ERR_MEMORY; + } + + if (enlist(rsp->headers, "Content-Type: image/x-icon")) { return JB_ERR_MEMORY; } @@ -549,7 +581,7 @@ jb_err cgi_transparent_image(struct client_state *csp, * CGI Parameters : None * * Returns : JB_ERR_OK on success - * JB_ERR_MEMORY on out-of-memory error. + * JB_ERR_MEMORY on out-of-memory error. * *********************************************************************/ jb_err cgi_send_stylesheet(struct client_state *csp, @@ -557,10 +589,12 @@ jb_err cgi_send_stylesheet(struct client_state *csp, const struct map *parameters) { jb_err err; - + assert(csp); assert(rsp); + (void)parameters; + err = template_load(csp, &rsp->body, "cgi-style.css", 0); if (err == JB_ERR_FILE) @@ -587,166 +621,278 @@ jb_err cgi_send_stylesheet(struct client_state *csp, /********************************************************************* * - * Function : cgi_show_version + * Function : cgi_send_url_info_osd * - * Description : CGI function that returns a a web page describing the - * file versions of Privoxy. + * Description : CGI function that sends the OpenSearch Description + * template for the show-url-info page. It allows to + * access the page through "search engine plugins". * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) * 2 : rsp = http_response data structure for output * 3 : parameters = map of cgi parameters * - * CGI Parameters : none + * CGI Parameters : None * * Returns : JB_ERR_OK on success - * JB_ERR_MEMORY on out-of-memory error. + * JB_ERR_MEMORY on out-of-memory error. * *********************************************************************/ -jb_err cgi_show_version(struct client_state *csp, - struct http_response *rsp, - const struct map *parameters) +jb_err cgi_send_url_info_osd(struct client_state *csp, + struct http_response *rsp, + const struct map *parameters) { - struct map *exports; + jb_err err = JB_ERR_MEMORY; + struct map *exports = default_exports(csp, NULL); - assert(csp); - assert(rsp); - assert(parameters); + (void)csp; + (void)parameters; - if (NULL == (exports = default_exports(csp, "show-version"))) + if (NULL != exports) { - return JB_ERR_MEMORY; + err = template_fill_for_cgi(csp, "url-info-osd.xml", exports, rsp); + if (JB_ERR_OK == err) + { + err = enlist(rsp->headers, + "Content-Type: application/opensearchdescription+xml"); + } } - if (map(exports, "sourceversions", 1, show_rcs(), 0)) + return err; + +} + + +/********************************************************************* + * + * Function : get_content_type + * + * Description : Use the file extension to guess the content type + * header we should use to serve the file. + * + * Parameters : + * 1 : filename = Name of the file whose content type + * we care about + * + * Returns : The guessed content type. + * + *********************************************************************/ +static const char *get_content_type(const char *filename) +{ + int i; + struct content_type { - free_map(exports); - return JB_ERR_MEMORY; + const char extension[6]; + const char content_type[11]; + }; + static const struct content_type content_types[] = + { + {".css", "text/css"}, + {".jpg", "image/jpeg"}, + {".jpeg", "image/jpeg"}, + {".png", "image/png"}, + }; + + for (i = 0; i < SZ(content_types); i++) + { + if (strstr(filename, content_types[i].extension)) + { + return content_types[i].content_type; + } } - return template_fill_for_cgi(csp, "show-version", exports, rsp); + /* No match by extension, default to html */ + return "text/html"; } - /********************************************************************* * - * Function : cgi_show_status + * Function : cgi_send_user_manual * - * Description : CGI function that returns a a web page describing the - * current status of Privoxy. + * Description : CGI function that sends a file in the user + * manual directory. * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) * 2 : rsp = http_response data structure for output * 3 : parameters = map of cgi parameters * - * CGI Parameters : - * file : Which file to show. Only first letter is checked, - * valid values are: - * - "p"ermissions (actions) file - * - "r"egex - * - "t"rust - * Default is to show menu and other information. + * CGI Parameters : file=name.html, the name of the HTML file + * (relative to user-manual from config) * * Returns : JB_ERR_OK on success - * JB_ERR_MEMORY on out-of-memory error. + * JB_ERR_MEMORY on out-of-memory error. * *********************************************************************/ -jb_err cgi_show_status(struct client_state *csp, - struct http_response *rsp, - const struct map *parameters) +jb_err cgi_send_user_manual(struct client_state *csp, + struct http_response *rsp, + const struct map *parameters) { - char *s = NULL; - unsigned i; - int j; - - FILE * fp; - char buf[BUFFER_SIZE]; - const char * filename = NULL; - char * file_description = NULL; -#ifdef FEATURE_STATISTICS - float perc_rej; /* Percentage of http requests rejected */ - int local_urls_read; - int local_urls_rejected; -#endif /* ndef FEATURE_STATISTICS */ - struct file_list * fl; - struct url_actions * b; + const char *filename; + char *full_path; jb_err err = JB_ERR_OK; - - struct map *exports; + const char *content_type; assert(csp); assert(rsp); assert(parameters); - if (NULL == (exports = default_exports(csp, "show-status"))) + if (0 == strncmpic(csp->config->usermanual, "http://", 7)) + { + log_error(LOG_LEVEL_CGI, "Request for local user-manual " + "received while user-manual delivery is disabled."); + return cgi_error_404(csp, rsp, parameters); + } + + if (!parameters->first) + { + /* requested http://p.p/user-manual (without trailing slash) */ + return cgi_redirect(rsp, CGI_PREFIX "user-manual/"); + } + + get_string_param(parameters, "file", &filename); + if (filename == NULL) + { + /* It's '/' so serve the index.html if there is one. */ + filename = "index.html"; + } + else if (NULL != strchr(filename, '/') || NULL != strstr(filename, "..")) + { + /* + * We currently only support a flat file + * hierarchy for the documentation. + */ + log_error(LOG_LEVEL_ERROR, + "Rejecting the request to serve '%s' as it contains '/' or '..'", + filename); + return JB_ERR_CGI_PARAMS; + } + + full_path = make_path(csp->config->usermanual, filename); + if (full_path == NULL) { return JB_ERR_MEMORY; } - switch (*(lookup(parameters, "file"))) + err = load_file(full_path, &rsp->body, &rsp->content_length); + if (JB_ERR_OK != err) { - case 'a': - if (!get_number_param(csp, parameters, "index", &i) && i < MAX_ACTION_FILES && csp->actions_list[i]) + assert((JB_ERR_FILE == err) || (JB_ERR_MEMORY == err)); + if (JB_ERR_FILE == err) { - filename = csp->actions_list[i]->filename; - file_description = "Actions File"; + err = cgi_error_no_template(csp, rsp, full_path); } - break; + freez(full_path); + return err; + } + freez(full_path); - case 'f': - if (csp->rlist) - { - filename = csp->rlist->filename; - file_description = "Filter File"; - } - break; + content_type = get_content_type(filename); + log_error(LOG_LEVEL_CGI, + "Content-Type guessed for %s: %s", filename, content_type); -#ifdef FEATURE_TRUST - case 't': - if (csp->tlist) - { - filename = csp->tlist->filename; - file_description = "Trust File"; - } - break; -#endif /* def FEATURE_TRUST */ + return enlist_unique_header(rsp->headers, "Content-Type", content_type); + +} + + +/********************************************************************* + * + * Function : cgi_show_version + * + * Description : CGI function that returns a a web page describing the + * file versions of Privoxy. + * + * Parameters : + * 1 : csp = Current client state (buffers, headers, etc...) + * 2 : rsp = http_response data structure for output + * 3 : parameters = map of cgi parameters + * + * CGI Parameters : none + * + * Returns : JB_ERR_OK on success + * JB_ERR_MEMORY on out-of-memory error. + * + *********************************************************************/ +jb_err cgi_show_version(struct client_state *csp, + struct http_response *rsp, + const struct map *parameters) +{ + struct map *exports; + + assert(csp); + assert(rsp); + assert(parameters); + + if (NULL == (exports = default_exports(csp, "show-version"))) + { + return JB_ERR_MEMORY; } - if (NULL != filename) + if (map(exports, "sourceversions", 1, show_rcs(), 0)) { - if ( map(exports, "file-description", 1, file_description, 1) - || map(exports, "filepath", 1, html_encode(filename), 0) ) - { - free_map(exports); - return JB_ERR_MEMORY; - } + free_map(exports); + return JB_ERR_MEMORY; + } - if ((fp = fopen(filename, "r")) == NULL) - { - if (map(exports, "content", 1, "In file: "); string_join (&matches, html_encode(csp->config->actions_file_short[i])); - snprintf(buf, 150, ".action ", i); + snprintf(buf, sizeof(buf), " ", i); string_append(&matches, buf); - string_append(&matches, "View config->actions_file_short[i])); - string_append(&matches, "\">Edit |
---|
{");
string_join (&matches, actions_to_html(csp, b->action));
- string_append(&matches, " } \n ");
+ string_append(&matches, " } |