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 */
146 } viaopt; /* how to deal with proxy Via: headers */
147 apr_size_t recv_buffer_size;
148 apr_size_t io_buffer_size;
150 apr_interval_time_t timeout;
155 } badopt; /* how to deal with bad headers */
160 } proxy_status; /* Status display options */
161 apr_sockaddr_t *source_address;
162 apr_global_mutex_t *mutex; /* global lock (needed??) */
163 ap_slotmem_instance_t *slot; /* balancers shm data - runtime */
167 int recv_buffer_size_set:1;
168 int io_buffer_size_set:1;
172 int proxy_status_set:1;
173 int source_address_set:1;
178 const char *p; /* The path */
179 ap_regex_t *r; /* Is this a regex? */
182 * ProxyPassReverse and friends are documented as working inside
183 * <Location>. But in fact they never have done in the case of
184 * more than one <Location>, because the server_conf can't see it.
185 * We need to move them to the per-dir config.
186 * Discussed in February 2005:
187 * http://marc.theaimsgroup.com/?l=apache-httpd-dev&m=110726027118798&w=2
189 apr_array_header_t *raliases;
190 apr_array_header_t* cookie_paths;
191 apr_array_header_t* cookie_domains;
192 const apr_strmatch_pattern* cookie_path_str;
193 const apr_strmatch_pattern* cookie_domain_str;
194 signed char p_is_fnmatch; /* Is the path an fnmatch candidate? */
195 signed char interpolate_env;
196 struct proxy_alias *alias;
199 * the following setting masks the error page
200 * returned from the 'proxied server' and just
201 * forwards the status code upwards.
202 * This allows the main server (us) to generate
203 * the error page, (so it will look like a error
204 * returned from the rest of the system
206 int error_override:1;
208 int preserve_host_set:1;
209 int error_override_set:1;
211 int add_forwarded_headers:1;
214 /* if we interpolate env vars per-request, we'll need a per-request
215 * copy of the reverse proxy config
218 apr_array_header_t *raliases;
219 apr_array_header_t* cookie_paths;
220 apr_array_header_t* cookie_domains;
224 conn_rec *connection;
225 request_rec *r; /* Request record of the backend request
226 * that is used over the backend connection. */
227 proxy_worker *worker; /* Connection pool this connection belongs to */
228 apr_pool_t *pool; /* Subpool for hostname and addr data */
229 const char *hostname;
230 apr_sockaddr_t *addr; /* Preparsed remote address info */
231 apr_pool_t *scpool; /* Subpool used for socket and connection data */
232 apr_socket_t *sock; /* Connection socket */
233 void *data; /* per scheme connection data */
234 void *forward; /* opaque forward proxy data */
235 apr_uint32_t flags; /* Connection flags */
238 int close:1; /* Close 'this' connection */
239 int need_flush:1; /* Flag to decide whether we need to flush the
240 * filter chain or not */
241 int inreslist:1; /* connection in apr_reslist? */
245 float cache_completion; /* completion percentage */
246 int content_length; /* length of the content */
249 /* Connection pool */
250 struct proxy_conn_pool {
251 apr_pool_t *pool; /* The pool used in constructor and destructor calls */
252 apr_sockaddr_t *addr; /* Preparsed remote address info */
253 apr_reslist_t *res; /* Connection resource list */
254 proxy_conn_rec *conn; /* Single connection for prefork mpm */
257 /* Keep below in sync with proxy_util.c! */
258 /* worker status bits */
259 #define PROXY_WORKER_INITIALIZED 0x0001
260 #define PROXY_WORKER_IGNORE_ERRORS 0x0002
261 #define PROXY_WORKER_DRAIN 0x0004
262 #define PROXY_WORKER_IN_SHUTDOWN 0x0010
263 #define PROXY_WORKER_DISABLED 0x0020
264 #define PROXY_WORKER_STOPPED 0x0040
265 #define PROXY_WORKER_IN_ERROR 0x0080
266 #define PROXY_WORKER_HOT_STANDBY 0x0100
267 #define PROXY_WORKER_FREE 0x0200
269 /* worker status flags */
270 #define PROXY_WORKER_INITIALIZED_FLAG 'O'
271 #define PROXY_WORKER_IGNORE_ERRORS_FLAG 'I'
272 #define PROXY_WORKER_DRAIN_FLAG 'N'
273 #define PROXY_WORKER_IN_SHUTDOWN_FLAG 'U'
274 #define PROXY_WORKER_DISABLED_FLAG 'D'
275 #define PROXY_WORKER_STOPPED_FLAG 'S'
276 #define PROXY_WORKER_IN_ERROR_FLAG 'E'
277 #define PROXY_WORKER_HOT_STANDBY_FLAG 'H'
278 #define PROXY_WORKER_FREE_FLAG 'F'
280 #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
281 PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
283 /* NOTE: these check the shared status */
284 #define PROXY_WORKER_IS_INITIALIZED(f) ( (f)->s->status & PROXY_WORKER_INITIALIZED )
286 #define PROXY_WORKER_IS_STANDBY(f) ( (f)->s->status & PROXY_WORKER_HOT_STANDBY )
288 #define PROXY_WORKER_IS_USABLE(f) ( ( !( (f)->s->status & PROXY_WORKER_NOT_USABLE_BITMAP) ) && \
289 PROXY_WORKER_IS_INITIALIZED(f) )
291 /* default worker retry timeout in seconds */
292 #define PROXY_WORKER_DEFAULT_RETRY 60
294 /* Some max char string sizes, for shm fields */
295 #define PROXY_WORKER_MAX_SCHEME_SIZE 16
296 #define PROXY_WORKER_MAX_ROUTE_SIZE 64
297 #define PROXY_WORKER_MAX_NAME_SIZE 96
298 #define PROXY_BALANCER_MAX_STICKY_SIZE 64
300 #define PROXY_STRNCPY(dst, src) apr_cpystrn((dst), (src), sizeof(dst))
302 /* Runtime worker status informations. Shared in scoreboard */
304 char name[PROXY_WORKER_MAX_NAME_SIZE];
305 char scheme[PROXY_WORKER_MAX_SCHEME_SIZE]; /* scheme to use ajp|http|https */
306 char hostname[PROXY_WORKER_MAX_ROUTE_SIZE]; /* remote backend address */
307 char route[PROXY_WORKER_MAX_ROUTE_SIZE]; /* balancing route */
308 char redirect[PROXY_WORKER_MAX_ROUTE_SIZE]; /* temporary balancing redirection route */
309 char flusher[PROXY_WORKER_MAX_SCHEME_SIZE]; /* flush provider used by mod_proxy_fdpass */
310 int lbset; /* load balancer cluster set */
311 int retries; /* number of retries on this worker */
312 int lbstatus; /* Current lbstatus */
313 int lbfactor; /* dynamic lbfactor */
314 int min; /* Desired minimum number of available connections */
315 int smax; /* Soft maximum on the total number of connections */
316 int hmax; /* Hard maximum on the total number of connections */
317 int flush_wait; /* poll wait time in microseconds if flush_auto */
318 int index; /* shm array index */
319 unsigned int hash; /* hash of worker name */
320 unsigned int status; /* worker status bitfield */
325 } flush_packets; /* control AJP flushing */
326 apr_time_t updated; /* timestamp of last update */
327 apr_time_t error_time; /* time of the last error */
328 apr_interval_time_t ttl; /* maximum amount of time in seconds a connection
329 * may be available while exceeding the soft limit */
330 apr_interval_time_t retry; /* retry interval */
331 apr_interval_time_t timeout; /* connection timeout */
332 apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
333 apr_interval_time_t ping_timeout;
334 apr_interval_time_t conn_timeout;
335 apr_size_t recv_buffer_size;
336 apr_size_t io_buffer_size;
337 apr_size_t elected; /* Number of times the worker was elected */
338 apr_size_t busy; /* busyness factor */
340 apr_off_t transferred;/* Number of bytes transferred to remote */
341 apr_off_t read; /* Number of bytes read from remote */
342 void *context; /* general purpose storage */
343 unsigned int keepalive:1;
344 unsigned int disablereuse:1;
345 unsigned int is_address_reusable:1;
346 unsigned int retry_set:1;
347 unsigned int timeout_set:1;
348 unsigned int acquire_set:1;
349 unsigned int ping_timeout_set:1;
350 unsigned int conn_timeout_set:1;
351 unsigned int recv_buffer_size_set:1;
352 unsigned int io_buffer_size_set:1;
353 unsigned int keepalive_set:1;
354 unsigned int disablereuse_set:1;
355 unsigned int was_malloced:1;
356 } proxy_worker_shared;
358 #define ALIGNED_PROXY_WORKER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_worker_shared)))
360 /* Worker configuration */
361 struct proxy_worker {
362 unsigned int hash; /* hash of worker name */
363 unsigned int local_status; /* status of per-process worker */
364 proxy_conn_pool *cp; /* Connection pool to use */
365 proxy_worker_shared *s; /* Shared data */
366 proxy_balancer *balancer; /* which balancer am I in? */
367 apr_thread_mutex_t *mutex; /* Thread lock for updating address cache */
368 void *context; /* general purpose storage */
372 * Time to wait (in microseconds) to find out if more data is currently
373 * available at the backend.
375 #define PROXY_FLUSH_WAIT 10000
378 char sticky_path[PROXY_BALANCER_MAX_STICKY_SIZE]; /* URL sticky session identifier */
379 char sticky[PROXY_BALANCER_MAX_STICKY_SIZE]; /* sticky session identifier */
380 char nonce[APR_UUID_FORMATTED_LENGTH + 1];
381 apr_interval_time_t timeout; /* Timeout for waiting on free connection */
382 apr_time_t wupdated; /* timestamp of last change to workers list */
383 proxy_balancer_method *lbmethod;
384 int max_attempts; /* Number of attempts before failing */
385 int index; /* shm array index */
386 unsigned int sticky_force:1; /* Disable failover for sticky sessions */
387 unsigned int scolonsep:1; /* true if ';' seps sticky session paths */
388 unsigned int max_attempts_set:1;
389 unsigned int was_malloced:1;
390 } proxy_balancer_shared;
392 #define ALIGNED_PROXY_BALANCER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_balancer_shared)))
394 struct proxy_balancer {
395 apr_array_header_t *workers; /* initially configured workers */
396 apr_array_header_t *errstatuses; /* statuses to force members into error */
397 ap_slotmem_instance_t *slot; /* worker shm data - runtime */
398 int growth; /* number of post-config workers can added */
399 int max_workers; /* maximum number of allowed workers */
400 const char *name; /* name of the load balancer */
401 const char *sname; /* filesystem safe balancer name */
402 apr_time_t wupdated; /* timestamp of last change to workers list */
403 apr_global_mutex_t *mutex; /* global lock for updating lb params */
404 void *context; /* general purpose storage */
405 proxy_balancer_shared *s; /* Shared data */
408 struct proxy_balancer_method {
409 const char *name; /* name of the load balancer method*/
410 proxy_worker *(*finder)(proxy_balancer *balancer,
412 void *context; /* general purpose storage */
413 apr_status_t (*reset)(proxy_balancer *balancer, server_rec *s);
414 apr_status_t (*age)(proxy_balancer *balancer, server_rec *s);
415 apr_status_t (*updatelbstatus)(proxy_balancer *balancer, proxy_worker *elected, server_rec *s);
418 #define PROXY_THREAD_LOCK(x) apr_thread_mutex_lock((x)->mutex)
419 #define PROXY_THREAD_UNLOCK(x) apr_thread_mutex_unlock((x)->mutex)
421 #define PROXY_GLOBAL_LOCK(x) apr_global_mutex_lock((x)->mutex)
422 #define PROXY_GLOBAL_UNLOCK(x) apr_global_mutex_unlock((x)->mutex)
426 /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and
427 * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
430 #define PROXY_DECLARE(type) type
431 #define PROXY_DECLARE_NONSTD(type) type
432 #define PROXY_DECLARE_DATA
433 #elif defined(PROXY_DECLARE_STATIC)
434 #define PROXY_DECLARE(type) type __stdcall
435 #define PROXY_DECLARE_NONSTD(type) type
436 #define PROXY_DECLARE_DATA
437 #elif defined(PROXY_DECLARE_EXPORT)
438 #define PROXY_DECLARE(type) __declspec(dllexport) type __stdcall
439 #define PROXY_DECLARE_NONSTD(type) __declspec(dllexport) type
440 #define PROXY_DECLARE_DATA __declspec(dllexport)
442 #define PROXY_DECLARE(type) __declspec(dllimport) type __stdcall
443 #define PROXY_DECLARE_NONSTD(type) __declspec(dllimport) type
444 #define PROXY_DECLARE_DATA __declspec(dllimport)
448 * Hook an optional proxy hook. Unlike static hooks, this uses a macro
449 * instead of a function.
451 #define PROXY_OPTIONAL_HOOK(name,fn,pre,succ,order) \
452 APR_OPTIONAL_HOOK(proxy,name,fn,pre,succ,order)
454 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, scheme_handler, (request_rec *r,
455 proxy_worker *worker, proxy_server_conf *conf, char *url,
456 const char *proxyhost, apr_port_t proxyport))
457 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, canon_handler, (request_rec *r,
460 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
461 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r))
465 * It will return the most suitable worker at the moment
466 * and coresponding balancer.
467 * The url is rewritten from balancer://cluster/uri to scheme://host:port/uri
468 * and then the scheme_handler is called.
471 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
472 proxy_balancer **balancer,
474 proxy_server_conf *conf, char **url))
477 * It is called after request for updating runtime balancer status.
479 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, post_request, (proxy_worker *worker,
480 proxy_balancer *balancer, request_rec *r,
481 proxy_server_conf *conf))
484 * request status hook
485 * It is called after all proxy processing has been done. This gives other
486 * modules a chance to create default content on failure, for example
488 APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, request_status,
489 (int *status, request_rec *r))
493 PROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r);
494 PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
495 PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
496 PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
497 int forcedec, int proxyreq);
498 PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
499 char **passwordp, char **hostp, apr_port_t *port);
500 PROXY_DECLARE(const char *)ap_proxy_date_canon(apr_pool_t *p, const char *x);
501 PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val);
502 PROXY_DECLARE(char *)ap_proxy_removestr(apr_pool_t *pool, const char *list, const char *val);
503 PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x);
504 PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y);
505 PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
506 PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p);
507 PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p);
508 PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p);
509 PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
510 PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
511 PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
512 PROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb, char *buff, size_t bufflen, int *eos);
513 PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key);
514 /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
515 PROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **, const char *, apr_sockaddr_t *, const char *, proxy_server_conf *, request_rec *);
516 PROXY_DECLARE(apr_status_t) ap_proxy_ssl_connection_cleanup(proxy_conn_rec *conn,
518 PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
519 PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
520 PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c);
521 PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
523 /* Header mapping functions, and a typedef of their signature */
524 PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url);
525 PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str);
528 typedef const char *(*ap_proxy_header_reverse_map_fn)(request_rec *,
529 proxy_dir_conf *, const char *);
530 #elif defined(PROXY_DECLARE_STATIC)
531 typedef const char *(__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
532 proxy_dir_conf *, const char *);
533 #elif defined(PROXY_DECLARE_EXPORT)
534 typedef __declspec(dllexport) const char *
535 (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
536 proxy_dir_conf *, const char *);
538 typedef __declspec(dllimport) const char *
539 (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
540 proxy_dir_conf *, const char *);
544 /* Connection pool API */
546 * Get the worker from proxy configuration
547 * @param p memory pool used for finding worker
548 * @param balancer the balancer that the worker belongs to
549 * @param conf current proxy server configuration
550 * @param url url to find the worker from
551 * @return proxy_worker or NULL if not found
553 PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
554 proxy_balancer *balancer,
555 proxy_server_conf *conf,
558 * Define and Allocate space for the worker to proxy configuration
559 * @param p memory pool to allocate worker from
560 * @param worker the new worker
561 * @param balancer the balancer that the worker belongs to
562 * @param conf current proxy server configuration
563 * @param url url containing worker name
564 * @param do_malloc true if shared struct should be malloced
565 * @return error message or NULL if successful (*worker is new worker)
567 PROXY_DECLARE(char *) ap_proxy_define_worker(apr_pool_t *p,
568 proxy_worker **worker,
569 proxy_balancer *balancer,
570 proxy_server_conf *conf,
575 * Share a defined proxy worker via shm
576 * @param worker worker to be shared
577 * @param shm location of shared info
578 * @param i index into shm
579 * @return APR_SUCCESS or error code
581 PROXY_DECLARE(apr_status_t) ap_proxy_share_worker(proxy_worker *worker, proxy_worker_shared *shm, int i);
584 * Initialize the worker by setting up worker connection pool and mutex
585 * @param worker worker to initialize
586 * @param s current server record
587 * @param p memory pool used for mutex and connection pool
588 * @return APR_SUCCESS or error code
590 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
595 * Verifies valid balancer name (eg: balancer://foo)
596 * @param name name to test
597 * @param i number of chars to test; 0 for all.
600 PROXY_DECLARE(int) ap_proxy_valid_balancer_name(char *name, int i);
604 * Get the balancer from proxy configuration
605 * @param p memory pool used for temporary storage while finding balancer
606 * @param conf current proxy server configuration
607 * @param url url to find the worker from; must have balancer:// prefix
608 * @return proxy_balancer or NULL if not found
610 PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
611 proxy_server_conf *conf,
615 * Define and Allocate space for the balancer to proxy configuration
616 * @param p memory pool to allocate balancer from
617 * @param balancer the new balancer
618 * @param conf current proxy server configuration
619 * @param url url containing balancer name
620 * @param do_malloc true if shared struct should be malloced
621 * @return error message or NULL if successfull
623 PROXY_DECLARE(char *) ap_proxy_define_balancer(apr_pool_t *p,
624 proxy_balancer **balancer,
625 proxy_server_conf *conf,
630 * Share a defined proxy balancer via shm
631 * @param balancer balancer to be shared
632 * @param shm location of shared info
633 * @param i index into shm
634 * @return APR_SUCCESS or error code
636 PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,
637 proxy_balancer_shared *shm,
641 * Initialize the balancer as needed
642 * @param balancer balancer to initialize
643 * @param s current server record
644 * @param p memory pool used for mutex and connection pool
645 * @return APR_SUCCESS or error code
647 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balancer,
652 * Get the most suitable worker and/or balancer for the request
653 * @param worker worker used for processing request
654 * @param balancer balancer used for processing request
655 * @param r current request
656 * @param conf current proxy server configuration
657 * @param url request url that balancer can rewrite.
658 * @return OK or HTTP_XXX error
659 * @note It calls balancer pre_request hook if the url starts with balancer://
660 * The balancer then rewrites the url to particular worker, like http://host:port
662 PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
663 proxy_balancer **balancer,
665 proxy_server_conf *conf,
668 * Post request worker and balancer cleanup
669 * @param worker worker used for processing request
670 * @param balancer balancer used for processing request
671 * @param r current request
672 * @param conf current proxy server configuration
673 * @return OK or HTTP_XXX error
674 * @note Whenever the pre_request is called, the post_request has to be
677 PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
678 proxy_balancer *balancer,
680 proxy_server_conf *conf);
683 * Request status function
684 * @param status status of proxy request (result)
685 * @param r the request to obtain the status for
686 * @return OK or DECLINED
688 PROXY_DECLARE(int) ap_proxy_request_status(int *status, request_rec *r);
691 * Determine backend hostname and port
692 * @param p memory pool used for processing
693 * @param r current request
694 * @param conf current proxy server configuration
695 * @param worker worker used for processing request
696 * @param conn proxy connection struct
697 * @param uri processed uri
698 * @param url request url
699 * @param proxyname are we connecting directly or via a proxy
700 * @param proxyport proxy host port
701 * @param server_portstr Via headers server port
702 * @param server_portstr_size size of the server_portstr buffer
703 * @return OK or HTTP_XXX error
705 PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
706 proxy_server_conf *conf,
707 proxy_worker *worker,
708 proxy_conn_rec *conn,
711 const char *proxyname,
712 apr_port_t proxyport,
713 char *server_portstr,
714 int server_portstr_size);
717 * Mark a worker for retry
718 * @param proxy_function calling proxy scheme (http, ajp, ...)
719 * @param worker worker used for retrying
720 * @param s current server record
721 * @return OK if marked for retry, DECLINED otherwise
722 * @note The error status of the worker will cleared if the retry interval has
723 * elapsed since the last error.
725 PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
726 proxy_worker *worker,
730 * Acquire a connection from worker connection pool
731 * @param proxy_function calling proxy scheme (http, ajp, ...)
732 * @param conn acquired connection
733 * @param worker worker used for obtaining connection
734 * @param s current server record
735 * @return OK or HTTP_XXX error
736 * @note If the connection limit has been reached, the function will
737 * block until a connection becomes available or the timeout has
740 PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
741 proxy_conn_rec **conn,
742 proxy_worker *worker,
745 * Release a connection back to worker connection pool
746 * @param proxy_function calling proxy scheme (http, ajp, ...)
747 * @param conn acquired connection
748 * @param s current server record
749 * @return OK or HTTP_XXX error
750 * @note The connection will be closed if conn->close_on_release is set
752 PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
753 proxy_conn_rec *conn,
756 * Make a connection to the backend
757 * @param proxy_function calling proxy scheme (http, ajp, ...)
758 * @param conn acquired connection
759 * @param worker connection worker
760 * @param s current server record
761 * @return OK or HTTP_XXX error
762 * @note In case the socket already exists for conn, just check the link
765 PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
766 proxy_conn_rec *conn,
767 proxy_worker *worker,
770 * Make a connection record for backend connection
771 * @param proxy_function calling proxy scheme (http, ajp, ...)
772 * @param conn acquired connection
773 * @param c client connection record
774 * @param s current server record
775 * @return OK or HTTP_XXX error
776 * @note The function will return immediately if conn->connection
779 PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
780 proxy_conn_rec *conn,
781 conn_rec *c, server_rec *s);
783 * Signal the upstream chain that the connection to the backend broke in the
784 * middle of the response. This is done by sending an error bucket with
785 * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
786 * @param r current request record of client request
787 * @param brigade The brigade that is sent through the output filter chain
789 PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
790 apr_bucket_brigade *brigade);
793 * Transform buckets from one bucket allocator to another one by creating a
794 * transient bucket for each data bucket and let it use the data read from
795 * the old bucket. Metabuckets are transformed by just recreating them.
796 * Attention: Currently only the following bucket types are handled:
802 * If an other bucket type is found its type is logged as a debug message
803 * and APR_EGENERAL is returned.
804 * @param r current request record of client request. Only used for logging
806 * @param from the brigade that contains the buckets to transform
807 * @param to the brigade that will receive the transformed buckets
808 * @return APR_SUCCESS if all buckets could be transformed APR_EGENERAL
811 PROXY_DECLARE(apr_status_t)
812 ap_proxy_buckets_lifetime_transform(request_rec *r, apr_bucket_brigade *from,
813 apr_bucket_brigade *to);
815 * Return a hash based on the passed string
816 * @param str string to produce hash from
817 * @param method hashing method to use
818 * @return hash as unsigned int
821 typedef enum { PROXY_HASHFUNC_DEFAULT, PROXY_HASHFUNC_APR, PROXY_HASHFUNC_FNV } proxy_hash_t;
823 PROXY_DECLARE(unsigned int) ap_proxy_hashfunc(const char *str, proxy_hash_t method);
827 * Set/unset the worker status bitfield depending on flag
829 * @param set set or unset bit
830 * @param w worker to use
831 * @return APR_SUCCESS if valid flag
833 PROXY_DECLARE(apr_status_t) ap_proxy_set_wstatus(char c, int set, proxy_worker *w);
837 * Create readable representation of worker status bitfield
839 * @param w worker to use
840 * @return string representation of status
842 PROXY_DECLARE(char *) ap_proxy_parse_wstatus(apr_pool_t *p, proxy_worker *w);
844 #define PROXY_LBMETHOD "proxylbmethod"
846 /* The number of dynamic workers that can be added when reconfiguring.
847 * If this limit is reached you must stop and restart the server.
849 #define PROXY_DYNAMIC_BALANCER_LIMIT 16
852 * Calculate maximum number of workers in scoreboard.
853 * @return number of workers to allocate in the scoreboard
855 int ap_proxy_lb_workers(void);
858 extern module PROXY_DECLARE_DATA proxy_module;
860 extern int PROXY_DECLARE_DATA proxy_lb_workers;
862 #endif /*MOD_PROXY_H*/