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