]> granicus.if.org Git - apache/blob - modules/proxy/mod_proxy.h
Make the balancer shared memory slot more human readable. But we need
[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 "apr_uuid.h"
46 #include "util_mutex.h"
47 #include "apr_global_mutex.h"
48 #include "apr_thread_mutex.h"
49
50 #include "httpd.h"
51 #include "http_config.h"
52 #include "ap_config.h"
53 #include "http_core.h"
54 #include "http_protocol.h"
55 #include "http_request.h"
56 #include "http_vhost.h"
57 #include "http_main.h"
58 #include "http_log.h"
59 #include "http_connection.h"
60 #include "util_filter.h"
61 #include "util_ebcdic.h"
62 #include "ap_provider.h"
63 #include "ap_slotmem.h"
64
65 #if APR_HAVE_NETINET_IN_H
66 #include <netinet/in.h>
67 #endif
68 #if APR_HAVE_ARPA_INET_H
69 #include <arpa/inet.h>
70 #endif
71
72 /* for proxy_canonenc() */
73 enum enctype {
74     enc_path, enc_search, enc_user, enc_fpath, enc_parm
75 };
76
77 #define BALANCER_PREFIX "balancer://"
78
79 #if APR_CHARSET_EBCDIC
80 #define CRLF   "\r\n"
81 #else /*APR_CHARSET_EBCDIC*/
82 #define CRLF   "\015\012"
83 #endif /*APR_CHARSET_EBCDIC*/
84
85 /* default Max-Forwards header setting */
86 /* Set this to -1, which complies with RFC2616 by not setting
87  * max-forwards if the client didn't send it to us.
88  */
89 #define DEFAULT_MAX_FORWARDS    -1
90
91 /* static information about a remote proxy */
92 struct proxy_remote {
93     const char *scheme;     /* the schemes handled by this proxy, or '*' */
94     const char *protocol;   /* the scheme used to talk to this proxy */
95     const char *hostname;   /* the hostname of 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     apr_port_t  port;       /* the port for this proxy */
99 };
100
101 #define PROXYPASS_NOCANON 0x01
102 #define PROXYPASS_INTERPOLATE 0x02
103 struct proxy_alias {
104     const char  *real;
105     const char  *fake;
106     ap_regex_t  *regex;
107     unsigned int flags;
108 };
109
110 struct dirconn_entry {
111     char *name;
112     struct in_addr addr, mask;
113     struct apr_sockaddr_t *hostaddr;
114     int (*matcher) (struct dirconn_entry * This, request_rec *r);
115 };
116
117 struct noproxy_entry {
118     const char *name;
119     struct apr_sockaddr_t *addr;
120 };
121
122 typedef struct proxy_balancer  proxy_balancer;
123 typedef struct proxy_worker    proxy_worker;
124 typedef struct proxy_conn_pool proxy_conn_pool;
125 typedef struct proxy_balancer_method proxy_balancer_method;
126
127 typedef struct {
128     apr_array_header_t *proxies;
129     apr_array_header_t *sec_proxy;
130     apr_array_header_t *aliases;
131     apr_array_header_t *noproxies;
132     apr_array_header_t *dirconn;
133     apr_array_header_t *workers;    /* non-balancer workers, eg ProxyPass http://example.com */
134     apr_array_header_t *balancers;  /* list of balancers @ config time */
135     proxy_worker       *forward;    /* forward proxy worker */
136     proxy_worker       *reverse;    /* reverse "module-driven" proxy worker */
137     const char *domain;     /* domain name to use in absence of a domain name in the request */
138     apr_pool_t *pool;       /* Pool used for allocating this struct */
139     int req;                /* true if proxy requests are enabled */
140     enum {
141       via_off,
142       via_on,
143       via_block,
144       via_full
145     } viaopt;                   /* how to deal with proxy Via: headers */
146     apr_size_t recv_buffer_size;
147     apr_size_t io_buffer_size;
148     long maxfwd;
149     apr_interval_time_t timeout;
150     enum {
151       bad_error,
152       bad_ignore,
153       bad_body
154     } badopt;                   /* how to deal with bad headers */
155     enum {
156         status_off,
157         status_on,
158         status_full
159     } proxy_status;             /* Status display options */
160     apr_sockaddr_t *source_address;
161     apr_global_mutex_t  *mutex; /* global lock (needed??) */
162
163     int req_set:1;
164     int viaopt_set:1;
165     int recv_buffer_size_set:1;
166     int io_buffer_size_set:1;
167     int maxfwd_set:1;
168     int timeout_set:1;
169     int badopt_set:1;
170     int proxy_status_set:1;
171     int source_address_set:1;
172 } proxy_server_conf;
173
174
175 typedef struct {
176     const char *p;            /* The path */
177     ap_regex_t  *r;            /* Is this a regex? */
178
179 /* FIXME
180  * ProxyPassReverse and friends are documented as working inside
181  * <Location>.  But in fact they never have done in the case of
182  * more than one <Location>, because the server_conf can't see it.
183  * We need to move them to the per-dir config.
184  * Discussed in February 2005:
185  * http://marc.theaimsgroup.com/?l=apache-httpd-dev&m=110726027118798&w=2
186  */
187     apr_array_header_t *raliases;
188     apr_array_header_t* cookie_paths;
189     apr_array_header_t* cookie_domains;
190     const apr_strmatch_pattern* cookie_path_str;
191     const apr_strmatch_pattern* cookie_domain_str;
192     signed char p_is_fnmatch; /* Is the path an fnmatch candidate? */
193     signed char interpolate_env;
194     struct proxy_alias *alias;
195
196     /**
197      * the following setting masks the error page
198      * returned from the 'proxied server' and just
199      * forwards the status code upwards.
200      * This allows the main server (us) to generate
201      * the error page, (so it will look like a error
202      * returned from the rest of the system
203      */
204     int error_override:1;
205     int preserve_host:1;
206     int preserve_host_set:1;
207     int error_override_set:1;
208     int alias_set:1;
209 } proxy_dir_conf;
210
211 /* if we interpolate env vars per-request, we'll need a per-request
212  * copy of the reverse proxy config
213  */
214 typedef struct {
215     apr_array_header_t *raliases;
216     apr_array_header_t* cookie_paths;
217     apr_array_header_t* cookie_domains;
218 } proxy_req_conf;
219
220 typedef struct {
221     conn_rec     *connection;
222     request_rec  *r;           /* Request record of the backend request
223                                 * that is used over the backend connection. */
224     proxy_worker *worker;      /* Connection pool this connection belongs to */
225     apr_pool_t   *pool;        /* Subpool for hostname and addr data */
226     const char   *hostname;
227     apr_sockaddr_t *addr;      /* Preparsed remote address info */
228     apr_pool_t   *scpool;      /* Subpool used for socket and connection data */
229     apr_socket_t *sock;        /* Connection socket */
230     void         *data;        /* per scheme connection data */
231     void         *forward;     /* opaque forward proxy data */
232     apr_uint32_t flags;        /* Connection flags */
233     apr_port_t   port;
234     int          is_ssl:1;
235     int          close:1;      /* Close 'this' connection */
236     int          need_flush:1; /* Flag to decide whether we need to flush the
237                                 * filter chain or not */
238     int          inreslist:1;  /* connection in apr_reslist? */
239 } proxy_conn_rec;
240
241 typedef struct {
242         float cache_completion; /* completion percentage */
243         int content_length; /* length of the content */
244 } proxy_completion;
245
246 /* Connection pool */
247 struct proxy_conn_pool {
248     apr_pool_t     *pool;   /* The pool used in constructor and destructor calls */
249     apr_sockaddr_t *addr;   /* Preparsed remote address info */
250     apr_reslist_t  *res;    /* Connection resource list */
251     proxy_conn_rec *conn;   /* Single connection for prefork mpm */
252 };
253
254 /* worker status flags */
255 #define PROXY_WORKER_INITIALIZED    0x0001
256 #define PROXY_WORKER_IGNORE_ERRORS  0x0002
257 #define PROXY_WORKER_DRAIN          0x0004
258 #define PROXY_WORKER_IN_SHUTDOWN    0x0010
259 #define PROXY_WORKER_DISABLED       0x0020
260 #define PROXY_WORKER_STOPPED        0x0040
261 #define PROXY_WORKER_IN_ERROR       0x0080
262 #define PROXY_WORKER_HOT_STANDBY    0x0100
263 #define PROXY_WORKER_FREE           0x0200
264
265 #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
266 PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
267
268 /* NOTE: these check the shared status */
269 #define PROXY_WORKER_IS_INITIALIZED(f)  ( (f)->s->status &  PROXY_WORKER_INITIALIZED )
270
271 #define PROXY_WORKER_IS_STANDBY(f)   ( (f)->s->status &  PROXY_WORKER_HOT_STANDBY )
272
273 #define PROXY_WORKER_IS_USABLE(f)   ( ( !( (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_SCHEME_SIZE  16
279 #define PROXY_WORKER_MAX_ROUTE_SIZE   64
280 #define PROXY_WORKER_MAX_NAME_SIZE    96
281
282 #define PROXY_STRNCPY(dst, src) apr_cpystrn((dst), (src), sizeof(dst))
283
284 /* Runtime worker status informations. Shared in scoreboard */
285 typedef struct {
286     char      name[PROXY_WORKER_MAX_NAME_SIZE];
287     char      scheme[PROXY_WORKER_MAX_SCHEME_SIZE];   /* scheme to use ajp|http|https */
288     char      hostname[PROXY_WORKER_MAX_ROUTE_SIZE];  /* remote backend address */
289     char      route[PROXY_WORKER_MAX_ROUTE_SIZE];     /* balancing route */
290     char      redirect[PROXY_WORKER_MAX_ROUTE_SIZE];  /* temporary balancing redirection route */
291     char      flusher[PROXY_WORKER_MAX_SCHEME_SIZE];  /* flush provider used by mod_proxy_fdpass */
292     int             lbset;      /* load balancer cluster set */
293     int             status;
294     int             retries;    /* number of retries on this worker */
295     int             lbstatus;   /* Current lbstatus */
296     int             lbfactor;   /* dynamic lbfactor */
297     int             min;        /* Desired minimum number of available connections */
298     int             smax;       /* Soft maximum on the total number of connections */
299     int             hmax;       /* Hard maximum on the total number of connections */
300     int             flush_wait; /* poll wait time in microseconds if flush_auto */
301     int             index;      /* shm array index */
302     unsigned int    hash;       /* hash of worker name */
303     enum {
304         flush_off,
305         flush_on,
306         flush_auto
307     } flush_packets;           /* control AJP flushing */
308     apr_time_t      error_time; /* time of the last error */
309     apr_interval_time_t ttl;    /* maximum amount of time in seconds a connection
310                                  * may be available while exceeding the soft limit */
311     apr_interval_time_t retry;   /* retry interval */
312     apr_interval_time_t timeout; /* connection timeout */
313     apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
314     apr_interval_time_t ping_timeout;
315     apr_interval_time_t conn_timeout;
316     apr_size_t      recv_buffer_size;
317     apr_size_t      io_buffer_size;
318     apr_size_t      elected;    /* Number of times the worker was elected */
319     apr_size_t      busy;       /* busyness factor */
320     apr_port_t      port;
321     apr_off_t       transferred;/* Number of bytes transferred to remote */
322     apr_off_t       read;       /* Number of bytes read from remote */
323     void            *context;   /* general purpose storage */
324     unsigned int     keepalive:1;
325     unsigned int     disablereuse:1;
326     unsigned int     is_address_reusable:1;
327     unsigned int     retry_set:1;
328     unsigned int     timeout_set:1;
329     unsigned int     acquire_set:1;
330     unsigned int     ping_timeout_set:1;
331     unsigned int     conn_timeout_set:1;
332     unsigned int     recv_buffer_size_set:1;
333     unsigned int     io_buffer_size_set:1;
334     unsigned int     keepalive_set:1;
335     unsigned int     disablereuse_set:1;
336 } proxy_worker_shared;
337
338 #define ALIGNED_PROXY_WORKER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_worker_shared)))
339
340 /* Worker configuration */
341 struct proxy_worker {
342     int             index;      /* shm array index */
343     unsigned int    hash;       /* hash of worker name */
344     proxy_conn_pool     *cp;    /* Connection pool to use */
345     proxy_worker_shared   *s;   /* Shared data */
346     proxy_balancer  *balancer;  /* which balancer am I in? */
347     apr_thread_mutex_t  *mutex; /* Thread lock for updating address cache */
348     int local_status;           /* status of per-process worker */
349     void            *context;   /* general purpose storage */
350 };
351
352 /*
353  * Time to wait (in microseconds) to find out if more data is currently
354  * available at the backend.
355  */
356 #define PROXY_FLUSH_WAIT 10000
357
358 struct proxy_balancer {
359     apr_array_header_t *workers;  /* initially configured workers */
360     ap_slotmem_instance_t *slot;  /* worker shm data - runtime */
361     int growth;                   /* number of post-config workers can added */
362     int max_workers;              /* maximum number of allowed workers */
363     const char *name;             /* name of the load balancer */
364     const char *sname;            /* filesystem safe balancer name */
365     apr_interval_time_t timeout;  /* Timeout for waiting on free connection */
366     const char *lbprovider;       /* name of the lbmethod provider to use */
367     proxy_balancer_method *lbmethod;
368
369     const char      *sticky_path;     /* URL sticky session identifier */
370     apr_array_header_t *errstatuses;  /* statuses to force members into error */
371     const char      *sticky;          /* sticky session identifier */
372     int             max_attempts;     /* Number of attempts before failing */
373
374     apr_time_t      updated;    /* timestamp of last update */
375     char nonce[APR_UUID_FORMATTED_LENGTH + 1];
376     apr_global_mutex_t  *mutex; /* global lock for updating lb params */
377     void            *context;   /* general purpose storage */
378     int             sticky_force:1;   /* Disable failover for sticky sessions */
379     int             scolonsep:1;      /* true if ';' seps sticky session paths */
380     int             max_attempts_set:1;
381 };
382
383 struct proxy_balancer_method {
384     const char *name;            /* name of the load balancer method*/
385     proxy_worker *(*finder)(proxy_balancer *balancer,
386                             request_rec *r);
387     void            *context;   /* general purpose storage */
388     apr_status_t (*reset)(proxy_balancer *balancer, server_rec *s);
389     apr_status_t (*age)(proxy_balancer *balancer, server_rec *s);
390     apr_status_t (*updatelbstatus)(proxy_balancer *balancer, proxy_worker *elected, server_rec *s);
391 };
392
393 #define PROXY_THREAD_LOCK(x)      apr_thread_mutex_lock((x)->mutex)
394 #define PROXY_THREAD_UNLOCK(x)    apr_thread_mutex_unlock((x)->mutex)
395
396 #define PROXY_GLOBAL_LOCK(x)      apr_global_mutex_lock((x)->mutex)
397 #define PROXY_GLOBAL_UNLOCK(x)    apr_global_mutex_unlock((x)->mutex)
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 balancer the balancer that the worker belongs to
524  * @param conf     current proxy server configuration
525  * @param url      url to find the worker from
526  * @return         proxy_worker or NULL if not found
527  */
528 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
529                                                   proxy_balancer *balancer,
530                                                   proxy_server_conf *conf,
531                                                   const char *url);
532 /**
533  * Define and Allocate space for the worker to proxy configuration
534  * @param p         memory pool to allocate worker from
535  * @param worker    the new worker
536  * @param balancer  the balancer that the worker belongs to
537  * @param conf      current proxy server configuration
538  * @param url       url containing worker name
539  * @return          error message or NULL if successful (*worker is new worker)
540  */
541 PROXY_DECLARE(char *) ap_proxy_define_worker(apr_pool_t *p,
542                                                    proxy_worker **worker,
543                                                    proxy_balancer *balancer,
544                                                    proxy_server_conf *conf,
545                                                    const char *url);
546
547 /**
548  * Share a defined proxy worker via shm
549  * @param worker  worker to be shared
550  * @param shm     location of shared info
551  * @param i       index into shm
552  * @return        APR_SUCCESS or error code
553  */
554 PROXY_DECLARE(apr_status_t) ap_proxy_share_worker(proxy_worker *worker, proxy_worker_shared *shm, int i);
555
556 /**
557  * Initialize the worker by setting up worker connection pool and mutex
558  * @param worker worker to initialize
559  * @param s      current server record
560  * @param p      memory pool used for mutex and connection pool
561  * @return       APR_SUCCESS or error code
562  */
563 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
564                                                        server_rec *s,
565                                                        apr_pool_t *p);
566
567 /**
568  * Verifies valid balancer name (eg: balancer://foo)
569  * @param name  name to test
570  * @param i     number of chars to test; 0 for all.
571  * @return      true/false
572  */
573 PROXY_DECLARE(int) ap_proxy_valid_balancer_name(char *name, int i);
574
575
576 /**
577  * Get the balancer from proxy configuration
578  * @param p     memory pool used for temporary storage while finding balancer
579  * @param conf  current proxy server configuration
580  * @param url   url to find the worker from; must have balancer:// prefix
581  * @return      proxy_balancer or NULL if not found
582  */
583 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
584                                                       proxy_server_conf *conf,
585                                                       const char *url);
586
587 /**
588  * Define and Allocate space for the balancer to proxy configuration
589  * @param p      memory pool to allocate balancer from
590  * @param balancer the new balancer
591  * @param conf   current proxy server configuration
592  * @param url    url containing balancer name
593  * @return       error message or NULL if successfull
594  */
595 PROXY_DECLARE(char *) ap_proxy_define_balancer(apr_pool_t *p,
596                                                     proxy_balancer **balancer,
597                                                     proxy_server_conf *conf,
598                                                     const char *url);
599
600 /**
601  * Get the most suitable worker and/or balancer for the request
602  * @param worker   worker used for processing request
603  * @param balancer balancer used for processing request
604  * @param r        current request
605  * @param conf     current proxy server configuration
606  * @param url      request url that balancer can rewrite.
607  * @return         OK or  HTTP_XXX error
608  * @note It calls balancer pre_request hook if the url starts with balancer://
609  * The balancer then rewrites the url to particular worker, like http://host:port
610  */
611 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
612                                         proxy_balancer **balancer,
613                                         request_rec *r,
614                                         proxy_server_conf *conf,
615                                         char **url);
616 /**
617  * Post request worker and balancer cleanup
618  * @param worker   worker used for processing request
619  * @param balancer balancer used for processing request
620  * @param r        current request
621  * @param conf     current proxy server configuration
622  * @return         OK or  HTTP_XXX error
623  * @note Whenever the pre_request is called, the post_request has to be
624  * called too.
625  */
626 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
627                                          proxy_balancer *balancer,
628                                          request_rec *r,
629                                          proxy_server_conf *conf);
630
631 /**
632  * Request status function
633  * @param status   status of proxy request (result)
634  * @param r        the request to obtain the status for
635  * @return         OK or DECLINED
636  */
637  PROXY_DECLARE(int) ap_proxy_request_status(int *status, request_rec *r);
638
639 /**
640  * Determine backend hostname and port
641  * @param p       memory pool used for processing
642  * @param r       current request
643  * @param conf    current proxy server configuration
644  * @param worker  worker used for processing request
645  * @param conn    proxy connection struct
646  * @param uri     processed uri
647  * @param url     request url
648  * @param proxyname are we connecting directly or via a proxy
649  * @param proxyport proxy host port
650  * @param server_portstr Via headers server port
651  * @param server_portstr_size size of the server_portstr buffer
652  * @return         OK or HTTP_XXX error
653  */
654 PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
655                                                  proxy_server_conf *conf,
656                                                  proxy_worker *worker,
657                                                  proxy_conn_rec *conn,
658                                                  apr_uri_t *uri,
659                                                  char **url,
660                                                  const char *proxyname,
661                                                  apr_port_t proxyport,
662                                                  char *server_portstr,
663                                                  int server_portstr_size);
664
665 /**
666  * Mark a worker for retry
667  * @param proxy_function calling proxy scheme (http, ajp, ...)
668  * @param worker  worker used for retrying
669  * @param s       current server record
670  * @return        OK if marked for retry, DECLINED otherwise
671  * @note The error status of the worker will cleared if the retry interval has
672  * elapsed since the last error.
673  */
674 PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
675                                          proxy_worker *worker,
676                                          server_rec *s);
677
678 /**
679  * Acquire a connection from worker connection pool
680  * @param proxy_function calling proxy scheme (http, ajp, ...)
681  * @param conn    acquired connection
682  * @param worker  worker used for obtaining connection
683  * @param s       current server record
684  * @return        OK or HTTP_XXX error
685  * @note If the connection limit has been reached, the function will
686  * block until a connection becomes available or the timeout has
687  * elapsed.
688  */
689 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
690                                                proxy_conn_rec **conn,
691                                                proxy_worker *worker,
692                                                server_rec *s);
693 /**
694  * Release a connection back to worker connection pool
695  * @param proxy_function calling proxy scheme (http, ajp, ...)
696  * @param conn    acquired connection
697  * @param s       current server record
698  * @return        OK or HTTP_XXX error
699  * @note The connection will be closed if conn->close_on_release is set
700  */
701 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
702                                                proxy_conn_rec *conn,
703                                                server_rec *s);
704 /**
705  * Make a connection to the backend
706  * @param proxy_function calling proxy scheme (http, ajp, ...)
707  * @param conn    acquired connection
708  * @param worker  connection worker
709  * @param s       current server record
710  * @return        OK or HTTP_XXX error
711  * @note In case the socket already exists for conn, just check the link
712  * status.
713  */
714 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
715                                             proxy_conn_rec *conn,
716                                             proxy_worker *worker,
717                                             server_rec *s);
718 /**
719  * Make a connection record for backend connection
720  * @param proxy_function calling proxy scheme (http, ajp, ...)
721  * @param conn    acquired connection
722  * @param c       client connection record
723  * @param s       current server record
724  * @return        OK or HTTP_XXX error
725  * @note The function will return immediately if conn->connection
726  * is already set,
727  */
728 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
729                                               proxy_conn_rec *conn,
730                                               conn_rec *c, server_rec *s);
731 /**
732  * Signal the upstream chain that the connection to the backend broke in the
733  * middle of the response. This is done by sending an error bucket with
734  * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
735  * @param r       current request record of client request
736  * @param brigade The brigade that is sent through the output filter chain
737  */
738 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
739                                            apr_bucket_brigade *brigade);
740
741 /**
742  * Transform buckets from one bucket allocator to another one by creating a
743  * transient bucket for each data bucket and let it use the data read from
744  * the old bucket. Metabuckets are transformed by just recreating them.
745  * Attention: Currently only the following bucket types are handled:
746  *
747  * All data buckets
748  * FLUSH
749  * EOS
750  *
751  * If an other bucket type is found its type is logged as a debug message
752  * and APR_EGENERAL is returned.
753  * @param r    current request record of client request. Only used for logging
754  *             purposes
755  * @param from the brigade that contains the buckets to transform
756  * @param to   the brigade that will receive the transformed buckets
757  * @return     APR_SUCCESS if all buckets could be transformed APR_EGENERAL
758  *             otherwise
759  */
760 PROXY_DECLARE(apr_status_t)
761 ap_proxy_buckets_lifetime_transform(request_rec *r, apr_bucket_brigade *from,
762                                         apr_bucket_brigade *to);
763 /**
764  * Return a hash based on the passed string
765  * @param str     string to produce hash from
766  * @param method  hashing method to use
767  * @return        hash as unsigned int
768  */
769
770 typedef enum { PROXY_HASHFUNC_DEFAULT, PROXY_HASHFUNC_APR,  PROXY_HASHFUNC_FNV } proxy_hash_t;
771
772 PROXY_DECLARE(unsigned int)
773 ap_proxy_hashfunc(const char *str, proxy_hash_t method);
774
775 #define PROXY_LBMETHOD "proxylbmethod"
776
777 /* The number of dynamic workers that can be added when reconfiguring.
778  * If this limit is reached you must stop and restart the server.
779  */
780 #define PROXY_DYNAMIC_BALANCER_LIMIT    16
781
782 /**
783  * Calculate maximum number of workers in scoreboard.
784  * @return  number of workers to allocate in the scoreboard
785  */
786 int ap_proxy_lb_workers(void);
787
788 /* For proxy_util */
789 extern module PROXY_DECLARE_DATA proxy_module;
790
791 extern int PROXY_DECLARE_DATA proxy_lb_workers;
792
793 #endif /*MOD_PROXY_H*/
794 /** @} */