mod_proxy_ajp.c revision 638f1e58d1cab42397613af5155264832e3365d8
/* 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;
const char *pnocanon;
/* 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;
}
/*
*
* N.B. if this isn't a true proxy request, then the URL _path_
* has already been decoded. True proxy requests have
* r->uri == r->unparsed_uri, and no others have that property.
*/
*(search++) = '\0';
}
else
/* process path */
if (pnocanon) {
}
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;
}
}
/*
* 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;
const char *tenc;
int havebody = 1;
int output_failed = 0;
int backend_failed = 0;
int data_sent = 0;
int headers_sent = 0;
int rv = 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 {
maxsize - AJP_HEADER_SZ);
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
"proxy: ap_get_brigade failed");
return HTTP_INTERNAL_SERVER_ERROR;
}
/* 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;
}
}
}
/* 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:
/* 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_TIMEUP) ) ) {
}
if (bb_len != -1)
}
if (ap_pass_brigade(r->output_filters,
output_brigade) != APR_SUCCESS) {
"proxy: error processing body");
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;
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;
}
/*
* 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 {
"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";
&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;
}
/* Step One: Determine Who To Connect To */
sizeof(server_portstr));
goto cleanup;
/* Step Two: Make the Connection */
"proxy: AJP: failed to make connection to backend: %s",
goto cleanup;
}
if (worker->ping_timeout_set) {
if (status != APR_SUCCESS) {
goto cleanup;
}
}
/* Step Three: Process the Request */
/* 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 */
};