mod_proxy_ajp.c revision 3ccd32fa0cabe48785497e239e0e30bec6bdf10b
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* AJP routines for Apache proxy */
#include "mod_proxy.h"
#include "ajp.h"
/*
* Canonicalise http-like URLs.
* scheme is the scheme for the URL
* url is the URL starting with the first '/'
* def_port is the default port for this scheme.
*/
{
const char *err;
/* ap_port_of_scheme() */
url += 4;
}
else {
return DECLINED;
}
"proxy: AJP: canonicalising URL %s", url);
/*
* do syntactic check.
* We break the URL into host, port, path, search
*/
if (err) {
"error parsing URL %s: %s",
return HTTP_BAD_REQUEST;
}
/*
* process the path. With proxy-nocanon set (by
* mod_proxy) we use the raw, unparsed uri
*/
}
else {
r->proxyreq);
}
return HTTP_BAD_REQUEST;
/* if literal IPv6 address */
}
return OK;
}
#define METHOD_NON_IDEMPOTENT 0
#define METHOD_IDEMPOTENT 1
#define METHOD_IDEMPOTENT_WITH_ARGS 2
static int is_idempotent(request_rec *r)
{
/*
* RFC2616 (9.1.2): GET, HEAD, PUT, DELETE, OPTIONS, TRACE are considered
* idempotent. Hint: HEAD requests use M_GET as method number as well.
*/
switch (r->method_number) {
case M_GET:
case M_DELETE:
case M_PUT:
case M_OPTIONS:
case M_TRACE:
/*
* If the request has arguments it might have side-effects and thus
* it might be undesirable to resent it to a backend again
* automatically.
*/
if (r->args) {
return METHOD_IDEMPOTENT_WITH_ARGS;
}
return METHOD_IDEMPOTENT;
/* Everything else is not considered idempotent. */
default:
return METHOD_NON_IDEMPOTENT;
}
}
{
if (r->clength > 0) {
return r->clength;
}
if (clp) {
char *errp;
len = 0; /* parse error */
}
}
}
return len;
}
/*
* XXX: AJP Auto Flushing
*
* 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.
*/
/*
* process the request and write the response.
*/
char *url, char *server_portstr)
{
int result;
apr_bucket *e;
apr_size_t bufsiz = 0;
char *buff;
char *send_body_chunk_buff;
const char *tenc;
int havebody = 1;
int output_failed = 0;
int backend_failed = 0;
int data_sent = 0;
int request_ended = 0;
int headers_sent = 0;
int rv = 0;
int send_body = 0;
apr_off_t content_length = 0;
if (psf->io_buffer_size_set)
if (maxsize > AJP_MAX_BUFFER_SZ)
else if (maxsize < AJP_MSG_BUFFER_SZ)
/*
* Send the AJP request to the remote server
*/
/* send request headers */
if (status != APR_SUCCESS) {
"proxy: AJP: request failed to %pI (%s)",
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 */
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
"proxy: ajp_alloc_data_msg failed");
return HTTP_INTERNAL_SERVER_ERROR;
}
/* read the first bloc of data */
/* The AJP protocol does not want body data yet */
"proxy: request is chunked");
} else {
/* Get client provided Content-Length header */
maxsize - AJP_HEADER_SZ);
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
"proxy: ap_get_brigade failed");
return HTTP_BAD_REQUEST;
}
/* have something */
"proxy: APR_BUCKET_IS_EOS");
}
/* Try to send something */
"proxy: data to read (max %" APR_SIZE_T_FMT
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
"proxy: apr_brigade_flatten");
return HTTP_INTERNAL_SERVER_ERROR;
}
if (bufsiz > 0) {
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
"proxy: send failed to %pI (%s)",
/*
* It is fatal when we failed to send a (part) of the request
* body.
*/
return HTTP_INTERNAL_SERVER_ERROR;
}
send_body = 1;
}
else if (content_length > 0) {
"proxy: read zero bytes, expecting"
/*
* We can only get here if the client closed the connection
* to us without sending the body.
* Now the connection is in the wrong state on the backend.
* Sending an empty data msg doesn't help either as it does
* not move this connection to the correct state on the backend
* for later resusage by the next request again.
* Close it to clean things up.
*/
return HTTP_BAD_REQUEST;
}
}
/* read the response */
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
"proxy: read response failed from %pI (%s)",
/*
* 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.
*/
return HTTP_SERVICE_UNAVAILABLE;
}
return HTTP_INTERNAL_SERVER_ERROR;
}
/* parse the reponse */
/*
* 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)
*/
for (;;) {
switch (result) {
case CMD_AJP13_GET_BODY_CHUNK:
if (havebody) {
/* This is the end */
bufsiz = 0;
havebody = 0;
"proxy: APR_BUCKET_IS_EOS");
} else {
maxsize - AJP_HEADER_SZ);
if (status != APR_SUCCESS) {
r->server,
"ap_get_brigade failed");
output_failed = 1;
break;
}
&bufsiz);
if (status != APR_SUCCESS) {
r->server,
"apr_brigade_flatten failed");
output_failed = 1;
break;
}
}
/* will go in ajp_send_data_msg */
if (status != APR_SUCCESS) {
"ajp_send_data_msg failed");
backend_failed = 1;
break;
}
} else {
/*
* something is wrong TC asks for more body but we are
* already at the end of the body data
*/
"ap_proxy_ajp_request error read after end");
backend_failed = 1;
}
break;
case CMD_AJP13_SEND_HEADERS:
if (headers_sent) {
/* Do not send anything to the client.
* Backend already send us the headers.
*/
backend_failed = 1;
"proxy: Backend sent headers twice.");
break;
}
/* AJP13_SEND_HEADERS: process them */
if (status != APR_SUCCESS) {
backend_failed = 1;
}
headers_sent = 1;
break;
/* AJP13_SEND_BODY_CHUNK: piece of data */
if (status == APR_SUCCESS) {
/* AJP13_SEND_BODY_CHUNK with zero length
* is explicit flush message
*/
if (size == 0) {
if (headers_sent) {
}
else {
"Ignoring flush message received before headers");
}
}
else {
r->connection->bucket_alloc);
!= APR_SUCCESS) &&
APR_STATUS_IS_TIMEUP(rv))) {
}
if (bb_len != -1)
}
if (ap_pass_brigade(r->output_filters,
output_brigade) != APR_SUCCESS) {
"proxy: error processing body.%s",
r->connection->aborted ?
" Client aborted connection." : "");
output_failed = 1;
}
data_sent = 1;
}
else {
backend_failed = 1;
}
break;
case CMD_AJP13_END_RESPONSE:
if (ap_pass_brigade(r->output_filters,
output_brigade) != APR_SUCCESS) {
"proxy: error processing end");
output_failed = 1;
}
/* XXX: what about flush here? See mod_jk */
data_sent = 1;
request_ended = 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) {
output_failed = 0;
request_ended = 1;
}
/*
* We either have finished successfully or we failed.
* So bail out
*/
|| output_failed)
break;
/* read the response */
if (status != APR_SUCCESS) {
backend_failed = 1;
"ajp_read_header failed");
break;
}
}
/*
* Clear output_brigade to remove possible buckets that remained there
* after an error.
*/
if (backend_failed || output_failed) {
"proxy: Processing of request failed backend: %i, "
/* We had a failure: Close connection to backend */
/* Return DONE to avoid error messages being added to the stream */
if (data_sent) {
}
}
else if (!request_ended) {
"proxy: Processing of request didn't terminate cleanly");
/* We had a failure: Close connection to backend */
backend_failed = 1;
/* Return DONE to avoid error messages being added to the stream */
if (data_sent) {
}
}
else {
"proxy: got response from %pI (%s)",
}
if (backend_failed) {
"proxy: dialog to %pI (%s) failed",
/*
* If we already send data, signal a broken backend connection
* upwards in the chain.
*/
if (data_sent) {
} else
}
/*
* 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 we have added something to the brigade above, sent it */
if (!APR_BRIGADE_EMPTY(output_brigade))
}
return rv;
}
/*
* This handles ajp:// URLs
*/
{
int status;
char server_portstr[32];
const char *scheme = "AJP";
int retry;
&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.
*/
"proxy: AJP: declining URL %s", url);
return DECLINED;
}
"proxy: AJP: serving URL %s", url);
/* create space for state information */
r->server);
if (backend) {
}
return status;
}
retry = 0;
while (retry < 2) {
/* Step One: Determine Who To Connect To */
sizeof(server_portstr));
break;
/* Step Two: Make the Connection */
"proxy: AJP: failed to make connection to backend: %s",
break;
}
if (worker->ping_timeout_set) {
/*
* In case the CPING / CPONG failed for the first time we might be
* just out of luck and got a faulty backend connection, but the
* backend might be healthy nevertheless. So ensure that the backend
* TCP connection gets closed and try it once again.
*/
if (status != APR_SUCCESS) {
retry++;
continue;
}
}
/* Step Three: Process the Request */
break;
}
/* Do not close the socket */
return status;
}
static void ap_proxy_http_register_hook(apr_pool_t *p)
{
}
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 */
};