mod_proxy_ajp.c revision 188befd3a49e3a126bd801d7dc5a7f6e63ad4332
/* 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;
}
/*
*
* 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.
*/
if (r->uri == r->unparsed_uri) {
*(search++) = '\0';
}
else
/* process path */
r->proxyreq);
return HTTP_BAD_REQUEST;
/* if literal IPv6 address */
}
return OK;
}
/*
* 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;
char *buff;
const char *tenc;
int havebody = 1;
int isok = 1;
int data_sent = 0;
int rv = 0;
/*
* 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
return HTTP_SERVICE_UNAVAILABLE;
}
/* 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 {
if (status != APR_SUCCESS) {
"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)",
return HTTP_SERVICE_UNAVAILABLE;
}
}
}
/* read the response */
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
"proxy: read response failed from %pI (%s)",
return HTTP_SERVICE_UNAVAILABLE;
}
/* 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)
*/
while (isok) {
switch (result) {
case CMD_AJP13_GET_BODY_CHUNK:
if (havebody) {
/* This is the end */
bufsiz = 0;
havebody = 0;
"proxy: APR_BUCKET_IS_EOS");
} else {
if (status != APR_SUCCESS) {
r->server,
"ap_get_brigade failed");
break;
}
&bufsiz);
if (status != APR_SUCCESS) {
r->server,
"apr_brigade_flatten failed");
break;
}
}
/* will go in ajp_send_data_msg */
if (status != APR_SUCCESS) {
"ajp_send_data_msg failed");
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");
isok = 0;
}
break;
case CMD_AJP13_SEND_HEADERS:
/* AJP13_SEND_HEADERS: process them */
if (status != APR_SUCCESS) {
isok = 0;
}
break;
/* AJP13_SEND_BODY_CHUNK: piece of data */
if (status == APR_SUCCESS) {
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");
isok = 0;
}
data_sent = 1;
}
else {
isok = 0;
}
break;
case CMD_AJP13_END_RESPONSE:
if (ap_pass_brigade(r->output_filters,
output_brigade) != APR_SUCCESS) {
"proxy: error processing body");
isok = 0;
}
/* XXX: what about flush here? See mod_jk */
data_sent = 1;
break;
default:
isok = 0;
break;
}
/*
* If connection has been aborted by client: Stop working.
* Nevertheless, we regard our operation so far as a success:
* So do not set isok to 0 and set result to CMD_AJP13_END_RESPONSE
* But: Close this connection to the backend.
*/
if (r->connection->aborted) {
break;
}
if (!isok)
break;
if (result == CMD_AJP13_END_RESPONSE)
break;
/* read the response */
if (status != APR_SUCCESS) {
isok = 0;
"ajp_read_header failed");
break;
}
}
/*
* Clear output_brigade to remove possible buckets that remained there
* after an error.
*/
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
"proxy: send body failed to %pI (%s)",
/*
* If we already send data, signal a broken backend connection
* upwards in the chain.
*/
if (data_sent) {
/* Return DONE to avoid error messages being added to the stream */
} 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))
if (rv)
return rv;
/* Nice we have answer to send to the client */
"proxy: got response from %pI (%s)",
return OK;
}
"proxy: got bad response (%d) from %pI (%s)",
/* We had a failure: Close connection to backend */
return HTTP_SERVICE_UNAVAILABLE;
}
/*
* 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 */
if (!backend) {
r->server);
if (backend) {
}
return status;
}
}
backend->close_on_recycle = 0;
/* 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 */
};