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

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.81    ! frystyk     6: **     @(#) $Id: HTAnchor.c,v 1.80 1998/10/20 13:08:29 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.76      frystyk    20: #include "wwwsys.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.70      frystyk    28: #define HASH_SIZE      599        /* Arbitrary prime. Memory/speed tradeoff */
                     29: #define CHILD_HASH_SIZE         97            /* 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.75      frystyk   142:     char *tag = HTParse (address, "", PARSE_VIEW);             /* 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.71      frystyk   274: }
                    275: 
                    276: /*
                    277: **  Returns a link with a given link type or NULL if nothing found
                    278: */
                    279: PUBLIC HTLink * HTAnchor_findLinkType (HTAnchor * me, HTLinkType type)
                    280: {
                    281:     if (me) {
                    282:        HTLink * link = HTAnchor_mainLink(me);
                    283:        HTList * sublinks = HTAnchor_subLinks(me);
                    284:        if (link && HTLink_type(link) == type)
                    285:            return link;
                    286:        else if (sublinks) {
                    287:            while ((link = (HTLink *) HTList_nextObject (sublinks)))
                    288:                if (HTLink_type(link) == type) return link;
                    289:        }
                    290:     }
                    291:     return NULL;
1.17      frystyk   292: }
                    293: 
                    294: /* ------------------------------------------------------------------------- */
                    295: /*                             Deletion Methods                             */
                    296: /* ------------------------------------------------------------------------- */
1.1       timbl     297: 
                    298: /*     Delete an anchor and possibly related things (auto garbage collection)
                    299: **     --------------------------------------------
                    300: **
                    301: **     The anchor is only deleted if the corresponding document is not loaded.
1.10      frystyk   302: **     All outgoing links from parent and children are deleted, and this
                    303: **     anchor is removed from the sources list of all its targets.
1.1       timbl     304: **     We also try to delete the targets whose documents are not loaded.
                    305: **     If this anchor's source list is empty, we delete it and its children.
                    306: */
                    307: 
1.41      frystyk   308: /*     Deletes all the memory allocated in a parent anchor and returns any
                    309: **     hyperdoc object hanging of this anchor
1.19      frystyk   310: */
1.41      frystyk   311: PRIVATE void * delete_parent (HTParentAnchor * me)
1.19      frystyk   312: {
1.41      frystyk   313:     void * doc = me->document;
1.19      frystyk   314: 
                    315:     /* Remove link and address information */
1.26      frystyk   316:     if (me->links) {
                    317:        HTList *cur = me->links;
                    318:        HTLink *pres;
                    319:        while ((pres = (HTLink *) HTList_nextObject(cur)))
1.52      frystyk   320:            HTLink_delete(pres);
1.26      frystyk   321:        HTList_delete(me->links);
                    322:     }
1.54      frystyk   323: 
                    324:     /* Remove children */
                    325:     if (me->children) {
                    326:        int cnt = 0;
                    327:        for (; cnt<CHILD_HASH_SIZE; cnt++) {
                    328:            if (me->children[cnt]) HTList_delete(me->children[cnt]);
                    329:        }
                    330:        HT_FREE(me->children);
                    331:     }
                    332: 
1.19      frystyk   333:     HTList_delete (me->sources);
1.52      frystyk   334:     HTList_delete (me->variants);
1.43      frystyk   335:     HT_FREE(me->physical);
                    336:     HT_FREE(me->address);
1.19      frystyk   337: 
                    338:     /* Then remove entity header information (metainformation) */
1.59      frystyk   339:     HTAnchor_clearHeader(me);
1.57      frystyk   340: 
1.43      frystyk   341:     HT_FREE(me);
1.19      frystyk   342:     return doc;
                    343: }
                    344: 
                    345: 
1.41      frystyk   346: /*     Delete a parent anchor and all its children. If a hyperdoc object
                    347: **     is found hanging off the parent anchor then this is returned
1.19      frystyk   348: */
1.41      frystyk   349: PRIVATE void * delete_family (HTAnchor * me)
1.19      frystyk   350: {
1.64      frystyk   351:     HTParentAnchor * parent = NULL;
1.19      frystyk   352:     if (!me) {
1.64      frystyk   353:        if (ANCH_TRACE) HTTrace("AnchorDelete No anchor found\n");
1.19      frystyk   354:        return NULL;
                    355:     }
1.64      frystyk   356:     parent = me->parent;
                    357:     if (ANCH_TRACE)
                    358:        HTTrace("AnchorDelete Remove parent %p and children\n", parent);
1.19      frystyk   359: 
                    360:     /* Delete children */
                    361:     if (parent->children) {
1.54      frystyk   362:        int cnt = 0;
                    363:        for (; cnt<CHILD_HASH_SIZE; cnt++) {
                    364:            HTList * kids = parent->children[cnt];
                    365:            if (kids) {
                    366:                HTChildAnchor * child;
                    367:                while ((child=(HTChildAnchor*)HTList_removeLastObject(kids))) {
                    368:                    HT_FREE(child->tag);
                    369:                    if (child->links) {
                    370:                        HTList * cur = child->links;
                    371:                        HTLink * pres;
                    372:                        while ((pres = (HTLink *) HTList_nextObject(cur)))
                    373:                            HTLink_delete(pres);
                    374:                        HTList_delete(child->links);
                    375:                    }
                    376:                    HT_FREE(child);
                    377:                }
                    378:                HTList_delete(kids);
                    379:                parent->children[cnt] = NULL;
1.26      frystyk   380:            }
1.19      frystyk   381:        }
                    382:     }
                    383:     return delete_parent(parent);
                    384: }
                    385: 
                    386: 
                    387: /*     DELETE ALL ANCHORS
                    388: **     ------------------
                    389: **     Deletes all anchors and return a list of all the HyperDocs found.
                    390: **     It is for the application to delete any HyperDocs.
1.39      frystyk   391: **     If NULL then no hyperdocs are returned
1.19      frystyk   392: **     Return YES if OK, else NO
                    393: */
1.35      frystyk   394: PUBLIC BOOL HTAnchor_deleteAll (HTList * documents)
1.19      frystyk   395: {
                    396:     int cnt;
                    397:     HTList *cur;
1.39      frystyk   398:     if (!adult_table)
1.19      frystyk   399:        return NO;
                    400:     for (cnt=0; cnt<HASH_SIZE; cnt++) {
                    401:        if ((cur = adult_table[cnt])) { 
                    402:            HTParentAnchor *pres;
                    403:            while ((pres = (HTParentAnchor *) HTList_nextObject(cur)) != NULL){
1.41      frystyk   404:                void * doc = delete_family((HTAnchor *) pres);
                    405:                if (doc && documents) HTList_addObject(documents, doc);
1.19      frystyk   406:            }
                    407:        }
                    408:        HTList_delete(adult_table[cnt]);
                    409:     }
1.43      frystyk   410:     HT_FREE(adult_table);
1.19      frystyk   411:     return YES;
                    412: }
                    413: 
                    414: 
1.54      frystyk   415: PRIVATE void delete_links (HTAnchor * me)
1.1       timbl     416: {
1.3       timbl     417:   if (! me)
1.1       timbl     418:     return;
                    419: 
                    420:   /* Recursively try to delete target anchors */
1.3       timbl     421:   if (me->mainLink.dest) {
                    422:     HTParentAnchor *parent = me->mainLink.dest->parent;
                    423:     HTList_removeObject (parent->sources, me);
1.1       timbl     424:     if (! parent->document)  /* Test here to avoid calling overhead */
                    425:       HTAnchor_delete (parent);
                    426:   }
1.3       timbl     427:   if (me->links) {  /* Extra destinations */
1.1       timbl     428:     HTLink *target;
1.12      frystyk   429:     while ((target = (HTLink *) HTList_removeLastObject (me->links))) {
1.1       timbl     430:       HTParentAnchor *parent = target->dest->parent;
1.3       timbl     431:       HTList_removeObject (parent->sources, me);
1.1       timbl     432:       if (! parent->document)  /* Test here to avoid calling overhead */
                    433:        HTAnchor_delete (parent);
                    434:     }
                    435:   }
                    436: }
                    437: 
1.35      frystyk   438: PUBLIC BOOL HTAnchor_delete (HTParentAnchor * me)
1.1       timbl     439: {
1.54      frystyk   440:     /* Don't delete if document is loaded */
                    441:     if (!me || me->document) {
                    442:        if (ANCH_TRACE) HTTrace("Anchor...... Not deleted\n");
                    443:        return NO;
                    444:     }
                    445: 
                    446:     /* Recursively try to delete target anchors */
                    447:     delete_links ((HTAnchor *) me);
1.1       timbl     448: 
1.54      frystyk   449:     if (!HTList_isEmpty(me->sources)) {    /* There are still incoming links */
                    450: 
                    451:        /*
                    452:        ** Delete all outgoing links from children, if any
                    453:        */
                    454:        if (me->children) {
                    455:            int cnt = 0;
                    456:            for (; cnt<CHILD_HASH_SIZE; cnt++) {
                    457:                HTList * kids = me->children[cnt];
                    458:                if (kids) {
                    459:                    HTChildAnchor * child;
                    460:                    while ((child = (HTChildAnchor *) HTList_nextObject(kids)))
                    461:                        delete_links((HTAnchor *) child);
                    462:                    return NO;  /* Parent not deleted */
                    463:                }
                    464:            }
                    465:        }
1.1       timbl     466: 
1.54      frystyk   467:        /*
                    468:        ** No more incoming links : kill everything
                    469:        ** First, recursively delete children
                    470:        */
                    471:        if (me->children) {
                    472:            int cnt = 0;
                    473:            for (; cnt<CHILD_HASH_SIZE; cnt++) {
                    474:                HTList * kids = me->children[cnt];
                    475:                if (kids) {
                    476:                    HTChildAnchor * child;
                    477:                    while ((child=(HTChildAnchor *) HTList_removeLastObject(kids)))
                    478:                        delete_links((HTAnchor *) child);
                    479:                    HT_FREE(child->tag);
                    480:                    HT_FREE(child);
                    481:                }
                    482:            }
                    483:        }
                    484:     }
1.1       timbl     485: 
1.54      frystyk   486:     /* Now kill myself */
                    487:     delete_parent(me);
                    488:     return YES;  /* Parent deleted */
                    489: #if 0
1.3       timbl     490:   if (! HTList_isEmpty (me->sources)) {  /* There are still incoming links */
1.1       timbl     491:     /* Delete all outgoing links from children, if any */
1.3       timbl     492:     HTList *kids = me->children;
1.12      frystyk   493:     while ((child = (HTChildAnchor *) HTList_nextObject (kids)))
1.54      frystyk   494:       delete_links ((HTAnchor *) child);
1.1       timbl     495:     return NO;  /* Parent not deleted */
                    496:   }
                    497: 
                    498:   /* No more incoming links : kill everything */
                    499:   /* First, recursively delete children */
1.12      frystyk   500:   while ((child = (HTChildAnchor *) HTList_removeLastObject (me->children))) {
1.54      frystyk   501:     delete_links ((HTAnchor *) child);
1.43      frystyk   502:     HT_FREE(child->tag);
                    503:     HT_FREE(child);
1.1       timbl     504:   }
1.54      frystyk   505: #endif
1.69      frystyk   506: }
                    507: 
                    508: /*     FLATTEN ALL ANCHORS
                    509: **     -------------------
                    510: **     Flattens the anchor web structure into an array.
                    511: **     This is useful for calculating statistics, sorting
                    512: **     the parent anchors etc.
                    513: **
                    514: **      The caller can indicate the size of the array (total
                    515: **      number of anchors if known - otherwise 0).
                    516: **
                    517: **     Return an array that must be freed by the caller or
                    518: **      NULL if no anchors.
                    519: */
                    520: PUBLIC HTArray * HTAnchor_getArray (int growby)
                    521: {
                    522:     int cnt;
                    523:     HTArray * array = NULL;
                    524:     HTList * cur = NULL;
                    525:     if (!adult_table) return NULL;
                    526: 
                    527:     /* Allocate an array for the anchors */
                    528:     if (!growby) growby = HASH_SIZE;
                    529:     array = HTArray_new(growby);
                    530: 
                    531:     /* Traverse anchor structure */
                    532:     for (cnt=0; cnt<HASH_SIZE; cnt++) {
                    533:        if ((cur = adult_table[cnt])) { 
                    534:            HTParentAnchor * pres = NULL;
                    535:            while ((pres = (HTParentAnchor *) HTList_nextObject(cur)) != NULL) {
                    536:                 if (HTArray_addObject(array, pres) == NO) {
                    537:                     if (ANCH_TRACE)
                    538:                         HTTrace("Anchor...... Can't add object %p to array %p\n",
                    539:                                pres, array);
                    540:                     break;
                    541:                 }
                    542:            }
                    543:        }
                    544:     }
                    545:     return array;
1.1       timbl     546: }
                    547: 
1.17      frystyk   548: /* ------------------------------------------------------------------------- */
                    549: /*                             Data Access Methods                          */
                    550: /* ------------------------------------------------------------------------- */
1.1       timbl     551: 
1.35      frystyk   552: PUBLIC HTParentAnchor * HTAnchor_parent  (HTAnchor * me)
1.1       timbl     553: {
1.17      frystyk   554:     return me ? me->parent : NULL;
1.1       timbl     555: }
                    556: 
1.41      frystyk   557: PUBLIC void HTAnchor_setDocument  (HTParentAnchor * me, void * doc)
1.1       timbl     558: {
1.41      frystyk   559:     if (me) me->document = doc;
1.1       timbl     560: }
                    561: 
1.41      frystyk   562: PUBLIC void * HTAnchor_document  (HTParentAnchor * me)
1.1       timbl     563: {
1.17      frystyk   564:     return me ? me->document : NULL;
1.1       timbl     565: }
                    566: 
1.53      frystyk   567: PUBLIC char * HTAnchor_address  (HTAnchor * me) 
                    568: { 
                    569:     char *addr = NULL;
                    570:     if (me) {
                    571:         if (((HTParentAnchor *) me == me->parent) ||
                    572:             !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
                    573:             StrAllocCopy (addr, me->parent->address);
                    574:         }
                    575:         else {                  /* it's a named child */
                    576:             if ((addr = (char  *) HT_MALLOC(2 + strlen (me->parent->address) + \
                    577: strlen (((HTChildAnchor *) me)->tag))) == NULL)
                    578:                 HT_OUTOFMEM("HTAnchor_address");
                    579:             sprintf (addr, "%s#%s", me->parent->address,
                    580:                      ((HTChildAnchor *) me)->tag);
                    581:         }
                    582:     }
                    583:     return addr;
                    584: }
                    585: 
1.51      frystyk   586: /*
1.53      frystyk   587: **     We resolve the child address with respect to either a base URL,
                    588: **     a content-location, or to the request-URI
1.51      frystyk   589: */
1.53      frystyk   590: PUBLIC char * HTAnchor_expandedAddress  (HTAnchor * me)
1.1       timbl     591: {
1.17      frystyk   592:     char *addr = NULL;
                    593:     if (me) {
1.53      frystyk   594:        HTParentAnchor * parent = me->parent;
1.63      frystyk   595:        char * base = HTAnchor_base(parent);
1.17      frystyk   596:        if (((HTParentAnchor *) me == me->parent) ||
                    597:            !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
1.52      frystyk   598:            StrAllocCopy(addr, base);
1.51      frystyk   599:        } else {                        /* it's a named child */
1.52      frystyk   600:            if ((addr = (char *) HT_MALLOC(2 + strlen(base) + strlen(((HTChildAnchor *) me)->tag))) == NULL)
1.43      frystyk   601:                HT_OUTOFMEM("HTAnchor_address");
1.52      frystyk   602:            sprintf (addr, "%s#%s", base, ((HTChildAnchor *) me)->tag);
1.17      frystyk   603:        }
1.1       timbl     604:     }
1.17      frystyk   605:     return addr;
1.1       timbl     606: }
                    607: 
1.52      frystyk   608: /*     Physical Address
                    609: **     ----------------
                    610: */
                    611: PUBLIC char * HTAnchor_physical (HTParentAnchor * me)
                    612: {
1.53      frystyk   613:     return me ? me->physical ? me->physical : me->address : NULL;
1.52      frystyk   614: }
                    615: 
1.53      frystyk   616: PUBLIC void HTAnchor_setPhysical (HTParentAnchor * me, char * physical)
1.52      frystyk   617: {
                    618:     if (!me || !physical) {
                    619:        if (ANCH_TRACE)
                    620:            HTTrace("HTAnchor.... setPhysical, called with null argument\n");
                    621:        return;
                    622:     }
                    623:     StrAllocCopy(me->physical, physical);
                    624: }
                    625: 
1.53      frystyk   626: PUBLIC void HTAnchor_clearPhysical(HTParentAnchor * me)
                    627: {
                    628:     if (me) HT_FREE(me->physical);
                    629: }
                    630: 
                    631: /*
                    632: **     Children information
                    633: */
1.35      frystyk   634: PUBLIC BOOL HTAnchor_hasChildren  (HTParentAnchor * me)
1.17      frystyk   635: {
1.54      frystyk   636:     return (me && me->children);
1.74      frystyk   637: }
                    638: 
                    639: /*
                    640: ** Fix up a simple routine to see if this anchor is a (ChildAnchor *)
                    641: ** Seem to be doing it all over the place, so simplify!
                    642: */
                    643: PUBLIC BOOL HTAnchor_isChild (HTAnchor * me)
                    644: {
                    645:     return (me && (HTParentAnchor *) me != me->parent);
                    646: }
                    647: 
                    648: PUBLIC char * HTAnchor_view (HTAnchor * me)
                    649: {
                    650:     char * view = NULL;
                    651:     if (me && (HTParentAnchor *) me != me->parent && ((HTChildAnchor *) me)->tag)
                    652:        StrAllocCopy(view, ((HTChildAnchor *) me)->tag);
                    653:     return view;
1.17      frystyk   654: }
1.1       timbl     655: 
1.63      frystyk   656: /* ------------------------------------------------------------------------- */
                    657: /*                           Entity Header Information                      */
                    658: /* ------------------------------------------------------------------------- */
                    659: 
1.59      frystyk   660: /*
1.63      frystyk   661: **  Take the relevant infomration from the response object and cache it
                    662: **  in the anchor object. We inherit the information that is already
                    663: **  parsed in the response along with the unparsed headers.
1.52      frystyk   664: */
1.63      frystyk   665: PUBLIC BOOL HTAnchor_update (HTParentAnchor * me, HTResponse * response)
1.52      frystyk   666: {
1.63      frystyk   667:     if (me && response) {
1.59      frystyk   668: 
1.79      frystyk   669:        if (HTResponse_isCachable(response) == HT_CACHE_ETAG) {
                    670:            char * etag = HTResponse_etag(response);
                    671:            if (ANCH_TRACE) HTTrace("HTAnchor.... Updating etag for %p\n", me);
                    672:            if (etag) {
                    673:                HTAnchor_setEtag(me, etag);
                    674:                return YES;
                    675:            }
                    676:        } else if (HTResponse_isCachable(response) == HT_CACHE_ALL) {
1.81    ! frystyk   677:            char * etag = HTResponse_etag(response);
1.79      frystyk   678:            if (ANCH_TRACE)
                    679:                HTTrace("HTAnchor.... Updating metainformation for %p\n", me);
                    680: 
                    681:            /*
                    682:            **  The content length and type is already parsed at this point
                    683:            **  in time. We also check for format parameters like charset etc.
                    684:            **  and copy the contents in the anchor object
                    685:            */
                    686:            me->content_length = HTResponse_length(response);
                    687:            me->content_type = HTResponse_format(response);
                    688:            me->type_parameters = HTResponse_formatParam(response);
1.80      frystyk   689:            me->content_encoding = HTResponse_encoding(response);
1.63      frystyk   690:        
1.81    ! frystyk   691:             /* Don't forget the etag as well */
        !           692:                    if (etag) HTAnchor_setEtag(me, etag);
        !           693: 
1.79      frystyk   694:            /*
                    695:            **  Inherit all the unparsed headers - we may need them later!
                    696:            */
                    697:            me->headers = HTResponse_handOverHeader(response);
                    698: 
                    699:            /*
                    700:            **  Notifify the response object not to delete the lists that we
                    701:            **  have inherited in the anchor object
                    702:            */
                    703:            HTResponse_isCached(response, YES);
1.59      frystyk   704: 
1.79      frystyk   705:            return YES;
                    706:        }
1.59      frystyk   707:     }
                    708:     return NO;
1.52      frystyk   709: }
                    710: 
                    711: /*
                    712: **     Variants. If this anchor has any variants then keep them in a list
                    713: **     so that we can find them later. The list is simply a list of 
                    714: **     parent anchors.
                    715: */
                    716: PUBLIC HTList * HTAnchor_variants (HTParentAnchor * me)
1.17      frystyk   717: {
1.52      frystyk   718:     return me ? me->variants : NULL;
1.17      frystyk   719: }
1.1       timbl     720: 
1.52      frystyk   721: PUBLIC BOOL HTAnchor_addVariant (HTParentAnchor * me,
                    722:                                 HTParentAnchor * variant)
1.1       timbl     723: {
1.52      frystyk   724:     if (me && variant) {
                    725:        if (!me->variants) me->variants = HTList_new();
                    726:        return HTList_addObject(me->variants, variant);
                    727:     }
                    728:     return NO;
1.17      frystyk   729: }
                    730: 
1.52      frystyk   731: PUBLIC BOOL HTAnchor_deleteVariant (HTParentAnchor * me,
                    732:                                    HTParentAnchor * variant)
1.17      frystyk   733: {
1.52      frystyk   734:     return (me && variant) ? HTList_removeObject(me->variants, variant) : NO;
1.9       frystyk   735: }
1.1       timbl     736: 
1.52      frystyk   737: /*
                    738: **     Is this resource an index?
1.17      frystyk   739: */
1.52      frystyk   740: PUBLIC void HTAnchor_clearIndex  (HTParentAnchor * me)
                    741: {
                    742:     if (me) me->isIndex = NO;
                    743: }
1.17      frystyk   744: 
1.52      frystyk   745: PUBLIC void HTAnchor_setIndex  (HTParentAnchor * me)
1.1       timbl     746: {
1.52      frystyk   747:   if (me) me->isIndex = YES;
1.1       timbl     748: }
                    749: 
1.52      frystyk   750: PUBLIC BOOL HTAnchor_isIndex  (HTParentAnchor * me)
1.1       timbl     751: {
1.52      frystyk   752:     return me ? me->isIndex : NO;
1.27      frystyk   753: }
                    754: 
1.52      frystyk   755: /*     Content Base
1.51      frystyk   756: **     ------------
                    757: */
                    758: PUBLIC char * HTAnchor_base (HTParentAnchor * me)
                    759: {
1.63      frystyk   760:     if (me) {
                    761:        if (me->content_base) return me->content_base;
                    762:        if (me->headers) {
                    763:            char * base = HTAssocList_findObject(me->headers, "content-base");
                    764:            /*
                    765:            **  If no base is found then take the content-location if this
                    766:            **  is present and is absolute, else use the Request-URI.
                    767:            */
1.66      frystyk   768:            if (base) StrAllocCopy(me->content_base, HTStrip(base));
1.63      frystyk   769:        }
1.66      frystyk   770: 
                    771:        /*
                    772:        **  Try the content location if any
                    773:        */
                    774:        {
                    775:            char * location = HTAnchor_location(me);
                    776:            StrAllocCopy(me->content_base,
                    777:                         (location && HTURL_isAbsolute(location)) ?
                    778:                         location : me->address);
                    779:        }
                    780:        return me->content_base;
1.63      frystyk   781:     }
                    782:     return NULL;
1.51      frystyk   783: }
                    784: 
                    785: PUBLIC BOOL HTAnchor_setBase (HTParentAnchor * me, char * base)
                    786: {
1.63      frystyk   787:     if (me && base) {
                    788:        StrAllocCopy(me->content_base, base);
                    789:        return YES;
1.51      frystyk   790:     }
1.63      frystyk   791:     return NO;
1.51      frystyk   792: }
                    793: 
1.52      frystyk   794: /*     Content Location
                    795: **     ----------------
1.27      frystyk   796: */
1.52      frystyk   797: PUBLIC char * HTAnchor_location (HTParentAnchor * me)
1.27      frystyk   798: {
1.63      frystyk   799:     if (me) {
                    800:        if (me->content_location)
                    801:            return *me->content_location ? me->content_location : NULL;
                    802:        if (me->headers) {
                    803:            char * location = HTAssocList_findObject(me->headers, "content-location");
                    804:            StrAllocCopy(me->content_location, location ? HTStrip(location) : "");
                    805:            return me->content_location;
                    806:        }
                    807:     }
                    808:     return NULL;
1.27      frystyk   809: }
                    810: 
1.53      frystyk   811: /*
                    812: **     Expand the location relative to the base URL if any, otherwise the 
                    813: **     anchor address it self
                    814: */
1.52      frystyk   815: PUBLIC BOOL HTAnchor_setLocation (HTParentAnchor * me, char * location)
1.27      frystyk   816: {
1.53      frystyk   817:     if (me && location) {
1.63      frystyk   818:        char * base = HTAnchor_base(me);
                    819:        if (!base) base = me->address;
1.53      frystyk   820:        me->content_location = HTParse(location, base, PARSE_ALL);
                    821:        return YES;
1.52      frystyk   822:     }
1.53      frystyk   823:     return NO;
1.1       timbl     824: }
                    825: 
1.72      frystyk   826: /*     Meta tags
                    827: **     ---------
                    828: */
                    829: PUBLIC HTAssocList * HTAnchor_meta (HTParentAnchor * me)
                    830: {
                    831:     return me ? me->meta_tags : NULL;
                    832: }
                    833: 
                    834: PUBLIC BOOL HTAnchor_addMeta (HTParentAnchor * me,
                    835:                              const char * name, const char * value)
                    836: {
                    837:     if (me) {
                    838:        if (!me->meta_tags) me->meta_tags = HTAssocList_new();
                    839:        return HTAssocList_replaceObject(me->meta_tags, name, value);
                    840:     }
                    841:     return NO;
                    842: }
                    843: 
                    844: /*
                    845: **     robots meta tag
                    846: */
                    847: PUBLIC char * HTAnchor_robots (HTParentAnchor * me)
                    848: {
                    849:     if (me && me->meta_tags) {
                    850:        char * robots = HTAssocList_findObject(me->meta_tags, "robots");
                    851:        return robots;
                    852:     }
                    853:     return NULL;
                    854: }
                    855: 
1.52      frystyk   856: /*     Content-Type
                    857: **     ------------
1.17      frystyk   858: */
1.35      frystyk   859: PUBLIC HTFormat HTAnchor_format (HTParentAnchor * me)
1.17      frystyk   860: {
                    861:     return me ? me->content_type : NULL;
                    862: }
1.1       timbl     863: 
1.35      frystyk   864: PUBLIC void HTAnchor_setFormat (HTParentAnchor * me, HTFormat form)
1.1       timbl     865: {
1.17      frystyk   866:     if (me) me->content_type = form;
1.1       timbl     867: }
                    868: 
1.52      frystyk   869: PUBLIC HTAssocList * HTAnchor_formatParam (HTParentAnchor * me)
                    870: {
                    871:     return me ? me->type_parameters : NULL;
                    872: }
                    873: 
                    874: PUBLIC BOOL HTAnchor_addFormatParam (HTParentAnchor * me,
                    875:                                     const char * name, const char * value)
                    876: {
                    877:     if (me) {
                    878:        if (!me->type_parameters) me->type_parameters = HTAssocList_new();
                    879:        return HTAssocList_replaceObject(me->type_parameters, name, value);
                    880:     }
                    881:     return NO;
                    882: }
                    883: 
1.17      frystyk   884: /*
                    885: **     Charset parameter to Content-Type
1.1       timbl     886: */
1.35      frystyk   887: PUBLIC HTCharset HTAnchor_charset (HTParentAnchor * me)
1.1       timbl     888: {
1.52      frystyk   889:     if (me && me->type_parameters) {
                    890:        char * charset = HTAssocList_findObject(me->type_parameters,"charset");
                    891:        return HTAtom_for(charset);
                    892:     }
                    893:     return NULL;
1.1       timbl     894: }
                    895: 
1.52      frystyk   896: PUBLIC BOOL HTAnchor_setCharset (HTParentAnchor * me, HTCharset charset)
1.1       timbl     897: {
1.52      frystyk   898:     return HTAnchor_addFormatParam(me, "charset", HTAtom_name(charset));
1.1       timbl     899: }
                    900: 
1.17      frystyk   901: /*
1.20      frystyk   902: **     Level parameter to Content-Type
                    903: */
1.35      frystyk   904: PUBLIC HTLevel HTAnchor_level (HTParentAnchor * me)
1.20      frystyk   905: {
1.52      frystyk   906:     if (me && me->type_parameters) {
                    907:        char * level = HTAssocList_findObject(me->type_parameters, "level");
                    908:        return HTAtom_for(level);
                    909:     }
                    910:     return NULL;
1.20      frystyk   911: }
                    912: 
1.52      frystyk   913: PUBLIC BOOL HTAnchor_setLevel (HTParentAnchor * me, HTLevel level)
1.20      frystyk   914: {
1.52      frystyk   915:     return HTAnchor_addFormatParam(me, "level", HTAtom_name(level));
1.20      frystyk   916: }
                    917: 
                    918: /*
1.17      frystyk   919: **     Content Encoding
                    920: */
1.47      frystyk   921: PUBLIC HTList * HTAnchor_encoding (HTParentAnchor * me)
1.1       timbl     922: {
1.17      frystyk   923:     return me ? me->content_encoding : NULL;
1.1       timbl     924: }
                    925: 
1.63      frystyk   926: PUBLIC BOOL HTAnchor_addEncoding (HTParentAnchor * me, HTEncoding encoding)
1.17      frystyk   927: {
1.47      frystyk   928:     if (me && encoding) {
                    929:        if (!me->content_encoding) me->content_encoding = HTList_new();
                    930:        return HTList_addObject(me->content_encoding, encoding);
                    931:     }
                    932:     return NO;
1.17      frystyk   933: }
                    934: 
1.77      frystyk   935: PUBLIC BOOL HTAnchor_deleteEncoding (HTParentAnchor * me, HTEncoding encoding)
1.67      frystyk   936: {
                    937:     return (me && me->content_encoding && encoding) ?
                    938:        HTList_removeObject(me->content_encoding, encoding) : NO;
                    939: }
                    940: 
1.77      frystyk   941: PUBLIC BOOL HTAnchor_deleteEncodingAll (HTParentAnchor * me)
                    942: {
                    943:     if (me && me->content_encoding) {
                    944:        HTList_delete(me->content_encoding);
                    945:        me->content_encoding = NULL;
                    946:        return YES;
                    947:     }
                    948:     return NO;
                    949: }
                    950: 
1.17      frystyk   951: /*
1.21      frystyk   952: **     Content Language
                    953: */
1.47      frystyk   954: PUBLIC HTList * HTAnchor_language (HTParentAnchor * me)
1.21      frystyk   955: {
1.63      frystyk   956:     if (me) {
                    957:        if (me->content_language == NULL && me->headers) {
                    958:            char * value = HTAssocList_findObject(me->headers, "content-language");
                    959:            char * field;
                    960:            if (!me->content_language) me->content_language = HTList_new();
                    961:            while ((field = HTNextField(&value)) != NULL) {
                    962:                char * lc = field;
                    963:                while ((*lc = TOLOWER(*lc))) lc++;
                    964:                HTList_addObject(me->content_language, HTAtom_for(field));
                    965:            }
                    966:        }
                    967:        return me->content_language;
                    968:     }
                    969:     return NULL;
1.21      frystyk   970: }
                    971: 
1.63      frystyk   972: PUBLIC BOOL HTAnchor_addLanguage (HTParentAnchor * me, HTLanguage language)
1.21      frystyk   973: {
1.47      frystyk   974:     if (me && language) {
                    975:        if (!me->content_language) me->content_language = HTList_new();
                    976:        return HTList_addObject(me->content_language, language);
1.77      frystyk   977:     }
                    978:     return NO;
                    979: }
                    980: 
                    981: PUBLIC BOOL HTAnchor_deleteLanguageAll (HTParentAnchor * me)
                    982: {
                    983:     if (me && me->content_language) {
                    984:        HTList_delete(me->content_language);
                    985:        me->content_language = NULL;
                    986:        return YES;
1.47      frystyk   987:     }
                    988:     return NO;
1.21      frystyk   989: }
                    990: 
                    991: /*
1.17      frystyk   992: **     Content Length
                    993: */
1.35      frystyk   994: PUBLIC long int HTAnchor_length (HTParentAnchor * me)
1.1       timbl     995: {
1.17      frystyk   996:     return me ? me->content_length : -1;
1.1       timbl     997: }
                    998: 
1.35      frystyk   999: PUBLIC void HTAnchor_setLength (HTParentAnchor * me, long int length)
1.17      frystyk  1000: {
                   1001:     if (me) me->content_length = length;
                   1002: }
1.1       timbl    1003: 
1.49      frystyk  1004: PUBLIC void HTAnchor_addLength (HTParentAnchor * me, long int deltalength)
                   1005: {
                   1006:     if (me) {
                   1007:        if (me->content_length < 0)
                   1008:            me->content_length = deltalength;
                   1009:        else
                   1010:            me->content_length += deltalength;
                   1011:     }
                   1012: }
                   1013: 
1.17      frystyk  1014: /*
1.63      frystyk  1015: **     Content Transfer Encoding
                   1016: */
1.73      frystyk  1017: PUBLIC HTEncoding HTAnchor_contentTransferEncoding (HTParentAnchor * me)
1.63      frystyk  1018: {
1.73      frystyk  1019:     return me ? me->cte : NULL;
1.63      frystyk  1020: }
                   1021: 
1.73      frystyk  1022: PUBLIC void HTAnchor_setContentTransferEncoding (HTParentAnchor * me, HTEncoding cte)
1.63      frystyk  1023: {
1.73      frystyk  1024:     if (me) me->cte = cte;
1.63      frystyk  1025: }
                   1026: 
                   1027: /*
1.17      frystyk  1028: **     Allowed methods (Allow)
1.1       timbl    1029: */
1.63      frystyk  1030: PUBLIC HTMethod HTAnchor_allow (HTParentAnchor * me)
1.17      frystyk  1031: {
1.63      frystyk  1032:     if (me) {
                   1033:        if (me->allow == 0 && me->headers) {
                   1034:            char * value = HTAssocList_findObject(me->headers, "allow");
                   1035:            char * field;
                   1036: 
                   1037:            /*
                   1038:            **  We treat methods allowed on this object as case insensitive
                   1039:            **  in case we receive the information over the net - that is -
                   1040:            **  in the Allow header.
                   1041:            */
                   1042:            while ((field = HTNextField(&value)) != NULL) {
                   1043:                HTMethod new_method;
                   1044:                if ((new_method = HTMethod_enum(field)) != METHOD_INVALID)
                   1045:                    me->allow |= new_method;
                   1046:            }
                   1047:        }
                   1048:        return me->allow;
                   1049:     }  
                   1050:     return METHOD_INVALID;
1.17      frystyk  1051: }
1.1       timbl    1052: 
1.63      frystyk  1053: PUBLIC void HTAnchor_setAllow (HTParentAnchor * me, HTMethod methodset)
1.1       timbl    1054: {
1.63      frystyk  1055:     if (me) me->allow = methodset;
1.1       timbl    1056: }
                   1057: 
1.63      frystyk  1058: PUBLIC void HTAnchor_appendAllow (HTParentAnchor * me, HTMethod methodset)
1.1       timbl    1059: {
1.63      frystyk  1060:     if (me) me->allow |= methodset;
1.1       timbl    1061: }
                   1062: 
1.17      frystyk  1063: /*
                   1064: **     Title
1.2       timbl    1065: */
1.46      frystyk  1066: PUBLIC const char * HTAnchor_title  (HTParentAnchor * me)
1.1       timbl    1067: {
1.63      frystyk  1068:     if (me) {
                   1069:        if (me->title)
                   1070:            return *me->title ? me->title : NULL;
                   1071:        if (me->headers) {
                   1072:            char * value = HTAssocList_findObject(me->headers, "title");
                   1073:            char * title;
                   1074:            if ((title = HTNextField(&value))) StrAllocCopy(me->title, title);
                   1075:            return me->title;
                   1076:        }
                   1077:     }
                   1078:     return NULL;
1.17      frystyk  1079: }
1.1       timbl    1080: 
1.46      frystyk  1081: PUBLIC void HTAnchor_setTitle (HTParentAnchor * me, const char * title)
1.17      frystyk  1082: {
1.76      frystyk  1083:     if (me && title) {
                   1084:        char * ptr;
                   1085:        StrAllocCopy(me->title, title);
                   1086:        ptr = me->title;
                   1087:        while (*ptr) {
                   1088:            if (isspace((int) *ptr)) *ptr = ' ';                
                   1089:            ptr++;
                   1090:        }
                   1091:     }
1.2       timbl    1092: }
                   1093: 
1.46      frystyk  1094: PUBLIC void HTAnchor_appendTitle (HTParentAnchor * me, const char * title)
1.17      frystyk  1095: {
                   1096:     if (me && title) StrAllocCat(me->title, title);
                   1097: }
1.2       timbl    1098: 
1.17      frystyk  1099: /*
                   1100: **     Version
1.2       timbl    1101: */
1.41      frystyk  1102: PUBLIC char * HTAnchor_version (HTParentAnchor * me)
1.17      frystyk  1103: {
1.63      frystyk  1104:     if (me) {
                   1105:        if (me->version)
                   1106:            return *me->version ? me->version : NULL;
                   1107:        if (me->headers) {
                   1108:            char * value = HTAssocList_findObject(me->headers, "version");
                   1109:            char * version;
                   1110:            if ((version = HTNextField(&value)))
                   1111:                StrAllocCopy(me->version, version);
                   1112:            return me->version;
                   1113:        }
                   1114:     }
                   1115:     return NULL;
1.17      frystyk  1116: }
1.2       timbl    1117: 
1.46      frystyk  1118: PUBLIC void HTAnchor_setVersion (HTParentAnchor * me, const char * version)
1.2       timbl    1119: {
1.17      frystyk  1120:     if (me && version) StrAllocCopy(me->version, version);
1.2       timbl    1121: }
                   1122: 
1.17      frystyk  1123: /*
                   1124: **     Derived from
1.2       timbl    1125: */
1.41      frystyk  1126: PUBLIC char * HTAnchor_derived (HTParentAnchor * me)
1.17      frystyk  1127: {
1.63      frystyk  1128:     if (me) {
                   1129:        if (me->derived_from)
                   1130:            return *me->derived_from ? me->derived_from : NULL;
                   1131:        if (me->headers) {
                   1132:            char * value = HTAssocList_findObject(me->headers, "derived-from");
                   1133:            char * derived_from;
                   1134:            if ((derived_from = HTNextField(&value)))
                   1135:                StrAllocCopy(me->derived_from, derived_from);
                   1136:            return me->derived_from;
                   1137:        }
                   1138:     }
                   1139:     return NULL;
1.17      frystyk  1140: }
                   1141: 
1.46      frystyk  1142: PUBLIC void HTAnchor_setDerived (HTParentAnchor * me, const char *derived_from)
1.17      frystyk  1143: {
                   1144:     if (me && derived_from) StrAllocCopy(me->derived_from, derived_from);
                   1145: }
1.2       timbl    1146: 
1.17      frystyk  1147: /*
1.52      frystyk  1148: **     Content MD5
                   1149: */
                   1150: PUBLIC char * HTAnchor_md5 (HTParentAnchor * me)
                   1151: {
1.63      frystyk  1152:     if (me) {
                   1153:        if (me->content_md5)
                   1154:            return *me->content_md5 ? me->content_md5 : NULL;
                   1155:        if (me->headers) {
                   1156:            char * value = HTAssocList_findObject(me->headers, "content-md5");
                   1157:            char * md5;
                   1158:            if ((md5 = HTNextField(&value))) StrAllocCopy(me->content_md5,md5);
                   1159:            return me->content_md5;
                   1160:        }
                   1161:     }
                   1162:     return NULL;
1.52      frystyk  1163: }
                   1164: 
1.63      frystyk  1165: PUBLIC BOOL HTAnchor_setMd5 (HTParentAnchor * me, const char * hash)
1.52      frystyk  1166: {
1.63      frystyk  1167:     if (me && hash) {
                   1168:        StrAllocCopy(me->content_md5, hash);
                   1169:        return YES;
                   1170:     }
                   1171:     return NO;
1.52      frystyk  1172: }
                   1173: 
                   1174: /*
1.28      frystyk  1175: **     Date
                   1176: */
1.41      frystyk  1177: PUBLIC time_t HTAnchor_date (HTParentAnchor * me)
                   1178: {
1.63      frystyk  1179:     if (me) {
                   1180:        if (me->date == (time_t) -1 && me->headers) {
                   1181:            char * value = HTAssocList_findObject(me->headers, "date");
                   1182:            if (value) me->date = HTParseTime(value, NULL, YES);
                   1183:        }
                   1184:        return me->date;
                   1185:     }  
                   1186:     return (time_t) -1;
1.41      frystyk  1187: }
                   1188: 
1.46      frystyk  1189: PUBLIC void HTAnchor_setDate (HTParentAnchor * me, const time_t date)
1.28      frystyk  1190: {
1.41      frystyk  1191:     if (me) me->date = date;
1.28      frystyk  1192: }
                   1193: 
                   1194: /*
                   1195: **     Expires
                   1196: */
1.41      frystyk  1197: PUBLIC time_t HTAnchor_expires (HTParentAnchor * me)
                   1198: {
1.63      frystyk  1199:     if (me) {
                   1200:        if (me->expires == (time_t) -1 && me->headers) {
                   1201:            char * value = HTAssocList_findObject(me->headers, "expires");
                   1202:            if (value) me->expires = HTParseTime(value, NULL, YES);
                   1203:        }
                   1204:        return me->expires;
                   1205:     }  
                   1206:     return (time_t) -1;
1.41      frystyk  1207: }
                   1208: 
1.46      frystyk  1209: PUBLIC void HTAnchor_setExpires (HTParentAnchor * me, const time_t expires)
1.28      frystyk  1210: {
1.41      frystyk  1211:     if (me) me->expires = expires;
1.28      frystyk  1212: }
                   1213: 
                   1214: /*
                   1215: **     Last Modified
                   1216: */
1.41      frystyk  1217: PUBLIC time_t HTAnchor_lastModified (HTParentAnchor * me)
                   1218: {
1.63      frystyk  1219:     if (me) {
                   1220:        if (me->last_modified == (time_t) -1 && me->headers) {
                   1221:            char * value = HTAssocList_findObject(me->headers,"last-modified");
                   1222:            if (value) me->last_modified = HTParseTime(value, NULL, YES);
                   1223:        }
                   1224:        return me->last_modified;
                   1225:     }  
                   1226:     return (time_t) -1;
1.41      frystyk  1227: }
                   1228: 
1.46      frystyk  1229: PUBLIC void HTAnchor_setLastModified (HTParentAnchor * me, const time_t lm)
1.28      frystyk  1230: {
1.41      frystyk  1231:     if (me) me->last_modified = lm;
1.28      frystyk  1232: }
                   1233: 
                   1234: /*
1.59      frystyk  1235: **     Age
                   1236: */
                   1237: PUBLIC time_t HTAnchor_age (HTParentAnchor * me)
                   1238: {
1.63      frystyk  1239:     if (me) {
                   1240:        if (me->age == (time_t) -1 && me->headers) {
                   1241:            char * value = HTAssocList_findObject(me->headers, "age");
                   1242:            if (value) me->age = atol(value);
                   1243:        }
                   1244:        return me->age;
                   1245:     }  
                   1246:     return (time_t) -1;
1.59      frystyk  1247: }
                   1248: 
                   1249: PUBLIC void HTAnchor_setAge (HTParentAnchor * me, const time_t age)
                   1250: {
                   1251:     if (me) me->age = age;
                   1252: }
                   1253: 
                   1254: /*
1.52      frystyk  1255: **     Entity Tag
                   1256: */
                   1257: PUBLIC char * HTAnchor_etag (HTParentAnchor * me)
                   1258: {
1.63      frystyk  1259:     if (me) {
                   1260:        if (me->etag)
                   1261:            return *me->etag ? me->etag : NULL;
                   1262:        if (me->headers) {
                   1263:            char * value = HTAssocList_findObject(me->headers, "etag");
                   1264:            char * etag;
                   1265:            if ((etag = HTNextField(&value))) StrAllocCopy(me->etag, etag);
                   1266:            return me->etag;
                   1267:        }
                   1268:     }
                   1269: 
1.52      frystyk  1270:     return me ? me->etag : NULL;
                   1271: }
                   1272: 
                   1273: PUBLIC void HTAnchor_setEtag (HTParentAnchor * me, const char * etag)
                   1274: {
1.78      frystyk  1275:     if (me && etag && me->etag != etag) StrAllocCopy(me->etag, etag);
1.52      frystyk  1276: }
                   1277: 
                   1278: PUBLIC BOOL HTAnchor_isEtagWeak (HTParentAnchor * me)
                   1279: {
                   1280:     return (me && me->etag && !strncasecomp(me->etag, "W/", 2));
                   1281: }
                   1282: 
                   1283: /*
1.63      frystyk  1284: **     Original headers (if any)
1.17      frystyk  1285: */
1.63      frystyk  1286: PUBLIC HTAssocList * HTAnchor_header (HTParentAnchor * me)
1.2       timbl    1287: {
1.63      frystyk  1288:     return me ? me->headers : NULL;
1.2       timbl    1289: }
                   1290: 
1.63      frystyk  1291: PUBLIC BOOL HTAnchor_setHeader (HTParentAnchor * me, HTAssocList * headers)
1.2       timbl    1292: {
1.17      frystyk  1293:     if (me) {
1.63      frystyk  1294:        me->headers = headers;
                   1295:        return YES;
1.17      frystyk  1296:     }
1.63      frystyk  1297:     return NO;
1.2       timbl    1298: }
                   1299: 
1.23      frystyk  1300: /*
1.59      frystyk  1301: **  Validate anchor values and finish up parsing
1.2       timbl    1302: */
1.59      frystyk  1303: PUBLIC void HTAnchor_setHeaderParsed (HTParentAnchor * me)
1.2       timbl    1304: {
1.59      frystyk  1305:     if (me) {
                   1306: 
                   1307:        /*
                   1308:        **  If the server did not send a date then use the current time
                   1309:        */
                   1310:        if (me->date < 0) me->date = time(NULL);
                   1311: 
                   1312:        /*
                   1313:        **  If we don't get a Last-Modified header then set it to date
                   1314:        */
1.62      frystyk  1315:        if (ANCH_TRACE) HTTrace("HTAnchor.... Anchor is parsed\n");
1.59      frystyk  1316:        me->header_parsed = YES;
                   1317:     }
1.23      frystyk  1318: }
                   1319: 
1.59      frystyk  1320: PUBLIC BOOL HTAnchor_headerParsed (HTParentAnchor * me)
1.23      frystyk  1321: {
1.59      frystyk  1322:     return (me ? me->header_parsed : NO);
1.2       timbl    1323: }
                   1324: 
1.17      frystyk  1325: /*     Clear Header Information
                   1326: **     ------------------------
                   1327: */
1.35      frystyk  1328: PUBLIC void HTAnchor_clearHeader (HTParentAnchor * me)
1.2       timbl    1329: {
1.63      frystyk  1330:     if (ANCH_TRACE) HTTrace("HTAnchor.... Clear all header information\n");
                   1331:     me->allow = METHOD_INVALID;
1.48      frystyk  1332:     if (me->content_encoding) {
                   1333:        HTList_delete(me->content_encoding);
1.65      frystyk  1334:        me->content_encoding = NULL;
1.48      frystyk  1335:     }
1.17      frystyk  1336:     if (me->content_language) {
                   1337:        HTList_delete(me->content_language);
1.65      frystyk  1338:        me->content_language = NULL;
1.9       frystyk  1339:     }
1.51      frystyk  1340:     HT_FREE(me->content_base);
                   1341:     HT_FREE(me->content_location);
1.17      frystyk  1342:     me->content_length = -1;                                     /* Invalid */
1.57      frystyk  1343: 
1.59      frystyk  1344:     /* Delete the title */
                   1345:     HT_FREE(me->title);
                   1346: 
1.57      frystyk  1347:     /* Clear the content type */
1.17      frystyk  1348:     me->content_type = WWW_UNKNOWN;
1.52      frystyk  1349:     if (me->type_parameters) {
                   1350:        HTAssocList_delete(me->type_parameters);
                   1351:        me->type_parameters = NULL;
1.72      frystyk  1352:     }    
                   1353: 
                   1354:     /* Meta tags */
                   1355:     if (me->meta_tags) {
                   1356:        HTAssocList_delete(me->meta_tags);
                   1357:        me->meta_tags = NULL;
1.52      frystyk  1358:     }    
1.57      frystyk  1359: 
                   1360:     /* Dates etc. */
1.28      frystyk  1361:     me->date = (time_t) -1;
                   1362:     me->expires = (time_t) -1;
                   1363:     me->last_modified = (time_t) -1;
1.59      frystyk  1364:     me->age = (time_t) -1;
1.17      frystyk  1365:     
1.43      frystyk  1366:     HT_FREE(me->derived_from);
                   1367:     HT_FREE(me->version);
1.58      frystyk  1368:     HT_FREE(me->etag);
1.63      frystyk  1369: 
                   1370:     /* Delete any original headers */
                   1371:     if (me->headers) HTAssocList_delete(me->headers);
                   1372:     me->headers = NULL;
1.18      frystyk  1373: 
1.62      frystyk  1374:     /* Anchor is cleared */
                   1375:     me->header_parsed = NO;
1.1       timbl    1376: }

Webmaster