Annotation of libwww/Library/src/HTFilter.c, revision 2.34
2.1 frystyk 1: /*
2: ** BEFORE AND AFTER FILTERS
3: **
4: ** (c) COPYRIGHT MIT 1995.
5: ** Please first read the full copyright statement in the file COPYRIGH.
2.34 ! frystyk 6: ** @(#) $Id: HTFilter.c,v 2.33 1999/02/03 16:35:11 frystyk Exp $
2.1 frystyk 7: **
8: ** This module implrments a set of default filters that can be registerd
9: ** as BEFORE and AFTER filters to the Net manager
10: ** Authors
11: ** HFN Henrik Frystyk, frystyk@w.org
12: ** History
13: ** Jul 4, 96 Written
14: */
15:
16: /* Library include files */
17: #include "WWWLib.h"
18: #include "WWWCache.h"
19: #include "WWWHTTP.h"
20: #include "HTLog.h"
21: #include "HTAccess.h"
2.10 frystyk 22: #include "HTProxy.h"
23: #include "HTRules.h"
2.1 frystyk 24: #include "HTFilter.h" /* Implemented here */
25:
26: /* ------------------------------------------------------------------------- */
27:
28: /*
29: ** Proxy and Gateway BEFORE filter
30: ** -------------------------------
31: ** Checks for registerd proxy servers or gateways and sees whether this
32: ** request should be redirected to a proxy or a gateway. Proxies have
33: ** higher priority than gateways so we look for them first!
34: ** For HTTP/1.0 and HTTP/1.1 we may only send a full URL (including the
35: ** host portion) to proxy servers. Therefore, we tell the Library whether
36: ** to use the full URL or the traditional HTTP one without the host part.
37: */
2.15 frystyk 38: PUBLIC int HTProxyFilter (HTRequest * request, void * param, int mode)
2.1 frystyk 39: {
40: HTParentAnchor * anchor = HTRequest_anchor(request);
2.2 frystyk 41: char * addr = HTAnchor_physical(anchor);
2.1 frystyk 42: char * physical = NULL;
43: if ((physical = HTProxy_find(addr))) {
2.6 frystyk 44: HTRequest_setFullURI(request, YES); /* For now */
2.5 frystyk 45: HTRequest_setProxy(request, physical);
2.8 frystyk 46: HT_FREE(physical);
2.6 frystyk 47: #if 0
48: /* Don't paste the URLs together anymore */
2.1 frystyk 49: StrAllocCat(physical, addr);
2.5 frystyk 50: HTAnchor_setPhysical(anchor, physical);
2.6 frystyk 51: #endif
2.1 frystyk 52: } else if ((physical = HTGateway_find(addr))) {
53: /*
54: ** A gateway URL is crated by chopping off any leading "/" to make the
55: ** host into part of path
56: */
57: char * path =
58: HTParse(addr, "", PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
2.26 frystyk 59: char * gatewayed = HTParse((*path=='/') ? path+1 : path, physical, PARSE_ALL);
2.1 frystyk 60: HTAnchor_setPhysical(anchor, gatewayed);
61: HT_FREE(path);
62: HT_FREE(gatewayed);
63: HTRequest_setFullURI(request, NO);
2.6 frystyk 64: HTRequest_deleteProxy(request);
2.1 frystyk 65: } else {
2.6 frystyk 66: HTRequest_setFullURI(request, NO); /* For now */
67: HTRequest_deleteProxy(request);
2.1 frystyk 68: }
69: return HT_OK;
70: }
71:
72: /*
73: ** Rule Translation BEFORE Filter
74: ** ------------------------------
75: ** If we have a set of rules loaded (see the Rule manager) then check
76: ** before each request whether how that should be translated. The trick
77: ** is that a parent anchor has a "address" which is the part from the URL
78: ** we used when we created the anchor. However, it also have a "physical
79: ** address" which is the place we are actually going to look for the
2.2 frystyk 80: ** resource. Hence this filter translates the physical address
81: ** (if any translations are found)
2.1 frystyk 82: */
2.15 frystyk 83: PUBLIC int HTRuleFilter (HTRequest * request, void * param, int mode)
2.1 frystyk 84: {
85: HTList * list = HTRule_global();
86: HTParentAnchor * anchor = HTRequest_anchor(request);
2.2 frystyk 87: char * addr = HTAnchor_physical(anchor);
2.1 frystyk 88: char * physical = HTRule_translate(list, addr, NO);
89: if (!physical) {
90: HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
91: NULL, 0, "HTRuleFilter");
92: return HT_ERROR;
93: }
94: HTAnchor_setPhysical(anchor, physical);
95: HT_FREE(physical);
96: return HT_OK;
97: }
98:
99: /*
2.16 frystyk 100: ** A small BEFORE filter that just finds a cache entry unconditionally
101: ** and loads the entry. All freshness and any other constraints are
102: ** ignored.
103: */
104: PUBLIC int HTCacheLoadFilter (HTRequest * request, void * param, int mode)
105: {
106: HTParentAnchor * anchor = HTRequest_anchor(request);
107: HTCache * cache = HTCache_find(anchor);
108: if (cache) {
109: char * name = HTCache_name(cache);
110: HTAnchor_setPhysical(anchor, name);
111: HTCache_addHit(cache);
112: HT_FREE(name);
113:
114: /*
115: ** Start request directly from the cache. As with the redirection
116: ** filter we reuse the same request object which means that we must
117: ** keep this around until the cache load request has terminated
118: */
119: {
120: HTLoad(request, NO);
121: return HT_ERROR;
122: }
123: }
124: return HT_OK;
125: }
126:
127: /*
2.15 frystyk 128: ** Check the Memory Cache (History list) BEFORE filter
129: ** ---------------------------------------------------
2.11 frystyk 130: ** Check if document is already loaded. The user can define whether
131: ** the history list should follow normal expiration or work as a
132: ** traditional history list where expired documents are not updated.
133: ** We don't check for anything but existence proof of a document
134: ** associated with the anchor as the definition is left to the application
135: */
2.15 frystyk 136: PUBLIC int HTMemoryCacheFilter (HTRequest * request, void * param, int mode)
2.11 frystyk 137: {
138: HTReload validation = HTRequest_reloadMode(request);
139: HTParentAnchor * anchor = HTRequest_anchor(request);
140: void * document = HTAnchor_document(anchor);
2.1 frystyk 141:
2.11 frystyk 142: /*
2.14 frystyk 143: ** We only check the memory cache if it's a GET method
144: */
145: if (HTRequest_method(request) != METHOD_GET) {
146: if (CACHE_TRACE) HTTrace("Mem Cache... We only check GET methods\n");
147: return HT_OK;
148: }
149:
150: /*
2.11 frystyk 151: ** If we are asked to flush the persistent cache then there is no reason
152: ** to do anything here - we're flushing it anyway. Also if no document
153: ** then just exit from this filter.
154: */
155: if (!document || validation > HT_CACHE_FLUSH_MEM) {
156: if (CACHE_TRACE) HTTrace("Mem Cache... No fresh document...\n");
157: return HT_OK;
158: }
2.1 frystyk 159:
2.11 frystyk 160: /*
161: ** If we have a document object associated with this anchor then we also
162: ** have the object in the history list. Depending on what the user asked,
163: ** we can add a cache validator
164: */
2.14 frystyk 165: if (document && validation != HT_CACHE_FLUSH_MEM) {
166: if (CACHE_TRACE) HTTrace("Mem Cache... Document already in memory\n");
167: return HT_LOADED;
2.15 frystyk 168: }
2.17 frystyk 169: return HT_OK;
2.15 frystyk 170: }
171:
172: /*
2.1 frystyk 173: ** Error and Information AFTER filter
174: ** ----------------------------------
175: ** It checks the status code from a request and generates an
176: ** error/information message if required.
177: */
2.15 frystyk 178: PUBLIC int HTInfoFilter (HTRequest * request, HTResponse * response,
179: void * param, int status)
2.1 frystyk 180: {
181: HTParentAnchor * anchor = HTRequest_anchor(request);
182: char * uri = HTAnchor_address((HTAnchor*) anchor);
183: switch (status) {
2.28 frystyk 184: case HT_RETRY: {
185: HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
186: if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
187: HTRequest_error(request), NULL);
2.1 frystyk 188: if (PROT_TRACE)
189: HTTrace("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
2.15 frystyk 190: HTResponse_retryTime(response));
2.28 frystyk 191: }
192: break;
2.1 frystyk 193:
2.7 frystyk 194: case HT_NO_DATA:
195: {
196: /*
197: ** The document was empty
198: */
199: HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
200: if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
201: HTRequest_error(request), NULL);
202: if (PROT_TRACE)
203: HTTrace("Load End.... EMPTY: No content `%s\'\n",
204: uri ? uri : "<UNKNOWN>");
205: break;
206: }
2.3 frystyk 207:
208: case HT_LOADED:
2.16 frystyk 209: if (PROT_TRACE) HTTrace("Load End.... OK: `%s\'\n", uri);
210: break;
211:
212: default:
2.3 frystyk 213: {
214: /*
2.16 frystyk 215: ** See if we have a function registered for outputting errors.
216: ** If so then call it and present the message to the user
2.3 frystyk 217: */
218: HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
219: if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
220: HTRequest_error(request), NULL);
2.1 frystyk 221: if (PROT_TRACE)
222: HTTrace("Load End.... Request ended with code %d\n", status);
223: break;
224: }
2.16 frystyk 225: }
2.1 frystyk 226: HT_FREE(uri);
227: return HT_OK;
228: }
229:
230: /*
231: ** Redirection AFTER filter
232: ** ------------------------
233: ** The redirection handler only handles redirections
234: ** on the GET or HEAD method (or any other safe method)
235: */
2.15 frystyk 236: PUBLIC int HTRedirectFilter (HTRequest * request, HTResponse * response,
237: void * param, int status)
2.1 frystyk 238: {
239: HTMethod method = HTRequest_method(request);
2.15 frystyk 240: HTAnchor * new_anchor = HTResponse_redirection(response);
2.7 frystyk 241: if (!new_anchor) {
242: if (PROT_TRACE) HTTrace("Redirection. No destination\n");
243: return HT_OK;
244: }
245:
2.1 frystyk 246: /*
2.21 frystyk 247: ** Only do automatic redirect on GET and HEAD. Ask for all
2.27 frystyk 248: ** other methods.
2.1 frystyk 249: */
2.21 frystyk 250: if (!HTMethod_isSafe(method)) {
251:
252: /*
253: ** If we got a 303 See Other then change the method to GET.
254: ** Otherwise ask the user whether we should continue.
255: */
256: if (status == HT_SEE_OTHER) {
257: if (PROT_TRACE)
258: HTTrace("Redirection. Changing method from %s to GET\n",
259: HTMethod_name(method));
260: HTRequest_setMethod(request, METHOD_GET);
261: } else {
262: HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
263: if (prompt) {
264: if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
265: NULL, NULL, NULL) != YES)
2.27 frystyk 266: return HT_OK;
2.21 frystyk 267: }
2.4 frystyk 268: }
2.1 frystyk 269: }
270:
2.34 ! frystyk 271: /* Delete any auth credendials as they get regenerated */
! 272: HTRequest_deleteCredentialsAll(request);
! 273:
2.1 frystyk 274: /*
275: ** Start new request with the redirect anchor found in the headers.
276: ** Note that we reuse the same request object which means that we must
277: ** keep this around until the redirected request has terminated. It also
278: ** allows us in an easy way to keep track of the number of redirections
279: ** so that we can detect endless loops.
280: */
2.4 frystyk 281: if (HTRequest_doRetry(request)) {
2.1 frystyk 282: HTLoadAnchor(new_anchor, request);
2.9 frystyk 283: } else {
284: HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
285: NULL, 0, "HTRedirectFilter");
286: }
287:
288: /*
289: ** By returning HT_ERROR we make sure that this is the last handler to be
290: ** called. We do this as we don't want any other filter to delete the
291: ** request object now when we have just started a new one ourselves
292: */
293: return HT_ERROR;
294: }
295:
296: /*
2.15 frystyk 297: ** Retry through Proxy AFTER Filter
298: ** --------------------------------
2.9 frystyk 299: ** This filter handles a 305 Use Proxy response and retries the request
300: ** through the proxy
301: */
2.15 frystyk 302: PUBLIC int HTUseProxyFilter (HTRequest * request, HTResponse * response,
303: void * param, int status)
2.9 frystyk 304: {
2.20 frystyk 305: HTAlertCallback * cbf = HTAlert_find(HT_A_CONFIRM);
2.15 frystyk 306: HTAnchor * proxy_anchor = HTResponse_redirection(response);
2.9 frystyk 307: if (!proxy_anchor) {
308: if (PROT_TRACE) HTTrace("Use Proxy... No proxy location\n");
309: return HT_OK;
310: }
311:
312: /*
313: ** Add the proxy to the list. Assume HTTP access method only!
2.20 frystyk 314: ** Because evil servers may rediret the client to an untrusted
315: ** proxy, we can only accept redirects for this particular
316: ** server. Also, we do not know whether this is for HTTP or all
317: ** other requests as well
2.9 frystyk 318: */
2.20 frystyk 319: if ((cbf && (*cbf)(request, HT_A_CONFIRM, HT_MSG_PROXY, NULL,NULL,NULL))) {
2.9 frystyk 320: char * addr = HTAnchor_address(proxy_anchor);
321: HTProxy_add("http", addr);
322: HT_FREE(addr);
323:
2.20 frystyk 324: /*
325: ** Start new request through the proxy if we haven't reached the max
326: ** number of redirections for this request
327: */
328: if (HTRequest_doRetry(request)) {
329: HTLoadAnchor(proxy_anchor, request);
330: } else {
331: HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
332: NULL, 0, "HTRedirectFilter");
333: }
334:
335: /*
336: ** By returning HT_ERROR we make sure that this is the last handler to be
337: ** called. We do this as we don't want any other filter to delete the
338: ** request object now when we have just started a new one ourselves
339: */
340: return HT_ERROR;
341:
2.1 frystyk 342: } else {
2.20 frystyk 343: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_AUTO_PROXY,
344: NULL, 0, "HTUseProxyFilter");
345: return HT_OK;
2.1 frystyk 346: }
347: }
348:
349: /*
350: ** Client side authentication BEFORE filter
351: ** ----------------------------------------
352: ** The filter generates the credentials required to access a document
353: ** Getting the credentials may involve asking the user
354: */
2.15 frystyk 355: PUBLIC int HTCredentialsFilter (HTRequest * request, void * param, int mode)
2.1 frystyk 356: {
357: /*
358: ** Ask the authentication module to call the right credentials generator
359: ** that understands this scheme
360: */
2.15 frystyk 361: if (HTAA_beforeFilter(request, param, mode) == HT_OK) {
2.1 frystyk 362: if (PROT_TRACE) HTTrace("Credentials. verified\n");
363: return HT_OK;
364: } else {
365: HTRequest_addError(request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
366: NULL, 0, "HTCredentialsFilter");
367: return HT_ERROR;
368: }
369: }
370:
371: /*
372: ** Client side authentication AFTER filter
373: ** ---------------------------------------
374: ** The client side authentication filter uses the
375: ** user dialog messages registered in the HTAlert module.
376: ** By default these are the ones used by the line mode browser but you can
377: ** just register something else.
378: */
2.15 frystyk 379: PUBLIC int HTAuthFilter (HTRequest * request, HTResponse * response,
380: void * param, int status)
2.1 frystyk 381: {
382: /*
383: ** Ask the authentication module to call the right challenge parser
384: ** that understands this scheme
385: */
2.15 frystyk 386: if (HTAA_afterFilter(request, response, param, status) == HT_OK) {
2.1 frystyk 387:
388: /*
389: ** Start request with new credentials. As with the redirection filter
390: ** we reuse the same request object which means that we must
391: ** keep this around until the redirected request has terminated
392: */
393: HTLoad(request, NO);
394:
395: /*
396: ** We return HT_ERROR to make sure that this is the last handler to be
397: ** called. We do this as we don't want any other filter to delete the
398: ** request object now when we have just started a new one ourselves
399: */
400: return HT_ERROR;
401: }
402: return HT_OK;
2.32 kahan 403: }
404:
405: /*
406: ** Client side authentication info AFTER filter
407: ** ---------------------------------------
408: */
409: PUBLIC int HTAuthInfoFilter (HTRequest * request, HTResponse * response,
410: void * param, int status)
411: {
412: /*
413: ** Ask the authentication module to call the right authentication info
414: ** parser
415: */
416: if (! HTResponse_challenge (response))
417: return HT_OK;
418: else if (HTAA_updateFilter(request, response, param, status) == HT_OK)
419: return HT_OK;
420: else
421: return HT_ERROR;
2.1 frystyk 422: }
423:
424: /*
2.24 frystyk 425: ** Request Logging AFTER filter
426: ** ----------------------------
2.1 frystyk 427: ** Default Logging filter using the log manager provided by HTLog.c
428: */
2.15 frystyk 429: PUBLIC int HTLogFilter (HTRequest * request, HTResponse * response,
430: void * param, int status)
2.1 frystyk 431: {
432: if (request) {
2.23 frystyk 433: HTLog * log = (HTLog *) param;
434: if (log) HTLog_addCLF(log, request, status);
2.24 frystyk 435: return HT_OK;
436: }
437: return HT_ERROR;
438: }
439:
440: /*
441: ** Request Referer AFTER filter
442: ** ----------------------------
443: ** Default Referer Log filter using the log manager provided by HTLog.c
444: */
445: PUBLIC int HTRefererFilter (HTRequest * request, HTResponse * response,
446: void * param, int status)
447: {
448: if (request) {
449: HTLog * log = (HTLog *) param;
450: if (log) HTLog_addReferer(log, request, status);
2.1 frystyk 451: return HT_OK;
452: }
453: return HT_ERROR;
454: }
Webmaster