1 /* $MirOS: src/usr.sbin/httpd/src/modules/proxy/mod_proxy.h,v 1.4 2008/03/19 23:07:22 tg Exp $ */
2 
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  *    if any, must include the following acknowledgment:
23  *       "This product includes software developed by the
24  *        Apache Software Foundation (http://www.apache.org/)."
25  *    Alternately, this acknowledgment may appear in the software itself,
26  *    if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" must
29  *    not be used to endorse or promote products derived from this
30  *    software without prior written permission. For written
31  *    permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  *    nor may "Apache" appear in their name, without prior written
35  *    permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation.  For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  * Portions of this software are based upon public domain software
57  * originally written at the National Center for Supercomputing Applications,
58  * University of Illinois, Urbana-Champaign.
59  */
60 
61 #ifndef MOD_PROXY_H
62 #define MOD_PROXY_H
63 
64 /*
65  * Main include file for the Apache proxy
66  */
67 
68 /*
69 
70    Note numerous FIXMEs and CHECKMEs which should be eliminated.
71 
72    If TESTING is set, then garbage collection doesn't delete ... probably a good
73    idea when hacking.
74 
75  */
76 
77 #define TESTING 0
78 
79 #include "httpd.h"
80 #include "http_config.h"
81 #include "http_protocol.h"
82 
83 #include "explain.h"
84 
85 extern module MODULE_VAR_EXPORT proxy_module;
86 
87 
88 /* for proxy_canonenc() */
89 enum enctype {
90     enc_path, enc_search, enc_user, enc_fpath, enc_parm
91 };
92 
93 #define HDR_APP (0)             /* append header, for proxy_add_header() */
94 #define HDR_REP (1)             /* replace header, for proxy_add_header() */
95 
96 /* number of characters in the hash */
97 #define HASH_LEN (22*2)
98 
99 /* maximum  'CacheDirLevels*CacheDirLength' value */
100 #define CACHEFILE_LEN 20        /* must be less than HASH_LEN/2 */
101 
102 #define SEC_ONE_DAY             86400   /* one day, in seconds */
103 #define SEC_ONE_HR              3600    /* one hour, in seconds */
104 
105 #define DEFAULT_FTP_DATA_PORT   20
106 #define DEFAULT_FTP_PORT        21
107 #define DEFAULT_GOPHER_PORT     70
108 #define DEFAULT_NNTP_PORT       119
109 #define DEFAULT_WAIS_PORT       210
110 #define DEFAULT_HTTPS_PORT      443
111 #define DEFAULT_SNEWS_PORT      563
112 #define DEFAULT_PROSPERO_PORT   1525    /* WARNING: conflict w/Oracle */
113 
114 /* Some WWW schemes and their default ports; this is basically /etc/services */
115 struct proxy_services {
116     const char *scheme;
117     int port;
118 };
119 
120 /* static information about a remote proxy */
121 struct proxy_remote {
122     const char *scheme;         /* the schemes handled by this proxy, or '*' */
123     const char *protocol;       /* the scheme used to talk to this proxy */
124     const char *hostname;       /* the hostname of this proxy */
125     int port;                   /* the port for this proxy */
126 };
127 
128 struct proxy_alias {
129     char *real;
130     char *fake;
131 };
132 
133 struct dirconn_entry {
134     char *name;
135     struct in_addr addr, mask;
136     struct hostent *hostentry;
137     int (*matcher) (struct dirconn_entry * This, request_rec *r);
138 };
139 
140 struct noproxy_entry {
141     char *name;
142     struct in_addr addr;
143 };
144 
145 struct nocache_entry {
146     char *name;
147     struct in_addr addr;
148 };
149 
150 #define DEFAULT_CACHE_SPACE 5
151 #define DEFAULT_CACHE_MAXEXPIRE SEC_ONE_DAY
152 #define DEFAULT_CACHE_EXPIRE    SEC_ONE_HR
153 #define DEFAULT_CACHE_LMFACTOR (0.1)
154 #define DEFAULT_CACHE_COMPLETION (0.9)
155 #define DEFAULT_CACHE_GCINTERVAL SEC_ONE_HR
156 
157 #ifndef MAX
158 #define MAX(a,b)                ((a) > (b) ? (a) : (b))
159 #endif
160 #ifndef MIN
161 #define MIN(a,b)                ((a) < (b) ? (a) : (b))
162 #endif
163 
164 /* static information about the local cache */
165 struct cache_conf {
166     const char *root;           /* the location of the cache directory */
167     off_t space;                /* Maximum cache size (in 1024 bytes) */
168     char space_set;
169     time_t maxexpire;           /* Maximum time to keep cached files in secs */
170     char maxexpire_set;
171     time_t defaultexpire;       /* default time to keep cached file in secs */
172     char defaultexpire_set;
173     double lmfactor;            /* factor for estimating expires date */
174     char lmfactor_set;
175     time_t gcinterval;          /* garbage collection interval, in seconds */
176     char gcinterval_set;
177     int dirlevels;              /* Number of levels of subdirectories */
178     char dirlevels_set;
179     int dirlength;              /* Length of subdirectory names */
180     char dirlength_set;
181     float cache_completion;     /* Force cache completion after this point */
182     char cache_completion_set;
183 };
184 
185 typedef struct {
186     struct cache_conf cache;    /* cache configuration */
187     array_header *proxies;
188     array_header *aliases;
189     array_header *raliases;
190     array_header *noproxies;
191     array_header *dirconn;
192     array_header *nocaches;
193     array_header *allowed_connect_ports;
194     char *domain;               /* domain name to use in absence of a domain name in the request */
195     int req;                    /* true if proxy requests are enabled */
196     char req_set;
197     enum {
198       via_off,
199       via_on,
200       via_block,
201       via_full
202     } viaopt;                   /* how to deal with proxy Via: headers */
203     char viaopt_set;
204     size_t recv_buffer_size;
205     char recv_buffer_size_set;
206     size_t io_buffer_size;
207     char io_buffer_size_set;
208     int preserve_host;
209     int preserve_host_set;
210 } proxy_server_conf;
211 
212 struct hdr_entry {
213     const char *field;
214     const char *value;
215 };
216 
217 /* caching information about a request */
218 typedef struct {
219     request_rec *req;           /* the request */
220     char *url;                  /* the URL requested */
221     char *filename;             /* name of the cache file,
222                                    or NULL if no cache */
223     char *tempfile;             /* name of the temporary file,
224                                    or NULL if not caching */
225     time_t ims;                 /* if-Modified-Since date of request,
226                                    -1 if no header */
227     time_t ius;                 /* if-Unmodified-Since date of request,
228                                    -1 if no header */
229     const char *im;             /* if-Match etag of request,
230                                    NULL if no header */
231     const char *inm;            /* if-None-Match etag of request,
232                                    NULL if no header */
233     BUFF *fp;                   /* the cache file descriptor if the file
234                                    is cached and may be returned,
235                                    or NULL if the file is not cached
236                                    (or must be reloaded) */
237     BUFF *origfp;               /* the old cache file descriptor if the file has
238                                    been revalidated and is being rewritten to
239                                    disk */
240     time_t expire;              /* calculated expire date of cached entity */
241     time_t lmod;                /* last-modified date of cached entity */
242     time_t date;                /* the date the cached file was last touched */
243     time_t req_time;            /* the time the request started */
244     time_t resp_time;           /* the time the response was received */
245     int version;                /* update count of the file */
246     off_t len;                  /* content length */
247     char *protocol;             /* Protocol, and major/minor number,
248                                    e.g. HTTP/1.1 */
249     int status;                 /* the status of the cached file */
250     unsigned int written;       /* total *content* bytes written to cache */
251     float cache_completion;     /* specific to this request */
252     char *resp_line;            /* the whole status line
253                                    (protocol, code + message) */
254     table *req_hdrs;            /* the original request headers */
255     table *hdrs;                /* the original HTTP response headers
256                                    of the file */
257     char *xcache;               /* the X-Cache header value
258                                    to be sent to client */
259 } cache_req;
260 
261 struct per_thread_data {
262     struct hostent hpbuf;
263     in_addr_t ipaddr;
264     char *charpbuf[2];
265 };
266 /* Function prototypes */
267 
268 /* proxy_cache.c */
269 
270 void ap_proxy_cache_tidy(cache_req *c);
271 int ap_proxy_cache_check(request_rec *r, char *url, struct cache_conf *conf,
272                       cache_req **cr);
273 int ap_proxy_cache_update(cache_req *c, table *resp_hdrs,
274                        const int is_HTTP1, int nocache);
275 void ap_proxy_garbage_coll(request_rec *r);
276 
277 /* proxy_connect.c */
278 
279 int ap_proxy_connect_handler(request_rec *r, cache_req *c, char *url,
280                           const char *proxyhost, int proxyport);
281 
282 /* proxy_ftp.c */
283 
284 int ap_proxy_ftp_canon(request_rec *r, char *url);
285 int ap_proxy_ftp_handler(request_rec *r, cache_req *c, char *url);
286 
287 /* proxy_http.c */
288 
289 int ap_proxy_http_canon(request_rec *r, char *url, const char *scheme,
290                      int def_port);
291 int ap_proxy_http_handler(request_rec *r, cache_req *c, char *url,
292                        const char *proxyhost, int proxyport);
293 
294 /* proxy_util.c */
295 
296 int ap_proxy_hex2c(const char *x);
297 void ap_proxy_c2hex(int ch, char *x);
298 char *ap_proxy_canonenc(pool *p, const char *x, int len, enum enctype t,
299                         enum proxyreqtype isenc);
300 char *ap_proxy_canon_netloc(pool *p, char **const urlp, char **userp,
301                          char **passwordp, char **hostp, int *port);
302 const char *ap_proxy_date_canon(pool *p, const char *x);
303 table *ap_proxy_read_headers(request_rec *r, char *buffer, int size, BUFF *f);
304 long int ap_proxy_send_fb(BUFF *f, request_rec *r, cache_req *c, off_t len, int nowrite, int chunked, size_t recv_buffer_size);
305 void ap_proxy_write_headers(cache_req *c, const char *respline, table *t);
306 int ap_proxy_liststr(const char *list, const char *key, char **val);
307 void ap_proxy_hash(const char *it, char *val, int ndepth, int nlength);
308 int ap_proxy_hex2sec(const char *x);
309 int ap_proxy_sec2hex(int t, char *y, int len);
310 cache_req *ap_proxy_cache_error(cache_req *r);
311 int ap_proxyerror(request_rec *r, int statuscode, const char *message);
312 const char *ap_proxy_host2addr(const char *host, struct hostent *reqhp);
313 int ap_proxy_is_ipaddr(struct dirconn_entry *This, pool *p);
314 int ap_proxy_is_domainname(struct dirconn_entry *This, pool *p);
315 int ap_proxy_is_hostname(struct dirconn_entry *This, pool *p);
316 int ap_proxy_is_word(struct dirconn_entry *This, pool *p);
317 int ap_proxy_doconnect(int sock, struct sockaddr *addr, request_rec *r);
318 int ap_proxy_garbage_init(server_rec *, pool *);
319 /* This function is called by ap_table_do() for all header lines */
320 int ap_proxy_send_hdr_line(void *p, const char *key, const char *value);
321 unsigned ap_proxy_bputs2(const char *data, BUFF *client, cache_req *cache);
322 time_t ap_proxy_current_age(cache_req *c, const time_t age_value);
323 BUFF *ap_proxy_open_cachefile(request_rec *r, char *filename);
324 BUFF *ap_proxy_create_cachefile(request_rec *r, char *filename);
325 void ap_proxy_clear_connection(pool *p, table *headers);
326 int ap_proxy_table_replace(table *base, table *overlay);
327 void ap_proxy_table_unmerge(pool *p, table *t, char *key);
328 int ap_proxy_read_response_line(BUFF *f, request_rec *r, char *buffer, int size, int *backasswards, int *major, int *minor);
329 
330 /* WARNING - PRIVATE DEFINITION BELOW */
331 
332 /* XXX: if you tweak this you should look at is_empty_table() and table_elts()
333  * in ap_alloc.h
334  *
335  * NOTE: this private definition is a duplicate of the one in alloc.c
336  * It's here for ap_proxy_table_replace() to avoid breaking binary compat
337  */
338 struct table {
339     /* This has to be first to promote backwards compatibility with
340      * older modules which cast a table * to an array_header *...
341      * they should use the table_elts() function for most of the
342      * cases they do this for.
343      */
344     array_header a;
345 #ifdef MAKE_TABLE_PROFILE
346     void *creator;
347 #endif
348 };
349 
350 #endif /*MOD_PROXY_H*/
351