mod_proxy.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MOD_PROXY_H
  17. #define MOD_PROXY_H
  18. /**
  19. * @file mod_proxy.h
  20. * @brief Proxy Extension Module for Apache
  21. *
  22. * @defgroup MOD_PROXY mod_proxy
  23. * @ingroup APACHE_MODS
  24. * @{
  25. */
  26. #include "apr_hooks.h"
  27. #include "apr_optional.h"
  28. #include "apr.h"
  29. #include "apr_lib.h"
  30. #include "apr_strings.h"
  31. #include "apr_buckets.h"
  32. #include "apr_md5.h"
  33. #include "apr_network_io.h"
  34. #include "apr_pools.h"
  35. #include "apr_strings.h"
  36. #include "apr_uri.h"
  37. #include "apr_date.h"
  38. #include "apr_strmatch.h"
  39. #include "apr_fnmatch.h"
  40. #include "apr_reslist.h"
  41. #define APR_WANT_STRFUNC
  42. #include "apr_want.h"
  43. #include "apr_uuid.h"
  44. #include "util_mutex.h"
  45. #include "apr_global_mutex.h"
  46. #include "apr_thread_mutex.h"
  47. #include "httpd.h"
  48. #include "http_config.h"
  49. #include "ap_config.h"
  50. #include "http_core.h"
  51. #include "http_protocol.h"
  52. #include "http_request.h"
  53. #include "http_vhost.h"
  54. #include "http_main.h"
  55. #include "http_log.h"
  56. #include "http_connection.h"
  57. #include "util_filter.h"
  58. #include "util_ebcdic.h"
  59. #include "ap_provider.h"
  60. #include "ap_slotmem.h"
  61. #if APR_HAVE_NETINET_IN_H
  62. #include <netinet/in.h>
  63. #endif
  64. #if APR_HAVE_ARPA_INET_H
  65. #include <arpa/inet.h>
  66. #endif
  67. /* for proxy_canonenc() */
  68. enum enctype {
  69. enc_path, enc_search, enc_user, enc_fpath, enc_parm
  70. };
  71. typedef enum {
  72. NONE, TCP, OPTIONS, HEAD, GET, CPING, PROVIDER, EOT
  73. } hcmethod_t;
  74. typedef struct {
  75. hcmethod_t method;
  76. char *name;
  77. int implemented;
  78. } proxy_hcmethods_t;
  79. typedef struct {
  80. unsigned int bit;
  81. char flag;
  82. const char *name;
  83. } proxy_wstat_t;
  84. #define BALANCER_PREFIX "balancer://"
  85. #if APR_CHARSET_EBCDIC
  86. #define CRLF "\r\n"
  87. #else /*APR_CHARSET_EBCDIC*/
  88. #define CRLF "\015\012"
  89. #endif /*APR_CHARSET_EBCDIC*/
  90. /* default Max-Forwards header setting */
  91. /* Set this to -1, which complies with RFC2616 by not setting
  92. * max-forwards if the client didn't send it to us.
  93. */
  94. #define DEFAULT_MAX_FORWARDS -1
  95. typedef struct proxy_balancer proxy_balancer;
  96. typedef struct proxy_worker proxy_worker;
  97. typedef struct proxy_conn_pool proxy_conn_pool;
  98. typedef struct proxy_balancer_method proxy_balancer_method;
  99. /* static information about a remote proxy */
  100. struct proxy_remote {
  101. const char *scheme; /* the schemes handled by this proxy, or '*' */
  102. const char *protocol; /* the scheme used to talk to this proxy */
  103. const char *hostname; /* the hostname of this proxy */
  104. ap_regex_t *regexp; /* compiled regex (if any) for the remote */
  105. int use_regex; /* simple boolean. True if we have a regex pattern */
  106. apr_port_t port; /* the port for this proxy */
  107. };
  108. #define PROXYPASS_NOCANON 0x01
  109. #define PROXYPASS_INTERPOLATE 0x02
  110. #define PROXYPASS_NOQUERY 0x04
  111. struct proxy_alias {
  112. const char *real;
  113. const char *fake;
  114. ap_regex_t *regex;
  115. unsigned int flags;
  116. proxy_balancer *balancer; /* only valid for reverse-proxys */
  117. };
  118. struct dirconn_entry {
  119. char *name;
  120. struct in_addr addr, mask;
  121. struct apr_sockaddr_t *hostaddr;
  122. int (*matcher) (struct dirconn_entry * This, request_rec *r);
  123. };
  124. struct noproxy_entry {
  125. const char *name;
  126. struct apr_sockaddr_t *addr;
  127. };
  128. typedef struct {
  129. apr_array_header_t *proxies;
  130. apr_array_header_t *sec_proxy;
  131. apr_array_header_t *aliases;
  132. apr_array_header_t *noproxies;
  133. apr_array_header_t *dirconn;
  134. apr_array_header_t *workers; /* non-balancer workers, eg ProxyPass http://example.com */
  135. apr_array_header_t *balancers; /* list of balancers @ config time */
  136. proxy_worker *forward; /* forward proxy worker */
  137. proxy_worker *reverse; /* reverse "module-driven" proxy worker */
  138. const char *domain; /* domain name to use in absence of a domain name in the request */
  139. const char *id;
  140. apr_pool_t *pool; /* Pool used for allocating this struct's elements */
  141. int req; /* true if proxy requests are enabled */
  142. int max_balancers; /* maximum number of allowed balancers */
  143. int bgrowth; /* number of post-config balancers can added */
  144. enum {
  145. via_off,
  146. via_on,
  147. via_block,
  148. via_full
  149. } viaopt; /* how to deal with proxy Via: headers */
  150. apr_size_t recv_buffer_size;
  151. apr_size_t io_buffer_size;
  152. long maxfwd;
  153. apr_interval_time_t timeout;
  154. enum {
  155. bad_error,
  156. bad_ignore,
  157. bad_body
  158. } badopt; /* how to deal with bad headers */
  159. enum {
  160. status_off,
  161. status_on,
  162. status_full
  163. } proxy_status; /* Status display options */
  164. apr_sockaddr_t *source_address;
  165. apr_global_mutex_t *mutex; /* global lock, for pool, etc */
  166. ap_slotmem_instance_t *bslot; /* balancers shm data - runtime */
  167. 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;
  178. unsigned int bal_persist:1;
  179. unsigned int inherit:1;
  180. unsigned int inherit_set:1;
  181. unsigned int ppinherit:1;
  182. unsigned int ppinherit_set:1;
  183. } proxy_server_conf;
  184. typedef struct {
  185. const char *p; /* The path */
  186. ap_regex_t *r; /* Is this a regex? */
  187. /* FIXME
  188. * ProxyPassReverse and friends are documented as working inside
  189. * <Location>. But in fact they never have done in the case of
  190. * more than one <Location>, because the server_conf can't see it.
  191. * We need to move them to the per-dir config.
  192. * Discussed in February 2005:
  193. * http://marc.theaimsgroup.com/?l=apache-httpd-dev&m=110726027118798&w=2
  194. */
  195. apr_array_header_t *raliases;
  196. apr_array_header_t* cookie_paths;
  197. apr_array_header_t* cookie_domains;
  198. signed char p_is_fnmatch; /* Is the path an fnmatch candidate? */
  199. signed char interpolate_env;
  200. struct proxy_alias *alias;
  201. /**
  202. * the following setting masks the error page
  203. * returned from the 'proxied server' and just
  204. * forwards the status code upwards.
  205. * This allows the main server (us) to generate
  206. * the error page, (so it will look like a error
  207. * returned from the rest of the system
  208. */
  209. unsigned int error_override:1;
  210. unsigned int preserve_host:1;
  211. unsigned int preserve_host_set:1;
  212. unsigned int error_override_set:1;
  213. unsigned int alias_set:1;
  214. unsigned int add_forwarded_headers:1;
  215. unsigned int add_forwarded_headers_set:1;
  216. /** Named back references */
  217. apr_array_header_t *refs;
  218. unsigned int forward_100_continue:1;
  219. unsigned int forward_100_continue_set:1;
  220. } proxy_dir_conf;
  221. /* if we interpolate env vars per-request, we'll need a per-request
  222. * copy of the reverse proxy config
  223. */
  224. typedef struct {
  225. apr_array_header_t *raliases;
  226. apr_array_header_t* cookie_paths;
  227. apr_array_header_t* cookie_domains;
  228. } proxy_req_conf;
  229. typedef struct {
  230. conn_rec *connection;
  231. request_rec *r; /* Request record of the backend request
  232. * that is used over the backend connection. */
  233. proxy_worker *worker; /* Connection pool this connection belongs to */
  234. apr_pool_t *pool; /* Subpool for hostname and addr data */
  235. const char *hostname;
  236. apr_sockaddr_t *addr; /* Preparsed remote address info */
  237. apr_pool_t *scpool; /* Subpool used for socket and connection data */
  238. apr_socket_t *sock; /* Connection socket */
  239. void *data; /* per scheme connection data */
  240. void *forward; /* opaque forward proxy data */
  241. apr_uint32_t flags; /* Connection flags */
  242. apr_port_t port;
  243. unsigned int is_ssl:1;
  244. unsigned int close:1; /* Close 'this' connection */
  245. unsigned int need_flush:1; /* Flag to decide whether we need to flush the
  246. * filter chain or not */
  247. unsigned int inreslist:1; /* connection in apr_reslist? */
  248. const char *uds_path; /* Unix domain socket path */
  249. const char *ssl_hostname;/* Hostname (SNI) in use by SSL connection */
  250. apr_bucket_brigade *tmp_bb;/* Temporary brigade created with the connection
  251. * and its scpool/bucket_alloc (NULL before),
  252. * must be left cleaned when used (locally).
  253. */
  254. } proxy_conn_rec;
  255. typedef struct {
  256. float cache_completion; /* completion percentage */
  257. int content_length; /* length of the content */
  258. } proxy_completion;
  259. /* Connection pool */
  260. struct proxy_conn_pool {
  261. apr_pool_t *pool; /* The pool used in constructor and destructor calls */
  262. apr_sockaddr_t *addr; /* Preparsed remote address info */
  263. apr_reslist_t *res; /* Connection resource list */
  264. proxy_conn_rec *conn; /* Single connection for prefork mpm */
  265. };
  266. /* worker status bits */
  267. /*
  268. * NOTE: Keep up-to-date w/ proxy_wstat_tbl[]
  269. * in mod_proxy.c !
  270. */
  271. #define PROXY_WORKER_INITIALIZED 0x0001
  272. #define PROXY_WORKER_IGNORE_ERRORS 0x0002
  273. #define PROXY_WORKER_DRAIN 0x0004
  274. #define PROXY_WORKER_GENERIC 0x0008
  275. #define PROXY_WORKER_IN_SHUTDOWN 0x0010
  276. #define PROXY_WORKER_DISABLED 0x0020
  277. #define PROXY_WORKER_STOPPED 0x0040
  278. #define PROXY_WORKER_IN_ERROR 0x0080
  279. #define PROXY_WORKER_HOT_STANDBY 0x0100
  280. #define PROXY_WORKER_FREE 0x0200
  281. #define PROXY_WORKER_HC_FAIL 0x0400
  282. #define PROXY_WORKER_HOT_SPARE 0x0800
  283. /* worker status flags */
  284. #define PROXY_WORKER_INITIALIZED_FLAG 'O'
  285. #define PROXY_WORKER_IGNORE_ERRORS_FLAG 'I'
  286. #define PROXY_WORKER_DRAIN_FLAG 'N'
  287. #define PROXY_WORKER_GENERIC_FLAG 'G'
  288. #define PROXY_WORKER_IN_SHUTDOWN_FLAG 'U'
  289. #define PROXY_WORKER_DISABLED_FLAG 'D'
  290. #define PROXY_WORKER_STOPPED_FLAG 'S'
  291. #define PROXY_WORKER_IN_ERROR_FLAG 'E'
  292. #define PROXY_WORKER_HOT_STANDBY_FLAG 'H'
  293. #define PROXY_WORKER_FREE_FLAG 'F'
  294. #define PROXY_WORKER_HC_FAIL_FLAG 'C'
  295. #define PROXY_WORKER_HOT_SPARE_FLAG 'R'
  296. #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
  297. PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR | \
  298. PROXY_WORKER_HC_FAIL )
  299. /* NOTE: these check the shared status */
  300. #define PROXY_WORKER_IS_INITIALIZED(f) ( (f)->s->status & PROXY_WORKER_INITIALIZED )
  301. #define PROXY_WORKER_IS_STANDBY(f) ( (f)->s->status & PROXY_WORKER_HOT_STANDBY )
  302. #define PROXY_WORKER_IS_SPARE(f) ( (f)->s->status & PROXY_WORKER_HOT_SPARE )
  303. #define PROXY_WORKER_IS_USABLE(f) ( ( !( (f)->s->status & PROXY_WORKER_NOT_USABLE_BITMAP) ) && \
  304. PROXY_WORKER_IS_INITIALIZED(f) )
  305. #define PROXY_WORKER_IS_DRAINING(f) ( (f)->s->status & PROXY_WORKER_DRAIN )
  306. #define PROXY_WORKER_IS_GENERIC(f) ( (f)->s->status & PROXY_WORKER_GENERIC )
  307. #define PROXY_WORKER_IS_HCFAILED(f) ( (f)->s->status & PROXY_WORKER_HC_FAIL )
  308. #define PROXY_WORKER_IS(f, b) ( (f)->s->status & (b) )
  309. /* default worker retry timeout in seconds */
  310. #define PROXY_WORKER_DEFAULT_RETRY 60
  311. /* Some max char string sizes, for shm fields */
  312. #define PROXY_WORKER_MAX_SCHEME_SIZE 16
  313. #define PROXY_WORKER_MAX_ROUTE_SIZE 64
  314. #define PROXY_BALANCER_MAX_ROUTE_SIZE PROXY_WORKER_MAX_ROUTE_SIZE
  315. #define PROXY_WORKER_MAX_NAME_SIZE 96
  316. #define PROXY_BALANCER_MAX_NAME_SIZE PROXY_WORKER_MAX_NAME_SIZE
  317. #define PROXY_WORKER_MAX_HOSTNAME_SIZE 64
  318. #define PROXY_BALANCER_MAX_HOSTNAME_SIZE PROXY_WORKER_MAX_HOSTNAME_SIZE
  319. #define PROXY_BALANCER_MAX_STICKY_SIZE 64
  320. #define PROXY_RFC1035_HOSTNAME_SIZE 256
  321. /* RFC-1035 mentions limits of 255 for host-names and 253 for domain-names,
  322. * dotted together(?) this would fit the below size (+ trailing NUL).
  323. */
  324. #define PROXY_WORKER_RFC1035_NAME_SIZE 512
  325. #define PROXY_MAX_PROVIDER_NAME_SIZE 16
  326. #define PROXY_STRNCPY(dst, src) ap_proxy_strncpy((dst), (src), (sizeof(dst)))
  327. #define PROXY_COPY_CONF_PARAMS(w, c) \
  328. do { \
  329. (w)->s->timeout = (c)->timeout; \
  330. (w)->s->timeout_set = (c)->timeout_set; \
  331. (w)->s->recv_buffer_size = (c)->recv_buffer_size; \
  332. (w)->s->recv_buffer_size_set = (c)->recv_buffer_size_set; \
  333. (w)->s->io_buffer_size = (c)->io_buffer_size; \
  334. (w)->s->io_buffer_size_set = (c)->io_buffer_size_set; \
  335. } while (0)
  336. #define PROXY_DO_100_CONTINUE(w, r) \
  337. ((w)->s->ping_timeout_set \
  338. && (PROXYREQ_REVERSE == (r)->proxyreq) \
  339. && !(apr_table_get((r)->subprocess_env, "force-proxy-request-1.0")) \
  340. && ap_request_has_body((r)))
  341. /* use 2 hashes */
  342. typedef struct {
  343. unsigned int def;
  344. unsigned int fnv;
  345. } proxy_hashes ;
  346. /* Runtime worker status information. Shared in scoreboard */
  347. /* The addition of member uds_path in 2.4.7 was an incompatible API change. */
  348. typedef struct {
  349. char name[PROXY_WORKER_MAX_NAME_SIZE];
  350. char scheme[PROXY_WORKER_MAX_SCHEME_SIZE]; /* scheme to use ajp|http|https */
  351. char hostname[PROXY_WORKER_MAX_HOSTNAME_SIZE]; /* remote backend address (deprecated, use hostname_ex below) */
  352. char route[PROXY_WORKER_MAX_ROUTE_SIZE]; /* balancing route */
  353. char redirect[PROXY_WORKER_MAX_ROUTE_SIZE]; /* temporary balancing redirection route */
  354. char flusher[PROXY_WORKER_MAX_SCHEME_SIZE]; /* flush provider used by mod_proxy_fdpass */
  355. char uds_path[PROXY_WORKER_MAX_NAME_SIZE]; /* path to worker's unix domain socket if applicable */
  356. int lbset; /* load balancer cluster set */
  357. int retries; /* number of retries on this worker */
  358. int lbstatus; /* Current lbstatus */
  359. int lbfactor; /* dynamic lbfactor */
  360. int min; /* Desired minimum number of available connections */
  361. int smax; /* Soft maximum on the total number of connections */
  362. int hmax; /* Hard maximum on the total number of connections */
  363. int flush_wait; /* poll wait time in microseconds if flush_auto */
  364. int index; /* shm array index */
  365. proxy_hashes hash; /* hash of worker name */
  366. unsigned int status; /* worker status bitfield */
  367. enum {
  368. flush_off,
  369. flush_on,
  370. flush_auto
  371. } flush_packets; /* control AJP flushing */
  372. apr_time_t updated; /* timestamp of last update for dynamic workers, or queue-time of HC workers */
  373. apr_time_t error_time; /* time of the last error */
  374. apr_interval_time_t ttl; /* maximum amount of time in seconds a connection
  375. * may be available while exceeding the soft limit */
  376. apr_interval_time_t retry; /* retry interval */
  377. apr_interval_time_t timeout; /* connection timeout */
  378. apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
  379. apr_interval_time_t ping_timeout;
  380. apr_interval_time_t conn_timeout;
  381. apr_size_t recv_buffer_size;
  382. apr_size_t io_buffer_size;
  383. apr_size_t elected; /* Number of times the worker was elected */
  384. apr_size_t busy; /* busyness factor */
  385. apr_port_t port;
  386. apr_off_t transferred;/* Number of bytes transferred to remote */
  387. apr_off_t read; /* Number of bytes read from remote */
  388. void *context; /* general purpose storage */
  389. unsigned int keepalive:1;
  390. unsigned int disablereuse:1;
  391. unsigned int is_address_reusable:1;
  392. unsigned int retry_set:1;
  393. unsigned int timeout_set:1;
  394. unsigned int acquire_set:1;
  395. unsigned int ping_timeout_set:1;
  396. unsigned int conn_timeout_set:1;
  397. unsigned int recv_buffer_size_set:1;
  398. unsigned int io_buffer_size_set:1;
  399. unsigned int keepalive_set:1;
  400. unsigned int disablereuse_set:1;
  401. unsigned int was_malloced:1;
  402. char hcuri[PROXY_WORKER_MAX_ROUTE_SIZE]; /* health check uri */
  403. char hcexpr[PROXY_WORKER_MAX_SCHEME_SIZE]; /* name of condition expr for health check */
  404. int passes; /* number of successes for check to pass */
  405. int pcount; /* current count of passes */
  406. int fails; /* number of failures for check to fail */
  407. int fcount; /* current count of failures */
  408. hcmethod_t method; /* method to use for health check */
  409. apr_interval_time_t interval;
  410. char upgrade[PROXY_WORKER_MAX_SCHEME_SIZE];/* upgrade protocol used by mod_proxy_wstunnel */
  411. char hostname_ex[PROXY_RFC1035_HOSTNAME_SIZE]; /* RFC1035 compliant version of the remote backend address */
  412. apr_size_t response_field_size; /* Size of proxy response buffer in bytes. */
  413. unsigned int response_field_size_set:1;
  414. } proxy_worker_shared;
  415. #define ALIGNED_PROXY_WORKER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_worker_shared)))
  416. /* Worker configuration */
  417. struct proxy_worker {
  418. proxy_hashes hash; /* hash of worker name */
  419. unsigned int local_status; /* status of per-process worker */
  420. proxy_conn_pool *cp; /* Connection pool to use */
  421. proxy_worker_shared *s; /* Shared data */
  422. proxy_balancer *balancer; /* which balancer am I in? */
  423. apr_thread_mutex_t *tmutex; /* Thread lock for updating address cache */
  424. void *context; /* general purpose storage */
  425. ap_conf_vector_t *section_config; /* <Proxy>-section wherein defined */
  426. };
  427. /* default to health check every 30 seconds */
  428. #define HCHECK_WATHCHDOG_DEFAULT_INTERVAL (30)
  429. /* The watchdog runs every 2 seconds, which is also the minimal check */
  430. #define HCHECK_WATHCHDOG_INTERVAL (2)
  431. /*
  432. * Time to wait (in microseconds) to find out if more data is currently
  433. * available at the backend.
  434. */
  435. #define PROXY_FLUSH_WAIT 10000
  436. typedef struct {
  437. char sticky_path[PROXY_BALANCER_MAX_STICKY_SIZE]; /* URL sticky session identifier */
  438. char sticky[PROXY_BALANCER_MAX_STICKY_SIZE]; /* sticky session identifier */
  439. char lbpname[PROXY_MAX_PROVIDER_NAME_SIZE]; /* lbmethod provider name */
  440. char nonce[APR_UUID_FORMATTED_LENGTH + 1];
  441. char name[PROXY_BALANCER_MAX_NAME_SIZE];
  442. char sname[PROXY_BALANCER_MAX_NAME_SIZE];
  443. char vpath[PROXY_BALANCER_MAX_ROUTE_SIZE];
  444. char vhost[PROXY_BALANCER_MAX_HOSTNAME_SIZE];
  445. apr_interval_time_t timeout; /* Timeout for waiting on free connection */
  446. apr_time_t wupdated; /* timestamp of last change to workers list */
  447. int max_attempts; /* Number of attempts before failing */
  448. int index; /* shm array index */
  449. proxy_hashes hash;
  450. unsigned int sticky_force:1; /* Disable failover for sticky sessions */
  451. unsigned int scolonsep:1; /* true if ';' seps sticky session paths */
  452. unsigned int max_attempts_set:1;
  453. unsigned int was_malloced:1;
  454. unsigned int need_reset:1;
  455. unsigned int vhosted:1;
  456. unsigned int inactive:1;
  457. unsigned int forcerecovery:1;
  458. char sticky_separator; /* separator for sessionid/route */
  459. unsigned int forcerecovery_set:1;
  460. unsigned int scolonsep_set:1;
  461. unsigned int sticky_force_set:1;
  462. unsigned int nonce_set:1;
  463. unsigned int sticky_separator_set:1;
  464. } proxy_balancer_shared;
  465. #define ALIGNED_PROXY_BALANCER_SHARED_SIZE (APR_ALIGN_DEFAULT(sizeof(proxy_balancer_shared)))
  466. struct proxy_balancer {
  467. apr_array_header_t *workers; /* initially configured workers */
  468. apr_array_header_t *errstatuses; /* statuses to force members into error */
  469. ap_slotmem_instance_t *wslot; /* worker shm data - runtime */
  470. ap_slotmem_provider_t *storage;
  471. int growth; /* number of post-config workers can added */
  472. int max_workers; /* maximum number of allowed workers */
  473. proxy_hashes hash;
  474. apr_time_t wupdated; /* timestamp of last change to workers list */
  475. proxy_balancer_method *lbmethod;
  476. apr_global_mutex_t *gmutex; /* global lock for updating list of workers */
  477. apr_thread_mutex_t *tmutex; /* Thread lock for updating shm */
  478. proxy_server_conf *sconf;
  479. void *context; /* general purpose storage */
  480. proxy_balancer_shared *s; /* Shared data */
  481. int failontimeout; /* Whether to mark a member in Err if IO timeout occurs */
  482. unsigned int failontimeout_set:1;
  483. unsigned int growth_set:1;
  484. unsigned int lbmethod_set:1;
  485. ap_conf_vector_t *section_config; /* <Proxy>-section wherein defined */
  486. };
  487. struct proxy_balancer_method {
  488. const char *name; /* name of the load balancer method*/
  489. proxy_worker *(*finder)(proxy_balancer *balancer,
  490. request_rec *r);
  491. void *context; /* general purpose storage */
  492. apr_status_t (*reset)(proxy_balancer *balancer, server_rec *s);
  493. apr_status_t (*age)(proxy_balancer *balancer, server_rec *s);
  494. apr_status_t (*updatelbstatus)(proxy_balancer *balancer, proxy_worker *elected, server_rec *s);
  495. };
  496. #define PROXY_THREAD_LOCK(x) ( (x) && (x)->tmutex ? apr_thread_mutex_lock((x)->tmutex) : APR_SUCCESS)
  497. #define PROXY_THREAD_UNLOCK(x) ( (x) && (x)->tmutex ? apr_thread_mutex_unlock((x)->tmutex) : APR_SUCCESS)
  498. #define PROXY_GLOBAL_LOCK(x) ( (x) && (x)->gmutex ? apr_global_mutex_lock((x)->gmutex) : APR_SUCCESS)
  499. #define PROXY_GLOBAL_UNLOCK(x) ( (x) && (x)->gmutex ? apr_global_mutex_unlock((x)->gmutex) : APR_SUCCESS)
  500. /* hooks */
  501. /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and
  502. * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
  503. */
  504. #if !defined(WIN32)
  505. #define PROXY_DECLARE(type) type
  506. #define PROXY_DECLARE_NONSTD(type) type
  507. #define PROXY_DECLARE_DATA
  508. #elif defined(PROXY_DECLARE_STATIC)
  509. #define PROXY_DECLARE(type) type __stdcall
  510. #define PROXY_DECLARE_NONSTD(type) type
  511. #define PROXY_DECLARE_DATA
  512. #elif defined(PROXY_DECLARE_EXPORT)
  513. #define PROXY_DECLARE(type) __declspec(dllexport) type __stdcall
  514. #define PROXY_DECLARE_NONSTD(type) __declspec(dllexport) type
  515. #define PROXY_DECLARE_DATA __declspec(dllexport)
  516. #else
  517. #define PROXY_DECLARE(type) __declspec(dllimport) type __stdcall
  518. #define PROXY_DECLARE_NONSTD(type) __declspec(dllimport) type
  519. #define PROXY_DECLARE_DATA __declspec(dllimport)
  520. #endif
  521. /* Using PROXY_DECLARE_OPTIONAL_HOOK instead of
  522. * APR_DECLARE_EXTERNAL_HOOK allows build/make_nw_export.awk
  523. * to distinguish between hooks that implement
  524. * proxy_hook_xx and proxy_hook_get_xx in mod_proxy.c and
  525. * those which don't.
  526. */
  527. #define PROXY_DECLARE_OPTIONAL_HOOK APR_DECLARE_EXTERNAL_HOOK
  528. /* These 2 are in mod_proxy.c */
  529. extern PROXY_DECLARE_DATA proxy_hcmethods_t proxy_hcmethods[];
  530. extern PROXY_DECLARE_DATA proxy_wstat_t proxy_wstat_tbl[];
  531. /* Following 4 from health check */
  532. APR_DECLARE_OPTIONAL_FN(void, hc_show_exprs, (request_rec *));
  533. APR_DECLARE_OPTIONAL_FN(void, hc_select_exprs, (request_rec *, const char *));
  534. APR_DECLARE_OPTIONAL_FN(int, hc_valid_expr, (request_rec *, const char *));
  535. APR_DECLARE_OPTIONAL_FN(const char *, set_worker_hc_param,
  536. (apr_pool_t *, server_rec *, proxy_worker *,
  537. const char *, const char *, void *));
  538. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, section_post_config,
  539. (apr_pool_t *p, apr_pool_t *plog,
  540. apr_pool_t *ptemp, server_rec *s,
  541. ap_conf_vector_t *section_config))
  542. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, scheme_handler,
  543. (request_rec *r, proxy_worker *worker,
  544. proxy_server_conf *conf, char *url,
  545. const char *proxyhost, apr_port_t proxyport))
  546. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, canon_handler,
  547. (request_rec *r, char *url))
  548. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
  549. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r))
  550. /**
  551. * pre request hook.
  552. * It will return the most suitable worker at the moment
  553. * and corresponding balancer.
  554. * The url is rewritten from balancer://cluster/uri to scheme://host:port/uri
  555. * and then the scheme_handler is called.
  556. *
  557. */
  558. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
  559. proxy_balancer **balancer,
  560. request_rec *r,
  561. proxy_server_conf *conf, char **url))
  562. /**
  563. * post request hook.
  564. * It is called after request for updating runtime balancer status.
  565. */
  566. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, post_request, (proxy_worker *worker,
  567. proxy_balancer *balancer, request_rec *r,
  568. proxy_server_conf *conf))
  569. /**
  570. * request status hook
  571. * It is called after all proxy processing has been done. This gives other
  572. * modules a chance to create default content on failure, for example
  573. */
  574. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, request_status,
  575. (int *status, request_rec *r))
  576. /* proxy_util.c */
  577. PROXY_DECLARE(apr_status_t) ap_proxy_strncpy(char *dst, const char *src,
  578. apr_size_t dlen);
  579. PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
  580. PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
  581. PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
  582. int forcedec, int proxyreq);
  583. PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
  584. char **passwordp, char **hostp, apr_port_t *port);
  585. PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
  586. PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
  587. /** Test whether the hostname/address of the request are blocked by the ProxyBlock
  588. * configuration.
  589. * @param r request
  590. * @param conf server configuration
  591. * @param hostname hostname from request URI
  592. * @param addr resolved address of hostname, or NULL if not known
  593. * @return OK on success, or else an errro
  594. */
  595. PROXY_DECLARE(int) ap_proxy_checkproxyblock2(request_rec *r, proxy_server_conf *conf,
  596. const char *hostname, apr_sockaddr_t *addr);
  597. PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
  598. /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
  599. PROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **, const char *, apr_sockaddr_t *, const char *, proxy_server_conf *, request_rec *);
  600. /* DEPRECATED (will be replaced with ap_proxy_check_connection */
  601. PROXY_DECLARE(apr_status_t) ap_proxy_ssl_connection_cleanup(proxy_conn_rec *conn,
  602. request_rec *r);
  603. PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
  604. PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
  605. PROXY_DECLARE(int) ap_proxy_ssl_engine(conn_rec *c,
  606. ap_conf_vector_t *per_dir_config,
  607. int enable);
  608. PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c);
  609. PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
  610. /* Header mapping functions, and a typedef of their signature */
  611. PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url);
  612. PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str);
  613. #if !defined(WIN32)
  614. typedef const char *(*ap_proxy_header_reverse_map_fn)(request_rec *,
  615. proxy_dir_conf *, const char *);
  616. #elif defined(PROXY_DECLARE_STATIC)
  617. typedef const char *(__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
  618. proxy_dir_conf *, const char *);
  619. #elif defined(PROXY_DECLARE_EXPORT)
  620. typedef __declspec(dllexport) const char *
  621. (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
  622. proxy_dir_conf *, const char *);
  623. #else
  624. typedef __declspec(dllimport) const char *
  625. (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
  626. proxy_dir_conf *, const char *);
  627. #endif
  628. /* Connection pool API */
  629. /**
  630. * Return the user-land, UDS aware worker name
  631. * @param p memory pool used for displaying worker name
  632. * @param worker the worker
  633. * @return name
  634. */
  635. PROXY_DECLARE(char *) ap_proxy_worker_name(apr_pool_t *p,
  636. proxy_worker *worker);
  637. /**
  638. * Get the worker from proxy configuration
  639. * @param p memory pool used for finding worker
  640. * @param balancer the balancer that the worker belongs to
  641. * @param conf current proxy server configuration
  642. * @param url url to find the worker from
  643. * @return proxy_worker or NULL if not found
  644. */
  645. PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
  646. proxy_balancer *balancer,
  647. proxy_server_conf *conf,
  648. const char *url);
  649. /**
  650. * Define and Allocate space for the worker to proxy configuration
  651. * @param p memory pool to allocate worker from
  652. * @param worker the new worker
  653. * @param balancer the balancer that the worker belongs to
  654. * @param conf current proxy server configuration
  655. * @param url url containing worker name
  656. * @param do_malloc true if shared struct should be malloced
  657. * @return error message or NULL if successful (*worker is new worker)
  658. */
  659. PROXY_DECLARE(char *) ap_proxy_define_worker(apr_pool_t *p,
  660. proxy_worker **worker,
  661. proxy_balancer *balancer,
  662. proxy_server_conf *conf,
  663. const char *url,
  664. int do_malloc);
  665. /**
  666. * Share a defined proxy worker via shm
  667. * @param worker worker to be shared
  668. * @param shm location of shared info
  669. * @param i index into shm
  670. * @return APR_SUCCESS or error code
  671. */
  672. PROXY_DECLARE(apr_status_t) ap_proxy_share_worker(proxy_worker *worker,
  673. proxy_worker_shared *shm,
  674. int i);
  675. /**
  676. * Initialize the worker by setting up worker connection pool and mutex
  677. * @param worker worker to initialize
  678. * @param s current server record
  679. * @param p memory pool used for mutex and connection pool
  680. * @return APR_SUCCESS or error code
  681. */
  682. PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
  683. server_rec *s,
  684. apr_pool_t *p);
  685. /**
  686. * Verifies valid balancer name (eg: balancer://foo)
  687. * @param name name to test
  688. * @param i number of chars to test; 0 for all.
  689. * @return true/false
  690. */
  691. PROXY_DECLARE(int) ap_proxy_valid_balancer_name(char *name, int i);
  692. /**
  693. * Get the balancer from proxy configuration
  694. * @param p memory pool used for temporary storage while finding balancer
  695. * @param conf current proxy server configuration
  696. * @param url url to find the worker from; must have balancer:// prefix
  697. * @param careactive true if we care if the balancer is active or not
  698. * @return proxy_balancer or NULL if not found
  699. */
  700. PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
  701. proxy_server_conf *conf,
  702. const char *url,
  703. int careactive);
  704. /**
  705. * Update the balancer's vhost related fields
  706. * @param p memory pool used for temporary storage while finding balancer
  707. * @param balancer balancer to be updated
  708. * @param url url to find vhost info
  709. * @return error string or NULL if OK
  710. */
  711. PROXY_DECLARE(char *) ap_proxy_update_balancer(apr_pool_t *p,
  712. proxy_balancer *balancer,
  713. const char *url);
  714. /**
  715. * Define and Allocate space for the balancer to proxy configuration
  716. * @param p memory pool to allocate balancer from
  717. * @param balancer the new balancer
  718. * @param conf current proxy server configuration
  719. * @param url url containing balancer name
  720. * @param alias alias/fake-path to this balancer
  721. * @param do_malloc true if shared struct should be malloced
  722. * @return error message or NULL if successful
  723. */
  724. PROXY_DECLARE(char *) ap_proxy_define_balancer(apr_pool_t *p,
  725. proxy_balancer **balancer,
  726. proxy_server_conf *conf,
  727. const char *url,
  728. const char *alias,
  729. int do_malloc);
  730. /**
  731. * Share a defined proxy balancer via shm
  732. * @param balancer balancer to be shared
  733. * @param shm location of shared info
  734. * @param i index into shm
  735. * @return APR_SUCCESS or error code
  736. */
  737. PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,
  738. proxy_balancer_shared *shm,
  739. int i);
  740. /**
  741. * Initialize the balancer as needed
  742. * @param balancer balancer to initialize
  743. * @param s current server record
  744. * @param p memory pool used for mutex and connection pool
  745. * @return APR_SUCCESS or error code
  746. */
  747. PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balancer,
  748. server_rec *s,
  749. apr_pool_t *p);
  750. typedef int (proxy_is_best_callback_fn_t)(proxy_worker *current, proxy_worker *prev_best, void *baton);
  751. /**
  752. * Retrieve the best worker in a balancer for the current request
  753. * @param balancer balancer for which to find the best worker
  754. * @param r current request record
  755. * @param is_best a callback function provide by the lbmethod
  756. * that determines if the current worker is best
  757. * @param baton an lbmethod-specific context pointer (baton)
  758. * passed to the is_best callback
  759. * @return the best worker to be used for the request
  760. */
  761. PROXY_DECLARE(proxy_worker *) ap_proxy_balancer_get_best_worker(proxy_balancer *balancer,
  762. request_rec *r,
  763. proxy_is_best_callback_fn_t *is_best,
  764. void *baton);
  765. /*
  766. * Needed by the lb modules.
  767. */
  768. APR_DECLARE_OPTIONAL_FN(proxy_worker *, proxy_balancer_get_best_worker,
  769. (proxy_balancer *balancer,
  770. request_rec *r,
  771. proxy_is_best_callback_fn_t *is_best,
  772. void *baton));
  773. /**
  774. * Find the shm of the worker as needed
  775. * @param storage slotmem provider
  776. * @param slot slotmem instance
  777. * @param worker worker to find
  778. * @param index pointer to index within slotmem of worker
  779. * @return pointer to shm of worker, or NULL
  780. */
  781. PROXY_DECLARE(proxy_worker_shared *) ap_proxy_find_workershm(ap_slotmem_provider_t *storage,
  782. ap_slotmem_instance_t *slot,
  783. proxy_worker *worker,
  784. unsigned int *index);
  785. /**
  786. * Find the shm of the balancer as needed
  787. * @param storage slotmem provider
  788. * @param slot slotmem instance
  789. * @param balancer balancer of shm to find
  790. * @param index pointer to index within slotmem of balancer
  791. * @return pointer to shm of balancer, or NULL
  792. */
  793. PROXY_DECLARE(proxy_balancer_shared *) ap_proxy_find_balancershm(ap_slotmem_provider_t *storage,
  794. ap_slotmem_instance_t *slot,
  795. proxy_balancer *balancer,
  796. unsigned int *index);
  797. /**
  798. * Get the most suitable worker and/or balancer for the request
  799. * @param worker worker used for processing request
  800. * @param balancer balancer used for processing request
  801. * @param r current request
  802. * @param conf current proxy server configuration
  803. * @param url request url that balancer can rewrite.
  804. * @return OK or HTTP_XXX error
  805. * @note It calls balancer pre_request hook if the url starts with balancer://
  806. * The balancer then rewrites the url to particular worker, like http://host:port
  807. */
  808. PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
  809. proxy_balancer **balancer,
  810. request_rec *r,
  811. proxy_server_conf *conf,
  812. char **url);
  813. /**
  814. * Post request worker and balancer cleanup
  815. * @param worker worker used for processing request
  816. * @param balancer balancer used for processing request
  817. * @param r current request
  818. * @param conf current proxy server configuration
  819. * @return OK or HTTP_XXX error
  820. * @note Whenever the pre_request is called, the post_request has to be
  821. * called too.
  822. */
  823. PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
  824. proxy_balancer *balancer,
  825. request_rec *r,
  826. proxy_server_conf *conf);
  827. /**
  828. * Determine backend hostname and port
  829. * @param p memory pool used for processing
  830. * @param r current request
  831. * @param conf current proxy server configuration
  832. * @param worker worker used for processing request
  833. * @param conn proxy connection struct
  834. * @param uri processed uri
  835. * @param url request url
  836. * @param proxyname are we connecting directly or via a proxy
  837. * @param proxyport proxy host port
  838. * @param server_portstr Via headers server port, must be non-NULL
  839. * @param server_portstr_size size of the server_portstr buffer; must
  840. * be at least one, even if the protocol doesn't use this
  841. * @return OK or HTTP_XXX error
  842. */
  843. PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
  844. proxy_server_conf *conf,
  845. proxy_worker *worker,
  846. proxy_conn_rec *conn,
  847. apr_uri_t *uri,
  848. char **url,
  849. const char *proxyname,
  850. apr_port_t proxyport,
  851. char *server_portstr,
  852. int server_portstr_size);
  853. /**
  854. * Mark a worker for retry
  855. * @param proxy_function calling proxy scheme (http, ajp, ...)
  856. * @param worker worker used for retrying
  857. * @param s current server record
  858. * @return OK if marked for retry, DECLINED otherwise
  859. * @note The error status of the worker will cleared if the retry interval has
  860. * elapsed since the last error.
  861. */
  862. APR_DECLARE_OPTIONAL_FN(int, ap_proxy_retry_worker,
  863. (const char *proxy_function, proxy_worker *worker, server_rec *s));
  864. /**
  865. * Acquire a connection from worker connection pool
  866. * @param proxy_function calling proxy scheme (http, ajp, ...)
  867. * @param conn acquired connection
  868. * @param worker worker used for obtaining connection
  869. * @param s current server record
  870. * @return OK or HTTP_XXX error
  871. * @note If the connection limit has been reached, the function will
  872. * block until a connection becomes available or the timeout has
  873. * elapsed.
  874. */
  875. PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
  876. proxy_conn_rec **conn,
  877. proxy_worker *worker,
  878. server_rec *s);
  879. /**
  880. * Release a connection back to worker connection pool
  881. * @param proxy_function calling proxy scheme (http, ajp, ...)
  882. * @param conn acquired connection
  883. * @param s current server record
  884. * @return OK or HTTP_XXX error
  885. * @note The connection will be closed if conn->close_on_release is set
  886. */
  887. PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
  888. proxy_conn_rec *conn,
  889. server_rec *s);
  890. #define PROXY_CHECK_CONN_EMPTY (1 << 0)
  891. /**
  892. * Check a connection to the backend
  893. * @param scheme calling proxy scheme (http, ajp, ...)
  894. * @param conn acquired connection
  895. * @param server current server record
  896. * @param max_blank_lines how many blank lines to consume,
  897. * or zero for none (considered data)
  898. * @param flags PROXY_CHECK_* bitmask
  899. * @return APR_SUCCESS: connection established,
  900. * APR_ENOTEMPTY: connection established with data,
  901. * APR_ENOSOCKET: not connected,
  902. * APR_EINVAL: worker in error state (unusable),
  903. * other: connection closed/aborted (remotely)
  904. */
  905. PROXY_DECLARE(apr_status_t) ap_proxy_check_connection(const char *scheme,
  906. proxy_conn_rec *conn,
  907. server_rec *server,
  908. unsigned max_blank_lines,
  909. int flags);
  910. /**
  911. * Make a connection to the backend
  912. * @param proxy_function calling proxy scheme (http, ajp, ...)
  913. * @param conn acquired connection
  914. * @param worker connection worker
  915. * @param s current server record
  916. * @return OK or HTTP_XXX error
  917. * @note In case the socket already exists for conn, just check the link
  918. * status.
  919. */
  920. PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
  921. proxy_conn_rec *conn,
  922. proxy_worker *worker,
  923. server_rec *s);
  924. /**
  925. * Make a connection to a Unix Domain Socket (UDS) path
  926. * @param sock UDS to connect
  927. * @param uds_path UDS path to connect to
  928. * @param p pool to make the sock addr
  929. * @return APR_SUCCESS or error status
  930. */
  931. PROXY_DECLARE(apr_status_t) ap_proxy_connect_uds(apr_socket_t *sock,
  932. const char *uds_path,
  933. apr_pool_t *p);
  934. /**
  935. * Make a connection record for backend connection
  936. * @param proxy_function calling proxy scheme (http, ajp, ...)
  937. * @param conn acquired connection
  938. * @param c client connection record (unused, deprecated)
  939. * @param s current server record
  940. * @return OK or HTTP_XXX error
  941. * @note The function will return immediately if conn->connection
  942. * is already set,
  943. */
  944. PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
  945. proxy_conn_rec *conn,
  946. conn_rec *c, server_rec *s);
  947. /**
  948. * Make a connection record for backend connection, using request dir config
  949. * @param proxy_function calling proxy scheme (http, ajp, ...)
  950. * @param conn acquired connection
  951. * @param r current request record
  952. * @return OK or HTTP_XXX error
  953. * @note The function will return immediately if conn->connection
  954. * is already set,
  955. */
  956. PROXY_DECLARE(int) ap_proxy_connection_create_ex(const char *proxy_function,
  957. proxy_conn_rec *conn,
  958. request_rec *r);
  959. /**
  960. * Determine if proxy connection can potentially be reused at the
  961. * end of this request.
  962. * @param conn proxy connection
  963. * @return non-zero if reusable, 0 otherwise
  964. * @note Even if this function returns non-zero, the connection may
  965. * be subsequently marked for closure.
  966. */
  967. PROXY_DECLARE(int) ap_proxy_connection_reusable(proxy_conn_rec *conn);
  968. /**
  969. * Signal the upstream chain that the connection to the backend broke in the
  970. * middle of the response. This is done by sending an error bucket with
  971. * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
  972. * @param r current request record of client request
  973. * @param brigade The brigade that is sent through the output filter chain
  974. */
  975. PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
  976. apr_bucket_brigade *brigade);
  977. /**
  978. * Return a hash based on the passed string
  979. * @param str string to produce hash from
  980. * @param method hashing method to use
  981. * @return hash as unsigned int
  982. */
  983. typedef enum { PROXY_HASHFUNC_DEFAULT, PROXY_HASHFUNC_APR, PROXY_HASHFUNC_FNV } proxy_hash_t;
  984. PROXY_DECLARE(unsigned int) ap_proxy_hashfunc(const char *str, proxy_hash_t method);
  985. /**
  986. * Set/unset the worker status bitfield depending on flag
  987. * @param c flag
  988. * @param set set or unset bit
  989. * @param w worker to use
  990. * @return APR_SUCCESS if valid flag
  991. */
  992. PROXY_DECLARE(apr_status_t) ap_proxy_set_wstatus(char c, int set, proxy_worker *w);
  993. /**
  994. * Create readable representation of worker status bitfield
  995. * @param p pool
  996. * @param w worker to use
  997. * @return string representation of status
  998. */
  999. PROXY_DECLARE(char *) ap_proxy_parse_wstatus(apr_pool_t *p, proxy_worker *w);
  1000. /**
  1001. * Sync balancer and workers based on any updates w/i shm
  1002. * @param b balancer to check/update member list of
  1003. * @param s server rec
  1004. * @param conf config
  1005. * @return APR_SUCCESS if all goes well
  1006. */
  1007. PROXY_DECLARE(apr_status_t) ap_proxy_sync_balancer(proxy_balancer *b,
  1008. server_rec *s,
  1009. proxy_server_conf *conf);
  1010. /**
  1011. * Find the matched alias for this request and setup for proxy handler
  1012. * @param r request
  1013. * @param ent proxy_alias record
  1014. * @param dconf per-dir config or NULL
  1015. * @return DECLINED, DONE or OK if matched
  1016. */
  1017. PROXY_DECLARE(int) ap_proxy_trans_match(request_rec *r,
  1018. struct proxy_alias *ent,
  1019. proxy_dir_conf *dconf);
  1020. /**
  1021. * Create a HTTP request header brigade, old_cl_val and old_te_val as required.
  1022. * @param p pool
  1023. * @param header_brigade header brigade to use/fill
  1024. * @param r request
  1025. * @param p_conn proxy connection rec
  1026. * @param worker selected worker
  1027. * @param conf per-server proxy config
  1028. * @param uri uri
  1029. * @param url url
  1030. * @param server_portstr port as string
  1031. * @param old_cl_val stored old content-len val
  1032. * @param old_te_val stored old TE val
  1033. * @return OK or HTTP_EXPECTATION_FAILED
  1034. */
  1035. PROXY_DECLARE(int) ap_proxy_create_hdrbrgd(apr_pool_t *p,
  1036. apr_bucket_brigade *header_brigade,
  1037. request_rec *r,
  1038. proxy_conn_rec *p_conn,
  1039. proxy_worker *worker,
  1040. proxy_server_conf *conf,
  1041. apr_uri_t *uri,
  1042. char *url, char *server_portstr,
  1043. char **old_cl_val,
  1044. char **old_te_val);
  1045. /**
  1046. * @param bucket_alloc bucket allocator
  1047. * @param r request
  1048. * @param p_conn proxy connection
  1049. * @param origin connection rec of origin
  1050. * @param bb brigade to send to origin
  1051. * @param flush flush
  1052. * @return status (OK)
  1053. */
  1054. PROXY_DECLARE(int) ap_proxy_pass_brigade(apr_bucket_alloc_t *bucket_alloc,
  1055. request_rec *r, proxy_conn_rec *p_conn,
  1056. conn_rec *origin, apr_bucket_brigade *bb,
  1057. int flush);
  1058. /**
  1059. * Clear the headers referenced by the Connection header from the given
  1060. * table, and remove the Connection header.
  1061. * @param r request
  1062. * @param headers table of headers to clear
  1063. * @return 1 if "close" was present, 0 otherwise.
  1064. */
  1065. APR_DECLARE_OPTIONAL_FN(int, ap_proxy_clear_connection,
  1066. (request_rec *r, apr_table_t *headers));
  1067. /**
  1068. * @param socket socket to test
  1069. * @return TRUE if socket is connected/active
  1070. */
  1071. PROXY_DECLARE(int) ap_proxy_is_socket_connected(apr_socket_t *socket);
  1072. #define PROXY_LBMETHOD "proxylbmethod"
  1073. /* The number of dynamic workers that can be added when reconfiguring.
  1074. * If this limit is reached you must stop and restart the server.
  1075. */
  1076. #define PROXY_DYNAMIC_BALANCER_LIMIT 16
  1077. /**
  1078. * Calculate maximum number of workers in scoreboard.
  1079. * @return number of workers to allocate in the scoreboard
  1080. */
  1081. int ap_proxy_lb_workers(void);
  1082. /**
  1083. * Return the port number of a known scheme (eg: http -> 80).
  1084. * @param scheme scheme to test
  1085. * @return port number or 0 if unknown
  1086. */
  1087. PROXY_DECLARE(apr_port_t) ap_proxy_port_of_scheme(const char *scheme);
  1088. /**
  1089. * Return the name of the health check method (eg: "OPTIONS").
  1090. * @param method method enum
  1091. * @return name of method
  1092. */
  1093. PROXY_DECLARE (const char *) ap_proxy_show_hcmethod(hcmethod_t method);
  1094. /**
  1095. * Strip a unix domain socket (UDS) prefix from the input URL
  1096. * @param p pool to allocate result from
  1097. * @param url a URL potentially prefixed with a UDS path
  1098. * @return URL with the UDS prefix removed
  1099. */
  1100. PROXY_DECLARE(const char *) ap_proxy_de_socketfy(apr_pool_t *p, const char *url);
  1101. /*
  1102. * Transform buckets from one bucket allocator to another one by creating a
  1103. * transient bucket for each data bucket and let it use the data read from
  1104. * the old bucket. Metabuckets are transformed by just recreating them.
  1105. * Attention: Currently only the following bucket types are handled:
  1106. *
  1107. * All data buckets
  1108. * FLUSH
  1109. * EOS
  1110. *
  1111. * If an other bucket type is found its type is logged as a debug message
  1112. * and APR_EGENERAL is returned.
  1113. *
  1114. * @param r request_rec of the actual request. Used for logging purposes
  1115. * @param from the bucket brigade to take the buckets from
  1116. * @param to the bucket brigade to store the transformed buckets
  1117. * @return apr_status_t of the operation. Either APR_SUCCESS or
  1118. * APR_EGENERAL
  1119. */
  1120. PROXY_DECLARE(apr_status_t) ap_proxy_buckets_lifetime_transform(request_rec *r,
  1121. apr_bucket_brigade *from,
  1122. apr_bucket_brigade *to);
  1123. /*
  1124. * Sends all data that can be read non blocking from the input filter chain of
  1125. * c_i and send it down the output filter chain of c_o. For reading it uses
  1126. * the bucket brigade bb_i which should be created from the bucket allocator
  1127. * associated with c_i. For sending through the output filter chain it uses
  1128. * the bucket brigade bb_o which should be created from the bucket allocator
  1129. * associated with c_o. In order to get the buckets from bb_i to bb_o
  1130. * ap_proxy_buckets_lifetime_transform is used.
  1131. *
  1132. * @param r request_rec of the actual request. Used for logging purposes
  1133. * @param c_i inbound connection conn_rec
  1134. * @param c_o outbound connection conn_rec
  1135. * @param bb_i bucket brigade for pulling data from the inbound connection
  1136. * @param bb_o bucket brigade for sending data through the outbound connection
  1137. * @param name string for logging from where data was pulled
  1138. * @param sent if not NULL will be set to 1 if data was sent through c_o
  1139. * @param bsize maximum amount of data pulled in one iteration from c_i
  1140. * @param after if set flush data on c_o only once after the loop
  1141. * @return apr_status_t of the operation. Could be any error returned from
  1142. * either the input filter chain of c_i or the output filter chain
  1143. * of c_o. APR_EPIPE if the outgoing connection was aborted.
  1144. */
  1145. PROXY_DECLARE(apr_status_t) ap_proxy_transfer_between_connections(
  1146. request_rec *r,
  1147. conn_rec *c_i,
  1148. conn_rec *c_o,
  1149. apr_bucket_brigade *bb_i,
  1150. apr_bucket_brigade *bb_o,
  1151. const char *name,
  1152. int *sent,
  1153. apr_off_t bsize,
  1154. int after);
  1155. extern module PROXY_DECLARE_DATA proxy_module;
  1156. #endif /*MOD_PROXY_H*/
  1157. /** @} */