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)
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,
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))
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.
503 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
504 proxy_balancer **balancer,
506 proxy_server_conf *conf, char **url))
509 * It is called after request for updating runtime balancer status.
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))
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
520 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, request_status,
521 (int *status, request_rec *r))
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,
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);
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);
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 *);
559 typedef __declspec(dllimport) const char *
560 (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
561 proxy_dir_conf *, const char *);
565 /* Connection pool API */
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
574 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
575 proxy_balancer *balancer,
576 proxy_server_conf *conf,
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)
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,
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
602 PROXY_DECLARE(apr_status_t) ap_proxy_share_worker(proxy_worker *worker,
603 proxy_worker_shared *shm,
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
613 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
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.
623 PROXY_DECLARE(int) ap_proxy_valid_balancer_name(char *name, int i);
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
634 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
635 proxy_server_conf *conf,
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
646 PROXY_DECLARE(char *) ap_proxy_update_balancer(apr_pool_t *p,
647 proxy_balancer *balancer,
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
660 PROXY_DECLARE(char *) ap_proxy_define_balancer(apr_pool_t *p,
661 proxy_balancer **balancer,
662 proxy_server_conf *conf,
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
674 PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,
675 proxy_balancer_shared *shm,
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
685 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balancer,
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
700 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
701 proxy_balancer **balancer,
703 proxy_server_conf *conf,
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
715 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
716 proxy_balancer *balancer,
718 proxy_server_conf *conf);
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
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,
741 const char *proxyname,
742 apr_port_t proxyport,
743 char *server_portstr,
744 int server_portstr_size);
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.
755 APR_DECLARE_OPTIONAL_FN(int, ap_proxy_retry_worker,
756 (const char *proxy_function, proxy_worker *worker, server_rec *s));
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
769 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
770 proxy_conn_rec **conn,
771 proxy_worker *worker,
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
781 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
782 proxy_conn_rec *conn,
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
794 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
795 proxy_conn_rec *conn,
796 proxy_worker *worker,
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
808 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
809 proxy_conn_rec *conn,
810 conn_rec *c, server_rec *s);
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
818 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
819 apr_bucket_brigade *brigade);
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
828 typedef enum { PROXY_HASHFUNC_DEFAULT, PROXY_HASHFUNC_APR, PROXY_HASHFUNC_FNV } proxy_hash_t;
830 PROXY_DECLARE(unsigned int) ap_proxy_hashfunc(const char *str, proxy_hash_t method);
834 * Set/unset the worker status bitfield depending on flag
836 * @param set set or unset bit
837 * @param w worker to use
838 * @return APR_SUCCESS if valid flag
840 PROXY_DECLARE(apr_status_t) ap_proxy_set_wstatus(char c, int set, proxy_worker *w);
844 * Create readable representation of worker status bitfield
846 * @param w worker to use
847 * @return string representation of status
849 PROXY_DECLARE(char *) ap_proxy_parse_wstatus(apr_pool_t *p, proxy_worker *w);
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
857 * @return APR_SUCCESS if all goes well
859 PROXY_DECLARE(apr_status_t) ap_proxy_sync_balancer(proxy_balancer *b,
861 proxy_server_conf *conf);
865 * Find the matched alias for this request and setup for proxy handler
867 * @param ent proxy_alias record
868 * @param dconf per-dir config or NULL
869 * @return DECLINED, DONE or OK if matched
871 PROXY_DECLARE(int) ap_proxy_trans_match(request_rec *r,
872 struct proxy_alias *ent,
873 proxy_dir_conf *dconf);
875 #define PROXY_LBMETHOD "proxylbmethod"
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.
880 #define PROXY_DYNAMIC_BALANCER_LIMIT 16
883 * Calculate maximum number of workers in scoreboard.
884 * @return number of workers to allocate in the scoreboard
886 int ap_proxy_lb_workers(void);
888 extern module PROXY_DECLARE_DATA proxy_module;
890 #endif /*MOD_PROXY_H*/