http_request.c revision a0944d0cea0301ca402cfc896485d03da0d6209c
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny/* Licensed to the Apache Software Foundation (ASF) under one or more
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * contributor license agreements. See the NOTICE file distributed with
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * this work for additional information regarding copyright ownership.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * The ASF licenses this file to You under the Apache License, Version 2.0
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * (the "License"); you may not use this file except in compliance with
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * the License. You may obtain a copy of the License at
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Unless required by applicable law or agreed to in writing, software
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * distributed under the License is distributed on an "AS IS" BASIS,
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * See the License for the specific language governing permissions and
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * limitations under the License.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * http_request.c: functions to get and process requests
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Rob McCool 3/21/93
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Thoroughly revamped by rst for Apache. NB this file reads
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * best from the bottom up.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny/*****************************************************************
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * Mainline request processing...
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny/* XXX A cleaner and faster way to do this might be to pass the request_rec
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * down the filter chain as a parameter. It would need to change for
46c5deedec570bb5f99702a933ba99d76f9f09cbJakub Hrozek * subrequest vs. main request filters; perhaps the subrequest filter could
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * make the switch.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny if (f->r == from) {
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny char *custom_response = ap_response_code_string(r, error_index);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Check if we still have the ap_http_header_filter in place. If
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * this is the case we should not ignore AP_FILTER_ERROR here because
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * it means that we have not sent any response at all and never
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * will. This is bad. Sent an internal server error instead.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny while (next && (next->frec != ap_http_header_filter_handle)) {
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * If next != NULL then we left the while above because of
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * next->frec == ap_http_header_filter
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny "Custom error page caused AP_FILTER_ERROR");
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * The following takes care of Apache redirects to custom response URLs
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Note that if we are already dealing with the response to some other
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * error condition, we just report on the original error, and give up on
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * any attempt to handle the other thing "intelligently"...
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek while (r_1st_err->prev && (r_1st_err->prev->status != HTTP_OK))
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek r_1st_err = r_1st_err->prev; /* Get back to original error */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* The recursive error was caused by an ErrorDocument specifying
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * an internal redirect to a bad URI. ap_internal_redirect has
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * changed the filter chains to point to the ErrorDocument's
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * request_rec. Back out those changes so we can safely use the
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * original failing request_rec to send the canned error message.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * ap_send_error_response gets rid of existing resource filters
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * on the output side, so we can skip those.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek update_r_in_filters(r_1st_err->proto_output_filters, r, r_1st_err);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek update_r_in_filters(r_1st_err->input_filters, r, r_1st_err);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek custom_response = NULL; /* Do NOT retry the custom thing! */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * This test is done here so that none of the auth modules needs to know
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * about proxy authentication. They treat it like normal auth, and then
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * we tweak the status.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (HTTP_UNAUTHORIZED == r->status && PROXYREQ_PROXY == r->proxyreq) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek r->status = HTTP_PROXY_AUTHENTICATION_REQUIRED;
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* If we don't want to keep the connection, make sure we mark that the
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * connection is not eligible for keepalive. If we want to keep the
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * connection, be sure that the request body (if any) has been read.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * Two types of custom redirects --- plain text, and URLs. Plain text has
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * a leading '"', so the URL code, here, is triggered on its absence
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (custom_response && custom_response[0] != '"') {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * The URL isn't local, so lets drop through the rest of this
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * apache code, and continue with the usual REDIRECT handler.
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * But note that the client will ultimately see the wrong
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov apr_table_setn(r->headers_out, "Location", custom_response);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek r->no_local_copy = 1; /* Do NOT send HTTP_NOT_MODIFIED for
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * error documents! */
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * This redirect needs to be a GET no matter what the original
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * method was.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek apr_table_setn(r->subprocess_env, "REQUEST_METHOD", r->method);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * Provide a special method for modules to communicate
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * more informative (than the plain canned) messages to us.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * Propagate them to ErrorDocuments via the ERROR_NOTES variable:
0232747f04b650796db56fd7b487aee8a96fab03Simo Sorce apr_table_setn(r->subprocess_env, "ERROR_NOTES", error_notes);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * Dumb user has given us a bad url to redirect to --- fake up
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * dying with a recursive server error...
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek "Invalid error redirection directive: %s",
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek ap_send_error_response(r_1st_err, recursive_error);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek apr_bucket_brigade *bb = apr_brigade_create(c->pool, c->bucket_alloc);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek rv = ap_get_brigade(c->input_filters, bb, AP_MODE_SPECULATIVE,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek if (rv != APR_SUCCESS || APR_BRIGADE_EMPTY(bb)) {
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * Error or empty brigade: There is no data present in the input
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub HrozekAP_DECLARE(void) ap_process_request_after_handler(request_rec *r)
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny /* Send an EOR bucket through the output filter chain. When
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * this bucket is destroyed, the request will be logged and
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov * its pool will be freed
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny bb = apr_brigade_create(r->connection->pool, r->connection->bucket_alloc);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny b = ap_bucket_eor_create(r->connection->bucket_alloc, r);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek ap_pass_brigade(r->connection->output_filters, bb);
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek /* From here onward, it is no longer safe to reference r
0232747f04b650796db56fd7b487aee8a96fab03Simo Sorce * or r->pool, because r->pool may have been destroyed
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * already by the EOR bucket's cleanup function.
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek AP_PROCESS_REQUEST_RETURN((uintptr_t)r, r->uri, r->status);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny /* Give quick handlers a shot at serving the request on the fast
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * path, bypassing all of the other Apache hooks.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * This hook was added to enable serving files out of a URI keyed
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * content cache ( e.g., Mike Abbott's Quick Shortcut Cache,
8ba8222afca3026fd67af08e224b1d9e848aceaaJakub Hrozek * described here: http://oss.sgi.com/projects/apache/mod_qsc.html )
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * It may have other uses as well, such as routing requests directly to
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * content handlers that have the ability to grok HTTP and do their
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * own access checking, etc (e.g. servlet engines).
65393a294e635822c1d7a15fe5853dc457ad8a2aSimo Sorce * Use this hook with extreme care and only if you know what you are
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov ap_time_process_request(r->connection->sbh, START_PREQUEST);
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce const apr_array_header_t *t_h = apr_table_elts(r->headers_in);
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce const apr_table_entry_t *t_elt = (apr_table_entry_t *)t_h->elts;
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce "Headers received from client:");
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r, " %s: %s",
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny apr_thread_mutex_create(&r->invoke_mtx, APR_THREAD_MUTEX_DEFAULT, r->pool);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny access_status = ap_run_quick_handler(r, 0); /* Not a look-up request */
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny /* TODO: Should move these steps into a generic function, so modules
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * working on a suspended request can also call _ENTRY again.
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny AP_PROCESS_REQUEST_RETURN((uintptr_t)r, r->uri, access_status);
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov /* e.g., something not in storage like TRACE */
bba1a5fd62cffcae076d1351df5a83fbc4a6ec17Simo Sorce bb = apr_brigade_create(c->pool, c->bucket_alloc);
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * Notice a timeout as an error message. This might be
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * valuable for detecting clients with broken network
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny * connections or possible DoS attacks.
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose * It is still safe to use r / r->pool here as the eor bucket
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose * could not have been destroyed in the event of a timeout.
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose "Timeout while writing data for URI %s to the"
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bosestatic apr_table_t *rename_original_env(apr_pool_t *p, apr_table_t *t)
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose const apr_array_header_t *env_arr = apr_table_elts(t);
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose const apr_table_entry_t *elts = (const apr_table_entry_t *) env_arr->elts;
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose apr_table_t *new = apr_table_make(p, env_arr->nalloc);
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose apr_table_setn(new, apr_pstrcat(p, "REDIRECT_", elts[i].key, NULL),
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bosestatic request_rec *internal_internal_redirect(const char *new_uri,
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov new = (request_rec *) apr_pcalloc(r->pool, sizeof(request_rec));
909a86af4eb99f5d311d7136cab78dca535ae304Sumit Bose * A whole lot of this really ought to be shared with http_protocol.c...
if (location)
f->r = new;
f = nextf;
return NULL;
return new;
r->headers_out);
r->err_headers_out);
r->subprocess_env);
if (r->main) {
int access_status;
if (!new) {
int access_status;
if (!new) {
if (r->handler)
const char *method;
if (reset) {
int method;
if (reset) {
mask = 0;