mod_proxy_ajp.c revision 5d392744e2077f71f34ce098ab49d2c0ddcf4ea3
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * applicable.
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 * 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/* AJP routines for Apache proxy */
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 const char *err;
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* ap_port_of_scheme() */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * do syntactic check.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * We break the URL into host, port, path, search
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "error parsing URL %s: %s",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * now parse path/search args, according to rfc1738
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 /* process path */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* if literal IPv6 address */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb r->filename = apr_pstrcat(r->pool, "proxy:ajp://", host, sport,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * XXX: AJP Auto Flushing
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 * For further discussion see PR37100.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * http://issues.apache.org/bugzilla/show_bug.cgi?id=37100
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * process the request and write the response.
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe const char *tenc;
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe * Send the AJP request to the remote server
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* send request headers */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "proxy: AJP: request failed to %pI (%s)",
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* allocate an AJP message to store the data of the buckets */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe status = ajp_alloc_data_msg(r->pool, &buff, &bufsiz, &msg);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* We had a failure: Close connection to backend */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: ajp_alloc_data_msg failed");
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 /* The AJP protocol does not want body data yet */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: request is chunked");
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe status = ap_get_brigade(r->input_filters, input_brigade,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "proxy: ap_get_brigade failed");
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* have something */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: APR_BUCKET_IS_EOS");
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* Try to send something */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* We had a failure: Close connection to backend */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: apr_brigade_flatten");
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: got %" APR_SIZE_T_FMT " bytes of data", bufsiz);
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* We had a failure: Close connection to backend */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: send failed to %pI (%s)",
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* read the response */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* We had a failure: Close connection to backend */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe "proxy: read response failed from %pI (%s)",
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe /* parse the reponse */
fa06de8a28a737e8fbaad76d7f3ff67aaa5e4a09wrowe output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
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)
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe /* This is the end */
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "proxy: APR_BUCKET_IS_EOS");
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe status = ap_get_brigade(r->input_filters, input_brigade,
83a8dc5a596a8a1b9d14f063268287d123b9ed7ewrowe "ap_get_brigade failed");
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe "apr_brigade_flatten failed");
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* will go in ajp_send_data_msg */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe "ajp_send_data_msg failed");
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * something is wrong TC asks for more body but we are
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe * already at the end of the body data
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe "ap_proxy_ajp_request error read after end");
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* AJP13_SEND_HEADERS: process them */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe /* AJP13_SEND_BODY_CHUNK: piece of data */
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe if ( (conn->worker->ajp_flush_packets == ajp_flush_on) ||
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe ( (conn->worker->ajp_flush_packets == ajp_flush_auto) &&
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe e = apr_bucket_flush_create(r->connection->bucket_alloc);
cc9582e53aead2a044077c4a92f3dfc3605590b3wrowe "proxy: error processing body");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: error processing body");
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* XXX: what about flush here? See mod_jk */
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.
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* read the response */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe "ajp_read_header failed");
2f1949bb0e3c209db94c8d521cba7380b9d11421trawick * Clear output_brigade to remove possible buckets that remained there
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * after an error.
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* We had a failure: Close connection to backend */
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe "proxy: send body failed to %pI (%s)",
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe * If we already send data, signal a broken backend connection
2f1949bb0e3c209db94c8d521cba7380b9d11421trawick * upwards in the chain.
290ecc1ddceca1ed49bc1a5338921264b5c3e07cwrowe /* Return DONE to avoid error messages being added to the stream */
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 if (data_sent && !r->eos_sent && APR_BRIGADE_EMPTY(output_brigade)) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* If we have added something to the brigade above, sent it */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Nice we have answer to send to the client */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: got response from %pI (%s)",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: got bad response (%d) from %pI (%s)",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* We had a failure: Close connection to backend */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb * This handles ajp:// URLs
b38846b15c8891c6dec44dcc4f96ca40721bf663rbbstatic int proxy_ajp_handler(request_rec *r, proxy_worker *worker,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb proxy_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
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 * 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 apr_uri_t *uri = apr_palloc(r->connection->pool, sizeof(*uri));
4ca6cbe768b4e0917ac0b76333c26a7d5396d454trawick /* create space for state information */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb status = ap_proxy_acquire_connection(scheme, &backend, worker,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_proxy_release_connection(scheme, backend, r->server);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Step One: Determine Who To Connect To */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb status = ap_proxy_determine_connection(p, r, conf, worker, backend,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Step Two: Make the Connection */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb "proxy: AJP: failed to make connection to backend: %s",
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Step Three: Process the Request */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb status = ap_proxy_ajp_request(p, r, backend, origin, dconf, uri, url,
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb /* Do not close the socket */
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb ap_proxy_release_connection(scheme, backend, r->server);
b38846b15c8891c6dec44dcc4f96ca40721bf663rbb proxy_hook_scheme_handler(proxy_ajp_handler, NULL, NULL, APR_HOOK_FIRST);