+/*********************************************************************
+ *
+ * Function : crunch_reason
+ *
+ * Description : Translates the crunch reason code into a string.
+ *
+ * Parameters :
+ * 1 : rsp = a http_response
+ *
+ * Returns : A string with the crunch reason or an error description.
+ *
+ *********************************************************************/
+const char *crunch_reason(const struct http_response *rsp)
+{
+ char * reason = NULL;
+
+ assert(rsp != NULL);
+ if (rsp == NULL)
+ {
+ return "Internal error while searching for crunch reason";
+ }
+
+ switch (rsp->reason)
+ {
+ case RSP_REASON_UNSUPPORTED:
+ reason = "Unsupported HTTP feature";
+ break;
+ case RSP_REASON_BLOCKED:
+ reason = "Blocked";
+ break;
+ case RSP_REASON_UNTRUSTED:
+ reason = "Untrusted";
+ break;
+ case RSP_REASON_REDIRECTED:
+ reason = "Redirected";
+ break;
+ case RSP_REASON_CGI_CALL:
+ reason = "CGI Call";
+ break;
+ case RSP_REASON_NO_SUCH_DOMAIN:
+ reason = "DNS failure";
+ break;
+ case RSP_REASON_FORWARDING_FAILED:
+ reason = "Forwarding failed";
+ break;
+ case RSP_REASON_CONNECT_FAILED:
+ reason = "Connection failure";
+ break;
+ case RSP_REASON_OUT_OF_MEMORY:
+ reason = "Out of memory (may mask other reasons)";
+ break;
+ default:
+ reason = "No reason recorded";
+ break;
+ }
+
+ return reason;
+}
+
+
+/*********************************************************************
+ *
+ * Function : send_crunch_response
+ *
+ * Description : Delivers already prepared response for
+ * intercepted requests, logs the interception
+ * and frees the response.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 1 : rsp = Fully prepared response. Will be freed on exit.
+ *
+ * Returns : Nothing.
+ *
+ *********************************************************************/
+void send_crunch_response(struct client_state *csp, struct http_response *rsp)
+{
+ const struct http_request *http = csp->http;
+ char status_code[4];
+
+ assert(rsp != NULL);
+ assert(rsp->head != NULL);
+
+ if (rsp == NULL)
+ {
+ /*
+ * Not supposed to happen. If it does
+ * anyway, treat it as an unknown error.
+ */
+ cgi_error_unknown(csp, rsp, RSP_REASON_INTERNAL_ERROR);
+ /* return code doesn't matter */
+ }
+
+ if (rsp == NULL)
+ {
+ /* If rsp is still NULL, we have serious internal problems. */
+ log_error(LOG_LEVEL_FATAL,
+ "NULL response in send_crunch_response and cgi_error_unknown failed as well.");
+ }
+
+ /*
+ * Extract the status code from the actual head
+ * that was send to the client. It is the only
+ * way to get it right for all requests, including
+ * the fixed ones for out-of-memory problems.
+ *
+ * A head starts like this: 'HTTP/1.1 200...'
+ * 0123456789|11
+ * 10
+ */
+ status_code[0] = rsp->head[9];
+ status_code[1] = rsp->head[10];
+ status_code[2] = rsp->head[11];
+ status_code[3] = '\0';
+
+ /* Write the answer to the client */
+ if (write_socket(csp->cfd, rsp->head, rsp->head_length)
+ || write_socket(csp->cfd, rsp->body, rsp->content_length))
+ {
+ /* There is nothing we can do about it. */
+ log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", csp->http->host);
+ }
+
+ /* Log that the request was crunched and why. */
+ log_error(LOG_LEVEL_GPC, "%s%s crunch! (%s)",
+ http->hostport, http->path, crunch_reason(rsp));
+ log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" %s %d",
+ csp->ip_addr_str, http->ocmd, status_code, rsp->content_length);
+
+ /* Clean up and return */
+ if (cgi_error_memory() != rsp)
+ {
+ free_http_response(rsp);
+ }
+ return;
+}
+
+