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"
32 #include "apr_strings.h"
33 #include "apr_buckets.h"
35 #include "apr_network_io.h"
36 #include "apr_pools.h"
37 #include "apr_strings.h"
40 #include "apr_strmatch.h"
41 #include "apr_fnmatch.h"
42 #include "apr_reslist.h"
43 #define APR_WANT_STRFUNC
46 #include "util_mutex.h"
47 #include "apr_global_mutex.h"
48 #include "apr_thread_mutex.h"
51 #include "http_config.h"
52 #include "ap_config.h"
53 #include "http_core.h"
54 #include "http_protocol.h"
55 #include "http_request.h"
56 #include "http_vhost.h"
57 #include "http_main.h"
59 #include "http_connection.h"
60 #include "util_filter.h"
61 #include "util_ebcdic.h"
62 #include "ap_provider.h"
63 #include "ap_slotmem.h"
65 #if APR_HAVE_NETINET_IN_H
66 #include <netinet/in.h>
68 #if APR_HAVE_ARPA_INET_H
69 #include <arpa/inet.h>
72 /* for proxy_canonenc() */
74 enc_path, enc_search, enc_user, enc_fpath, enc_parm
77 #define BALANCER_PREFIX "balancer://"
79 #if APR_CHARSET_EBCDIC
81 #else /*APR_CHARSET_EBCDIC*/
82 #define CRLF "\015\012"
83 #endif /*APR_CHARSET_EBCDIC*/
85 /* default Max-Forwards header setting */
86 /* Set this to -1, which complies with RFC2616 by not setting
87 * max-forwards if the client didn't send it to us.
89 #define DEFAULT_MAX_FORWARDS -1
91 /* static information about a remote proxy */
93 const char *scheme; /* the schemes handled by this proxy, or '*' */
94 const char *protocol; /* the scheme used to talk to this proxy */
95 const char *hostname; /* the hostname of this proxy */
96 ap_regex_t *regexp; /* compiled regex (if any) for the remote */
97 int use_regex; /* simple boolean. True if we have a regex pattern */
98 apr_port_t port; /* the port for this proxy */
101 #define PROXYPASS_NOCANON 0x01
102 #define PROXYPASS_INTERPOLATE 0x02
110 struct dirconn_entry {
112 struct in_addr addr, mask;
113 struct apr_sockaddr_t *hostaddr;
114 int (*matcher) (struct dirconn_entry * This, request_rec *r);
117 struct noproxy_entry {
119 struct apr_sockaddr_t *addr;
122 typedef struct proxy_balancer proxy_balancer;
123 typedef struct proxy_worker proxy_worker;
124 typedef struct proxy_conn_pool proxy_conn_pool;
125 typedef struct proxy_balancer_method proxy_balancer_method;
128 apr_array_header_t *proxies;
129 apr_array_header_t *sec_proxy;
130 apr_array_header_t *aliases;
131 apr_array_header_t *noproxies;
132 apr_array_header_t *dirconn;
133 apr_array_header_t *workers; /* non-balancer workers, eg ProxyPass http://example.com */
134 apr_array_header_t *balancers; /* list of balancers @ config time */
135 proxy_worker *forward; /* forward proxy worker */
136 proxy_worker *reverse; /* reverse "module-driven" proxy worker */
137 const char *domain; /* domain name to use in absence of a domain name in the request */
139 apr_pool_t *pool; /* Pool used for allocating this struct */
140 int req; /* true if proxy requests are enabled */
141 int max_balancers; /* maximum number of allowed balancers */
142 int bgrowth; /* number of post-config balancers can added */
148 } viaopt; /* how to deal with proxy Via: headers */
149 apr_size_t recv_buffer_size;
150 apr_size_t io_buffer_size;
152 apr_interval_time_t timeout;
157 } badopt; /* how to deal with bad headers */
162 } proxy_status; /* Status display options */
163 apr_sockaddr_t *source_address;
164 apr_global_mutex_t *mutex; /* global lock (needed??) */
165 ap_slotmem_instance_t *bslot; /* balancers shm data - runtime */
166 ap_slotmem_provider_t *storage;
168 unsigned int req_set:1;
169 unsigned int viaopt_set:1;
170 unsigned int recv_buffer_size_set:1;
171 unsigned int io_buffer_size_set:1;
172 unsigned int maxfwd_set:1;
173 unsigned int timeout_set:1;
174 unsigned int badopt_set:1;
175 unsigned int proxy_status_set:1;
176 unsigned int source_address_set:1;
177 unsigned int bgrowth_set:1;
182 const char *p; /* The path */
183 ap_regex_t *r; /* Is this a regex? */
186 * ProxyPassReverse and friends are documented as working inside
187 * <Location>. But in fact they never have done in the case of
188 * more than one <Location>, because the server_conf can't see it.
189 * We need to move them to the per-dir config.
190 * Discussed in February 2005:
191 * http://marc.theaimsgroup.com/?l=apache-httpd-dev&m=110726027118798&w=2
193 apr_array_header_t *raliases;
194 apr_array_header_t* cookie_paths;
195 apr_array_header_t* cookie_domains;
196 signed char p_is_fnmatch; /* Is the path an fnmatch candidate? */
197 signed char interpolate_env;
198 struct proxy_alias *alias;
201 * the following setting masks the error page
202 * returned from the 'proxied server' and just
203 * forwards the status code upwards.
204 * This allows the main server (us) to generate
205 * the error page, (so it will look like a error
206 * returned from the rest of the system
208 unsigned int error_override:1;
209 unsigned int preserve_host:1;
210 unsigned int preserve_host_set:1;
211 unsigned int error_override_set:1;
212 unsigned int alias_set:1;
213 unsigned int add_forwarded_headers:1;
216 /* if we interpolate env vars per-request, we'll need a per-request
217 * copy of the reverse proxy config
220 apr_array_header_t *raliases;
221 apr_array_header_t* cookie_paths;
222 apr_array_header_t* cookie_domains;
226 conn_rec *connection;
227 request_rec *r; /* Request record of the backend request
228 * that is used over the backend connection. */
229 proxy_worker *worker; /* Connection pool this connection belongs to */
230 apr_pool_t *pool; /* Subpool for hostname and addr data */
231 const char *hostname;
232 apr_sockaddr_t *addr; /* Preparsed remote address info */
233 apr_pool_t *scpool; /* Subpool used for socket and connection data */
234 apr_socket_t *sock; /* Connection socket */
235 void *data; /* per scheme connection data */
236 void *forward; /* opaque forward proxy data */
237 apr_uint32_t flags; /* Connection flags */
239 unsigned int is_ssl:1;
240 unsigned int close:1; /* Close 'this' connection */
241 unsigned int need_flush:1; /* Flag to decide whether we need to flush the
242 * filter chain or not */
243 unsigned int inreslist:1; /* connection in apr_reslist? */
247 float cache_completion; /* completion percentage */
248 int content_length; /* length of the content */
251 /* Connection pool */
252 struct proxy_conn_pool {
253 apr_pool_t *pool; /* The pool used in constructor and destructor calls */
254 apr_sockaddr_t *addr; /* Preparsed remote address info */
255 apr_reslist_t *res; /* Connection resource list */
256 proxy_conn_rec *conn; /* Single connection for prefork mpm */
259 /* Keep below in sync with proxy_util.c! */
260 /* worker status bits */
261 #define PROXY_WORKER_INITIALIZED 0x0001
262 #define PROXY_WORKER_IGNORE_ERRORS 0x0002
263 #define PROXY_WORKER_DRAIN 0x0004
264 #define PROXY_WORKER_IN_SHUTDOWN 0x0010
265 #define PROXY_WORKER_DISABLED 0x0020
266 #define PROXY_WORKER_STOPPED 0x0040
267 #define PROXY_WORKER_IN_ERROR 0x0080
268 #define PROXY_WORKER_HOT_STANDBY 0x0100
269 #define PROXY_WORKER_FREE 0x0200
271 /* worker status flags */
272 #define PROXY_WORKER_INITIALIZED_FLAG 'O'
273 #define PROXY_WORKER_IGNORE_ERRORS_FLAG 'I'
274 #define PROXY_WORKER_DRAIN_FLAG 'N'
275 #define PROXY_WORKER_IN_SHUTDOWN_FLAG 'U'
276 #define PROXY_WORKER_DISABLED_FLAG 'D'
277 #define PROXY_WORKER_STOPPED_FLAG 'S'
278 #define PROXY_WORKER_IN_ERROR_FLAG 'E'
279 #define PROXY_WORKER_HOT_STANDBY_FLAG 'H'
280 #define PROXY_WORKER_FREE_FLAG 'F'
282 #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
283 PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
285 /* NOTE: these check the shared status */
286 #define PROXY_WORKER_IS_INITIALIZED(f) ( (f)->s->status & PROXY_WORKER_INITIALIZED )
288 #define PROXY_WORKER_IS_STANDBY(f) ( (f)->s->status & PROXY_WORKER_HOT_STANDBY )
290 #define PROXY_WORKER_IS_USABLE(f) ( ( !( (f)->s->status & PROXY_WORKER_NOT_USABLE_BITMAP) ) && \
291 PROXY_WORKER_IS_INITIALIZED(f) )
293 #define PROXY_WORKER_IS_DRAINING(f) ( (f)->s->status & PROXY_WORKER_DRAIN )
295 /* default worker retry timeout in seconds */
296 #define PROXY_WORKER_DEFAULT_RETRY 60
298 /* Some max char string sizes, for shm fields */
299 #define PROXY_WORKER_MAX_SCHEME_SIZE 16
300 #define PROXY_WORKER_MAX_ROUTE_SIZE 64
301 #define PROXY_BALANCER_MAX_ROUTE_SIZE PROXY_WORKER_MAX_ROUTE_SIZE
302 #define PROXY_WORKER_MAX_NAME_SIZE 96
303 #define PROXY_BALANCER_MAX_NAME_SIZE PROXY_WORKER_MAX_NAME_SIZE
304 #define PROXY_WORKER_MAX_HOSTNAME_SIZE 64
305 #define PROXY_BALANCER_MAX_HOSTNAME_SIZE PROXY_WORKER_MAX_HOSTNAME_SIZE
306 #define PROXY_BALANCER_MAX_STICKY_SIZE 64
308 #define PROXY_MAX_PROVIDER_NAME_SIZE 16
310 #define PROXY_STRNCPY(dst, src) ap_proxy_strncpy((dst), (src), (sizeof(dst)))
312 #define PROXY_COPY_CONF_PARAMS(w, c) \
314 (w)->s->timeout = (c)->timeout; \
315 (w)->s->timeout_set = (c)->timeout_set; \
316 (w)->s->recv_buffer_size = (c)->recv_buffer_size; \
317 (w)->s->recv_buffer_size_set = (c)->recv_buffer_size_set; \
318 (w)->s->io_buffer_size = (c)->io_buffer_size; \
319 (w)->s->io_buffer_size_set = (c)->io_buffer_size_set; \
323 /* Runtime worker status informations. Shared in scoreboard */
325 char name[PROXY_WORKER_MAX_NAME_SIZE];
326 char scheme[PROXY_WORKER_MAX_SCHEME_SIZE]; /* scheme to use ajp|http|https */
327 char hostname[PROXY_WORKER_MAX_HOSTNAME_SIZE]; /* remote backend address */
328 char route[PROXY_WORKER_MAX_ROUTE_SIZE]; /* balancing route */
329 char redirect[PROXY_WORKER_MAX_ROUTE_SIZE]; /* temporary balancing redirection route */
330 char flusher[PROXY_WORKER_MAX_SCHEME_SIZE]; /* flush provider used by mod_proxy_fdpass */
331 int lbset; /* load balancer cluster set */
332 int retries; /* number of retries on this worker */
333 int lbstatus; /* Current lbstatus */
334 int lbfactor; /* dynamic lbfactor */
335 int min; /* Desired minimum number of available connections */
336 int smax; /* Soft maximum on the total number of connections */
337 int hmax; /* Hard maximum on the total number of connections */
338 int flush_wait; /* poll wait time in microseconds if flush_auto */
339 int index; /* shm array index */
340 unsigned int hash; /* hash of worker name */
341 unsigned int status; /* worker status bitfield */
346 } flush_packets; /* control AJP flushing */
347 apr_time_t updated; /* timestamp of last update */
348 apr_time_t error_time; /* time of the last error */
349 apr_interval_time_t ttl; /* maximum amount of time in seconds a connection
350 * may be available while exceeding the soft limit */
351 apr_interval_time_t retry; /* retry interval */
352 apr_interval_time_t timeout; /* connection timeout */
353 apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
354 apr_interval_time_t ping_timeout;
355 apr_interval_time_t conn_timeout;
356 apr_size_t recv_buffer_size;
357 apr_size_t io_buffer_size;
358 apr_size_t elected; /* Number of times the worker was elected */
359 apr_size_t busy; /* busyness factor */
361 apr_off_t transferred;/* Number of bytes transferred to remote */
362 apr_off_t read; /* Number of bytes read from remote */
363 void *context; /* general purpose storage */
364 unsigned int keepalive:1;
365 unsigned int disablereuse:1;
366 unsigned int is_address_reusable:1;
367 unsigned int retry_set:1;
368 unsigned int timeout_set:1;
369 unsigned int acquire_set:1;
370 unsigned int ping_timeout_set:1;
371 unsigned int conn_timeout_set:1;
372 unsigned int recv_buffer_size_set:1;
373 unsigned int io_buffer_size_set:1;
374 unsigned int keepalive_set:1;
375 unsigned int disablereuse_set:1;
376 unsigned int was_malloced:1;
377 } proxy_worker_shared;
379 #define ALIGNED_PROXY_WORKER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_worker_shared)))
381 /* Worker configuration */
382 struct proxy_worker {
383 unsigned int hash; /* hash of worker name */
384 unsigned int local_status; /* status of per-process worker */
385 proxy_conn_pool *cp; /* Connection pool to use */
386 proxy_worker_shared *s; /* Shared data */
387 proxy_balancer *balancer; /* which balancer am I in? */
388 apr_thread_mutex_t *tmutex; /* Thread lock for updating address cache */
389 void *context; /* general purpose storage */
393 * Time to wait (in microseconds) to find out if more data is currently
394 * available at the backend.
396 #define PROXY_FLUSH_WAIT 10000
399 char sticky_path[PROXY_BALANCER_MAX_STICKY_SIZE]; /* URL sticky session identifier */
400 char sticky[PROXY_BALANCER_MAX_STICKY_SIZE]; /* sticky session identifier */
401 char lbpname[PROXY_MAX_PROVIDER_NAME_SIZE]; /* lbmethod provider name */
402 char nonce[APR_UUID_FORMATTED_LENGTH + 1];
403 char name[PROXY_BALANCER_MAX_NAME_SIZE];
404 char sname[PROXY_BALANCER_MAX_NAME_SIZE];
405 char vpath[PROXY_BALANCER_MAX_ROUTE_SIZE];
406 char vhost[PROXY_BALANCER_MAX_HOSTNAME_SIZE];
407 apr_interval_time_t timeout; /* Timeout for waiting on free connection */
408 apr_time_t wupdated; /* timestamp of last change to workers list */
409 int max_attempts; /* Number of attempts before failing */
410 int index; /* shm array index */
412 unsigned int sticky_force:1; /* Disable failover for sticky sessions */
413 unsigned int scolonsep:1; /* true if ';' seps sticky session paths */
414 unsigned int max_attempts_set:1;
415 unsigned int was_malloced:1;
416 unsigned int need_reset:1;
417 unsigned int vhosted:1;
418 unsigned int inactive:1;
419 } proxy_balancer_shared;
421 #define ALIGNED_PROXY_BALANCER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_balancer_shared)))
423 struct proxy_balancer {
424 apr_array_header_t *workers; /* initially configured workers */
425 apr_array_header_t *errstatuses; /* statuses to force members into error */
426 ap_slotmem_instance_t *wslot; /* worker shm data - runtime */
427 ap_slotmem_provider_t *storage;
428 int growth; /* number of post-config workers can added */
429 int max_workers; /* maximum number of allowed workers */
431 apr_time_t wupdated; /* timestamp of last change to workers list */
432 proxy_balancer_method *lbmethod;
433 apr_global_mutex_t *gmutex; /* global lock for updating list of workers */
434 apr_thread_mutex_t *tmutex; /* Thread lock for updating shm */
435 proxy_server_conf *sconf;
436 void *context; /* general purpose storage */
437 proxy_balancer_shared *s; /* Shared data */
440 struct proxy_balancer_method {
441 const char *name; /* name of the load balancer method*/
442 proxy_worker *(*finder)(proxy_balancer *balancer,
444 void *context; /* general purpose storage */
445 apr_status_t (*reset)(proxy_balancer *balancer, server_rec *s);
446 apr_status_t (*age)(proxy_balancer *balancer, server_rec *s);
447 apr_status_t (*updatelbstatus)(proxy_balancer *balancer, proxy_worker *elected, server_rec *s);
450 #define PROXY_THREAD_LOCK(x) ( (x) && (x)->tmutex ? apr_thread_mutex_lock((x)->tmutex) : APR_SUCCESS)
451 #define PROXY_THREAD_UNLOCK(x) ( (x) && (x)->tmutex ? apr_thread_mutex_unlock((x)->tmutex) : APR_SUCCESS)
453 #define PROXY_GLOBAL_LOCK(x) ( (x) && (x)->gmutex ? apr_global_mutex_lock((x)->gmutex) : APR_SUCCESS)
454 #define PROXY_GLOBAL_UNLOCK(x) ( (x) && (x)->gmutex ? apr_global_mutex_unlock((x)->gmutex) : APR_SUCCESS)
458 /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and
459 * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
462 #define PROXY_DECLARE(type) type
463 #define PROXY_DECLARE_NONSTD(type) type
464 #define PROXY_DECLARE_DATA
465 #elif defined(PROXY_DECLARE_STATIC)
466 #define PROXY_DECLARE(type) type __stdcall
467 #define PROXY_DECLARE_NONSTD(type) type
468 #define PROXY_DECLARE_DATA
469 #elif defined(PROXY_DECLARE_EXPORT)
470 #define PROXY_DECLARE(type) __declspec(dllexport) type __stdcall
471 #define PROXY_DECLARE_NONSTD(type) __declspec(dllexport) type
472 #define PROXY_DECLARE_DATA __declspec(dllexport)
474 #define PROXY_DECLARE(type) __declspec(dllimport) type __stdcall
475 #define PROXY_DECLARE_NONSTD(type) __declspec(dllimport) type
476 #define PROXY_DECLARE_DATA __declspec(dllimport)
480 * Hook an optional proxy hook. Unlike static hooks, this uses a macro
481 * instead of a function.
483 #define PROXY_OPTIONAL_HOOK(name,fn,pre,succ,order) \
484 APR_OPTIONAL_HOOK(proxy,name,fn,pre,succ,order)
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(request_rec *) ap_proxy_make_fake_req(conn_rec *c, request_rec *r);
527 PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
528 PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
529 PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
530 int forcedec, int proxyreq);
531 PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
532 char **passwordp, char **hostp, apr_port_t *port);
533 PROXY_DECLARE(const char *)ap_proxy_date_canon(apr_pool_t *p, const char *x);
534 PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val);
535 PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x);
536 PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y);
537 PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
538 PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p);
539 PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p);
540 PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p);
541 PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
542 PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
543 PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
544 PROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb, char *buff, size_t bufflen, int *eos);
545 PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key);
546 /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
547 PROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **, const char *, apr_sockaddr_t *, const char *, proxy_server_conf *, request_rec *);
548 PROXY_DECLARE(apr_status_t) ap_proxy_ssl_connection_cleanup(proxy_conn_rec *conn,
550 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
551 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
552 PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c);
553 PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
555 /* Header mapping functions, and a typedef of their signature */
556 PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url);
557 PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str);
560 typedef const char *(*ap_proxy_header_reverse_map_fn)(request_rec *,
561 proxy_dir_conf *, const char *);
562 #elif defined(PROXY_DECLARE_STATIC)
563 typedef const char *(__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
564 proxy_dir_conf *, const char *);
565 #elif defined(PROXY_DECLARE_EXPORT)
566 typedef __declspec(dllexport) const char *
567 (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
568 proxy_dir_conf *, const char *);
570 typedef __declspec(dllimport) const char *
571 (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
572 proxy_dir_conf *, const char *);
576 /* Connection pool API */
578 * Get the worker from proxy configuration
579 * @param p memory pool used for finding worker
580 * @param balancer the balancer that the worker belongs to
581 * @param conf current proxy server configuration
582 * @param url url to find the worker from
583 * @return proxy_worker or NULL if not found
585 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
586 proxy_balancer *balancer,
587 proxy_server_conf *conf,
590 * Define and Allocate space for the worker to proxy configuration
591 * @param p memory pool to allocate worker from
592 * @param worker the new worker
593 * @param balancer the balancer that the worker belongs to
594 * @param conf current proxy server configuration
595 * @param url url containing worker name
596 * @param do_malloc true if shared struct should be malloced
597 * @return error message or NULL if successful (*worker is new worker)
599 PROXY_DECLARE(char *) ap_proxy_define_worker(apr_pool_t *p,
600 proxy_worker **worker,
601 proxy_balancer *balancer,
602 proxy_server_conf *conf,
607 * Share a defined proxy worker via shm
608 * @param worker worker to be shared
609 * @param shm location of shared info
610 * @param i index into shm
611 * @return APR_SUCCESS or error code
613 PROXY_DECLARE(apr_status_t) ap_proxy_share_worker(proxy_worker *worker,
614 proxy_worker_shared *shm,
618 * Initialize the worker by setting up worker connection pool and mutex
619 * @param worker worker to initialize
620 * @param s current server record
621 * @param p memory pool used for mutex and connection pool
622 * @return APR_SUCCESS or error code
624 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
629 * Verifies valid balancer name (eg: balancer://foo)
630 * @param name name to test
631 * @param i number of chars to test; 0 for all.
634 PROXY_DECLARE(int) ap_proxy_valid_balancer_name(char *name, int i);
638 * Get the balancer from proxy configuration
639 * @param p memory pool used for temporary storage while finding balancer
640 * @param conf current proxy server configuration
641 * @param url url to find the worker from; must have balancer:// prefix
642 * @return proxy_balancer or NULL if not found
644 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
645 proxy_server_conf *conf,
649 * Update the balancer's vhost related fields
650 * @param p memory pool used for temporary storage while finding balancer
651 * @param balancer balancer to be updated
652 * @param url url to find vhost info
653 * @return error string or NULL if OK
655 PROXY_DECLARE(char *) ap_proxy_update_balancer(apr_pool_t *p,
656 proxy_balancer *balancer,
660 * Define and Allocate space for the balancer to proxy configuration
661 * @param p memory pool to allocate balancer from
662 * @param balancer the new balancer
663 * @param conf current proxy server configuration
664 * @param url url containing balancer name
665 * @param alias alias/fake-path to this balancer
666 * @param do_malloc true if shared struct should be malloced
667 * @return error message or NULL if successfull
669 PROXY_DECLARE(char *) ap_proxy_define_balancer(apr_pool_t *p,
670 proxy_balancer **balancer,
671 proxy_server_conf *conf,
677 * Share a defined proxy balancer via shm
678 * @param balancer balancer to be shared
679 * @param shm location of shared info
680 * @param i index into shm
681 * @return APR_SUCCESS or error code
683 PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,
684 proxy_balancer_shared *shm,
688 * Initialize the balancer as needed
689 * @param balancer balancer to initialize
690 * @param s current server record
691 * @param p memory pool used for mutex and connection pool
692 * @return APR_SUCCESS or error code
694 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balancer,
699 * Get the most suitable worker and/or balancer for the request
700 * @param worker worker used for processing request
701 * @param balancer balancer used for processing request
702 * @param r current request
703 * @param conf current proxy server configuration
704 * @param url request url that balancer can rewrite.
705 * @return OK or HTTP_XXX error
706 * @note It calls balancer pre_request hook if the url starts with balancer://
707 * The balancer then rewrites the url to particular worker, like http://host:port
709 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
710 proxy_balancer **balancer,
712 proxy_server_conf *conf,
715 * Post request worker and balancer cleanup
716 * @param worker worker used for processing request
717 * @param balancer balancer used for processing request
718 * @param r current request
719 * @param conf current proxy server configuration
720 * @return OK or HTTP_XXX error
721 * @note Whenever the pre_request is called, the post_request has to be
724 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
725 proxy_balancer *balancer,
727 proxy_server_conf *conf);
730 * Determine backend hostname and port
731 * @param p memory pool used for processing
732 * @param r current request
733 * @param conf current proxy server configuration
734 * @param worker worker used for processing request
735 * @param conn proxy connection struct
736 * @param uri processed uri
737 * @param url request url
738 * @param proxyname are we connecting directly or via a proxy
739 * @param proxyport proxy host port
740 * @param server_portstr Via headers server port
741 * @param server_portstr_size size of the server_portstr buffer
742 * @return OK or HTTP_XXX error
744 PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
745 proxy_server_conf *conf,
746 proxy_worker *worker,
747 proxy_conn_rec *conn,
750 const char *proxyname,
751 apr_port_t proxyport,
752 char *server_portstr,
753 int server_portstr_size);
756 * Mark a worker for retry
757 * @param proxy_function calling proxy scheme (http, ajp, ...)
758 * @param worker worker used for retrying
759 * @param s current server record
760 * @return OK if marked for retry, DECLINED otherwise
761 * @note The error status of the worker will cleared if the retry interval has
762 * elapsed since the last error.
764 PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
765 proxy_worker *worker,
769 * Acquire a connection from worker connection pool
770 * @param proxy_function calling proxy scheme (http, ajp, ...)
771 * @param conn acquired connection
772 * @param worker worker used for obtaining connection
773 * @param s current server record
774 * @return OK or HTTP_XXX error
775 * @note If the connection limit has been reached, the function will
776 * block until a connection becomes available or the timeout has
779 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
780 proxy_conn_rec **conn,
781 proxy_worker *worker,
784 * Release a connection back to worker connection pool
785 * @param proxy_function calling proxy scheme (http, ajp, ...)
786 * @param conn acquired connection
787 * @param s current server record
788 * @return OK or HTTP_XXX error
789 * @note The connection will be closed if conn->close_on_release is set
791 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
792 proxy_conn_rec *conn,
795 * Make a connection to the backend
796 * @param proxy_function calling proxy scheme (http, ajp, ...)
797 * @param conn acquired connection
798 * @param worker connection worker
799 * @param s current server record
800 * @return OK or HTTP_XXX error
801 * @note In case the socket already exists for conn, just check the link
804 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
805 proxy_conn_rec *conn,
806 proxy_worker *worker,
809 * Make a connection record for backend connection
810 * @param proxy_function calling proxy scheme (http, ajp, ...)
811 * @param conn acquired connection
812 * @param c client connection record
813 * @param s current server record
814 * @return OK or HTTP_XXX error
815 * @note The function will return immediately if conn->connection
818 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
819 proxy_conn_rec *conn,
820 conn_rec *c, server_rec *s);
822 * Signal the upstream chain that the connection to the backend broke in the
823 * middle of the response. This is done by sending an error bucket with
824 * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
825 * @param r current request record of client request
826 * @param brigade The brigade that is sent through the output filter chain
828 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
829 apr_bucket_brigade *brigade);
832 * Transform buckets from one bucket allocator to another one by creating a
833 * transient bucket for each data bucket and let it use the data read from
834 * the old bucket. Metabuckets are transformed by just recreating them.
835 * Attention: Currently only the following bucket types are handled:
841 * If an other bucket type is found its type is logged as a debug message
842 * and APR_EGENERAL is returned.
843 * @param r current request record of client request. Only used for logging
845 * @param from the brigade that contains the buckets to transform
846 * @param to the brigade that will receive the transformed buckets
847 * @return APR_SUCCESS if all buckets could be transformed APR_EGENERAL
850 PROXY_DECLARE(apr_status_t) ap_proxy_buckets_lifetime_transform(request_rec *r,
851 apr_bucket_brigade *from,
852 apr_bucket_brigade *to);
854 * Return a hash based on the passed string
855 * @param str string to produce hash from
856 * @param method hashing method to use
857 * @return hash as unsigned int
860 typedef enum { PROXY_HASHFUNC_DEFAULT, PROXY_HASHFUNC_APR, PROXY_HASHFUNC_FNV } proxy_hash_t;
862 PROXY_DECLARE(unsigned int) ap_proxy_hashfunc(const char *str, proxy_hash_t method);
866 * Set/unset the worker status bitfield depending on flag
868 * @param set set or unset bit
869 * @param w worker to use
870 * @return APR_SUCCESS if valid flag
872 PROXY_DECLARE(apr_status_t) ap_proxy_set_wstatus(char c, int set, proxy_worker *w);
876 * Create readable representation of worker status bitfield
878 * @param w worker to use
879 * @return string representation of status
881 PROXY_DECLARE(char *) ap_proxy_parse_wstatus(apr_pool_t *p, proxy_worker *w);
885 * Sync balancer and workers based on any updates w/i shm
886 * @param b balancer to check/update member list of
887 * @param s server rec
889 * @return APR_SUCCESS if all goes well
891 PROXY_DECLARE(apr_status_t) ap_proxy_sync_balancer(proxy_balancer *b,
893 proxy_server_conf *conf);
897 * Find the matched alias for this request and setup for proxy handler
899 * @param ent proxy_alias record
900 * @param dconf per-dir config or NULL
901 * @return DECLINED, DONE or OK if matched
903 PROXY_DECLARE(int) ap_proxy_trans_match(request_rec *r,
904 struct proxy_alias *ent,
905 proxy_dir_conf *dconf);
907 #define PROXY_LBMETHOD "proxylbmethod"
909 /* The number of dynamic workers that can be added when reconfiguring.
910 * If this limit is reached you must stop and restart the server.
912 #define PROXY_DYNAMIC_BALANCER_LIMIT 16
915 * Calculate maximum number of workers in scoreboard.
916 * @return number of workers to allocate in the scoreboard
918 int ap_proxy_lb_workers(void);
921 extern module PROXY_DECLARE_DATA proxy_module;
923 extern int PROXY_DECLARE_DATA proxy_lb_workers;
924 extern const apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_path;
925 extern const apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_domain;
927 #endif /*MOD_PROXY_H*/