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

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.4     ! frystyk     6: **     @(#) $Id: HTFilter.c,v 2.5 1996/07/20 00:12:36 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
2.3       frystyk    97: **     We only check the cache in caseof a GET request. Otherwise, we go
                     98: **     directly to the source.
2.1       frystyk    99: */
                    100: PUBLIC int HTCacheFilter (HTRequest * request, void * param, int status)
                    101: {
                    102:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    103:     HTReload mode = HTRequest_reloadMode(request);
2.3       frystyk   104:     HTMethod method = HTRequest_method(request);
                    105: 
                    106:     /*
                    107:     ** Check the method of the request
                    108:     */
                    109:     if (method != METHOD_GET) {
                    110:        if (CACHE_TRACE) HTTrace("Cachefilter. We only check GET methods\n");
                    111:        return HT_OK;
                    112:     }
                    113: 
2.1       frystyk   114:     /*
                    115:     ** If the mode if "Force Reload" then don't even bother to check the
                    116:     ** cache - we flush everything we know about this document
                    117:     */
                    118:     if (mode == HT_FORCE_RELOAD) {
                    119:        /*
                    120:        ** Add the appropriate request headers. We use both the "pragma"
                    121:        ** and the "cache-control" headers in order to be
                    122:        ** backwards compatible with HTP/1.0
                    123:        */
                    124:        HTRequest_addGnHd(request, HT_G_PRAGMA_NO_CACHE);
                    125: 
                    126:        /* @@@ CACHE CONTROL @@@ */
                    127: 
                    128:        /*
                    129:        ** We also flush the information in the anchor
                    130:        */
                    131:        HTAnchor_clearHeader(anchor);
                    132:        return HT_OK;
                    133:     }
                    134: 
                    135:     /*
                    136:     ** Check the application provided memory cache. This is equivalent to a
                    137:     ** history list and does not follow the same cache mechanisms as the 
                    138:     ** persistent cache
                    139:     */
                    140:     if (HTMemoryCache_check(request) == HT_LOADED)
                    141:        return HT_LOADED;
                    142:     
                    143:     /*
                    144:     ** Check the persistent cache manager. If we have a cache hit then
                    145:     ** continue to see if the reload mode requires us to do a validation check.
                    146:     ** This filter assumes that we can get the cached version through one of
                    147:     ** our protocol modules (for example the file module)
                    148:     */
                    149:     {
                    150:        char * addr = HTAnchor_address((HTAnchor *) anchor);
                    151:        char * cache = HTCache_getReference(addr);
                    152:        if (cache) {
                    153:            if (mode != HT_CACHE_REFRESH) {
                    154:                HTAnchor_setPhysical(anchor, cache);
                    155:                HTAnchor_setCacheHit(anchor, YES);
                    156:            } else {
                    157: 
                    158:                /* @@@ Do cache validation @@@ */
                    159: 
                    160:            }
                    161:        }
                    162:        HT_FREE(addr);
                    163:     }
                    164:     return HT_OK;
                    165: }
                    166: 
                    167: /*
                    168: **     Error and Information AFTER filter
                    169: **     ----------------------------------
                    170: **     It checks the status code from a request and generates an 
                    171: **     error/information message if required.
                    172: */
                    173: PUBLIC int HTInfoFilter (HTRequest * request, void * param, int status)
                    174: {
                    175:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    176:     char * uri = HTAnchor_address((HTAnchor*) anchor);
                    177:     switch (status) {
                    178:     case HT_RETRY:
                    179:        if (PROT_TRACE)
                    180:            HTTrace("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
                    181:                    HTRequest_retryTime(request));
                    182:        break;
                    183: 
                    184:     case HT_ERROR:
                    185:     {
                    186:        /*
                    187:        ** See if we have a function registered for outputting errors.
                    188:        ** If so then call it and present the message to the user
                    189:        */
                    190:        HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    191:        if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    192:                        HTRequest_error(request), NULL);
                    193:        if (PROT_TRACE)
                    194:            HTTrace("Load End.... ERROR: Can't access `%s\'\n",
                    195:                    uri ? uri : "<UNKNOWN>");
2.3       frystyk   196:        break;
                    197:     }
                    198: 
                    199:     case HT_LOADED:
                    200:     {
                    201:        /*
                    202:        ** Even though we have received a loaded status the thing we have
                    203:        ** loaded successfully may in fact be an error message. We therefore
                    204:        ** look at the error stack to see what to do.
                    205:        */
                    206:        HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    207:        if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    208:                        HTRequest_error(request), NULL);
                    209:        if (PROT_TRACE) HTTrace("Load End.... OK: `%s\'\n", uri);
2.1       frystyk   210:        break;
                    211:     }
                    212: 
                    213:     default:
                    214:        if (PROT_TRACE)
                    215:            HTTrace("Load End.... Request ended with code %d\n", status);
                    216:        break;
                    217:     }
                    218: 
                    219:     HT_FREE(uri);
                    220:     return HT_OK;
                    221: }
                    222: 
                    223: /*
                    224: **     Redirection AFTER filter
                    225: **     ------------------------
                    226: **     The redirection handler only handles redirections
                    227: **     on the GET or HEAD method (or any other safe method)
                    228: */
                    229: PUBLIC int HTRedirectFilter (HTRequest * request, void * param, int status)
                    230: {
                    231:     HTMethod method = HTRequest_method(request); 
                    232:     HTAnchor * new_anchor = HTRequest_redirection(request); 
                    233:  
                    234:     /*
                    235:     ** Only do redirect on GET and HEAD
                    236:     */
                    237:     if (!HTMethod_isSafe(method) || !new_anchor) { 
2.4     ! frystyk   238:        HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
        !           239:        if (prompt) {
        !           240:            if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
        !           241:                          NULL, NULL, NULL) != YES)
        !           242:                return HT_ERROR;
        !           243:        }
2.1       frystyk   244:     } 
                    245:  
                    246:     /*
                    247:     **  Start new request with the redirect anchor found in the headers.
                    248:     ** Note that we reuse the same request object which means that we must
                    249:     **  keep this around until the redirected request has terminated. It also
                    250:     **  allows us in an easy way to keep track of the number of redirections
                    251:     ** so that we can detect endless loops.
                    252:     */ 
2.4     ! frystyk   253:     if (HTRequest_doRetry(request)) { 
2.1       frystyk   254:        HTLoadAnchor(new_anchor, request);
                    255:     } else {
                    256:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    257:                           NULL, 0, "HTRedirectFilter");
                    258:     }
                    259: 
                    260:     /*
                    261:     **  By returning HT_ERROR we make sure that this is the last handler to be
                    262:     **  called. We do this as we don't want any other filter to delete the 
                    263:     **  request object now when we have just started a new one ourselves
                    264:     */
                    265:     return HT_ERROR;
                    266: } 
                    267: 
                    268: /*
                    269: **     Client side authentication BEFORE filter
                    270: **     ----------------------------------------
                    271: **     The filter generates the credentials required to access a document
                    272: **     Getting the credentials may involve asking the user
                    273: */
                    274: PUBLIC int HTCredentialsFilter (HTRequest * request, void * param, int status)
                    275: {
                    276:     /*
                    277:     ** Ask the authentication module to call the right credentials generator
                    278:     ** that understands this scheme
                    279:     */
                    280:     if (HTAA_beforeFilter(request, param, status) == HT_OK) {
                    281:        if (PROT_TRACE) HTTrace("Credentials. verified\n");
                    282:        return HT_OK;
                    283:     } else {
                    284:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
                    285:                           NULL, 0, "HTCredentialsFilter");
                    286:        return HT_ERROR;
                    287:     }
                    288: }
                    289: 
                    290: /*
                    291: **     Client side authentication AFTER filter
                    292: **     ---------------------------------------
                    293: **     The client side authentication filter uses the 
                    294: **     user dialog messages registered in the HTAlert module.
                    295: **     By default these are the ones used by the line mode browser but you can
                    296: **     just register something else.
                    297: */
                    298: PUBLIC int HTAuthFilter (HTRequest * request, void * param, int status)
                    299: {
                    300:     /*
                    301:     ** Ask the authentication module to call the right challenge parser
                    302:     ** that understands this scheme
                    303:     */
                    304:     if (HTAA_afterFilter(request, param, status) == HT_OK) {
                    305: 
                    306:        /*
                    307:        ** Start request with new credentials. As with the redirection filter
                    308:        ** we reuse the same request object which means that we must
                    309:        ** keep this around until the redirected request has terminated
                    310:        */
                    311:        HTLoad(request, NO);
                    312: 
                    313:        /*
                    314:        **  We return HT_ERROR to make sure that this is the last handler to be
                    315:        **  called. We do this as we don't want any other filter to delete the 
                    316:        **  request object now when we have just started a new one ourselves
                    317:        */
                    318:        return HT_ERROR;
                    319:     }
                    320:     return HT_OK;
                    321: }
                    322: 
                    323: /*
                    324: **     Request Loggin AFTER filter
                    325: **     ---------------------------
                    326: **     Default Logging filter using the log manager provided by HTLog.c
                    327: */
                    328: PUBLIC int HTLogFilter (HTRequest * request, void * param, int status)
                    329: {
                    330:     if (request) {
                    331:        if (HTLog_isOpen()) HTLog_add(request, status);
                    332:        return HT_OK;
                    333:     }
                    334:     return HT_ERROR;
                    335: }

Webmaster