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

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

Webmaster