1 /* Copyright 1999-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 /* AJP routines for Apache proxy */
18 #include "mod_proxy.h"
21 module AP_MODULE_DECLARE_DATA proxy_ajp_module;
23 int ap_proxy_ajp_canon(request_rec *r, char *url);
24 int ap_proxy_ajp_handler(request_rec *r, proxy_worker *worker,
25 proxy_server_conf *conf,
26 char *url, const char *proxyname,
27 apr_port_t proxyport);
30 * Canonicalise http-like URLs.
31 * scheme is the scheme for the URL
32 * url is the URL starting with the first '/'
33 * def_port is the default port for this scheme.
35 int ap_proxy_ajp_canon(request_rec *r, char *url)
37 char *host, *path, *search, sport[7];
40 apr_port_t port, def_port;
42 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
43 "proxy: AJP: canonicalising URL %s", url);
45 /* ap_port_of_scheme() */
46 if (strncasecmp(url, "ajp:", 4) == 0) {
50 /* XXX This is probably faulty */
51 else if (strncasecmp(url, "ajps:", 5) == 0) {
58 def_port = apr_uri_port_of_scheme(scheme);
60 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
61 "proxy: AJP: canonicalising URL %s", url);
64 * We break the URL into host, port, path, search
67 err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
69 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
70 "error parsing URL %s: %s",
72 return HTTP_BAD_REQUEST;
75 /* now parse path/search args, according to rfc1738 */
76 /* N.B. if this isn't a true proxy request, then the URL _path_
77 * has already been decoded. True proxy requests have r->uri
78 * == r->unparsed_uri, and no others have that property.
80 if (r->uri == r->unparsed_uri) {
81 search = strchr(url, '?');
89 path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, r->proxyreq);
91 return HTTP_BAD_REQUEST;
94 apr_snprintf(sport, sizeof(sport), ":%d", port);
98 if (ap_strchr_c(host, ':')) { /* if literal IPv6 address */
99 host = apr_pstrcat(r->pool, "[", host, "]", NULL);
101 r->filename = apr_pstrcat(r->pool, "proxy:", scheme, "://", host, sport,
102 "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
107 * process the request and write the respnse.
109 static int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
110 proxy_conn_rec *conn,
112 proxy_server_conf *conf,
114 char *url, char *server_portstr)
119 apr_bucket_brigade *input_brigade;
120 apr_bucket_brigade *output_brigade;
130 * Send the AJP request to the remote server
133 /* send request headers */
134 status = ajp_send_header(conn->sock, r);
135 if (status != APR_SUCCESS) {
137 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
138 "proxy: AJP: request failed to %pI (%s)",
139 conn->worker->cp->addr,
140 conn->worker->hostname);
141 return HTTP_SERVICE_UNAVAILABLE;
144 /* allocate an AJP message to store the data of the buckets */
145 status = ajp_alloc_data_msg(r, &buff, &bufsiz, &msg);
146 if (status != APR_SUCCESS) {
147 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
148 "proxy: ajp_alloc_data_msg failed");
151 /* read the first bloc of data */
152 input_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
153 tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
154 if (tenc && strcasecmp(tenc, "chunked")==0) {
155 /* The AJP protocol does not want body data yet */
156 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
157 "proxy: request is chunked");
159 status = ap_get_brigade(r->input_filters, input_brigade,
160 AP_MODE_READBYTES, APR_BLOCK_READ,
161 AJP13_MAX_SEND_BODY_SZ);
163 if (status != APR_SUCCESS) {
164 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
165 "proxy: ap_get_brigade failed");
166 apr_brigade_destroy(input_brigade);
167 return HTTP_INTERNAL_SERVER_ERROR;
171 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
172 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
173 "proxy: APR_BUCKET_IS_EOS");
176 /* Try to send something */
177 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
178 "proxy: data to read (max %d at %08x)", bufsiz, buff);
180 status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
181 if (status != APR_SUCCESS) {
182 apr_brigade_destroy(input_brigade);
183 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
184 "proxy: apr_brigade_flatten");
185 return HTTP_INTERNAL_SERVER_ERROR;
187 apr_brigade_cleanup(input_brigade);
189 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
190 "proxy: got %d byte of data", bufsiz);
192 status = ajp_send_data_msg(conn->sock, r, msg, bufsiz);
193 if (status != APR_SUCCESS) {
194 apr_brigade_destroy(input_brigade);
195 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
196 "proxy: send failed to %pI (%s)",
197 conn->worker->cp->addr,
198 conn->worker->hostname);
199 return HTTP_SERVICE_UNAVAILABLE;
204 /* read the response */
205 status = ajp_read_header(conn->sock, r,
206 (ajp_msg_t **)&(conn->data));
207 if (status != APR_SUCCESS) {
208 apr_brigade_destroy(input_brigade);
209 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
210 "proxy: read response failed from %pI (%s)",
211 conn->worker->cp->addr,
212 conn->worker->hostname);
213 return HTTP_SERVICE_UNAVAILABLE;
216 /* parse the reponse */
217 result = ajp_parse_type(r, conn->data);
218 output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
220 bufsiz = AJP13_MAX_SEND_BODY_SZ;
221 // while (result == CMD_AJP13_GET_BODY_CHUNK && bufsiz != 0) {
224 case CMD_AJP13_GET_BODY_CHUNK:
226 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
227 /* That is the end */
230 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
231 "proxy: APR_BUCKET_IS_EOS");
233 status = ap_get_brigade(r->input_filters, input_brigade,
234 AP_MODE_READBYTES, APR_BLOCK_READ,
235 AJP13_MAX_SEND_BODY_SZ);
236 if (status != APR_SUCCESS) {
237 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
238 "ap_get_brigade failed");
241 bufsiz = AJP13_MAX_SEND_BODY_SZ;
242 status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
243 apr_brigade_cleanup(input_brigade);
244 if (status != APR_SUCCESS) {
245 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
246 "apr_brigade_flatten failed");
251 ajp_msg_reset(msg); /* will go in ajp_send_data_msg */
252 status = ajp_send_data_msg(conn->sock, r, msg, bufsiz);
253 if (status != APR_SUCCESS) {
254 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
255 "ajp_send_data_msg failed");
259 /* something is wrong TC asks for more body but we are
260 * already at the end of the body data
262 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
263 "ap_proxy_ajp_request error read after end");
267 case CMD_AJP13_SEND_HEADERS:
268 /* AJP13_SEND_HEADERS: process them */
269 status = ajp_parse_header(r, conn->data);
270 if (status != APR_SUCCESS) {
274 case CMD_AJP13_SEND_BODY_CHUNK:
275 /* AJP13_SEND_BODY_CHUNK: piece of data */
276 status = ajp_parse_data(r, conn->data, &size, &buff);
277 e = apr_bucket_transient_create(buff, size,
278 r->connection->bucket_alloc);
279 APR_BRIGADE_INSERT_TAIL(output_brigade, e);
280 if (status != APR_SUCCESS)
283 case CMD_AJP13_END_RESPONSE:
284 e = apr_bucket_eos_create(r->connection->bucket_alloc);
285 APR_BRIGADE_INSERT_TAIL(output_brigade, e);
286 if (ap_pass_brigade(r->output_filters, output_brigade) != APR_SUCCESS) {
287 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
288 "proxy: error processing body");
299 if (result == CMD_AJP13_END_RESPONSE)
302 /* read the response */
303 status = ajp_read_header(conn->sock, r,
304 (ajp_msg_t **)&(conn->data));
305 if (status != APR_SUCCESS) {
307 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
308 "ajp_read_header failed");
311 result = ajp_parse_type(r, conn->data);
313 apr_brigade_destroy(input_brigade);
315 apr_brigade_destroy(output_brigade);
317 if (status != APR_SUCCESS) {
318 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
319 "proxy: send body failed to %pI (%s)",
320 conn->worker->cp->addr,
321 conn->worker->hostname);
322 return HTTP_SERVICE_UNAVAILABLE;
325 /* Nice we have answer to send to the client */
326 if (result == CMD_AJP13_END_RESPONSE && isok) {
327 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
328 "proxy: got response from %pI (%s)",
329 conn->worker->cp->addr,
330 conn->worker->hostname);
334 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
335 "proxy: got bad response (%d) from %pI (%s)",
337 conn->worker->cp->addr,
338 conn->worker->hostname);
340 return HTTP_SERVICE_UNAVAILABLE;
344 * This handles http:// URLs, and other URLs using a remote proxy over http
345 * If proxyhost is NULL, then contact the server directly, otherwise
347 * Note that if a proxy is used, then URLs other than http: can be accessed,
348 * also, if we have trouble which is clearly specific to the proxy, then
349 * we return DECLINED so that we can try another proxy. (Or the direct
352 int ap_proxy_ajp_handler(request_rec *r, proxy_worker *worker,
353 proxy_server_conf *conf,
354 char *url, const char *proxyname,
355 apr_port_t proxyport)
358 char server_portstr[32];
359 conn_rec *origin = NULL;
360 proxy_conn_rec *backend = NULL;
362 const char *scheme = "AJP";
364 /* Note: Memory pool allocation.
365 * A downstream keepalive connection is always connected to the existence
366 * (or not) of an upstream keepalive connection. If this is not done then
367 * load balancing against multiple backend servers breaks (one backend
368 * server ends up taking 100% of the load), and the risk is run of
369 * downstream keepalive connections being kept open unnecessarily. This
370 * keeps webservers busy and ties up resources.
372 * As a result, we allocate all sockets out of the upstream connection
373 * pool, and when we want to reuse a socket, we check first whether the
374 * connection ID of the current upstream connection is the same as that
375 * of the connection when the socket was opened.
377 apr_pool_t *p = r->connection->pool;
378 conn_rec *c = r->connection;
379 apr_uri_t *uri = apr_palloc(r->connection->pool, sizeof(*uri));
382 if (strncasecmp(url, "ajp:", 4) != 0) {
383 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
384 "proxy: AJP: declining URL %s", url);
387 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
388 "proxy: AJP: serving URL %s", url);
391 /* only use stored info for top-level pages. Sub requests don't share
396 backend = (proxy_conn_rec *) ap_get_module_config(c->conn_config,
400 /* create space for state information */
402 status = ap_proxy_acquire_connection(scheme, &backend, worker, r->server);
405 backend->close_on_recycle = 1;
406 ap_proxy_release_connection(scheme, backend, r->server);
412 ap_set_module_config(c->conn_config, &proxy_ajp_module, backend);
418 backend->close_on_recycle = 0;
420 /* Step One: Determine Who To Connect To */
421 status = ap_proxy_determine_connection(p, r, conf, worker, backend, c->pool,
422 uri, &url, proxyname, proxyport,
424 sizeof(server_portstr));
428 /* Step Two: Make the Connection */
429 if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
430 status = HTTP_SERVICE_UNAVAILABLE;
434 /* XXX: we don't need to create the bound client connection */
436 /* Step Three: Create conn_rec */
437 if (!backend->connection) {
438 status = ap_proxy_connection_create(scheme, backend, c, r->server);
445 /* Step Four: Process the Request */
446 status = ap_proxy_ajp_request(p, r, backend, origin, conf, uri, url,
453 /* Clear the module config */
454 ap_set_module_config(c->conn_config, &proxy_ajp_module, NULL);
456 /* Do not close the socket */
457 ap_proxy_release_connection(scheme, backend, r->server);
461 static void ap_proxy_http_register_hook(apr_pool_t *p)
463 proxy_hook_scheme_handler(ap_proxy_ajp_handler, NULL, NULL, APR_HOOK_FIRST);
464 proxy_hook_canon_handler(ap_proxy_ajp_canon, NULL, NULL, APR_HOOK_FIRST);
467 module AP_MODULE_DECLARE_DATA proxy_ajp_module = {
468 STANDARD20_MODULE_STUFF,
469 NULL, /* create per-directory config structure */
470 NULL, /* merge per-directory config structures */
471 NULL, /* create per-server config structure */
472 NULL, /* merge per-server config structures */
473 NULL, /* command apr_table_t */
474 ap_proxy_http_register_hook/* register hooks */