]> granicus.if.org Git - apache/blob - modules/proxy/proxy_ajp.c
850ab1a2122626ee160059e3d7382c0957eea282
[apache] / modules / proxy / proxy_ajp.c
1 /* Copyright 1999-2004 The Apache Software Foundation
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 /* AJP routines for Apache proxy */
17
18 #include "mod_proxy.h"
19 #include "ajp.h"
20
21 module AP_MODULE_DECLARE_DATA proxy_ajp_module;
22
23 /*
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.
28  */
29 int ap_proxy_ajp_canon(request_rec *r, char *url)
30 {
31     char *host, *path, *search, sport[7];
32     const char *err;
33     const char *scheme;
34     apr_port_t port, def_port;
35
36     ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
37              "proxy: AJP: canonicalising URL %s", url);
38
39     /* ap_port_of_scheme() */
40     if (strncasecmp(url, "ajp:", 4) == 0) {
41         url += 4;
42         scheme = "ajp";
43     }    
44     /* XXX This is probably faulty */ 
45     else if (strncasecmp(url, "ajps:", 5) == 0) {
46         url += 5;
47         scheme = "ajps";
48     }
49     else {
50         return DECLINED;
51     }
52     def_port = apr_uri_port_of_scheme(scheme);
53
54     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
55              "proxy: AJP: canonicalising URL %s", url);
56
57     /* do syntatic check.
58      * We break the URL into host, port, path, search
59      */
60     port = def_port;
61     err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
62     if (err) {
63         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
64                       "error parsing URL %s: %s",
65                       url, err);
66         return HTTP_BAD_REQUEST;
67     }
68
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.
73      */
74     if (r->uri == r->unparsed_uri) {
75         search = strchr(url, '?');
76         if (search != NULL)
77             *(search++) = '\0';
78     }
79     else
80         search = r->args;
81
82     /* process path */
83     path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, r->proxyreq);
84     if (path == NULL)
85         return HTTP_BAD_REQUEST;
86
87     if (port != def_port)
88         apr_snprintf(sport, sizeof(sport), ":%d", port);
89     else
90         sport[0] = '\0';
91
92     if (ap_strchr_c(host, ':')) { /* if literal IPv6 address */
93         host = apr_pstrcat(r->pool, "[", host, "]", NULL);
94     }
95     r->filename = apr_pstrcat(r->pool, "proxy:", scheme, "://", host, sport, 
96             "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
97     return OK;
98 }
99
100 /*
101  * process the request and write the respnse.
102  */ 
103 static int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
104                                 proxy_conn_rec *conn, 
105                                 conn_rec *origin, 
106                                 proxy_server_conf *conf,
107                                 apr_uri_t *uri,
108                                 char *url, char *server_portstr)
109 {
110     apr_status_t status;
111     int result;
112     apr_bucket *e;
113     apr_bucket_brigade *input_brigade;
114     apr_bucket_brigade *output_brigade;
115     ajp_msg_t *msg;
116     apr_size_t bufsiz;
117     char *buff;
118     apr_uint16_t size;
119     const char *tenc;
120     int havebody=1;
121     int isok=1;
122
123     /*
124      * Send the AJP request to the remote server
125      */
126
127     /* send request headers */
128     status = ajp_send_header(conn->sock, r);
129     if (status != APR_SUCCESS) {
130         conn->close++;
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;
136     }
137
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");
143         return status;
144     }
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");
152     } else {
153         status = ap_get_brigade(r->input_filters, input_brigade,
154                                 AP_MODE_READBYTES, APR_BLOCK_READ,
155                                 AJP13_MAX_SEND_BODY_SZ);
156  
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;
162         }
163
164         /* have something */
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");
168         }
169
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);
173
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;
180         }
181         apr_brigade_cleanup(input_brigade);
182
183         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
184                      "proxy: got %d byte of data", bufsiz);
185         if (bufsiz > 0) {
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;
194             }
195         }
196     }
197
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;
208     }
209
210     /* parse the reponse */
211     result = ajp_parse_type(r, conn->data);
212     output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
213     
214     bufsiz = AJP13_MAX_SEND_BODY_SZ;
215     // while (result == CMD_AJP13_GET_BODY_CHUNK && bufsiz != 0) {
216     while (isok) {
217         switch (result) {
218             case CMD_AJP13_GET_BODY_CHUNK:
219                 if (havebody) {
220                     if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
221                         /* That is the end */
222                         bufsiz = 0;
223                         havebody = 0;
224                         ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
225                                      "proxy: APR_BUCKET_IS_EOS");
226                     } else {
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");
233                             break;
234                         }
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");
241                             break;
242                         }
243                     }
244
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");
250                         break;
251                     }
252                 } else {
253                     /* something is wrong TC asks for more body but we are
254                      * already at the end of the body data
255                      */
256                     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
257                          "ap_proxy_ajp_request error read after end");
258                     isok = 0;
259                 }
260                 break;
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) {
265                     isok=0;
266                 }
267                 break;
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)
275                     isok = 0;
276                 break;
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");
283                     isok=0;
284                 }
285                 break;
286             default:
287                 isok=0;
288                 break;
289         }
290         if (!isok)
291             break;
292         
293         if (result == CMD_AJP13_END_RESPONSE)
294             break;
295
296         /* read the response */
297         status = ajp_read_header(conn->sock, r,
298                                  (ajp_msg_t **)&(conn->data));
299         if (status != APR_SUCCESS) {
300             isok=0;
301             ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
302                          "ajp_read_header failed");
303             break;
304         }
305         result = ajp_parse_type(r, conn->data);
306     }
307     apr_brigade_destroy(input_brigade);
308     if (!isok)
309         apr_brigade_destroy(output_brigade);
310
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;
317     }
318
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);
325         return OK;
326     }
327
328     ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
329                  "proxy: got bad response (%d) from %pI (%s)",
330                  result,
331                  conn->worker->cp->addr,
332                  conn->worker->hostname);
333
334     return HTTP_SERVICE_UNAVAILABLE;
335 }
336
337 /*
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
340  * go via the proxy.
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
344  * route.)
345  */
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)
350 {
351     int status;
352     char server_portstr[32];
353     conn_rec *origin = NULL;
354     proxy_conn_rec *backend = NULL;
355     int is_ssl = 0;
356     const char *scheme = "AJP";
357
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.
365      *
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.
370      */
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));
374
375     
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);
379         return DECLINED;
380     }
381     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
382              "proxy: AJP: serving URL %s", url);
383     
384
385     /* only use stored info for top-level pages. Sub requests don't share 
386      * in keepalives
387      */
388 #if 0
389     if (!r->main) {
390         backend = (proxy_conn_rec *) ap_get_module_config(c->conn_config,
391                                                       &proxy_ajp_module);
392     }
393 #endif
394     /* create space for state information */
395     if (!backend) {
396         status = ap_proxy_acquire_connection(scheme, &backend, worker, r->server);
397         if (status != OK) {
398             if (backend) {
399                 backend->close_on_recycle = 1;
400                 ap_proxy_release_connection(scheme, backend, r->server);
401             }
402             return status;
403         }
404 #if 0
405         if (!r->main) {
406             ap_set_module_config(c->conn_config, &proxy_ajp_module, backend);
407         }
408 #endif
409     }
410
411     backend->is_ssl = 0;
412     backend->close_on_recycle = 0;
413
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,
417                                            server_portstr,
418                                            sizeof(server_portstr));
419
420     if (status != OK)
421         goto cleanup;
422     /* Step Two: Make the Connection */
423     if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
424         status = HTTP_SERVICE_UNAVAILABLE;
425         goto cleanup;
426     }
427 #if 0
428     /* XXX: we don't need to create the bound client connection */
429
430     /* Step Three: Create conn_rec */
431     if (!backend->connection) {
432         status = ap_proxy_connection_create(scheme, backend, c, r->server);
433         if (status != OK)
434             goto cleanup;
435     }
436 #endif
437    
438    
439     /* Step Four: Process the Request */
440     status = ap_proxy_ajp_request(p, r, backend, origin, conf, uri, url,
441                                   server_portstr);
442     if (status != OK)
443         goto cleanup;
444
445 cleanup:
446 #if 0
447     /* Clear the module config */
448     ap_set_module_config(c->conn_config, &proxy_ajp_module, NULL);
449 #endif
450     /* Do not close the socket */
451     ap_proxy_release_connection(scheme, backend, r->server);
452     return status;
453 }
454
455 static void ap_proxy_http_register_hook(apr_pool_t *p)
456 {
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);
459 }
460
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 */
469 };
470