+ static const char status[] =
+ "500 Internal Privoxy Error";
+ static const char body_prefix[] =
+ "<html>\n"
+ "<head>\n"
+ " <title>500 Internal Privoxy Error</title>\n"
+ " <link rel=\"shortcut icon\" href=\"" CGI_PREFIX "error-favicon.ico\" type=\"image/x-icon\">"
+ "</head>\n"
+ "<body>\n"
+ "<h1>500 Internal Privoxy Error</h1>\n"
+ "<p>Privoxy encountered an error while processing your request:</p>\n"
+ "<p><b>Unexpected internal error: ";
+ static const char body_suffix[] =
+ "</b></p>\n"
+ "<p>Please "
+ "<a href=\"https://sourceforge.net/p/ijbswa/bugs/\">"
+ "file a bug report</a>.</p>\n"
+ "</body>\n"
+ "</html>\n";
+ /* Includes room for larger error numbers in the future. */
+ const size_t body_size = sizeof(body_prefix) + sizeof(body_suffix) + 5;
+ assert(csp);
+ assert(rsp);
+
+ /* Reset rsp, if needed */
+ freez(rsp->status);
+ freez(rsp->head);
+ freez(rsp->body);
+ rsp->content_length = 0;
+ rsp->head_length = 0;
+ rsp->is_static = 0;
+ rsp->crunch_reason = INTERNAL_ERROR;
+
+ rsp->body = malloc_or_die(body_size);
+
+ snprintf(rsp->body, body_size, "%s%d%s", body_prefix, error_to_report, body_suffix);
+
+ rsp->status = strdup(status);
+ if (rsp->status == NULL)
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ return JB_ERR_OK;
+}
+
+
+/*********************************************************************
+ *
+ * Function : cgi_error_bad_param
+ *
+ * Description : CGI function that is called if the parameters
+ * (query string) for a CGI were wrong.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : rsp = http_response data structure for output
+ *
+ * CGI Parameters : none
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+jb_err cgi_error_bad_param(const struct client_state *csp,
+ struct http_response *rsp)
+{
+ struct map *exports;
+
+ assert(csp);
+ assert(rsp);
+
+ if (NULL == (exports = default_exports(csp, NULL)))
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ return template_fill_for_cgi(csp, "cgi-error-bad-param", exports, rsp);
+}
+
+
+/*********************************************************************
+ *
+ * Function : cgi_redirect
+ *
+ * Description : CGI support function to generate a HTTP redirect
+ * message
+ *
+ * Parameters :
+ * 1 : rsp = http_response data structure for output
+ * 2 : target = string with the target URL
+ *
+ * CGI Parameters : None
+ *
+ * Returns : JB_ERR_OK on success
+ * JB_ERR_MEMORY on out-of-memory error.
+ *
+ *********************************************************************/
+jb_err cgi_redirect (struct http_response * rsp, const char *target)
+{
+ jb_err err;
+
+ assert(rsp);
+ assert(target);
+
+ err = enlist_unique_header(rsp->headers, "Location", target);
+
+ rsp->status = strdup("302 Local Redirect from Privoxy");
+ if (rsp->status == NULL)
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ return err;
+}
+
+
+/*********************************************************************
+ *
+ * Function : add_help_link
+ *
+ * Description : Produce a copy of the string given as item,
+ * embedded in an HTML link to its corresponding
+ * section (item name in uppercase) in the actions
+ * chapter of the user manual, (whose URL is given in
+ * the config and defaults to our web site).
+ *
+ * FIXME: I currently only work for actions, and would
+ * like to be generalized for other topics.
+ *
+ * Parameters :
+ * 1 : item = item (will NOT be free()d.)
+ * It is assumed to be HTML-safe.
+ * 2 : config = The current configuration.
+ *
+ * Returns : String with item embedded in link, or NULL on
+ * out-of-memory
+ *
+ *********************************************************************/
+char *add_help_link(const char *item,
+ struct configuration_spec *config)
+{
+ char *result;
+
+ if (!item) return NULL;
+
+ result = strdup("<a href=\"");
+ if (!strncmpic(config->usermanual, "file://", 7) ||
+ !strncmpic(config->usermanual, "http", 4))
+ {
+ string_append(&result, config->usermanual);
+ }
+ else
+ {
+ string_append(&result, "http://");
+ string_append(&result, CGI_SITE_2_HOST);
+ string_append(&result, "/user-manual/");
+ }
+ string_append(&result, ACTIONS_HELP_PREFIX);
+ string_join (&result, string_toupper(item));
+ string_append(&result, "\">");
+ string_append(&result, item);
+ string_append(&result, "</a>");
+
+ return result;
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_http_time
+ *
+ * Description : Get the time in a format suitable for use in a
+ * HTTP header - e.g.:
+ * "Sun, 06 Nov 1994 08:49:37 GMT"
+ *
+ * Parameters :
+ * 1 : time_offset = Time returned will be current time
+ * plus this number of seconds.
+ * 2 : buf = Destination for result.
+ * 3 : buffer_size = Size of the buffer above. Must be big
+ * enough to hold 29 characters plus a
+ * trailing zero.
+ *
+ * Returns : N/A
+ *
+ *********************************************************************/
+void get_http_time(int time_offset, char *buf, size_t buffer_size)
+{
+ struct tm *t;
+ time_t current_time;
+#if defined(HAVE_GMTIME_R)
+ struct tm dummy;
+#endif
+
+ assert(buf);
+ assert(buffer_size > (size_t)29);
+
+ time(¤t_time);
+
+ current_time += time_offset;
+
+ /* get and save the gmt */
+#if HAVE_GMTIME_R
+ t = gmtime_r(¤t_time, &dummy);
+#elif defined(MUTEX_LOCKS_AVAILABLE)
+ privoxy_mutex_lock(&gmtime_mutex);
+ t = gmtime(¤t_time);
+ privoxy_mutex_unlock(&gmtime_mutex);
+#else
+ t = gmtime(¤t_time);
+#endif
+
+ strftime(buf, buffer_size, "%a, %d %b %Y %H:%M:%S GMT", t);
+
+}
+
+/*********************************************************************
+ *
+ * Function : get_locale_time
+ *
+ * Description : Get the time in a date(1)-like format
+ * according to the current locale - e.g.:
+ * "Fri Aug 29 19:37:12 CEST 2008"
+ *
+ * XXX: Should we allow the user to change the format?
+ *
+ * Parameters :
+ * 1 : buf = Destination for result.
+ * 2 : buffer_size = Size of the buffer above. Must be big
+ * enough to hold 29 characters plus a
+ * trailing zero.
+ *
+ * Returns : N/A
+ *
+ *********************************************************************/
+static void get_locale_time(char *buf, size_t buffer_size)
+{
+ struct tm *timeptr;
+ time_t current_time;
+#if defined(HAVE_LOCALTIME_R)
+ struct tm dummy;
+#endif
+
+ assert(buf);
+ assert(buffer_size > (size_t)29);
+
+ time(¤t_time);
+
+#if HAVE_LOCALTIME_R
+ timeptr = localtime_r(¤t_time, &dummy);
+#elif defined(MUTEX_LOCKS_AVAILABLE)
+ privoxy_mutex_lock(&localtime_mutex);
+ timeptr = localtime(¤t_time);
+#else
+ timeptr = localtime(¤t_time);
+#endif
+
+ strftime(buf, buffer_size, "%a %b %d %X %Z %Y", timeptr);
+
+#if !defined(HAVE_LOCALTIME_R) && defined(MUTEX_LOCKS_AVAILABLE)
+ privoxy_mutex_unlock(&localtime_mutex);
+#endif
+}
+
+
+#ifdef FEATURE_COMPRESSION
+/*********************************************************************
+ *
+ * Function : compress_buffer
+ *
+ * Description : Compresses the content of a buffer with zlib's deflate
+ * Allocates a new buffer for the result, free'ing it is
+ * up to the caller.
+ *
+ * Parameters :
+ * 1 : buffer = buffer whose content should be compressed
+ * 2 : buffer_length = length of the buffer
+ * 3 : compression_level = compression level for compress2()
+ *
+ * Returns : NULL on error, otherwise a pointer to the compressed
+ * content of the input buffer.
+ *
+ *********************************************************************/
+char *compress_buffer(char *buffer, size_t *buffer_length, int compression_level)
+{
+ char *compressed_buffer;
+ uLongf new_length;
+ assert(-1 <= compression_level && compression_level <= 9);
+
+ /* Let zlib figure out the maximum length of the compressed data */
+ new_length = compressBound((uLongf)*buffer_length);
+
+ compressed_buffer = malloc_or_die(new_length);