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

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.2     ! frystyk     6: **     @(#) $Id: HTFilter.c,v 2.1 1996/07/08 19:10:42 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 "WWWRules.h"
                     20: #include "WWWHTTP.h"
                     21: #include "HTLog.h"
                     22: #include "HTAccess.h"
                     23: #include "HTFilter.h"                                   /* Implemented here */
                     24: 
                     25: /* ------------------------------------------------------------------------- */
                     26: 
                     27: /*
                     28: **     Proxy and Gateway BEFORE filter
                     29: **     -------------------------------
                     30: **     Checks for registerd proxy servers or gateways and sees whether this
                     31: **     request should be redirected to a proxy or a gateway. Proxies have
                     32: **     higher priority than gateways so we look for them first!
                     33: **     For HTTP/1.0 and HTTP/1.1 we may only send a full URL (including the
                     34: **     host portion) to proxy servers. Therefore, we tell the Library whether
                     35: **     to use the full URL or the traditional HTTP one without the host part.
                     36: */
                     37: PUBLIC int HTProxyFilter (HTRequest * request, void * param, int status)
                     38: {
                     39:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.2     ! frystyk    40:     char * addr = HTAnchor_physical(anchor);
2.1       frystyk    41:     char * physical = NULL;
                     42:     if ((physical = HTProxy_find(addr))) {
                     43:        StrAllocCat(physical, addr);
                     44:        HTAnchor_setPhysical(anchor, physical);
                     45:        HTRequest_setFullURI(request, YES);
                     46:     } else if ((physical = HTGateway_find(addr))) {
                     47:        /* 
                     48:        ** A gateway URL is crated by chopping off any leading "/" to make the
                     49:        ** host into part of path
                     50:        */
                     51:        char * path =
                     52:            HTParse(addr, "", PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
                     53:        char * gatewayed = HTParse(path+1, physical, PARSE_ALL);
                     54:        HTAnchor_setPhysical(anchor, gatewayed);
                     55:        HT_FREE(path);
                     56:        HT_FREE(gatewayed);
                     57:        HTRequest_setFullURI(request, NO);
                     58:     } else {
                     59:        HTRequest_setFullURI(request, NO);
                     60:     }
                     61:     return HT_OK;
                     62: }
                     63: 
                     64: /*
                     65: **     Rule Translation BEFORE Filter
                     66: **     ------------------------------
                     67: **     If we have a set of rules loaded (see the Rule manager) then check
                     68: **     before each request whether how that should be translated. The trick
                     69: **     is that a parent anchor has a "address" which is the part from the URL
                     70: **     we used when we created the anchor. However, it also have a "physical
                     71: **     address" which is the place we are actually going to look for the
2.2     ! frystyk    72: **     resource. Hence this filter translates the physical address
        !            73: **     (if any translations are found)
2.1       frystyk    74: */
                     75: PUBLIC int HTRuleFilter (HTRequest * request, void * param, int status)
                     76: {
                     77:     HTList * list = HTRule_global();
                     78:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.2     ! frystyk    79:     char * addr = HTAnchor_physical(anchor);
2.1       frystyk    80:     char * physical = HTRule_translate(list, addr, NO);
                     81:     if (!physical) {
                     82:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                     83:                           NULL, 0, "HTRuleFilter");
                     84:        return HT_ERROR;
                     85:     }
                     86:     HTAnchor_setPhysical(anchor, physical);
                     87:     HT_FREE(physical);
                     88:     return HT_OK;
                     89: }
                     90: 
                     91: /*
                     92: **     Cache Validation BEFORE Filter
                     93: **     ------------------------------
                     94: **     Check the cache mode to see if we can use an already loaded version
                     95: **     of this document. If so and our copy is valid then we don't have
                     96: **     to go out and get it unless we are forced to
                     97: */
                     98: PUBLIC int HTCacheFilter (HTRequest * request, void * param, int status)
                     99: {
                    100:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    101:     HTReload mode = HTRequest_reloadMode(request);
                    102:     /*
                    103:     ** If the mode if "Force Reload" then don't even bother to check the
                    104:     ** cache - we flush everything we know about this document
                    105:     */
                    106:     if (mode == HT_FORCE_RELOAD) {
                    107:        /*
                    108:        ** Add the appropriate request headers. We use both the "pragma"
                    109:        ** and the "cache-control" headers in order to be
                    110:        ** backwards compatible with HTP/1.0
                    111:        */
                    112:        HTRequest_addGnHd(request, HT_G_PRAGMA_NO_CACHE);
                    113: 
                    114:        /* @@@ CACHE CONTROL @@@ */
                    115: 
                    116:        /*
                    117:        ** We also flush the information in the anchor
                    118:        */
                    119:        HTAnchor_clearHeader(anchor);
                    120:        return HT_OK;
                    121:     }
                    122: 
                    123:     /*
                    124:     ** Check the application provided memory cache. This is equivalent to a
                    125:     ** history list and does not follow the same cache mechanisms as the 
                    126:     ** persistent cache
                    127:     */
                    128:     if (HTMemoryCache_check(request) == HT_LOADED)
                    129:        return HT_LOADED;
                    130:     
                    131:     /*
                    132:     ** Check the persistent cache manager. If we have a cache hit then
                    133:     ** continue to see if the reload mode requires us to do a validation check.
                    134:     ** This filter assumes that we can get the cached version through one of
                    135:     ** our protocol modules (for example the file module)
                    136:     */
                    137:     {
                    138:        char * addr = HTAnchor_address((HTAnchor *) anchor);
                    139:        char * cache = HTCache_getReference(addr);
                    140:        if (cache) {
                    141:            if (mode != HT_CACHE_REFRESH) {
                    142:                HTAnchor_setPhysical(anchor, cache);
                    143:                HTAnchor_setCacheHit(anchor, YES);
                    144:            } else {
                    145: 
                    146:                /* @@@ Do cache validation @@@ */
                    147: 
                    148:            }
                    149:        }
                    150:        HT_FREE(addr);
                    151:     }
                    152:     return HT_OK;
                    153: }
                    154: 
                    155: /*
                    156: **     Error and Information AFTER filter
                    157: **     ----------------------------------
                    158: **     It checks the status code from a request and generates an 
                    159: **     error/information message if required.
                    160: */
                    161: PUBLIC int HTInfoFilter (HTRequest * request, void * param, int status)
                    162: {
                    163:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    164:     char * uri = HTAnchor_address((HTAnchor*) anchor);
                    165:     switch (status) {
                    166:     case HT_RETRY:
                    167:        if (PROT_TRACE)
                    168:            HTTrace("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
                    169:                    HTRequest_retryTime(request));
                    170:        break;
                    171: 
                    172:     case HT_ERROR:
                    173:     {
                    174:        /*
                    175:        ** See if we have a function registered for outputting errors.
                    176:        ** If so then call it and present the message to the user
                    177:        */
                    178:        HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    179:        if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    180:                        HTRequest_error(request), NULL);
                    181:        if (PROT_TRACE)
                    182:            HTTrace("Load End.... ERROR: Can't access `%s\'\n",
                    183:                    uri ? uri : "<UNKNOWN>");
                    184:        break;
                    185:     }
                    186: 
                    187:     default:
                    188:        if (PROT_TRACE)
                    189:            HTTrace("Load End.... Request ended with code %d\n", status);
                    190:        break;
                    191:     }
                    192: 
                    193:     HT_FREE(uri);
                    194:     return HT_OK;
                    195: }
                    196: 
                    197: /*
                    198: **     Redirection AFTER filter
                    199: **     ------------------------
                    200: **     The redirection handler only handles redirections
                    201: **     on the GET or HEAD method (or any other safe method)
                    202: */
                    203: PUBLIC int HTRedirectFilter (HTRequest * request, void * param, int status)
                    204: {
                    205:     HTMethod method = HTRequest_method(request); 
                    206:     HTAnchor * new_anchor = HTRequest_redirection(request); 
                    207:  
                    208:     /*
                    209:     ** Only do redirect on GET and HEAD
                    210:     */
                    211:     if (!HTMethod_isSafe(method) || !new_anchor) { 
                    212:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_AUTO_REDIRECT,
                    213:                           NULL, 0, "RedirectionFilter");
                    214:        return HT_ERROR;
                    215:     } 
                    216:  
                    217:     /*
                    218:     **  Start new request with the redirect anchor found in the headers.
                    219:     ** Note that we reuse the same request object which means that we must
                    220:     **  keep this around until the redirected request has terminated. It also
                    221:     **  allows us in an easy way to keep track of the number of redirections
                    222:     ** so that we can detect endless loops.
                    223:     */ 
                    224:     if (HTRequest_retry(request)) { 
                    225:        HTLoadAnchor(new_anchor, request);
                    226:     } else {
                    227:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    228:                           NULL, 0, "HTRedirectFilter");
                    229:     }
                    230: 
                    231:     /*
                    232:     **  By returning HT_ERROR we make sure that this is the last handler to be
                    233:     **  called. We do this as we don't want any other filter to delete the 
                    234:     **  request object now when we have just started a new one ourselves
                    235:     */
                    236:     return HT_ERROR;
                    237: } 
                    238: 
                    239: /*
                    240: **     Client side authentication BEFORE filter
                    241: **     ----------------------------------------
                    242: **     The filter generates the credentials required to access a document
                    243: **     Getting the credentials may involve asking the user
                    244: */
                    245: PUBLIC int HTCredentialsFilter (HTRequest * request, void * param, int status)
                    246: {
                    247:     /*
                    248:     ** Ask the authentication module to call the right credentials generator
                    249:     ** that understands this scheme
                    250:     */
                    251:     if (HTAA_beforeFilter(request, param, status) == HT_OK) {
                    252:        if (PROT_TRACE) HTTrace("Credentials. verified\n");
                    253:        return HT_OK;
                    254:     } else {
                    255:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
                    256:                           NULL, 0, "HTCredentialsFilter");
                    257:        return HT_ERROR;
                    258:     }
                    259: }
                    260: 
                    261: /*
                    262: **     Client side authentication AFTER filter
                    263: **     ---------------------------------------
                    264: **     The client side authentication filter uses the 
                    265: **     user dialog messages registered in the HTAlert module.
                    266: **     By default these are the ones used by the line mode browser but you can
                    267: **     just register something else.
                    268: */
                    269: PUBLIC int HTAuthFilter (HTRequest * request, void * param, int status)
                    270: {
                    271:     /*
                    272:     ** Ask the authentication module to call the right challenge parser
                    273:     ** that understands this scheme
                    274:     */
                    275:     if (HTAA_afterFilter(request, param, status) == HT_OK) {
                    276:        HTMethod method = HTRequest_method(request);
                    277: 
                    278:        if (HTMethod_hasEntity(method)) { 
                    279:            if (APP_TRACE)
                    280:                HTTrace("Auth filter. Can't authenticate PUT or POST\n");
                    281:            return HT_ERROR;
                    282:        } 
                    283:  
                    284:        /*
                    285:        ** Start request with new credentials. As with the redirection filter
                    286:        ** we reuse the same request object which means that we must
                    287:        ** keep this around until the redirected request has terminated
                    288:        */
                    289:        HTLoad(request, NO);
                    290: 
                    291:        /*
                    292:        **  We return HT_ERROR to make sure that this is the last handler to be
                    293:        **  called. We do this as we don't want any other filter to delete the 
                    294:        **  request object now when we have just started a new one ourselves
                    295:        */
                    296:        return HT_ERROR;
                    297:     }
                    298:     return HT_OK;
                    299: }
                    300: 
                    301: /*
                    302: **     Request Loggin AFTER filter
                    303: **     ---------------------------
                    304: **     Default Logging filter using the log manager provided by HTLog.c
                    305: */
                    306: PUBLIC int HTLogFilter (HTRequest * request, void * param, int status)
                    307: {
                    308:     if (request) {
                    309:        if (HTLog_isOpen()) HTLog_add(request, status);
                    310:        return HT_OK;
                    311:     }
                    312:     return HT_ERROR;
                    313: }

Webmaster