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