]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy.h
Adjust for conf->workers being *proxy_worker and balancer->workers being **proxy_worker
[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 #include "apr_hooks.h"
30 #include "apr.h"
31 #include "apr_lib.h"
32 #include "apr_strings.h"
33 #include "apr_buckets.h"
34 #include "apr_md5.h"
35 #include "apr_network_io.h"
36 #include "apr_pools.h"
37 #include "apr_strings.h"
38 #include "apr_uri.h"
39 #include "apr_date.h"
40 #include "apr_strmatch.h"
41 #include "apr_fnmatch.h"
42 #include "apr_reslist.h"
43 #define APR_WANT_STRFUNC
44 #include "apr_want.h"
45 #include "util_mutex.h"
46 #include "apr_global_mutex.h"
47 #include "apr_thread_mutex.h"
48
49 #include "httpd.h"
50 #include "http_config.h"
51 #include "ap_config.h"
52 #include "http_core.h"
53 #include "http_protocol.h"
54 #include "http_request.h"
55 #include "http_vhost.h"
56 #include "http_main.h"
57 #include "http_log.h"
58 #include "http_connection.h"
59 #include "util_filter.h"
60 #include "util_ebcdic.h"
61 #include "ap_provider.h"
62 #include "ap_slotmem.h"
63
64 #if APR_HAVE_NETINET_IN_H
65 #include <netinet/in.h>
66 #endif
67 #if APR_HAVE_ARPA_INET_H
68 #include <arpa/inet.h>
69 #endif
70
71 /* for proxy_canonenc() */
72 enum enctype {
73     enc_path, enc_search, enc_user, enc_fpath, enc_parm
74 };
75
76 #define BALANCER_PREFIX "balancer://"
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;    /* non-balancer workers, eg ProxyPass http://example.com */
133     apr_array_header_t *balancers;  /* list of balancers @ config time */
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     apr_sockaddr_t *source_address;
160     apr_global_mutex_t  *mutex; /* global lock for updating lb params */
161     
162     int req_set:1;
163     int viaopt_set:1;
164     int recv_buffer_size_set:1;
165     int io_buffer_size_set:1;
166     int maxfwd_set:1;
167     int timeout_set:1;
168     int badopt_set:1;
169     int proxy_status_set:1;
170     int source_address_set:1;
171 } proxy_server_conf;
172
173
174 typedef struct {
175     const char *p;            /* The path */
176     ap_regex_t  *r;            /* Is this a regex? */
177
178 /* FIXME
179  * ProxyPassReverse and friends are documented as working inside
180  * <Location>.  But in fact they never have done in the case of
181  * more than one <Location>, because the server_conf can't see it.
182  * We need to move them to the per-dir config.
183  * Discussed in February 2005:
184  * http://marc.theaimsgroup.com/?l=apache-httpd-dev&m=110726027118798&w=2
185  */
186     apr_array_header_t *raliases;
187     apr_array_header_t* cookie_paths;
188     apr_array_header_t* cookie_domains;
189     const apr_strmatch_pattern* cookie_path_str;
190     const apr_strmatch_pattern* cookie_domain_str;
191     signed char p_is_fnmatch; /* Is the path an fnmatch candidate? */
192     signed char interpolate_env;
193     struct proxy_alias *alias;
194
195     /**
196      * the following setting masks the error page
197      * returned from the 'proxied server' and just
198      * forwards the status code upwards.
199      * This allows the main server (us) to generate
200      * the error page, (so it will look like a error
201      * returned from the rest of the system
202      */
203     int error_override:1;
204     int preserve_host:1;
205     int preserve_host_set:1;
206     int error_override_set:1;
207     int alias_set:1;
208 } proxy_dir_conf;
209
210 /* if we interpolate env vars per-request, we'll need a per-request
211  * copy of the reverse proxy config
212  */
213 typedef struct {
214     apr_array_header_t *raliases;
215     apr_array_header_t* cookie_paths;
216     apr_array_header_t* cookie_domains;
217 } proxy_req_conf;
218
219 typedef struct {
220     conn_rec     *connection;
221     request_rec  *r;           /* Request record of the backend request
222                                 * that is used over the backend connection. */
223     proxy_worker *worker;      /* Connection pool this connection belongs to */
224     apr_pool_t   *pool;        /* Subpool for hostname and addr data */
225     const char   *hostname;
226     apr_sockaddr_t *addr;      /* Preparsed remote address info */
227     apr_pool_t   *scpool;      /* Subpool used for socket and connection data */
228     apr_socket_t *sock;        /* Connection socket */
229     void         *data;        /* per scheme connection data */
230     void         *forward;     /* opaque forward proxy data */
231     apr_uint32_t flags;        /* Connection flags */
232     apr_port_t   port;
233     int          is_ssl:1;
234     int          close:1;      /* Close 'this' connection */
235     int          need_flush:1; /* Flag to decide whether we need to flush the
236                                 * filter chain or not */
237     int          inreslist:1;  /* connection in apr_reslist? */
238 } proxy_conn_rec;
239
240 typedef struct {
241         float cache_completion; /* completion percentage */
242         int content_length; /* length of the content */
243 } proxy_completion;
244
245 /* Connection pool */
246 struct proxy_conn_pool {
247     apr_pool_t     *pool;   /* The pool used in constructor and destructor calls */
248     apr_sockaddr_t *addr;   /* Preparsed remote address info */
249     apr_reslist_t  *res;    /* Connection resource list */
250     proxy_conn_rec *conn;   /* Single connection for prefork mpm */
251 };
252
253 /* worker status flags */
254 #define PROXY_WORKER_INITIALIZED    0x0001
255 #define PROXY_WORKER_IGNORE_ERRORS  0x0002
256 #define PROXY_WORKER_DRAIN          0x0004
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 #define PROXY_WORKER_FREE           0x0200
263
264 #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
265 PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
266
267 /* NOTE: these check the shared status */
268 #define PROXY_WORKER_IS_INITIALIZED(f)  ( (f)->s->status &  PROXY_WORKER_INITIALIZED )
269
270 #define PROXY_WORKER_IS_STANDBY(f)   ( (f)->s->status &  PROXY_WORKER_HOT_STANDBY )
271
272 #define PROXY_WORKER_IS_USABLE(f)   ( ( !( (f)->s->status & PROXY_WORKER_NOT_USABLE_BITMAP) ) && \
273   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_SCHEME_SIZE  16
278 #define PROXY_WORKER_MAX_ROUTE_SIZE   64
279 #define PROXY_WORKER_MAX_NAME_SIZE    96
280
281 #define PROXY_STRNCPY(dst, src) apr_cpystrn((dst), (src), sizeof(dst))
282
283 /* Runtime worker status informations. Shared in scoreboard */
284 typedef struct {
285     char      name[PROXY_WORKER_MAX_NAME_SIZE];
286     char      scheme[PROXY_WORKER_MAX_SCHEME_SIZE];   /* scheme to use ajp|http|https */
287     char      hostname[PROXY_WORKER_MAX_ROUTE_SIZE];  /* remote backend address */
288     char      route[PROXY_WORKER_MAX_ROUTE_SIZE];     /* balancing route */
289     char      redirect[PROXY_WORKER_MAX_ROUTE_SIZE];  /* temporary balancing redirection route */
290     char      flusher[PROXY_WORKER_MAX_SCHEME_SIZE];  /* flush provider used by mod_proxy_fdpass */
291     int             lbset;      /* load balancer cluster set */
292     int             status;
293     int             retries;    /* number of retries on this worker */
294     int             lbstatus;   /* Current lbstatus */
295     int             lbfactor;   /* dynamic lbfactor */
296     int             min;        /* Desired minimum number of available connections */
297     int             smax;       /* Soft maximum on the total number of connections */
298     int             hmax;       /* Hard maximum on the total number of connections */
299     int             flush_wait; /* poll wait time in microseconds if flush_auto */
300     int             index;      /* shm array index */
301     unsigned int    hash;       /* hash of worker name */
302     enum {
303         flush_off,
304         flush_on,
305         flush_auto
306     } flush_packets;           /* control AJP flushing */
307     apr_time_t      error_time; /* time of the last error */
308     apr_interval_time_t ttl;    /* maximum amount of time in seconds a connection
309                                  * may be available while exceeding the soft limit */
310     apr_interval_time_t retry;   /* retry interval */
311     apr_interval_time_t timeout; /* connection timeout */
312     apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
313     apr_interval_time_t ping_timeout;
314     apr_interval_time_t conn_timeout;
315     apr_size_t      recv_buffer_size;
316     apr_size_t      io_buffer_size;
317     apr_size_t      elected;    /* Number of times the worker was elected */
318     apr_size_t      busy;       /* busyness factor */
319     apr_port_t      port;
320     apr_off_t       transferred;/* Number of bytes transferred to remote */
321     apr_off_t       read;       /* Number of bytes read from remote */
322     void            *context;   /* general purpose storage */
323     unsigned int     keepalive:1;
324     unsigned int     disablereuse:1;
325     unsigned int     is_address_reusable:1;
326     unsigned int     retry_set:1;
327     unsigned int     timeout_set:1;
328     unsigned int     acquire_set:1;
329     unsigned int     ping_timeout_set:1;
330     unsigned int     conn_timeout_set:1;
331     unsigned int     recv_buffer_size_set:1;
332     unsigned int     io_buffer_size_set:1;
333     unsigned int     keepalive_set:1;
334     unsigned int     disablereuse_set:1;
335 } proxy_worker_shared;
336
337 /* Worker configuration */
338 struct proxy_worker {
339     int             index;      /* shm array index */
340     unsigned int    hash;       /* hash of worker name */
341     proxy_conn_pool     *cp;    /* Connection pool to use */
342     proxy_worker_shared   *s;   /* Shared data */
343     proxy_balancer  *balancer;  /* which balancer am I in? */
344     apr_thread_mutex_t  *mutex; /* Thread lock for updating address cache */
345     void            *context;   /* general purpose storage */
346 };
347
348 /*
349  * Time to wait (in microseconds) to find out if more data is currently
350  * available at the backend.
351  */
352 #define PROXY_FLUSH_WAIT 10000
353
354 struct proxy_balancer {
355     apr_array_header_t *workers;  /* initially configured workers */
356     ap_slotmem_instance_t *slot;  /* worker shm data - runtime */
357     int growth;                   /* number of post-config workers can added */
358     int max_workers;              /* maximum number of allowed workers */
359     const char *name;             /* name of the load balancer */
360     apr_interval_time_t timeout;  /* Timeout for waiting on free connection */
361     const char *lbprovider;       /* name of the lbmethod provider to use */
362     proxy_balancer_method *lbmethod;
363
364     const char      *sticky_path;     /* URL sticky session identifier */
365     apr_array_header_t *errstatuses;  /* statuses to force members into error */
366     const char      *sticky;          /* sticky session identifier */
367     int             max_attempts;     /* Number of attempts before failing */
368
369     int             sticky_force:1;   /* Disable failover for sticky sessions */
370     int             scolonsep:1;      /* true if ';' seps sticky session paths */
371     int             max_attempts_set:1;
372     void            *context;   /* general purpose storage */
373     apr_time_t      updated;    /* timestamp of last update */
374 };
375
376 struct proxy_balancer_method {
377     const char *name;            /* name of the load balancer method*/
378     proxy_worker *(*finder)(proxy_balancer *balancer,
379                             request_rec *r);
380     void            *context;   /* general purpose storage */
381     apr_status_t (*reset)(proxy_balancer *balancer, server_rec *s);
382     apr_status_t (*age)(proxy_balancer *balancer, server_rec *s);
383     apr_status_t (*updatelbstatus)(proxy_balancer *balancer, proxy_worker *elected, server_rec *s); 
384 };
385
386 #define PROXY_THREAD_LOCK(x)      apr_thread_mutex_lock((x)->mutex)
387 #define PROXY_THREAD_UNLOCK(x)    apr_thread_mutex_unlock((x)->mutex)
388
389 #define PROXY_GLOBAL_LOCK(x)      apr_global_mutex_lock((x)->mutex)
390 #define PROXY_GLOBAL_UNLOCK(x)    apr_global_mutex_unlock((x)->mutex)
391
392 /* hooks */
393
394 /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and 
395  * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
396  */
397 #if !defined(WIN32)
398 #define PROXY_DECLARE(type)            type
399 #define PROXY_DECLARE_NONSTD(type)     type
400 #define PROXY_DECLARE_DATA
401 #elif defined(PROXY_DECLARE_STATIC)
402 #define PROXY_DECLARE(type)            type __stdcall
403 #define PROXY_DECLARE_NONSTD(type)     type
404 #define PROXY_DECLARE_DATA
405 #elif defined(PROXY_DECLARE_EXPORT)
406 #define PROXY_DECLARE(type)            __declspec(dllexport) type __stdcall
407 #define PROXY_DECLARE_NONSTD(type)     __declspec(dllexport) type
408 #define PROXY_DECLARE_DATA             __declspec(dllexport)
409 #else
410 #define PROXY_DECLARE(type)            __declspec(dllimport) type __stdcall
411 #define PROXY_DECLARE_NONSTD(type)     __declspec(dllimport) type
412 #define PROXY_DECLARE_DATA             __declspec(dllimport)
413 #endif
414
415 /**
416  * Hook an optional proxy hook.  Unlike static hooks, this uses a macro
417  * instead of a function.
418  */
419 #define PROXY_OPTIONAL_HOOK(name,fn,pre,succ,order) \
420         APR_OPTIONAL_HOOK(proxy,name,fn,pre,succ,order)
421
422 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, scheme_handler, (request_rec *r, 
423                           proxy_worker *worker, proxy_server_conf *conf, char *url, 
424                           const char *proxyhost, apr_port_t proxyport))
425 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, canon_handler, (request_rec *r, 
426                           char *url))
427
428 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
429 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r)) 
430
431 /**
432  * pre request hook.
433  * It will return the most suitable worker at the moment
434  * and coresponding balancer.
435  * The url is rewritten from balancer://cluster/uri to scheme://host:port/uri
436  * and then the scheme_handler is called.
437  *
438  */
439 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
440                           proxy_balancer **balancer,
441                           request_rec *r,
442                           proxy_server_conf *conf, char **url))                          
443 /**
444  * post request hook.
445  * It is called after request for updating runtime balancer status.
446  */
447 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, post_request, (proxy_worker *worker,
448                           proxy_balancer *balancer, request_rec *r,
449                           proxy_server_conf *conf))
450
451 /**
452  * request status hook
453  * It is called after all proxy processing has been done.  This gives other
454  * modules a chance to create default content on failure, for example
455  */
456 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, request_status,
457                           (int *status, request_rec *r))
458
459 /* proxy_util.c */
460
461 PROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r);
462 PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
463 PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
464 PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
465                                        int forcedec, int proxyreq);
466 PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
467                                            char **passwordp, char **hostp, apr_port_t *port);
468 PROXY_DECLARE(const char *)ap_proxy_date_canon(apr_pool_t *p, const char *x);
469 PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val);
470 PROXY_DECLARE(char *)ap_proxy_removestr(apr_pool_t *pool, const char *list, const char *val);
471 PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x);
472 PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y);
473 PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
474 PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p);
475 PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p);
476 PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p);
477 PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
478 PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
479 PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
480 PROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb, char *buff, size_t bufflen, int *eos);
481 PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key);
482 /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
483 PROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **, const char *, apr_sockaddr_t *, const char *, proxy_server_conf *, request_rec *);
484 PROXY_DECLARE(apr_status_t) ap_proxy_ssl_connection_cleanup(proxy_conn_rec *conn,
485                                                             request_rec *r);
486 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
487 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
488 PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c);
489 PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
490
491 /* Header mapping functions, and a typedef of their signature */
492 PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url);
493 PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str);
494
495 #if !defined(WIN32)
496 typedef const char *(*ap_proxy_header_reverse_map_fn)(request_rec *,
497                        proxy_dir_conf *, const char *);
498 #elif defined(PROXY_DECLARE_STATIC)
499 typedef const char *(__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
500                                  proxy_dir_conf *, const char *);
501 #elif defined(PROXY_DECLARE_EXPORT)
502 typedef __declspec(dllexport) const char *
503   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
504                proxy_dir_conf *, const char *);
505 #else
506 typedef __declspec(dllimport) const char *
507   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
508                proxy_dir_conf *, const char *);
509 #endif
510
511
512 /* Connection pool API */
513 /**
514  * Get the worker from proxy configuration
515  * @param p        memory pool used for finding worker
516  * @param balancer the balancer that the worker belongs to
517  * @param conf     current proxy server configuration
518  * @param url      url to find the worker from
519  * @return         proxy_worker or NULL if not found
520  */
521 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
522                                                   proxy_balancer *balancer,
523                                                   proxy_server_conf *conf,
524                                                   const char *url);
525 /**
526  * Define and Allocate space for the worker to proxy configuration
527  * @param p         memory pool to allocate worker from 
528  * @param worker    the new worker
529  * @param balancer  the balancer that the worker belongs to
530  * @param conf      current proxy server configuration
531  * @param url       url containing worker name
532  * @return          error message or NULL if successful (*worker is new worker)
533  */
534 PROXY_DECLARE(char *) ap_proxy_define_worker(apr_pool_t *p,
535                                                    proxy_worker **worker,
536                                                    proxy_balancer *balancer,
537                                                    proxy_server_conf *conf,
538                                                    const char *url);
539
540 /**
541  * Share a defined proxy worker via shm
542  * @param worker  worker to be shared 
543  * @param shm     location of shared info
544  * @param i       index into shm
545  * @return        APR_SUCCESS or error code
546  */
547 PROXY_DECLARE(apr_status_t) ap_proxy_share_worker(proxy_worker *worker, proxy_worker_shared *shm, int i);
548
549 /**
550  * Initialize the worker by setting up worker connection pool and mutex
551  * @param worker worker to initialize
552  * @param s      current server record
553  * @param p      memory pool used for mutex and connection pool
554  * @return       APR_SUCCESS or error code
555  */
556 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
557                                                        server_rec *s,
558                                                        apr_pool_t *p);
559
560 /**
561  * Verifies valid balancer name (eg: balancer://foo)
562  * @param name  name to test
563  * @return      ptr to start of name or NULL if not valid
564  */
565 PROXY_DECLARE(char *) ap_proxy_valid_balancer_name(char *name);
566
567
568 /**
569  * Get the balancer from proxy configuration
570  * @param p     memory pool used for temporary storage while finding balancer
571  * @param conf  current proxy server configuration
572  * @param url   url to find the worker from; must have balancer:// prefix
573  * @return      proxy_balancer or NULL if not found
574  */
575 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
576                                                       proxy_server_conf *conf,
577                                                       const char *url);
578
579 /**
580  * Define and Allocate space for the balancer to proxy configuration
581  * @param p      memory pool to allocate balancer from 
582  * @param balancer the new balancer
583  * @param conf   current proxy server configuration
584  * @param url    url containing balancer name
585  * @return       error message or NULL if successfull
586  */
587 PROXY_DECLARE(char *) ap_proxy_define_balancer(apr_pool_t *p,
588                                                     proxy_balancer **balancer,
589                                                     proxy_server_conf *conf,
590                                                     const char *url);
591
592 /**
593  * Get the most suitable worker and/or balancer for the request
594  * @param worker   worker used for processing request
595  * @param balancer balancer used for processing request
596  * @param r        current request
597  * @param conf     current proxy server configuration
598  * @param url      request url that balancer can rewrite.
599  * @return         OK or  HTTP_XXX error 
600  * @note It calls balancer pre_request hook if the url starts with balancer://
601  * The balancer then rewrites the url to particular worker, like http://host:port
602  */
603 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
604                                         proxy_balancer **balancer,
605                                         request_rec *r,
606                                         proxy_server_conf *conf,
607                                         char **url);
608 /**
609  * Post request worker and balancer cleanup
610  * @param worker   worker used for processing request
611  * @param balancer balancer used for processing request
612  * @param r        current request
613  * @param conf     current proxy server configuration
614  * @return         OK or  HTTP_XXX error
615  * @note Whenever the pre_request is called, the post_request has to be
616  * called too. 
617  */
618 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
619                                          proxy_balancer *balancer,
620                                          request_rec *r,
621                                          proxy_server_conf *conf);
622
623 /**
624  * Request status function
625  * @param status   status of proxy request (result)
626  * @param r        the request to obtain the status for
627  * @return         OK or DECLINED
628  */
629  PROXY_DECLARE(int) ap_proxy_request_status(int *status, request_rec *r);
630
631 /**
632  * Determine backend hostname and port
633  * @param p       memory pool used for processing
634  * @param r       current request
635  * @param conf    current proxy server configuration
636  * @param worker  worker used for processing request
637  * @param conn    proxy connection struct
638  * @param uri     processed uri
639  * @param url     request url
640  * @param proxyname are we connecting directly or via a proxy
641  * @param proxyport proxy host port
642  * @param server_portstr Via headers server port
643  * @param server_portstr_size size of the server_portstr buffer
644  * @return         OK or HTTP_XXX error
645  */                                         
646 PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
647                                                  proxy_server_conf *conf,
648                                                  proxy_worker *worker,
649                                                  proxy_conn_rec *conn,
650                                                  apr_uri_t *uri,
651                                                  char **url,
652                                                  const char *proxyname,
653                                                  apr_port_t proxyport,
654                                                  char *server_portstr,
655                                                  int server_portstr_size);
656
657 /**
658  * Mark a worker for retry
659  * @param proxy_function calling proxy scheme (http, ajp, ...)
660  * @param worker  worker used for retrying
661  * @param s       current server record
662  * @return        OK if marked for retry, DECLINED otherwise
663  * @note The error status of the worker will cleared if the retry interval has
664  * elapsed since the last error.
665  */
666 PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
667                                          proxy_worker *worker,
668                                          server_rec *s);
669
670 /**
671  * Acquire a connection from worker connection pool
672  * @param proxy_function calling proxy scheme (http, ajp, ...)
673  * @param conn    acquired connection
674  * @param worker  worker used for obtaining connection
675  * @param s       current server record
676  * @return        OK or HTTP_XXX error
677  * @note If the connection limit has been reached, the function will
678  * block until a connection becomes available or the timeout has
679  * elapsed.
680  */
681 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
682                                                proxy_conn_rec **conn,
683                                                proxy_worker *worker,
684                                                server_rec *s);
685 /**
686  * Release a connection back to worker connection pool
687  * @param proxy_function calling proxy scheme (http, ajp, ...)
688  * @param conn    acquired connection
689  * @param s       current server record
690  * @return        OK or HTTP_XXX error
691  * @note The connection will be closed if conn->close_on_release is set
692  */                                         
693 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
694                                                proxy_conn_rec *conn,
695                                                server_rec *s);
696 /**
697  * Make a connection to the backend
698  * @param proxy_function calling proxy scheme (http, ajp, ...)
699  * @param conn    acquired connection
700  * @param worker  connection worker
701  * @param s       current server record
702  * @return        OK or HTTP_XXX error
703  * @note In case the socket already exists for conn, just check the link
704  * status.
705  */                                         
706 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
707                                             proxy_conn_rec *conn,
708                                             proxy_worker *worker,
709                                             server_rec *s);
710 /**
711  * Make a connection record for backend connection
712  * @param proxy_function calling proxy scheme (http, ajp, ...)
713  * @param conn    acquired connection
714  * @param c       client connection record
715  * @param s       current server record
716  * @return        OK or HTTP_XXX error
717  * @note The function will return immediately if conn->connection
718  * is already set,
719  */
720 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
721                                               proxy_conn_rec *conn,
722                                               conn_rec *c, server_rec *s);
723 /**
724  * Signal the upstream chain that the connection to the backend broke in the
725  * middle of the response. This is done by sending an error bucket with
726  * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
727  * @param r       current request record of client request
728  * @param brigade The brigade that is sent through the output filter chain
729  */
730 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
731                                            apr_bucket_brigade *brigade);
732
733 /**
734  * Transform buckets from one bucket allocator to another one by creating a
735  * transient bucket for each data bucket and let it use the data read from
736  * the old bucket. Metabuckets are transformed by just recreating them.
737  * Attention: Currently only the following bucket types are handled:
738  *
739  * All data buckets
740  * FLUSH
741  * EOS
742  *
743  * If an other bucket type is found its type is logged as a debug message
744  * and APR_EGENERAL is returned.
745  * @param r    current request record of client request. Only used for logging
746  *             purposes
747  * @param from the brigade that contains the buckets to transform
748  * @param to   the brigade that will receive the transformed buckets
749  * @return     APR_SUCCESS if all buckets could be transformed APR_EGENERAL
750  *             otherwise
751  */
752 PROXY_DECLARE(apr_status_t)
753 ap_proxy_buckets_lifetime_transform(request_rec *r, apr_bucket_brigade *from,
754                                         apr_bucket_brigade *to);
755 /**
756  * Return a hash based on the passed string
757  * @param str     string to produce hash from
758  * @param method  hashing method to use
759  * @return        hash as unsigned int
760  */
761
762 typedef enum { PROXY_HASHFUNC_DEFAULT, PROXY_HASHFUNC_APR,  PROXY_HASHFUNC_FNV } proxy_hash_t;
763
764 PROXY_DECLARE(unsigned int)
765 ap_proxy_hashfunc(const char *str, proxy_hash_t method);
766
767 #define PROXY_LBMETHOD "proxylbmethod"
768
769 /* The number of dynamic workers that can be added when reconfiguring.
770  * If this limit is reached you must stop and restart the server.
771  */
772 #define PROXY_DYNAMIC_BALANCER_LIMIT    16
773
774 /**
775  * Calculate maximum number of workers in scoreboard.
776  * @return  number of workers to allocate in the scoreboard
777  */
778 int ap_proxy_lb_workers(void);
779
780 /* For proxy_util */
781 extern module PROXY_DECLARE_DATA proxy_module;
782
783 extern int PROXY_DECLARE_DATA proxy_lb_workers;
784
785 #endif /*MOD_PROXY_H*/
786 /** @} */