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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * @brief Proxy Extension Module for Apache
24 * @defgroup MOD_PROXY mod_proxy
25 * @ingroup APACHE_MODS
29 #include "apr_hooks.h"
30 #include "apr_optional.h"
33 #include "apr_strings.h"
34 #include "apr_buckets.h"
36 #include "apr_network_io.h"
37 #include "apr_pools.h"
38 #include "apr_strings.h"
41 #include "apr_strmatch.h"
42 #include "apr_fnmatch.h"
43 #include "apr_reslist.h"
44 #define APR_WANT_STRFUNC
47 #include "util_mutex.h"
48 #include "apr_global_mutex.h"
49 #include "apr_thread_mutex.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"
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"
66 #if APR_HAVE_NETINET_IN_H
67 #include <netinet/in.h>
69 #if APR_HAVE_ARPA_INET_H
70 #include <arpa/inet.h>
73 /* for proxy_canonenc() */
75 enc_path, enc_search, enc_user, enc_fpath, enc_parm
78 #define BALANCER_PREFIX "balancer://"
80 #if APR_CHARSET_EBCDIC
82 #else /*APR_CHARSET_EBCDIC*/
83 #define CRLF "\015\012"
84 #endif /*APR_CHARSET_EBCDIC*/
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.
90 #define DEFAULT_MAX_FORWARDS -1
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;
97 /* static information about a remote proxy */
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 */
107 #define PROXYPASS_NOCANON 0x01
108 #define PROXYPASS_INTERPOLATE 0x02
114 proxy_balancer *balancer; /* only valid for reverse-proxys */
117 struct dirconn_entry {
119 struct in_addr addr, mask;
120 struct apr_sockaddr_t *hostaddr;
121 int (*matcher) (struct dirconn_entry * This, request_rec *r);
124 struct noproxy_entry {
126 struct apr_sockaddr_t *addr;
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 */
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 */
150 } viaopt; /* how to deal with proxy Via: headers */
151 apr_size_t recv_buffer_size;
152 apr_size_t io_buffer_size;
154 apr_interval_time_t timeout;
159 } badopt; /* how to deal with bad headers */
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;
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;
184 const char *p; /* The path */
185 ap_regex_t *r; /* Is this a regex? */
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
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;
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
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;
218 /* if we interpolate env vars per-request, we'll need a per-request
219 * copy of the reverse proxy config
222 apr_array_header_t *raliases;
223 apr_array_header_t* cookie_paths;
224 apr_array_header_t* cookie_domains;
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 */
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? */
249 float cache_completion; /* completion percentage */
250 int content_length; /* length of the content */
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 */
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
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'
284 #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
285 PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
287 /* NOTE: these check the shared status */
288 #define PROXY_WORKER_IS_INITIALIZED(f) ( (f)->s->status & PROXY_WORKER_INITIALIZED )
290 #define PROXY_WORKER_IS_STANDBY(f) ( (f)->s->status & PROXY_WORKER_HOT_STANDBY )
292 #define PROXY_WORKER_IS_USABLE(f) ( ( !( (f)->s->status & PROXY_WORKER_NOT_USABLE_BITMAP) ) && \
293 PROXY_WORKER_IS_INITIALIZED(f) )
295 #define PROXY_WORKER_IS_DRAINING(f) ( (f)->s->status & PROXY_WORKER_DRAIN )
297 /* default worker retry timeout in seconds */
298 #define PROXY_WORKER_DEFAULT_RETRY 60
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
310 #define PROXY_MAX_PROVIDER_NAME_SIZE 16
312 #define PROXY_STRNCPY(dst, src) ap_proxy_strncpy((dst), (src), (sizeof(dst)))
314 #define PROXY_COPY_CONF_PARAMS(w, c) \
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; \
330 /* Runtime worker status informations. Shared in scoreboard */
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 */
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 */
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;
386 #define ALIGNED_PROXY_WORKER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_worker_shared)))
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 */
400 * Time to wait (in microseconds) to find out if more data is currently
401 * available at the backend.
403 #define PROXY_FLUSH_WAIT 10000
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 */
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;
428 #define ALIGNED_PROXY_BALANCER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_balancer_shared)))
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 */
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 */
447 struct proxy_balancer_method {
448 const char *name; /* name of the load balancer method*/
449 proxy_worker *(*finder)(proxy_balancer *balancer,
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);
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)
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)
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
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)
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)
487 * Hook an optional proxy hook. Unlike static hooks, this uses a macro
488 * instead of a function.
490 #define PROXY_OPTIONAL_HOOK(name,fn,pre,succ,order) \
491 APR_OPTIONAL_HOOK(proxy,name,fn,pre,succ,order)
493 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, scheme_handler, (request_rec *r,
494 proxy_worker *worker, proxy_server_conf *conf, char *url,
495 const char *proxyhost, apr_port_t proxyport))
496 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, canon_handler, (request_rec *r,
499 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
500 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r))
504 * It will return the most suitable worker at the moment
505 * and coresponding balancer.
506 * The url is rewritten from balancer://cluster/uri to scheme://host:port/uri
507 * and then the scheme_handler is called.
510 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
511 proxy_balancer **balancer,
513 proxy_server_conf *conf, char **url))
516 * It is called after request for updating runtime balancer status.
518 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, post_request, (proxy_worker *worker,
519 proxy_balancer *balancer, request_rec *r,
520 proxy_server_conf *conf))
523 * request status hook
524 * It is called after all proxy processing has been done. This gives other
525 * modules a chance to create default content on failure, for example
527 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, request_status,
528 (int *status, request_rec *r))
532 PROXY_DECLARE(apr_status_t) ap_proxy_strncpy(char *dst, const char *src, size_t dlen);
533 PROXY_DECLARE(request_rec *) ap_proxy_make_fake_req(conn_rec *c, request_rec *r);
534 PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
535 PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
536 PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
537 int forcedec, int proxyreq);
538 PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
539 char **passwordp, char **hostp, apr_port_t *port);
540 PROXY_DECLARE(const char *)ap_proxy_date_canon(apr_pool_t *p, const char *x);
541 PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val);
542 PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x);
543 PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y);
544 PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
545 PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p);
546 PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p);
547 PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p);
548 PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
549 PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
550 PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
551 PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key);
552 /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
553 PROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **, const char *, apr_sockaddr_t *, const char *, proxy_server_conf *, request_rec *);
554 PROXY_DECLARE(apr_status_t) ap_proxy_ssl_connection_cleanup(proxy_conn_rec *conn,
556 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
557 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
558 PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c);
559 PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
561 /* Header mapping functions, and a typedef of their signature */
562 PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url);
563 PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str);
566 typedef const char *(*ap_proxy_header_reverse_map_fn)(request_rec *,
567 proxy_dir_conf *, const char *);
568 #elif defined(PROXY_DECLARE_STATIC)
569 typedef const char *(__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
570 proxy_dir_conf *, const char *);
571 #elif defined(PROXY_DECLARE_EXPORT)
572 typedef __declspec(dllexport) const char *
573 (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
574 proxy_dir_conf *, const char *);
576 typedef __declspec(dllimport) const char *
577 (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
578 proxy_dir_conf *, const char *);
582 /* Connection pool API */
584 * Get the worker from proxy configuration
585 * @param p memory pool used for finding worker
586 * @param balancer the balancer that the worker belongs to
587 * @param conf current proxy server configuration
588 * @param url url to find the worker from
589 * @return proxy_worker or NULL if not found
591 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
592 proxy_balancer *balancer,
593 proxy_server_conf *conf,
596 * Define and Allocate space for the worker to proxy configuration
597 * @param p memory pool to allocate worker from
598 * @param worker the new worker
599 * @param balancer the balancer that the worker belongs to
600 * @param conf current proxy server configuration
601 * @param url url containing worker name
602 * @param do_malloc true if shared struct should be malloced
603 * @return error message or NULL if successful (*worker is new worker)
605 PROXY_DECLARE(char *) ap_proxy_define_worker(apr_pool_t *p,
606 proxy_worker **worker,
607 proxy_balancer *balancer,
608 proxy_server_conf *conf,
613 * Share a defined proxy worker via shm
614 * @param worker worker to be shared
615 * @param shm location of shared info
616 * @param i index into shm
617 * @return APR_SUCCESS or error code
619 PROXY_DECLARE(apr_status_t) ap_proxy_share_worker(proxy_worker *worker,
620 proxy_worker_shared *shm,
624 * Initialize the worker by setting up worker connection pool and mutex
625 * @param worker worker to initialize
626 * @param s current server record
627 * @param p memory pool used for mutex and connection pool
628 * @return APR_SUCCESS or error code
630 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
635 * Verifies valid balancer name (eg: balancer://foo)
636 * @param name name to test
637 * @param i number of chars to test; 0 for all.
640 PROXY_DECLARE(int) ap_proxy_valid_balancer_name(char *name, int i);
644 * Get the balancer from proxy configuration
645 * @param p memory pool used for temporary storage while finding balancer
646 * @param conf current proxy server configuration
647 * @param url url to find the worker from; must have balancer:// prefix
648 * @param careactive true if we care if the balancer is active or not
649 * @return proxy_balancer or NULL if not found
651 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
652 proxy_server_conf *conf,
657 * Update the balancer's vhost related fields
658 * @param p memory pool used for temporary storage while finding balancer
659 * @param balancer balancer to be updated
660 * @param url url to find vhost info
661 * @return error string or NULL if OK
663 PROXY_DECLARE(char *) ap_proxy_update_balancer(apr_pool_t *p,
664 proxy_balancer *balancer,
668 * Define and Allocate space for the balancer to proxy configuration
669 * @param p memory pool to allocate balancer from
670 * @param balancer the new balancer
671 * @param conf current proxy server configuration
672 * @param url url containing balancer name
673 * @param alias alias/fake-path to this balancer
674 * @param do_malloc true if shared struct should be malloced
675 * @return error message or NULL if successfull
677 PROXY_DECLARE(char *) ap_proxy_define_balancer(apr_pool_t *p,
678 proxy_balancer **balancer,
679 proxy_server_conf *conf,
685 * Share a defined proxy balancer via shm
686 * @param balancer balancer to be shared
687 * @param shm location of shared info
688 * @param i index into shm
689 * @return APR_SUCCESS or error code
691 PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,
692 proxy_balancer_shared *shm,
696 * Initialize the balancer as needed
697 * @param balancer balancer to initialize
698 * @param s current server record
699 * @param p memory pool used for mutex and connection pool
700 * @return APR_SUCCESS or error code
702 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balancer,
707 * Get the most suitable worker and/or balancer for the request
708 * @param worker worker used for processing request
709 * @param balancer balancer used for processing request
710 * @param r current request
711 * @param conf current proxy server configuration
712 * @param url request url that balancer can rewrite.
713 * @return OK or HTTP_XXX error
714 * @note It calls balancer pre_request hook if the url starts with balancer://
715 * The balancer then rewrites the url to particular worker, like http://host:port
717 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
718 proxy_balancer **balancer,
720 proxy_server_conf *conf,
723 * Post request worker and balancer cleanup
724 * @param worker worker used for processing request
725 * @param balancer balancer used for processing request
726 * @param r current request
727 * @param conf current proxy server configuration
728 * @return OK or HTTP_XXX error
729 * @note Whenever the pre_request is called, the post_request has to be
732 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
733 proxy_balancer *balancer,
735 proxy_server_conf *conf);
738 * Determine backend hostname and port
739 * @param p memory pool used for processing
740 * @param r current request
741 * @param conf current proxy server configuration
742 * @param worker worker used for processing request
743 * @param conn proxy connection struct
744 * @param uri processed uri
745 * @param url request url
746 * @param proxyname are we connecting directly or via a proxy
747 * @param proxyport proxy host port
748 * @param server_portstr Via headers server port
749 * @param server_portstr_size size of the server_portstr buffer
750 * @return OK or HTTP_XXX error
752 PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
753 proxy_server_conf *conf,
754 proxy_worker *worker,
755 proxy_conn_rec *conn,
758 const char *proxyname,
759 apr_port_t proxyport,
760 char *server_portstr,
761 int server_portstr_size);
764 * Mark a worker for retry
765 * @param proxy_function calling proxy scheme (http, ajp, ...)
766 * @param worker worker used for retrying
767 * @param s current server record
768 * @return OK if marked for retry, DECLINED otherwise
769 * @note The error status of the worker will cleared if the retry interval has
770 * elapsed since the last error.
772 APR_DECLARE_OPTIONAL_FN(int, ap_proxy_retry_worker,
773 (const char *proxy_function, proxy_worker *worker, server_rec *s));
776 * Acquire a connection from worker connection pool
777 * @param proxy_function calling proxy scheme (http, ajp, ...)
778 * @param conn acquired connection
779 * @param worker worker used for obtaining connection
780 * @param s current server record
781 * @return OK or HTTP_XXX error
782 * @note If the connection limit has been reached, the function will
783 * block until a connection becomes available or the timeout has
786 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
787 proxy_conn_rec **conn,
788 proxy_worker *worker,
791 * Release a connection back to worker connection pool
792 * @param proxy_function calling proxy scheme (http, ajp, ...)
793 * @param conn acquired connection
794 * @param s current server record
795 * @return OK or HTTP_XXX error
796 * @note The connection will be closed if conn->close_on_release is set
798 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
799 proxy_conn_rec *conn,
802 * Make a connection to the backend
803 * @param proxy_function calling proxy scheme (http, ajp, ...)
804 * @param conn acquired connection
805 * @param worker connection worker
806 * @param s current server record
807 * @return OK or HTTP_XXX error
808 * @note In case the socket already exists for conn, just check the link
811 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
812 proxy_conn_rec *conn,
813 proxy_worker *worker,
816 * Make a connection record for backend connection
817 * @param proxy_function calling proxy scheme (http, ajp, ...)
818 * @param conn acquired connection
819 * @param c client connection record
820 * @param s current server record
821 * @return OK or HTTP_XXX error
822 * @note The function will return immediately if conn->connection
825 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
826 proxy_conn_rec *conn,
827 conn_rec *c, server_rec *s);
829 * Signal the upstream chain that the connection to the backend broke in the
830 * middle of the response. This is done by sending an error bucket with
831 * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
832 * @param r current request record of client request
833 * @param brigade The brigade that is sent through the output filter chain
835 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
836 apr_bucket_brigade *brigade);
839 * Transform buckets from one bucket allocator to another one by creating a
840 * transient bucket for each data bucket and let it use the data read from
841 * the old bucket. Metabuckets are transformed by just recreating them.
842 * Attention: Currently only the following bucket types are handled:
848 * If an other bucket type is found its type is logged as a debug message
849 * and APR_EGENERAL is returned.
850 * @param r current request record of client request. Only used for logging
852 * @param from the brigade that contains the buckets to transform
853 * @param to the brigade that will receive the transformed buckets
854 * @return APR_SUCCESS if all buckets could be transformed APR_EGENERAL
857 PROXY_DECLARE(apr_status_t) ap_proxy_buckets_lifetime_transform(request_rec *r,
858 apr_bucket_brigade *from,
859 apr_bucket_brigade *to);
861 * Return a hash based on the passed string
862 * @param str string to produce hash from
863 * @param method hashing method to use
864 * @return hash as unsigned int
867 typedef enum { PROXY_HASHFUNC_DEFAULT, PROXY_HASHFUNC_APR, PROXY_HASHFUNC_FNV } proxy_hash_t;
869 PROXY_DECLARE(unsigned int) ap_proxy_hashfunc(const char *str, proxy_hash_t method);
873 * Set/unset the worker status bitfield depending on flag
875 * @param set set or unset bit
876 * @param w worker to use
877 * @return APR_SUCCESS if valid flag
879 PROXY_DECLARE(apr_status_t) ap_proxy_set_wstatus(char c, int set, proxy_worker *w);
883 * Create readable representation of worker status bitfield
885 * @param w worker to use
886 * @return string representation of status
888 PROXY_DECLARE(char *) ap_proxy_parse_wstatus(apr_pool_t *p, proxy_worker *w);
892 * Sync balancer and workers based on any updates w/i shm
893 * @param b balancer to check/update member list of
894 * @param s server rec
896 * @return APR_SUCCESS if all goes well
898 PROXY_DECLARE(apr_status_t) ap_proxy_sync_balancer(proxy_balancer *b,
900 proxy_server_conf *conf);
904 * Find the matched alias for this request and setup for proxy handler
906 * @param ent proxy_alias record
907 * @param dconf per-dir config or NULL
908 * @return DECLINED, DONE or OK if matched
910 PROXY_DECLARE(int) ap_proxy_trans_match(request_rec *r,
911 struct proxy_alias *ent,
912 proxy_dir_conf *dconf);
914 #define PROXY_LBMETHOD "proxylbmethod"
916 /* The number of dynamic workers that can be added when reconfiguring.
917 * If this limit is reached you must stop and restart the server.
919 #define PROXY_DYNAMIC_BALANCER_LIMIT 16
922 * Calculate maximum number of workers in scoreboard.
923 * @return number of workers to allocate in the scoreboard
925 int ap_proxy_lb_workers(void);
928 extern module PROXY_DECLARE_DATA proxy_module;
930 extern int PROXY_DECLARE_DATA proxy_lb_workers;
931 extern const apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_path;
932 extern const apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_domain;
934 #endif /*MOD_PROXY_H*/