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

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.6     ! frystyk     6: **     @(#) $Id: HTFilter.c,v 2.5 1996/08/08 02:16:45 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;
                    203:     }
                    204: 
                    205:     case HT_LOADED:
                    206:     {
                    207:        /*
                    208:        ** Even though we have received a loaded status the thing we have
                    209:        ** loaded successfully may in fact be an error message. We therefore
                    210:        ** look at the error stack to see what to do.
                    211:        */
                    212:        HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    213:        if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    214:                        HTRequest_error(request), NULL);
                    215:        if (PROT_TRACE) HTTrace("Load End.... OK: `%s\'\n", uri);
2.1       frystyk   216:        break;
                    217:     }
                    218: 
                    219:     default:
                    220:        if (PROT_TRACE)
                    221:            HTTrace("Load End.... Request ended with code %d\n", status);
                    222:        break;
                    223:     }
                    224: 
                    225:     HT_FREE(uri);
                    226:     return HT_OK;
                    227: }
                    228: 
                    229: /*
                    230: **     Redirection AFTER filter
                    231: **     ------------------------
                    232: **     The redirection handler only handles redirections
                    233: **     on the GET or HEAD method (or any other safe method)
                    234: */
                    235: PUBLIC int HTRedirectFilter (HTRequest * request, void * param, int status)
                    236: {
                    237:     HTMethod method = HTRequest_method(request); 
                    238:     HTAnchor * new_anchor = HTRequest_redirection(request); 
                    239:  
                    240:     /*
                    241:     ** Only do redirect on GET and HEAD
                    242:     */
                    243:     if (!HTMethod_isSafe(method) || !new_anchor) { 
2.4       frystyk   244:        HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
                    245:        if (prompt) {
                    246:            if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
                    247:                          NULL, NULL, NULL) != YES)
                    248:                return HT_ERROR;
                    249:        }
2.1       frystyk   250:     } 
                    251:  
                    252:     /*
                    253:     **  Start new request with the redirect anchor found in the headers.
                    254:     ** Note that we reuse the same request object which means that we must
                    255:     **  keep this around until the redirected request has terminated. It also
                    256:     **  allows us in an easy way to keep track of the number of redirections
                    257:     ** so that we can detect endless loops.
                    258:     */ 
2.4       frystyk   259:     if (HTRequest_doRetry(request)) { 
2.1       frystyk   260:        HTLoadAnchor(new_anchor, request);
                    261:     } else {
                    262:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    263:                           NULL, 0, "HTRedirectFilter");
                    264:     }
                    265: 
                    266:     /*
                    267:     **  By returning HT_ERROR we make sure that this is the last handler to be
                    268:     **  called. We do this as we don't want any other filter to delete the 
                    269:     **  request object now when we have just started a new one ourselves
                    270:     */
                    271:     return HT_ERROR;
                    272: } 
                    273: 
                    274: /*
                    275: **     Client side authentication BEFORE filter
                    276: **     ----------------------------------------
                    277: **     The filter generates the credentials required to access a document
                    278: **     Getting the credentials may involve asking the user
                    279: */
                    280: PUBLIC int HTCredentialsFilter (HTRequest * request, void * param, int status)
                    281: {
                    282:     /*
                    283:     ** Ask the authentication module to call the right credentials generator
                    284:     ** that understands this scheme
                    285:     */
                    286:     if (HTAA_beforeFilter(request, param, status) == HT_OK) {
                    287:        if (PROT_TRACE) HTTrace("Credentials. verified\n");
                    288:        return HT_OK;
                    289:     } else {
                    290:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
                    291:                           NULL, 0, "HTCredentialsFilter");
                    292:        return HT_ERROR;
                    293:     }
                    294: }
                    295: 
                    296: /*
                    297: **     Client side authentication AFTER filter
                    298: **     ---------------------------------------
                    299: **     The client side authentication filter uses the 
                    300: **     user dialog messages registered in the HTAlert module.
                    301: **     By default these are the ones used by the line mode browser but you can
                    302: **     just register something else.
                    303: */
                    304: PUBLIC int HTAuthFilter (HTRequest * request, void * param, int status)
                    305: {
                    306:     /*
                    307:     ** Ask the authentication module to call the right challenge parser
                    308:     ** that understands this scheme
                    309:     */
                    310:     if (HTAA_afterFilter(request, param, status) == HT_OK) {
                    311: 
                    312:        /*
                    313:        ** Start request with new credentials. As with the redirection filter
                    314:        ** we reuse the same request object which means that we must
                    315:        ** keep this around until the redirected request has terminated
                    316:        */
                    317:        HTLoad(request, NO);
                    318: 
                    319:        /*
                    320:        **  We return HT_ERROR to make sure that this is the last handler to be
                    321:        **  called. We do this as we don't want any other filter to delete the 
                    322:        **  request object now when we have just started a new one ourselves
                    323:        */
                    324:        return HT_ERROR;
                    325:     }
                    326:     return HT_OK;
                    327: }
                    328: 
                    329: /*
                    330: **     Request Loggin AFTER filter
                    331: **     ---------------------------
                    332: **     Default Logging filter using the log manager provided by HTLog.c
                    333: */
                    334: PUBLIC int HTLogFilter (HTRequest * request, void * param, int status)
                    335: {
                    336:     if (request) {
                    337:        if (HTLog_isOpen()) HTLog_add(request, status);
                    338:        return HT_OK;
                    339:     }
                    340:     return HT_ERROR;
                    341: }

Webmaster