]> granicus.if.org Git - apache/blob - modules/proxy/proxy_ajp.c
Add function protos so that -Wmissing-declarations doesn't complain.
[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 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);
28
29 /*
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.
34  */
35 int ap_proxy_ajp_canon(request_rec *r, char *url)
36 {
37     char *host, *path, *search, sport[7];
38     const char *err;
39     const char *scheme;
40     apr_port_t port, def_port;
41
42     ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
43              "proxy: AJP: canonicalising URL %s", url);
44
45     /* ap_port_of_scheme() */
46     if (strncasecmp(url, "ajp:", 4) == 0) {
47         url += 4;
48         scheme = "ajp";
49     }    
50     /* XXX This is probably faulty */ 
51     else if (strncasecmp(url, "ajps:", 5) == 0) {
52         url += 5;
53         scheme = "ajps";
54     }
55     else {
56         return DECLINED;
57     }
58     def_port = apr_uri_port_of_scheme(scheme);
59
60     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
61              "proxy: AJP: canonicalising URL %s", url);
62
63     /* do syntatic check.
64      * We break the URL into host, port, path, search
65      */
66     port = def_port;
67     err = ap_proxy_canon_netloc(r->pool, &url, NULL, NULL, &host, &port);
68     if (err) {
69         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
70                       "error parsing URL %s: %s",
71                       url, err);
72         return HTTP_BAD_REQUEST;
73     }
74
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.
79      */
80     if (r->uri == r->unparsed_uri) {
81         search = strchr(url, '?');
82         if (search != NULL)
83             *(search++) = '\0';
84     }
85     else
86         search = r->args;
87
88     /* process path */
89     path = ap_proxy_canonenc(r->pool, url, strlen(url), enc_path, r->proxyreq);
90     if (path == NULL)
91         return HTTP_BAD_REQUEST;
92
93     if (port != def_port)
94         apr_snprintf(sport, sizeof(sport), ":%d", port);
95     else
96         sport[0] = '\0';
97
98     if (ap_strchr_c(host, ':')) { /* if literal IPv6 address */
99         host = apr_pstrcat(r->pool, "[", host, "]", NULL);
100     }
101     r->filename = apr_pstrcat(r->pool, "proxy:", scheme, "://", host, sport, 
102             "/", path, (search) ? "?" : "", (search) ? search : "", NULL);
103     return OK;
104 }
105
106 /*
107  * process the request and write the respnse.
108  */ 
109 static int ap_proxy_ajp_request(apr_pool_t *p, request_rec *r,
110                                 proxy_conn_rec *conn, 
111                                 conn_rec *origin, 
112                                 proxy_server_conf *conf,
113                                 apr_uri_t *uri,
114                                 char *url, char *server_portstr)
115 {
116     apr_status_t status;
117     int result;
118     apr_bucket *e;
119     apr_bucket_brigade *input_brigade;
120     apr_bucket_brigade *output_brigade;
121     ajp_msg_t *msg;
122     apr_size_t bufsiz;
123     char *buff;
124     apr_uint16_t size;
125     const char *tenc;
126     int havebody=1;
127     int isok=1;
128
129     /*
130      * Send the AJP request to the remote server
131      */
132
133     /* send request headers */
134     status = ajp_send_header(conn->sock, r);
135     if (status != APR_SUCCESS) {
136         conn->close++;
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;
142     }
143
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");
149         return status;
150     }
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");
158     } else {
159         status = ap_get_brigade(r->input_filters, input_brigade,
160                                 AP_MODE_READBYTES, APR_BLOCK_READ,
161                                 AJP13_MAX_SEND_BODY_SZ);
162  
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;
168         }
169
170         /* have something */
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");
174         }
175
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);
179
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;
186         }
187         apr_brigade_cleanup(input_brigade);
188
189         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
190                      "proxy: got %d byte of data", bufsiz);
191         if (bufsiz > 0) {
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;
200             }
201         }
202     }
203
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;
214     }
215
216     /* parse the reponse */
217     result = ajp_parse_type(r, conn->data);
218     output_brigade = apr_brigade_create(p, r->connection->bucket_alloc);
219     
220     bufsiz = AJP13_MAX_SEND_BODY_SZ;
221     // while (result == CMD_AJP13_GET_BODY_CHUNK && bufsiz != 0) {
222     while (isok) {
223         switch (result) {
224             case CMD_AJP13_GET_BODY_CHUNK:
225                 if (havebody) {
226                     if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(input_brigade))) {
227                         /* That is the end */
228                         bufsiz = 0;
229                         havebody = 0;
230                         ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
231                                      "proxy: APR_BUCKET_IS_EOS");
232                     } else {
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");
239                             break;
240                         }
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");
247                             break;
248                         }
249                     }
250
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");
256                         break;
257                     }
258                 } else {
259                     /* something is wrong TC asks for more body but we are
260                      * already at the end of the body data
261                      */
262                     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
263                          "ap_proxy_ajp_request error read after end");
264                     isok = 0;
265                 }
266                 break;
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) {
271                     isok=0;
272                 }
273                 break;
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)
281                     isok = 0;
282                 break;
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");
289                     isok=0;
290                 }
291                 break;
292             default:
293                 isok=0;
294                 break;
295         }
296         if (!isok)
297             break;
298         
299         if (result == CMD_AJP13_END_RESPONSE)
300             break;
301
302         /* read the response */
303         status = ajp_read_header(conn->sock, r,
304                                  (ajp_msg_t **)&(conn->data));
305         if (status != APR_SUCCESS) {
306             isok=0;
307             ap_log_error(APLOG_MARK, APLOG_DEBUG, status, r->server,
308                          "ajp_read_header failed");
309             break;
310         }
311         result = ajp_parse_type(r, conn->data);
312     }
313     apr_brigade_destroy(input_brigade);
314     if (!isok)
315         apr_brigade_destroy(output_brigade);
316
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;
323     }
324
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);
331         return OK;
332     }
333
334     ap_log_error(APLOG_MARK, APLOG_ERR, status, r->server,
335                  "proxy: got bad response (%d) from %pI (%s)",
336                  result,
337                  conn->worker->cp->addr,
338                  conn->worker->hostname);
339
340     return HTTP_SERVICE_UNAVAILABLE;
341 }
342
343 /*
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
346  * go via the proxy.
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
350  * route.)
351  */
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)
356 {
357     int status;
358     char server_portstr[32];
359     conn_rec *origin = NULL;
360     proxy_conn_rec *backend = NULL;
361     int is_ssl = 0;
362     const char *scheme = "AJP";
363
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.
371      *
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.
376      */
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));
380
381     
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);
385         return DECLINED;
386     }
387     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
388              "proxy: AJP: serving URL %s", url);
389     
390
391     /* only use stored info for top-level pages. Sub requests don't share 
392      * in keepalives
393      */
394 #if 0
395     if (!r->main) {
396         backend = (proxy_conn_rec *) ap_get_module_config(c->conn_config,
397                                                       &proxy_ajp_module);
398     }
399 #endif
400     /* create space for state information */
401     if (!backend) {
402         status = ap_proxy_acquire_connection(scheme, &backend, worker, r->server);
403         if (status != OK) {
404             if (backend) {
405                 backend->close_on_recycle = 1;
406                 ap_proxy_release_connection(scheme, backend, r->server);
407             }
408             return status;
409         }
410 #if 0
411         if (!r->main) {
412             ap_set_module_config(c->conn_config, &proxy_ajp_module, backend);
413         }
414 #endif
415     }
416
417     backend->is_ssl = 0;
418     backend->close_on_recycle = 0;
419
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,
423                                            server_portstr,
424                                            sizeof(server_portstr));
425
426     if (status != OK)
427         goto cleanup;
428     /* Step Two: Make the Connection */
429     if (ap_proxy_connect_backend(scheme, backend, worker, r->server)) {
430         status = HTTP_SERVICE_UNAVAILABLE;
431         goto cleanup;
432     }
433 #if 0
434     /* XXX: we don't need to create the bound client connection */
435
436     /* Step Three: Create conn_rec */
437     if (!backend->connection) {
438         status = ap_proxy_connection_create(scheme, backend, c, r->server);
439         if (status != OK)
440             goto cleanup;
441     }
442 #endif
443    
444    
445     /* Step Four: Process the Request */
446     status = ap_proxy_ajp_request(p, r, backend, origin, conf, uri, url,
447                                   server_portstr);
448     if (status != OK)
449         goto cleanup;
450
451 cleanup:
452 #if 0
453     /* Clear the module config */
454     ap_set_module_config(c->conn_config, &proxy_ajp_module, NULL);
455 #endif
456     /* Do not close the socket */
457     ap_proxy_release_connection(scheme, backend, r->server);
458     return status;
459 }
460
461 static void ap_proxy_http_register_hook(apr_pool_t *p)
462 {
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);
465 }
466
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 */
475 };
476