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