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

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

Webmaster