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