home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / apache_2.2.8-win32-x86-no_ssl.msi / Data1.cab / _1DF9B4D7CB89BBECFD402D99AE371E0C < prev    next >
Text File  |  2008-01-02  |  31KB  |  749 lines

  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.  
  17. #ifndef MOD_PROXY_H
  18. #define MOD_PROXY_H 
  19.  
  20. /**
  21.  * @file  mod_proxy.h
  22.  * @brief Proxy Extension Module for Apache
  23.  *
  24.  * @defgroup MOD_PROXY mod_proxy
  25.  * @ingroup  APACHE_MODS
  26.  * @{
  27.  */
  28.  
  29. /*
  30.  
  31.    Also note numerous FIXMEs and CHECKMEs which should be eliminated.
  32.  
  33.    This code is once again experimental!
  34.  
  35.    Things to do:
  36.  
  37.    1. Make it completely work (for FTP too)
  38.  
  39.    2. HTTP/1.1
  40.  
  41.    Chuck Murcko <chuck@topsail.org> 02-06-01
  42.  
  43.  */
  44.  
  45. #define CORE_PRIVATE
  46.  
  47. #include "apr_hooks.h"
  48. #include "apr.h"
  49. #include "apr_lib.h"
  50. #include "apr_strings.h"
  51. #include "apr_buckets.h"
  52. #include "apr_md5.h"
  53. #include "apr_network_io.h"
  54. #include "apr_pools.h"
  55. #include "apr_strings.h"
  56. #include "apr_uri.h"
  57. #include "apr_date.h"
  58. #include "apr_strmatch.h"
  59. #include "apr_fnmatch.h"
  60. #include "apr_reslist.h"
  61. #define APR_WANT_STRFUNC
  62. #include "apr_want.h"
  63.  
  64. #include "httpd.h"
  65. #include "http_config.h"
  66. #include "ap_config.h"
  67. #include "http_core.h"
  68. #include "http_protocol.h"
  69. #include "http_request.h"
  70. #include "http_vhost.h"
  71. #include "http_main.h"
  72. #include "http_log.h"
  73. #include "http_connection.h"
  74. #include "util_filter.h"
  75. #include "util_ebcdic.h"
  76. #include "ap_provider.h"
  77.  
  78. #if APR_HAVE_NETINET_IN_H
  79. #include <netinet/in.h>
  80. #endif
  81. #if APR_HAVE_ARPA_INET_H
  82. #include <arpa/inet.h>
  83. #endif
  84.  
  85. /* for proxy_canonenc() */
  86. enum enctype {
  87.     enc_path, enc_search, enc_user, enc_fpath, enc_parm
  88. };
  89.  
  90. #if APR_CHARSET_EBCDIC
  91. #define CRLF   "\r\n"
  92. #else /*APR_CHARSET_EBCDIC*/
  93. #define CRLF   "\015\012"
  94. #endif /*APR_CHARSET_EBCDIC*/
  95.  
  96. /* default Max-Forwards header setting */
  97. /* Set this to -1, which complies with RFC2616 by not setting
  98.  * max-forwards if the client didn't send it to us.
  99.  */
  100. #define DEFAULT_MAX_FORWARDS    -1
  101.  
  102. /* static information about a remote proxy */
  103. struct proxy_remote {
  104.     const char *scheme;     /* the schemes handled by this proxy, or '*' */
  105.     const char *protocol;   /* the scheme used to talk to this proxy */
  106.     const char *hostname;   /* the hostname of this proxy */
  107.     apr_port_t  port;       /* the port for this proxy */
  108.     ap_regex_t *regexp;        /* compiled regex (if any) for the remote */
  109.     int use_regex;          /* simple boolean. True if we have a regex pattern */
  110. };
  111.  
  112. #define PROXYPASS_NOCANON 0x01
  113. struct proxy_alias {
  114.     const char  *real;
  115.     const char  *fake;
  116.     ap_regex_t  *regex;
  117.     unsigned int flags;
  118. };
  119.  
  120. struct dirconn_entry {
  121.     char *name;
  122.     struct in_addr addr, mask;
  123.     struct apr_sockaddr_t *hostaddr;
  124.     int (*matcher) (struct dirconn_entry * This, request_rec *r);
  125. };
  126.  
  127. struct noproxy_entry {
  128.     const char *name;
  129.     struct apr_sockaddr_t *addr;
  130. };
  131.  
  132. typedef struct proxy_balancer  proxy_balancer;
  133. typedef struct proxy_worker    proxy_worker;
  134. typedef struct proxy_conn_pool proxy_conn_pool;
  135. typedef struct proxy_balancer_method proxy_balancer_method;
  136.  
  137. typedef struct {
  138.     apr_array_header_t *proxies;
  139.     apr_array_header_t *sec_proxy;
  140.     apr_array_header_t *aliases;
  141.     apr_array_header_t *noproxies;
  142.     apr_array_header_t *dirconn;
  143.     apr_array_header_t *allowed_connect_ports;
  144.     apr_array_header_t *workers;
  145.     apr_array_header_t *balancers;
  146.     proxy_worker       *forward;    /* forward proxy worker */
  147.     proxy_worker       *reverse;    /* reverse "module-driven" proxy worker */
  148.     const char *domain;     /* domain name to use in absence of a domain name in the request */
  149.     int req;                /* true if proxy requests are enabled */
  150.     char req_set;
  151.     enum {
  152.       via_off,
  153.       via_on,
  154.       via_block,
  155.       via_full
  156.     } viaopt;                   /* how to deal with proxy Via: headers */
  157.     char viaopt_set;
  158.     apr_size_t recv_buffer_size;
  159.     char recv_buffer_size_set;
  160.     apr_size_t io_buffer_size;
  161.     char io_buffer_size_set;
  162.     long maxfwd;
  163.     char maxfwd_set;
  164.     /** 
  165.      * the following setting masks the error page
  166.      * returned from the 'proxied server' and just 
  167.      * forwards the status code upwards.
  168.      * This allows the main server (us) to generate
  169.      * the error page, (so it will look like a error
  170.      * returned from the rest of the system 
  171.      */
  172.     int error_override;
  173.     int error_override_set;
  174.     int preserve_host;
  175.     int preserve_host_set;
  176.     apr_interval_time_t timeout;
  177.     char timeout_set;
  178.     enum {
  179.       bad_error,
  180.       bad_ignore,
  181.       bad_body
  182.     } badopt;                   /* how to deal with bad headers */
  183.     char badopt_set;
  184. /* putting new stuff on the end maximises binary back-compatibility.
  185.  * the strmatch_patterns are really a const just to have a
  186.  * case-independent strstr.
  187.  */
  188.     enum {
  189.         status_off,
  190.         status_on,
  191.         status_full
  192.     } proxy_status;             /* Status display options */
  193.     char proxy_status_set;
  194.     apr_pool_t *pool;           /* Pool used for allocating this struct */
  195. } proxy_server_conf;
  196.  
  197.  
  198. typedef struct {
  199.     const char *p;            /* The path */
  200.     int         p_is_fnmatch; /* Is this path an fnmatch candidate? */
  201.     ap_regex_t  *r;            /* Is this a regex? */
  202.  
  203. /* ProxyPassReverse and friends are documented as working inside
  204.  * <Location>.  But in fact they never have done in the case of
  205.  * more than one <Location>, because the server_conf can't see it.
  206.  * We need to move them to the per-dir config.
  207.  * Discussed in February:
  208.  * http://marc.theaimsgroup.com/?l=apache-httpd-dev&m=110726027118798&w=2
  209.  */
  210.     apr_array_header_t *raliases;
  211.     apr_array_header_t* cookie_paths;
  212.     apr_array_header_t* cookie_domains;
  213.     const apr_strmatch_pattern* cookie_path_str;
  214.     const apr_strmatch_pattern* cookie_domain_str;
  215.     const char *ftp_directory_charset;
  216. } proxy_dir_conf;
  217.  
  218. typedef struct {
  219.     conn_rec     *connection;
  220.     const char   *hostname;
  221.     apr_port_t   port;
  222.     int          is_ssl;
  223.     apr_pool_t   *pool;     /* Subpool used for creating socket */
  224.     apr_socket_t *sock;     /* Connection socket */
  225.     apr_sockaddr_t *addr;   /* Preparsed remote address info */
  226.     apr_uint32_t flags;     /* Conection flags */
  227.     int          close;     /* Close 'this' connection */
  228.     int          close_on_recycle; /* Close the connection when returning to pool */
  229.     proxy_worker *worker;   /* Connection pool this connection belongs to */
  230.     void         *data;     /* per scheme connection data */
  231. #if APR_HAS_THREADS
  232.     int          inreslist; /* connection in apr_reslist? */
  233. #endif
  234. } proxy_conn_rec;
  235.  
  236. typedef struct {
  237.         float cache_completion; /* completion percentage */
  238.         int content_length; /* length of the content */
  239. } proxy_completion;
  240.  
  241. /* Connection pool */
  242. struct proxy_conn_pool {
  243.     apr_pool_t     *pool;   /* The pool used in constructor and destructor calls */
  244.     apr_sockaddr_t *addr;   /* Preparsed remote address info */
  245. #if APR_HAS_THREADS
  246.     apr_reslist_t  *res;    /* Connection resource list */
  247. #endif
  248.     proxy_conn_rec *conn;   /* Single connection for prefork mpm's */
  249. };
  250.  
  251. /* worker status flags */
  252. #define PROXY_WORKER_INITIALIZED    0x0001
  253. #define PROXY_WORKER_IGNORE_ERRORS  0x0002
  254. #define PROXY_WORKER_IN_SHUTDOWN    0x0010
  255. #define PROXY_WORKER_DISABLED       0x0020
  256. #define PROXY_WORKER_STOPPED        0x0040
  257. #define PROXY_WORKER_IN_ERROR       0x0080
  258. #define PROXY_WORKER_HOT_STANDBY    0x0100
  259.  
  260. #define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
  261. PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR )
  262.  
  263. /* NOTE: these check the shared status */
  264. #define PROXY_WORKER_IS_INITIALIZED(f)   ( (f)->s && \
  265.   ( (f)->s->status &  PROXY_WORKER_INITIALIZED ) )
  266.  
  267. #define PROXY_WORKER_IS_STANDBY(f)   ( (f)->s && \
  268.   ( (f)->s->status &  PROXY_WORKER_HOT_STANDBY ) )
  269.  
  270. #define PROXY_WORKER_IS_USABLE(f)   ( (f)->s && \
  271.   ( !( (f)->s->status & PROXY_WORKER_NOT_USABLE_BITMAP) ) && \
  272.   PROXY_WORKER_IS_INITIALIZED(f) )
  273.  
  274. /* default worker retry timeout in seconds */
  275. #define PROXY_WORKER_DEFAULT_RETRY  60
  276. #define PROXY_WORKER_MAX_ROUTE_SIZ  63
  277.  
  278. /* Runtime worker status informations. Shared in scoreboard */
  279. typedef struct {
  280.     int             status;
  281.     apr_time_t      error_time; /* time of the last error */
  282.     int             retries;    /* number of retries on this worker */
  283.     int             lbstatus;   /* Current lbstatus */
  284.     int             lbfactor;   /* dynamic lbfactor */
  285.     apr_off_t       transferred;/* Number of bytes transferred to remote */
  286.     apr_off_t       read;       /* Number of bytes read from remote */
  287.     apr_size_t      elected;    /* Number of times the worker was elected */
  288.     char            route[PROXY_WORKER_MAX_ROUTE_SIZ+1];
  289.     char            redirect[PROXY_WORKER_MAX_ROUTE_SIZ+1];
  290.     void            *context;   /* general purpose storage */
  291.     apr_size_t      busy;       /* busyness factor */
  292.     int             lbset;      /* load balancer cluster set */
  293. } proxy_worker_stat;
  294.  
  295. /* Worker configuration */
  296. struct proxy_worker {
  297.     int             id;         /* scoreboard id */
  298.     apr_interval_time_t retry;  /* retry interval */
  299.     int             lbfactor;   /* initial load balancing factor */
  300.     const char      *name;
  301.     const char      *scheme;    /* scheme to use ajp|http|https */
  302.     const char      *hostname;  /* remote backend address */
  303.     const char      *route;     /* balancing route */
  304.     const char      *redirect;  /* temporary balancing redirection route */
  305.     int             status;     /* temporary worker status */
  306.     apr_port_t      port;
  307.     int             min;        /* Desired minimum number of available connections */
  308.     int             smax;       /* Soft maximum on the total number of connections */
  309.     int             hmax;       /* Hard maximum on the total number of connections */
  310.     apr_interval_time_t ttl;    /* maximum amount of time in seconds a connection
  311.                                  * may be available while exceeding the soft limit */
  312.     apr_interval_time_t timeout; /* connection timeout */
  313.     char            timeout_set;
  314.     apr_interval_time_t acquire; /* acquire timeout when the maximum number of connections is exceeded */
  315.     char            acquire_set;
  316.     apr_size_t      recv_buffer_size;
  317.     char            recv_buffer_size_set;
  318.     apr_size_t      io_buffer_size;
  319.     char            io_buffer_size_set;
  320.     char            keepalive;
  321.     char            keepalive_set;
  322.     proxy_conn_pool     *cp;        /* Connection pool to use */
  323.     proxy_worker_stat   *s;         /* Shared data */
  324.     void            *opaque;    /* per scheme worker data */
  325.     int             is_address_reusable;
  326. #if APR_HAS_THREADS
  327.     apr_thread_mutex_t  *mutex;  /* Thread lock for updating address cache */
  328. #endif
  329.     void            *context;   /* general purpose storage */
  330.     enum {
  331.          flush_off,
  332.          flush_on,
  333.          flush_auto
  334.     } flush_packets;           /* control AJP flushing */
  335.     int             flush_wait;  /* poll wait time in microseconds if flush_auto */
  336.     int             lbset;      /* load balancer cluster set */
  337.     apr_interval_time_t ping_timeout;
  338.     char ping_timeout_set;
  339.     char            retry_set;
  340. };
  341.  
  342. /*
  343.  * Wait 10000 microseconds to find out if more data is currently
  344.  * available at the backend. Just an arbitrary choose.
  345.  */
  346. #define PROXY_FLUSH_WAIT 10000
  347.  
  348. struct proxy_balancer {
  349.     apr_array_header_t *workers; /* array of proxy_workers */
  350.     const char *name;            /* name of the load balancer */
  351.     const char *sticky;          /* sticky session identifier */
  352.     int         sticky_force;    /* Disable failover for sticky sessions */
  353.     apr_interval_time_t timeout; /* Timeout for waiting on free connection */
  354.     int                 max_attempts; /* Number of attempts before failing */
  355.     char                max_attempts_set;
  356.     proxy_balancer_method *lbmethod;
  357.  
  358.     /* XXX: Perhaps we will need the proc mutex too.
  359.      * Altrough we are only using arithmetic operations
  360.      * it may lead to a incorrect calculations.
  361.      * For now use only the thread mutex.
  362.      */
  363. #if APR_HAS_THREADS
  364.     apr_thread_mutex_t  *mutex;  /* Thread lock for updating lb params */
  365. #endif
  366.     void            *context;   /* general purpose storage */
  367. };
  368.  
  369. struct proxy_balancer_method {
  370.     const char *name;            /* name of the load balancer method*/
  371.     proxy_worker *(*finder)(proxy_balancer *balancer,
  372.                             request_rec *r);
  373.     void            *context;   /* general purpose storage */
  374. };
  375.  
  376. #if APR_HAS_THREADS
  377. #define PROXY_THREAD_LOCK(x)      apr_thread_mutex_lock((x)->mutex)
  378. #define PROXY_THREAD_UNLOCK(x)    apr_thread_mutex_unlock((x)->mutex)
  379. #else
  380. #define PROXY_THREAD_LOCK(x)      APR_SUCCESS
  381. #define PROXY_THREAD_UNLOCK(x)    APR_SUCCESS
  382. #endif
  383.  
  384. /* hooks */
  385.  
  386. /* Create a set of PROXY_DECLARE(type), PROXY_DECLARE_NONSTD(type) and 
  387.  * PROXY_DECLARE_DATA with appropriate export and import tags for the platform
  388.  */
  389. #if !defined(WIN32)
  390. #define PROXY_DECLARE(type)            type
  391. #define PROXY_DECLARE_NONSTD(type)     type
  392. #define PROXY_DECLARE_DATA
  393. #elif defined(PROXY_DECLARE_STATIC)
  394. #define PROXY_DECLARE(type)            type __stdcall
  395. #define PROXY_DECLARE_NONSTD(type)     type
  396. #define PROXY_DECLARE_DATA
  397. #elif defined(PROXY_DECLARE_EXPORT)
  398. #define PROXY_DECLARE(type)            __declspec(dllexport) type __stdcall
  399. #define PROXY_DECLARE_NONSTD(type)     __declspec(dllexport) type
  400. #define PROXY_DECLARE_DATA             __declspec(dllexport)
  401. #else
  402. #define PROXY_DECLARE(type)            __declspec(dllimport) type __stdcall
  403. #define PROXY_DECLARE_NONSTD(type)     __declspec(dllimport) type
  404. #define PROXY_DECLARE_DATA             __declspec(dllimport)
  405. #endif
  406.  
  407. /**
  408.  * Hook an optional proxy hook.  Unlike static hooks, this uses a macro
  409.  * instead of a function.
  410.  */
  411. #define PROXY_OPTIONAL_HOOK(name,fn,pre,succ,order) \
  412.         APR_OPTIONAL_HOOK(proxy,name,fn,pre,succ,order)
  413.  
  414. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, scheme_handler, (request_rec *r, 
  415.                           proxy_worker *worker, proxy_server_conf *conf, char *url, 
  416.                           const char *proxyhost, apr_port_t proxyport))
  417. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, canon_handler, (request_rec *r, 
  418.                           char *url))
  419.  
  420. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, create_req, (request_rec *r, request_rec *pr))
  421. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, fixups, (request_rec *r)) 
  422.  
  423. /**
  424.  * pre request hook.
  425.  * It will return the most suitable worker at the moment
  426.  * and coresponding balancer.
  427.  * The url is rewritten from balancer://cluster/uri to scheme://host:port/uri
  428.  * and then the scheme_handler is called.
  429.  *
  430.  */
  431. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, pre_request, (proxy_worker **worker,
  432.                           proxy_balancer **balancer,
  433.                           request_rec *r,
  434.                           proxy_server_conf *conf, char **url))                          
  435. /**
  436.  * post request hook.
  437.  * It is called after request for updating runtime balancer status.
  438.  */
  439. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, post_request, (proxy_worker *worker,
  440.                           proxy_balancer *balancer, request_rec *r,
  441.                           proxy_server_conf *conf))
  442.  
  443. /**
  444.  * request status hook
  445.  * It is called after all proxy processing has been done.  This gives other
  446.  * modules a chance to create default content on failure, for example
  447.  */
  448. APR_DECLARE_EXTERNAL_HOOK(proxy, PROXY, int, request_status,
  449.                           (int *status, request_rec *r))
  450.  
  451. /* proxy_util.c */
  452.  
  453. PROXY_DECLARE(request_rec *)ap_proxy_make_fake_req(conn_rec *c, request_rec *r);
  454. PROXY_DECLARE(int) ap_proxy_hex2c(const char *x);
  455. PROXY_DECLARE(void) ap_proxy_c2hex(int ch, char *x);
  456. PROXY_DECLARE(char *)ap_proxy_canonenc(apr_pool_t *p, const char *x, int len, enum enctype t,
  457.                                        int forcedec, int proxyreq);
  458. PROXY_DECLARE(char *)ap_proxy_canon_netloc(apr_pool_t *p, char **const urlp, char **userp,
  459.                                            char **passwordp, char **hostp, apr_port_t *port);
  460. PROXY_DECLARE(const char *)ap_proxy_date_canon(apr_pool_t *p, const char *x);
  461. PROXY_DECLARE(int) ap_proxy_liststr(const char *list, const char *val);
  462. PROXY_DECLARE(char *)ap_proxy_removestr(apr_pool_t *pool, const char *list, const char *val);
  463. PROXY_DECLARE(int) ap_proxy_hex2sec(const char *x);
  464. PROXY_DECLARE(void) ap_proxy_sec2hex(int t, char *y);
  465. PROXY_DECLARE(int) ap_proxyerror(request_rec *r, int statuscode, const char *message);
  466. PROXY_DECLARE(int) ap_proxy_is_ipaddr(struct dirconn_entry *This, apr_pool_t *p);
  467. PROXY_DECLARE(int) ap_proxy_is_domainname(struct dirconn_entry *This, apr_pool_t *p);
  468. PROXY_DECLARE(int) ap_proxy_is_hostname(struct dirconn_entry *This, apr_pool_t *p);
  469. PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
  470. PROXY_DECLARE(int) ap_proxy_checkproxyblock(request_rec *r, proxy_server_conf *conf, apr_sockaddr_t *uri_addr);
  471. PROXY_DECLARE(int) ap_proxy_pre_http_request(conn_rec *c, request_rec *r);
  472. PROXY_DECLARE(apr_status_t) ap_proxy_string_read(conn_rec *c, apr_bucket_brigade *bb, char *buff, size_t bufflen, int *eos);
  473. PROXY_DECLARE(void) ap_proxy_table_unmerge(apr_pool_t *p, apr_table_t *t, char *key);
  474. /* DEPRECATED (will be replaced with ap_proxy_connect_backend */
  475. PROXY_DECLARE(int) ap_proxy_connect_to_backend(apr_socket_t **, const char *, apr_sockaddr_t *, const char *, proxy_server_conf *, server_rec *, apr_pool_t *);
  476. PROXY_DECLARE(int) ap_proxy_ssl_enable(conn_rec *c);
  477. PROXY_DECLARE(int) ap_proxy_ssl_disable(conn_rec *c);
  478. PROXY_DECLARE(int) ap_proxy_conn_is_https(conn_rec *c);
  479. PROXY_DECLARE(const char *) ap_proxy_ssl_val(apr_pool_t *p, server_rec *s, conn_rec *c, request_rec *r, const char *var);
  480.  
  481. /* Header mapping functions, and a typedef of their signature */
  482. PROXY_DECLARE(const char *) ap_proxy_location_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *url);
  483. PROXY_DECLARE(const char *) ap_proxy_cookie_reverse_map(request_rec *r, proxy_dir_conf *conf, const char *str);
  484.  
  485. #if !defined(WIN32)
  486. typedef const char *(*ap_proxy_header_reverse_map_fn)(request_rec *,
  487.                        proxy_dir_conf *, const char *);
  488. #elif defined(PROXY_DECLARE_STATIC)
  489. typedef const char *(__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
  490.                                  proxy_dir_conf *, const char *);
  491. #elif defined(PROXY_DECLARE_EXPORT)
  492. typedef __declspec(dllexport) const char *
  493.   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
  494.                proxy_dir_conf *, const char *);
  495. #else
  496. typedef __declspec(dllimport) const char *
  497.   (__stdcall *ap_proxy_header_reverse_map_fn)(request_rec *,
  498.                proxy_dir_conf *, const char *);
  499. #endif
  500.  
  501.  
  502. /* Connection pool API */
  503. /**
  504.  * Get the worker from proxy configuration
  505.  * @param p     memory pool used for finding worker
  506.  * @param conf  current proxy server configuration
  507.  * @param url   url to find the worker from
  508.  * @return      proxy_worker or NULL if not found
  509.  */
  510. PROXY_DECLARE(proxy_worker *) ap_proxy_get_worker(apr_pool_t *p,
  511.                                                   proxy_server_conf *conf,
  512.                                                   const char *url);
  513. /**
  514.  * Add the worker to proxy configuration
  515.  * @param worker the new worker
  516.  * @param p      memory pool to allocate worker from 
  517.  * @param conf   current proxy server configuration
  518.  * @param url    url containing worker name
  519.  * @return       error message or NULL if successfull
  520.  */
  521. PROXY_DECLARE(const char *) ap_proxy_add_worker(proxy_worker **worker,
  522.                                                 apr_pool_t *p,
  523.                                                 proxy_server_conf *conf,
  524.                                                 const char *url);
  525.  
  526. /**
  527.  * Create new worker
  528.  * @param p      memory pool to allocate worker from 
  529.  * @return       new worker
  530.  */
  531. PROXY_DECLARE(proxy_worker *) ap_proxy_create_worker(apr_pool_t *p);
  532.  
  533. /**
  534.  * Initize the worker's shared data
  535.  * @param conf   current proxy server configuration
  536.  * @param worker worker to initialize
  537.  * @param s      current server record
  538.  * @param worker worker to initialize
  539.  */
  540. PROXY_DECLARE(void) ap_proxy_initialize_worker_share(proxy_server_conf *conf,
  541.                                                      proxy_worker *worker,
  542.                                                      server_rec *s);
  543.  
  544.  
  545. /**
  546.  * Initize the worker
  547.  * @param worker worker to initialize
  548.  * @param s      current server record
  549.  * @return       APR_SUCCESS or error code
  550.  */
  551. PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker,
  552.                                                        server_rec *s);
  553. /**
  554.  * Get the balancer from proxy configuration
  555.  * @param p     memory pool used for finding balancer
  556.  * @param conf  current proxy server configuration
  557.  * @param url   url to find the worker from. Has to have balancer:// prefix
  558.  * @return      proxy_balancer or NULL if not found
  559.  */
  560. PROXY_DECLARE(proxy_balancer *) ap_proxy_get_balancer(apr_pool_t *p,
  561.                                                       proxy_server_conf *conf,
  562.                                                       const char *url);
  563. /**
  564.  * Add the balancer to proxy configuration
  565.  * @param balancer the new balancer
  566.  * @param p      memory pool to allocate balancer from 
  567.  * @param conf   current proxy server configuration
  568.  * @param url    url containing balancer name
  569.  * @return       error message or NULL if successfull
  570.  */
  571. PROXY_DECLARE(const char *) ap_proxy_add_balancer(proxy_balancer **balancer,
  572.                                                   apr_pool_t *p,
  573.                                                   proxy_server_conf *conf,
  574.                                                   const char *url);
  575.  
  576. /**
  577.  * Add the worker to the balancer
  578.  * @param pool     memory pool for adding worker 
  579.  * @param balancer balancer to add to
  580.  * @param balancer worker to add
  581.  * @note Single worker can be added to multiple balancers.
  582.  */
  583. PROXY_DECLARE(void) ap_proxy_add_worker_to_balancer(apr_pool_t *pool,
  584.                                                     proxy_balancer *balancer,
  585.                                                     proxy_worker *worker);
  586. /**
  587.  * Get the most suitable worker and(or) balancer for the request
  588.  * @param worker   worker used for processing request
  589.  * @param balancer balancer used for processing request
  590.  * @param r        current request
  591.  * @param conf     current proxy server configuration
  592.  * @param url      request url that balancer can rewrite.
  593.  * @return         OK or  HTTP_XXX error 
  594.  * @note It calls balancer pre_request hook if the url starts with balancer://
  595.  * The balancer then rewrites the url to particular worker, like http://host:port
  596.  */
  597. PROXY_DECLARE(int) ap_proxy_pre_request(proxy_worker **worker,
  598.                                         proxy_balancer **balancer,
  599.                                         request_rec *r,
  600.                                         proxy_server_conf *conf,
  601.                                         char **url);
  602. /**
  603.  * Post request worker and balancer cleanup
  604.  * @param worker   worker used for processing request
  605.  * @param balancer balancer used for processing request
  606.  * @param r        current request
  607.  * @param conf     current proxy server configuration
  608.  * @return         OK or  HTTP_XXX error
  609.  * @note When ever the pre_request is called, the post_request has to be
  610.  * called too. 
  611.  */
  612. PROXY_DECLARE(int) ap_proxy_post_request(proxy_worker *worker,
  613.                                          proxy_balancer *balancer,
  614.                                          request_rec *r,
  615.                                          proxy_server_conf *conf);
  616.  
  617. /**
  618.  * Request status function
  619.  * @param status   status of proxy request
  620.  * @return         OK or DECLINED
  621.  */
  622.  PROXY_DECLARE(int) ap_proxy_request_status(int *status, request_rec *r);
  623.  
  624. /**
  625.  * Deternime backend hostname and port
  626.  * @param p       memory pool used for processing
  627.  * @param r       current request
  628.  * @param conf    current proxy server configuration
  629.  * @param worker  worker used for processing request
  630.  * @param conn    proxy connection struct
  631.  * @param uri     processed uri
  632.  * @param url     request url
  633.  * @param proxyname are we connecting directly or via s proxy
  634.  * @param proxyport proxy host port
  635.  * @param server_portstr Via headers server port
  636.  * @param server_portstr_size size of the server_portstr buffer
  637.  * @return         OK or HTTP_XXX error
  638.  */                                         
  639. PROXY_DECLARE(int) ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
  640.                                                  proxy_server_conf *conf,
  641.                                                  proxy_worker *worker,
  642.                                                  proxy_conn_rec *conn,
  643.                                                  apr_uri_t *uri,
  644.                                                  char **url,
  645.                                                  const char *proxyname,
  646.                                                  apr_port_t proxyport,
  647.                                                  char *server_portstr,
  648.                                                  int server_portstr_size);
  649. /**
  650.  * Mark a worker for retry
  651.  * @param proxy_function calling proxy scheme (http, ajp, ...)
  652.  * @param conf    current proxy server configuration
  653.  * @param worker  worker used for retrying
  654.  * @param s       current server record
  655.  * @return        OK if marked for retry, DECLINED otherwise
  656.  * @note Worker will be marker for retry if the time of the last retry
  657.  * has been ellapsed. In case there is no retry option set, defaults to
  658.  * number_of_retries seconds.
  659.  */                                         
  660. PROXY_DECLARE(int) ap_proxy_retry_worker(const char *proxy_function,
  661.                                          proxy_worker *worker,
  662.                                          server_rec *s);
  663. /**
  664.  * Acquire a connection from workers connection pool
  665.  * @param proxy_function calling proxy scheme (http, ajp, ...)
  666.  * @param conn    acquired connection
  667.  * @param worker  worker used for obtaining connection
  668.  * @param s       current server record
  669.  * @return        OK or HTTP_XXX error
  670.  * @note If the number of connections is exhaused the function will
  671.  * block untill the timeout is reached.
  672.  */                                         
  673. PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
  674.                                                proxy_conn_rec **conn,
  675.                                                proxy_worker *worker,
  676.                                                server_rec *s);
  677. /**
  678.  * Release a connection back to worker connection pool
  679.  * @param proxy_function calling proxy scheme (http, ajp, ...)
  680.  * @param conn    acquired connection
  681.  * @param s       current server record
  682.  * @return        OK or HTTP_XXX error
  683.  * @note The connection will be closed if conn->close_on_release is set
  684.  */                                         
  685. PROXY_DECLARE(int) ap_proxy_release_connection(const char *proxy_function,
  686.                                                proxy_conn_rec *conn,
  687.                                                server_rec *s);
  688. /**
  689.  * Make a connection to the backend
  690.  * @param proxy_function calling proxy scheme (http, ajp, ...)
  691.  * @param conn    acquired connection
  692.  * @param worker  connection worker
  693.  * @param s       current server record
  694.  * @return        OK or HTTP_XXX error
  695.  * @note In case the socket already exists for conn, just check the link
  696.  * status.
  697.  */                                         
  698. PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
  699.                                             proxy_conn_rec *conn,
  700.                                             proxy_worker *worker,
  701.                                             server_rec *s);
  702. /**
  703.  * Make a connection record for backend connection
  704.  * @param proxy_function calling proxy scheme (http, ajp, ...)
  705.  * @param conn    acquired connection
  706.  * @param c       client connection record
  707.  * @param s       current server record
  708.  * @return        OK or HTTP_XXX error
  709.  */                                         
  710. PROXY_DECLARE(int) ap_proxy_connection_create(const char *proxy_function,
  711.                                               proxy_conn_rec *conn,
  712.                                               conn_rec *c, server_rec *s);
  713. /**
  714.  * Signal the upstream chain that the connection to the backend broke in the
  715.  * middle of the response. This is done by sending an error bucket with
  716.  * status HTTP_BAD_GATEWAY and an EOS bucket up the filter chain.
  717.  * @param r       current request record of client request
  718.  * @param brigade The brigade that is sent through the output filter chain
  719.  */
  720. PROXY_DECLARE(void) ap_proxy_backend_broke(request_rec *r,
  721.                                            apr_bucket_brigade *brigade);
  722.  
  723. /* Scoreboard */
  724. #if MODULE_MAGIC_NUMBER_MAJOR > 20020903
  725. #define PROXY_HAS_SCOREBOARD 1
  726. #else
  727. #define PROXY_HAS_SCOREBOARD 0
  728. #endif
  729.  
  730. #define PROXY_LBMETHOD "proxylbmethod"
  731.  
  732. /* The number of dynamic workers that can be added when reconfiguring.
  733.  * If this limit is reached you must stop and restart the server.
  734.  */
  735. #define PROXY_DYNAMIC_BALANCER_LIMIT    16
  736. /**
  737.  * Calculate number of maximum number of workers in scoreboard.
  738.  * @return  number of workers to allocate in the scoreboard
  739.  */
  740. int ap_proxy_lb_workers(void);
  741.  
  742. /* For proxy_util */
  743. extern module PROXY_DECLARE_DATA proxy_module;
  744.  
  745. extern int PROXY_DECLARE_DATA proxy_lb_workers;
  746.  
  747. #endif /*MOD_PROXY_H*/
  748. /** @} */
  749.