mod_proxy_ajp.c revision 0f1e6e90cf575408293a9cf4276c2c5ec9171c3d
97a9a944b5887e91042b019776c41d5dd74557aferikabele/* Licensed to the Apache Software Foundation (ASF) under one or more
97a9a944b5887e91042b019776c41d5dd74557aferikabele * contributor license agreements. See the NOTICE file distributed with
97a9a944b5887e91042b019776c41d5dd74557aferikabele * this work for additional information regarding copyright ownership.
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive * The ASF licenses this file to You under the Apache License, Version 2.0
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive * (the "License"); you may not use this file except in compliance with
a945f35eff8b6a88009ce73de6d4c862ce58de3cslive * the License. You may obtain a copy of the License at
b686b6a420bde7f78c416b90be11db94cb789979nd *
b686b6a420bde7f78c416b90be11db94cb789979nd * http://www.apache.org/licenses/LICENSE-2.0
b686b6a420bde7f78c416b90be11db94cb789979nd *
b686b6a420bde7f78c416b90be11db94cb789979nd * Unless required by applicable law or agreed to in writing, software
b686b6a420bde7f78c416b90be11db94cb789979nd * distributed under the License is distributed on an "AS IS" BASIS,
b686b6a420bde7f78c416b90be11db94cb789979nd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b686b6a420bde7f78c416b90be11db94cb789979nd * See the License for the specific language governing permissions and
b686b6a420bde7f78c416b90be11db94cb789979nd * limitations under the License.
b686b6a420bde7f78c416b90be11db94cb789979nd */
52fff662005b1866a3ff09bb6c902800c5cc6dedjerenkrantz
b686b6a420bde7f78c416b90be11db94cb789979nd/* AJP routines for Apache proxy */
b686b6a420bde7f78c416b90be11db94cb789979nd
b686b6a420bde7f78c416b90be11db94cb789979nd#include "mod_proxy.h"
4b5981e276e93df97c34e4da05ca5cf8bbd937dand#include "ajp.h"
b686b6a420bde7f78c416b90be11db94cb789979nd
b686b6a420bde7f78c416b90be11db94cb789979ndmodule AP_MODULE_DECLARE_DATA proxy_ajp_module;
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd/*
0066eddda7203f6345b56f77d146a759298dc635gryzor * Canonicalise http-like URLs.
7f5b59ccc63c0c0e3e678a168f09ee6a2f51f9d0nd * scheme is the scheme for the URL
f3ec420152ca921e4c1ce77782f51b53f659018dnd * url is the URL starting with the first '/'
f3ec420152ca921e4c1ce77782f51b53f659018dnd * def_port is the default port for this scheme.
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd */
b686b6a420bde7f78c416b90be11db94cb789979ndstatic int proxy_ajp_canon(request_rec *r, char *url)
b686b6a420bde7f78c416b90be11db94cb789979nd{
b686b6a420bde7f78c416b90be11db94cb789979nd char *host, *path, *search, sport[7];
b686b6a420bde7f78c416b90be11db94cb789979nd const char *err;
b686b6a420bde7f78c416b90be11db94cb789979nd apr_port_t port = AJP13_DEF_PORT;
b686b6a420bde7f78c416b90be11db94cb789979nd
b686b6a420bde7f78c416b90be11db94cb789979nd /* ap_port_of_scheme() */
06ba4a61654b3763ad65f52283832ebf058fdf1cslive if (strncasecmp(url, "ajp:", 4) == 0) {
06ba4a61654b3763ad65f52283832ebf058fdf1cslive url += 4;
9bcfc3697a91b5215893a7d0206865b13fc72148nd }
9bcfc3697a91b5215893a7d0206865b13fc72148nd else {
06ba4a61654b3763ad65f52283832ebf058fdf1cslive return DECLINED;
b686b6a420bde7f78c416b90be11db94cb789979nd }
b686b6a420bde7f78c416b90be11db94cb789979nd
b686b6a420bde7f78c416b90be11db94cb789979nd ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
b686b6a420bde7f78c416b90be11db94cb789979nd "proxy: AJP: canonicalising URL %s", url);
117c1f888a14e73cdd821dc6c23eb0411144a41cnd
117c1f888a14e73cdd821dc6c23eb0411144a41cnd /*
b686b6a420bde7f78c416b90be11db94cb789979nd * do syntactic check.
b686b6a420bde7f78c416b90be11db94cb789979nd * We break the URL into host, port, path, search
b686b6a420bde7f78c416b90be11db94cb789979nd */
b686b6a420bde7f78c416b90be11db94cb789979nd err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
b686b6a420bde7f78c416b90be11db94cb789979nd if (err) {
b686b6a420bde7f78c416b90be11db94cb789979nd ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b686b6a420bde7f78c416b90be11db94cb789979nd "error parsing URL %s: %s",
b686b6a420bde7f78c416b90be11db94cb789979nd url, err);
b686b6a420bde7f78c416b90be11db94cb789979nd return HTTP_BAD_REQUEST;
b686b6a420bde7f78c416b90be11db94cb789979nd }
b686b6a420bde7f78c416b90be11db94cb789979nd
b686b6a420bde7f78c416b90be11db94cb789979nd /*
b686b6a420bde7f78c416b90be11db94cb789979nd * now parse path/search args, according to rfc1738
b686b6a420bde7f78c416b90be11db94cb789979nd */
b686b6a420bde7f78c416b90be11db94cb789979nd search = NULL;
b686b6a420bde7f78c416b90be11db94cb789979nd
b686b6a420bde7f78c416b90be11db94cb789979nd /* process path */
06ba4a61654b3763ad65f52283832ebf058fdf1cslive if (apr_table_get(r->notes, "proxy-nocanon")) {
06ba4a61654b3763ad65f52283832ebf058fdf1cslive path = url; /* this is the raw path */
9bcfc3697a91b5215893a7d0206865b13fc72148nd }
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd else {
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd r->proxyreq);
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd search = r->args;
b686b6a420bde7f78c416b90be11db94cb789979nd }
b686b6a420bde7f78c416b90be11db94cb789979nd if (path == NULL)
b686b6a420bde7f78c416b90be11db94cb789979nd return HTTP_BAD_REQUEST;
b686b6a420bde7f78c416b90be11db94cb789979nd
b686b6a420bde7f78c416b90be11db94cb789979nd apr_snprintf(sport, sizeof(sport), ":%d", port);
b686b6a420bde7f78c416b90be11db94cb789979nd
b686b6a420bde7f78c416b90be11db94cb789979nd if (ap_strchr_c(host, ':')) {
b686b6a420bde7f78c416b90be11db94cb789979nd /* if literal IPv6 address */
b686b6a420bde7f78c416b90be11db94cb789979nd host = apr_pstrcat(r->pool, "[", host, "]", NULL);
b686b6a420bde7f78c416b90be11db94cb789979nd }
b686b6a420bde7f78c416b90be11db94cb789979nd r->filename = apr_pstrcat(r->pool, "proxy:ajp://", host, sport,
b686b6a420bde7f78c416b90be11db94cb789979nd "/", path, (search) ? "?" : "",
06ba4a61654b3763ad65f52283832ebf058fdf1cslive (search) ? search : "", NULL);
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd return OK;
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd}
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd#define METHOD_NON_IDEMPOTENT 0
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd#define METHOD_IDEMPOTENT 1
b686b6a420bde7f78c416b90be11db94cb789979nd#define METHOD_IDEMPOTENT_WITH_ARGS 2
b686b6a420bde7f78c416b90be11db94cb789979nd
b686b6a420bde7f78c416b90be11db94cb789979ndstatic int is_idempotent(request_rec *r)
b686b6a420bde7f78c416b90be11db94cb789979nd{
b686b6a420bde7f78c416b90be11db94cb789979nd /*
b686b6a420bde7f78c416b90be11db94cb789979nd * RFC2616 (9.1.2): GET, HEAD, PUT, DELETE, OPTIONS, TRACE are considered
b686b6a420bde7f78c416b90be11db94cb789979nd * idempotent. Hint: HEAD requests use M_GET as method number as well.
b686b6a420bde7f78c416b90be11db94cb789979nd */
b686b6a420bde7f78c416b90be11db94cb789979nd switch (r->method_number) {
b686b6a420bde7f78c416b90be11db94cb789979nd case M_GET:
b686b6a420bde7f78c416b90be11db94cb789979nd case M_DELETE:
b686b6a420bde7f78c416b90be11db94cb789979nd case M_PUT:
b686b6a420bde7f78c416b90be11db94cb789979nd case M_OPTIONS:
06ba4a61654b3763ad65f52283832ebf058fdf1cslive case M_TRACE:
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd /*
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd * If the request has arguments it might have side-effects and thus
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd * it might be undesirable to resent it to a backend again
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd * automatically.
e55e60efce8a3e2139132c1d6ad9f6f0d2976614nd */
b686b6a420bde7f78c416b90be11db94cb789979nd if (r->args) {
b686b6a420bde7f78c416b90be11db94cb789979nd return METHOD_IDEMPOTENT_WITH_ARGS;
b686b6a420bde7f78c416b90be11db94cb789979nd }
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd return METHOD_IDEMPOTENT;
ad74a0524a06bfe11b7de9e3b4ce7233ab3bd3f7nd /* Everything else is not considered idempotent. */
0066eddda7203f6345b56f77d146a759298dc635gryzor default:
7f5b59ccc63c0c0e3e678a168f09ee6a2f51f9d0nd return METHOD_NON_IDEMPOTENT;
f3ec420152ca921e4c1ce77782f51b53f659018dnd }
f3ec420152ca921e4c1ce77782f51b53f659018dnd}
3b3b7fc78d1f5bfc2769903375050048ff41ff26nd
b036ef2952fb6924b308f954b39786443460ddc6rpluem/*
b686b6a420bde7f78c416b90be11db94cb789979nd * XXX: AJP Auto Flushing
b686b6a420bde7f78c416b90be11db94cb789979nd *
* When processing CMD_AJP13_SEND_BODY_CHUNK AJP messages we will do a poll
* with FLUSH_WAIT miliseconds timeout to determine if more data is currently
* available at the backend. If there is no more data available, we flush
* the data to the client by adding a flush bucket to the brigade we pass
* up the filter chain.
* This is only a bandaid to fix the AJP/1.3 protocol shortcoming of not
* sending (actually not having defined) a flush message, when the data
* should be flushed to the client. As soon as this protocol shortcoming is
* fixed this code should be removed.
*
* For further discussion see PR37100.
* http://issues.apache.org/bugzilla/show_bug.cgi?id=37100
*/
/*
* process the request and write the response.
*/
static int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
proxy_conn_rec *conn,
conn_rec *origin,
proxy_dir_conf *conf,
apr_uri_t *uri,
char *url, char *server_portstr)
{
apr_status_t status;
int result;
apr_bucket *e;
apr_bucket_brigade *input_brigade;
apr_bucket_brigade *output_brigade;
ajp_msg_t *msg;
apr_size_t bufsiz = 0;
char *buff;
apr_uint16_t size;
const char *tenc;
int havebody = 1;
int output_failed = 0;
int backend_failed = 0;
apr_off_t bb_len;
int data_sent = 0;
int headers_sent = 0;
int rv = 0;
apr_int32_t conn_poll_fd;
apr_pollfd_t *conn_poll;
proxy_server_conf *psf =
ap_get_module_config(r->server->module_config, &proxy_module);
apr_size_t maxsize = AJP_MSG_BUFFER_SZ;
if (psf->io_buffer_size_set)
maxsize = psf->io_buffer_size;
if (maxsize > AJP_MAX_BUFFER_SZ)
maxsize = AJP_MAX_BUFFER_SZ;
else if (maxsize < AJP_MSG_BUFFER_SZ)
maxsize = AJP_MSG_BUFFER_SZ;
maxsize = APR_ALIGN(maxsize, 1024);
/*
* Send the AJP request to the remote server
*/
/* send request headers */
status = ajp_send_header(conn->sock, r, maxsize, uri);
if (status != APR_SUCCESS) {
conn->close++;
ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
"proxy: AJP: request failed to %pI (%s)",
conn->worker->cp->addr,
conn->worker->hostname);
if (status == AJP_EOVERFLOW)
return HTTP_BAD_REQUEST;
else {
/*
* This is only non fatal when the method is idempotent. In this
* case we can dare to retry it with a different worker if we are
* a balancer member.
*/
if (is_idempotent(r) == METHOD_IDEMPOTENT) {
return HTTP_SERVICE_UNAVAILABLE;
}
return HTTP_INTERNAL_SERVER_ERROR;
}
}
/* allocate an AJP message to store the data of the buckets */
bufsiz = maxsize;
status = ajp_alloc_data_msg(r->pool, &buff, &bufsiz, &msg);
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
conn->close++;
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: ajp_alloc_data_msg failed");
return HTTP_INTERNAL_SERVER_ERROR;
}
/* read the first bloc of data */
input_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
if (tenc && (strcasecmp(tenc, "chunked") == 0)) {
/* The AJP protocol does not want body data yet */
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: request is chunked");
} else {
status = ap_get_brigade(r->input_filters, input_brigade,
AP_MODE_READBYTES, APR_BLOCK_READ,
maxsize - AJP_HEADER_SZ);
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
conn->close++;
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: ap_get_brigade failed");
apr_brigade_destroy(input_brigade);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* have something */
if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: APR_BUCKET_IS_EOS");
}
/* Try to send something */
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: data to read (max %" APR_SIZE_T_FMT
" at %" APR_SIZE_T_FMT ")", bufsiz, msg->pos);
status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
conn->close++;
apr_brigade_destroy(input_brigade);
ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
"proxy: apr_brigade_flatten");
return HTTP_INTERNAL_SERVER_ERROR;
}
apr_brigade_cleanup(input_brigade);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: got %" APR_SIZE_T_FMT " bytes of data", bufsiz);
if (bufsiz > 0) {
status = ajp_send_data_msg(conn->sock, msg, bufsiz);
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
conn->close++;
apr_brigade_destroy(input_brigade);
ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
"proxy: send failed to %pI (%s)",
conn->worker->cp->addr,
conn->worker->hostname);
/*
* It is fatal when we failed to send a (part) of the request
* body.
*/
return HTTP_INTERNAL_SERVER_ERROR;
}
conn->worker->s->transferred += bufsiz;
}
}
/* read the response */
conn->data = NULL;
status = ajp_read_header(conn->sock, r, maxsize,
(ajp_msg_t **)&(conn->data));
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
conn->close++;
apr_brigade_destroy(input_brigade);
ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
"proxy: read response failed from %pI (%s)",
conn->worker->cp->addr,
conn->worker->hostname);
/*
* This is only non fatal when we have not sent (parts) of a possible
* request body so far (we do not store it and thus cannot sent it
* again) and the method is idempotent. In this case we can dare to
* retry it with a different worker if we are a balancer member.
*/
if ((bufsiz == 0) && (is_idempotent(r) == METHOD_IDEMPOTENT)) {
return HTTP_SERVICE_UNAVAILABLE;
}
return HTTP_INTERNAL_SERVER_ERROR;
}
/* parse the reponse */
result = ajp_parse_type(r, conn->data);
output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
/*
* Prepare apr_pollfd_t struct for possible later check if there is currently
* data available from the backend (do not flush response to client)
* or not (flush response to client)
*/
conn_poll = apr_pcalloc(p, sizeof(apr_pollfd_t));
conn_poll->reqevents = APR_POLLIN;
conn_poll->desc_type = APR_POLL_SOCKET;
conn_poll->desc.s = conn->sock;
bufsiz = maxsize;
for (;;) {
switch (result) {
case CMD_AJP13_GET_BODY_CHUNK:
if (havebody) {
if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
/* This is the end */
bufsiz = 0;
havebody = 0;
ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
"proxy: APR_BUCKET_IS_EOS");
} else {
status = ap_get_brigade(r->input_filters, input_brigade,
AP_MODE_READBYTES,
APR_BLOCK_READ,
maxsize - AJP_HEADER_SZ);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, status,
r->server,
"ap_get_brigade failed");
output_failed = 1;
break;
}
bufsiz = maxsize;
status = apr_brigade_flatten(input_brigade, buff,
&bufsiz);
apr_brigade_cleanup(input_brigade);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, status,
r->server,
"apr_brigade_flatten failed");
output_failed = 1;
break;
}
}
ajp_msg_reset(msg);
/* will go in ajp_send_data_msg */
status = ajp_send_data_msg(conn->sock, msg, bufsiz);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
"ajp_send_data_msg failed");
backend_failed = 1;
break;
}
conn->worker->s->transferred += bufsiz;
} else {
/*
* something is wrong TC asks for more body but we are
* already at the end of the body data
*/
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"ap_proxy_ajp_request error read after end");
backend_failed = 1;
}
break;
case CMD_AJP13_SEND_HEADERS:
/* AJP13_SEND_HEADERS: process them */
status = ajp_parse_header(r, conf, conn->data);
if (status != APR_SUCCESS) {
backend_failed = 1;
}
headers_sent = 1;
break;
case CMD_AJP13_SEND_BODY_CHUNK:
/* AJP13_SEND_BODY_CHUNK: piece of data */
status = ajp_parse_data(r, conn->data, &size, &buff);
if (status == APR_SUCCESS) {
/* AJP13_SEND_BODY_CHUNK with zero length
* is explicit flush message
*/
if (size == 0) {
if (headers_sent) {
e = apr_bucket_flush_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
}
else {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"Ignoring flush message received before headers");
}
}
else {
e = apr_bucket_transient_create(buff, size,
r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
if ((conn->worker->flush_packets == flush_on) ||
((conn->worker->flush_packets == flush_auto) &&
(apr_poll(conn_poll, 1, &conn_poll_fd,
conn->worker->flush_wait)
== APR_TIMEUP) ) ) {
e = apr_bucket_flush_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
}
apr_brigade_length(output_brigade, 0, &bb_len);
if (bb_len != -1)
conn->worker->s->read += bb_len;
}
if (ap_pass_brigade(r->output_filters,
output_brigade) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"proxy: error processing body");
output_failed = 1;
}
data_sent = 1;
apr_brigade_cleanup(output_brigade);
}
else {
backend_failed = 1;
}
break;
case CMD_AJP13_END_RESPONSE:
e = apr_bucket_eos_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
if (ap_pass_brigade(r->output_filters,
output_brigade) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"proxy: error processing end");
output_failed = 1;
}
/* XXX: what about flush here? See mod_jk */
data_sent = 1;
break;
default:
backend_failed = 1;
break;
}
/*
* If connection has been aborted by client: Stop working.
* Nevertheless, we regard our operation so far as a success:
* So reset output_failed to 0 and set result to CMD_AJP13_END_RESPONSE
* But: Close this connection to the backend.
*/
if (r->connection->aborted) {
conn->close++;
output_failed = 0;
result = CMD_AJP13_END_RESPONSE;
}
/*
* We either have finished successfully or we failed.
* So bail out
*/
if ((result == CMD_AJP13_END_RESPONSE) || backend_failed
|| output_failed)
break;
/* read the response */
status = ajp_read_header(conn->sock, r, maxsize,
(ajp_msg_t **)&(conn->data));
if (status != APR_SUCCESS) {
backend_failed = 1;
ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
"ajp_read_header failed");
break;
}
result = ajp_parse_type(r, conn->data);
}
apr_brigade_destroy(input_brigade);
/*
* Clear output_brigade to remove possible buckets that remained there
* after an error.
*/
apr_brigade_cleanup(output_brigade);
if (backend_failed || output_failed) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: Processing of request failed backend: %i, "
"output: %i", backend_failed, output_failed);
/* We had a failure: Close connection to backend */
conn->close++;
/* Return DONE to avoid error messages being added to the stream */
if (data_sent) {
rv = DONE;
}
}
else {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: got response from %pI (%s)",
conn->worker->cp->addr,
conn->worker->hostname);
rv = OK;
}
if (backend_failed) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
"proxy: dialog to %pI (%s) failed",
conn->worker->cp->addr,
conn->worker->hostname);
/*
* If we already send data, signal a broken backend connection
* upwards in the chain.
*/
if (data_sent) {
ap_proxy_backend_broke(r, output_brigade);
} else
rv = HTTP_SERVICE_UNAVAILABLE;
}
/*
* Ensure that we sent an EOS bucket thru the filter chain, if we already
* have sent some data. Maybe ap_proxy_backend_broke was called and added
* one to the brigade already (no longer making it empty). So we should
* not do this in this case.
*/
if (data_sent && !r->eos_sent && APR_BRIGADE_EMPTY(output_brigade)) {
e = apr_bucket_eos_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
}
/* If we have added something to the brigade above, sent it */
if (!APR_BRIGADE_EMPTY(output_brigade))
ap_pass_brigade(r->output_filters, output_brigade);
apr_brigade_destroy(output_brigade);
if (apr_table_get(r->subprocess_env, "proxy-nokeepalive")) {
conn->close++;
}
return rv;
}
/*
* This handles ajp:// URLs
*/
static int proxy_ajp_handler(request_rec *r, proxy_worker *worker,
proxy_server_conf *conf,
char *url, const char *proxyname,
apr_port_t proxyport)
{
int status;
char server_portstr[32];
conn_rec *origin = NULL;
proxy_conn_rec *backend = NULL;
const char *scheme = "AJP";
proxy_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
&proxy_module);
/*
* Note: Memory pool allocation.
* A downstream keepalive connection is always connected to the existence
* (or not) of an upstream keepalive connection. If this is not done then
* load balancing against multiple backend servers breaks (one backend
* server ends up taking 100% of the load), and the risk is run of
* downstream keepalive connections being kept open unnecessarily. This
* keeps webservers busy and ties up resources.
*
* As a result, we allocate all sockets out of the upstream connection
* pool, and when we want to reuse a socket, we check first whether the
* connection ID of the current upstream connection is the same as that
* of the connection when the socket was opened.
*/
apr_pool_t *p = r->connection->pool;
apr_uri_t *uri = apr_palloc(r->connection->pool, sizeof(*uri));
if (strncasecmp(url, "ajp:", 4) != 0) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: AJP: declining URL %s", url);
return DECLINED;
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: AJP: serving URL %s", url);
/* create space for state information */
status = ap_proxy_acquire_connection(scheme, &backend, worker,
r->server);
if (status != OK) {
if (backend) {
backend->close = 1;
ap_proxy_release_connection(scheme, backend, r->server);
}
return status;
}
backend->is_ssl = 0;
backend->close = 0;
/* Step One: Determine Who To Connect To */
status = ap_proxy_determine_connection(p, r, conf, worker, backend,
uri, &url, proxyname, proxyport,
server_portstr,
sizeof(server_portstr));
if (status != OK)
goto cleanup;
/* Step Two: Make the Connection */
if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"proxy: AJP: failed to make connection to backend: %s",
backend->hostname);
status = HTTP_SERVICE_UNAVAILABLE;
goto cleanup;
}
/* Handle CPING/CPONG */
if (worker->ping_timeout_set) {
status = ajp_handle_cping_cpong(backend->sock, r,
worker->ping_timeout);
if (status != APR_SUCCESS) {
backend->close++;
ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
"proxy: AJP: cping/cpong failed to %pI (%s)",
worker->cp->addr,
worker->hostname);
status = HTTP_SERVICE_UNAVAILABLE;
goto cleanup;
}
}
/* Step Three: Process the Request */
status = ap_proxy_ajp_request(p, r, backend, origin, dconf, uri, url,
server_portstr);
cleanup:
/* Do not close the socket */
ap_proxy_release_connection(scheme, backend, r->server);
return status;
}
static void ap_proxy_http_register_hook(apr_pool_t *p)
{
proxy_hook_scheme_handler(proxy_ajp_handler, NULL, NULL, APR_HOOK_FIRST);
proxy_hook_canon_handler(proxy_ajp_canon, NULL, NULL, APR_HOOK_FIRST);
}
module AP_MODULE_DECLARE_DATA proxy_ajp_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
NULL, /* command apr_table_t */
ap_proxy_http_register_hook /* register hooks */
};