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

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.38    ! frystyk     6: **     @(#) $Id: HTFilter.c,v 2.37 1999/03/09 18:59:08 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 "WWWHTTP.h"
                     20: #include "HTLog.h"
                     21: #include "HTAccess.h"
2.10      frystyk    22: #include "HTProxy.h"
                     23: #include "HTRules.h"
2.1       frystyk    24: #include "HTFilter.h"                                   /* Implemented here */
                     25: 
                     26: /* ------------------------------------------------------------------------- */
                     27: 
                     28: /*
                     29: **     Proxy and Gateway BEFORE filter
                     30: **     -------------------------------
                     31: **     Checks for registerd proxy servers or gateways and sees whether this
                     32: **     request should be redirected to a proxy or a gateway. Proxies have
                     33: **     higher priority than gateways so we look for them first!
                     34: **     For HTTP/1.0 and HTTP/1.1 we may only send a full URL (including the
                     35: **     host portion) to proxy servers. Therefore, we tell the Library whether
                     36: **     to use the full URL or the traditional HTTP one without the host part.
                     37: */
2.15      frystyk    38: PUBLIC int HTProxyFilter (HTRequest * request, void * param, int mode)
2.1       frystyk    39: {
                     40:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.2       frystyk    41:     char * addr = HTAnchor_physical(anchor);
2.1       frystyk    42:     char * physical = NULL;
                     43:     if ((physical = HTProxy_find(addr))) {
2.6       frystyk    44:        HTRequest_setFullURI(request, YES);                       /* For now */
2.5       frystyk    45:        HTRequest_setProxy(request, physical);
2.8       frystyk    46:        HT_FREE(physical);
2.6       frystyk    47: #if 0
                     48:        /* Don't paste the URLs together anymore */
2.1       frystyk    49:        StrAllocCat(physical, addr);
2.5       frystyk    50:        HTAnchor_setPhysical(anchor, physical); 
2.6       frystyk    51: #endif
2.1       frystyk    52:     } else if ((physical = HTGateway_find(addr))) {
                     53:        /* 
                     54:        ** A gateway URL is crated by chopping off any leading "/" to make the
                     55:        ** host into part of path
                     56:        */
                     57:        char * path =
                     58:            HTParse(addr, "", PARSE_HOST + PARSE_PATH + PARSE_PUNCTUATION);
2.26      frystyk    59:        char * gatewayed = HTParse((*path=='/') ? path+1 : path, physical, PARSE_ALL);
2.1       frystyk    60:        HTAnchor_setPhysical(anchor, gatewayed);
                     61:        HT_FREE(path);
                     62:        HT_FREE(gatewayed);
                     63:        HTRequest_setFullURI(request, NO);
2.6       frystyk    64:        HTRequest_deleteProxy(request);
2.1       frystyk    65:     } else {
2.6       frystyk    66:        HTRequest_setFullURI(request, NO);                        /* For now */
                     67:        HTRequest_deleteProxy(request);
2.1       frystyk    68:     }
                     69:     return HT_OK;
                     70: }
                     71: 
                     72: /*
                     73: **     Rule Translation BEFORE Filter
                     74: **     ------------------------------
                     75: **     If we have a set of rules loaded (see the Rule manager) then check
                     76: **     before each request whether how that should be translated. The trick
                     77: **     is that a parent anchor has a "address" which is the part from the URL
                     78: **     we used when we created the anchor. However, it also have a "physical
                     79: **     address" which is the place we are actually going to look for the
2.2       frystyk    80: **     resource. Hence this filter translates the physical address
                     81: **     (if any translations are found)
2.1       frystyk    82: */
2.15      frystyk    83: PUBLIC int HTRuleFilter (HTRequest * request, void * param, int mode)
2.1       frystyk    84: {
                     85:     HTList * list = HTRule_global();
                     86:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.2       frystyk    87:     char * addr = HTAnchor_physical(anchor);
2.1       frystyk    88:     char * physical = HTRule_translate(list, addr, NO);
                     89:     if (!physical) {
                     90:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_FORBIDDEN,
                     91:                           NULL, 0, "HTRuleFilter");
                     92:        return HT_ERROR;
                     93:     }
                     94:     HTAnchor_setPhysical(anchor, physical);
                     95:     HT_FREE(physical);
2.16      frystyk    96:     return HT_OK;
                     97: }
                     98: 
                     99: /*
2.15      frystyk   100: **     Check the Memory Cache (History list) BEFORE filter
                    101: **     ---------------------------------------------------
2.11      frystyk   102: **     Check if document is already loaded. The user can define whether
                    103: **     the history list should follow normal expiration or work as a
                    104: **     traditional history list where expired documents are not updated.
                    105: **     We don't check for anything but existence proof of a document
                    106: **     associated with the anchor as the definition is left to the application
                    107: */
2.15      frystyk   108: PUBLIC int HTMemoryCacheFilter (HTRequest * request, void * param, int mode)
2.11      frystyk   109: {
                    110:     HTReload validation = HTRequest_reloadMode(request);
                    111:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    112:     void * document = HTAnchor_document(anchor);
2.1       frystyk   113: 
2.11      frystyk   114:     /*
2.14      frystyk   115:     **  We only check the memory cache if it's a GET method
                    116:     */
                    117:     if (HTRequest_method(request) != METHOD_GET) {
2.35      frystyk   118:        HTTRACE(CACHE_TRACE, "Mem Cache... We only check GET methods\n");
2.14      frystyk   119:        return HT_OK;
                    120:     }
                    121: 
                    122:     /*
2.11      frystyk   123:     **  If we are asked to flush the persistent cache then there is no reason
                    124:     **  to do anything here - we're flushing it anyway. Also if no document
                    125:     **  then just exit from this filter.
                    126:     */
                    127:     if (!document || validation > HT_CACHE_FLUSH_MEM) {
2.35      frystyk   128:        HTTRACE(CACHE_TRACE, "Mem Cache... No fresh document...\n");
2.11      frystyk   129:        return HT_OK;
                    130:     }
2.1       frystyk   131: 
2.11      frystyk   132:     /*
                    133:     **  If we have a document object associated with this anchor then we also
                    134:     **  have the object in the history list. Depending on what the user asked,
                    135:     **  we can add a cache validator
                    136:     */
2.14      frystyk   137:     if (document && validation != HT_CACHE_FLUSH_MEM) {
2.35      frystyk   138:        HTTRACE(CACHE_TRACE, "Mem Cache... Document already in memory\n");
2.14      frystyk   139:        return HT_LOADED;
2.15      frystyk   140:     }
2.17      frystyk   141:     return HT_OK;
2.15      frystyk   142: }
                    143: 
                    144: /*
2.1       frystyk   145: **     Error and Information AFTER filter
                    146: **     ----------------------------------
                    147: **     It checks the status code from a request and generates an 
                    148: **     error/information message if required.
                    149: */
2.15      frystyk   150: PUBLIC int HTInfoFilter (HTRequest * request, HTResponse * response,
                    151:                         void * param, int status)
2.1       frystyk   152: {
                    153:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    154:     char * uri = HTAnchor_address((HTAnchor*) anchor);
                    155:     switch (status) {
2.28      frystyk   156:     case HT_RETRY: {
                    157:         HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    158:        if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    159:                        HTRequest_error(request), NULL);
2.35      frystyk   160:        HTTRACE(PROT_TRACE, "Load End.... NOT AVAILABLE, RETRY AT %ld\n" _ 
2.15      frystyk   161:                    HTResponse_retryTime(response));
2.28      frystyk   162:         }
                    163:         break;
2.1       frystyk   164: 
2.7       frystyk   165:     case HT_NO_DATA:
                    166:     {
                    167:        /*
                    168:        ** The document was empty
                    169:        */
                    170:        HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    171:        if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    172:                        HTRequest_error(request), NULL);
2.35      frystyk   173:        HTTRACE(PROT_TRACE, "Load End.... EMPTY: No content `%s\'\n" _ 
2.7       frystyk   174:                    uri ? uri : "<UNKNOWN>");
                    175:        break;
                    176:     }    
2.3       frystyk   177: 
                    178:     case HT_LOADED:
2.35      frystyk   179:        HTTRACE(PROT_TRACE, "Load End.... OK: `%s\'\n" _ uri);
2.16      frystyk   180:        break;
                    181: 
                    182:     default:
2.3       frystyk   183:     {
                    184:        /*
2.16      frystyk   185:        ** See if we have a function registered for outputting errors.
                    186:        ** If so then call it and present the message to the user
2.3       frystyk   187:        */
                    188:        HTAlertCallback *cbf = HTAlert_find(HT_A_MESSAGE);
                    189:        if (cbf) (*cbf)(request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    190:                        HTRequest_error(request), NULL);
2.35      frystyk   191:        HTTRACE(PROT_TRACE, "Load End.... Request ended with code %d\n" _ status);
2.1       frystyk   192:        break;
                    193:     }
2.16      frystyk   194:     }
2.1       frystyk   195:     HT_FREE(uri);
                    196:     return HT_OK;
                    197: }
                    198: 
                    199: /*
                    200: **     Redirection AFTER filter
                    201: **     ------------------------
                    202: **     The redirection handler only handles redirections
                    203: **     on the GET or HEAD method (or any other safe method)
                    204: */
2.15      frystyk   205: PUBLIC int HTRedirectFilter (HTRequest * request, HTResponse * response,
                    206:                             void * param, int status)
2.1       frystyk   207: {
                    208:     HTMethod method = HTRequest_method(request); 
2.15      frystyk   209:     HTAnchor * new_anchor = HTResponse_redirection(response); 
2.37      frystyk   210: 
                    211:     /* Check for destination */
2.7       frystyk   212:     if (!new_anchor) {
2.35      frystyk   213:        HTTRACE(PROT_TRACE, "Redirection. No destination\n");
2.7       frystyk   214:        return HT_OK;
                    215:     }
                    216: 
2.1       frystyk   217:     /*
2.21      frystyk   218:     ** Only do automatic redirect on GET and HEAD. Ask for all
2.27      frystyk   219:     ** other methods.
2.1       frystyk   220:     */
2.21      frystyk   221:     if (!HTMethod_isSafe(method)) {
                    222: 
                    223:        /*
                    224:        ** If we got a 303 See Other then change the method to GET.
                    225:        ** Otherwise ask the user whether we should continue.
                    226:        */
                    227:        if (status == HT_SEE_OTHER) {
2.35      frystyk   228:            HTTRACE(PROT_TRACE, "Redirection. Changing method from %s to GET\n" _ 
2.21      frystyk   229:                        HTMethod_name(method));
                    230:            HTRequest_setMethod(request, METHOD_GET);
                    231:        } else {
                    232:            HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
                    233:            if (prompt) {
                    234:                if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
                    235:                              NULL, NULL, NULL) != YES)
2.27      frystyk   236:                    return HT_OK;
2.21      frystyk   237:            }
2.4       frystyk   238:        }
2.1       frystyk   239:     } 
2.36      frystyk   240: 
                    241:     /* Register the redirection as a link relationship */
                    242:     {
                    243:        HTLinkType ltype = status==HT_PERM_REDIRECT ? HT_LR_PERM_REDIRECT :
                    244:            (status==HT_TEMP_REDIRECT || status==HT_FOUND) ? HT_LR_TEMP_REDIRECT :
                    245:            status==HT_SEE_OTHER ? HT_LR_SEE_OTHER : NULL;
                    246:        if (ltype) {
                    247:            HTLink_add((HTAnchor *) HTRequest_anchor(request), new_anchor, 
                    248:                       ltype, method);
                    249:        }
                    250:     }
                    251: 
2.34      frystyk   252:     /* Delete any auth credendials as they get regenerated */
                    253:     HTRequest_deleteCredentialsAll(request);
                    254: 
2.1       frystyk   255:     /*
                    256:     **  Start new request with the redirect anchor found in the headers.
                    257:     ** Note that we reuse the same request object which means that we must
                    258:     **  keep this around until the redirected request has terminated. It also
                    259:     **  allows us in an easy way to keep track of the number of redirections
                    260:     ** so that we can detect endless loops.
                    261:     */ 
2.4       frystyk   262:     if (HTRequest_doRetry(request)) { 
2.1       frystyk   263:        HTLoadAnchor(new_anchor, request);
2.9       frystyk   264:     } else {
                    265:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    266:                           NULL, 0, "HTRedirectFilter");
2.37      frystyk   267:        return HT_OK;           /* Wanna fall through */
2.9       frystyk   268:     }
                    269: 
                    270:     /*
                    271:     **  By returning HT_ERROR we make sure that this is the last handler to be
                    272:     **  called. We do this as we don't want any other filter to delete the 
                    273:     **  request object now when we have just started a new one ourselves
                    274:     */
                    275:     return HT_ERROR;
                    276: } 
                    277: 
                    278: /*
2.15      frystyk   279: **     Retry through Proxy AFTER Filter
                    280: **     --------------------------------
2.9       frystyk   281: **     This filter handles a 305 Use Proxy response and retries the request
                    282: **     through the proxy
                    283: */
2.15      frystyk   284: PUBLIC int HTUseProxyFilter (HTRequest * request, HTResponse * response,
                    285:                             void * param, int status)
2.9       frystyk   286: {
2.20      frystyk   287:     HTAlertCallback * cbf = HTAlert_find(HT_A_CONFIRM);
2.15      frystyk   288:     HTAnchor * proxy_anchor = HTResponse_redirection(response); 
2.9       frystyk   289:     if (!proxy_anchor) {
2.35      frystyk   290:        HTTRACE(PROT_TRACE, "Use Proxy... No proxy location\n");
2.9       frystyk   291:        return HT_OK;
                    292:     }
                    293: 
                    294:     /*
                    295:     **  Add the proxy to the list. Assume HTTP access method only!
2.20      frystyk   296:     **  Because evil servers may rediret the client to an untrusted
                    297:     **  proxy, we can only accept redirects for this particular
                    298:     **  server. Also, we do not know whether this is for HTTP or all
                    299:     **  other requests as well
2.9       frystyk   300:     */
2.20      frystyk   301:     if ((cbf && (*cbf)(request, HT_A_CONFIRM, HT_MSG_PROXY, NULL,NULL,NULL))) {
2.9       frystyk   302:        char * addr = HTAnchor_address(proxy_anchor);
                    303:        HTProxy_add("http", addr);
                    304:        HT_FREE(addr);
                    305:  
2.20      frystyk   306:        /*
                    307:        **  Start new request through the proxy if we haven't reached the max
                    308:        **  number of redirections for this request
                    309:        */ 
                    310:        if (HTRequest_doRetry(request)) { 
                    311:            HTLoadAnchor(proxy_anchor, request);
                    312:        } else {
                    313:            HTRequest_addError(request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    314:                               NULL, 0, "HTRedirectFilter");
                    315:        }
                    316: 
                    317:        /*
                    318:        **  By returning HT_ERROR we make sure that this is the last handler to be
                    319:        **  called. We do this as we don't want any other filter to delete the 
                    320:        **  request object now when we have just started a new one ourselves
                    321:        */
                    322:        return HT_ERROR;
                    323: 
2.1       frystyk   324:     } else {
2.20      frystyk   325:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_AUTO_PROXY,
                    326:                           NULL, 0, "HTUseProxyFilter");
                    327:        return HT_OK;
2.1       frystyk   328:     }
                    329: } 
                    330: 
                    331: /*
                    332: **     Client side authentication BEFORE filter
                    333: **     ----------------------------------------
                    334: **     The filter generates the credentials required to access a document
                    335: **     Getting the credentials may involve asking the user
                    336: */
2.15      frystyk   337: PUBLIC int HTCredentialsFilter (HTRequest * request, void * param, int mode)
2.1       frystyk   338: {
                    339:     /*
                    340:     ** Ask the authentication module to call the right credentials generator
                    341:     ** that understands this scheme
                    342:     */
2.15      frystyk   343:     if (HTAA_beforeFilter(request, param, mode) == HT_OK) {
2.35      frystyk   344:        HTTRACE(PROT_TRACE, "Credentials. verified\n");
2.1       frystyk   345:        return HT_OK;
                    346:     } else {
                    347:        HTRequest_addError(request, ERR_FATAL, NO, HTERR_UNAUTHORIZED,
                    348:                           NULL, 0, "HTCredentialsFilter");
                    349:        return HT_ERROR;
                    350:     }
                    351: }
                    352: 
                    353: /*
                    354: **     Client side authentication AFTER filter
                    355: **     ---------------------------------------
                    356: **     The client side authentication filter uses the 
                    357: **     user dialog messages registered in the HTAlert module.
                    358: **     By default these are the ones used by the line mode browser but you can
                    359: **     just register something else.
                    360: */
2.15      frystyk   361: PUBLIC int HTAuthFilter (HTRequest * request, HTResponse * response,
                    362:                         void * param, int status)
2.1       frystyk   363: {
                    364:     /*
                    365:     ** Ask the authentication module to call the right challenge parser
                    366:     ** that understands this scheme
                    367:     */
2.15      frystyk   368:     if (HTAA_afterFilter(request, response, param, status) == HT_OK) {
2.1       frystyk   369: 
                    370:        /*
                    371:        ** Start request with new credentials. As with the redirection filter
                    372:        ** we reuse the same request object which means that we must
                    373:        ** keep this around until the redirected request has terminated
                    374:        */
                    375:        HTLoad(request, NO);
                    376: 
                    377:        /*
                    378:        **  We return HT_ERROR to make sure that this is the last handler to be
                    379:        **  called. We do this as we don't want any other filter to delete the 
                    380:        **  request object now when we have just started a new one ourselves
                    381:        */
                    382:        return HT_ERROR;
                    383:     }
                    384:     return HT_OK;
2.32      kahan     385: }
                    386: 
                    387: /*
                    388: **     Client side authentication info AFTER filter
                    389: **     ---------------------------------------
                    390: */
                    391: PUBLIC int HTAuthInfoFilter (HTRequest * request, HTResponse * response,
                    392:                             void * param, int status)
                    393: {
                    394:     /*
                    395:     ** Ask the authentication module to call the right authentication info
                    396:     ** parser
                    397:     */
                    398:     if (! HTResponse_challenge (response))
                    399:       return HT_OK;
                    400:     else if (HTAA_updateFilter(request, response, param, status) == HT_OK) 
                    401:       return HT_OK;
                    402:     else
                    403:       return HT_ERROR;
2.1       frystyk   404: }
                    405: 
                    406: /*
2.24      frystyk   407: **     Request Logging AFTER filter
                    408: **     ----------------------------
2.1       frystyk   409: **     Default Logging filter using the log manager provided by HTLog.c
                    410: */
2.15      frystyk   411: PUBLIC int HTLogFilter (HTRequest * request, HTResponse * response,
                    412:                        void * param, int status)
2.1       frystyk   413: {
                    414:     if (request) {
2.23      frystyk   415:        HTLog * log = (HTLog *) param;
                    416:        if (log) HTLog_addCLF(log, request, status);
2.24      frystyk   417:        return HT_OK;
                    418:     }
                    419:     return HT_ERROR;
                    420: }
                    421: 
                    422: /*
                    423: **     Request Referer AFTER filter
                    424: **     ----------------------------
                    425: **     Default Referer Log filter using the log manager provided by HTLog.c
                    426: */
                    427: PUBLIC int HTRefererFilter (HTRequest * request, HTResponse * response,
                    428:                            void * param, int status)
                    429: {
                    430:     if (request) {
                    431:        HTLog * log = (HTLog *) param;
                    432:        if (log) HTLog_addReferer(log, request, status);
2.1       frystyk   433:        return HT_OK;
                    434:     }
                    435:     return HT_ERROR;
                    436: }

Webmaster