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