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;
24 * Canonicalise http-like URLs.
25 * scheme is the scheme for the URL
26 * url is the URL starting with the first '/'
27 * def_port is the default port for this scheme.
29 int ap_proxy_ajp_canon(request_rec *r, char *url)
31 char *host, *path, *search, sport[7];
34 apr_port_t port, def_port;
36 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
37 "proxy: AJP: canonicalising URL %s", url);
39 /* ap_port_of_scheme() */
40 if (strncasecmp(url, "ajp:", 4) == 0) {
44 /* XXX This is probably faulty */
45 else if (strncasecmp(url, "ajps:", 5) == 0) {
52 def_port = apr_uri_port_of_scheme(scheme);
54 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
55 "proxy: AJP: canonicalising URL %s", url);
58 * We break the URL into host, port, path, search
61 err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
63 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
64 "error parsing URL %s: %s",
66 return HTTP_BAD_REQUEST;
69 /* now parse path/search args, according to rfc1738 */
70 /* N.B. if this isn't a true proxy request, then the URL _path_
71 * has already been decoded. True proxy requests have r->uri
72 * == r->unparsed_uri, and no others have that property.
74 if (r->uri == r->unparsed_uri) {
75 search = strchr(url, '?');
83 path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, r->proxyreq);
85 return HTTP_BAD_REQUEST;
88 apr_snprintf(sport, sizeof(sport), ":%d", port);
92 if (ap_strchr_c(host, ':')) { /* if literal IPv6 address */
93 host = apr_pstrcat(r->pool, "[", host, "]", NULL);
95 r->filename = apr_pstrcat(r->pool, "proxy:", scheme, "://", host, sport,
96 "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
101 * process the request and write the respnse.
103 static int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
104 proxy_conn_rec *conn,
106 proxy_server_conf *conf,
108 char *url, char *server_portstr)
113 apr_bucket_brigade *input_brigade;
114 apr_bucket_brigade *output_brigade;
124 * Send the AJP request to the remote server
127 /* send request headers */
128 status = ajp_send_header(conn->sock, r);
129 if (status != APR_SUCCESS) {
131 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
132 "proxy: AJP: request failed to %pI (%s)",
133 conn->worker->cp->addr,
134 conn->worker->hostname);
135 return HTTP_SERVICE_UNAVAILABLE;
138 /* allocate an AJP message to store the data of the buckets */
139 status = ajp_alloc_data_msg(r, &buff, &bufsiz, &msg);
140 if (status != APR_SUCCESS) {
141 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
142 "proxy: ajp_alloc_data_msg failed");
145 /* read the first bloc of data */
146 input_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
147 tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
148 if (tenc && strcasecmp(tenc, "chunked")==0) {
149 /* The AJP protocol does not want body data yet */
150 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
151 "proxy: request is chunked");
153 status = ap_get_brigade(r->input_filters, input_brigade,
154 AP_MODE_READBYTES, APR_BLOCK_READ,
155 AJP13_MAX_SEND_BODY_SZ);
157 if (status != APR_SUCCESS) {
158 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
159 "proxy: ap_get_brigade failed");
160 apr_brigade_destroy(input_brigade);
161 return HTTP_INTERNAL_SERVER_ERROR;
165 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
166 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
167 "proxy: APR_BUCKET_IS_EOS");
170 /* Try to send something */
171 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
172 "proxy: data to read (max %d at %08x)", bufsiz, buff);
174 status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
175 if (status != APR_SUCCESS) {
176 apr_brigade_destroy(input_brigade);
177 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
178 "proxy: apr_brigade_flatten");
179 return HTTP_INTERNAL_SERVER_ERROR;
181 apr_brigade_cleanup(input_brigade);
183 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
184 "proxy: got %d byte of data", bufsiz);
186 status = ajp_send_data_msg(conn->sock, r, msg, bufsiz);
187 if (status != APR_SUCCESS) {
188 apr_brigade_destroy(input_brigade);
189 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
190 "proxy: send failed to %pI (%s)",
191 conn->worker->cp->addr,
192 conn->worker->hostname);
193 return HTTP_SERVICE_UNAVAILABLE;
198 /* read the response */
199 status = ajp_read_header(conn->sock, r,
200 (ajp_msg_t **)&(conn->data));
201 if (status != APR_SUCCESS) {
202 apr_brigade_destroy(input_brigade);
203 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
204 "proxy: read response failed from %pI (%s)",
205 conn->worker->cp->addr,
206 conn->worker->hostname);
207 return HTTP_SERVICE_UNAVAILABLE;
210 /* parse the reponse */
211 result = ajp_parse_type(r, conn->data);
212 output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
214 bufsiz = AJP13_MAX_SEND_BODY_SZ;
215 // while (result == CMD_AJP13_GET_BODY_CHUNK && bufsiz != 0) {
218 case CMD_AJP13_GET_BODY_CHUNK:
220 if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
221 /* That is the end */
224 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
225 "proxy: APR_BUCKET_IS_EOS");
227 status = ap_get_brigade(r->input_filters, input_brigade,
228 AP_MODE_READBYTES, APR_BLOCK_READ,
229 AJP13_MAX_SEND_BODY_SZ);
230 if (status != APR_SUCCESS) {
231 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
232 "ap_get_brigade failed");
235 bufsiz = AJP13_MAX_SEND_BODY_SZ;
236 status = apr_brigade_flatten(input_brigade, buff, &bufsiz);
237 apr_brigade_cleanup(input_brigade);
238 if (status != APR_SUCCESS) {
239 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
240 "apr_brigade_flatten failed");
245 ajp_msg_reset(msg); /* will go in ajp_send_data_msg */
246 status = ajp_send_data_msg(conn->sock, r, msg, bufsiz);
247 if (status != APR_SUCCESS) {
248 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
249 "ajp_send_data_msg failed");
253 /* something is wrong TC asks for more body but we are
254 * already at the end of the body data
256 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
257 "ap_proxy_ajp_request error read after end");
261 case CMD_AJP13_SEND_HEADERS:
262 /* AJP13_SEND_HEADERS: process them */
263 status = ajp_parse_header(r, conn->data);
264 if (status != APR_SUCCESS) {
268 case CMD_AJP13_SEND_BODY_CHUNK:
269 /* AJP13_SEND_BODY_CHUNK: piece of data */
270 status = ajp_parse_data(r, conn->data, &size, &buff);
271 e = apr_bucket_transient_create(buff, size,
272 r->connection->bucket_alloc);
273 APR_BRIGADE_INSERT_TAIL(output_brigade, e);
274 if (status != APR_SUCCESS)
277 case CMD_AJP13_END_RESPONSE:
278 e = apr_bucket_eos_create(r->connection->bucket_alloc);
279 APR_BRIGADE_INSERT_TAIL(output_brigade, e);
280 if (ap_pass_brigade(r->output_filters, output_brigade) != APR_SUCCESS) {
281 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
282 "proxy: error processing body");
293 if (result == CMD_AJP13_END_RESPONSE)
296 /* read the response */
297 status = ajp_read_header(conn->sock, r,
298 (ajp_msg_t **)&(conn->data));
299 if (status != APR_SUCCESS) {
301 ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
302 "ajp_read_header failed");
305 result = ajp_parse_type(r, conn->data);
307 apr_brigade_destroy(input_brigade);
309 apr_brigade_destroy(output_brigade);
311 if (status != APR_SUCCESS) {
312 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
313 "proxy: send body failed to %pI (%s)",
314 conn->worker->cp->addr,
315 conn->worker->hostname);
316 return HTTP_SERVICE_UNAVAILABLE;
319 /* Nice we have answer to send to the client */
320 if (result == CMD_AJP13_END_RESPONSE && isok) {
321 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
322 "proxy: got response from %pI (%s)",
323 conn->worker->cp->addr,
324 conn->worker->hostname);
328 ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
329 "proxy: got bad response (%d) from %pI (%s)",
331 conn->worker->cp->addr,
332 conn->worker->hostname);
334 return HTTP_SERVICE_UNAVAILABLE;
338 * This handles http:// URLs, and other URLs using a remote proxy over http
339 * If proxyhost is NULL, then contact the server directly, otherwise
341 * Note that if a proxy is used, then URLs other than http: can be accessed,
342 * also, if we have trouble which is clearly specific to the proxy, then
343 * we return DECLINED so that we can try another proxy. (Or the direct
346 int ap_proxy_ajp_handler(request_rec *r, proxy_worker *worker,
347 proxy_server_conf *conf,
348 char *url, const char *proxyname,
349 apr_port_t proxyport)
352 char server_portstr[32];
353 conn_rec *origin = NULL;
354 proxy_conn_rec *backend = NULL;
356 const char *scheme = "AJP";
358 /* Note: Memory pool allocation.
359 * A downstream keepalive connection is always connected to the existence
360 * (or not) of an upstream keepalive connection. If this is not done then
361 * load balancing against multiple backend servers breaks (one backend
362 * server ends up taking 100% of the load), and the risk is run of
363 * downstream keepalive connections being kept open unnecessarily. This
364 * keeps webservers busy and ties up resources.
366 * As a result, we allocate all sockets out of the upstream connection
367 * pool, and when we want to reuse a socket, we check first whether the
368 * connection ID of the current upstream connection is the same as that
369 * of the connection when the socket was opened.
371 apr_pool_t *p = r->connection->pool;
372 conn_rec *c = r->connection;
373 apr_uri_t *uri = apr_palloc(r->connection->pool, sizeof(*uri));
376 if (strncasecmp(url, "ajp:", 4) != 0) {
377 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
378 "proxy: AJP: declining URL %s", url);
381 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
382 "proxy: AJP: serving URL %s", url);
385 /* only use stored info for top-level pages. Sub requests don't share
390 backend = (proxy_conn_rec *) ap_get_module_config(c->conn_config,
394 /* create space for state information */
396 status = ap_proxy_acquire_connection(scheme, &backend, worker, r->server);
399 backend->close_on_recycle = 1;
400 ap_proxy_release_connection(scheme, backend, r->server);
406 ap_set_module_config(c->conn_config, &proxy_ajp_module, backend);
412 backend->close_on_recycle = 0;
414 /* Step One: Determine Who To Connect To */
415 status = ap_proxy_determine_connection(p, r, conf, worker, backend, c->pool,
416 uri, &url, proxyname, proxyport,
418 sizeof(server_portstr));
422 /* Step Two: Make the Connection */
423 if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
424 status = HTTP_SERVICE_UNAVAILABLE;
428 /* XXX: we don't need to create the bound client connection */
430 /* Step Three: Create conn_rec */
431 if (!backend->connection) {
432 status = ap_proxy_connection_create(scheme, backend, c, r->server);
439 /* Step Four: Process the Request */
440 status = ap_proxy_ajp_request(p, r, backend, origin, conf, uri, url,
447 /* Clear the module config */
448 ap_set_module_config(c->conn_config, &proxy_ajp_module, NULL);
450 /* Do not close the socket */
451 ap_proxy_release_connection(scheme, backend, r->server);
455 static void ap_proxy_http_register_hook(apr_pool_t *p)
457 proxy_hook_scheme_handler(ap_proxy_ajp_handler, NULL, NULL, APR_HOOK_FIRST);
458 proxy_hook_canon_handler(ap_proxy_ajp_canon, NULL, NULL, APR_HOOK_FIRST);
461 module AP_MODULE_DECLARE_DATA proxy_ajp_module = {
462 STANDARD20_MODULE_STUFF,
463 NULL, /* create per-directory config structure */
464 NULL, /* merge per-directory config structures */
465 NULL, /* create per-server config structure */
466 NULL, /* merge per-server config structures */
467 NULL, /* command apr_table_t */
468 ap_proxy_http_register_hook/* register hooks */