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.
20 * Main include file for the Apache proxy
25 Also note numerous FIXMEs and CHECKMEs which should be eliminated.
27 This code is once again experimental!
31 1. Make it completely work (for FTP too)
35 Chuck Murcko <chuck@topsail.org> 02-06-01
41 #include "apr_hooks.h"
44 #include "apr_strings.h"
45 #include "apr_buckets.h"
47 #include "apr_network_io.h"
48 #include "apr_pools.h"
49 #include "apr_strings.h"
52 #include "apr_strmatch.h"
53 #include "apr_fnmatch.h"
54 #include "apr_reslist.h"
55 #define APR_WANT_STRFUNC
59 #include "http_config.h"
60 #include "ap_config.h"
61 #include "http_core.h"
62 #include "http_protocol.h"
63 #include "http_request.h"
64 #include "http_vhost.h"
65 #include "http_main.h"
67 #include "http_connection.h"
68 #include "util_filter.h"
69 #include "util_ebcdic.h"
71 #if APR_HAVE_NETINET_IN_H
72 #include <netinet/in.h>
74 #if APR_HAVE_ARPA_INET_H
75 #include <arpa/inet.h>
78 /* for proxy_canonenc() */
80 enc_path, enc_search, enc_user, enc_fpath, enc_parm
83 #if APR_CHARSET_EBCDIC
85 #else /*APR_CHARSET_EBCDIC*/
86 #define CRLF "\015\012"
87 #endif /*APR_CHARSET_EBCDIC*/
89 /* default Max-Forwards header setting */
90 #define DEFAULT_MAX_FORWARDS 10
92 /* static information about a remote proxy */
94 const char *scheme; /* the schemes handled by this proxy, or '*' */
95 const char *protocol; /* the scheme used to talk to this proxy */
96 const char *hostname; /* the hostname of this proxy */
97 apr_port_t port; /* the port for this proxy */
98 regex_t *regexp; /* compiled regex (if any) for the remote */
99 int use_regex; /* simple boolean. True if we have a regex pattern */
107 struct dirconn_entry {
109 struct in_addr addr, mask;
110 struct apr_sockaddr_t *hostaddr;
111 int (*matcher) (struct dirconn_entry * This, request_rec *r);
114 struct noproxy_entry {
116 struct apr_sockaddr_t *addr;
119 typedef struct proxy_balancer proxy_balancer;
120 typedef struct proxy_worker proxy_worker;
121 typedef struct proxy_conn_pool proxy_conn_pool;
124 apr_array_header_t *proxies;
125 apr_array_header_t *sec_proxy;
126 apr_array_header_t *aliases;
127 apr_array_header_t *raliases;
128 apr_array_header_t *noproxies;
129 apr_array_header_t *dirconn;
130 apr_array_header_t *allowed_connect_ports;
131 apr_array_header_t *workers;
132 apr_array_header_t *balancers;
133 proxy_worker *forward; /* forward proxy worker */
134 const char *domain; /* domain name to use in absence of a domain name in the request */
135 int req; /* true if proxy requests are enabled */
142 } viaopt; /* how to deal with proxy Via: headers */
144 apr_size_t recv_buffer_size;
145 char recv_buffer_size_set;
146 apr_size_t io_buffer_size;
147 char io_buffer_size_set;
151 * the following setting masks the error page
152 * returned from the 'proxied server' and just
153 * forwards the status code upwards.
154 * This allows the main server (us) to generate
155 * the error page, (so it will look like a error
156 * returned from the rest of the system
159 int error_override_set;
161 int preserve_host_set;
162 apr_interval_time_t timeout;
168 } badopt; /* how to deal with bad headers */
170 /* putting new stuff on the end maximises binary back-compatibility.
171 * the strmatch_patterns are really a const just to have a
172 * case-independent strstr.
174 apr_array_header_t* cookie_paths;
175 apr_array_header_t* cookie_domains;
176 const apr_strmatch_pattern* cookie_path_str;
177 const apr_strmatch_pattern* cookie_domain_str;
182 } proxy_status; /* Status display options */
183 char proxy_status_set;
184 apr_pool_t *pool; /* Pool used for allocating this struct */
189 const char *p; /* The path */
190 int p_is_fnmatch; /* Is this path an fnmatch candidate? */
191 regex_t *r; /* Is this a regex? */
195 conn_rec *connection;
196 const char *hostname;
199 apr_pool_t *pool; /* Subpool used for creating socket */
200 apr_socket_t *sock; /* Connection socket */
201 apr_sockaddr_t *addr; /* Preparsed remote address info */
202 apr_uint32_t flags; /* Conection flags */
203 int close; /* Close 'this' connection */
204 int close_on_recycle; /* Close the connection when returning to pool */
205 proxy_worker *worker; /* Connection pool this connection belogns to */
206 void *data; /* per scheme connection data */
210 float cache_completion; /* completion percentage */
211 int content_length; /* length of the content */
214 /* Connection pool */
215 struct proxy_conn_pool {
216 apr_pool_t *pool; /* The pool used in constructor and destructor calls */
217 apr_sockaddr_t *addr; /* Preparsed remote address info */
219 apr_reslist_t *res; /* Connection resource list */
221 int nfree; /* Balancer free count number */
222 proxy_conn_rec *conn; /* Single connection for prefork mpm's */
225 /* woker status flags */
226 #define PROXY_WORKER_INITIALIZED 0x0001
227 #define PROXY_WORKER_IGNORE_ERRORS 0x0002
228 #define PROXY_WORKER_IN_SHUTDOWN 0x0010
229 #define PROXY_WORKER_DISABLED 0x0020
230 #define PROXY_WORKER_IN_ERROR 0x0040
232 #define PROXY_WORKER_IS_USABLE(f) (!((f)->status & 0x00F0))
235 /* Worker configuration */
236 struct proxy_worker {
238 apr_time_t error_time; /* time of the last error */
239 apr_interval_time_t retry; /* retry interval */
240 int retries; /* number of retries on this worker */
241 int lbfactor; /* initial load balancing factor */
243 const char *scheme; /* scheme to use ajp|http|https */
244 const char *hostname; /* remote backend address */
245 const char *route; /* balancing route */
246 const char *redirect; /* temporary balancing redirection route */
248 int min; /* Desired minimum number of available connections */
249 int smax; /* Soft maximum on the total number of connections */
250 int hmax; /* Hard maximum on the total number of connections */
251 apr_interval_time_t ttl; /* maximum amount of time in seconds a connection
252 * may be available while exceeding the soft limit */
253 apr_interval_time_t timeout; /* connection timeout */
255 apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
257 apr_size_t recv_buffer_size;
258 char recv_buffer_size_set;
259 apr_size_t io_buffer_size;
260 char io_buffer_size_set;
263 proxy_conn_pool *cp; /* Connection pool to use */
264 void *opaque; /* per scheme worker data */
267 /* Runtime worker status informations. Shared in scoreboard */
269 double lbstatus; /* Current lbstatus */
270 double lbfactor; /* dynamic lbfactor */
271 apr_size_t transfered; /* Number of bytes transfered to remote */
272 apr_size_t readed; /* Number of bytes readed from remote */
273 apr_size_t elected; /* Number of times the worker was elected */
274 } proxy_runtime_stat;
276 /* Runtime worker. */
278 int id; /* scoreboard id */
279 proxy_balancer *b; /* balancer containing this worker */
281 proxy_runtime_stat *s;
282 } proxy_runtime_worker;
284 struct proxy_balancer {
285 apr_array_header_t *workers; /* array of proxy_runtime_workers */
286 const char *name; /* name of the load balancer */
287 const char *sticky; /* sticky session identifier */
288 int sticky_force; /* Disable failover for sticky sessions */
289 apr_interval_time_t timeout; /* Timeout for waiting on free connection */
290 int max_attempts; /* Number of attempts before failing */
291 char max_attempts_set;
293 /* XXX: Perhaps we will need the proc mutex too.
294 * Altrough we are only using arithmetic operations
295 * it may lead to a incorrect calculations.
296 * For now use only the thread mutex.
299 apr_thread_mutex_t *mutex; /* Thread lock for updating lb params */
305 /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and
306 * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
309 #define PROXY_DECLARE(type) type
310 #define PROXY_DECLARE_NONSTD(type) type
311 #define PROXY_DECLARE_DATA
312 #elif defined(PROXY_DECLARE_STATIC)
313 #define PROXY_DECLARE(type) type __stdcall
314 #define PROXY_DECLARE_NONSTD(type) type
315 #define PROXY_DECLARE_DATA
316 #elif defined(PROXY_DECLARE_EXPORT)
317 #define PROXY_DECLARE(type) __declspec(dllexport) type __stdcall
318 #define PROXY_DECLARE_NONSTD(type) __declspec(dllexport) type
319 #define PROXY_DECLARE_DATA __declspec(dllexport)
321 #define PROXY_DECLARE(type) __declspec(dllimport) type __stdcall
322 #define PROXY_DECLARE_NONSTD(type) __declspec(dllimport) type
323 #define PROXY_DECLARE_DATA __declspec(dllimport)
327 * Hook an optional proxy hook. Unlike static hooks, this uses a macro
328 * instead of a function.
330 #define PROXY_OPTIONAL_HOOK(name,fn,pre,succ,order) \
331 APR_OPTIONAL_HOOK(proxy,name,fn,pre,succ,order)
333 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, scheme_handler, (request_rec *r,
334 proxy_worker *worker, proxy_server_conf *conf, char *url,
335 const char *proxyhost, apr_port_t proxyport))
336 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, canon_handler, (request_rec *r,
339 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
340 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r))
344 * It will return the most suitable worker at the moment
345 * and coresponding balancer.
346 * The url is rewritten from balancer://cluster/uri to scheme://host:port/uri
347 * and then the scheme_handler is called.
350 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
351 proxy_balancer **balancer,
353 proxy_server_conf *conf, char **url))
356 * It is called after request for updating runtime balancer status.
358 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, post_request, (proxy_worker *worker,
359 proxy_balancer *balancer, request_rec *r,
360 proxy_server_conf *conf))
365 PROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r);
366 PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
367 PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
368 PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
370 PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
371 char **passwordp, char **hostp, apr_port_t *port);
372 PROXY_DECLARE(const char *)ap_proxy_date_canon(apr_pool_t *p, const char *x);
373 PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val);
374 PROXY_DECLARE(char *)ap_proxy_removestr(apr_pool_t *pool, const char *list, const char *val);
375 PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x);
376 PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y);
377 PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
378 PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p);
379 PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p);
380 PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p);
381 PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
382 PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
383 PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
384 PROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb, char *buff, size_t bufflen, int *eos);
385 PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key);
386 /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
387 PROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **, const char *, apr_sockaddr_t *, const char *, proxy_server_conf *, server_rec *, apr_pool_t *);
388 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
389 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
391 /* Connection pool API */
393 * Get the worker from proxy configuration
394 * @param p memory pool used for finding worker
395 * @param conf current proxy server configuration
396 * @param url url to find the worker from
397 * @return proxy_worker or NULL if not found
399 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
400 proxy_server_conf *conf,
403 * Add the worker to proxy configuration
404 * @param worker the new worker
405 * @param p memory pool to allocate worker from
406 * @param conf current proxy server configuration
407 * @param url url containing worker name
408 * @return error message or NULL if successfull
410 PROXY_DECLARE(const char *) ap_proxy_add_worker(proxy_worker **worker,
412 proxy_server_conf *conf,
417 * @param p memory pool to allocate worker from
420 PROXY_DECLARE(proxy_worker *) ap_proxy_create_worker(apr_pool_t *p);
424 * @param worker the new worker
425 * @param p memory pool to allocate worker from
426 * @param s current server record
427 * @return APR_SUCCESS or error code
429 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
432 * Get the balancer from proxy configuration
433 * @param p memory pool used for finding balancer
434 * @param conf current proxy server configuration
435 * @param url url to find the worker from. Has to have balancer:// prefix
436 * @return proxy_balancer or NULL if not found
438 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
439 proxy_server_conf *conf,
442 * Add the balancer to proxy configuration
443 * @param balancer the new balancer
444 * @param p memory pool to allocate balancer from
445 * @param conf current proxy server configuration
446 * @param url url containing balancer name
447 * @return error message or NULL if successfull
449 PROXY_DECLARE(const char *) ap_proxy_add_balancer(proxy_balancer **balancer,
451 proxy_server_conf *conf,
455 * Add the worker to the balancer
456 * @param pool memory pool for adding worker
457 * @param balancer balancer to add to
458 * @param balancer worker to add
459 * @note Single worker can be added to multiple balancers.
461 PROXY_DECLARE(void) ap_proxy_add_worker_to_balancer(apr_pool_t *pool,
462 proxy_balancer *balancer,
463 proxy_worker *worker);
465 * Get the most suitable worker and(or) balancer for the request
466 * @param worker worker used for processing request
467 * @param balancer balancer used for processing request
468 * @param r current request
469 * @param conf current proxy server configuration
470 * @param url request url that balancer can rewrite.
471 * @return OK or HTTP_XXX error
472 * @note It calls balancer pre_request hook if the url starts with balancer://
473 * The balancer then rewrites the url to particular worker, like http://host:port
475 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
476 proxy_balancer **balancer,
478 proxy_server_conf *conf,
481 * Post request worker and balancer cleanup
482 * @param worker worker used for processing request
483 * @param balancer balancer used for processing request
484 * @param r current request
485 * @param conf current proxy server configuration
486 * @return OK or HTTP_XXX error
487 * @note When ever the pre_request is called, the post_request has to be
490 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
491 proxy_balancer *balancer,
493 proxy_server_conf *conf);
495 * Deternime backend hostname and port
496 * @param p memory pool used for processing
497 * @param r current request
498 * @param conf current proxy server configuration
499 * @param worker worker used for processing request
500 * @param conn proxy connection struct
501 * @param uri processed uri
502 * @param url request url
503 * @param proxyname are we connecting directly or via s proxy
504 * @param proxyport proxy host port
505 * @param server_portstr Via headers server port
506 * @param server_portstr_size size of the server_portstr buffer
507 * @return OK or HTTP_XXX error
509 PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
510 proxy_server_conf *conf,
511 proxy_worker *worker,
512 proxy_conn_rec *conn,
515 const char *proxyname,
516 apr_port_t proxyport,
517 char *server_portstr,
518 int server_portstr_size);
520 * Mark a worker for retry
521 * @param proxy_function calling proxy scheme (http, ajp, ...)
522 * @param conf current proxy server configuration
523 * @param worker worker used for retrying
524 * @param s current server record
525 * @return OK if marked for retry, DECLINED otherwise
526 * @note Worker will be marker for retry if the time of the last retry
527 * has been ellapsed. In case there is no retry option set, defaults to
528 * number_of_retries seconds.
530 PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
531 proxy_worker *worker,
534 * Acquire a connection from workers connection pool
535 * @param proxy_function calling proxy scheme (http, ajp, ...)
536 * @param conn acquired connection
537 * @param worker worker used for obtaining connection
538 * @param s current server record
539 * @return OK or HTTP_XXX error
540 * @note If the number of connections is exhaused the function will
541 * block untill the timeout is reached.
543 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
544 proxy_conn_rec **conn,
545 proxy_worker *worker,
548 * Release a connection back to worker connection pool
549 * @param proxy_function calling proxy scheme (http, ajp, ...)
550 * @param conn acquired connection
551 * @param s current server record
552 * @return OK or HTTP_XXX error
553 * @note The connection will be closed if conn->close_on_release is set
555 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
556 proxy_conn_rec *conn,
559 * Make a connection to the backend
560 * @param proxy_function calling proxy scheme (http, ajp, ...)
561 * @param conn acquired connection
562 * @param worker connection worker
563 * @param s current server record
564 * @return OK or HTTP_XXX error
565 * @note In case the socket already exists for conn, just check the link
568 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
569 proxy_conn_rec *conn,
570 proxy_worker *worker,
573 * Make a connection record for backend connection
574 * @param proxy_function calling proxy scheme (http, ajp, ...)
575 * @param conn acquired connection
576 * @param c client connection record
577 * @param s current server record
578 * @return OK or HTTP_XXX error
580 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
581 proxy_conn_rec *conn,
582 conn_rec *c, server_rec *s);
585 #if MODULE_MAGIC_NUMBER_MAJOR > 20020903
586 #define PROXY_HAS_SCOREBOARD 1
588 #define PROXY_HAS_SCOREBOARD 0
590 /* The number of dynamic balancers that can be added */
591 #define PROXY_DYNAMIC_BALANCER_LIMIT 16
592 int ap_proxy_lb_workers(void);
595 extern module PROXY_DECLARE_DATA proxy_module;
597 #endif /*MOD_PROXY_H*/