]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy.h
Add regex pattern matching to ProxyPass, allowing,
[apache] / modules / proxy / mod_proxy.h
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef MOD_PROXY_H
18 #define MOD_PROXY_H 
19
20 /**
21  * @file  mod_proxy.h
22  * @brief Proxy Extension Module for Apache
23  *
24  * @defgroup MOD_PROXY mod_proxy
25  * @ingroup  APACHE_MODS
26  * @{
27  */
28
29 /*
30
31    Also note numerous FIXMEs and CHECKMEs which should be eliminated.
32
33    This code is once again experimental!
34
35    Things to do:
36
37    1. Make it completely work (for FTP too)
38
39    2. HTTP/1.1
40
41    Chuck Murcko <chuck@topsail.org> 02-06-01
42
43  */
44
45 #define CORE_PRIVATE
46
47 #include "apr_hooks.h"
48 #include "apr.h"
49 #include "apr_lib.h"
50 #include "apr_strings.h"
51 #include "apr_buckets.h"
52 #include "apr_md5.h"
53 #include "apr_network_io.h"
54 #include "apr_pools.h"
55 #include "apr_strings.h"
56 #include "apr_uri.h"
57 #include "apr_date.h"
58 #include "apr_strmatch.h"
59 #include "apr_fnmatch.h"
60 #include "apr_reslist.h"
61 #define APR_WANT_STRFUNC
62 #include "apr_want.h"
63
64 #include "httpd.h"
65 #include "http_config.h"
66 #include "ap_config.h"
67 #include "http_core.h"
68 #include "http_protocol.h"
69 #include "http_request.h"
70 #include "http_vhost.h"
71 #include "http_main.h"
72 #include "http_log.h"
73 #include "http_connection.h"
74 #include "util_filter.h"
75 #include "util_ebcdic.h"
76 #include "ap_provider.h"
77
78 #if APR_HAVE_NETINET_IN_H
79 #include <netinet/in.h>
80 #endif
81 #if APR_HAVE_ARPA_INET_H
82 #include <arpa/inet.h>
83 #endif
84
85 /* for proxy_canonenc() */
86 enum enctype {
87     enc_path, enc_search, enc_user, enc_fpath, enc_parm
88 };
89
90 #if APR_CHARSET_EBCDIC
91 #define CRLF   "\r\n"
92 #else /*APR_CHARSET_EBCDIC*/
93 #define CRLF   "\015\012"
94 #endif /*APR_CHARSET_EBCDIC*/
95
96 /* default Max-Forwards header setting */
97 #define DEFAULT_MAX_FORWARDS    10
98
99 /* static information about a remote proxy */
100 struct proxy_remote {
101     const char *scheme;     /* the schemes handled by this proxy, or '*' */
102     const char *protocol;   /* the scheme used to talk to this proxy */
103     const char *hostname;   /* the hostname of this proxy */
104     apr_port_t  port;       /* the port for this proxy */
105     ap_regex_t *regexp;        /* compiled regex (if any) for the remote */
106     int use_regex;          /* simple boolean. True if we have a regex pattern */
107 };
108
109 struct proxy_alias {
110     const char  *real;
111     const char  *fake;
112     ap_regex_t  *regex;
113 };
114
115 struct dirconn_entry {
116     char *name;
117     struct in_addr addr, mask;
118     struct apr_sockaddr_t *hostaddr;
119     int (*matcher) (struct dirconn_entry * This, request_rec *r);
120 };
121
122 struct noproxy_entry {
123     const char *name;
124     struct apr_sockaddr_t *addr;
125 };
126
127 typedef struct proxy_balancer  proxy_balancer;
128 typedef struct proxy_worker    proxy_worker;
129 typedef struct proxy_conn_pool proxy_conn_pool;
130 typedef struct proxy_balancer_method proxy_balancer_method;
131
132 typedef struct {
133     apr_array_header_t *proxies;
134     apr_array_header_t *sec_proxy;
135     apr_array_header_t *aliases;
136     apr_array_header_t *noproxies;
137     apr_array_header_t *dirconn;
138     apr_array_header_t *allowed_connect_ports;
139     apr_array_header_t *workers;
140     apr_array_header_t *balancers;
141     proxy_worker       *forward;    /* forward proxy worker */
142     proxy_worker       *reverse;    /* reverse "module-driven" proxy worker */
143     const char *domain;     /* domain name to use in absence of a domain name in the request */
144     int req;                /* true if proxy requests are enabled */
145     char req_set;
146     enum {
147       via_off,
148       via_on,
149       via_block,
150       via_full
151     } viaopt;                   /* how to deal with proxy Via: headers */
152     char viaopt_set;
153     apr_size_t recv_buffer_size;
154     char recv_buffer_size_set;
155     apr_size_t io_buffer_size;
156     char io_buffer_size_set;
157     long maxfwd;
158     char maxfwd_set;
159     /** 
160      * the following setting masks the error page
161      * returned from the 'proxied server' and just 
162      * forwards the status code upwards.
163      * This allows the main server (us) to generate
164      * the error page, (so it will look like a error
165      * returned from the rest of the system 
166      */
167     int error_override;
168     int error_override_set;
169     int preserve_host;
170     int preserve_host_set;
171     apr_interval_time_t timeout;
172     char timeout_set;
173     enum {
174       bad_error,
175       bad_ignore,
176       bad_body
177     } badopt;                   /* how to deal with bad headers */
178     char badopt_set;
179 /* putting new stuff on the end maximises binary back-compatibility.
180  * the strmatch_patterns are really a const just to have a
181  * case-independent strstr.
182  */
183     enum {
184         status_off,
185         status_on,
186         status_full
187     } proxy_status;             /* Status display options */
188     char proxy_status_set;
189     apr_pool_t *pool;           /* Pool used for allocating this struct */
190 } proxy_server_conf;
191
192
193 typedef struct {
194     const char *p;            /* The path */
195     int         p_is_fnmatch; /* Is this path an fnmatch candidate? */
196     ap_regex_t  *r;            /* Is this a regex? */
197
198 /* ProxyPassReverse and friends are documented as working inside
199  * <Location>.  But in fact they never have done in the case of
200  * more than one <Location>, because the server_conf can't see it.
201  * We need to move them to the per-dir config.
202  * Discussed in February:
203  * http://marc.theaimsgroup.com/?l=apache-httpd-dev&m=110726027118798&w=2
204  */
205     apr_array_header_t *raliases;
206     apr_array_header_t* cookie_paths;
207     apr_array_header_t* cookie_domains;
208     const apr_strmatch_pattern* cookie_path_str;
209     const apr_strmatch_pattern* cookie_domain_str;
210     int interpolate_env;
211 } proxy_dir_conf;
212
213 /* if we interpolate env vars per-request, we'll need a per-request
214  * copy of the reverse proxy config
215  */
216 typedef struct {
217     apr_array_header_t *raliases;
218     apr_array_header_t* cookie_paths;
219     apr_array_header_t* cookie_domains;
220 } proxy_req_conf;
221
222 typedef struct {
223     conn_rec     *connection;
224     const char   *hostname;
225     apr_port_t   port;
226     int          is_ssl;
227     apr_pool_t   *pool;     /* Subpool used for creating socket */
228     apr_socket_t *sock;     /* Connection socket */
229     apr_sockaddr_t *addr;   /* Preparsed remote address info */
230     apr_uint32_t flags;     /* Conection flags */
231     int          close;     /* Close 'this' connection */
232     proxy_worker *worker;   /* Connection pool this connection belongs to */
233     void         *data;     /* per scheme connection data */
234 #if APR_HAS_THREADS
235     int          inreslist; /* connection in apr_reslist? */
236 #endif
237 } proxy_conn_rec;
238
239 typedef struct {
240         float cache_completion; /* completion percentage */
241         int content_length; /* length of the content */
242 } proxy_completion;
243
244 /* Connection pool */
245 struct proxy_conn_pool {
246     apr_pool_t     *pool;   /* The pool used in constructor and destructor calls */
247     apr_sockaddr_t *addr;   /* Preparsed remote address info */
248 #if APR_HAS_THREADS
249     apr_reslist_t  *res;    /* Connection resource list */
250 #endif
251     proxy_conn_rec *conn;   /* Single connection for prefork mpm's */
252 };
253
254 /* worker status flags */
255 #define PROXY_WORKER_INITIALIZED    0x0001
256 #define PROXY_WORKER_IGNORE_ERRORS  0x0002
257 #define PROXY_WORKER_IN_SHUTDOWN    0x0010
258 #define PROXY_WORKER_DISABLED       0x0020
259 #define PROXY_WORKER_STOPPED        0x0040
260 #define PROXY_WORKER_IN_ERROR       0x0080
261 #define PROXY_WORKER_HOT_STANDBY    0x0100
262
263 #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
264 PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
265
266 #define PROXY_WORKER_IS_INITIALIZED(f)   ( (f)->s->status & \
267   PROXY_WORKER_INITIALIZED )
268
269 #define PROXY_WORKER_IS_STANDBY(f)   ( (f)->s->status & \
270   PROXY_WORKER_HOT_STANDBY )
271
272 #define PROXY_WORKER_IS_USABLE(f)   ( !((f)->s->status & \
273   (PROXY_WORKER_NOT_USABLE_BITMAP)) && PROXY_WORKER_IS_INITIALIZED(f) )
274
275 /* default worker retry timeout in seconds */
276 #define PROXY_WORKER_DEFAULT_RETRY  60
277 #define PROXY_WORKER_MAX_ROUTE_SIZ  63
278
279 /* Runtime worker status informations. Shared in scoreboard */
280 typedef struct {
281     int             status;
282     apr_time_t      error_time; /* time of the last error */
283     int             retries;    /* number of retries on this worker */
284     int             lbstatus;   /* Current lbstatus */
285     int             lbfactor;   /* dynamic lbfactor */
286     apr_off_t       transferred;/* Number of bytes transferred to remote */
287     apr_off_t       read;       /* Number of bytes read from remote */
288     apr_size_t      elected;    /* Number of times the worker was elected */
289     char            route[PROXY_WORKER_MAX_ROUTE_SIZ+1];
290     char            redirect[PROXY_WORKER_MAX_ROUTE_SIZ+1];
291     void            *context;   /* general purpose storage */
292     apr_size_t      busy;       /* busyness factor */
293     int             lbset;      /* load balancer cluster set */
294 } proxy_worker_stat;
295
296 /* Worker configuration */
297 struct proxy_worker {
298     int             id;         /* scoreboard id */
299     apr_interval_time_t retry;  /* retry interval */
300     int             lbfactor;   /* initial load balancing factor */
301     const char      *name;
302     const char      *scheme;    /* scheme to use ajp|http|https */
303     const char      *hostname;  /* remote backend address */
304     const char      *route;     /* balancing route */
305     const char      *redirect;  /* temporary balancing redirection route */
306     int             status;     /* temporary worker status */
307     apr_port_t      port;
308     int             min;        /* Desired minimum number of available connections */
309     int             smax;       /* Soft maximum on the total number of connections */
310     int             hmax;       /* Hard maximum on the total number of connections */
311     apr_interval_time_t ttl;    /* maximum amount of time in seconds a connection
312                                  * may be available while exceeding the soft limit */
313     apr_interval_time_t timeout; /* connection timeout */
314     char            timeout_set;
315     apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
316     char            acquire_set;
317     apr_size_t      recv_buffer_size;
318     char            recv_buffer_size_set;
319     apr_size_t      io_buffer_size;
320     char            io_buffer_size_set;
321     char            keepalive;
322     char            keepalive_set;
323     proxy_conn_pool     *cp;        /* Connection pool to use */
324     proxy_worker_stat   *s;         /* Shared data */
325     void            *opaque;    /* per scheme worker data */
326     int             is_address_reusable;
327 #if APR_HAS_THREADS
328     apr_thread_mutex_t  *mutex;  /* Thread lock for updating address cache */
329 #endif
330     void            *context;   /* general purpose storage */
331     enum {
332          flush_off,
333          flush_on,
334          flush_auto
335     } flush_packets;           /* control AJP flushing */
336     int             flush_wait;  /* poll wait time in microseconds if flush_auto */
337     apr_interval_time_t ping_timeout;
338     char ping_timeout_set;
339     int             lbset;      /* load balancer cluster set */
340     char            retry_set;
341 };
342
343 /*
344  * Wait 10000 microseconds to find out if more data is currently
345  * available at the backend. Just an arbitrary choose.
346  */
347 #define PROXY_FLUSH_WAIT 10000
348
349 struct proxy_balancer {
350     apr_array_header_t *workers; /* array of proxy_workers */
351     const char *name;            /* name of the load balancer */
352     const char *sticky;          /* sticky session identifier */
353     int         sticky_force;    /* Disable failover for sticky sessions */
354     apr_interval_time_t timeout; /* Timeout for waiting on free connection */
355     int                 max_attempts; /* Number of attempts before failing */
356     char                max_attempts_set;
357     proxy_balancer_method *lbmethod;
358
359     /* XXX: Perhaps we will need the proc mutex too.
360      * Altrough we are only using arithmetic operations
361      * it may lead to a incorrect calculations.
362      * For now use only the thread mutex.
363      */
364 #if APR_HAS_THREADS
365     apr_thread_mutex_t  *mutex;  /* Thread lock for updating lb params */
366 #endif
367     void            *context;   /* general purpose storage */
368 };
369
370 struct proxy_balancer_method {
371     const char *name;            /* name of the load balancer method*/
372     proxy_worker *(*finder)(proxy_balancer *balancer,
373                             request_rec *r);
374     void            *context;   /* general purpose storage */
375 };
376
377 #if APR_HAS_THREADS
378 #define PROXY_THREAD_LOCK(x)      apr_thread_mutex_lock((x)->mutex)
379 #define PROXY_THREAD_UNLOCK(x)    apr_thread_mutex_unlock((x)->mutex)
380 #else
381 #define PROXY_THREAD_LOCK(x)      APR_SUCCESS
382 #define PROXY_THREAD_UNLOCK(x)    APR_SUCCESS
383 #endif
384
385 /* hooks */
386
387 /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and 
388  * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
389  */
390 #if !defined(WIN32)
391 #define PROXY_DECLARE(type)            type
392 #define PROXY_DECLARE_NONSTD(type)     type
393 #define PROXY_DECLARE_DATA
394 #elif defined(PROXY_DECLARE_STATIC)
395 #define PROXY_DECLARE(type)            type __stdcall
396 #define PROXY_DECLARE_NONSTD(type)     type
397 #define PROXY_DECLARE_DATA
398 #elif defined(PROXY_DECLARE_EXPORT)
399 #define PROXY_DECLARE(type)            __declspec(dllexport) type __stdcall
400 #define PROXY_DECLARE_NONSTD(type)     __declspec(dllexport) type
401 #define PROXY_DECLARE_DATA             __declspec(dllexport)
402 #else
403 #define PROXY_DECLARE(type)            __declspec(dllimport) type __stdcall
404 #define PROXY_DECLARE_NONSTD(type)     __declspec(dllimport) type
405 #define PROXY_DECLARE_DATA             __declspec(dllimport)
406 #endif
407
408 /**
409  * Hook an optional proxy hook.  Unlike static hooks, this uses a macro
410  * instead of a function.
411  */
412 #define PROXY_OPTIONAL_HOOK(name,fn,pre,succ,order) \
413         APR_OPTIONAL_HOOK(proxy,name,fn,pre,succ,order)
414
415 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, scheme_handler, (request_rec *r, 
416                           proxy_worker *worker, proxy_server_conf *conf, char *url, 
417                           const char *proxyhost, apr_port_t proxyport))
418 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, canon_handler, (request_rec *r, 
419                           char *url))
420
421 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
422 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r)) 
423
424 /**
425  * pre request hook.
426  * It will return the most suitable worker at the moment
427  * and coresponding balancer.
428  * The url is rewritten from balancer://cluster/uri to scheme://host:port/uri
429  * and then the scheme_handler is called.
430  *
431  */
432 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
433                           proxy_balancer **balancer,
434                           request_rec *r,
435                           proxy_server_conf *conf, char **url))                          
436 /**
437  * post request hook.
438  * It is called after request for updating runtime balancer status.
439  */
440 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, post_request, (proxy_worker *worker,
441                           proxy_balancer *balancer, request_rec *r,
442                           proxy_server_conf *conf))
443
444 /**
445  * request status hook
446  * It is called after all proxy processing has been done.  This gives other
447  * modules a chance to create default content on failure, for example
448  */
449 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, request_status,
450                           (int *status, request_rec *r))
451
452 /* proxy_util.c */
453
454 PROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r);
455 PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
456 PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
457 PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
458                                        int forcedec, int proxyreq);
459 PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
460                                            char **passwordp, char **hostp, apr_port_t *port);
461 PROXY_DECLARE(const char *)ap_proxy_date_canon(apr_pool_t *p, const char *x);
462 PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val);
463 PROXY_DECLARE(char *)ap_proxy_removestr(apr_pool_t *pool, const char *list, const char *val);
464 PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x);
465 PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y);
466 PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
467 PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p);
468 PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p);
469 PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p);
470 PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
471 PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
472 PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
473 PROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb, char *buff, size_t bufflen, int *eos);
474 PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key);
475 /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
476 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 *);
477 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
478 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
479 PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c);
480 PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
481
482 /* Header mapping functions, and a typedef of their signature */
483 PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url);
484 PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str);
485
486 #if !defined(WIN32)
487 typedef const char *(*ap_proxy_header_reverse_map_fn)(request_rec *,
488                        proxy_dir_conf *, const char *);
489 #elif defined(PROXY_DECLARE_STATIC)
490 typedef const char *(__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
491                                  proxy_dir_conf *, const char *);
492 #elif defined(PROXY_DECLARE_EXPORT)
493 typedef __declspec(dllexport) const char *
494   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
495                proxy_dir_conf *, const char *);
496 #else
497 typedef __declspec(dllimport) const char *
498   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
499                proxy_dir_conf *, const char *);
500 #endif
501
502
503 /* Connection pool API */
504 /**
505  * Get the worker from proxy configuration
506  * @param p     memory pool used for finding worker
507  * @param conf  current proxy server configuration
508  * @param url   url to find the worker from
509  * @return      proxy_worker or NULL if not found
510  */
511 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
512                                                   proxy_server_conf *conf,
513                                                   const char *url);
514 /**
515  * Add the worker to proxy configuration
516  * @param worker the new worker
517  * @param p      memory pool to allocate worker from 
518  * @param conf   current proxy server configuration
519  * @param url    url containing worker name
520  * @return       error message or NULL if successfull
521  */
522 PROXY_DECLARE(const char *) ap_proxy_add_worker(proxy_worker **worker,
523                                                 apr_pool_t *p,
524                                                 proxy_server_conf *conf,
525                                                 const char *url);
526
527 /**
528  * Create new worker
529  * @param p      memory pool to allocate worker from 
530  * @return       new worker
531  */
532 PROXY_DECLARE(proxy_worker *) ap_proxy_create_worker(apr_pool_t *p);
533
534 /**
535  * Initize the worker's shared data
536  * @param conf   current proxy server configuration
537  * @param worker worker to initialize
538  * @param s      current server record
539  * @param worker worker to initialize
540  */
541 PROXY_DECLARE(void) ap_proxy_initialize_worker_share(proxy_server_conf *conf,
542                                                      proxy_worker *worker,
543                                                      server_rec *s);
544
545
546 /**
547  * Initize the worker
548  * @param worker worker to initialize
549  * @param s      current server record
550  * @return       APR_SUCCESS or error code
551  */
552 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
553                                                        server_rec *s);
554 /**
555  * Get the balancer from proxy configuration
556  * @param p     memory pool used for finding balancer
557  * @param conf  current proxy server configuration
558  * @param url   url to find the worker from. Has to have balancer:// prefix
559  * @return      proxy_balancer or NULL if not found
560  */
561 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
562                                                       proxy_server_conf *conf,
563                                                       const char *url);
564 /**
565  * Add the balancer to proxy configuration
566  * @param balancer the new balancer
567  * @param p      memory pool to allocate balancer from 
568  * @param conf   current proxy server configuration
569  * @param url    url containing balancer name
570  * @return       error message or NULL if successfull
571  */
572 PROXY_DECLARE(const char *) ap_proxy_add_balancer(proxy_balancer **balancer,
573                                                   apr_pool_t *p,
574                                                   proxy_server_conf *conf,
575                                                   const char *url);
576
577 /**
578  * Add the worker to the balancer
579  * @param pool     memory pool for adding worker 
580  * @param balancer balancer to add to
581  * @param balancer worker to add
582  * @note Single worker can be added to multiple balancers.
583  */
584 PROXY_DECLARE(void) ap_proxy_add_worker_to_balancer(apr_pool_t *pool,
585                                                     proxy_balancer *balancer,
586                                                     proxy_worker *worker);
587 /**
588  * Get the most suitable worker and(or) balancer for the request
589  * @param worker   worker used for processing request
590  * @param balancer balancer used for processing request
591  * @param r        current request
592  * @param conf     current proxy server configuration
593  * @param url      request url that balancer can rewrite.
594  * @return         OK or  HTTP_XXX error 
595  * @note It calls balancer pre_request hook if the url starts with balancer://
596  * The balancer then rewrites the url to particular worker, like http://host:port
597  */
598 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
599                                         proxy_balancer **balancer,
600                                         request_rec *r,
601                                         proxy_server_conf *conf,
602                                         char **url);
603 /**
604  * Post request worker and balancer cleanup
605  * @param worker   worker used for processing request
606  * @param balancer balancer used for processing request
607  * @param r        current request
608  * @param conf     current proxy server configuration
609  * @return         OK or  HTTP_XXX error
610  * @note When ever the pre_request is called, the post_request has to be
611  * called too. 
612  */
613 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
614                                          proxy_balancer *balancer,
615                                          request_rec *r,
616                                          proxy_server_conf *conf);
617
618 /**
619  * Request status function
620  * @param status   status of proxy request
621  * @return         OK or DECLINED
622  */
623  PROXY_DECLARE(int) ap_proxy_request_status(int *status, request_rec *r);
624
625 /**
626  * Deternime backend hostname and port
627  * @param p       memory pool used for processing
628  * @param r       current request
629  * @param conf    current proxy server configuration
630  * @param worker  worker used for processing request
631  * @param conn    proxy connection struct
632  * @param uri     processed uri
633  * @param url     request url
634  * @param proxyname are we connecting directly or via s proxy
635  * @param proxyport proxy host port
636  * @param server_portstr Via headers server port
637  * @param server_portstr_size size of the server_portstr buffer
638  * @return         OK or HTTP_XXX error
639  */                                         
640 PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
641                                                  proxy_server_conf *conf,
642                                                  proxy_worker *worker,
643                                                  proxy_conn_rec *conn,
644                                                  apr_uri_t *uri,
645                                                  char **url,
646                                                  const char *proxyname,
647                                                  apr_port_t proxyport,
648                                                  char *server_portstr,
649                                                  int server_portstr_size);
650 /**
651  * Mark a worker for retry
652  * @param proxy_function calling proxy scheme (http, ajp, ...)
653  * @param conf    current proxy server configuration
654  * @param worker  worker used for retrying
655  * @param s       current server record
656  * @return        OK if marked for retry, DECLINED otherwise
657  * @note Worker will be marker for retry if the time of the last retry
658  * has been ellapsed. In case there is no retry option set, defaults to
659  * number_of_retries seconds.
660  */                                         
661 PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
662                                          proxy_worker *worker,
663                                          server_rec *s);
664 /**
665  * Acquire a connection from workers connection pool
666  * @param proxy_function calling proxy scheme (http, ajp, ...)
667  * @param conn    acquired connection
668  * @param worker  worker used for obtaining connection
669  * @param s       current server record
670  * @return        OK or HTTP_XXX error
671  * @note If the number of connections is exhaused the function will
672  * block untill the timeout is reached.
673  */                                         
674 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
675                                                proxy_conn_rec **conn,
676                                                proxy_worker *worker,
677                                                server_rec *s);
678 /**
679  * Release a connection back to worker connection pool
680  * @param proxy_function calling proxy scheme (http, ajp, ...)
681  * @param conn    acquired connection
682  * @param s       current server record
683  * @return        OK or HTTP_XXX error
684  * @note The connection will be closed if conn->close_on_release is set
685  */                                         
686 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
687                                                proxy_conn_rec *conn,
688                                                server_rec *s);
689 /**
690  * Make a connection to the backend
691  * @param proxy_function calling proxy scheme (http, ajp, ...)
692  * @param conn    acquired connection
693  * @param worker  connection worker
694  * @param s       current server record
695  * @return        OK or HTTP_XXX error
696  * @note In case the socket already exists for conn, just check the link
697  * status.
698  */                                         
699 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
700                                             proxy_conn_rec *conn,
701                                             proxy_worker *worker,
702                                             server_rec *s);
703 /**
704  * Make a connection record for backend connection
705  * @param proxy_function calling proxy scheme (http, ajp, ...)
706  * @param conn    acquired connection
707  * @param c       client connection record
708  * @param s       current server record
709  * @return        OK or HTTP_XXX error
710  */                                         
711 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
712                                               proxy_conn_rec *conn,
713                                               conn_rec *c, server_rec *s);
714 /**
715  * Signal the upstream chain that the connection to the backend broke in the
716  * middle of the response. This is done by sending an error bucket with
717  * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
718  * @param r       current request record of client request
719  * @param brigade The brigade that is sent through the output filter chain
720  */
721 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
722                                            apr_bucket_brigade *brigade);
723
724 #define PROXY_LBMETHOD "proxylbmethod"
725
726 /* The number of dynamic workers that can be added when reconfiguring.
727  * If this limit is reached you must stop and restart the server.
728  */
729 #define PROXY_DYNAMIC_BALANCER_LIMIT    16
730 /**
731  * Calculate number of maximum number of workers in scoreboard.
732  * @return  number of workers to allocate in the scoreboard
733  */
734 int ap_proxy_lb_workers(void);
735
736 /* For proxy_util */
737 extern module PROXY_DECLARE_DATA proxy_module;
738
739 extern int PROXY_DECLARE_DATA proxy_lb_workers;
740
741 #endif /*MOD_PROXY_H*/
742 /** @} */