]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy.h
ddef166b12247b3de71bd3754f0d84e7b327fa33
[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  */
34
35 #include "apr_hooks.h"
36 #include "apr.h"
37 #include "apr_lib.h"
38 #include "apr_strings.h"
39 #include "apr_buckets.h"
40 #include "apr_md5.h"
41 #include "apr_network_io.h"
42 #include "apr_pools.h"
43 #include "apr_strings.h"
44 #include "apr_uri.h"
45 #include "apr_date.h"
46 #include "apr_strmatch.h"
47 #include "apr_fnmatch.h"
48 #include "apr_reslist.h"
49 #define APR_WANT_STRFUNC
50 #include "apr_want.h"
51
52 #include "httpd.h"
53 #include "http_config.h"
54 #include "ap_config.h"
55 #include "http_core.h"
56 #include "http_protocol.h"
57 #include "http_request.h"
58 #include "http_vhost.h"
59 #include "http_main.h"
60 #include "http_log.h"
61 #include "http_connection.h"
62 #include "util_filter.h"
63 #include "util_ebcdic.h"
64 #include "ap_provider.h"
65
66 #if APR_HAVE_NETINET_IN_H
67 #include <netinet/in.h>
68 #endif
69 #if APR_HAVE_ARPA_INET_H
70 #include <arpa/inet.h>
71 #endif
72
73 /* for proxy_canonenc() */
74 enum enctype {
75     enc_path, enc_search, enc_user, enc_fpath, enc_parm
76 };
77
78 #if APR_CHARSET_EBCDIC
79 #define CRLF   "\r\n"
80 #else /*APR_CHARSET_EBCDIC*/
81 #define CRLF   "\015\012"
82 #endif /*APR_CHARSET_EBCDIC*/
83
84 /* default Max-Forwards header setting */
85 /* Set this to -1, which complies with RFC2616 by not setting
86  * max-forwards if the client didn't send it to us.
87  */
88 #define DEFAULT_MAX_FORWARDS    -1
89
90 /* static information about a remote proxy */
91 struct proxy_remote {
92     const char *scheme;     /* the schemes handled by this proxy, or '*' */
93     const char *protocol;   /* the scheme used to talk to this proxy */
94     const char *hostname;   /* the hostname of this proxy */
95     ap_regex_t *regexp;     /* compiled regex (if any) for the remote */
96     int use_regex;          /* simple boolean. True if we have a regex pattern */
97     apr_port_t  port;       /* the port for this proxy */
98 };
99
100 #define PROXYPASS_NOCANON 0x01
101 #define PROXYPASS_INTERPOLATE 0x02
102 struct proxy_alias {
103     const char  *real;
104     const char  *fake;
105     ap_regex_t  *regex;
106     unsigned int flags;
107 };
108
109 struct dirconn_entry {
110     char *name;
111     struct in_addr addr, mask;
112     struct apr_sockaddr_t *hostaddr;
113     int (*matcher) (struct dirconn_entry * This, request_rec *r);
114 };
115
116 struct noproxy_entry {
117     const char *name;
118     struct apr_sockaddr_t *addr;
119 };
120
121 typedef struct proxy_balancer  proxy_balancer;
122 typedef struct proxy_worker    proxy_worker;
123 typedef struct proxy_conn_pool proxy_conn_pool;
124 typedef struct proxy_balancer_method proxy_balancer_method;
125
126 typedef struct {
127     apr_array_header_t *proxies;
128     apr_array_header_t *sec_proxy;
129     apr_array_header_t *aliases;
130     apr_array_header_t *noproxies;
131     apr_array_header_t *dirconn;
132     apr_array_header_t *workers;
133     apr_array_header_t *balancers;
134     proxy_worker       *forward;    /* forward proxy worker */
135     proxy_worker       *reverse;    /* reverse "module-driven" proxy worker */
136     const char *domain;     /* domain name to use in absence of a domain name in the request */
137     apr_pool_t *pool;       /* Pool used for allocating this struct */
138     int req;                /* true if proxy requests are enabled */
139     enum {
140       via_off,
141       via_on,
142       via_block,
143       via_full
144     } viaopt;                   /* how to deal with proxy Via: headers */
145     apr_size_t recv_buffer_size;
146     apr_size_t io_buffer_size;
147     long maxfwd;
148     apr_interval_time_t timeout;
149     enum {
150       bad_error,
151       bad_ignore,
152       bad_body
153     } badopt;                   /* how to deal with bad headers */
154     enum {
155         status_off,
156         status_on,
157         status_full
158     } proxy_status;             /* Status display options */
159
160     int req_set:1;
161     int viaopt_set:1;
162     int recv_buffer_size_set:1;
163     int io_buffer_size_set:1;
164     int maxfwd_set:1;
165     int timeout_set:1;
166     int badopt_set:1;
167     int proxy_status_set:1;
168 } proxy_server_conf;
169
170
171 typedef struct {
172     const char *p;            /* The path */
173     ap_regex_t  *r;            /* Is this a regex? */
174
175 /* ProxyPassReverse and friends are documented as working inside
176  * <Location>.  But in fact they never have done in the case of
177  * more than one <Location>, because the server_conf can't see it.
178  * We need to move them to the per-dir config.
179  * Discussed in February:
180  * http://marc.theaimsgroup.com/?l=apache-httpd-dev&m=110726027118798&w=2
181  */
182     apr_array_header_t *raliases;
183     apr_array_header_t* cookie_paths;
184     apr_array_header_t* cookie_domains;
185     const apr_strmatch_pattern* cookie_path_str;
186     const apr_strmatch_pattern* cookie_domain_str;
187     signed char p_is_fnmatch; /* Is the path an fnmatch candidate? */
188     signed char interpolate_env;
189     struct proxy_alias *alias;
190
191     /**
192      * the following setting masks the error page
193      * returned from the 'proxied server' and just
194      * forwards the status code upwards.
195      * This allows the main server (us) to generate
196      * the error page, (so it will look like a error
197      * returned from the rest of the system
198      */
199     int error_override:1;
200     int preserve_host:1;
201     int preserve_host_set:1;
202     int error_override_set:1;
203     int alias_set:1;
204 } proxy_dir_conf;
205
206 /* if we interpolate env vars per-request, we'll need a per-request
207  * copy of the reverse proxy config
208  */
209 typedef struct {
210     apr_array_header_t *raliases;
211     apr_array_header_t* cookie_paths;
212     apr_array_header_t* cookie_domains;
213 } proxy_req_conf;
214
215 typedef struct {
216     conn_rec     *connection;
217     request_rec  *r;           /* Request record of the frontend request
218                                 * which the backend currently answers. */
219     proxy_worker *worker;      /* Connection pool this connection belongs to */
220     apr_pool_t   *pool;        /* Subpool for hostname and addr data */
221     const char   *hostname;
222     apr_sockaddr_t *addr;      /* Preparsed remote address info */
223     apr_pool_t   *scpool;      /* Subpool used for socket and connection data */
224     apr_socket_t *sock;        /* Connection socket */
225     void         *data;        /* per scheme connection data */
226     void         *forward;     /* opaque forward proxy data */
227     apr_uint32_t flags;        /* Connection flags */
228     apr_port_t   port;
229     int          is_ssl:1;
230     int          close:1;      /* Close 'this' connection */
231     int          need_flush:1; /* Flag to decide whether we need to flush the
232                                 * filter chain or not */
233 #if APR_HAS_THREADS
234     int          inreslist:1;  /* connection in apr_reslist? */
235 #endif
236     int          cleaned:1;    /* connection cleaned? */
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_DRAIN          0x0004
258 #define PROXY_WORKER_IN_SHUTDOWN    0x0010
259 #define PROXY_WORKER_DISABLED       0x0020
260 #define PROXY_WORKER_STOPPED        0x0040
261 #define PROXY_WORKER_IN_ERROR       0x0080
262 #define PROXY_WORKER_HOT_STANDBY    0x0100
263 #define PROXY_WORKER_FREE           0x0200
264
265 #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
266 PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
267
268 /* NOTE: these check the shared status */
269 #define PROXY_WORKER_IS_INITIALIZED(f)   ( (f)->s && \
270   ( (f)->s->status &  PROXY_WORKER_INITIALIZED ) )
271
272 #define PROXY_WORKER_IS_STANDBY(f)   ( (f)->s && \
273   ( (f)->s->status &  PROXY_WORKER_HOT_STANDBY ) )
274
275 #define PROXY_WORKER_IS_USABLE(f)   ( (f)->s && \
276   ( !( (f)->s->status & PROXY_WORKER_NOT_USABLE_BITMAP) ) && \
277   PROXY_WORKER_IS_INITIALIZED(f) )
278
279 /* default worker retry timeout in seconds */
280 #define PROXY_WORKER_DEFAULT_RETRY  60
281 #define PROXY_WORKER_MAX_ROUTE_SIZ  63
282
283 /* Runtime worker status informations. Shared in scoreboard */
284 typedef struct {
285     apr_time_t      error_time; /* time of the last error */
286     int             status;
287     int             retries;    /* number of retries on this worker */
288     int             lbstatus;   /* Current lbstatus */
289     int             lbfactor;   /* dynamic lbfactor */
290     apr_off_t       transferred;/* Number of bytes transferred to remote */
291     apr_off_t       read;       /* Number of bytes read from remote */
292     apr_size_t      elected;    /* Number of times the worker was elected */
293     char            route[PROXY_WORKER_MAX_ROUTE_SIZ+1];
294     char            redirect[PROXY_WORKER_MAX_ROUTE_SIZ+1];
295     void            *context;   /* general purpose storage */
296     apr_size_t      busy;       /* busyness factor */
297     int             lbset;      /* load balancer cluster set */
298     unsigned int    apr_hash;      /* hash #0 of worker name */
299     unsigned int    our_hash;      /* hash #1 of worker name. Why 2? hash collisions. */
300 } proxy_worker_stat;
301
302 /* Worker configuration */
303 struct proxy_worker {
304     const char      *name;
305     const char      *scheme;    /* scheme to use ajp|http|https */
306     const char      *hostname;  /* remote backend address */
307     const char      *route;     /* balancing route */
308     const char      *redirect;  /* temporary balancing redirection route */
309     int             id;         /* scoreboard id */
310     int             status;     /* temporary worker status */
311     int             lbfactor;   /* initial load balancing factor */
312     int             lbset;      /* load balancer cluster set */
313     int             min;        /* Desired minimum number of available connections */
314     int             smax;       /* Soft maximum on the total number of connections */
315     int             hmax;       /* Hard maximum on the total number of connections */
316     apr_interval_time_t ttl;    /* maximum amount of time in seconds a connection
317                                  * may be available while exceeding the soft limit */
318     apr_interval_time_t retry;  /* retry interval */
319     apr_interval_time_t timeout; /* connection timeout */
320     apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
321     apr_interval_time_t ping_timeout;
322     apr_interval_time_t conn_timeout;
323     apr_size_t      recv_buffer_size;
324     apr_size_t      io_buffer_size;
325     apr_port_t      port;
326     char            keepalive;
327     char            disablereuse;
328     int             is_address_reusable:1;
329     proxy_conn_pool     *cp;        /* Connection pool to use */
330     proxy_worker_stat   *s;         /* Shared data */
331     void            *opaque;    /* per scheme worker data */
332     void            *context;   /* general purpose storage */
333     enum {
334          flush_off,
335          flush_on,
336          flush_auto
337     } flush_packets;           /* control AJP flushing */
338     int             flush_wait;  /* poll wait time in microseconds if flush_auto */
339     const char      *flusher;  /* flush provider used by mod_proxy_fdpass */
340 #if APR_HAS_THREADS
341     apr_thread_mutex_t  *mutex;  /* Thread lock for updating address cache */
342 #endif
343
344     int             retry_set:1;
345     int             timeout_set:1;
346     int             acquire_set:1;
347     int             ping_timeout_set:1;
348     int             conn_timeout_set:1;
349     int             recv_buffer_size_set:1;
350     int             io_buffer_size_set:1;
351     int             keepalive_set:1;
352     int             disablereuse_set:1;
353     unsigned int    apr_hash;      /* hash #0 of worker name */
354     unsigned int    our_hash;      /* hash #1 of worker name. Why 2? hash collisions. */
355 };
356
357 /*
358  * Wait 10000 microseconds to find out if more data is currently
359  * available at the backend. Just an arbitrary choose.
360  */
361 #define PROXY_FLUSH_WAIT 10000
362
363 struct proxy_balancer {
364     apr_array_header_t *workers; /* array of proxy_workers */
365     const char *name;            /* name of the load balancer */
366     apr_interval_time_t timeout; /* Timeout for waiting on free connection */
367     proxy_balancer_method *lbmethod;
368
369     const char      *sticky_path;     /* URL sticky session identifier */
370     apr_array_header_t *errstatuses;  /* statuses to force members into error */
371     const char      *sticky;          /* sticky session identifier */
372     int             max_attempts;     /* Number of attempts before failing */
373
374     int             sticky_force:1;   /* Disable failover for sticky sessions */
375     int             scolonsep:1;      /* true if ';' seps sticky session paths */
376     int             max_attempts_set:1;
377
378     /* XXX: Perhaps we will need the proc mutex too.
379      * Altrough we are only using arithmetic operations
380      * it may lead to a incorrect calculations.
381      * For now use only the thread mutex.
382      */
383 #if APR_HAS_THREADS
384     apr_thread_mutex_t  *mutex;  /* Thread lock for updating lb params */
385 #endif
386     void            *context;         /* general purpose storage */
387     apr_time_t      updated;   /* timestamp of last update */
388 };
389
390 struct proxy_balancer_method {
391     const char *name;            /* name of the load balancer method*/
392     proxy_worker *(*finder)(proxy_balancer *balancer,
393                             request_rec *r);
394     void            *context;   /* general purpose storage */
395     apr_status_t (*reset)(proxy_balancer *balancer, server_rec *s);
396     apr_status_t (*age)(proxy_balancer *balancer, server_rec *s);
397     apr_status_t (*updatelbstatus)(proxy_balancer *balancer, proxy_worker *elected, server_rec *s); 
398 };
399
400 #if APR_HAS_THREADS
401 #define PROXY_THREAD_LOCK(x)      apr_thread_mutex_lock((x)->mutex)
402 #define PROXY_THREAD_UNLOCK(x)    apr_thread_mutex_unlock((x)->mutex)
403 #else
404 #define PROXY_THREAD_LOCK(x)      APR_SUCCESS
405 #define PROXY_THREAD_UNLOCK(x)    APR_SUCCESS
406 #endif
407
408 /* hooks */
409
410 /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and 
411  * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
412  */
413 #if !defined(WIN32)
414 #define PROXY_DECLARE(type)            type
415 #define PROXY_DECLARE_NONSTD(type)     type
416 #define PROXY_DECLARE_DATA
417 #elif defined(PROXY_DECLARE_STATIC)
418 #define PROXY_DECLARE(type)            type __stdcall
419 #define PROXY_DECLARE_NONSTD(type)     type
420 #define PROXY_DECLARE_DATA
421 #elif defined(PROXY_DECLARE_EXPORT)
422 #define PROXY_DECLARE(type)            __declspec(dllexport) type __stdcall
423 #define PROXY_DECLARE_NONSTD(type)     __declspec(dllexport) type
424 #define PROXY_DECLARE_DATA             __declspec(dllexport)
425 #else
426 #define PROXY_DECLARE(type)            __declspec(dllimport) type __stdcall
427 #define PROXY_DECLARE_NONSTD(type)     __declspec(dllimport) type
428 #define PROXY_DECLARE_DATA             __declspec(dllimport)
429 #endif
430
431 /**
432  * Hook an optional proxy hook.  Unlike static hooks, this uses a macro
433  * instead of a function.
434  */
435 #define PROXY_OPTIONAL_HOOK(name,fn,pre,succ,order) \
436         APR_OPTIONAL_HOOK(proxy,name,fn,pre,succ,order)
437
438 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, scheme_handler, (request_rec *r, 
439                           proxy_worker *worker, proxy_server_conf *conf, char *url, 
440                           const char *proxyhost, apr_port_t proxyport))
441 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, canon_handler, (request_rec *r, 
442                           char *url))
443
444 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
445 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r)) 
446
447 /**
448  * pre request hook.
449  * It will return the most suitable worker at the moment
450  * and coresponding balancer.
451  * The url is rewritten from balancer://cluster/uri to scheme://host:port/uri
452  * and then the scheme_handler is called.
453  *
454  */
455 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
456                           proxy_balancer **balancer,
457                           request_rec *r,
458                           proxy_server_conf *conf, char **url))                          
459 /**
460  * post request hook.
461  * It is called after request for updating runtime balancer status.
462  */
463 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, post_request, (proxy_worker *worker,
464                           proxy_balancer *balancer, request_rec *r,
465                           proxy_server_conf *conf))
466
467 /**
468  * request status hook
469  * It is called after all proxy processing has been done.  This gives other
470  * modules a chance to create default content on failure, for example
471  */
472 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, request_status,
473                           (int *status, request_rec *r))
474
475 /* proxy_util.c */
476
477 PROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r);
478 PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
479 PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
480 PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
481                                        int forcedec, int proxyreq);
482 PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
483                                            char **passwordp, char **hostp, apr_port_t *port);
484 PROXY_DECLARE(const char *)ap_proxy_date_canon(apr_pool_t *p, const char *x);
485 PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val);
486 PROXY_DECLARE(char *)ap_proxy_removestr(apr_pool_t *pool, const char *list, const char *val);
487 PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x);
488 PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y);
489 PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
490 PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p);
491 PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p);
492 PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p);
493 PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
494 PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
495 PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
496 PROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb, char *buff, size_t bufflen, int *eos);
497 PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key);
498 /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
499 PROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **, const char *, apr_sockaddr_t *, const char *, proxy_server_conf *, request_rec *);
500 PROXY_DECLARE(apr_status_t) ap_proxy_ssl_connection_cleanup(proxy_conn_rec *conn,
501                                                             request_rec *r);
502 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
503 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
504 PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c);
505 PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
506
507 /* Header mapping functions, and a typedef of their signature */
508 PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url);
509 PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str);
510
511 #if !defined(WIN32)
512 typedef const char *(*ap_proxy_header_reverse_map_fn)(request_rec *,
513                        proxy_dir_conf *, const char *);
514 #elif defined(PROXY_DECLARE_STATIC)
515 typedef const char *(__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
516                                  proxy_dir_conf *, const char *);
517 #elif defined(PROXY_DECLARE_EXPORT)
518 typedef __declspec(dllexport) const char *
519   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
520                proxy_dir_conf *, const char *);
521 #else
522 typedef __declspec(dllimport) const char *
523   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
524                proxy_dir_conf *, const char *);
525 #endif
526
527
528 /* Connection pool API */
529 /**
530  * Get the worker from proxy configuration
531  * @param p     memory pool used for finding worker
532  * @param conf  current proxy server configuration
533  * @param url   url to find the worker from
534  * @return      proxy_worker or NULL if not found
535  */
536 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
537                                                   proxy_server_conf *conf,
538                                                   const char *url);
539 /**
540  * Add the worker to proxy configuration
541  * @param worker the new worker
542  * @param p      memory pool to allocate worker from 
543  * @param conf   current proxy server configuration
544  * @param url    url containing worker name
545  * @param id     slotnumber id or -1 for auto allocation
546  * @return       error message or NULL if successful
547  */
548 PROXY_DECLARE(const char *) ap_proxy_add_worker_wid(proxy_worker **worker,
549                                                 apr_pool_t *p,
550                                                 proxy_server_conf *conf,
551                                                 const char *url,
552                                                 int id);
553
554 /**
555  * Add the worker to proxy configuration
556  * @param worker the new worker
557  * @param p      memory pool to allocate worker from 
558  * @param conf   current proxy server configuration
559  * @param url    url containing worker name
560  * @return       error message or NULL if successful
561  */
562 PROXY_DECLARE(const char *) ap_proxy_add_worker(proxy_worker **worker,
563                                                 apr_pool_t *p,
564                                                 proxy_server_conf *conf,
565                                                 const char *url);
566
567 /**
568  * Create new worker
569  * @param p      memory pool to allocate worker from 
570  * @param id     slotnumber id or -1 for auto allocation
571  * @return       new worker
572  */
573 PROXY_DECLARE(proxy_worker *) ap_proxy_create_worker_wid(apr_pool_t *p, int id);
574
575 /**
576  * Create new worker
577  * @param p      memory pool to allocate worker from 
578  * @return       new worker
579  */
580 PROXY_DECLARE(proxy_worker *) ap_proxy_create_worker(apr_pool_t *p);
581
582 /**
583  * Initize the worker's shared data
584  * @param conf   current proxy server configuration
585  * @param worker worker to initialize
586  * @param s      current server record
587  * @param worker worker to initialize
588  */
589 PROXY_DECLARE(void) ap_proxy_initialize_worker_share(proxy_server_conf *conf,
590                                                      proxy_worker *worker,
591                                                      server_rec *s);
592
593
594 /**
595  * Initize the worker
596  * @param worker worker to initialize
597  * @param s      current server record
598  * @param p      memory pool used for mutex and Connection pool.
599  * @return       APR_SUCCESS or error code
600  */
601 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
602                                                        server_rec *s,
603                                                        apr_pool_t *p);
604 /**
605  * Get the balancer from proxy configuration
606  * @param p     memory pool used for finding balancer
607  * @param conf  current proxy server configuration
608  * @param url   url to find the worker from. Has to have balancer:// prefix
609  * @return      proxy_balancer or NULL if not found
610  */
611 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
612                                                       proxy_server_conf *conf,
613                                                       const char *url);
614 /**
615  * Add the balancer to proxy configuration
616  * @param balancer the new balancer
617  * @param p      memory pool to allocate balancer from 
618  * @param conf   current proxy server configuration
619  * @param url    url containing balancer name
620  * @return       error message or NULL if successfull
621  */
622 PROXY_DECLARE(const char *) ap_proxy_add_balancer(proxy_balancer **balancer,
623                                                   apr_pool_t *p,
624                                                   proxy_server_conf *conf,
625                                                   const char *url);
626
627 /**
628  * Add the worker to the balancer
629  * @param pool     memory pool for adding worker 
630  * @param balancer balancer to add to
631  * @param worker worker to add
632  * @param id     slotnumber id or -1 for auto allocation
633  * @note Single worker can be added to multiple balancers.
634  */
635 PROXY_DECLARE(void) ap_proxy_add_worker_to_balancer_wid(apr_pool_t *pool,
636                                                     proxy_balancer *balancer,
637                                                     proxy_worker *worker,
638                                                     int id);
639 /**
640  * Add the worker to the balancer
641  * @param pool     memory pool for adding worker 
642  * @param balancer balancer to add to
643  * @param worker worker to add
644  * @note Single worker can be added to multiple balancers.
645  */
646 PROXY_DECLARE(void) ap_proxy_add_worker_to_balancer(apr_pool_t *pool,
647                                                     proxy_balancer *balancer,
648                                                     proxy_worker *worker);
649 /**
650  * Get the most suitable worker and(or) balancer for the request
651  * @param worker   worker used for processing request
652  * @param balancer balancer used for processing request
653  * @param r        current request
654  * @param conf     current proxy server configuration
655  * @param url      request url that balancer can rewrite.
656  * @return         OK or  HTTP_XXX error 
657  * @note It calls balancer pre_request hook if the url starts with balancer://
658  * The balancer then rewrites the url to particular worker, like http://host:port
659  */
660 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
661                                         proxy_balancer **balancer,
662                                         request_rec *r,
663                                         proxy_server_conf *conf,
664                                         char **url);
665 /**
666  * Post request worker and balancer cleanup
667  * @param worker   worker used for processing request
668  * @param balancer balancer used for processing request
669  * @param r        current request
670  * @param conf     current proxy server configuration
671  * @return         OK or  HTTP_XXX error
672  * @note When ever the pre_request is called, the post_request has to be
673  * called too. 
674  */
675 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
676                                          proxy_balancer *balancer,
677                                          request_rec *r,
678                                          proxy_server_conf *conf);
679
680 /**
681  * Request status function
682  * @param status   status of proxy request (result)
683  * @param r        the request to obtain the status for
684  * @return         OK or DECLINED
685  */
686  PROXY_DECLARE(int) ap_proxy_request_status(int *status, request_rec *r);
687
688 /**
689  * Deternime backend hostname and port
690  * @param p       memory pool used for processing
691  * @param r       current request
692  * @param conf    current proxy server configuration
693  * @param worker  worker used for processing request
694  * @param conn    proxy connection struct
695  * @param uri     processed uri
696  * @param url     request url
697  * @param proxyname are we connecting directly or via s proxy
698  * @param proxyport proxy host port
699  * @param server_portstr Via headers server port
700  * @param server_portstr_size size of the server_portstr buffer
701  * @return         OK or HTTP_XXX error
702  */                                         
703 PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
704                                                  proxy_server_conf *conf,
705                                                  proxy_worker *worker,
706                                                  proxy_conn_rec *conn,
707                                                  apr_uri_t *uri,
708                                                  char **url,
709                                                  const char *proxyname,
710                                                  apr_port_t proxyport,
711                                                  char *server_portstr,
712                                                  int server_portstr_size);
713
714 /**
715  * Mark a worker for retry
716  * @param proxy_function calling proxy scheme (http, ajp, ...)
717  * @param worker  worker used for retrying
718  * @param s       current server record
719  * @return        OK if marked for retry, DECLINED otherwise
720  * @note Worker will be marker for retry if the time of the last retry
721  * has been ellapsed. In case there is no retry option set, defaults to
722  * number_of_retries seconds.
723  */                                         
724 PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
725                                          proxy_worker *worker,
726                                          server_rec *s);
727 /**
728  * Acquire a connection from workers connection pool
729  * @param proxy_function calling proxy scheme (http, ajp, ...)
730  * @param conn    acquired connection
731  * @param worker  worker used for obtaining connection
732  * @param s       current server record
733  * @return        OK or HTTP_XXX error
734  * @note If the number of connections is exhaused the function will
735  * block untill the timeout is reached.
736  */                                         
737 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
738                                                proxy_conn_rec **conn,
739                                                proxy_worker *worker,
740                                                server_rec *s);
741 /**
742  * Release a connection back to worker connection pool
743  * @param proxy_function calling proxy scheme (http, ajp, ...)
744  * @param conn    acquired connection
745  * @param s       current server record
746  * @return        OK or HTTP_XXX error
747  * @note The connection will be closed if conn->close_on_release is set
748  */                                         
749 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
750                                                proxy_conn_rec *conn,
751                                                server_rec *s);
752 /**
753  * Make a connection to the backend
754  * @param proxy_function calling proxy scheme (http, ajp, ...)
755  * @param conn    acquired connection
756  * @param worker  connection worker
757  * @param s       current server record
758  * @return        OK or HTTP_XXX error
759  * @note In case the socket already exists for conn, just check the link
760  * status.
761  */                                         
762 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
763                                             proxy_conn_rec *conn,
764                                             proxy_worker *worker,
765                                             server_rec *s);
766 /**
767  * Make a connection record for backend connection
768  * @param proxy_function calling proxy scheme (http, ajp, ...)
769  * @param conn    acquired connection
770  * @param c       client connection record
771  * @param s       current server record
772  * @return        OK or HTTP_XXX error
773  */                                         
774 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
775                                               proxy_conn_rec *conn,
776                                               conn_rec *c, server_rec *s);
777 /**
778  * Signal the upstream chain that the connection to the backend broke in the
779  * middle of the response. This is done by sending an error bucket with
780  * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
781  * @param r       current request record of client request
782  * @param brigade The brigade that is sent through the output filter chain
783  */
784 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
785                                            apr_bucket_brigade *brigade);
786
787 /**
788  * Transform buckets from one bucket allocator to another one by creating a
789  * transient bucket for each data bucket and let it use the data read from
790  * the old bucket. Metabuckets are transformed by just recreating them.
791  * Attention: Currently only the following bucket types are handled:
792  *
793  * All data buckets
794  * FLUSH
795  * EOS
796  *
797  * If an other bucket type is found its type is logged as a debug message
798  * and APR_EGENERAL is returned.
799  * @param r    current request record of client request. Only used for logging
800  *             purposes
801  * @param from the brigade that contains the buckets to transform
802  * @param to   the brigade that will receive the transformed buckets
803  * @return     APR_SUCCESS if all buckets could be transformed APR_EGENERAL
804  *             otherwise
805  */
806 PROXY_DECLARE(apr_status_t)
807 ap_proxy_buckets_lifetime_transform(request_rec *r, apr_bucket_brigade *from,
808                                         apr_bucket_brigade *to);
809 /**
810  * Return a hash based on the passed string
811  * @param str     string to produce hash from
812  * @param method  hashing method to use
813  * @return        hash as unsigned int
814  */
815
816 typedef enum { PROXY_HASHFUNC_DEFAULT, PROXY_HASHFUNC_APR,  PROXY_HASHFUNC_FNV } proxy_hash_t;
817
818 PROXY_DECLARE(unsigned int)
819 ap_proxy_hashfunc(const char *str, proxy_hash_t method);
820
821 #define PROXY_LBMETHOD "proxylbmethod"
822
823 /* The number of dynamic workers that can be added when reconfiguring.
824  * If this limit is reached you must stop and restart the server.
825  */
826 #define PROXY_DYNAMIC_BALANCER_LIMIT    16
827 /**
828  * Calculate number of maximum number of workers in scoreboard.
829  * @return  number of workers to allocate in the scoreboard
830  */
831 int ap_proxy_lb_workers(void);
832
833 /* For proxy_util */
834 extern module PROXY_DECLARE_DATA proxy_module;
835
836 extern int PROXY_DECLARE_DATA proxy_lb_workers;
837
838 #endif /*MOD_PROXY_H*/
839 /** @} */