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