1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* AJP routines for Apache proxy */
19 #include "mod_proxy.h"
22 module AP_MODULE_DECLARE_DATA proxy_ajp_module;
25 * Canonicalise http-like URLs.
26 * scheme is the scheme for the URL
27 * url is the URL starting with the first '/'
28 * def_port is the default port for this scheme.
30 static int proxy_ajp_canon(request_rec *r, char *url)
32 char *host, *path, *search, sport[7];
34 apr_port_t port = AJP13_DEF_PORT;
36 /* ap_port_of_scheme() */
37 if (strncasecmp(url, "ajp:", 4) == 0) {
44 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
45 "proxy: AJP: canonicalising URL %s", url);
49 * We break the URL into host, port, path, search
51 err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
53 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
54 "error parsing URL %s: %s",
56 return HTTP_BAD_REQUEST;
60 * now parse path/search args, according to rfc1738
62 * N.B. if this isn't a true proxy request, then the URL _path_
63 * has already been decoded. True proxy requests have
64 * r->uri == r->unparsed_uri, and no others have that property.
66 if (r->uri == r->unparsed_uri) {
67 search = strchr(url, '?');
75 path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, 0,
78 return HTTP_BAD_REQUEST;
80 apr_snprintf(sport, sizeof(sport), ":%d", port);
82 if (ap_strchr_c(host, ':')) {
83 /* if literal IPv6 address */
84 host = apr_pstrcat(r->pool, "[", host, "]", NULL);
86 r->filename = apr_pstrcat(r->pool, "proxy:ajp://", host, sport,
87 "/", path, (search) ? "?" : "",
88 (search) ? search : "", NULL);
93 * XXX: AJP Auto Flushing
95 * When processing CMD_AJP13_SEND_BODY_CHUNK AJP messages we will do a poll
96 * with FLUSH_WAIT miliseconds timeout to determine if more data is currently
97 * available at the backend. If there is no more data available, we flush
98 * the data to the client by adding a flush bucket to the brigade we pass
99 * up the filter chain.
100 * This is only a bandaid to fix the AJP/1.3 protocol shortcoming of not
101 * sending (actually not having defined) a flush message, when the data
102 * should be flushed to the client. As soon as this protocol shortcoming is
103 * fixed this code should be removed.
105 * For further discussion see PR37100.
106 * http://issues.apache.org/bugzilla/show_bug.cgi?id=37100
110 * process the request and write the response.
112 static int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
113 proxy_conn_rec *conn,
115 proxy_dir_conf *conf,
117 char *url, char *server_portstr)
122 apr_bucket_brigade *input_brigade;
123 apr_bucket_brigade *output_brigade;
134 apr_int32_t conn_poll_fd;
135 apr_pollfd_t *conn_poll;
138 * Send the AJP request to the remote server
141 /* send request headers */
142 status = ajp_send_header(conn->sock, r, uri);
143 if (status != APR_SUCCESS) {
145 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
146 "proxy: AJP: request failed to %pI (%s)",
147 conn->worker->cp->addr,
148 conn->worker->hostname);
149 if (status == AJP_EOVERFLOW)
150 return HTTP_BAD_REQUEST;
152 return HTTP_SERVICE_UNAVAILABLE;
155 /* allocate an AJP message to store the data of the buckets */
156 status = ajp_alloc_data_msg(r->pool, &buff, &bufsiz, &msg);
157 if (status != APR_SUCCESS) {
158 /* We had a failure: Close connection to backend */
160 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
161 "proxy: ajp_alloc_data_msg failed");
162 return HTTP_INTERNAL_SERVER_ERROR;
165 /* read the first bloc of data */
166 input_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
167 tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
168 if (tenc && (strcasecmp(tenc, "chunked") == 0)) {
169 /* The AJP protocol does not want body data yet */
170 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
171 "proxy: request is chunked");
173 status = ap_get_brigade(r->input_filters, input_brigade,
174 AP_MODE_READBYTES, APR_BLOCK_READ,
175 AJP13_MAX_SEND_BODY_SZ);
177 if (status != APR_SUCCESS) {
178 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
179 "proxy: ap_get_brigade failed");
180 apr_brigade_destroy(input_brigade);
181 return HTTP_INTERNAL_SERVER_ERROR;
185 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
186 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
187 "proxy: APR_BUCKET_IS_EOS");
190 /* Try to send something */
191 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
192 "proxy: data to read (max %" APR_SIZE_T_FMT
193 " at %" APR_SIZE_T_FMT ")", bufsiz, msg->pos);
195 status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
196 if (status != APR_SUCCESS) {
197 /* We had a failure: Close connection to backend */
199 apr_brigade_destroy(input_brigade);
200 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
201 "proxy: apr_brigade_flatten");
202 return HTTP_INTERNAL_SERVER_ERROR;
204 apr_brigade_cleanup(input_brigade);
206 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
207 "proxy: got %" APR_SIZE_T_FMT " bytes of data", bufsiz);
209 status = ajp_send_data_msg(conn->sock, msg, bufsiz);
210 if (status != APR_SUCCESS) {
211 /* We had a failure: Close connection to backend */
213 apr_brigade_destroy(input_brigade);
214 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
215 "proxy: send failed to %pI (%s)",
216 conn->worker->cp->addr,
217 conn->worker->hostname);
218 return HTTP_SERVICE_UNAVAILABLE;
220 conn->worker->s->transferred += bufsiz;
224 /* read the response */
226 status = ajp_read_header(conn->sock, r,
227 (ajp_msg_t **)&(conn->data));
228 if (status != APR_SUCCESS) {
229 /* We had a failure: Close connection to backend */
231 apr_brigade_destroy(input_brigade);
232 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
233 "proxy: read response failed from %pI (%s)",
234 conn->worker->cp->addr,
235 conn->worker->hostname);
236 return HTTP_SERVICE_UNAVAILABLE;
238 /* parse the reponse */
239 result = ajp_parse_type(r, conn->data);
240 output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
243 * Prepare apr_pollfd_t struct for possible later check if there is currently
244 * data available from the backend (do not flush response to client)
245 * or not (flush response to client)
247 conn_poll = apr_pcalloc(p, sizeof(apr_pollfd_t));
248 conn_poll->reqevents = APR_POLLIN;
249 conn_poll->desc_type = APR_POLL_SOCKET;
250 conn_poll->desc.s = conn->sock;
252 bufsiz = AJP13_MAX_SEND_BODY_SZ;
255 case CMD_AJP13_GET_BODY_CHUNK:
257 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
258 /* This is the end */
261 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
262 "proxy: APR_BUCKET_IS_EOS");
264 status = ap_get_brigade(r->input_filters, input_brigade,
267 AJP13_MAX_SEND_BODY_SZ);
268 if (status != APR_SUCCESS) {
269 ap_log_error(APLOG_MARK, APLOG_DEBUG, status,
271 "ap_get_brigade failed");
274 bufsiz = AJP13_MAX_SEND_BODY_SZ;
275 status = apr_brigade_flatten(input_brigade, buff,
277 apr_brigade_cleanup(input_brigade);
278 if (status != APR_SUCCESS) {
279 ap_log_error(APLOG_MARK, APLOG_DEBUG, status,
281 "apr_brigade_flatten failed");
287 /* will go in ajp_send_data_msg */
288 status = ajp_send_data_msg(conn->sock, msg, bufsiz);
289 if (status != APR_SUCCESS) {
290 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
291 "ajp_send_data_msg failed");
294 conn->worker->s->transferred += bufsiz;
297 * something is wrong TC asks for more body but we are
298 * already at the end of the body data
300 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
301 "ap_proxy_ajp_request error read after end");
305 case CMD_AJP13_SEND_HEADERS:
306 /* AJP13_SEND_HEADERS: process them */
307 status = ajp_parse_header(r, conf, conn->data);
308 if (status != APR_SUCCESS) {
312 case CMD_AJP13_SEND_BODY_CHUNK:
313 /* AJP13_SEND_BODY_CHUNK: piece of data */
314 status = ajp_parse_data(r, conn->data, &size, &buff);
315 if (status == APR_SUCCESS) {
316 e = apr_bucket_transient_create(buff, size,
317 r->connection->bucket_alloc);
318 APR_BRIGADE_INSERT_TAIL(output_brigade, e);
320 if ( (conn->worker->flush_packets == flush_on) ||
321 ( (conn->worker->flush_packets == flush_auto) &&
322 (apr_poll(conn_poll, 1, &conn_poll_fd,
323 conn->worker->flush_wait)
325 e = apr_bucket_flush_create(r->connection->bucket_alloc);
326 APR_BRIGADE_INSERT_TAIL(output_brigade, e);
328 apr_brigade_length(output_brigade, 0, &bb_len);
330 conn->worker->s->read += bb_len;
331 if (ap_pass_brigade(r->output_filters,
332 output_brigade) != APR_SUCCESS) {
333 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
334 "proxy: error processing body");
338 apr_brigade_cleanup(output_brigade);
344 case CMD_AJP13_END_RESPONSE:
345 e = apr_bucket_eos_create(r->connection->bucket_alloc);
346 APR_BRIGADE_INSERT_TAIL(output_brigade, e);
347 if (ap_pass_brigade(r->output_filters,
348 output_brigade) != APR_SUCCESS) {
349 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
350 "proxy: error processing body");
353 /* XXX: what about flush here? See mod_jk */
362 * If connection has been aborted by client: Stop working.
363 * Nevertheless, we regard our operation so far as a success:
364 * So do not set isok to 0 and set result to CMD_AJP13_END_RESPONSE
365 * But: Close this connection to the backend.
367 if (r->connection->aborted) {
369 result = CMD_AJP13_END_RESPONSE;
376 if (result == CMD_AJP13_END_RESPONSE)
379 /* read the response */
380 status = ajp_read_header(conn->sock, r,
381 (ajp_msg_t **)&(conn->data));
382 if (status != APR_SUCCESS) {
384 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
385 "ajp_read_header failed");
388 result = ajp_parse_type(r, conn->data);
390 apr_brigade_destroy(input_brigade);
393 * Clear output_brigade to remove possible buckets that remained there
396 apr_brigade_cleanup(output_brigade);
398 if (status != APR_SUCCESS) {
399 /* We had a failure: Close connection to backend */
401 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
402 "proxy: send body failed to %pI (%s)",
403 conn->worker->cp->addr,
404 conn->worker->hostname);
406 * If we already send data, signal a broken backend connection
407 * upwards in the chain.
410 ap_proxy_backend_broke(r, output_brigade);
411 /* Return DONE to avoid error messages being added to the stream */
414 rv = HTTP_SERVICE_UNAVAILABLE;
418 * Ensure that we sent an EOS bucket thru the filter chain, if we already
419 * have sent some data. Maybe ap_proxy_backend_broke was called and added
420 * one to the brigade already (no longer making it empty). So we should
421 * not do this in this case.
423 if (data_sent && !r->eos_sent && APR_BRIGADE_EMPTY(output_brigade)) {
424 e = apr_bucket_eos_create(r->connection->bucket_alloc);
425 APR_BRIGADE_INSERT_TAIL(output_brigade, e);
428 /* If we have added something to the brigade above, sent it */
429 if (!APR_BRIGADE_EMPTY(output_brigade))
430 ap_pass_brigade(r->output_filters, output_brigade);
432 apr_brigade_destroy(output_brigade);
437 /* Nice we have answer to send to the client */
438 if (result == CMD_AJP13_END_RESPONSE && isok) {
439 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
440 "proxy: got response from %pI (%s)",
441 conn->worker->cp->addr,
442 conn->worker->hostname);
446 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
447 "proxy: got bad response (%d) from %pI (%s)",
449 conn->worker->cp->addr,
450 conn->worker->hostname);
452 /* We had a failure: Close connection to backend */
454 return HTTP_SERVICE_UNAVAILABLE;
458 * This handles ajp:// URLs
460 static int proxy_ajp_handler(request_rec *r, proxy_worker *worker,
461 proxy_server_conf *conf,
462 char *url, const char *proxyname,
463 apr_port_t proxyport)
466 char server_portstr[32];
467 conn_rec *origin = NULL;
468 proxy_conn_rec *backend = NULL;
469 const char *scheme = "AJP";
470 proxy_dir_conf *dconf = ap_get_module_config(r->per_dir_config,
474 * Note: Memory pool allocation.
475 * A downstream keepalive connection is always connected to the existence
476 * (or not) of an upstream keepalive connection. If this is not done then
477 * load balancing against multiple backend servers breaks (one backend
478 * server ends up taking 100% of the load), and the risk is run of
479 * downstream keepalive connections being kept open unnecessarily. This
480 * keeps webservers busy and ties up resources.
482 * As a result, we allocate all sockets out of the upstream connection
483 * pool, and when we want to reuse a socket, we check first whether the
484 * connection ID of the current upstream connection is the same as that
485 * of the connection when the socket was opened.
487 apr_pool_t *p = r->connection->pool;
488 apr_uri_t *uri = apr_palloc(r->connection->pool, sizeof(*uri));
491 if (strncasecmp(url, "ajp:", 4) != 0) {
492 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
493 "proxy: AJP: declining URL %s", url);
496 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
497 "proxy: AJP: serving URL %s", url);
499 /* create space for state information */
501 status = ap_proxy_acquire_connection(scheme, &backend, worker,
505 backend->close_on_recycle = 1;
506 ap_proxy_release_connection(scheme, backend, r->server);
513 backend->close_on_recycle = 0;
515 /* Step One: Determine Who To Connect To */
516 status = ap_proxy_determine_connection(p, r, conf, worker, backend,
517 uri, &url, proxyname, proxyport,
519 sizeof(server_portstr));
524 /* Step Two: Make the Connection */
525 if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
526 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
527 "proxy: AJP: failed to make connection to backend: %s",
529 status = HTTP_SERVICE_UNAVAILABLE;
533 /* Handle CPING/CPONG */
534 if (worker->ping_timeout_set) {
535 status = ajp_handle_cping_cpong(backend->sock, r,
536 worker->ping_timeout);
537 if (status != APR_SUCCESS) {
539 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
540 "proxy: AJP: cping/cpong failed to %pI (%s)",
543 status = HTTP_SERVICE_UNAVAILABLE;
547 /* Step Three: Process the Request */
548 status = ap_proxy_ajp_request(p, r, backend, origin, dconf, uri, url,
552 /* Do not close the socket */
553 ap_proxy_release_connection(scheme, backend, r->server);
557 static void ap_proxy_http_register_hook(apr_pool_t *p)
559 proxy_hook_scheme_handler(proxy_ajp_handler, NULL, NULL, APR_HOOK_FIRST);
560 proxy_hook_canon_handler(proxy_ajp_canon, NULL, NULL, APR_HOOK_FIRST);
563 module AP_MODULE_DECLARE_DATA proxy_ajp_module = {
564 STANDARD20_MODULE_STUFF,
565 NULL, /* create per-directory config structure */
566 NULL, /* merge per-directory config structures */
567 NULL, /* create per-server config structure */
568 NULL, /* merge per-server config structures */
569 NULL, /* command apr_table_t */
570 ap_proxy_http_register_hook /* register hooks */