mod_proxy_ajp.c revision 5d392744e2077f71f34ce098ab49d2c0ddcf4ea3
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * applicable.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Licensed under the Apache License, Version 2.0 (the "License");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * you may not use this file except in compliance with the License.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * You may obtain a copy of the License at
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * http://www.apache.org/licenses/LICENSE-2.0
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Unless required by applicable law or agreed to in writing, software
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * distributed under the License is distributed on an "AS IS" BASIS,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * See the License for the specific language governing permissions and
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * limitations under the License.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* AJP routines for Apache proxy */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "mod_proxy.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb#include "ajp.h"
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbmodule AP_MODULE_DECLARE_DATA proxy_ajp_module;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Canonicalise http-like URLs.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * scheme is the scheme for the URL
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * url is the URL starting with the first '/'
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * def_port is the default port for this scheme.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int proxy_ajp_canon(request_rec *r, char *url)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char *host, *path, *search, sport[7];
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb const char *err;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_port_t port = AJP13_DEF_PORT;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* ap_port_of_scheme() */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (strncasecmp(url, "ajp:", 4) == 0) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb url += 4;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb else {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return DECLINED;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: AJP: canonicalising URL %s", url);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * do syntactic check.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * We break the URL into host, port, path, search
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (err) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "error parsing URL %s: %s",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb url, err);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return HTTP_BAD_REQUEST;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * now parse path/search args, according to rfc1738
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * N.B. if this isn't a true proxy request, then the URL _path_
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * has already been decoded. True proxy requests have
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * r->uri == r->unparsed_uri, and no others have that property.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (r->uri == r->unparsed_uri) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb search = strchr(url, '?');
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (search != NULL)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *(search++) = '\0';
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb else
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb search = r->args;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* process path */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb r->proxyreq);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (path == NULL)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return HTTP_BAD_REQUEST;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_snprintf(sport, sizeof(sport), ":%d", port);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (ap_strchr_c(host, ':')) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* if literal IPv6 address */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb host = apr_pstrcat(r->pool, "[", host, "]", NULL);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb r->filename = apr_pstrcat(r->pool, "proxy:ajp://", host, sport,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "/", path, (search) ? "?" : "",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb (search) ? search : "", NULL);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return OK;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * XXX: AJP Auto Flushing
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * When processing CMD_AJP13_SEND_BODY_CHUNK AJP messages we will do a poll
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe * with FLUSH_WAIT miliseconds timeout to determine if more data is currently
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * available at the backend. If there is no more data available, we flush
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * the data to the client by adding a flush bucket to the brigade we pass
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * up the filter chain.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * This is only a bandaid to fix the AJP/1.3 protocol shortcoming of not
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * sending (actually not having defined) a flush message, when the data
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * should be flushed to the client. As soon as this protocol shortcoming is
b45c1c292ff1fa635004ae81fa691f8cb3cdda85rbb * fixed this code should be removed.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * For further discussion see PR37100.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * http://issues.apache.org/bugzilla/show_bug.cgi?id=37100
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe
a2a0abd88b19e042a3eb2a9fa1702c25ad51303dwrowe/*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * process the request and write the response.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb proxy_conn_rec *conn,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb conn_rec *origin,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb proxy_dir_conf *conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_uri_t *uri,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char *url, char *server_portstr)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_status_t status;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb int result;
c3e342e5b0b9fea6617ee16d2da02c3ef2108126dougm apr_bucket *e;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_bucket_brigade *input_brigade;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_bucket_brigade *output_brigade;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe ajp_msg_t *msg;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_size_t bufsiz;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe char *buff;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_uint16_t size;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe const char *tenc;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe int havebody = 1;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe int isok = 1;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_off_t bb_len;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe int data_sent = 0;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe int rv = 0;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_int32_t conn_poll_fd;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_pollfd_t *conn_poll;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /*
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Send the AJP request to the remote server
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* send request headers */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe status = ajp_send_header(conn->sock, r, uri);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (status != APR_SUCCESS) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe conn->close++;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "proxy: AJP: request failed to %pI (%s)",
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe conn->worker->cp->addr,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe conn->worker->hostname);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (status == AJP_EOVERFLOW)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe return HTTP_BAD_REQUEST;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe else
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe return HTTP_SERVICE_UNAVAILABLE;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* allocate an AJP message to store the data of the buckets */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe status = ajp_alloc_data_msg(r->pool, &buff, &bufsiz, &msg);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (status != APR_SUCCESS) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* We had a failure: Close connection to backend */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn->close++;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: ajp_alloc_data_msg failed");
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return HTTP_INTERNAL_SERVER_ERROR;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* read the first bloc of data */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe input_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (tenc && (strcasecmp(tenc, "chunked") == 0)) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* The AJP protocol does not want body data yet */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: request is chunked");
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe } else {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe status = ap_get_brigade(r->input_filters, input_brigade,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe AP_MODE_READBYTES, APR_BLOCK_READ,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe AJP13_MAX_SEND_BODY_SZ);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
4775dfc34c90fada8c7c4d6a57ed8a3114d55c2dtrawick if (status != APR_SUCCESS) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "proxy: ap_get_brigade failed");
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe apr_brigade_destroy(input_brigade);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return HTTP_INTERNAL_SERVER_ERROR;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* have something */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: APR_BUCKET_IS_EOS");
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* Try to send something */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "proxy: data to read (max %" APR_SIZE_T_FMT
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe " at %" APR_SIZE_T_FMT ")", bufsiz, msg->pos);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (status != APR_SUCCESS) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* We had a failure: Close connection to backend */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn->close++;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe apr_brigade_destroy(input_brigade);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: apr_brigade_flatten");
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return HTTP_INTERNAL_SERVER_ERROR;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe apr_brigade_cleanup(input_brigade);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: got %" APR_SIZE_T_FMT " bytes of data", bufsiz);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (bufsiz > 0) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe status = ajp_send_data_msg(conn->sock, msg, bufsiz);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (status != APR_SUCCESS) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* We had a failure: Close connection to backend */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn->close++;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe apr_brigade_destroy(input_brigade);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: send failed to %pI (%s)",
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn->worker->cp->addr,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn->worker->hostname);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe return HTTP_SERVICE_UNAVAILABLE;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn->worker->s->transferred += bufsiz;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* read the response */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn->data = NULL;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe status = ajp_read_header(conn->sock, r,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe (ajp_msg_t **)&(conn->data));
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (status != APR_SUCCESS) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* We had a failure: Close connection to backend */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe conn->close++;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe apr_brigade_destroy(input_brigade);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: read response failed from %pI (%s)",
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn->worker->cp->addr,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn->worker->hostname);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe return HTTP_SERVICE_UNAVAILABLE;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe }
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* parse the reponse */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe result = ajp_parse_type(r, conn->data);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /*
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * Prepare apr_pollfd_t struct for possible later check if there is currently
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * data available from the backend (do not flush response to client)
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe * or not (flush response to client)
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn_poll = apr_pcalloc(p, sizeof(apr_pollfd_t));
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn_poll->reqevents = APR_POLLIN;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe conn_poll->desc_type = APR_POLL_SOCKET;
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe conn_poll->desc.s = conn->sock;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe bufsiz = AJP13_MAX_SEND_BODY_SZ;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe while (isok) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe switch (result) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe case CMD_AJP13_GET_BODY_CHUNK:
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (havebody) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* This is the end */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe bufsiz = 0;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe havebody = 0;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "proxy: APR_BUCKET_IS_EOS");
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe } else {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe status = ap_get_brigade(r->input_filters, input_brigade,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe AP_MODE_READBYTES,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe APR_BLOCK_READ,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe AJP13_MAX_SEND_BODY_SZ);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (status != APR_SUCCESS) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, status,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe r->server,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "ap_get_brigade failed");
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe break;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe }
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe bufsiz = AJP13_MAX_SEND_BODY_SZ;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe status = apr_brigade_flatten(input_brigade, buff,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe &bufsiz);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe apr_brigade_cleanup(input_brigade);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (status != APR_SUCCESS) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, status,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe r->server,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe "apr_brigade_flatten failed");
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe break;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ajp_msg_reset(msg);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* will go in ajp_send_data_msg */
0540a0b469147b52e858587270dba31c2aaa9e09wrowe status = ajp_send_data_msg(conn->sock, msg, bufsiz);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (status != APR_SUCCESS) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe "ajp_send_data_msg failed");
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe break;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe conn->worker->s->transferred += bufsiz;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe } else {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /*
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * something is wrong TC asks for more body but we are
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * already at the end of the body data
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe "ap_proxy_ajp_request error read after end");
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe isok = 0;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe break;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe case CMD_AJP13_SEND_HEADERS:
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* AJP13_SEND_HEADERS: process them */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe status = ajp_parse_header(r, conf, conn->data);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (status != APR_SUCCESS) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe isok = 0;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe break;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe case CMD_AJP13_SEND_BODY_CHUNK:
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* AJP13_SEND_BODY_CHUNK: piece of data */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe status = ajp_parse_data(r, conn->data, &size, &buff);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (status == APR_SUCCESS) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe e = apr_bucket_transient_create(buff, size,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe r->connection->bucket_alloc);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe APR_BRIGADE_INSERT_TAIL(output_brigade, e);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if ( (conn->worker->ajp_flush_packets == ajp_flush_on) ||
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ( (conn->worker->ajp_flush_packets == ajp_flush_auto) &&
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe (apr_poll(conn_poll, 1, &conn_poll_fd,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe conn->worker->ajp_flush_wait)
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe == APR_TIMEUP) ) ) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe e = apr_bucket_flush_create(r->connection->bucket_alloc);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe APR_BRIGADE_INSERT_TAIL(output_brigade, e);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe }
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe apr_brigade_length(output_brigade, 0, &bb_len);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (bb_len != -1)
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe conn->worker->s->read += bb_len;
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if (ap_pass_brigade(r->output_filters,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe output_brigade) != APR_SUCCESS) {
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe "proxy: error processing body");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb isok = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb data_sent = 1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_brigade_cleanup(output_brigade);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb else {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb isok = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb break;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb case CMD_AJP13_END_RESPONSE:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb e = apr_bucket_eos_create(r->connection->bucket_alloc);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb APR_BRIGADE_INSERT_TAIL(output_brigade, e);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (ap_pass_brigade(r->output_filters,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb output_brigade) != APR_SUCCESS) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: error processing body");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb isok = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* XXX: what about flush here? See mod_jk */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb data_sent = 1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb break;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb default:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb isok = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb break;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * If connection has been aborted by client: Stop working.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Nevertheless, we regard our operation so far as a success:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * So do not set isok to 0 and set result to CMD_AJP13_END_RESPONSE
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * But: Close this connection to the backend.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (r->connection->aborted) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb conn->close++;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb result = CMD_AJP13_END_RESPONSE;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe break;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe if (!isok)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe break;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe if (result == CMD_AJP13_END_RESPONSE)
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe break;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* read the response */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe status = ajp_read_header(conn->sock, r,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe (ajp_msg_t **)&(conn->data));
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe if (status != APR_SUCCESS) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe isok = 0;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe "ajp_read_header failed");
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe break;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe result = ajp_parse_type(r, conn->data);
2f1949bb0e3c209db94c8d521cba7380b9d11421trawick }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe apr_brigade_destroy(input_brigade);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /*
2f1949bb0e3c209db94c8d521cba7380b9d11421trawick * Clear output_brigade to remove possible buckets that remained there
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * after an error.
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe apr_brigade_cleanup(output_brigade);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe if (status != APR_SUCCESS) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* We had a failure: Close connection to backend */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe conn->close++;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe "proxy: send body failed to %pI (%s)",
2f1949bb0e3c209db94c8d521cba7380b9d11421trawick conn->worker->cp->addr,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe conn->worker->hostname);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /*
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * If we already send data, signal a broken backend connection
2f1949bb0e3c209db94c8d521cba7380b9d11421trawick * upwards in the chain.
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe if (data_sent) {
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_proxy_backend_broke(r, output_brigade);
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* Return DONE to avoid error messages being added to the stream */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe rv = DONE;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe } else
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe rv = HTTP_SERVICE_UNAVAILABLE;
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe }
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /*
8aefbd756763807188d2e3ce336a8680e4893066wrowe * Ensure that we sent an EOS bucket thru the filter chain, if we already
8aefbd756763807188d2e3ce336a8680e4893066wrowe * have sent some data. Maybe ap_proxy_backend_broke was called and added
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * one to the brigade already (no longer making it empty). So we should
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * not do this in this case.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (data_sent && !r->eos_sent && APR_BRIGADE_EMPTY(output_brigade)) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb e = apr_bucket_eos_create(r->connection->bucket_alloc);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb APR_BRIGADE_INSERT_TAIL(output_brigade, e);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* If we have added something to the brigade above, sent it */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (!APR_BRIGADE_EMPTY(output_brigade))
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_pass_brigade(r->output_filters, output_brigade);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_brigade_destroy(output_brigade);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (rv)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return rv;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Nice we have answer to send to the client */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (result == CMD_AJP13_END_RESPONSE && isok) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: got response from %pI (%s)",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb conn->worker->cp->addr,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb conn->worker->hostname);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return OK;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: got bad response (%d) from %pI (%s)",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb result,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb conn->worker->cp->addr,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb conn->worker->hostname);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* We had a failure: Close connection to backend */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb conn->close++;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return HTTP_SERVICE_UNAVAILABLE;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * This handles ajp:// URLs
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int proxy_ajp_handler(request_rec *r, proxy_worker *worker,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb proxy_server_conf *conf,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char *url, const char *proxyname,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_port_t proxyport)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb int status;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb char server_portstr[32];
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb conn_rec *origin = NULL;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb proxy_conn_rec *backend = NULL;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb const char *scheme = "AJP";
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb proxy_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb &proxy_module);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /*
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * Note: Memory pool allocation.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * A downstream keepalive connection is always connected to the existence
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * (or not) of an upstream keepalive connection. If this is not done then
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * load balancing against multiple backend servers breaks (one backend
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * server ends up taking 100% of the load), and the risk is run of
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * downstream keepalive connections being kept open unnecessarily. This
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * keeps webservers busy and ties up resources.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb *
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * As a result, we allocate all sockets out of the upstream connection
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * pool, and when we want to reuse a socket, we check first whether the
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * connection ID of the current upstream connection is the same as that
0540a0b469147b52e858587270dba31c2aaa9e09wrowe * of the connection when the socket was opened.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_pool_t *p = r->connection->pool;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb apr_uri_t *uri = apr_palloc(r->connection->pool, sizeof(*uri));
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (strncasecmp(url, "ajp:", 4) != 0) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: AJP: declining URL %s", url);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return DECLINED;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: AJP: serving URL %s", url);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
4ca6cbe768b4e0917ac0b76333c26a7d5396d454trawick /* create space for state information */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (!backend) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb status = ap_proxy_acquire_connection(scheme, &backend, worker,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb r->server);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (status != OK) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (backend) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb backend->close_on_recycle = 1;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_proxy_release_connection(scheme, backend, r->server);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return status;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb backend->is_ssl = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb backend->close_on_recycle = 0;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Step One: Determine Who To Connect To */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb status = ap_proxy_determine_connection(p, r, conf, worker, backend,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb uri, &url, proxyname, proxyport,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb server_portstr,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb sizeof(server_portstr));
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (status != OK)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb goto cleanup;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Step Two: Make the Connection */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: AJP: failed to make connection to backend: %s",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb backend->hostname);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb status = HTTP_SERVICE_UNAVAILABLE;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb goto cleanup;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb }
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Step Three: Process the Request */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb status = ap_proxy_ajp_request(p, r, backend, origin, dconf, uri, url,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb server_portstr);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbcleanup:
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Do not close the socket */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_proxy_release_connection(scheme, backend, r->server);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb return status;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic void ap_proxy_http_register_hook(apr_pool_t *p)
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb{
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb proxy_hook_scheme_handler(proxy_ajp_handler, NULL, NULL, APR_HOOK_FIRST);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb proxy_hook_canon_handler(proxy_ajp_canon, NULL, NULL, APR_HOOK_FIRST);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb}
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b29f87f4b6c6886a04dccc296177a7033f70dfedtrawickmodule AP_MODULE_DECLARE_DATA proxy_ajp_module = {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb STANDARD20_MODULE_STUFF,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb NULL, /* create per-directory config structure */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb NULL, /* merge per-directory config structures */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb NULL, /* create per-server config structure */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb NULL, /* merge per-server config structures */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb NULL, /* command apr_table_t */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_proxy_http_register_hook /* register hooks */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb};
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb