Annotation of libwww/Library/src/HTFilter.c, revision 2.21

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.21    ! frystyk     6: **     @(#) $Id: HTFilter.c,v 2.20 1997/04/05 00:25:25 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);
                     59:        char * gatewayed = HTParse(path+1, physical, PARSE_ALL);
                     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: /*
                    100: **     Cache Validation BEFORE Filter
                    101: **     ------------------------------
                    102: **     Check the cache mode to see if we can use an already loaded version
                    103: **     of this document. If so and our copy is valid then we don't have
                    104: **     to go out and get it unless we are forced to
2.3       frystyk   105: **     We only check the cache in caseof a GET request. Otherwise, we go
                    106: **     directly to the source.
2.1       frystyk   107: */
2.15      frystyk   108: PUBLIC int HTCacheFilter (HTRequest * request, void * param, int mode)
2.1       frystyk   109: {
                    110:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.12      frystyk   111:     HTCache * cache = NULL;
2.15      frystyk   112:     HTReload reload = HTRequest_reloadMode(request);
2.3       frystyk   113:     HTMethod method = HTRequest_method(request);
2.12      frystyk   114:     HTDisconnectedMode disconnect = HTCacheMode_disconnected();
                    115:     BOOL validate = NO;
2.3       frystyk   116: 
                    117:     /*
2.12      frystyk   118:     **  If the cache is disabled all together then it won't help looking, huh?
2.3       frystyk   119:     */
2.12      frystyk   120:     if (!HTCacheMode_enabled()) return HT_OK;
                    121:     if (CACHE_TRACE) HTTrace("Cachefilter. Checking persistent cache\n");
2.3       frystyk   122: 
2.1       frystyk   123:     /*
2.12      frystyk   124:     **  Now check the cache...
2.1       frystyk   125:     */
2.12      frystyk   126:     if (method != METHOD_GET) {
                    127:        if (CACHE_TRACE) HTTrace("Cachefilter. We only check GET methods\n");
2.15      frystyk   128:     } else if (reload == HT_CACHE_FLUSH) {
2.12      frystyk   129:        /*
                    130:        ** If the mode if "Force Reload" then don't even bother to check the
                    131:        ** cache - we flush everything we know abut this document anyway.
2.1       frystyk   132:        ** Add the appropriate request headers. We use both the "pragma"
                    133:        ** and the "cache-control" headers in order to be
2.12      frystyk   134:        ** backwards compatible with HTTP/1.0
2.1       frystyk   135:        */
2.12      frystyk   136:        validate = YES;
2.1       frystyk   137:        HTRequest_addGnHd(request, HT_G_PRAGMA_NO_CACHE);
2.11      frystyk   138:        HTRequest_addCacheControl(request, "no-cache", "");
2.1       frystyk   139: 
                    140:        /*
2.15      frystyk   141:        **  We also flush the information in the anchor as we don't want to
                    142:        **  inherit any "old" values
2.1       frystyk   143:        */
                    144:        HTAnchor_clearHeader(anchor);
                    145: 
2.12      frystyk   146:     } else {
                    147:        /*
                    148:        ** Check the persistent cache manager. If we have a cache hit then
                    149:        ** continue to see if the reload mode requires us to do a validation
                    150:        ** check. This filter assumes that we can get the cached version
                    151:        ** through one of our protocol modules (for example the file module)
                    152:        */
                    153:        cache = HTCache_find(anchor);
2.1       frystyk   154:        if (cache) {
2.20      frystyk   155:            HTReload cache_mode = HTCache_isFresh(cache, request);
                    156:            if (cache_mode == HT_CACHE_ERROR) cache = NULL;
                    157:            reload = HTMAX(reload, cache_mode);
2.15      frystyk   158:            HTRequest_setReloadMode(request, reload);
2.12      frystyk   159: 
                    160:            /*
                    161:            **  Now check the mode and add the right headers for the validation
                    162:            **  If we are to validate a cache entry then we get a lock
                    163:            **  on it so that not other requests can steal it.
                    164:            */
2.15      frystyk   165:            if (reload == HT_CACHE_RANGE_VALIDATE) {
                    166:                /*
                    167:                **  If we were asked to range validate the cached object then
                    168:                **  use the etag or the last modified for cache validation
                    169:                */
                    170:                validate = YES;
                    171:                HTCache_getLock(cache, request);
                    172:                HTRequest_addRqHd(request, HT_C_IF_RANGE);
                    173:            } else if (reload == HT_CACHE_END_VALIDATE) {
2.12      frystyk   174:                /*
                    175:                **  If we were asked to end-to-end validate the cached object
                    176:                **  then use a max-age=0 cache control directive
                    177:                */
                    178:                validate = YES;
                    179:                HTCache_getLock(cache, request);
                    180:                HTRequest_addCacheControl(request, "max-age", "0");
2.15      frystyk   181:            } else if (reload == HT_CACHE_VALIDATE) {
2.11      frystyk   182:                /*
2.12      frystyk   183:                **  If we were asked to validate the cached object then
2.11      frystyk   184:                **  use the etag or the last modified for cache validation
2.15      frystyk   185:                **  We use both If-None-Match or If-Modified-Since.
2.11      frystyk   186:                */
2.12      frystyk   187:                validate = YES;
                    188:                HTCache_getLock(cache, request);
2.11      frystyk   189:                HTRequest_addRqHd(request, HT_C_IF_NONE_MATCH | HT_C_IMS);
2.20      frystyk   190:            } else if (cache) {
2.12      frystyk   191:                /*
                    192:                **  The entity does not require any validation at all. We
2.15      frystyk   193:                **  can just go ahead and get it from the cache. In case we
                    194:                **  have a fresh subpart of the entity, then we issue a 
                    195:                **  conditional GET request with the range set by the cache
                    196:                **  manager. Issuing the conditional range request is 
                    197:                **  equivalent to a validation as we have to go out on the
                    198:                **  net. This may have an effect if running in disconnected
                    199:                **  mode. We disable all BEFORE filters as they don't make
                    200:                **  sense while loading the cache entry.
2.12      frystyk   201:                */
2.15      frystyk   202:                {
                    203:                    char * name = HTCache_name(cache);
                    204:                    HTAnchor_setPhysical(anchor, name);
                    205:                    HTCache_addHit(cache);
                    206:                    HT_FREE(name);
                    207:                }
2.11      frystyk   208:            }
                    209:        }
2.12      frystyk   210:     }
                    211:     
                    212:     /*
                    213:     **  If we are in disconnected mode and we are to validate an entry
                    214:     **  then check whether what mode of disconnected mode we're in. If
                    215:     **  we are to use our own cache then return a "504 Gateway Timeout"
                    216:     */
                    217:     if ((!cache || validate) && disconnect != HT_DISCONNECT_NONE) {
                    218:        if (disconnect == HT_DISCONNECT_EXTERNAL)
                    219:            HTRequest_addCacheControl(request, "only-if-cached", "");
                    220:        else {
                    221:            HTRequest_addError(request, ERR_FATAL, NO,
                    222:                               HTERR_GATE_TIMEOUT, "Disconnected Cache Mode",
                    223:                               0, "HTCacheFilter");
                    224:            return HT_ERROR;
                    225:        }
2.11      frystyk   226:     }
                    227:     return HT_OK;
                    228: }
                    229: 
                    230: /*
2.16      frystyk   231: **     A small BEFORE filter that just finds a cache entry unconditionally
                    232: **     and loads the entry. All freshness and any other constraints are 
                    233: **     ignored.
                    234: */
                    235: PUBLIC int HTCacheLoadFilter (HTRequest * request, void * param, int mode)
                    236: {
                    237:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    238:     HTCache * cache = HTCache_find(anchor);
                    239:     if (cache) {
                    240:        char * name = HTCache_name(cache);
                    241:        HTAnchor_setPhysical(anchor, name);
                    242:        HTCache_addHit(cache);
                    243:        HT_FREE(name);
                    244: 
                    245:        /*
                    246:        **  Start request directly from the cache. As with the redirection
                    247:        **  filter we reuse the same request object which means that we must
                    248:        **  keep this around until the cache load request has terminated
                    249:        **  In the case of a 
                    250:        */
                    251:        {
                    252:            HTLoad(request, NO);
                    253:            return HT_ERROR;
                    254:        }
                    255:     }
                    256:     return HT_OK;
                    257: }
                    258: 
                    259: /*
2.15      frystyk   260: **     Check the Memory Cache (History list) BEFORE filter
                    261: **     ---------------------------------------------------
2.11      frystyk   262: **     Check if document is already loaded. The user can define whether
                    263: **     the history list should follow normal expiration or work as a
                    264: **     traditional history list where expired documents are not updated.
                    265: **     We don't check for anything but existence proof of a document
                    266: **     associated with the anchor as the definition is left to the application
                    267: */
2.15      frystyk   268: PUBLIC int HTMemoryCacheFilter (HTRequest * request, void * param, int mode)
2.11      frystyk   269: {
                    270:     HTReload validation = HTRequest_reloadMode(request);
                    271:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    272:     void * document = HTAnchor_document(anchor);
2.1       frystyk   273: 
2.11      frystyk   274:     /*
2.14      frystyk   275:     **  We only check the memory cache if it's a GET method
                    276:     */
                    277:     if (HTRequest_method(request) != METHOD_GET) {
                    278:        if (CACHE_TRACE) HTTrace("Mem Cache... We only check GET methods\n");
                    279:        return HT_OK;
                    280:     }
                    281: 
                    282:     /*
2.11      frystyk   283:     **  If we are asked to flush the persistent cache then there is no reason
                    284:     **  to do anything here - we're flushing it anyway. Also if no document
                    285:     **  then just exit from this filter.
                    286:     */
                    287:     if (!document || validation > HT_CACHE_FLUSH_MEM) {
                    288:        if (CACHE_TRACE) HTTrace("Mem Cache... No fresh document...\n");
                    289:        return HT_OK;
                    290:     }
2.1       frystyk   291: 
2.11      frystyk   292:     /*
                    293:     **  If we have a document object associated with this anchor then we also
                    294:     **  have the object in the history list. Depending on what the user asked,
                    295:     **  we can add a cache validator
                    296:     */
2.14      frystyk   297: #if 0
2.11      frystyk   298:     if (document) {
                    299:        if (validation != HT_CACHE_FLUSH_MEM) {
2.14      frystyk   300:            HTExpiresMode expires = HTCacheMode_expires();
2.11      frystyk   301:            if (CACHE_TRACE)
                    302:                HTTrace("Mem Cache... Document already in memory\n");
                    303:            if (expires != HT_EXPIRES_IGNORE) {
                    304: 
                    305:                /*
                    306:                **  Ask the cache manager if this object has expired. Also
                    307:                **  check if we should care about expiration or not.
                    308:                */
                    309:                if (!HTCache_isValid(anchor)) {
                    310:                    if (expires == HT_EXPIRES_NOTIFY) {
                    311: 
                    312:                        /*
                    313:                        ** See if we have a function registered for outputting errors.
                    314:                        ** If so then call it and present the message to the user
                    315:                        */
                    316:                        HTAlertCallback * cbf = HTAlert_find(HT_A_MESSAGE);
                    317:                        if (cbf)
                    318:                            (*cbf)(request, HT_A_MESSAGE, HTERR_CACHE_EXPIRED,
                    319:                                   NULL, HTRequest_error(request), NULL);
                    320:                    } else {
                    321:                        if (CACHE_TRACE) HTTrace("Mem Cache... Expired - autoreload\n");
                    322:                        HTRequest_addRqHd(request, HT_C_IF_NONE_MATCH | HT_C_IMS);
                    323:                        return HT_OK;           /* Must go get it */
                    324:                    }
                    325:                }
2.1       frystyk   326:            }
                    327:        }
2.14      frystyk   328:        return HT_LOADED;                       /* Got it! */
                    329:     }
                    330:     return HT_OK;
                    331: #endif
                    332:     if (document && validation != HT_CACHE_FLUSH_MEM) {
                    333:        if (CACHE_TRACE) HTTrace("Mem Cache... Document already in memory\n");
                    334:        return HT_LOADED;
2.1       frystyk   335:     }
                    336:     return HT_OK;
                    337: }
                    338: 
                    339: /*
2.15      frystyk   340: **     Cache Update AFTER filter
                    341: **     -------------------------
                    342: **     On our way out we catch the metainformation and stores it in
                    343: **     our persistent store. If we have a cache validation (a 304
                    344: **     response then we use the new metainformation and merges it with
                    345: **     the existing information already captured in the cache.
                    346: */
                    347: PUBLIC int HTCacheUpdateFilter (HTRequest * request, HTResponse * response,
                    348:                                void * param, int status)
                    349: {
                    350:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    351:     HTCache * cache = HTCache_find(anchor);
2.17      frystyk   352:     if (cache) {
2.15      frystyk   353: 
2.17      frystyk   354:        /*
                    355:        **  It may in fact be that the information in the 304 response
                    356:        **  told us that we can't cache the entity anymore. If this is the
                    357:        **  case then flush it now. Otherwise prepare for a cache read
                    358:        */
                    359:        if (CACHE_TRACE) HTTrace("Cache....... Merging metainformation\n");
                    360:        if (HTResponse_isCachable(response) == NO) {
                    361:            HTCache_remove(cache);
                    362:        } else {
                    363:            char * name = HTCache_name(cache);
                    364:            HTAnchor_setPhysical(anchor, name);
                    365:            HTCache_addHit(cache);
                    366:            HT_FREE(name);
                    367:            HTCache_updateMeta(cache, request, response);
                    368:        }
2.15      frystyk   369: 
2.17      frystyk   370:        /*
                    371:        **  Start request directly from the cache. As with the redirection filter
                    372:        **  we reuse the same request object which means that we must
                    373:        **  keep this around until the cache load request has terminated
                    374:        **  In the case of a 
                    375:        */
2.19      frystyk   376: #define SINGLE_CACHE_LOAD
                    377: #ifdef SINGLE_CACHE_LOAD
2.17      frystyk   378:        {
                    379:            static BOOL done = NO;
                    380:            if (!done) {        
                    381:                HTLoad(request, YES);
                    382:                done = YES;
                    383:                return HT_ERROR;
                    384:            }
                    385:        }
                    386: #else
                    387:        HTLoad(request, YES);
2.15      frystyk   388:        return HT_ERROR;
2.19      frystyk   389: #endif /* SINGLE_CACHE_LOAD */
2.15      frystyk   390:     }
2.17      frystyk   391:     return HT_OK;
2.15      frystyk   392: }
                    393: 
                    394: /*
2.1       frystyk   395: **     Error and Information AFTER filter
                    396: **     ----------------------------------
                    397: **     It checks the status code from a request and generates an 
                    398: **     error/information message if required.
                    399: */
2.15      frystyk   400: PUBLIC int HTInfoFilter (HTRequest * request, HTResponse * response,
                    401:                         void * param, int status)
2.1       frystyk   402: {
                    403:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    404:     char * uri = HTAnchor_address((HTAnchor*) anchor);
                    405:     switch (status) {
                    406:     case HT_RETRY:
                    407:        if (PROT_TRACE)
                    408:            HTTrace("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
2.15      frystyk   409:                    HTResponse_retryTime(response));
2.1       frystyk   410:        break;
                    411: 
2.7       frystyk   412:     case HT_NO_DATA:
                    413:     {
                    414:        /*
                    415:        ** The document was empty
                    416:        */
                    417:        HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    418:        if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    419:                        HTRequest_error(request), NULL);
                    420:        if (PROT_TRACE)
                    421:            HTTrace("Load End.... EMPTY: No content `%s\'\n",
                    422:                    uri ? uri : "<UNKNOWN>");
                    423:        break;
                    424:     }    
2.3       frystyk   425: 
                    426:     case HT_LOADED:
2.16      frystyk   427:        if (PROT_TRACE) HTTrace("Load End.... OK: `%s\'\n", uri);
                    428:        break;
                    429: 
                    430:     default:
2.3       frystyk   431:     {
                    432:        /*
2.16      frystyk   433:        ** See if we have a function registered for outputting errors.
                    434:        ** If so then call it and present the message to the user
2.3       frystyk   435:        */
                    436:        HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    437:        if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    438:                        HTRequest_error(request), NULL);
2.1       frystyk   439:        if (PROT_TRACE)
                    440:            HTTrace("Load End.... Request ended with code %d\n", status);
                    441:        break;
                    442:     }
2.16      frystyk   443:     }
2.1       frystyk   444:     HT_FREE(uri);
                    445:     return HT_OK;
                    446: }
                    447: 
                    448: /*
                    449: **     Redirection AFTER filter
                    450: **     ------------------------
                    451: **     The redirection handler only handles redirections
                    452: **     on the GET or HEAD method (or any other safe method)
                    453: */
2.15      frystyk   454: PUBLIC int HTRedirectFilter (HTRequest * request, HTResponse * response,
                    455:                             void * param, int status)
2.1       frystyk   456: {
                    457:     HTMethod method = HTRequest_method(request); 
2.15      frystyk   458:     HTAnchor * new_anchor = HTResponse_redirection(response); 
2.7       frystyk   459:     if (!new_anchor) {
                    460:        if (PROT_TRACE) HTTrace("Redirection. No destination\n");
                    461:        return HT_OK;
                    462:     }
                    463: 
2.1       frystyk   464:     /*
2.21    ! frystyk   465:     ** Only do automatic redirect on GET and HEAD. Ask for all
        !           466:     ** other methods
2.1       frystyk   467:     */
2.21    ! frystyk   468:     if (!HTMethod_isSafe(method)) {
        !           469: 
        !           470:        /*
        !           471:        ** If we got a 303 See Other then change the method to GET.
        !           472:        ** Otherwise ask the user whether we should continue.
        !           473:        */
        !           474:        if (status == HT_SEE_OTHER) {
        !           475:            if (PROT_TRACE)
        !           476:                HTTrace("Redirection. Changing method from %s to GET\n",
        !           477:                        HTMethod_name(method));
        !           478:            HTRequest_setMethod(request, METHOD_GET);
        !           479:        } else {
        !           480:            HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
        !           481:            if (prompt) {
        !           482:                if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
        !           483:                              NULL, NULL, NULL) != YES)
        !           484:                    return HT_ERROR;
        !           485:            }
2.4       frystyk   486:        }
2.1       frystyk   487:     } 
                    488:  
                    489:     /*
                    490:     **  Start new request with the redirect anchor found in the headers.
                    491:     ** Note that we reuse the same request object which means that we must
                    492:     **  keep this around until the redirected request has terminated. It also
                    493:     **  allows us in an easy way to keep track of the number of redirections
                    494:     ** so that we can detect endless loops.
                    495:     */ 
2.4       frystyk   496:     if (HTRequest_doRetry(request)) { 
2.1       frystyk   497:        HTLoadAnchor(new_anchor, request);
2.9       frystyk   498:     } else {
                    499:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    500:                           NULL, 0, "HTRedirectFilter");
                    501:     }
                    502: 
                    503:     /*
                    504:     **  By returning HT_ERROR we make sure that this is the last handler to be
                    505:     **  called. We do this as we don't want any other filter to delete the 
                    506:     **  request object now when we have just started a new one ourselves
                    507:     */
                    508:     return HT_ERROR;
                    509: } 
                    510: 
                    511: /*
2.15      frystyk   512: **     Retry through Proxy AFTER Filter
                    513: **     --------------------------------
2.9       frystyk   514: **     This filter handles a 305 Use Proxy response and retries the request
                    515: **     through the proxy
                    516: */
2.15      frystyk   517: PUBLIC int HTUseProxyFilter (HTRequest * request, HTResponse * response,
                    518:                             void * param, int status)
2.9       frystyk   519: {
2.20      frystyk   520:     HTAlertCallback * cbf = HTAlert_find(HT_A_CONFIRM);
2.15      frystyk   521:     HTAnchor * proxy_anchor = HTResponse_redirection(response); 
2.9       frystyk   522:     if (!proxy_anchor) {
                    523:        if (PROT_TRACE) HTTrace("Use Proxy... No proxy location\n");
                    524:        return HT_OK;
                    525:     }
                    526: 
                    527:     /*
                    528:     **  Add the proxy to the list. Assume HTTP access method only!
2.20      frystyk   529:     **  Because evil servers may rediret the client to an untrusted
                    530:     **  proxy, we can only accept redirects for this particular
                    531:     **  server. Also, we do not know whether this is for HTTP or all
                    532:     **  other requests as well
2.9       frystyk   533:     */
2.20      frystyk   534:     if ((cbf && (*cbf)(request, HT_A_CONFIRM, HT_MSG_PROXY, NULL,NULL,NULL))) {
2.9       frystyk   535:        char * addr = HTAnchor_address(proxy_anchor);
                    536:        HTProxy_add("http", addr);
                    537:        HT_FREE(addr);
                    538:  
2.20      frystyk   539:        /*
                    540:        **  Start new request through the proxy if we haven't reached the max
                    541:        **  number of redirections for this request
                    542:        */ 
                    543:        if (HTRequest_doRetry(request)) { 
                    544:            HTLoadAnchor(proxy_anchor, request);
                    545:        } else {
                    546:            HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    547:                               NULL, 0, "HTRedirectFilter");
                    548:        }
                    549: 
                    550:        /*
                    551:        **  By returning HT_ERROR we make sure that this is the last handler to be
                    552:        **  called. We do this as we don't want any other filter to delete the 
                    553:        **  request object now when we have just started a new one ourselves
                    554:        */
                    555:        return HT_ERROR;
                    556: 
2.1       frystyk   557:     } else {
2.20      frystyk   558:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_AUTO_PROXY,
                    559:                           NULL, 0, "HTUseProxyFilter");
                    560:        return HT_OK;
2.1       frystyk   561:     }
                    562: } 
                    563: 
                    564: /*
                    565: **     Client side authentication BEFORE filter
                    566: **     ----------------------------------------
                    567: **     The filter generates the credentials required to access a document
                    568: **     Getting the credentials may involve asking the user
                    569: */
2.15      frystyk   570: PUBLIC int HTCredentialsFilter (HTRequest * request, void * param, int mode)
2.1       frystyk   571: {
                    572:     /*
                    573:     ** Ask the authentication module to call the right credentials generator
                    574:     ** that understands this scheme
                    575:     */
2.15      frystyk   576:     if (HTAA_beforeFilter(request, param, mode) == HT_OK) {
2.1       frystyk   577:        if (PROT_TRACE) HTTrace("Credentials. verified\n");
                    578:        return HT_OK;
                    579:     } else {
                    580:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
                    581:                           NULL, 0, "HTCredentialsFilter");
                    582:        return HT_ERROR;
                    583:     }
                    584: }
                    585: 
                    586: /*
                    587: **     Client side authentication AFTER filter
                    588: **     ---------------------------------------
                    589: **     The client side authentication filter uses the 
                    590: **     user dialog messages registered in the HTAlert module.
                    591: **     By default these are the ones used by the line mode browser but you can
                    592: **     just register something else.
                    593: */
2.15      frystyk   594: PUBLIC int HTAuthFilter (HTRequest * request, HTResponse * response,
                    595:                         void * param, int status)
2.1       frystyk   596: {
                    597:     /*
                    598:     ** Ask the authentication module to call the right challenge parser
                    599:     ** that understands this scheme
                    600:     */
2.15      frystyk   601:     if (HTAA_afterFilter(request, response, param, status) == HT_OK) {
2.1       frystyk   602: 
                    603:        /*
                    604:        ** Start request with new credentials. As with the redirection filter
                    605:        ** we reuse the same request object which means that we must
                    606:        ** keep this around until the redirected request has terminated
                    607:        */
                    608:        HTLoad(request, NO);
                    609: 
                    610:        /*
                    611:        **  We return HT_ERROR to make sure that this is the last handler to be
                    612:        **  called. We do this as we don't want any other filter to delete the 
                    613:        **  request object now when we have just started a new one ourselves
                    614:        */
                    615:        return HT_ERROR;
                    616:     }
                    617:     return HT_OK;
                    618: }
                    619: 
                    620: /*
                    621: **     Request Loggin AFTER filter
                    622: **     ---------------------------
                    623: **     Default Logging filter using the log manager provided by HTLog.c
                    624: */
2.15      frystyk   625: PUBLIC int HTLogFilter (HTRequest * request, HTResponse * response,
                    626:                        void * param, int status)
2.1       frystyk   627: {
                    628:     if (request) {
                    629:        if (HTLog_isOpen()) HTLog_add(request, status);
                    630:        return HT_OK;
                    631:     }
                    632:     return HT_ERROR;
                    633: }

Webmaster