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

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

Webmaster