]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy.h
Proxy balancer: support setting error status according to
[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     apr_port_t  port;       /* the port for this proxy */
96     ap_regex_t *regexp;        /* compiled regex (if any) for the remote */
97     int use_regex;          /* simple boolean. True if we have a regex pattern */
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     int req;                /* true if proxy requests are enabled */
138     char req_set;
139     enum {
140       via_off,
141       via_on,
142       via_block,
143       via_full
144     } viaopt;                   /* how to deal with proxy Via: headers */
145     char viaopt_set;
146     apr_size_t recv_buffer_size;
147     char recv_buffer_size_set;
148     apr_size_t io_buffer_size;
149     char io_buffer_size_set;
150     long maxfwd;
151     char maxfwd_set;
152     /** 
153      * the following setting masks the error page
154      * returned from the 'proxied server' and just 
155      * forwards the status code upwards.
156      * This allows the main server (us) to generate
157      * the error page, (so it will look like a error
158      * returned from the rest of the system 
159      */
160     int error_override;
161     int error_override_set;
162     apr_interval_time_t timeout;
163     char timeout_set;
164     enum {
165       bad_error,
166       bad_ignore,
167       bad_body
168     } badopt;                   /* how to deal with bad headers */
169     char badopt_set;
170 /* putting new stuff on the end maximises binary back-compatibility.
171  * the strmatch_patterns are really a const just to have a
172  * case-independent strstr.
173  */
174     enum {
175         status_off,
176         status_on,
177         status_full
178     } proxy_status;             /* Status display options */
179     char proxy_status_set;
180     apr_pool_t *pool;           /* Pool used for allocating this struct */
181 } proxy_server_conf;
182
183
184 typedef struct {
185     const char *p;            /* The path */
186     int         p_is_fnmatch; /* Is this path an fnmatch candidate? */
187     ap_regex_t  *r;            /* Is this a regex? */
188
189 /* ProxyPassReverse and friends are documented as working inside
190  * <Location>.  But in fact they never have done in the case of
191  * more than one <Location>, because the server_conf can't see it.
192  * We need to move them to the per-dir config.
193  * Discussed in February:
194  * http://marc.theaimsgroup.com/?l=apache-httpd-dev&m=110726027118798&w=2
195  */
196     apr_array_header_t *raliases;
197     apr_array_header_t* cookie_paths;
198     apr_array_header_t* cookie_domains;
199     const apr_strmatch_pattern* cookie_path_str;
200     const apr_strmatch_pattern* cookie_domain_str;
201     int interpolate_env;
202     int preserve_host;
203     int preserve_host_set;
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     const char   *hostname;
218     apr_port_t   port;
219     int          is_ssl;
220     apr_pool_t   *pool;     /* Subpool for hostname and addr data */
221     apr_socket_t *sock;     /* Connection socket */
222     apr_sockaddr_t *addr;   /* Preparsed remote address info */
223     apr_uint32_t flags;     /* Connection flags */
224     int          close;     /* Close 'this' connection */
225     proxy_worker *worker;   /* Connection pool this connection belongs to */
226     void         *data;     /* per scheme connection data */
227 #if APR_HAS_THREADS
228     int          inreslist; /* connection in apr_reslist? */
229 #endif
230     apr_pool_t   *scpool;   /* Subpool used for socket and connection data */
231     request_rec  *r;        /* Request record of the frontend request
232                              * which the backend currently answers. */
233     int          need_flush;/* Flag to decide whether we need to flush the
234                              * filter chain or not */
235     void         *forward;  /* opaque forward proxy data */
236 } proxy_conn_rec;
237
238 typedef struct {
239         float cache_completion; /* completion percentage */
240         int content_length; /* length of the content */
241 } proxy_completion;
242
243 /* Connection pool */
244 struct proxy_conn_pool {
245     apr_pool_t     *pool;   /* The pool used in constructor and destructor calls */
246     apr_sockaddr_t *addr;   /* Preparsed remote address info */
247 #if APR_HAS_THREADS
248     apr_reslist_t  *res;    /* Connection resource list */
249 #endif
250     proxy_conn_rec *conn;   /* Single connection for prefork mpm's */
251 };
252
253 /* worker status flags */
254 #define PROXY_WORKER_INITIALIZED    0x0001
255 #define PROXY_WORKER_IGNORE_ERRORS  0x0002
256 #define PROXY_WORKER_IN_SHUTDOWN    0x0010
257 #define PROXY_WORKER_DISABLED       0x0020
258 #define PROXY_WORKER_STOPPED        0x0040
259 #define PROXY_WORKER_IN_ERROR       0x0080
260 #define PROXY_WORKER_HOT_STANDBY    0x0100
261
262 #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
263 PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
264
265 /* NOTE: these check the shared status */
266 #define PROXY_WORKER_IS_INITIALIZED(f)   ( (f)->s && \
267   ( (f)->s->status &  PROXY_WORKER_INITIALIZED ) )
268
269 #define PROXY_WORKER_IS_STANDBY(f)   ( (f)->s && \
270   ( (f)->s->status &  PROXY_WORKER_HOT_STANDBY ) )
271
272 #define PROXY_WORKER_IS_USABLE(f)   ( (f)->s && \
273   ( !( (f)->s->status & PROXY_WORKER_NOT_USABLE_BITMAP) ) && \
274   PROXY_WORKER_IS_INITIALIZED(f) )
275
276 /* default worker retry timeout in seconds */
277 #define PROXY_WORKER_DEFAULT_RETRY  60
278 #define PROXY_WORKER_MAX_ROUTE_SIZ  63
279
280 /* Runtime worker status informations. Shared in scoreboard */
281 typedef struct {
282     int             status;
283     apr_time_t      error_time; /* time of the last error */
284     int             retries;    /* number of retries on this worker */
285     int             lbstatus;   /* Current lbstatus */
286     int             lbfactor;   /* dynamic lbfactor */
287     apr_off_t       transferred;/* Number of bytes transferred to remote */
288     apr_off_t       read;       /* Number of bytes read from remote */
289     apr_size_t      elected;    /* Number of times the worker was elected */
290     char            route[PROXY_WORKER_MAX_ROUTE_SIZ+1];
291     char            redirect[PROXY_WORKER_MAX_ROUTE_SIZ+1];
292     void            *context;   /* general purpose storage */
293     apr_size_t      busy;       /* busyness factor */
294     int             lbset;      /* load balancer cluster set */
295 } proxy_worker_stat;
296
297 /* Worker configuration */
298 struct proxy_worker {
299     int             id;         /* scoreboard id */
300     apr_interval_time_t retry;  /* retry interval */
301     int             lbfactor;   /* initial load balancing factor */
302     const char      *name;
303     const char      *scheme;    /* scheme to use ajp|http|https */
304     const char      *hostname;  /* remote backend address */
305     const char      *route;     /* balancing route */
306     const char      *redirect;  /* temporary balancing redirection route */
307     int             status;     /* temporary worker status */
308     apr_port_t      port;
309     int             min;        /* Desired minimum number of available connections */
310     int             smax;       /* Soft maximum on the total number of connections */
311     int             hmax;       /* Hard maximum on the total number of connections */
312     apr_interval_time_t ttl;    /* maximum amount of time in seconds a connection
313                                  * may be available while exceeding the soft limit */
314     apr_interval_time_t timeout; /* connection timeout */
315     char            timeout_set;
316     apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
317     char            acquire_set;
318     apr_size_t      recv_buffer_size;
319     char            recv_buffer_size_set;
320     apr_size_t      io_buffer_size;
321     char            io_buffer_size_set;
322     char            keepalive;
323     char            keepalive_set;
324     proxy_conn_pool     *cp;        /* Connection pool to use */
325     proxy_worker_stat   *s;         /* Shared data */
326     void            *opaque;    /* per scheme worker data */
327     int             is_address_reusable;
328 #if APR_HAS_THREADS
329     apr_thread_mutex_t  *mutex;  /* Thread lock for updating address cache */
330 #endif
331     void            *context;   /* general purpose storage */
332     enum {
333          flush_off,
334          flush_on,
335          flush_auto
336     } flush_packets;           /* control AJP flushing */
337     int             flush_wait;  /* poll wait time in microseconds if flush_auto */
338     apr_interval_time_t ping_timeout;
339     char ping_timeout_set;
340     int             lbset;      /* load balancer cluster set */
341     char            retry_set;
342     char            disablereuse;
343     char            disablereuse_set;
344     apr_interval_time_t conn_timeout;
345     char            conn_timeout_set;
346     const char      *flusher;  /* flush provider used by mod_proxy_fdpass */
347 };
348
349 /*
350  * Wait 10000 microseconds to find out if more data is currently
351  * available at the backend. Just an arbitrary choose.
352  */
353 #define PROXY_FLUSH_WAIT 10000
354
355 struct proxy_balancer {
356     apr_array_header_t *workers; /* array of proxy_workers */
357     const char *name;            /* name of the load balancer */
358     const char *sticky;          /* sticky session identifier */
359     int         sticky_force;    /* Disable failover for sticky sessions */
360     apr_interval_time_t timeout; /* Timeout for waiting on free connection */
361     int                 max_attempts; /* Number of attempts before failing */
362     char                max_attempts_set;
363     proxy_balancer_method *lbmethod;
364
365     /* XXX: Perhaps we will need the proc mutex too.
366      * Altrough we are only using arithmetic operations
367      * it may lead to a incorrect calculations.
368      * For now use only the thread mutex.
369      */
370 #if APR_HAS_THREADS
371     apr_thread_mutex_t  *mutex;  /* Thread lock for updating lb params */
372 #endif
373     void            *context;   /* general purpose storage */
374     const char      *sticky_path;  /* URL sticky session identifier */
375     int             scolonsep;     /* true if ';' seps sticky session paths */
376     apr_array_header_t *errstatuses; /* statuses to force members into error */
377 };
378
379 struct proxy_balancer_method {
380     const char *name;            /* name of the load balancer method*/
381     proxy_worker *(*finder)(proxy_balancer *balancer,
382                             request_rec *r);
383     void            *context;   /* general purpose storage */
384     apr_status_t (*reset)(proxy_balancer *balancer, server_rec *s);
385     apr_status_t (*age)(proxy_balancer *balancer, server_rec *s);
386 };
387
388 #if APR_HAS_THREADS
389 #define PROXY_THREAD_LOCK(x)      apr_thread_mutex_lock((x)->mutex)
390 #define PROXY_THREAD_UNLOCK(x)    apr_thread_mutex_unlock((x)->mutex)
391 #else
392 #define PROXY_THREAD_LOCK(x)      APR_SUCCESS
393 #define PROXY_THREAD_UNLOCK(x)    APR_SUCCESS
394 #endif
395
396 /* hooks */
397
398 /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and 
399  * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
400  */
401 #if !defined(WIN32)
402 #define PROXY_DECLARE(type)            type
403 #define PROXY_DECLARE_NONSTD(type)     type
404 #define PROXY_DECLARE_DATA
405 #elif defined(PROXY_DECLARE_STATIC)
406 #define PROXY_DECLARE(type)            type __stdcall
407 #define PROXY_DECLARE_NONSTD(type)     type
408 #define PROXY_DECLARE_DATA
409 #elif defined(PROXY_DECLARE_EXPORT)
410 #define PROXY_DECLARE(type)            __declspec(dllexport) type __stdcall
411 #define PROXY_DECLARE_NONSTD(type)     __declspec(dllexport) type
412 #define PROXY_DECLARE_DATA             __declspec(dllexport)
413 #else
414 #define PROXY_DECLARE(type)            __declspec(dllimport) type __stdcall
415 #define PROXY_DECLARE_NONSTD(type)     __declspec(dllimport) type
416 #define PROXY_DECLARE_DATA             __declspec(dllimport)
417 #endif
418
419 /**
420  * Hook an optional proxy hook.  Unlike static hooks, this uses a macro
421  * instead of a function.
422  */
423 #define PROXY_OPTIONAL_HOOK(name,fn,pre,succ,order) \
424         APR_OPTIONAL_HOOK(proxy,name,fn,pre,succ,order)
425
426 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, scheme_handler, (request_rec *r, 
427                           proxy_worker *worker, proxy_server_conf *conf, char *url, 
428                           const char *proxyhost, apr_port_t proxyport))
429 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, canon_handler, (request_rec *r, 
430                           char *url))
431
432 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
433 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r)) 
434
435 /**
436  * pre request hook.
437  * It will return the most suitable worker at the moment
438  * and coresponding balancer.
439  * The url is rewritten from balancer://cluster/uri to scheme://host:port/uri
440  * and then the scheme_handler is called.
441  *
442  */
443 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
444                           proxy_balancer **balancer,
445                           request_rec *r,
446                           proxy_server_conf *conf, char **url))                          
447 /**
448  * post request hook.
449  * It is called after request for updating runtime balancer status.
450  */
451 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, post_request, (proxy_worker *worker,
452                           proxy_balancer *balancer, request_rec *r,
453                           proxy_server_conf *conf))
454
455 /**
456  * request status hook
457  * It is called after all proxy processing has been done.  This gives other
458  * modules a chance to create default content on failure, for example
459  */
460 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, request_status,
461                           (int *status, request_rec *r))
462
463 /* proxy_util.c */
464
465 PROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r);
466 PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
467 PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
468 PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
469                                        int forcedec, int proxyreq);
470 PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
471                                            char **passwordp, char **hostp, apr_port_t *port);
472 PROXY_DECLARE(const char *)ap_proxy_date_canon(apr_pool_t *p, const char *x);
473 PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val);
474 PROXY_DECLARE(char *)ap_proxy_removestr(apr_pool_t *pool, const char *list, const char *val);
475 PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x);
476 PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y);
477 PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
478 PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p);
479 PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p);
480 PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p);
481 PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
482 PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
483 PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
484 PROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb, char *buff, size_t bufflen, int *eos);
485 PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key);
486 /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
487 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 *);
488 PROXY_DECLARE(apr_status_t) ap_proxy_ssl_connection_cleanup(proxy_conn_rec *conn,
489                                                             request_rec *r);
490 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
491 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
492 PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c);
493 PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
494
495 /* Header mapping functions, and a typedef of their signature */
496 PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url);
497 PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str);
498
499 #if !defined(WIN32)
500 typedef const char *(*ap_proxy_header_reverse_map_fn)(request_rec *,
501                        proxy_dir_conf *, const char *);
502 #elif defined(PROXY_DECLARE_STATIC)
503 typedef const char *(__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
504                                  proxy_dir_conf *, const char *);
505 #elif defined(PROXY_DECLARE_EXPORT)
506 typedef __declspec(dllexport) const char *
507   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
508                proxy_dir_conf *, const char *);
509 #else
510 typedef __declspec(dllimport) const char *
511   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
512                proxy_dir_conf *, const char *);
513 #endif
514
515
516 /* Connection pool API */
517 /**
518  * Get the worker from proxy configuration
519  * @param p     memory pool used for finding worker
520  * @param conf  current proxy server configuration
521  * @param url   url to find the worker from
522  * @return      proxy_worker or NULL if not found
523  */
524 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
525                                                   proxy_server_conf *conf,
526                                                   const char *url);
527 /**
528  * Add the worker to proxy configuration
529  * @param worker the new worker
530  * @param p      memory pool to allocate worker from 
531  * @param conf   current proxy server configuration
532  * @param url    url containing worker name
533  * @return       error message or NULL if successfull
534  */
535 PROXY_DECLARE(const char *) ap_proxy_add_worker(proxy_worker **worker,
536                                                 apr_pool_t *p,
537                                                 proxy_server_conf *conf,
538                                                 const char *url);
539
540 /**
541  * Create new worker
542  * @param p      memory pool to allocate worker from 
543  * @return       new worker
544  */
545 PROXY_DECLARE(proxy_worker *) ap_proxy_create_worker(apr_pool_t *p);
546
547 /**
548  * Initize the worker's shared data
549  * @param conf   current proxy server configuration
550  * @param worker worker to initialize
551  * @param s      current server record
552  * @param worker worker to initialize
553  */
554 PROXY_DECLARE(void) ap_proxy_initialize_worker_share(proxy_server_conf *conf,
555                                                      proxy_worker *worker,
556                                                      server_rec *s);
557
558
559 /**
560  * Initize the worker
561  * @param worker worker to initialize
562  * @param s      current server record
563  * @param p      memory pool used for mutex and Connection pool.
564  * @return       APR_SUCCESS or error code
565  */
566 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
567                                                        server_rec *s,
568                                                        apr_pool_t *p);
569 /**
570  * Get the balancer from proxy configuration
571  * @param p     memory pool used for finding balancer
572  * @param conf  current proxy server configuration
573  * @param url   url to find the worker from. Has to have balancer:// prefix
574  * @return      proxy_balancer or NULL if not found
575  */
576 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
577                                                       proxy_server_conf *conf,
578                                                       const char *url);
579 /**
580  * Add the balancer to proxy configuration
581  * @param balancer the new balancer
582  * @param p      memory pool to allocate balancer from 
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(const char *) ap_proxy_add_balancer(proxy_balancer **balancer,
588                                                   apr_pool_t *p,
589                                                   proxy_server_conf *conf,
590                                                   const char *url);
591
592 /**
593  * Add the worker to the balancer
594  * @param pool     memory pool for adding worker 
595  * @param balancer balancer to add to
596  * @param worker worker to add
597  * @note Single worker can be added to multiple balancers.
598  */
599 PROXY_DECLARE(void) ap_proxy_add_worker_to_balancer(apr_pool_t *pool,
600                                                     proxy_balancer *balancer,
601                                                     proxy_worker *worker);
602 /**
603  * Get the most suitable worker and(or) balancer for the request
604  * @param worker   worker used for processing request
605  * @param balancer balancer used for processing request
606  * @param r        current request
607  * @param conf     current proxy server configuration
608  * @param url      request url that balancer can rewrite.
609  * @return         OK or  HTTP_XXX error 
610  * @note It calls balancer pre_request hook if the url starts with balancer://
611  * The balancer then rewrites the url to particular worker, like http://host:port
612  */
613 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
614                                         proxy_balancer **balancer,
615                                         request_rec *r,
616                                         proxy_server_conf *conf,
617                                         char **url);
618 /**
619  * Post request worker and balancer cleanup
620  * @param worker   worker used for processing request
621  * @param balancer balancer used for processing request
622  * @param r        current request
623  * @param conf     current proxy server configuration
624  * @return         OK or  HTTP_XXX error
625  * @note When ever the pre_request is called, the post_request has to be
626  * called too. 
627  */
628 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
629                                          proxy_balancer *balancer,
630                                          request_rec *r,
631                                          proxy_server_conf *conf);
632
633 /**
634  * Request status function
635  * @param status   status of proxy request (result)
636  * @param r        the request to obtain the status for
637  * @return         OK or DECLINED
638  */
639  PROXY_DECLARE(int) ap_proxy_request_status(int *status, request_rec *r);
640
641 /**
642  * Deternime backend hostname and port
643  * @param p       memory pool used for processing
644  * @param r       current request
645  * @param conf    current proxy server configuration
646  * @param worker  worker used for processing request
647  * @param conn    proxy connection struct
648  * @param uri     processed uri
649  * @param url     request url
650  * @param proxyname are we connecting directly or via s proxy
651  * @param proxyport proxy host port
652  * @param server_portstr Via headers server port
653  * @param server_portstr_size size of the server_portstr buffer
654  * @return         OK or HTTP_XXX error
655  */                                         
656 PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
657                                                  proxy_server_conf *conf,
658                                                  proxy_worker *worker,
659                                                  proxy_conn_rec *conn,
660                                                  apr_uri_t *uri,
661                                                  char **url,
662                                                  const char *proxyname,
663                                                  apr_port_t proxyport,
664                                                  char *server_portstr,
665                                                  int server_portstr_size);
666
667 /**
668  * Mark a worker for retry
669  * @param proxy_function calling proxy scheme (http, ajp, ...)
670  * @param worker  worker used for retrying
671  * @param s       current server record
672  * @return        OK if marked for retry, DECLINED otherwise
673  * @note Worker will be marker for retry if the time of the last retry
674  * has been ellapsed. In case there is no retry option set, defaults to
675  * number_of_retries seconds.
676  */                                         
677 PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
678                                          proxy_worker *worker,
679                                          server_rec *s);
680 /**
681  * Acquire a connection from workers connection pool
682  * @param proxy_function calling proxy scheme (http, ajp, ...)
683  * @param conn    acquired connection
684  * @param worker  worker used for obtaining connection
685  * @param s       current server record
686  * @return        OK or HTTP_XXX error
687  * @note If the number of connections is exhaused the function will
688  * block untill the timeout is reached.
689  */                                         
690 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
691                                                proxy_conn_rec **conn,
692                                                proxy_worker *worker,
693                                                server_rec *s);
694 /**
695  * Release a connection back to worker connection pool
696  * @param proxy_function calling proxy scheme (http, ajp, ...)
697  * @param conn    acquired connection
698  * @param s       current server record
699  * @return        OK or HTTP_XXX error
700  * @note The connection will be closed if conn->close_on_release is set
701  */                                         
702 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
703                                                proxy_conn_rec *conn,
704                                                server_rec *s);
705 /**
706  * Make a connection to the backend
707  * @param proxy_function calling proxy scheme (http, ajp, ...)
708  * @param conn    acquired connection
709  * @param worker  connection worker
710  * @param s       current server record
711  * @return        OK or HTTP_XXX error
712  * @note In case the socket already exists for conn, just check the link
713  * status.
714  */                                         
715 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
716                                             proxy_conn_rec *conn,
717                                             proxy_worker *worker,
718                                             server_rec *s);
719 /**
720  * Make a connection record for backend connection
721  * @param proxy_function calling proxy scheme (http, ajp, ...)
722  * @param conn    acquired connection
723  * @param c       client connection record
724  * @param s       current server record
725  * @return        OK or HTTP_XXX error
726  */                                         
727 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
728                                               proxy_conn_rec *conn,
729                                               conn_rec *c, server_rec *s);
730 /**
731  * Signal the upstream chain that the connection to the backend broke in the
732  * middle of the response. This is done by sending an error bucket with
733  * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
734  * @param r       current request record of client request
735  * @param brigade The brigade that is sent through the output filter chain
736  */
737 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
738                                            apr_bucket_brigade *brigade);
739
740 /**
741  * Transform buckets from one bucket allocator to another one by creating a
742  * transient bucket for each data bucket and let it use the data read from
743  * the old bucket. Metabuckets are transformed by just recreating them.
744  * Attention: Currently only the following bucket types are handled:
745  *
746  * All data buckets
747  * FLUSH
748  * EOS
749  *
750  * If an other bucket type is found its type is logged as a debug message
751  * and APR_EGENERAL is returned.
752  * @param r    current request record of client request. Only used for logging
753  *             purposes
754  * @param from the brigade that contains the buckets to transform
755  * @param to   the brigade that will receive the transformed buckets
756  * @return     APR_SUCCESS if all buckets could be transformed APR_EGENERAL
757  *             otherwise
758  */
759 PROXY_DECLARE(apr_status_t)
760 ap_proxy_buckets_lifetime_transform(request_rec *r, apr_bucket_brigade *from,
761                                         apr_bucket_brigade *to);
762
763 #define PROXY_LBMETHOD "proxylbmethod"
764
765 /* The number of dynamic workers that can be added when reconfiguring.
766  * If this limit is reached you must stop and restart the server.
767  */
768 #define PROXY_DYNAMIC_BALANCER_LIMIT    16
769 /**
770  * Calculate number of maximum number of workers in scoreboard.
771  * @return  number of workers to allocate in the scoreboard
772  */
773 int ap_proxy_lb_workers(void);
774
775 /* For proxy_util */
776 extern module PROXY_DECLARE_DATA proxy_module;
777
778 extern int PROXY_DECLARE_DATA proxy_lb_workers;
779
780 #endif /*MOD_PROXY_H*/
781 /** @} */