mod_proxy_ajp.c revision 62c48ce0fc62af721a6ee30a74cfc664c9714583
842ae4bd224140319ae7feec1872b93dfd491143fielding/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
842ae4bd224140319ae7feec1872b93dfd491143fielding * applicable.
842ae4bd224140319ae7feec1872b93dfd491143fielding *
842ae4bd224140319ae7feec1872b93dfd491143fielding * Licensed under the Apache License, Version 2.0 (the "License");
842ae4bd224140319ae7feec1872b93dfd491143fielding * you may not use this file except in compliance with the License.
842ae4bd224140319ae7feec1872b93dfd491143fielding * You may obtain a copy of the License at
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * http://www.apache.org/licenses/LICENSE-2.0
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* AJP routines for Apache proxy */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "mod_proxy.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#include "ajp.h"
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingmodule AP_MODULE_DECLARE_DATA proxy_ajp_module;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
7764153915ff9a646f32cbd6e39c26f3609829abtrawick/*
7764153915ff9a646f32cbd6e39c26f3609829abtrawick * Canonicalise http-like URLs.
2dcfdce30a4dabc6a194c367c9ef5e53d37df638jwoolley * scheme is the scheme for the URL
6865813dee5d3c1ebf12dd810368171792a0190atrawick * url is the URL starting with the first '/'
024cd9589e52cf11ce765dfddb5b5f0c6e421a48gstein * def_port is the default port for this scheme.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingstatic int proxy_ajp_canon(request_rec *r, char *url)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
d4f1d9c1ff112a8ab9bee31f196973761329b236rbb char *host, *path, *search, sport[7];
d4f1d9c1ff112a8ab9bee31f196973761329b236rbb const char *err;
4d12805e6c18253040223ea637acd6b3b3c18f60jorton apr_port_t port = AJP13_DEF_PORT;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* ap_port_of_scheme() */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (strncasecmp(url, "ajp:", 4) == 0) {
4d12805e6c18253040223ea637acd6b3b3c18f60jorton url += 4;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding else {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return DECLINED;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding
a277da68150d79889e3a7570925a16ece13b6659trawick ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding "proxy: AJP: canonicalising URL %s", url);
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding
e8f95a682820a599fe41b22977010636be5c2717jim /*
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding * do syntactic check.
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding * We break the URL into host, port, path, search
e8f95a682820a599fe41b22977010636be5c2717jim */
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding if (err) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "error parsing URL %s: %s",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding url, err);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return HTTP_BAD_REQUEST;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /*
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * now parse path/search args, according to rfc1738
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * N.B. if this isn't a true proxy request, then the URL _path_
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * has already been decoded. True proxy requests have
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * r->uri == r->unparsed_uri, and no others have that property.
4d12805e6c18253040223ea637acd6b3b3c18f60jorton */
4d12805e6c18253040223ea637acd6b3b3c18f60jorton if (r->uri == r->unparsed_uri) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding search = strchr(url, '?');
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (search != NULL)
4d12805e6c18253040223ea637acd6b3b3c18f60jorton *(search++) = '\0';
4d12805e6c18253040223ea637acd6b3b3c18f60jorton }
4d12805e6c18253040223ea637acd6b3b3c18f60jorton else
4d12805e6c18253040223ea637acd6b3b3c18f60jorton search = r->args;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
a93bec588d3829a236a6dc1dca8d51c4eca98d23sf /* process path */
a93bec588d3829a236a6dc1dca8d51c4eca98d23sf path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding r->proxyreq);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (path == NULL)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return HTTP_BAD_REQUEST;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_snprintf(sport, sizeof(sport), ":%d", port);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (ap_strchr_c(host, ':')) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* if literal IPv6 address */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding host = apr_pstrcat(r->pool, "[", host, "]", NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding r->filename = apr_pstrcat(r->pool, "proxy:ajp://", host, sport,
4d12805e6c18253040223ea637acd6b3b3c18f60jorton "/", path, (search) ? "?" : "",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (search) ? search : "", NULL);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return OK;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding}
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/*
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * XXX: Flushing bandaid
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * When processing CMD_AJP13_SEND_BODY_CHUNK AJP messages we will do a poll
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * with FLUSH_WAIT miliseconds timeout to determine if more data is currently
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * available at the backend. If there is no more data available, we flush
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * the data to the client by adding a flush bucket to the brigade we pass
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * up the filter chain.
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * This is only a bandaid to fix the AJP/1.3 protocol shortcoming of not
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * sending (actually not having defined) a flush message, when the data
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * should be flushed to the client. As soon as this protocol shortcoming is
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * fixed this code should be removed.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding *
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * For further discussion see PR37100.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * http://issues.apache.org/bugzilla/show_bug.cgi?id=37100
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#define FLUSHING_BANDAID 1
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef FLUSHING_BANDAID
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/*
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Wait 10000 microseconds to find out if more data is currently
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * available at the backend. Just an arbitrary choose.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#define FLUSH_WAIT 10000
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#endif
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/*
f95db61e2d8538db83aeef9ce7f9bfef11828ec2sf * process the request and write the response.
f95db61e2d8538db83aeef9ce7f9bfef11828ec2sf */
f95db61e2d8538db83aeef9ce7f9bfef11828ec2sfstatic int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
f95db61e2d8538db83aeef9ce7f9bfef11828ec2sf proxy_conn_rec *conn,
f95db61e2d8538db83aeef9ce7f9bfef11828ec2sf conn_rec *origin,
f95db61e2d8538db83aeef9ce7f9bfef11828ec2sf proxy_dir_conf *conf,
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding apr_uri_t *uri,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *url, char *server_portstr)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding{
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_status_t status;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int result;
4d12805e6c18253040223ea637acd6b3b3c18f60jorton apr_bucket *e;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_bucket_brigade *input_brigade;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_bucket_brigade *output_brigade;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ajp_msg_t *msg;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_size_t bufsiz;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding char *buff;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_uint16_t size;
9d0665da83d1e22c0ea0e5f6f940f70f75bf5237ianh const char *tenc;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int havebody = 1;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding int isok = 1;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_off_t bb_len;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding#ifdef FLUSHING_BANDAID
2dcfdce30a4dabc6a194c367c9ef5e53d37df638jwoolley apr_int32_t conn_poll_fd;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_pollfd_t *conn_poll;
4d12805e6c18253040223ea637acd6b3b3c18f60jorton#endif
4d12805e6c18253040223ea637acd6b3b3c18f60jorton
4d12805e6c18253040223ea637acd6b3b3c18f60jorton /*
4d12805e6c18253040223ea637acd6b3b3c18f60jorton * Send the AJP request to the remote server
4d12805e6c18253040223ea637acd6b3b3c18f60jorton */
4d12805e6c18253040223ea637acd6b3b3c18f60jorton
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* send request headers */
4d12805e6c18253040223ea637acd6b3b3c18f60jorton status = ajp_send_header(conn->sock, r, uri);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (status != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->close++;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "proxy: AJP: request failed to %pI (%s)",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->worker->cp->addr,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->worker->hostname);
9d0665da83d1e22c0ea0e5f6f940f70f75bf5237ianh if (status == AJP_EOVERFLOW)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return HTTP_BAD_REQUEST;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding else
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm return HTTP_SERVICE_UNAVAILABLE;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
4d12805e6c18253040223ea637acd6b3b3c18f60jorton
4d12805e6c18253040223ea637acd6b3b3c18f60jorton /* allocate an AJP message to store the data of the buckets */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding status = ajp_alloc_data_msg(r->pool, &buff, &bufsiz, &msg);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (status != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* We had a failure: Close connection to backend */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->close++;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "proxy: ajp_alloc_data_msg failed");
650d8321b62cccd1830684935bb5362b4c495b17sf return HTTP_INTERNAL_SERVER_ERROR;
650d8321b62cccd1830684935bb5362b4c495b17sf }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* read the first bloc of data */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding input_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (tenc && (strcasecmp(tenc, "chunked") == 0)) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* The AJP protocol does not want body data yet */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "proxy: request is chunked");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding } else {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding status = ap_get_brigade(r->input_filters, input_brigade,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding AP_MODE_READBYTES, APR_BLOCK_READ,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding AJP13_MAX_SEND_BODY_SZ);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (status != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
ce4dc40a4e87991087488f70d96d3447d7557294sf "proxy: ap_get_brigade failed");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_brigade_destroy(input_brigade);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return HTTP_INTERNAL_SERVER_ERROR;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* have something */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "proxy: APR_BUCKET_IS_EOS");
43c3e6a4b559b76b750c245ee95e2782c15b4296jim }
43c3e6a4b559b76b750c245ee95e2782c15b4296jim
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* Try to send something */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "proxy: data to read (max %" APR_SIZE_T_FMT
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding " at %" APR_SIZE_T_FMT ")", bufsiz, msg->pos);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
4d12805e6c18253040223ea637acd6b3b3c18f60jorton status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding if (status != APR_SUCCESS) {
3511969853863eeb6e80018afe63831e5bf81447rpluem /* We had a failure: Close connection to backend */
8079d062f963eccadaa8f584df5c5aa33be09525rbb conn->close++;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_brigade_destroy(input_brigade);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "proxy: apr_brigade_flatten");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return HTTP_INTERNAL_SERVER_ERROR;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_brigade_cleanup(input_brigade);
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "proxy: got %" APR_SIZE_T_FMT " bytes of data", bufsiz);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (bufsiz > 0) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding status = ajp_send_data_msg(conn->sock, msg, bufsiz);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (status != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* We had a failure: Close connection to backend */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->close++;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding apr_brigade_destroy(input_brigade);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
e8f95a682820a599fe41b22977010636be5c2717jim "proxy: send failed to %pI (%s)",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->worker->cp->addr,
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm conn->worker->hostname);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding return HTTP_SERVICE_UNAVAILABLE;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->worker->s->transferred += bufsiz;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding }
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* read the response */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->data = NULL;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding status = ajp_read_header(conn->sock, r,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding (ajp_msg_t **)&(conn->data));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding if (status != APR_SUCCESS) {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* We had a failure: Close connection to backend */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->close++;
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding apr_brigade_destroy(input_brigade);
ea1fb61cbff78b1e1e8d01acc5df0fb495d979c8fielding ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "proxy: read response failed from %pI (%s)",
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding conn->worker->cp->addr,
ce4dc40a4e87991087488f70d96d3447d7557294sf conn->worker->hostname);
ce4dc40a4e87991087488f70d96d3447d7557294sf return HTTP_SERVICE_UNAVAILABLE;
ce4dc40a4e87991087488f70d96d3447d7557294sf }
ce4dc40a4e87991087488f70d96d3447d7557294sf /* parse the reponse */
ce4dc40a4e87991087488f70d96d3447d7557294sf result = ajp_parse_type(r, conn->data);
ce4dc40a4e87991087488f70d96d3447d7557294sf output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
ce4dc40a4e87991087488f70d96d3447d7557294sf
ce4dc40a4e87991087488f70d96d3447d7557294sf#ifdef FLUSHING_BANDAID
ce4dc40a4e87991087488f70d96d3447d7557294sf /*
ce4dc40a4e87991087488f70d96d3447d7557294sf * Prepare apr_pollfd_t struct for later check if there is currently
ce4dc40a4e87991087488f70d96d3447d7557294sf * data available from the backend (do not flush response to client)
ce4dc40a4e87991087488f70d96d3447d7557294sf * or not (flush response to client)
ce4dc40a4e87991087488f70d96d3447d7557294sf */
ce4dc40a4e87991087488f70d96d3447d7557294sf conn_poll = apr_pcalloc(p, sizeof(apr_pollfd_t));
ce4dc40a4e87991087488f70d96d3447d7557294sf conn_poll->reqevents = APR_POLLIN;
ce4dc40a4e87991087488f70d96d3447d7557294sf conn_poll->desc_type = APR_POLL_SOCKET;
ce4dc40a4e87991087488f70d96d3447d7557294sf conn_poll->desc.s = conn->sock;
ce4dc40a4e87991087488f70d96d3447d7557294sf#endif
ce4dc40a4e87991087488f70d96d3447d7557294sf
ce4dc40a4e87991087488f70d96d3447d7557294sf bufsiz = AJP13_MAX_SEND_BODY_SZ;
ce4dc40a4e87991087488f70d96d3447d7557294sf while (isok) {
ce4dc40a4e87991087488f70d96d3447d7557294sf switch (result) {
ce4dc40a4e87991087488f70d96d3447d7557294sf case CMD_AJP13_GET_BODY_CHUNK:
ce4dc40a4e87991087488f70d96d3447d7557294sf if (havebody) {
ce4dc40a4e87991087488f70d96d3447d7557294sf if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
ce4dc40a4e87991087488f70d96d3447d7557294sf /* This is the end */
ce4dc40a4e87991087488f70d96d3447d7557294sf bufsiz = 0;
ce4dc40a4e87991087488f70d96d3447d7557294sf havebody = 0;
ce4dc40a4e87991087488f70d96d3447d7557294sf ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
ce4dc40a4e87991087488f70d96d3447d7557294sf "proxy: APR_BUCKET_IS_EOS");
ce4dc40a4e87991087488f70d96d3447d7557294sf } else {
ce4dc40a4e87991087488f70d96d3447d7557294sf status = ap_get_brigade(r->input_filters, input_brigade,
ce4dc40a4e87991087488f70d96d3447d7557294sf AP_MODE_READBYTES,
ce4dc40a4e87991087488f70d96d3447d7557294sf APR_BLOCK_READ,
ce4dc40a4e87991087488f70d96d3447d7557294sf AJP13_MAX_SEND_BODY_SZ);
ce4dc40a4e87991087488f70d96d3447d7557294sf if (status != APR_SUCCESS) {
ce4dc40a4e87991087488f70d96d3447d7557294sf ap_log_error(APLOG_MARK, APLOG_DEBUG, status,
ce4dc40a4e87991087488f70d96d3447d7557294sf r->server,
ce4dc40a4e87991087488f70d96d3447d7557294sf "ap_get_brigade failed");
ce4dc40a4e87991087488f70d96d3447d7557294sf break;
ce4dc40a4e87991087488f70d96d3447d7557294sf }
ce4dc40a4e87991087488f70d96d3447d7557294sf bufsiz = AJP13_MAX_SEND_BODY_SZ;
ce4dc40a4e87991087488f70d96d3447d7557294sf status = apr_brigade_flatten(input_brigade, buff,
ce4dc40a4e87991087488f70d96d3447d7557294sf &bufsiz);
ce4dc40a4e87991087488f70d96d3447d7557294sf apr_brigade_cleanup(input_brigade);
ce4dc40a4e87991087488f70d96d3447d7557294sf if (status != APR_SUCCESS) {
ce4dc40a4e87991087488f70d96d3447d7557294sf ap_log_error(APLOG_MARK, APLOG_DEBUG, status,
ce4dc40a4e87991087488f70d96d3447d7557294sf r->server,
ce4dc40a4e87991087488f70d96d3447d7557294sf "apr_brigade_flatten failed");
ce4dc40a4e87991087488f70d96d3447d7557294sf break;
ce4dc40a4e87991087488f70d96d3447d7557294sf }
ce4dc40a4e87991087488f70d96d3447d7557294sf }
ce4dc40a4e87991087488f70d96d3447d7557294sf
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ajp_msg_reset(msg);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* will go in ajp_send_data_msg */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding status = ajp_send_data_msg(conn->sock, msg, bufsiz);
73e8b26287de5c06fa470d36162e103dbac9c7e5wrowe if (status != APR_SUCCESS) {
8079d062f963eccadaa8f584df5c5aa33be09525rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe "ajp_send_data_msg failed");
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe break;
ce4dc40a4e87991087488f70d96d3447d7557294sf }
ce4dc40a4e87991087488f70d96d3447d7557294sf conn->worker->s->transferred += bufsiz;
8079d062f963eccadaa8f584df5c5aa33be09525rbb } else {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /*
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf * something is wrong TC asks for more body but we are
8079d062f963eccadaa8f584df5c5aa33be09525rbb * already at the end of the body data
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding "ap_proxy_ajp_request error read after end");
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding isok = 0;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm }
8079d062f963eccadaa8f584df5c5aa33be09525rbb break;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding case CMD_AJP13_SEND_HEADERS:
/* AJP13_SEND_HEADERS: process them */
status = ajp_parse_header(r, conf, conn->data);
if (status != APR_SUCCESS) {
isok = 0;
}
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) {
e = apr_bucket_transient_create(buff, size,
r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
#ifdef FLUSHING_BANDAID
/*
* If there is no more data available from backend side
* currently, flush response to client.
*/
if (apr_poll(conn_poll, 1, &conn_poll_fd, FLUSH_WAIT)
== APR_TIMEUP) {
e = apr_bucket_flush_create(r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(output_brigade, e);
}
#endif
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");
isok = 0;
}
apr_brigade_cleanup(output_brigade);
}
else {
isok = 0;
}
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 body");
isok = 0;
}
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) {
conn->close++;
result = CMD_AJP13_END_RESPONSE;
break;
}
if (!isok)
break;
if (result == CMD_AJP13_END_RESPONSE)
break;
/* read the response */
status = ajp_read_header(conn->sock, r,
(ajp_msg_t **)&(conn->data));
if (status != APR_SUCCESS) {
isok = 0;
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);
apr_brigade_destroy(output_brigade);
if (status != APR_SUCCESS) {
/* We had a failure: Close connection to backend */
conn->close++;
ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
"proxy: send body failed to %pI (%s)",
conn->worker->cp->addr,
conn->worker->hostname);
return HTTP_SERVICE_UNAVAILABLE;
}
/* Nice we have answer to send to the client */
if (result == CMD_AJP13_END_RESPONSE && isok) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"proxy: got response from %pI (%s)",
conn->worker->cp->addr,
conn->worker->hostname);
return OK;
}
ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
"proxy: got bad response (%d) from %pI (%s)",
result,
conn->worker->cp->addr,
conn->worker->hostname);
/* We had a failure: Close connection to backend */
conn->close++;
return HTTP_SERVICE_UNAVAILABLE;
}
/*
* 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 */
if (!backend) {
status = ap_proxy_acquire_connection(scheme, &backend, worker,
r->server);
if (status != OK) {
if (backend) {
backend->close_on_recycle = 1;
ap_proxy_release_connection(scheme, backend, r->server);
}
return status;
}
}
backend->is_ssl = 0;
backend->close_on_recycle = 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;
}
/* 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 */
};