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

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.89    ! kahan       6: **     @(#) $Id: HTAnchor.c,v 1.88 2000/02/29 14:47:57 kahan 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:        }
1.89    ! kahan     510:     }
        !           511: 
        !           512:     /* 2001/03/06: Bug fix by Serge Adda <sAdda@infovista.com>
        !           513:        HTAnchor_delete wasn't removing the reference to the deleted
        !           514:        anchor. This caused a bug whenever requesting another anchor
        !           515:        for the same URL.
        !           516:     */
        !           517:     if (adult_table) {
        !           518:       int hash;
        !           519:       const char *p;
        !           520:       HTList * adults;
        !           521:       HTList * grownups;
        !           522:       HTList * last;
        !           523:       HTParentAnchor * foundAnchor;
        !           524: 
        !           525:       /* Select list from hash table */
        !           526:       for(p=me->address, hash=0; *p; p++)
        !           527:        hash = (int) ((hash * 3 + (*(unsigned char*)p)) %
        !           528:                      PARENT_HASH_SIZE);
        !           529:       adults = adult_table[hash];
        !           530: 
        !           531:       /* Search list for anchor */
        !           532:       grownups = adults;
        !           533:       last = grownups;
        !           534:       while ((foundAnchor = (HTParentAnchor *)
        !           535:              HTList_nextObject(grownups))){
        !           536:        if (!strcmp(foundAnchor->address, me->address)) {
        !           537:          HTList_quickRemoveElement (grownups, last);
        !           538:          break;
        !           539:        }
        !           540:        last = grownups;
        !           541:       }
1.54      frystyk   542:     }
1.1       timbl     543: 
1.54      frystyk   544:     /* Now kill myself */
                    545:     delete_parent(me);
                    546:     return YES;  /* Parent deleted */
                    547: #if 0
1.3       timbl     548:   if (! HTList_isEmpty (me->sources)) {  /* There are still incoming links */
1.1       timbl     549:     /* Delete all outgoing links from children, if any */
1.3       timbl     550:     HTList *kids = me->children;
1.12      frystyk   551:     while ((child = (HTChildAnchor *) HTList_nextObject (kids)))
1.54      frystyk   552:       delete_links ((HTAnchor *) child);
1.1       timbl     553:     return NO;  /* Parent not deleted */
                    554:   }
                    555: 
                    556:   /* No more incoming links : kill everything */
                    557:   /* First, recursively delete children */
1.12      frystyk   558:   while ((child = (HTChildAnchor *) HTList_removeLastObject (me->children))) {
1.54      frystyk   559:     delete_links ((HTAnchor *) child);
1.43      frystyk   560:     HT_FREE(child->tag);
                    561:     HT_FREE(child);
1.1       timbl     562:   }
1.54      frystyk   563: #endif
1.69      frystyk   564: }
                    565: 
                    566: /*     FLATTEN ALL ANCHORS
                    567: **     -------------------
                    568: **     Flattens the anchor web structure into an array.
                    569: **     This is useful for calculating statistics, sorting
                    570: **     the parent anchors etc.
                    571: **
                    572: **      The caller can indicate the size of the array (total
                    573: **      number of anchors if known - otherwise 0).
                    574: **
                    575: **     Return an array that must be freed by the caller or
                    576: **      NULL if no anchors.
                    577: */
                    578: PUBLIC HTArray * HTAnchor_getArray (int growby)
                    579: {
                    580:     int cnt;
                    581:     HTArray * array = NULL;
                    582:     HTList * cur = NULL;
                    583:     if (!adult_table) return NULL;
                    584: 
                    585:     /* Allocate an array for the anchors */
1.87      frystyk   586:     if (growby <= 0) growby = PARENT_HASH_SIZE;
1.69      frystyk   587:     array = HTArray_new(growby);
                    588: 
                    589:     /* Traverse anchor structure */
1.85      frystyk   590:     for (cnt=0; cnt<PARENT_HASH_SIZE; cnt++) {
1.69      frystyk   591:        if ((cur = adult_table[cnt])) { 
                    592:            HTParentAnchor * pres = NULL;
                    593:            while ((pres = (HTParentAnchor *) HTList_nextObject(cur)) != NULL) {
                    594:                 if (HTArray_addObject(array, pres) == NO) {
1.86      frystyk   595:                     HTTRACE(ANCH_TRACE, "Anchor...... Can't add object %p to array %p\n" _ 
                    596:                                pres _ array);
1.69      frystyk   597:                     break;
                    598:                 }
                    599:            }
                    600:        }
                    601:     }
                    602:     return array;
1.1       timbl     603: }
                    604: 
1.17      frystyk   605: /* ------------------------------------------------------------------------- */
                    606: /*                             Data Access Methods                          */
                    607: /* ------------------------------------------------------------------------- */
1.1       timbl     608: 
1.35      frystyk   609: PUBLIC HTParentAnchor * HTAnchor_parent  (HTAnchor * me)
1.1       timbl     610: {
1.17      frystyk   611:     return me ? me->parent : NULL;
1.1       timbl     612: }
                    613: 
1.41      frystyk   614: PUBLIC void HTAnchor_setDocument  (HTParentAnchor * me, void * doc)
1.1       timbl     615: {
1.41      frystyk   616:     if (me) me->document = doc;
1.1       timbl     617: }
                    618: 
1.41      frystyk   619: PUBLIC void * HTAnchor_document  (HTParentAnchor * me)
1.1       timbl     620: {
1.17      frystyk   621:     return me ? me->document : NULL;
1.1       timbl     622: }
                    623: 
1.53      frystyk   624: PUBLIC char * HTAnchor_address  (HTAnchor * me) 
                    625: { 
                    626:     char *addr = NULL;
                    627:     if (me) {
                    628:         if (((HTParentAnchor *) me == me->parent) ||
                    629:             !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
                    630:             StrAllocCopy (addr, me->parent->address);
                    631:         }
                    632:         else {                  /* it's a named child */
                    633:             if ((addr = (char  *) HT_MALLOC(2 + strlen (me->parent->address) + \
                    634: strlen (((HTChildAnchor *) me)->tag))) == NULL)
                    635:                 HT_OUTOFMEM("HTAnchor_address");
                    636:             sprintf (addr, "%s#%s", me->parent->address,
                    637:                      ((HTChildAnchor *) me)->tag);
                    638:         }
                    639:     }
                    640:     return addr;
                    641: }
                    642: 
1.51      frystyk   643: /*
1.53      frystyk   644: **     We resolve the child address with respect to either a base URL,
                    645: **     a content-location, or to the request-URI
1.51      frystyk   646: */
1.53      frystyk   647: PUBLIC char * HTAnchor_expandedAddress  (HTAnchor * me)
1.1       timbl     648: {
1.17      frystyk   649:     char *addr = NULL;
                    650:     if (me) {
1.53      frystyk   651:        HTParentAnchor * parent = me->parent;
1.63      frystyk   652:        char * base = HTAnchor_base(parent);
1.17      frystyk   653:        if (((HTParentAnchor *) me == me->parent) ||
                    654:            !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
1.52      frystyk   655:            StrAllocCopy(addr, base);
1.51      frystyk   656:        } else {                        /* it's a named child */
1.52      frystyk   657:            if ((addr = (char *) HT_MALLOC(2 + strlen(base) + strlen(((HTChildAnchor *) me)->tag))) == NULL)
1.43      frystyk   658:                HT_OUTOFMEM("HTAnchor_address");
1.52      frystyk   659:            sprintf (addr, "%s#%s", base, ((HTChildAnchor *) me)->tag);
1.17      frystyk   660:        }
1.1       timbl     661:     }
1.17      frystyk   662:     return addr;
1.1       timbl     663: }
                    664: 
1.52      frystyk   665: /*     Physical Address
                    666: **     ----------------
                    667: */
                    668: PUBLIC char * HTAnchor_physical (HTParentAnchor * me)
                    669: {
1.53      frystyk   670:     return me ? me->physical ? me->physical : me->address : NULL;
1.52      frystyk   671: }
                    672: 
1.53      frystyk   673: PUBLIC void HTAnchor_setPhysical (HTParentAnchor * me, char * physical)
1.52      frystyk   674: {
                    675:     if (!me || !physical) {
1.86      frystyk   676:        HTTRACE(ANCH_TRACE, "HTAnchor.... setPhysical, called with null argument\n");
1.52      frystyk   677:        return;
                    678:     }
                    679:     StrAllocCopy(me->physical, physical);
                    680: }
                    681: 
1.53      frystyk   682: PUBLIC void HTAnchor_clearPhysical(HTParentAnchor * me)
                    683: {
                    684:     if (me) HT_FREE(me->physical);
                    685: }
                    686: 
                    687: /*
                    688: **     Children information
                    689: */
1.35      frystyk   690: PUBLIC BOOL HTAnchor_hasChildren  (HTParentAnchor * me)
1.17      frystyk   691: {
1.54      frystyk   692:     return (me && me->children);
1.74      frystyk   693: }
                    694: 
                    695: /*
                    696: ** Fix up a simple routine to see if this anchor is a (ChildAnchor *)
                    697: ** Seem to be doing it all over the place, so simplify!
                    698: */
                    699: PUBLIC BOOL HTAnchor_isChild (HTAnchor * me)
                    700: {
                    701:     return (me && (HTParentAnchor *) me != me->parent);
                    702: }
                    703: 
                    704: PUBLIC char * HTAnchor_view (HTAnchor * me)
                    705: {
                    706:     char * view = NULL;
                    707:     if (me && (HTParentAnchor *) me != me->parent && ((HTChildAnchor *) me)->tag)
                    708:        StrAllocCopy(view, ((HTChildAnchor *) me)->tag);
                    709:     return view;
1.17      frystyk   710: }
1.1       timbl     711: 
1.63      frystyk   712: /* ------------------------------------------------------------------------- */
                    713: /*                           Entity Header Information                      */
                    714: /* ------------------------------------------------------------------------- */
                    715: 
1.59      frystyk   716: /*
1.63      frystyk   717: **  Take the relevant infomration from the response object and cache it
                    718: **  in the anchor object. We inherit the information that is already
                    719: **  parsed in the response along with the unparsed headers.
1.52      frystyk   720: */
1.63      frystyk   721: PUBLIC BOOL HTAnchor_update (HTParentAnchor * me, HTResponse * response)
1.52      frystyk   722: {
1.63      frystyk   723:     if (me && response) {
1.84      frystyk   724:        HTCachable cachable = HTResponse_isCachable(response);
1.59      frystyk   725: 
1.84      frystyk   726:        if (cachable == HT_CACHE_ETAG) {
1.79      frystyk   727:            char * etag = HTResponse_etag(response);
1.86      frystyk   728:            HTTRACE(ANCH_TRACE, "HTAnchor.... Updating etag for %p\n" _ me);
1.79      frystyk   729:            if (etag) {
                    730:                HTAnchor_setEtag(me, etag);
                    731:                return YES;
                    732:            }
1.84      frystyk   733: 
                    734:        } else if (cachable == HT_CACHE_NOT_MODIFIED) {
1.86      frystyk   735:            HTTRACE(ANCH_TRACE, "HTAnchor.... Information is up to date for %p\n" _ me);
1.84      frystyk   736:            return YES;
                    737: 
                    738:        } else if (cachable == HT_CACHE_ALL) {
1.81      frystyk   739:            char * etag = HTResponse_etag(response);
1.86      frystyk   740:            HTTRACE(ANCH_TRACE, "HTAnchor.... Updating metainformation for %p\n" _ me);
1.79      frystyk   741: 
                    742:            /*
                    743:            **  The content length and type is already parsed at this point
                    744:            **  in time. We also check for format parameters like charset etc.
                    745:            **  and copy the contents in the anchor object
                    746:            */
                    747:            me->content_length = HTResponse_length(response);
                    748:            me->content_type = HTResponse_format(response);
                    749:            me->type_parameters = HTResponse_formatParam(response);
1.80      frystyk   750:            me->content_encoding = HTResponse_encoding(response);
1.63      frystyk   751:        
1.81      frystyk   752:             /* Don't forget the etag as well */
                    753:                    if (etag) HTAnchor_setEtag(me, etag);
                    754: 
1.79      frystyk   755:            /*
                    756:            **  Inherit all the unparsed headers - we may need them later!
                    757:            */
1.83      frystyk   758:            if (me->headers) HTAssocList_delete(me->headers);
1.79      frystyk   759:            me->headers = HTResponse_handOverHeader(response);
                    760: 
                    761:            /*
                    762:            **  Notifify the response object not to delete the lists that we
                    763:            **  have inherited in the anchor object
                    764:            */
                    765:            HTResponse_isCached(response, YES);
1.59      frystyk   766: 
1.84      frystyk   767:            /*
                    768:            **  Set the datestamp of when the anchor was updated if we didn't
                    769:            **  get any in the response
                    770:            */
                    771:            if (!HTAssocList_findObject(me->headers, "date"))
                    772:                HTAnchor_setDate(me, time(NULL));
                    773: 
1.79      frystyk   774:            return YES;
                    775:        }
1.59      frystyk   776:     }
                    777:     return NO;
1.52      frystyk   778: }
                    779: 
                    780: /*
                    781: **     Variants. If this anchor has any variants then keep them in a list
                    782: **     so that we can find them later. The list is simply a list of 
                    783: **     parent anchors.
                    784: */
                    785: PUBLIC HTList * HTAnchor_variants (HTParentAnchor * me)
1.17      frystyk   786: {
1.52      frystyk   787:     return me ? me->variants : NULL;
1.17      frystyk   788: }
1.1       timbl     789: 
1.52      frystyk   790: PUBLIC BOOL HTAnchor_addVariant (HTParentAnchor * me,
                    791:                                 HTParentAnchor * variant)
1.1       timbl     792: {
1.52      frystyk   793:     if (me && variant) {
                    794:        if (!me->variants) me->variants = HTList_new();
                    795:        return HTList_addObject(me->variants, variant);
                    796:     }
                    797:     return NO;
1.17      frystyk   798: }
                    799: 
1.52      frystyk   800: PUBLIC BOOL HTAnchor_deleteVariant (HTParentAnchor * me,
                    801:                                    HTParentAnchor * variant)
1.17      frystyk   802: {
1.52      frystyk   803:     return (me && variant) ? HTList_removeObject(me->variants, variant) : NO;
1.9       frystyk   804: }
1.1       timbl     805: 
1.52      frystyk   806: /*
                    807: **     Is this resource an index?
1.17      frystyk   808: */
1.52      frystyk   809: PUBLIC void HTAnchor_clearIndex  (HTParentAnchor * me)
                    810: {
                    811:     if (me) me->isIndex = NO;
                    812: }
1.17      frystyk   813: 
1.52      frystyk   814: PUBLIC void HTAnchor_setIndex  (HTParentAnchor * me)
1.1       timbl     815: {
1.52      frystyk   816:   if (me) me->isIndex = YES;
1.1       timbl     817: }
                    818: 
1.52      frystyk   819: PUBLIC BOOL HTAnchor_isIndex  (HTParentAnchor * me)
1.1       timbl     820: {
1.52      frystyk   821:     return me ? me->isIndex : NO;
1.27      frystyk   822: }
                    823: 
1.52      frystyk   824: /*     Content Base
1.51      frystyk   825: **     ------------
                    826: */
                    827: PUBLIC char * HTAnchor_base (HTParentAnchor * me)
                    828: {
1.63      frystyk   829:     if (me) {
                    830:        if (me->content_base) return me->content_base;
                    831:        if (me->headers) {
                    832:            char * base = HTAssocList_findObject(me->headers, "content-base");
                    833:            /*
                    834:            **  If no base is found then take the content-location if this
                    835:            **  is present and is absolute, else use the Request-URI.
                    836:            */
1.66      frystyk   837:            if (base) StrAllocCopy(me->content_base, HTStrip(base));
1.63      frystyk   838:        }
1.66      frystyk   839: 
                    840:        /*
                    841:        **  Try the content location if any
                    842:        */
                    843:        {
                    844:            char * location = HTAnchor_location(me);
                    845:            StrAllocCopy(me->content_base,
                    846:                         (location && HTURL_isAbsolute(location)) ?
                    847:                         location : me->address);
                    848:        }
                    849:        return me->content_base;
1.63      frystyk   850:     }
                    851:     return NULL;
1.51      frystyk   852: }
                    853: 
                    854: PUBLIC BOOL HTAnchor_setBase (HTParentAnchor * me, char * base)
                    855: {
1.63      frystyk   856:     if (me && base) {
                    857:        StrAllocCopy(me->content_base, base);
                    858:        return YES;
1.51      frystyk   859:     }
1.63      frystyk   860:     return NO;
1.51      frystyk   861: }
                    862: 
1.52      frystyk   863: /*     Content Location
                    864: **     ----------------
1.27      frystyk   865: */
1.52      frystyk   866: PUBLIC char * HTAnchor_location (HTParentAnchor * me)
1.27      frystyk   867: {
1.63      frystyk   868:     if (me) {
                    869:        if (me->content_location)
                    870:            return *me->content_location ? me->content_location : NULL;
                    871:        if (me->headers) {
                    872:            char * location = HTAssocList_findObject(me->headers, "content-location");
                    873:            StrAllocCopy(me->content_location, location ? HTStrip(location) : "");
                    874:            return me->content_location;
                    875:        }
                    876:     }
                    877:     return NULL;
1.27      frystyk   878: }
                    879: 
1.53      frystyk   880: /*
                    881: **     Expand the location relative to the base URL if any, otherwise the 
                    882: **     anchor address it self
                    883: */
1.52      frystyk   884: PUBLIC BOOL HTAnchor_setLocation (HTParentAnchor * me, char * location)
1.27      frystyk   885: {
1.53      frystyk   886:     if (me && location) {
1.63      frystyk   887:        char * base = HTAnchor_base(me);
                    888:        if (!base) base = me->address;
1.53      frystyk   889:        me->content_location = HTParse(location, base, PARSE_ALL);
                    890:        return YES;
1.52      frystyk   891:     }
1.53      frystyk   892:     return NO;
1.1       timbl     893: }
                    894: 
1.72      frystyk   895: /*     Meta tags
                    896: **     ---------
                    897: */
                    898: PUBLIC HTAssocList * HTAnchor_meta (HTParentAnchor * me)
                    899: {
                    900:     return me ? me->meta_tags : NULL;
                    901: }
                    902: 
                    903: PUBLIC BOOL HTAnchor_addMeta (HTParentAnchor * me,
                    904:                              const char * name, const char * value)
                    905: {
                    906:     if (me) {
                    907:        if (!me->meta_tags) me->meta_tags = HTAssocList_new();
                    908:        return HTAssocList_replaceObject(me->meta_tags, name, value);
                    909:     }
                    910:     return NO;
                    911: }
                    912: 
                    913: /*
                    914: **     robots meta tag
                    915: */
                    916: PUBLIC char * HTAnchor_robots (HTParentAnchor * me)
                    917: {
                    918:     if (me && me->meta_tags) {
                    919:        char * robots = HTAssocList_findObject(me->meta_tags, "robots");
                    920:        return robots;
                    921:     }
                    922:     return NULL;
                    923: }
                    924: 
1.52      frystyk   925: /*     Content-Type
                    926: **     ------------
1.17      frystyk   927: */
1.35      frystyk   928: PUBLIC HTFormat HTAnchor_format (HTParentAnchor * me)
1.17      frystyk   929: {
                    930:     return me ? me->content_type : NULL;
                    931: }
1.1       timbl     932: 
1.35      frystyk   933: PUBLIC void HTAnchor_setFormat (HTParentAnchor * me, HTFormat form)
1.1       timbl     934: {
1.17      frystyk   935:     if (me) me->content_type = form;
1.1       timbl     936: }
                    937: 
1.52      frystyk   938: PUBLIC HTAssocList * HTAnchor_formatParam (HTParentAnchor * me)
                    939: {
                    940:     return me ? me->type_parameters : NULL;
                    941: }
                    942: 
                    943: PUBLIC BOOL HTAnchor_addFormatParam (HTParentAnchor * me,
                    944:                                     const char * name, const char * value)
                    945: {
                    946:     if (me) {
                    947:        if (!me->type_parameters) me->type_parameters = HTAssocList_new();
                    948:        return HTAssocList_replaceObject(me->type_parameters, name, value);
                    949:     }
                    950:     return NO;
                    951: }
                    952: 
1.17      frystyk   953: /*
                    954: **     Charset parameter to Content-Type
1.1       timbl     955: */
1.35      frystyk   956: PUBLIC HTCharset HTAnchor_charset (HTParentAnchor * me)
1.1       timbl     957: {
1.52      frystyk   958:     if (me && me->type_parameters) {
                    959:        char * charset = HTAssocList_findObject(me->type_parameters,"charset");
                    960:        return HTAtom_for(charset);
                    961:     }
                    962:     return NULL;
1.1       timbl     963: }
                    964: 
1.52      frystyk   965: PUBLIC BOOL HTAnchor_setCharset (HTParentAnchor * me, HTCharset charset)
1.1       timbl     966: {
1.52      frystyk   967:     return HTAnchor_addFormatParam(me, "charset", HTAtom_name(charset));
1.1       timbl     968: }
                    969: 
1.17      frystyk   970: /*
1.20      frystyk   971: **     Level parameter to Content-Type
                    972: */
1.35      frystyk   973: PUBLIC HTLevel HTAnchor_level (HTParentAnchor * me)
1.20      frystyk   974: {
1.52      frystyk   975:     if (me && me->type_parameters) {
                    976:        char * level = HTAssocList_findObject(me->type_parameters, "level");
                    977:        return HTAtom_for(level);
                    978:     }
                    979:     return NULL;
1.20      frystyk   980: }
                    981: 
1.52      frystyk   982: PUBLIC BOOL HTAnchor_setLevel (HTParentAnchor * me, HTLevel level)
1.20      frystyk   983: {
1.52      frystyk   984:     return HTAnchor_addFormatParam(me, "level", HTAtom_name(level));
1.20      frystyk   985: }
                    986: 
                    987: /*
1.17      frystyk   988: **     Content Encoding
                    989: */
1.47      frystyk   990: PUBLIC HTList * HTAnchor_encoding (HTParentAnchor * me)
1.1       timbl     991: {
1.17      frystyk   992:     return me ? me->content_encoding : NULL;
1.1       timbl     993: }
                    994: 
1.63      frystyk   995: PUBLIC BOOL HTAnchor_addEncoding (HTParentAnchor * me, HTEncoding encoding)
1.17      frystyk   996: {
1.47      frystyk   997:     if (me && encoding) {
                    998:        if (!me->content_encoding) me->content_encoding = HTList_new();
                    999:        return HTList_addObject(me->content_encoding, encoding);
                   1000:     }
                   1001:     return NO;
1.17      frystyk  1002: }
                   1003: 
1.77      frystyk  1004: PUBLIC BOOL HTAnchor_deleteEncoding (HTParentAnchor * me, HTEncoding encoding)
1.67      frystyk  1005: {
                   1006:     return (me && me->content_encoding && encoding) ?
                   1007:        HTList_removeObject(me->content_encoding, encoding) : NO;
                   1008: }
                   1009: 
1.77      frystyk  1010: PUBLIC BOOL HTAnchor_deleteEncodingAll (HTParentAnchor * me)
                   1011: {
                   1012:     if (me && me->content_encoding) {
                   1013:        HTList_delete(me->content_encoding);
                   1014:        me->content_encoding = NULL;
                   1015:        return YES;
                   1016:     }
                   1017:     return NO;
                   1018: }
                   1019: 
1.17      frystyk  1020: /*
1.21      frystyk  1021: **     Content Language
                   1022: */
1.47      frystyk  1023: PUBLIC HTList * HTAnchor_language (HTParentAnchor * me)
1.21      frystyk  1024: {
1.63      frystyk  1025:     if (me) {
                   1026:        if (me->content_language == NULL && me->headers) {
                   1027:            char * value = HTAssocList_findObject(me->headers, "content-language");
                   1028:            char * field;
                   1029:            if (!me->content_language) me->content_language = HTList_new();
                   1030:            while ((field = HTNextField(&value)) != NULL) {
                   1031:                char * lc = field;
                   1032:                while ((*lc = TOLOWER(*lc))) lc++;
                   1033:                HTList_addObject(me->content_language, HTAtom_for(field));
                   1034:            }
                   1035:        }
                   1036:        return me->content_language;
                   1037:     }
                   1038:     return NULL;
1.21      frystyk  1039: }
                   1040: 
1.63      frystyk  1041: PUBLIC BOOL HTAnchor_addLanguage (HTParentAnchor * me, HTLanguage language)
1.21      frystyk  1042: {
1.47      frystyk  1043:     if (me && language) {
                   1044:        if (!me->content_language) me->content_language = HTList_new();
                   1045:        return HTList_addObject(me->content_language, language);
1.77      frystyk  1046:     }
                   1047:     return NO;
                   1048: }
                   1049: 
                   1050: PUBLIC BOOL HTAnchor_deleteLanguageAll (HTParentAnchor * me)
                   1051: {
                   1052:     if (me && me->content_language) {
                   1053:        HTList_delete(me->content_language);
                   1054:        me->content_language = NULL;
                   1055:        return YES;
1.47      frystyk  1056:     }
                   1057:     return NO;
1.21      frystyk  1058: }
                   1059: 
                   1060: /*
1.17      frystyk  1061: **     Content Length
                   1062: */
1.35      frystyk  1063: PUBLIC long int HTAnchor_length (HTParentAnchor * me)
1.1       timbl    1064: {
1.17      frystyk  1065:     return me ? me->content_length : -1;
1.1       timbl    1066: }
                   1067: 
1.35      frystyk  1068: PUBLIC void HTAnchor_setLength (HTParentAnchor * me, long int length)
1.17      frystyk  1069: {
                   1070:     if (me) me->content_length = length;
                   1071: }
1.1       timbl    1072: 
1.49      frystyk  1073: PUBLIC void HTAnchor_addLength (HTParentAnchor * me, long int deltalength)
                   1074: {
                   1075:     if (me) {
                   1076:        if (me->content_length < 0)
                   1077:            me->content_length = deltalength;
                   1078:        else
                   1079:            me->content_length += deltalength;
                   1080:     }
                   1081: }
                   1082: 
1.17      frystyk  1083: /*
1.63      frystyk  1084: **     Content Transfer Encoding
                   1085: */
1.73      frystyk  1086: PUBLIC HTEncoding HTAnchor_contentTransferEncoding (HTParentAnchor * me)
1.63      frystyk  1087: {
1.73      frystyk  1088:     return me ? me->cte : NULL;
1.63      frystyk  1089: }
                   1090: 
1.73      frystyk  1091: PUBLIC void HTAnchor_setContentTransferEncoding (HTParentAnchor * me, HTEncoding cte)
1.63      frystyk  1092: {
1.73      frystyk  1093:     if (me) me->cte = cte;
1.63      frystyk  1094: }
                   1095: 
                   1096: /*
1.17      frystyk  1097: **     Allowed methods (Allow)
1.1       timbl    1098: */
1.63      frystyk  1099: PUBLIC HTMethod HTAnchor_allow (HTParentAnchor * me)
1.17      frystyk  1100: {
1.63      frystyk  1101:     if (me) {
                   1102:        if (me->allow == 0 && me->headers) {
                   1103:            char * value = HTAssocList_findObject(me->headers, "allow");
                   1104:            char * field;
                   1105: 
                   1106:            /*
                   1107:            **  We treat methods allowed on this object as case insensitive
                   1108:            **  in case we receive the information over the net - that is -
                   1109:            **  in the Allow header.
                   1110:            */
                   1111:            while ((field = HTNextField(&value)) != NULL) {
                   1112:                HTMethod new_method;
                   1113:                if ((new_method = HTMethod_enum(field)) != METHOD_INVALID)
                   1114:                    me->allow |= new_method;
                   1115:            }
                   1116:        }
                   1117:        return me->allow;
                   1118:     }  
                   1119:     return METHOD_INVALID;
1.17      frystyk  1120: }
1.1       timbl    1121: 
1.63      frystyk  1122: PUBLIC void HTAnchor_setAllow (HTParentAnchor * me, HTMethod methodset)
1.1       timbl    1123: {
1.63      frystyk  1124:     if (me) me->allow = methodset;
1.1       timbl    1125: }
                   1126: 
1.63      frystyk  1127: PUBLIC void HTAnchor_appendAllow (HTParentAnchor * me, HTMethod methodset)
1.1       timbl    1128: {
1.63      frystyk  1129:     if (me) me->allow |= methodset;
1.1       timbl    1130: }
                   1131: 
1.17      frystyk  1132: /*
                   1133: **     Title
1.2       timbl    1134: */
1.46      frystyk  1135: PUBLIC const char * HTAnchor_title  (HTParentAnchor * me)
1.1       timbl    1136: {
1.63      frystyk  1137:     if (me) {
                   1138:        if (me->title)
                   1139:            return *me->title ? me->title : NULL;
                   1140:        if (me->headers) {
                   1141:            char * value = HTAssocList_findObject(me->headers, "title");
                   1142:            char * title;
                   1143:            if ((title = HTNextField(&value))) StrAllocCopy(me->title, title);
                   1144:            return me->title;
                   1145:        }
                   1146:     }
                   1147:     return NULL;
1.17      frystyk  1148: }
1.1       timbl    1149: 
1.46      frystyk  1150: PUBLIC void HTAnchor_setTitle (HTParentAnchor * me, const char * title)
1.17      frystyk  1151: {
1.76      frystyk  1152:     if (me && title) {
                   1153:        char * ptr;
                   1154:        StrAllocCopy(me->title, title);
                   1155:        ptr = me->title;
                   1156:        while (*ptr) {
                   1157:            if (isspace((int) *ptr)) *ptr = ' ';                
                   1158:            ptr++;
                   1159:        }
                   1160:     }
1.2       timbl    1161: }
                   1162: 
1.46      frystyk  1163: PUBLIC void HTAnchor_appendTitle (HTParentAnchor * me, const char * title)
1.17      frystyk  1164: {
                   1165:     if (me && title) StrAllocCat(me->title, title);
                   1166: }
1.2       timbl    1167: 
1.17      frystyk  1168: /*
                   1169: **     Version
1.2       timbl    1170: */
1.41      frystyk  1171: PUBLIC char * HTAnchor_version (HTParentAnchor * me)
1.17      frystyk  1172: {
1.63      frystyk  1173:     if (me) {
                   1174:        if (me->version)
                   1175:            return *me->version ? me->version : NULL;
                   1176:        if (me->headers) {
                   1177:            char * value = HTAssocList_findObject(me->headers, "version");
                   1178:            char * version;
                   1179:            if ((version = HTNextField(&value)))
                   1180:                StrAllocCopy(me->version, version);
                   1181:            return me->version;
                   1182:        }
                   1183:     }
                   1184:     return NULL;
1.17      frystyk  1185: }
1.2       timbl    1186: 
1.46      frystyk  1187: PUBLIC void HTAnchor_setVersion (HTParentAnchor * me, const char * version)
1.2       timbl    1188: {
1.17      frystyk  1189:     if (me && version) StrAllocCopy(me->version, version);
1.2       timbl    1190: }
                   1191: 
1.17      frystyk  1192: /*
                   1193: **     Derived from
1.2       timbl    1194: */
1.41      frystyk  1195: PUBLIC char * HTAnchor_derived (HTParentAnchor * me)
1.17      frystyk  1196: {
1.63      frystyk  1197:     if (me) {
                   1198:        if (me->derived_from)
                   1199:            return *me->derived_from ? me->derived_from : NULL;
                   1200:        if (me->headers) {
                   1201:            char * value = HTAssocList_findObject(me->headers, "derived-from");
                   1202:            char * derived_from;
                   1203:            if ((derived_from = HTNextField(&value)))
                   1204:                StrAllocCopy(me->derived_from, derived_from);
                   1205:            return me->derived_from;
                   1206:        }
                   1207:     }
                   1208:     return NULL;
1.17      frystyk  1209: }
                   1210: 
1.46      frystyk  1211: PUBLIC void HTAnchor_setDerived (HTParentAnchor * me, const char *derived_from)
1.17      frystyk  1212: {
                   1213:     if (me && derived_from) StrAllocCopy(me->derived_from, derived_from);
                   1214: }
1.2       timbl    1215: 
1.17      frystyk  1216: /*
1.52      frystyk  1217: **     Content MD5
                   1218: */
                   1219: PUBLIC char * HTAnchor_md5 (HTParentAnchor * me)
                   1220: {
1.63      frystyk  1221:     if (me) {
                   1222:        if (me->content_md5)
                   1223:            return *me->content_md5 ? me->content_md5 : NULL;
                   1224:        if (me->headers) {
                   1225:            char * value = HTAssocList_findObject(me->headers, "content-md5");
                   1226:            char * md5;
                   1227:            if ((md5 = HTNextField(&value))) StrAllocCopy(me->content_md5,md5);
                   1228:            return me->content_md5;
                   1229:        }
                   1230:     }
                   1231:     return NULL;
1.52      frystyk  1232: }
                   1233: 
1.63      frystyk  1234: PUBLIC BOOL HTAnchor_setMd5 (HTParentAnchor * me, const char * hash)
1.52      frystyk  1235: {
1.63      frystyk  1236:     if (me && hash) {
                   1237:        StrAllocCopy(me->content_md5, hash);
                   1238:        return YES;
                   1239:     }
                   1240:     return NO;
1.52      frystyk  1241: }
                   1242: 
                   1243: /*
1.28      frystyk  1244: **     Date
                   1245: */
1.41      frystyk  1246: PUBLIC time_t HTAnchor_date (HTParentAnchor * me)
                   1247: {
1.63      frystyk  1248:     if (me) {
                   1249:        if (me->date == (time_t) -1 && me->headers) {
                   1250:            char * value = HTAssocList_findObject(me->headers, "date");
                   1251:            if (value) me->date = HTParseTime(value, NULL, YES);
                   1252:        }
                   1253:        return me->date;
                   1254:     }  
                   1255:     return (time_t) -1;
1.41      frystyk  1256: }
                   1257: 
1.46      frystyk  1258: PUBLIC void HTAnchor_setDate (HTParentAnchor * me, const time_t date)
1.28      frystyk  1259: {
1.41      frystyk  1260:     if (me) me->date = date;
1.28      frystyk  1261: }
                   1262: 
                   1263: /*
                   1264: **     Expires
                   1265: */
1.41      frystyk  1266: PUBLIC time_t HTAnchor_expires (HTParentAnchor * me)
                   1267: {
1.63      frystyk  1268:     if (me) {
                   1269:        if (me->expires == (time_t) -1 && me->headers) {
                   1270:            char * value = HTAssocList_findObject(me->headers, "expires");
                   1271:            if (value) me->expires = HTParseTime(value, NULL, YES);
                   1272:        }
                   1273:        return me->expires;
                   1274:     }  
                   1275:     return (time_t) -1;
1.41      frystyk  1276: }
                   1277: 
1.46      frystyk  1278: PUBLIC void HTAnchor_setExpires (HTParentAnchor * me, const time_t expires)
1.28      frystyk  1279: {
1.41      frystyk  1280:     if (me) me->expires = expires;
1.28      frystyk  1281: }
                   1282: 
                   1283: /*
                   1284: **     Last Modified
                   1285: */
1.41      frystyk  1286: PUBLIC time_t HTAnchor_lastModified (HTParentAnchor * me)
                   1287: {
1.63      frystyk  1288:     if (me) {
                   1289:        if (me->last_modified == (time_t) -1 && me->headers) {
                   1290:            char * value = HTAssocList_findObject(me->headers,"last-modified");
                   1291:            if (value) me->last_modified = HTParseTime(value, NULL, YES);
                   1292:        }
                   1293:        return me->last_modified;
                   1294:     }  
                   1295:     return (time_t) -1;
1.41      frystyk  1296: }
                   1297: 
1.46      frystyk  1298: PUBLIC void HTAnchor_setLastModified (HTParentAnchor * me, const time_t lm)
1.28      frystyk  1299: {
1.41      frystyk  1300:     if (me) me->last_modified = lm;
1.28      frystyk  1301: }
                   1302: 
                   1303: /*
1.59      frystyk  1304: **     Age
                   1305: */
                   1306: PUBLIC time_t HTAnchor_age (HTParentAnchor * me)
                   1307: {
1.63      frystyk  1308:     if (me) {
                   1309:        if (me->age == (time_t) -1 && me->headers) {
                   1310:            char * value = HTAssocList_findObject(me->headers, "age");
                   1311:            if (value) me->age = atol(value);
                   1312:        }
                   1313:        return me->age;
                   1314:     }  
                   1315:     return (time_t) -1;
1.59      frystyk  1316: }
                   1317: 
                   1318: PUBLIC void HTAnchor_setAge (HTParentAnchor * me, const time_t age)
                   1319: {
                   1320:     if (me) me->age = age;
                   1321: }
                   1322: 
                   1323: /*
1.52      frystyk  1324: **     Entity Tag
                   1325: */
                   1326: PUBLIC char * HTAnchor_etag (HTParentAnchor * me)
                   1327: {
1.63      frystyk  1328:     if (me) {
                   1329:        if (me->etag)
                   1330:            return *me->etag ? me->etag : NULL;
                   1331:        if (me->headers) {
                   1332:            char * value = HTAssocList_findObject(me->headers, "etag");
                   1333:            char * etag;
                   1334:            if ((etag = HTNextField(&value))) StrAllocCopy(me->etag, etag);
                   1335:            return me->etag;
                   1336:        }
                   1337:     }
                   1338: 
1.52      frystyk  1339:     return me ? me->etag : NULL;
                   1340: }
                   1341: 
                   1342: PUBLIC void HTAnchor_setEtag (HTParentAnchor * me, const char * etag)
                   1343: {
1.88      kahan    1344:   /* JK: add a new etag if it doesn't exist or if the value has changed */
                   1345:     if (me && etag && ((me->etag == NULL) || strcmp (me->etag, etag)))
                   1346:        StrAllocCopy(me->etag, etag);
1.52      frystyk  1347: }
                   1348: 
                   1349: PUBLIC BOOL HTAnchor_isEtagWeak (HTParentAnchor * me)
                   1350: {
                   1351:     return (me && me->etag && !strncasecomp(me->etag, "W/", 2));
                   1352: }
                   1353: 
                   1354: /*
1.63      frystyk  1355: **     Original headers (if any)
1.17      frystyk  1356: */
1.63      frystyk  1357: PUBLIC HTAssocList * HTAnchor_header (HTParentAnchor * me)
1.2       timbl    1358: {
1.63      frystyk  1359:     return me ? me->headers : NULL;
1.2       timbl    1360: }
                   1361: 
1.63      frystyk  1362: PUBLIC BOOL HTAnchor_setHeader (HTParentAnchor * me, HTAssocList * headers)
1.2       timbl    1363: {
1.17      frystyk  1364:     if (me) {
1.63      frystyk  1365:        me->headers = headers;
                   1366:        return YES;
1.17      frystyk  1367:     }
1.63      frystyk  1368:     return NO;
1.2       timbl    1369: }
                   1370: 
1.23      frystyk  1371: /*
1.59      frystyk  1372: **  Validate anchor values and finish up parsing
1.2       timbl    1373: */
1.59      frystyk  1374: PUBLIC void HTAnchor_setHeaderParsed (HTParentAnchor * me)
1.2       timbl    1375: {
1.59      frystyk  1376:     if (me) {
1.86      frystyk  1377:        HTTRACE(ANCH_TRACE, "HTAnchor.... Anchor is parsed\n");
1.59      frystyk  1378:        me->header_parsed = YES;
                   1379:     }
1.23      frystyk  1380: }
                   1381: 
1.59      frystyk  1382: PUBLIC BOOL HTAnchor_headerParsed (HTParentAnchor * me)
1.23      frystyk  1383: {
1.59      frystyk  1384:     return (me ? me->header_parsed : NO);
1.2       timbl    1385: }
                   1386: 
1.17      frystyk  1387: /*     Clear Header Information
                   1388: **     ------------------------
                   1389: */
1.35      frystyk  1390: PUBLIC void HTAnchor_clearHeader (HTParentAnchor * me)
1.2       timbl    1391: {
1.86      frystyk  1392:     HTTRACE(ANCH_TRACE, "HTAnchor.... Clear all header information\n");
1.63      frystyk  1393:     me->allow = METHOD_INVALID;
1.48      frystyk  1394:     if (me->content_encoding) {
                   1395:        HTList_delete(me->content_encoding);
1.65      frystyk  1396:        me->content_encoding = NULL;
1.48      frystyk  1397:     }
1.17      frystyk  1398:     if (me->content_language) {
                   1399:        HTList_delete(me->content_language);
1.65      frystyk  1400:        me->content_language = NULL;
1.9       frystyk  1401:     }
1.51      frystyk  1402:     HT_FREE(me->content_base);
                   1403:     HT_FREE(me->content_location);
1.17      frystyk  1404:     me->content_length = -1;                                     /* Invalid */
1.57      frystyk  1405: 
1.59      frystyk  1406:     /* Delete the title */
                   1407:     HT_FREE(me->title);
                   1408: 
1.57      frystyk  1409:     /* Clear the content type */
1.17      frystyk  1410:     me->content_type = WWW_UNKNOWN;
1.52      frystyk  1411:     if (me->type_parameters) {
                   1412:        HTAssocList_delete(me->type_parameters);
                   1413:        me->type_parameters = NULL;
1.72      frystyk  1414:     }    
                   1415: 
                   1416:     /* Meta tags */
                   1417:     if (me->meta_tags) {
                   1418:        HTAssocList_delete(me->meta_tags);
                   1419:        me->meta_tags = NULL;
1.52      frystyk  1420:     }    
1.57      frystyk  1421: 
                   1422:     /* Dates etc. */
1.28      frystyk  1423:     me->date = (time_t) -1;
                   1424:     me->expires = (time_t) -1;
                   1425:     me->last_modified = (time_t) -1;
1.59      frystyk  1426:     me->age = (time_t) -1;
1.17      frystyk  1427:     
1.43      frystyk  1428:     HT_FREE(me->derived_from);
                   1429:     HT_FREE(me->version);
1.58      frystyk  1430:     HT_FREE(me->etag);
1.63      frystyk  1431: 
                   1432:     /* Delete any original headers */
                   1433:     if (me->headers) HTAssocList_delete(me->headers);
                   1434:     me->headers = NULL;
1.1       timbl    1435: }

Webmaster