+ return JB_ERR_OK;
+
+}
+
+
+#ifdef FUZZ
+/*********************************************************************
+ *
+ * Function : fuzz_chunked_transfer_encoding
+ *
+ * Description : Treat the fuzzed input as chunked transfer encoding
+ * to check and dechunk.
+ *
+ * Parameters :
+ * 1 : csp = Used to store the data.
+ * 2 : fuzz_input_file = File to read the input from.
+ *
+ * Returns : Result of dechunking
+ *
+ *********************************************************************/
+extern int fuzz_chunked_transfer_encoding(struct client_state *csp, char *fuzz_input_file)
+{
+ size_t length;
+ size_t size = (size_t)(csp->iob->eod - csp->iob->cur);
+ enum chunk_status status;
+
+ status = chunked_body_is_complete(csp->iob, &length);
+ if (CHUNK_STATUS_BODY_COMPLETE != status)
+ {
+ log_error(LOG_LEVEL_INFO, "Chunked body is incomplete or invalid");
+ }
+
+ return (JB_ERR_OK == remove_chunked_transfer_coding(csp->iob->cur, &size));
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : fuzz_client_request
+ *
+ * Description : Try to get a client request from the fuzzed input.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ * 2 : fuzz_input_file = File to read the input from.
+ *
+ * Returns : Result of fuzzing.
+ *
+ *********************************************************************/
+extern int fuzz_client_request(struct client_state *csp, char *fuzz_input_file)
+{
+ jb_err err;
+
+ csp->cfd = 0;
+ csp->ip_addr_str = "fuzzer";
+
+ if (strcmp(fuzz_input_file, "-") != 0)
+ {
+ log_error(LOG_LEVEL_FATAL,
+ "Fuzzed client requests can currenty only be read from stdin (-).");
+ }
+ err = receive_client_request(csp);
+ if (err != JB_ERR_OK)
+ {
+ return 1;
+ }
+ err = parse_client_request(csp);
+ if (err != JB_ERR_OK)
+ {
+ return 1;
+ }
+
+ return 0;
+
+}
+#endif /* def FUZZ */
+
+
+#ifdef FEATURE_FORCE_LOAD
+/*********************************************************************
+ *
+ * Function : force_required
+ *
+ * Description : Checks a request line to see if it contains
+ * the FORCE_PREFIX. If it does, it is removed
+ * unless enforcing requests has beend disabled.
+ *
+ * Parameters :
+ * 1 : request_line = HTTP request line
+ *
+ * Returns : TRUE if force is required, FALSE otherwise.
+ *
+ *********************************************************************/
+static int force_required(const struct client_state *csp, char *request_line)
+{
+ char *p;
+
+ p = strstr(request_line, "http://");
+ if (p != NULL)
+ {
+ /* Skip protocol */
+ p += strlen("http://");
+ }
+ else
+ {
+ /* Intercepted request usually don't specify the protocol. */
+ p = request_line;
+ }
+
+ /* Go to the beginning of the path */
+ p = strstr(p, "/");
+ if (p == NULL)
+ {
+ /*
+ * If the path is missing the request line is invalid and we
+ * are done here. The client-visible rejection happens later on.
+ */
+ return 0;
+ }
+
+ if (0 == strncmpic(p, FORCE_PREFIX, strlen(FORCE_PREFIX) - 1))
+ {
+ if (!(csp->config->feature_flags & RUNTIME_FEATURE_ENFORCE_BLOCKS))
+ {
+ /* XXX: Should clean more carefully */
+ strclean(request_line, FORCE_PREFIX);
+ log_error(LOG_LEVEL_FORCE,
+ "Enforcing request: \"%s\".", request_line);
+
+ return 1;
+ }
+ log_error(LOG_LEVEL_FORCE,
+ "Ignored force prefix in request: \"%s\".", request_line);
+ }
+
+ return 0;
+
+}
+#endif /* def FEATURE_FORCE_LOAD */
+
+
+/*********************************************************************
+ *
+ * Function : receive_client_request
+ *
+ * Description : Read the client's request (more precisely the
+ * client headers) and answer it if necessary.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : JB_ERR_OK, JB_ERR_PARSE or JB_ERR_MEMORY
+ *
+ *********************************************************************/
+static jb_err receive_client_request(struct client_state *csp)
+{
+ char buf[BUFFER_SIZE];
+ char *p;
+ char *req = NULL;
+ struct http_request *http;
+ int len;
+ jb_err err;
+
+ /* Temporary copy of the client's headers before they get enlisted in csp->headers */
+ struct list header_list;
+ struct list *headers = &header_list;
+
+ /* We don't care if the arriving data is a valid HTTP request or not. */
+ csp->requests_received_total++;
+
+ http = csp->http;
+
+ memset(buf, 0, sizeof(buf));
+
+ req = get_request_line(csp);
+ if (req == NULL)
+ {
+ mark_server_socket_tainted(csp);
+ return JB_ERR_PARSE;
+ }
+ assert(*req != '\0');
+
+ if (client_protocol_is_unsupported(csp, req))
+ {
+ return JB_ERR_PARSE;
+ }
+
+#ifdef FEATURE_FORCE_LOAD
+ if (force_required(csp, req))
+ {
+ csp->flags |= CSP_FLAG_FORCED;
+ }
+#endif /* def FEATURE_FORCE_LOAD */
+
+ err = parse_http_request(req, http);
+ freez(req);
+ if (JB_ERR_OK != err)
+ {
+ write_socket(csp->cfd, CHEADER, strlen(CHEADER));
+ /* XXX: Use correct size */
+ log_error(LOG_LEVEL_CLF, "%s - - [%T] \"Invalid request\" 400 0", csp->ip_addr_str);
+ log_error(LOG_LEVEL_ERROR,
+ "Couldn't parse request line received from %s: %s",
+ csp->ip_addr_str, jb_err_to_string(err));
+
+ free_http_request(http);
+ return JB_ERR_PARSE;
+ }
+
+ /* grab the rest of the client's headers */
+ init_list(headers);
+ for (;;)
+ {
+ p = get_header(csp->client_iob);
+
+ if (p == NULL)
+ {
+ /* There are no additional headers to read. */
+ break;
+ }
+
+ if (*p == '\0')
+ {
+ /*
+ * We didn't receive a complete header
+ * line yet, get the rest of it.
+ */
+ if (!data_is_available(csp->cfd, csp->config->socket_timeout))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Stopped grabbing the client headers.");
+ destroy_list(headers);
+ return JB_ERR_PARSE;
+ }
+
+ len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
+ if (len <= 0)
+ {
+ log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
+ destroy_list(headers);
+ return JB_ERR_PARSE;
+ }
+
+ if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
+ {
+ /*
+ * If there is no memory left for buffering the
+ * request, there is nothing we can do but hang up
+ */
+ destroy_list(headers);
+ return JB_ERR_MEMORY;
+ }
+ }
+ else
+ {
+ if (!strncmpic(p, "Transfer-Encoding:", 18))
+ {
+ /*
+ * XXX: should be called through sed()
+ * but currently can't.
+ */
+ client_transfer_encoding(csp, &p);
+ }
+ /*
+ * We were able to read a complete
+ * header and can finally enlist it.
+ */
+ enlist(headers, p);
+ freez(p);
+ }
+ }
+
+ if (http->host == NULL)