Annotation of libwww/Library/src/HTAnchor.c, revision 1.67

1.14      frystyk     1: /*                                                                  HTAnchor.c
                      2: **     HYPERTEXT "ANCHOR" OBJECT
1.1       timbl       3: **
1.22      frystyk     4: **     (c) COPYRIGHT MIT 1995.
1.14      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.67    ! frystyk     6: **     @(#) $Id: HTAnchor.c,v 1.66 1997/02/11 23:15:19 frystyk Exp $
1.14      frystyk     7: **
                      8: **     An anchor represents a region of a hypertext document which is
                      9: **     linked to another anchor in the same or a different document.
1.1       timbl      10: **
                     11: ** History
                     12: **         Nov 1990  Written in Objective-C for the NeXT browser (TBL)
                     13: **     24-Oct-1991 (JFG), written in C, browser-independant 
                     14: **     21-Nov-1991 (JFG), first complete version
1.41      frystyk    15: **      3-May-1995 (HF), Added a lot of methods and other stuff made an object
1.54      frystyk    16: **     July 1996       Patch for adding hash of children Michael Farrar
1.1       timbl      17: */
                     18: 
1.16      frystyk    19: /* Library include files */
1.46      frystyk    20: #include "sysdep.h"
1.50      frystyk    21: #include "WWWUtil.h"
1.7       luotonen   22: #include "HTFormat.h"
1.1       timbl      23: #include "HTParse.h"
1.24      frystyk    24: #include "HTMethod.h"
1.63      frystyk    25: #include "HTWWWStr.h"
1.41      frystyk    26: #include "HTAncMan.h"                                   /* Implemented here */
1.11      frystyk    27: 
1.54      frystyk    28: #define HASH_SIZE      101        /* Arbitrary prime. Memory/speed tradeoff */
                     29: #define CHILD_HASH_SIZE         67            /* Often smaller than hash of parents */
1.1       timbl      30: 
                     31: PRIVATE HTList **adult_table=0;  /* Point to table of lists of all parents */
                     32: 
1.17      frystyk    33: /* ------------------------------------------------------------------------- */
                     34: /*                             Creation Methods                             */
                     35: /* ------------------------------------------------------------------------- */
                     36: 
                     37: /*
1.1       timbl      38: **     Do not use "new" by itself outside this module. In order to enforce
                     39: **     consistency, we insist that you furnish more information about the
                     40: **     anchor you are creating : use newWithParent or newWithAddress.
                     41: */
1.35      frystyk    42: PRIVATE HTParentAnchor * HTParentAnchor_new (void)
1.1       timbl      43: {
1.43      frystyk    44:     HTParentAnchor *newAnchor;
1.54      frystyk    45:     if ((newAnchor = (HTParentAnchor *) HT_CALLOC(1, sizeof (HTParentAnchor))) == NULL)
1.43      frystyk    46:        HT_OUTOFMEM("HTParentAnchor_new");
1.16      frystyk    47:     newAnchor->parent = newAnchor;
1.17      frystyk    48:     newAnchor->content_type = WWW_UNKNOWN;
1.24      frystyk    49:     newAnchor->mainLink.method = METHOD_INVALID;
1.41      frystyk    50:     newAnchor->content_length = -1;                     /* howcome 6 dec 95 */
1.28      frystyk    51:     newAnchor->date = (time_t) -1;
                     52:     newAnchor->expires = (time_t) -1;
                     53:     newAnchor->last_modified = (time_t) -1;
1.59      frystyk    54:     newAnchor->age = (time_t) -1;
1.16      frystyk    55:     return newAnchor;
1.1       timbl      56: }
                     57: 
1.16      frystyk    58: 
1.35      frystyk    59: PRIVATE HTChildAnchor * HTChildAnchor_new (void)
1.1       timbl      60: {
1.43      frystyk    61:     HTChildAnchor *child;
                     62:     if ((child = (HTChildAnchor  *) HT_CALLOC(1, sizeof(HTChildAnchor))) == NULL)
                     63:         HT_OUTOFMEM("HTChildAnchor_new");
1.33      frystyk    64:     return child;
1.1       timbl      65: }
                     66: 
1.17      frystyk    67: /*     Create new or find old child anchor
                     68: **     -----------------------------------
1.1       timbl      69: **
1.3       timbl      70: **     Me one is for a new anchor being edited into an existing
1.17      frystyk    71: **     document. The parent anchor must already exist. All
                     72: **     children without tags (no NAME attribut) points to the same NULL
                     73: **     child.
1.54      frystyk    74: **     Children are now hashed for performance reasons. Thanks to
                     75: **     Michael Farrar
1.1       timbl      76: */
1.35      frystyk    77: PUBLIC HTChildAnchor * HTAnchor_findChild (HTParentAnchor *    parent,
1.46      frystyk    78:                                           const char *         tag)
1.1       timbl      79: {
1.54      frystyk    80:     HTChildAnchor * child = NULL;
                     81:     HTList * kids = NULL;
1.17      frystyk    82:     if (!parent) {
1.54      frystyk    83:        if (ANCH_TRACE) HTTrace("Child Anchor Bad argument\n");
1.17      frystyk    84:        return NULL;
                     85:     }
1.1       timbl      86: 
1.54      frystyk    87:     /* Find a hash for this tag (if any) */
                     88:     {
                     89:        int hash = 0;
                     90:        /*
                     91:        ** If tag is empty then use hash value 0
                     92:        */
                     93:        if (tag) {
                     94:            const char * ptr = tag;
                     95:            for(; *ptr; ptr++)
                     96:                hash = (int) ((hash*3 + (*(unsigned char*)ptr)) % CHILD_HASH_SIZE);
                     97:        }
1.55      frystyk    98:        if (!parent->children) {
                     99:            if (!(parent->children = (HTList **)        
1.54      frystyk   100:                  HT_CALLOC(CHILD_HASH_SIZE, sizeof(HTList *))))
                    101:                HT_OUTOFMEM("HTAnchor_findChild");
                    102:        }
1.55      frystyk   103:        if (!parent->children[hash]) parent->children[hash] = HTList_new();
                    104:        kids = parent->children[hash];
1.54      frystyk   105:     }
                    106: 
1.17      frystyk   107:     /* First search list of children to see if tag is already there */
1.54      frystyk   108:     if (tag && *tag) {
                    109:        HTList * cur = kids;
                    110:        while ((child = (HTChildAnchor *) HTList_nextObject(cur))) {
1.56      frystyk   111:            if (child->tag && !strcmp(child->tag, tag)) {
1.54      frystyk   112:                if (ANCH_TRACE)
                    113:                    HTTrace("Child Anchor %p of parent %p with name `%s' already exists.\n",
                    114:                            (void *) child, (void *) parent, tag);
                    115:                return child;
1.1       timbl     116:            }
                    117:        }
1.54      frystyk   118:     }
                    119: 
                    120:     /* If not found then create a new child anchor */
1.17      frystyk   121:     child = HTChildAnchor_new();
1.54      frystyk   122:     HTList_addObject(kids, (void *) child);
1.17      frystyk   123:     child->parent = parent;
1.54      frystyk   124:     if (tag) StrAllocCopy(child->tag, tag);
1.17      frystyk   125:     if (ANCH_TRACE)
1.54      frystyk   126:        HTTrace("Child Anchor New Anchor %p named `%s' is child of %p\n",
1.46      frystyk   127:                (void *) child, tag ? tag : (const char *) "", (void *)parent);
1.17      frystyk   128:     return child;
1.1       timbl     129: }
                    130: 
                    131: 
                    132: /*     Create new or find old named anchor
                    133: **     -----------------------------------
                    134: **
1.3       timbl     135: **     Me one is for a reference which is found in a document, and might
1.1       timbl     136: **     not be already loaded.
                    137: **     Note: You are not guaranteed a new anchor -- you might get an old one,
                    138: **     like with fonts.
                    139: */
1.46      frystyk   140: PUBLIC HTAnchor * HTAnchor_findAddress (const char * address)
1.1       timbl     141: {
1.16      frystyk   142:     char *tag = HTParse (address, "", PARSE_ANCHOR);           /* Any tags? */
1.1       timbl     143:     
1.16      frystyk   144:     /* If the address represents a sub-anchor, we recursively load its parent,
                    145:        then we create a child anchor within that document. */
                    146:     if (*tag) {
1.34      frystyk   147:        char *addr = HTParse(address, "", PARSE_ACCESS | PARSE_HOST |
                    148:                             PARSE_PATH | PARSE_PUNCTUATION);
                    149:        HTParentAnchor * parent = (HTParentAnchor*) HTAnchor_findAddress(addr);
                    150:        HTChildAnchor * child = HTAnchor_findChild(parent, tag);
1.43      frystyk   151:        HT_FREE(addr);
                    152:        HT_FREE(tag);
1.34      frystyk   153:        return (HTAnchor *) child;
1.16      frystyk   154:     } else {                        /* Else check whether we have this node */
                    155:        int hash;
1.46      frystyk   156:        const char *p;
1.16      frystyk   157:        HTList * adults;
                    158:        HTList *grownups;
                    159:        HTParentAnchor * foundAnchor;
1.34      frystyk   160:        char *newaddr = NULL;
                    161:        StrAllocCopy(newaddr, address);                  /* Get our own copy */
1.43      frystyk   162:        HT_FREE(tag);
1.38      frystyk   163:        newaddr = HTSimplify(&newaddr);
1.34      frystyk   164: 
1.16      frystyk   165:        /* Select list from hash table */
                    166:        for(p=newaddr, hash=0; *p; p++)
                    167:            hash = (int) ((hash * 3 + (*(unsigned char*)p)) % HASH_SIZE);
1.33      frystyk   168:        if (!adult_table) {
1.43      frystyk   169:            if ((adult_table = (HTList* *) HT_CALLOC(HASH_SIZE, sizeof(HTList*))) == NULL)
                    170:                HT_OUTOFMEM("HTAnchor_findAddress");
1.33      frystyk   171:        }
1.16      frystyk   172:        if (!adult_table[hash]) adult_table[hash] = HTList_new();
                    173:        adults = adult_table[hash];
                    174: 
                    175:        /* Search list for anchor */
                    176:        grownups = adults;
                    177:        while ((foundAnchor = (HTParentAnchor *) HTList_nextObject(grownups))){
1.33      frystyk   178:            if (!strcmp(foundAnchor->address, newaddr)) {
1.16      frystyk   179:                if (ANCH_TRACE)
1.44      eric      180:                    HTTrace("Find Parent. %p with address `%s' already exists.\n",
1.16      frystyk   181:                            (void*) foundAnchor, newaddr);
1.43      frystyk   182:                HT_FREE(newaddr);                      /* We already have it */
1.16      frystyk   183:                return (HTAnchor *) foundAnchor;
                    184:            }
                    185:        }
                    186:        
                    187:        /* Node not found : create new anchor. */
                    188:        foundAnchor = HTParentAnchor_new();
                    189:        foundAnchor->address = newaddr;                 /* Remember our copy */
                    190:        HTList_addObject (adults, foundAnchor);
1.44      eric      191:        if (ANCH_TRACE) HTTrace("Find Parent. %p with hash %d and address `%s' created\n", (void*)foundAnchor, hash, newaddr);
1.1       timbl     192:        return (HTAnchor *) foundAnchor;
                    193:     }
                    194: }
                    195: 
1.17      frystyk   196: /*     Create or find a child anchor with a possible link
                    197: **     --------------------------------------------------
                    198: **
                    199: **     Create new anchor with a given parent and possibly
                    200: **     a name, and possibly a link to a _relatively_ named anchor.
1.34      frystyk   201: **     All parameters EXCEPT parent can be NULL
1.17      frystyk   202: */
1.34      frystyk   203: PUBLIC HTChildAnchor * HTAnchor_findChildAndLink (HTParentAnchor *     parent,
1.46      frystyk   204:                                                  const char *          tag,
                    205:                                                  const char *          href,
1.41      frystyk   206:                                                  HTLinkType            ltype)
1.17      frystyk   207: {
1.34      frystyk   208:     HTChildAnchor * child = HTAnchor_findChild(parent, tag);
1.54      frystyk   209:     if (child && href && *href) {
1.53      frystyk   210:        char * relative_to = HTAnchor_expandedAddress((HTAnchor *) parent);
1.34      frystyk   211:        char * parsed_address = HTParse(href, relative_to, PARSE_ALL);
                    212:        HTAnchor * dest = HTAnchor_findAddress(parsed_address);
1.52      frystyk   213:        HTLink_add((HTAnchor *) child, dest, ltype, METHOD_INVALID);
1.43      frystyk   214:        HT_FREE(parsed_address);
                    215:        HT_FREE(relative_to);
1.34      frystyk   216:     }
                    217:     return child;
1.17      frystyk   218: }
                    219: 
1.52      frystyk   220: /* ------------------------------------------------------------------------- */
                    221: /*                                Link Methods                              */
                    222: /* ------------------------------------------------------------------------- */
1.28      frystyk   223: 
1.41      frystyk   224: /*
                    225: **  Upgrade the link to the main destination and and downgrade the
                    226: **  current main link to the list
                    227: */
                    228: PUBLIC HTLink * HTAnchor_mainLink (HTAnchor * me)
                    229: {
                    230:     return me ? &(me->mainLink) : NULL;
                    231: }
1.28      frystyk   232: 
1.41      frystyk   233: PUBLIC BOOL HTAnchor_setMainLink  (HTAnchor * me, HTLink * movingLink)
1.28      frystyk   234: {
1.41      frystyk   235:     if (!(me && me->links && movingLink &&
                    236:          HTList_removeObject(me->links, movingLink)))
                    237:        return NO;
                    238:     else {
                    239:        /* First push current main link onto top of links list */
1.52      frystyk   240:        HTLink * newLink = HTLink_new();
1.42      frystyk   241:        memcpy ((void *) newLink, & me->mainLink, sizeof (HTLink));
1.41      frystyk   242:        HTList_addObject (me->links, newLink);
                    243: 
1.52      frystyk   244:        /* Now make movingLink the new main link, and delete it */
1.42      frystyk   245:        memcpy ((void *) &me->mainLink, movingLink, sizeof (HTLink));
1.52      frystyk   246:        HTLink_delete(movingLink);
1.28      frystyk   247:        return YES;
                    248:     }
                    249: }
                    250: 
                    251: /*
1.41      frystyk   252: **     Handling sub links
1.24      frystyk   253: */
1.41      frystyk   254: PUBLIC HTList * HTAnchor_subLinks (HTAnchor * anchor)
1.24      frystyk   255: {
1.41      frystyk   256:     return anchor ? anchor->links : NULL;
1.24      frystyk   257: }
                    258: 
1.41      frystyk   259: PUBLIC BOOL HTAnchor_setSubLinks (HTAnchor * anchor, HTList * list)
                    260: {
                    261:     if (anchor) {
                    262:        anchor->links = list;
                    263:        return YES;
                    264:     }
                    265:     return NO;
                    266: }
1.24      frystyk   267: 
                    268: /*
1.41      frystyk   269: **  Returns the main destination of this anchor
                    270: */
                    271: PUBLIC HTAnchor * HTAnchor_followMainLink (HTAnchor * me)
                    272: {
1.52      frystyk   273:     return me ? HTLink_destination(&me->mainLink) : NULL;
1.17      frystyk   274: }
                    275: 
                    276: /* ------------------------------------------------------------------------- */
                    277: /*                             Deletion Methods                             */
                    278: /* ------------------------------------------------------------------------- */
1.1       timbl     279: 
                    280: /*     Delete an anchor and possibly related things (auto garbage collection)
                    281: **     --------------------------------------------
                    282: **
                    283: **     The anchor is only deleted if the corresponding document is not loaded.
1.10      frystyk   284: **     All outgoing links from parent and children are deleted, and this
                    285: **     anchor is removed from the sources list of all its targets.
1.1       timbl     286: **     We also try to delete the targets whose documents are not loaded.
                    287: **     If this anchor's source list is empty, we delete it and its children.
                    288: */
                    289: 
1.41      frystyk   290: /*     Deletes all the memory allocated in a parent anchor and returns any
                    291: **     hyperdoc object hanging of this anchor
1.19      frystyk   292: */
1.41      frystyk   293: PRIVATE void * delete_parent (HTParentAnchor * me)
1.19      frystyk   294: {
1.41      frystyk   295:     void * doc = me->document;
1.19      frystyk   296: 
                    297:     /* Remove link and address information */
1.26      frystyk   298:     if (me->links) {
                    299:        HTList *cur = me->links;
                    300:        HTLink *pres;
                    301:        while ((pres = (HTLink *) HTList_nextObject(cur)))
1.52      frystyk   302:            HTLink_delete(pres);
1.26      frystyk   303:        HTList_delete(me->links);
                    304:     }
1.54      frystyk   305: 
                    306:     /* Remove children */
                    307:     if (me->children) {
                    308:        int cnt = 0;
                    309:        for (; cnt<CHILD_HASH_SIZE; cnt++) {
                    310:            if (me->children[cnt]) HTList_delete(me->children[cnt]);
                    311:        }
                    312:        HT_FREE(me->children);
                    313:     }
                    314: 
1.19      frystyk   315:     HTList_delete (me->sources);
1.52      frystyk   316:     HTList_delete (me->variants);
1.43      frystyk   317:     HT_FREE(me->physical);
                    318:     HT_FREE(me->address);
1.19      frystyk   319: 
                    320:     /* Then remove entity header information (metainformation) */
1.59      frystyk   321:     HTAnchor_clearHeader(me);
1.57      frystyk   322: 
1.43      frystyk   323:     HT_FREE(me);
1.19      frystyk   324:     return doc;
                    325: }
                    326: 
                    327: 
1.41      frystyk   328: /*     Delete a parent anchor and all its children. If a hyperdoc object
                    329: **     is found hanging off the parent anchor then this is returned
1.19      frystyk   330: */
1.41      frystyk   331: PRIVATE void * delete_family (HTAnchor * me)
1.19      frystyk   332: {
1.64      frystyk   333:     HTParentAnchor * parent = NULL;
1.19      frystyk   334:     if (!me) {
1.64      frystyk   335:        if (ANCH_TRACE) HTTrace("AnchorDelete No anchor found\n");
1.19      frystyk   336:        return NULL;
                    337:     }
1.64      frystyk   338:     parent = me->parent;
                    339:     if (ANCH_TRACE)
                    340:        HTTrace("AnchorDelete Remove parent %p and children\n", parent);
1.19      frystyk   341: 
                    342:     /* Delete children */
                    343:     if (parent->children) {
1.54      frystyk   344:        int cnt = 0;
                    345:        for (; cnt<CHILD_HASH_SIZE; cnt++) {
                    346:            HTList * kids = parent->children[cnt];
                    347:            if (kids) {
                    348:                HTChildAnchor * child;
                    349:                while ((child=(HTChildAnchor*)HTList_removeLastObject(kids))) {
                    350:                    HT_FREE(child->tag);
                    351:                    if (child->links) {
                    352:                        HTList * cur = child->links;
                    353:                        HTLink * pres;
                    354:                        while ((pres = (HTLink *) HTList_nextObject(cur)))
                    355:                            HTLink_delete(pres);
                    356:                        HTList_delete(child->links);
                    357:                    }
                    358:                    HT_FREE(child);
                    359:                }
                    360:                HTList_delete(kids);
                    361:                parent->children[cnt] = NULL;
1.26      frystyk   362:            }
1.19      frystyk   363:        }
                    364:     }
                    365:     return delete_parent(parent);
                    366: }
                    367: 
                    368: 
                    369: /*     DELETE ALL ANCHORS
                    370: **     ------------------
                    371: **     Deletes all anchors and return a list of all the HyperDocs found.
                    372: **     It is for the application to delete any HyperDocs.
1.39      frystyk   373: **     If NULL then no hyperdocs are returned
1.19      frystyk   374: **     Return YES if OK, else NO
                    375: */
1.35      frystyk   376: PUBLIC BOOL HTAnchor_deleteAll (HTList * documents)
1.19      frystyk   377: {
                    378:     int cnt;
                    379:     HTList *cur;
1.39      frystyk   380:     if (!adult_table)
1.19      frystyk   381:        return NO;
                    382:     for (cnt=0; cnt<HASH_SIZE; cnt++) {
                    383:        if ((cur = adult_table[cnt])) { 
                    384:            HTParentAnchor *pres;
                    385:            while ((pres = (HTParentAnchor *) HTList_nextObject(cur)) != NULL){
1.41      frystyk   386:                void * doc = delete_family((HTAnchor *) pres);
                    387:                if (doc && documents) HTList_addObject(documents, doc);
1.19      frystyk   388:            }
                    389:        }
                    390:        HTList_delete(adult_table[cnt]);
                    391:     }
1.43      frystyk   392:     HT_FREE(adult_table);
1.19      frystyk   393:     return YES;
                    394: }
                    395: 
                    396: 
1.54      frystyk   397: PRIVATE void delete_links (HTAnchor * me)
1.1       timbl     398: {
1.3       timbl     399:   if (! me)
1.1       timbl     400:     return;
                    401: 
                    402:   /* Recursively try to delete target anchors */
1.3       timbl     403:   if (me->mainLink.dest) {
                    404:     HTParentAnchor *parent = me->mainLink.dest->parent;
                    405:     HTList_removeObject (parent->sources, me);
1.1       timbl     406:     if (! parent->document)  /* Test here to avoid calling overhead */
                    407:       HTAnchor_delete (parent);
                    408:   }
1.3       timbl     409:   if (me->links) {  /* Extra destinations */
1.1       timbl     410:     HTLink *target;
1.12      frystyk   411:     while ((target = (HTLink *) HTList_removeLastObject (me->links))) {
1.1       timbl     412:       HTParentAnchor *parent = target->dest->parent;
1.3       timbl     413:       HTList_removeObject (parent->sources, me);
1.1       timbl     414:       if (! parent->document)  /* Test here to avoid calling overhead */
                    415:        HTAnchor_delete (parent);
                    416:     }
                    417:   }
                    418: }
                    419: 
1.35      frystyk   420: PUBLIC BOOL HTAnchor_delete (HTParentAnchor * me)
1.1       timbl     421: {
1.54      frystyk   422:     /* Don't delete if document is loaded */
                    423:     if (!me || me->document) {
                    424:        if (ANCH_TRACE) HTTrace("Anchor...... Not deleted\n");
                    425:        return NO;
                    426:     }
                    427: 
                    428:     /* Recursively try to delete target anchors */
                    429:     delete_links ((HTAnchor *) me);
1.1       timbl     430: 
1.54      frystyk   431:     if (!HTList_isEmpty(me->sources)) {    /* There are still incoming links */
                    432: 
                    433:        /*
                    434:        ** Delete all outgoing links from children, if any
                    435:        */
                    436:        if (me->children) {
                    437:            int cnt = 0;
                    438:            for (; cnt<CHILD_HASH_SIZE; cnt++) {
                    439:                HTList * kids = me->children[cnt];
                    440:                if (kids) {
                    441:                    HTChildAnchor * child;
                    442:                    while ((child = (HTChildAnchor *) HTList_nextObject(kids)))
                    443:                        delete_links((HTAnchor *) child);
                    444:                    return NO;  /* Parent not deleted */
                    445:                }
                    446:            }
                    447:        }
1.1       timbl     448: 
1.54      frystyk   449:        /*
                    450:        ** No more incoming links : kill everything
                    451:        ** First, recursively delete children
                    452:        */
                    453:        if (me->children) {
                    454:            int cnt = 0;
                    455:            for (; cnt<CHILD_HASH_SIZE; cnt++) {
                    456:                HTList * kids = me->children[cnt];
                    457:                if (kids) {
                    458:                    HTChildAnchor * child;
                    459:                    while ((child=(HTChildAnchor *) HTList_removeLastObject(kids)))
                    460:                        delete_links((HTAnchor *) child);
                    461:                    HT_FREE(child->tag);
                    462:                    HT_FREE(child);
                    463:                }
                    464:            }
                    465:        }
                    466:     }
1.1       timbl     467: 
1.54      frystyk   468:     /* Now kill myself */
                    469:     delete_parent(me);
                    470:     return YES;  /* Parent deleted */
                    471: #if 0
1.3       timbl     472:   if (! HTList_isEmpty (me->sources)) {  /* There are still incoming links */
1.1       timbl     473:     /* Delete all outgoing links from children, if any */
1.3       timbl     474:     HTList *kids = me->children;
1.12      frystyk   475:     while ((child = (HTChildAnchor *) HTList_nextObject (kids)))
1.54      frystyk   476:       delete_links ((HTAnchor *) child);
1.1       timbl     477:     return NO;  /* Parent not deleted */
                    478:   }
                    479: 
                    480:   /* No more incoming links : kill everything */
                    481:   /* First, recursively delete children */
1.12      frystyk   482:   while ((child = (HTChildAnchor *) HTList_removeLastObject (me->children))) {
1.54      frystyk   483:     delete_links ((HTAnchor *) child);
1.43      frystyk   484:     HT_FREE(child->tag);
                    485:     HT_FREE(child);
1.1       timbl     486:   }
1.54      frystyk   487: #endif
1.1       timbl     488: }
                    489: 
1.17      frystyk   490: /* ------------------------------------------------------------------------- */
                    491: /*                             Data Access Methods                          */
                    492: /* ------------------------------------------------------------------------- */
1.1       timbl     493: 
1.35      frystyk   494: PUBLIC HTParentAnchor * HTAnchor_parent  (HTAnchor * me)
1.1       timbl     495: {
1.17      frystyk   496:     return me ? me->parent : NULL;
1.1       timbl     497: }
                    498: 
1.41      frystyk   499: PUBLIC void HTAnchor_setDocument  (HTParentAnchor * me, void * doc)
1.1       timbl     500: {
1.41      frystyk   501:     if (me) me->document = doc;
1.1       timbl     502: }
                    503: 
1.41      frystyk   504: PUBLIC void * HTAnchor_document  (HTParentAnchor * me)
1.1       timbl     505: {
1.17      frystyk   506:     return me ? me->document : NULL;
1.1       timbl     507: }
                    508: 
1.53      frystyk   509: PUBLIC char * HTAnchor_address  (HTAnchor * me) 
                    510: { 
                    511:     char *addr = NULL;
                    512:     if (me) {
                    513:         if (((HTParentAnchor *) me == me->parent) ||
                    514:             !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
                    515:             StrAllocCopy (addr, me->parent->address);
                    516:         }
                    517:         else {                  /* it's a named child */
                    518:             if ((addr = (char  *) HT_MALLOC(2 + strlen (me->parent->address) + \
                    519: strlen (((HTChildAnchor *) me)->tag))) == NULL)
                    520:                 HT_OUTOFMEM("HTAnchor_address");
                    521:             sprintf (addr, "%s#%s", me->parent->address,
                    522:                      ((HTChildAnchor *) me)->tag);
                    523:         }
                    524:     }
                    525:     return addr;
                    526: }
                    527: 
1.51      frystyk   528: /*
1.53      frystyk   529: **     We resolve the child address with respect to either a base URL,
                    530: **     a content-location, or to the request-URI
1.51      frystyk   531: */
1.53      frystyk   532: PUBLIC char * HTAnchor_expandedAddress  (HTAnchor * me)
1.1       timbl     533: {
1.17      frystyk   534:     char *addr = NULL;
                    535:     if (me) {
1.53      frystyk   536:        HTParentAnchor * parent = me->parent;
1.63      frystyk   537:        char * base = HTAnchor_base(parent);
1.17      frystyk   538:        if (((HTParentAnchor *) me == me->parent) ||
                    539:            !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
1.52      frystyk   540:            StrAllocCopy(addr, base);
1.51      frystyk   541:        } else {                        /* it's a named child */
1.52      frystyk   542:            if ((addr = (char *) HT_MALLOC(2 + strlen(base) + strlen(((HTChildAnchor *) me)->tag))) == NULL)
1.43      frystyk   543:                HT_OUTOFMEM("HTAnchor_address");
1.52      frystyk   544:            sprintf (addr, "%s#%s", base, ((HTChildAnchor *) me)->tag);
1.17      frystyk   545:        }
1.1       timbl     546:     }
1.17      frystyk   547:     return addr;
1.1       timbl     548: }
                    549: 
1.52      frystyk   550: /*     Physical Address
                    551: **     ----------------
                    552: */
                    553: PUBLIC char * HTAnchor_physical (HTParentAnchor * me)
                    554: {
1.53      frystyk   555:     return me ? me->physical ? me->physical : me->address : NULL;
1.52      frystyk   556: }
                    557: 
1.53      frystyk   558: PUBLIC void HTAnchor_setPhysical (HTParentAnchor * me, char * physical)
1.52      frystyk   559: {
                    560:     if (!me || !physical) {
                    561:        if (ANCH_TRACE)
                    562:            HTTrace("HTAnchor.... setPhysical, called with null argument\n");
                    563:        return;
                    564:     }
                    565:     StrAllocCopy(me->physical, physical);
                    566: }
                    567: 
1.53      frystyk   568: PUBLIC void HTAnchor_clearPhysical(HTParentAnchor * me)
                    569: {
                    570:     if (me) HT_FREE(me->physical);
                    571: }
                    572: 
                    573: /*
                    574: **     Children information
                    575: */
1.35      frystyk   576: PUBLIC BOOL HTAnchor_hasChildren  (HTParentAnchor * me)
1.17      frystyk   577: {
1.54      frystyk   578:     return (me && me->children);
1.17      frystyk   579: }
1.1       timbl     580: 
1.63      frystyk   581: /* ------------------------------------------------------------------------- */
                    582: /*                           Entity Header Information                      */
                    583: /* ------------------------------------------------------------------------- */
                    584: 
1.59      frystyk   585: /*
1.63      frystyk   586: **  Take the relevant infomration from the response object and cache it
                    587: **  in the anchor object. We inherit the information that is already
                    588: **  parsed in the response along with the unparsed headers.
1.52      frystyk   589: */
1.63      frystyk   590: PUBLIC BOOL HTAnchor_update (HTParentAnchor * me, HTResponse * response)
1.52      frystyk   591: {
1.63      frystyk   592:     if (me && response) {
1.59      frystyk   593: 
1.63      frystyk   594:        if (ANCH_TRACE)
                    595:            HTTrace("HTAnchor.... Updating metainformation for %p\n", me);
1.59      frystyk   596: 
1.63      frystyk   597:        /*
                    598:        **  The content length and type is already parsed at this point
                    599:        **  in time. We also check for format parameters like charset etc.
                    600:        **  and copy the contents in the anchor object
                    601:        */
                    602:        me->content_length = HTResponse_length(response);
                    603:        me->content_type = HTResponse_format(response);
                    604:        me->type_parameters = HTResponse_formatParam(response);
                    605:        
                    606:        /*
                    607:        **  Inherit all the unparsed headers - we may need them later!
                    608:        */
                    609:        me->headers = HTResponse_header(response);
1.59      frystyk   610: 
                    611:        /*
1.63      frystyk   612:        **  Notifify the response object not to delete the lists that we
                    613:        **  have inherited in the anchor object
1.59      frystyk   614:        */
1.63      frystyk   615:        HTResponse_isCached(response, YES);
1.59      frystyk   616: 
                    617:        return YES;
                    618:     }
                    619:     return NO;
1.52      frystyk   620: }
                    621: 
                    622: /*
                    623: **     Variants. If this anchor has any variants then keep them in a list
                    624: **     so that we can find them later. The list is simply a list of 
                    625: **     parent anchors.
                    626: */
                    627: PUBLIC HTList * HTAnchor_variants (HTParentAnchor * me)
1.17      frystyk   628: {
1.52      frystyk   629:     return me ? me->variants : NULL;
1.17      frystyk   630: }
1.1       timbl     631: 
1.52      frystyk   632: PUBLIC BOOL HTAnchor_addVariant (HTParentAnchor * me,
                    633:                                 HTParentAnchor * variant)
1.1       timbl     634: {
1.52      frystyk   635:     if (me && variant) {
                    636:        if (!me->variants) me->variants = HTList_new();
                    637:        return HTList_addObject(me->variants, variant);
                    638:     }
                    639:     return NO;
1.17      frystyk   640: }
                    641: 
1.52      frystyk   642: PUBLIC BOOL HTAnchor_deleteVariant (HTParentAnchor * me,
                    643:                                    HTParentAnchor * variant)
1.17      frystyk   644: {
1.52      frystyk   645:     return (me && variant) ? HTList_removeObject(me->variants, variant) : NO;
1.9       frystyk   646: }
1.1       timbl     647: 
1.52      frystyk   648: /*
                    649: **     Is this resource an index?
1.17      frystyk   650: */
1.52      frystyk   651: PUBLIC void HTAnchor_clearIndex  (HTParentAnchor * me)
                    652: {
                    653:     if (me) me->isIndex = NO;
                    654: }
1.17      frystyk   655: 
1.52      frystyk   656: PUBLIC void HTAnchor_setIndex  (HTParentAnchor * me)
1.1       timbl     657: {
1.52      frystyk   658:   if (me) me->isIndex = YES;
1.1       timbl     659: }
                    660: 
1.52      frystyk   661: PUBLIC BOOL HTAnchor_isIndex  (HTParentAnchor * me)
1.1       timbl     662: {
1.52      frystyk   663:     return me ? me->isIndex : NO;
1.27      frystyk   664: }
                    665: 
1.52      frystyk   666: /*     Content Base
1.51      frystyk   667: **     ------------
                    668: */
                    669: PUBLIC char * HTAnchor_base (HTParentAnchor * me)
                    670: {
1.63      frystyk   671:     if (me) {
                    672:        if (me->content_base) return me->content_base;
                    673:        if (me->headers) {
                    674:            char * base = HTAssocList_findObject(me->headers, "content-base");
                    675:            /*
                    676:            **  If no base is found then take the content-location if this
                    677:            **  is present and is absolute, else use the Request-URI.
                    678:            */
1.66      frystyk   679:            if (base) StrAllocCopy(me->content_base, HTStrip(base));
1.63      frystyk   680:        }
1.66      frystyk   681: 
                    682:        /*
                    683:        **  Try the content location if any
                    684:        */
                    685:        {
                    686:            char * location = HTAnchor_location(me);
                    687:            StrAllocCopy(me->content_base,
                    688:                         (location && HTURL_isAbsolute(location)) ?
                    689:                         location : me->address);
                    690:        }
                    691:        return me->content_base;
1.63      frystyk   692:     }
                    693:     return NULL;
1.51      frystyk   694: }
                    695: 
                    696: PUBLIC BOOL HTAnchor_setBase (HTParentAnchor * me, char * base)
                    697: {
1.63      frystyk   698:     if (me && base) {
                    699:        StrAllocCopy(me->content_base, base);
                    700:        return YES;
1.51      frystyk   701:     }
1.63      frystyk   702:     return NO;
1.51      frystyk   703: }
                    704: 
1.52      frystyk   705: /*     Content Location
                    706: **     ----------------
1.27      frystyk   707: */
1.52      frystyk   708: PUBLIC char * HTAnchor_location (HTParentAnchor * me)
1.27      frystyk   709: {
1.63      frystyk   710:     if (me) {
                    711:        if (me->content_location)
                    712:            return *me->content_location ? me->content_location : NULL;
                    713:        if (me->headers) {
                    714:            char * location = HTAssocList_findObject(me->headers, "content-location");
                    715:            StrAllocCopy(me->content_location, location ? HTStrip(location) : "");
                    716:            return me->content_location;
                    717:        }
                    718:     }
                    719:     return NULL;
1.27      frystyk   720: }
                    721: 
1.53      frystyk   722: /*
                    723: **     Expand the location relative to the base URL if any, otherwise the 
                    724: **     anchor address it self
                    725: */
1.52      frystyk   726: PUBLIC BOOL HTAnchor_setLocation (HTParentAnchor * me, char * location)
1.27      frystyk   727: {
1.53      frystyk   728:     if (me && location) {
1.63      frystyk   729:        char * base = HTAnchor_base(me);
                    730:        if (!base) base = me->address;
1.53      frystyk   731:        me->content_location = HTParse(location, base, PARSE_ALL);
                    732:        return YES;
1.52      frystyk   733:     }
1.53      frystyk   734:     return NO;
1.1       timbl     735: }
                    736: 
1.52      frystyk   737: /*     Content-Type
                    738: **     ------------
1.17      frystyk   739: */
1.35      frystyk   740: PUBLIC HTFormat HTAnchor_format (HTParentAnchor * me)
1.17      frystyk   741: {
                    742:     return me ? me->content_type : NULL;
                    743: }
1.1       timbl     744: 
1.35      frystyk   745: PUBLIC void HTAnchor_setFormat (HTParentAnchor * me, HTFormat form)
1.1       timbl     746: {
1.17      frystyk   747:     if (me) me->content_type = form;
1.1       timbl     748: }
                    749: 
1.52      frystyk   750: PUBLIC HTAssocList * HTAnchor_formatParam (HTParentAnchor * me)
                    751: {
                    752:     return me ? me->type_parameters : NULL;
                    753: }
                    754: 
                    755: PUBLIC BOOL HTAnchor_addFormatParam (HTParentAnchor * me,
                    756:                                     const char * name, const char * value)
                    757: {
                    758:     if (me) {
                    759:        if (!me->type_parameters) me->type_parameters = HTAssocList_new();
                    760:        return HTAssocList_replaceObject(me->type_parameters, name, value);
                    761:     }
                    762:     return NO;
                    763: }
                    764: 
1.17      frystyk   765: /*
                    766: **     Charset parameter to Content-Type
1.1       timbl     767: */
1.35      frystyk   768: PUBLIC HTCharset HTAnchor_charset (HTParentAnchor * me)
1.1       timbl     769: {
1.52      frystyk   770:     if (me && me->type_parameters) {
                    771:        char * charset = HTAssocList_findObject(me->type_parameters,"charset");
                    772:        return HTAtom_for(charset);
                    773:     }
                    774:     return NULL;
1.1       timbl     775: }
                    776: 
1.52      frystyk   777: PUBLIC BOOL HTAnchor_setCharset (HTParentAnchor * me, HTCharset charset)
1.1       timbl     778: {
1.52      frystyk   779:     return HTAnchor_addFormatParam(me, "charset", HTAtom_name(charset));
1.1       timbl     780: }
                    781: 
1.17      frystyk   782: /*
1.20      frystyk   783: **     Level parameter to Content-Type
                    784: */
1.35      frystyk   785: PUBLIC HTLevel HTAnchor_level (HTParentAnchor * me)
1.20      frystyk   786: {
1.52      frystyk   787:     if (me && me->type_parameters) {
                    788:        char * level = HTAssocList_findObject(me->type_parameters, "level");
                    789:        return HTAtom_for(level);
                    790:     }
                    791:     return NULL;
1.20      frystyk   792: }
                    793: 
1.52      frystyk   794: PUBLIC BOOL HTAnchor_setLevel (HTParentAnchor * me, HTLevel level)
1.20      frystyk   795: {
1.52      frystyk   796:     return HTAnchor_addFormatParam(me, "level", HTAtom_name(level));
1.20      frystyk   797: }
                    798: 
                    799: /*
1.17      frystyk   800: **     Content Encoding
                    801: */
1.47      frystyk   802: PUBLIC HTList * HTAnchor_encoding (HTParentAnchor * me)
1.1       timbl     803: {
1.17      frystyk   804:     return me ? me->content_encoding : NULL;
1.1       timbl     805: }
                    806: 
1.63      frystyk   807: PUBLIC BOOL HTAnchor_addEncoding (HTParentAnchor * me, HTEncoding encoding)
1.17      frystyk   808: {
1.47      frystyk   809:     if (me && encoding) {
                    810:        if (!me->content_encoding) me->content_encoding = HTList_new();
                    811:        return HTList_addObject(me->content_encoding, encoding);
                    812:     }
                    813:     return NO;
1.17      frystyk   814: }
                    815: 
1.67    ! frystyk   816: PUBLIC BOOL HTAnchor_removeEncoding (HTParentAnchor * me, HTEncoding encoding)
        !           817: {
        !           818:     return (me && me->content_encoding && encoding) ?
        !           819:        HTList_removeObject(me->content_encoding, encoding) : NO;
        !           820: }
        !           821: 
1.17      frystyk   822: /*
1.21      frystyk   823: **     Content Language
                    824: */
1.47      frystyk   825: PUBLIC HTList * HTAnchor_language (HTParentAnchor * me)
1.21      frystyk   826: {
1.63      frystyk   827:     if (me) {
                    828:        if (me->content_language == NULL && me->headers) {
                    829:            char * value = HTAssocList_findObject(me->headers, "content-language");
                    830:            char * field;
                    831:            if (!me->content_language) me->content_language = HTList_new();
                    832:            while ((field = HTNextField(&value)) != NULL) {
                    833:                char * lc = field;
                    834:                while ((*lc = TOLOWER(*lc))) lc++;
                    835:                HTList_addObject(me->content_language, HTAtom_for(field));
                    836:            }
                    837:        }
                    838:        return me->content_language;
                    839:     }
                    840:     return NULL;
1.21      frystyk   841: }
                    842: 
1.63      frystyk   843: PUBLIC BOOL HTAnchor_addLanguage (HTParentAnchor * me, HTLanguage language)
1.21      frystyk   844: {
1.47      frystyk   845:     if (me && language) {
                    846:        if (!me->content_language) me->content_language = HTList_new();
                    847:        return HTList_addObject(me->content_language, language);
                    848:     }
                    849:     return NO;
1.21      frystyk   850: }
                    851: 
                    852: /*
1.17      frystyk   853: **     Content Length
                    854: */
1.35      frystyk   855: PUBLIC long int HTAnchor_length (HTParentAnchor * me)
1.1       timbl     856: {
1.17      frystyk   857:     return me ? me->content_length : -1;
1.1       timbl     858: }
                    859: 
1.35      frystyk   860: PUBLIC void HTAnchor_setLength (HTParentAnchor * me, long int length)
1.17      frystyk   861: {
                    862:     if (me) me->content_length = length;
                    863: }
1.1       timbl     864: 
1.49      frystyk   865: PUBLIC void HTAnchor_addLength (HTParentAnchor * me, long int deltalength)
                    866: {
                    867:     if (me) {
                    868:        if (me->content_length < 0)
                    869:            me->content_length = deltalength;
                    870:        else
                    871:            me->content_length += deltalength;
                    872:     }
                    873: }
                    874: 
1.17      frystyk   875: /*
1.63      frystyk   876: **     Content Transfer Encoding
                    877: */
                    878: PUBLIC HTEncoding HTAnchor_transfer (HTParentAnchor * me)
                    879: {
                    880:     return me ? me->transfer : NULL;
                    881: }
                    882: 
                    883: PUBLIC void HTAnchor_setTransfer (HTParentAnchor * me, HTEncoding transfer)
                    884: {
                    885:     if (me) me->transfer = transfer;
                    886: }
                    887: 
                    888: /*
1.17      frystyk   889: **     Allowed methods (Allow)
1.1       timbl     890: */
1.63      frystyk   891: PUBLIC HTMethod HTAnchor_allow (HTParentAnchor * me)
1.17      frystyk   892: {
1.63      frystyk   893:     if (me) {
                    894:        if (me->allow == 0 && me->headers) {
                    895:            char * value = HTAssocList_findObject(me->headers, "allow");
                    896:            char * field;
                    897: 
                    898:            /*
                    899:            **  We treat methods allowed on this object as case insensitive
                    900:            **  in case we receive the information over the net - that is -
                    901:            **  in the Allow header.
                    902:            */
                    903:            while ((field = HTNextField(&value)) != NULL) {
                    904:                HTMethod new_method;
                    905:                if ((new_method = HTMethod_enum(field)) != METHOD_INVALID)
                    906:                    me->allow |= new_method;
                    907:            }
                    908:        }
                    909:        return me->allow;
                    910:     }  
                    911:     return METHOD_INVALID;
1.17      frystyk   912: }
1.1       timbl     913: 
1.63      frystyk   914: PUBLIC void HTAnchor_setAllow (HTParentAnchor * me, HTMethod methodset)
1.1       timbl     915: {
1.63      frystyk   916:     if (me) me->allow = methodset;
1.1       timbl     917: }
                    918: 
1.63      frystyk   919: PUBLIC void HTAnchor_appendAllow (HTParentAnchor * me, HTMethod methodset)
1.1       timbl     920: {
1.63      frystyk   921:     if (me) me->allow |= methodset;
1.1       timbl     922: }
                    923: 
1.17      frystyk   924: /*
                    925: **     Title
1.2       timbl     926: */
1.46      frystyk   927: PUBLIC const char * HTAnchor_title  (HTParentAnchor * me)
1.1       timbl     928: {
1.63      frystyk   929:     if (me) {
                    930:        if (me->title)
                    931:            return *me->title ? me->title : NULL;
                    932:        if (me->headers) {
                    933:            char * value = HTAssocList_findObject(me->headers, "title");
                    934:            char * title;
                    935:            if ((title = HTNextField(&value))) StrAllocCopy(me->title, title);
                    936:            return me->title;
                    937:        }
                    938:     }
                    939:     return NULL;
1.17      frystyk   940: }
1.1       timbl     941: 
1.46      frystyk   942: PUBLIC void HTAnchor_setTitle (HTParentAnchor * me, const char * title)
1.17      frystyk   943: {
                    944:     if (me && title) StrAllocCopy(me->title, title);
1.2       timbl     945: }
                    946: 
1.46      frystyk   947: PUBLIC void HTAnchor_appendTitle (HTParentAnchor * me, const char * title)
1.17      frystyk   948: {
                    949:     if (me && title) StrAllocCat(me->title, title);
                    950: }
1.2       timbl     951: 
1.17      frystyk   952: /*
                    953: **     Version
1.2       timbl     954: */
1.41      frystyk   955: PUBLIC char * HTAnchor_version (HTParentAnchor * me)
1.17      frystyk   956: {
1.63      frystyk   957:     if (me) {
                    958:        if (me->version)
                    959:            return *me->version ? me->version : NULL;
                    960:        if (me->headers) {
                    961:            char * value = HTAssocList_findObject(me->headers, "version");
                    962:            char * version;
                    963:            if ((version = HTNextField(&value)))
                    964:                StrAllocCopy(me->version, version);
                    965:            return me->version;
                    966:        }
                    967:     }
                    968:     return NULL;
1.17      frystyk   969: }
1.2       timbl     970: 
1.46      frystyk   971: PUBLIC void HTAnchor_setVersion (HTParentAnchor * me, const char * version)
1.2       timbl     972: {
1.17      frystyk   973:     if (me && version) StrAllocCopy(me->version, version);
1.2       timbl     974: }
                    975: 
1.17      frystyk   976: /*
                    977: **     Derived from
1.2       timbl     978: */
1.41      frystyk   979: PUBLIC char * HTAnchor_derived (HTParentAnchor * me)
1.17      frystyk   980: {
1.63      frystyk   981:     if (me) {
                    982:        if (me->derived_from)
                    983:            return *me->derived_from ? me->derived_from : NULL;
                    984:        if (me->headers) {
                    985:            char * value = HTAssocList_findObject(me->headers, "derived-from");
                    986:            char * derived_from;
                    987:            if ((derived_from = HTNextField(&value)))
                    988:                StrAllocCopy(me->derived_from, derived_from);
                    989:            return me->derived_from;
                    990:        }
                    991:     }
                    992:     return NULL;
1.17      frystyk   993: }
                    994: 
1.46      frystyk   995: PUBLIC void HTAnchor_setDerived (HTParentAnchor * me, const char *derived_from)
1.17      frystyk   996: {
                    997:     if (me && derived_from) StrAllocCopy(me->derived_from, derived_from);
                    998: }
1.2       timbl     999: 
1.17      frystyk  1000: /*
1.52      frystyk  1001: **     Content MD5
                   1002: */
                   1003: PUBLIC char * HTAnchor_md5 (HTParentAnchor * me)
                   1004: {
1.63      frystyk  1005:     if (me) {
                   1006:        if (me->content_md5)
                   1007:            return *me->content_md5 ? me->content_md5 : NULL;
                   1008:        if (me->headers) {
                   1009:            char * value = HTAssocList_findObject(me->headers, "content-md5");
                   1010:            char * md5;
                   1011:            if ((md5 = HTNextField(&value))) StrAllocCopy(me->content_md5,md5);
                   1012:            return me->content_md5;
                   1013:        }
                   1014:     }
                   1015:     return NULL;
1.52      frystyk  1016: }
                   1017: 
1.63      frystyk  1018: PUBLIC BOOL HTAnchor_setMd5 (HTParentAnchor * me, const char * hash)
1.52      frystyk  1019: {
1.63      frystyk  1020:     if (me && hash) {
                   1021:        StrAllocCopy(me->content_md5, hash);
                   1022:        return YES;
                   1023:     }
                   1024:     return NO;
1.52      frystyk  1025: }
                   1026: 
                   1027: /*
1.28      frystyk  1028: **     Date
                   1029: */
1.41      frystyk  1030: PUBLIC time_t HTAnchor_date (HTParentAnchor * me)
                   1031: {
1.63      frystyk  1032:     if (me) {
                   1033:        if (me->date == (time_t) -1 && me->headers) {
                   1034:            char * value = HTAssocList_findObject(me->headers, "date");
                   1035:            if (value) me->date = HTParseTime(value, NULL, YES);
                   1036:        }
                   1037:        return me->date;
                   1038:     }  
                   1039:     return (time_t) -1;
1.41      frystyk  1040: }
                   1041: 
1.46      frystyk  1042: PUBLIC void HTAnchor_setDate (HTParentAnchor * me, const time_t date)
1.28      frystyk  1043: {
1.41      frystyk  1044:     if (me) me->date = date;
1.28      frystyk  1045: }
                   1046: 
                   1047: /*
                   1048: **     Expires
                   1049: */
1.41      frystyk  1050: PUBLIC time_t HTAnchor_expires (HTParentAnchor * me)
                   1051: {
1.63      frystyk  1052:     if (me) {
                   1053:        if (me->expires == (time_t) -1 && me->headers) {
                   1054:            char * value = HTAssocList_findObject(me->headers, "expires");
                   1055:            if (value) me->expires = HTParseTime(value, NULL, YES);
                   1056:        }
                   1057:        return me->expires;
                   1058:     }  
                   1059:     return (time_t) -1;
1.41      frystyk  1060: }
                   1061: 
1.46      frystyk  1062: PUBLIC void HTAnchor_setExpires (HTParentAnchor * me, const time_t expires)
1.28      frystyk  1063: {
1.41      frystyk  1064:     if (me) me->expires = expires;
1.28      frystyk  1065: }
                   1066: 
                   1067: /*
                   1068: **     Last Modified
                   1069: */
1.41      frystyk  1070: PUBLIC time_t HTAnchor_lastModified (HTParentAnchor * me)
                   1071: {
1.63      frystyk  1072:     if (me) {
                   1073:        if (me->last_modified == (time_t) -1 && me->headers) {
                   1074:            char * value = HTAssocList_findObject(me->headers,"last-modified");
                   1075:            if (value) me->last_modified = HTParseTime(value, NULL, YES);
                   1076:        }
                   1077:        return me->last_modified;
                   1078:     }  
                   1079:     return (time_t) -1;
1.41      frystyk  1080: }
                   1081: 
1.46      frystyk  1082: PUBLIC void HTAnchor_setLastModified (HTParentAnchor * me, const time_t lm)
1.28      frystyk  1083: {
1.41      frystyk  1084:     if (me) me->last_modified = lm;
1.28      frystyk  1085: }
                   1086: 
                   1087: /*
1.59      frystyk  1088: **     Age
                   1089: */
                   1090: PUBLIC time_t HTAnchor_age (HTParentAnchor * me)
                   1091: {
1.63      frystyk  1092:     if (me) {
                   1093:        if (me->age == (time_t) -1 && me->headers) {
                   1094:            char * value = HTAssocList_findObject(me->headers, "age");
                   1095:            if (value) me->age = atol(value);
                   1096:        }
                   1097:        return me->age;
                   1098:     }  
                   1099:     return (time_t) -1;
1.59      frystyk  1100: }
                   1101: 
                   1102: PUBLIC void HTAnchor_setAge (HTParentAnchor * me, const time_t age)
                   1103: {
                   1104:     if (me) me->age = age;
                   1105: }
                   1106: 
                   1107: /*
1.52      frystyk  1108: **     Entity Tag
                   1109: */
                   1110: PUBLIC char * HTAnchor_etag (HTParentAnchor * me)
                   1111: {
1.63      frystyk  1112:     if (me) {
                   1113:        if (me->etag)
                   1114:            return *me->etag ? me->etag : NULL;
                   1115:        if (me->headers) {
                   1116:            char * value = HTAssocList_findObject(me->headers, "etag");
                   1117:            char * etag;
                   1118:            if ((etag = HTNextField(&value))) StrAllocCopy(me->etag, etag);
                   1119:            return me->etag;
                   1120:        }
                   1121:     }
                   1122: 
1.52      frystyk  1123:     return me ? me->etag : NULL;
                   1124: }
                   1125: 
                   1126: PUBLIC void HTAnchor_setEtag (HTParentAnchor * me, const char * etag)
                   1127: {
                   1128:     if (me && etag) StrAllocCopy(me->etag, etag);
                   1129: }
                   1130: 
                   1131: PUBLIC BOOL HTAnchor_isEtagWeak (HTParentAnchor * me)
                   1132: {
                   1133:     return (me && me->etag && !strncasecomp(me->etag, "W/", 2));
                   1134: }
                   1135: 
                   1136: /*
1.63      frystyk  1137: **     Original headers (if any)
1.17      frystyk  1138: */
1.63      frystyk  1139: PUBLIC HTAssocList * HTAnchor_header (HTParentAnchor * me)
1.2       timbl    1140: {
1.63      frystyk  1141:     return me ? me->headers : NULL;
1.2       timbl    1142: }
                   1143: 
1.63      frystyk  1144: PUBLIC BOOL HTAnchor_setHeader (HTParentAnchor * me, HTAssocList * headers)
1.2       timbl    1145: {
1.17      frystyk  1146:     if (me) {
1.63      frystyk  1147:        me->headers = headers;
                   1148:        return YES;
1.17      frystyk  1149:     }
1.63      frystyk  1150:     return NO;
1.2       timbl    1151: }
                   1152: 
1.23      frystyk  1153: /*
1.59      frystyk  1154: **  Validate anchor values and finish up parsing
1.2       timbl    1155: */
1.59      frystyk  1156: PUBLIC void HTAnchor_setHeaderParsed (HTParentAnchor * me)
1.2       timbl    1157: {
1.59      frystyk  1158:     if (me) {
                   1159: 
                   1160:        /*
                   1161:        **  If the server did not send a date then use the current time
                   1162:        */
                   1163:        if (me->date < 0) me->date = time(NULL);
                   1164: 
                   1165:        /*
                   1166:        **  If we don't get a Last-Modified header then set it to date
                   1167:        */
1.62      frystyk  1168:        if (ANCH_TRACE) HTTrace("HTAnchor.... Anchor is parsed\n");
1.59      frystyk  1169:        me->header_parsed = YES;
                   1170:     }
1.23      frystyk  1171: }
                   1172: 
1.59      frystyk  1173: PUBLIC BOOL HTAnchor_headerParsed (HTParentAnchor * me)
1.23      frystyk  1174: {
1.59      frystyk  1175:     return (me ? me->header_parsed : NO);
1.2       timbl    1176: }
                   1177: 
1.17      frystyk  1178: /*     Clear Header Information
                   1179: **     ------------------------
                   1180: */
1.35      frystyk  1181: PUBLIC void HTAnchor_clearHeader (HTParentAnchor * me)
1.2       timbl    1182: {
1.63      frystyk  1183:     if (ANCH_TRACE) HTTrace("HTAnchor.... Clear all header information\n");
                   1184:     me->allow = METHOD_INVALID;
1.48      frystyk  1185:     if (me->content_encoding) {
                   1186:        HTList_delete(me->content_encoding);
1.65      frystyk  1187:        me->content_encoding = NULL;
1.48      frystyk  1188:     }
1.17      frystyk  1189:     if (me->content_language) {
                   1190:        HTList_delete(me->content_language);
1.65      frystyk  1191:        me->content_language = NULL;
1.9       frystyk  1192:     }
1.51      frystyk  1193:     HT_FREE(me->content_base);
                   1194:     HT_FREE(me->content_location);
1.17      frystyk  1195:     me->content_length = -1;                                     /* Invalid */
1.57      frystyk  1196: 
1.59      frystyk  1197:     /* Delete the title */
                   1198:     HT_FREE(me->title);
                   1199: 
1.57      frystyk  1200:     /* Clear the content type */
1.17      frystyk  1201:     me->content_type = WWW_UNKNOWN;
1.52      frystyk  1202:     if (me->type_parameters) {
                   1203:        HTAssocList_delete(me->type_parameters);
                   1204:        me->type_parameters = NULL;
                   1205:     }    
1.57      frystyk  1206: 
                   1207:     /* Dates etc. */
1.28      frystyk  1208:     me->date = (time_t) -1;
                   1209:     me->expires = (time_t) -1;
                   1210:     me->last_modified = (time_t) -1;
1.59      frystyk  1211:     me->age = (time_t) -1;
1.17      frystyk  1212:     
1.43      frystyk  1213:     HT_FREE(me->derived_from);
                   1214:     HT_FREE(me->version);
1.58      frystyk  1215:     HT_FREE(me->etag);
1.63      frystyk  1216: 
                   1217:     /* Delete any original headers */
                   1218:     if (me->headers) HTAssocList_delete(me->headers);
                   1219:     me->headers = NULL;
1.18      frystyk  1220: 
1.62      frystyk  1221:     /* Anchor is cleared */
                   1222:     me->header_parsed = NO;
1.1       timbl    1223: }

Webmaster