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

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

Webmaster