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

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

Webmaster