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