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

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.53    ! frystyk     6: **     @(#) $Id: HTAnchor.c,v 1.52 1996/07/02 22:54:10 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) {
1.53    ! frystyk   189:        char * relative_to = HTAnchor_expandedAddress((HTAnchor *) parent);
1.34      frystyk   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.53    ! frystyk   460: PUBLIC char * HTAnchor_address  (HTAnchor * me) 
        !           461: { 
        !           462:     char *addr = NULL;
        !           463:     if (me) {
        !           464:         if (((HTParentAnchor *) me == me->parent) ||
        !           465:             !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
        !           466:             StrAllocCopy (addr, me->parent->address);
        !           467:         }
        !           468:         else {                  /* it's a named child */
        !           469:             if ((addr = (char  *) HT_MALLOC(2 + strlen (me->parent->address) + \
        !           470: strlen (((HTChildAnchor *) me)->tag))) == NULL)
        !           471:                 HT_OUTOFMEM("HTAnchor_address");
        !           472:             sprintf (addr, "%s#%s", me->parent->address,
        !           473:                      ((HTChildAnchor *) me)->tag);
        !           474:         }
        !           475:     }
        !           476:     return addr;
        !           477: }
        !           478: 
1.51      frystyk   479: /*
1.53    ! frystyk   480: **     We resolve the child address with respect to either a base URL,
        !           481: **     a content-location, or to the request-URI
1.51      frystyk   482: */
1.53    ! frystyk   483: PUBLIC char * HTAnchor_expandedAddress  (HTAnchor * me)
1.1       timbl     484: {
1.17      frystyk   485:     char *addr = NULL;
                    486:     if (me) {
1.53    ! frystyk   487:        HTParentAnchor * parent = me->parent;
        !           488:        char * base = parent->content_location ? parent->content_location :
        !           489:            parent->content_base ? parent->content_base : parent->address;
1.17      frystyk   490:        if (((HTParentAnchor *) me == me->parent) ||
                    491:            !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
1.52      frystyk   492:            StrAllocCopy(addr, base);
1.51      frystyk   493:        } else {                        /* it's a named child */
1.52      frystyk   494:            if ((addr = (char *) HT_MALLOC(2 + strlen(base) + strlen(((HTChildAnchor *) me)->tag))) == NULL)
1.43      frystyk   495:                HT_OUTOFMEM("HTAnchor_address");
1.52      frystyk   496:            sprintf (addr, "%s#%s", base, ((HTChildAnchor *) me)->tag);
1.17      frystyk   497:        }
1.1       timbl     498:     }
1.17      frystyk   499:     return addr;
1.1       timbl     500: }
                    501: 
1.52      frystyk   502: /*     Physical Address
                    503: **     ----------------
                    504: */
                    505: PUBLIC char * HTAnchor_physical (HTParentAnchor * me)
                    506: {
1.53    ! frystyk   507:     return me ? me->physical ? me->physical : me->address : NULL;
1.52      frystyk   508: }
                    509: 
1.53    ! frystyk   510: PUBLIC void HTAnchor_setPhysical (HTParentAnchor * me, char * physical)
1.52      frystyk   511: {
                    512:     if (!me || !physical) {
                    513:        if (ANCH_TRACE)
                    514:            HTTrace("HTAnchor.... setPhysical, called with null argument\n");
                    515:        return;
                    516:     }
                    517:     StrAllocCopy(me->physical, physical);
                    518: }
                    519: 
1.53    ! frystyk   520: PUBLIC void HTAnchor_clearPhysical(HTParentAnchor * me)
        !           521: {
        !           522:     if (me) HT_FREE(me->physical);
        !           523: }
        !           524: 
        !           525: /*
        !           526: **     Children information
        !           527: */
1.35      frystyk   528: PUBLIC BOOL HTAnchor_hasChildren  (HTParentAnchor * me)
1.17      frystyk   529: {
                    530:     return me ? ! HTList_isEmpty(me->children) : NO;
                    531: }
1.1       timbl     532: 
1.52      frystyk   533: /*     Cache Information
                    534: **     -----------------
                    535: */
                    536: PUBLIC BOOL HTAnchor_cacheHit (HTParentAnchor * me)
                    537: {
                    538:     return me ? me->cacheHit : NO;
                    539: }
                    540: 
                    541: PUBLIC void HTAnchor_setCacheHit (HTParentAnchor * me, BOOL cacheHit)
                    542: {
                    543:     if (me) me->cacheHit = cacheHit;
                    544: }
                    545: 
                    546: /* ------------------------------------------------------------------------- */
                    547: /*                           Entity Header Information                      */
                    548: /* ------------------------------------------------------------------------- */
                    549: 
                    550: /*
                    551: **     Variants. If this anchor has any variants then keep them in a list
                    552: **     so that we can find them later. The list is simply a list of 
                    553: **     parent anchors.
                    554: */
                    555: PUBLIC HTList * HTAnchor_variants (HTParentAnchor * me)
1.17      frystyk   556: {
1.52      frystyk   557:     return me ? me->variants : NULL;
1.17      frystyk   558: }
1.1       timbl     559: 
1.52      frystyk   560: PUBLIC BOOL HTAnchor_addVariant (HTParentAnchor * me,
                    561:                                 HTParentAnchor * variant)
1.1       timbl     562: {
1.52      frystyk   563:     if (me && variant) {
                    564:        if (!me->variants) me->variants = HTList_new();
                    565:        return HTList_addObject(me->variants, variant);
                    566:     }
                    567:     return NO;
1.17      frystyk   568: }
                    569: 
1.52      frystyk   570: PUBLIC BOOL HTAnchor_deleteVariant (HTParentAnchor * me,
                    571:                                    HTParentAnchor * variant)
1.17      frystyk   572: {
1.52      frystyk   573:     return (me && variant) ? HTList_removeObject(me->variants, variant) : NO;
1.9       frystyk   574: }
1.1       timbl     575: 
1.52      frystyk   576: /*
                    577: **     Is this resource an index?
1.17      frystyk   578: */
1.52      frystyk   579: PUBLIC void HTAnchor_clearIndex  (HTParentAnchor * me)
                    580: {
                    581:     if (me) me->isIndex = NO;
                    582: }
1.17      frystyk   583: 
1.52      frystyk   584: PUBLIC void HTAnchor_setIndex  (HTParentAnchor * me)
1.1       timbl     585: {
1.52      frystyk   586:   if (me) me->isIndex = YES;
1.1       timbl     587: }
                    588: 
1.52      frystyk   589: PUBLIC BOOL HTAnchor_isIndex  (HTParentAnchor * me)
1.1       timbl     590: {
1.52      frystyk   591:     return me ? me->isIndex : NO;
1.27      frystyk   592: }
                    593: 
1.52      frystyk   594: /*     Content Base
1.51      frystyk   595: **     ------------
                    596: */
                    597: PUBLIC char * HTAnchor_base (HTParentAnchor * me)
                    598: {
                    599:     return me ? me->content_base : NULL;
                    600: }
                    601: 
                    602: PUBLIC BOOL HTAnchor_setBase (HTParentAnchor * me, char * base)
                    603: {
                    604:     if (!me || !base) {
                    605:        if (ANCH_TRACE)
                    606:            HTTrace("HTAnchor.... set base called with null argument\n");
                    607:        return NO;
                    608:     }
                    609:     StrAllocCopy(me->content_base, base);
                    610:     return YES;
                    611: }
                    612: 
1.52      frystyk   613: /*     Content Location
                    614: **     ----------------
1.27      frystyk   615: */
1.52      frystyk   616: PUBLIC char * HTAnchor_location (HTParentAnchor * me)
1.27      frystyk   617: {
1.52      frystyk   618:     return me ? me->content_location : NULL;
1.27      frystyk   619: }
                    620: 
1.53    ! frystyk   621: /*
        !           622: **     Expand the location relative to the base URL if any, otherwise the 
        !           623: **     anchor address it self
        !           624: */
1.52      frystyk   625: PUBLIC BOOL HTAnchor_setLocation (HTParentAnchor * me, char * location)
1.27      frystyk   626: {
1.53    ! frystyk   627:     if (me && location) {
        !           628:        char * base = me->content_base ? me->content_base : me->address;
        !           629:        me->content_location = HTParse(location, base, PARSE_ALL);
        !           630:        return YES;
1.52      frystyk   631:     }
1.53    ! frystyk   632:     return NO;
1.1       timbl     633: }
                    634: 
1.52      frystyk   635: /*     Content-Type
                    636: **     ------------
1.17      frystyk   637: */
1.35      frystyk   638: PUBLIC HTFormat HTAnchor_format (HTParentAnchor * me)
1.17      frystyk   639: {
                    640:     return me ? me->content_type : NULL;
                    641: }
1.1       timbl     642: 
1.35      frystyk   643: PUBLIC void HTAnchor_setFormat (HTParentAnchor * me, HTFormat form)
1.1       timbl     644: {
1.17      frystyk   645:     if (me) me->content_type = form;
1.1       timbl     646: }
                    647: 
1.52      frystyk   648: PUBLIC HTAssocList * HTAnchor_formatParam (HTParentAnchor * me)
                    649: {
                    650:     return me ? me->type_parameters : NULL;
                    651: }
                    652: 
                    653: PUBLIC BOOL HTAnchor_addFormatParam (HTParentAnchor * me,
                    654:                                     const char * name, const char * value)
                    655: {
                    656:     if (me) {
                    657:        if (!me->type_parameters) me->type_parameters = HTAssocList_new();
                    658:        return HTAssocList_replaceObject(me->type_parameters, name, value);
                    659:     }
                    660:     return NO;
                    661: }
                    662: 
1.17      frystyk   663: /*
                    664: **     Charset parameter to Content-Type
1.1       timbl     665: */
1.35      frystyk   666: PUBLIC HTCharset HTAnchor_charset (HTParentAnchor * me)
1.1       timbl     667: {
1.52      frystyk   668:     if (me && me->type_parameters) {
                    669:        char * charset = HTAssocList_findObject(me->type_parameters,"charset");
                    670:        return HTAtom_for(charset);
                    671:     }
                    672:     return NULL;
1.1       timbl     673: }
                    674: 
1.52      frystyk   675: PUBLIC BOOL HTAnchor_setCharset (HTParentAnchor * me, HTCharset charset)
1.1       timbl     676: {
1.52      frystyk   677:     return HTAnchor_addFormatParam(me, "charset", HTAtom_name(charset));
1.1       timbl     678: }
                    679: 
1.17      frystyk   680: /*
1.20      frystyk   681: **     Level parameter to Content-Type
                    682: */
1.35      frystyk   683: PUBLIC HTLevel HTAnchor_level (HTParentAnchor * me)
1.20      frystyk   684: {
1.52      frystyk   685:     if (me && me->type_parameters) {
                    686:        char * level = HTAssocList_findObject(me->type_parameters, "level");
                    687:        return HTAtom_for(level);
                    688:     }
                    689:     return NULL;
1.20      frystyk   690: }
                    691: 
1.52      frystyk   692: PUBLIC BOOL HTAnchor_setLevel (HTParentAnchor * me, HTLevel level)
1.20      frystyk   693: {
1.52      frystyk   694:     return HTAnchor_addFormatParam(me, "level", HTAtom_name(level));
1.20      frystyk   695: }
                    696: 
                    697: /*
1.17      frystyk   698: **     Content Encoding
                    699: */
1.47      frystyk   700: PUBLIC HTList * HTAnchor_encoding (HTParentAnchor * me)
1.1       timbl     701: {
1.17      frystyk   702:     return me ? me->content_encoding : NULL;
1.1       timbl     703: }
                    704: 
1.47      frystyk   705: PUBLIC BOOL HTAnchor_addEncoding (HTParentAnchor * me, HTEncoding  encoding)
1.17      frystyk   706: {
1.47      frystyk   707:     if (me && encoding) {
                    708:        if (!me->content_encoding) me->content_encoding = HTList_new();
                    709:        return HTList_addObject(me->content_encoding, encoding);
                    710:     }
                    711:     return NO;
1.17      frystyk   712: }
                    713: 
                    714: /*
1.21      frystyk   715: **     Content Language
                    716: */
1.47      frystyk   717: PUBLIC HTList * HTAnchor_language (HTParentAnchor * me)
1.21      frystyk   718: {
                    719:     return me ? me->content_language : NULL;
                    720: }
                    721: 
1.47      frystyk   722: PUBLIC BOOL HTAnchor_addLanguage (HTParentAnchor * me, HTLanguage  language)
1.21      frystyk   723: {
1.47      frystyk   724:     if (me && language) {
                    725:        if (!me->content_language) me->content_language = HTList_new();
                    726:        return HTList_addObject(me->content_language, language);
                    727:     }
                    728:     return NO;
1.21      frystyk   729: }
                    730: 
                    731: /*
1.17      frystyk   732: **     Content Transfer Encoding
1.1       timbl     733: */
1.48      frystyk   734: PUBLIC HTEncoding HTAnchor_transfer (HTParentAnchor * me)
1.17      frystyk   735: {
1.48      frystyk   736:     return me ? me->transfer : NULL;
1.17      frystyk   737: }
1.1       timbl     738: 
1.48      frystyk   739: PUBLIC void HTAnchor_setTransfer (HTParentAnchor * me, HTEncoding transfer)
1.17      frystyk   740: {
1.48      frystyk   741:     if (me) me->transfer = transfer;
1.17      frystyk   742: }
                    743: 
                    744: /*
                    745: **     Content Length
                    746: */
1.35      frystyk   747: PUBLIC long int HTAnchor_length (HTParentAnchor * me)
1.1       timbl     748: {
1.17      frystyk   749:     return me ? me->content_length : -1;
1.1       timbl     750: }
                    751: 
1.35      frystyk   752: PUBLIC void HTAnchor_setLength (HTParentAnchor * me, long int length)
1.17      frystyk   753: {
                    754:     if (me) me->content_length = length;
                    755: }
1.1       timbl     756: 
1.49      frystyk   757: PUBLIC void HTAnchor_addLength (HTParentAnchor * me, long int deltalength)
                    758: {
                    759:     if (me) {
                    760:        if (me->content_length < 0)
                    761:            me->content_length = deltalength;
                    762:        else
                    763:            me->content_length += deltalength;
                    764:     }
                    765: }
                    766: 
1.17      frystyk   767: /*
                    768: **     Allowed methods (Allow)
1.1       timbl     769: */
1.45      frystyk   770: PUBLIC HTMethod HTAnchor_methods (HTParentAnchor * me)
1.17      frystyk   771: {
1.32      frystyk   772:     return me ? me->methods : METHOD_INVALID;
1.17      frystyk   773: }
1.1       timbl     774: 
1.45      frystyk   775: PUBLIC void HTAnchor_setMethods (HTParentAnchor * me, HTMethod methodset)
1.1       timbl     776: {
1.17      frystyk   777:     if (me) me->methods = methodset;
1.1       timbl     778: }
                    779: 
1.45      frystyk   780: PUBLIC void HTAnchor_appendMethods (HTParentAnchor * me, HTMethod methodset)
1.1       timbl     781: {
1.32      frystyk   782:     if (me) me->methods |= methodset;
1.1       timbl     783: }
                    784: 
1.17      frystyk   785: /*
                    786: **     Title
1.2       timbl     787: */
1.46      frystyk   788: PUBLIC const char * HTAnchor_title  (HTParentAnchor * me)
1.1       timbl     789: {
1.17      frystyk   790:     return me ? me->title : NULL;
                    791: }
1.1       timbl     792: 
1.46      frystyk   793: PUBLIC void HTAnchor_setTitle (HTParentAnchor * me, const char * title)
1.17      frystyk   794: {
                    795:     if (me && title) StrAllocCopy(me->title, title);
1.2       timbl     796: }
                    797: 
1.46      frystyk   798: PUBLIC void HTAnchor_appendTitle (HTParentAnchor * me, const char * title)
1.17      frystyk   799: {
                    800:     if (me && title) StrAllocCat(me->title, title);
                    801: }
1.2       timbl     802: 
1.17      frystyk   803: /*
                    804: **     Version
1.2       timbl     805: */
1.41      frystyk   806: PUBLIC char * HTAnchor_version (HTParentAnchor * me)
1.17      frystyk   807: {
                    808:     return me ? me->version : NULL;
                    809: }
1.2       timbl     810: 
1.46      frystyk   811: PUBLIC void HTAnchor_setVersion (HTParentAnchor * me, const char * version)
1.2       timbl     812: {
1.17      frystyk   813:     if (me && version) StrAllocCopy(me->version, version);
1.2       timbl     814: }
                    815: 
1.17      frystyk   816: /*
                    817: **     Derived from
1.2       timbl     818: */
1.41      frystyk   819: PUBLIC char * HTAnchor_derived (HTParentAnchor * me)
1.17      frystyk   820: {
                    821:     return me ? me->derived_from : NULL;
                    822: }
                    823: 
1.46      frystyk   824: PUBLIC void HTAnchor_setDerived (HTParentAnchor * me, const char *derived_from)
1.17      frystyk   825: {
                    826:     if (me && derived_from) StrAllocCopy(me->derived_from, derived_from);
                    827: }
1.2       timbl     828: 
1.17      frystyk   829: /*
1.52      frystyk   830: **     Content MD5
                    831: */
                    832: PUBLIC char * HTAnchor_md5 (HTParentAnchor * me)
                    833: {
                    834:     return me ? me->content_md5 : NULL;
                    835: }
                    836: 
                    837: PUBLIC void HTAnchor_setMd5 (HTParentAnchor * me, const char * hash)
                    838: {
                    839:     if (me && hash) StrAllocCopy(me->content_md5, hash);
                    840: }
                    841: 
                    842: /*
1.28      frystyk   843: **     Date
                    844: */
1.41      frystyk   845: PUBLIC time_t HTAnchor_date (HTParentAnchor * me)
                    846: {
                    847:     return me ? me->date : -1;
                    848: }
                    849: 
1.46      frystyk   850: PUBLIC void HTAnchor_setDate (HTParentAnchor * me, const time_t date)
1.28      frystyk   851: {
1.41      frystyk   852:     if (me) me->date = date;
1.28      frystyk   853: }
                    854: 
                    855: /*
                    856: **     Expires
                    857: */
1.41      frystyk   858: PUBLIC time_t HTAnchor_expires (HTParentAnchor * me)
                    859: {
                    860:     return me ? me->expires : -1;
                    861: }
                    862: 
1.46      frystyk   863: PUBLIC void HTAnchor_setExpires (HTParentAnchor * me, const time_t expires)
1.28      frystyk   864: {
1.41      frystyk   865:     if (me) me->expires = expires;
1.28      frystyk   866: }
                    867: 
                    868: /*
                    869: **     Last Modified
                    870: */
1.41      frystyk   871: PUBLIC time_t HTAnchor_lastModified (HTParentAnchor * me)
                    872: {
                    873:     return me ? me->last_modified : -1;
                    874: }
                    875: 
1.46      frystyk   876: PUBLIC void HTAnchor_setLastModified (HTParentAnchor * me, const time_t lm)
1.28      frystyk   877: {
1.41      frystyk   878:     if (me) me->last_modified = lm;
1.28      frystyk   879: }
                    880: 
                    881: /*
1.52      frystyk   882: **     Entity Tag
                    883: */
                    884: PUBLIC char * HTAnchor_etag (HTParentAnchor * me)
                    885: {
                    886:     return me ? me->etag : NULL;
                    887: }
                    888: 
                    889: PUBLIC void HTAnchor_setEtag (HTParentAnchor * me, const char * etag)
                    890: {
                    891:     if (me && etag) StrAllocCopy(me->etag, etag);
                    892: }
                    893: 
                    894: PUBLIC BOOL HTAnchor_isEtagWeak (HTParentAnchor * me)
                    895: {
                    896:     return (me && me->etag && !strncasecomp(me->etag, "W/", 2));
                    897: }
                    898: 
                    899: /*
1.17      frystyk   900: **     Extra Header List of unknown headers
                    901: */
1.35      frystyk   902: PUBLIC HTList * HTAnchor_Extra  (HTParentAnchor * me)
1.2       timbl     903: {
1.17      frystyk   904:     return me ? me->extra_headers : NULL;
1.2       timbl     905: }
                    906: 
1.46      frystyk   907: PUBLIC void HTAnchor_addExtra (HTParentAnchor * me, const char * header)
1.2       timbl     908: {
1.17      frystyk   909:     if (me) {
1.18      frystyk   910:        char *newhead = NULL;
                    911:        StrAllocCopy(newhead, header);
1.17      frystyk   912:        if (!me->extra_headers)
                    913:            me->extra_headers = HTList_new();
1.18      frystyk   914:        HTList_addObject(me->extra_headers, (void *) newhead);
1.17      frystyk   915:     }
1.2       timbl     916: }
                    917: 
1.23      frystyk   918: /*
                    919: **     Has header been parsed?
1.2       timbl     920: */
1.35      frystyk   921: PUBLIC BOOL HTAnchor_headerParsed (HTParentAnchor * me)
1.2       timbl     922: {
1.17      frystyk   923:     return (me ? me->header_parsed : NO);
1.23      frystyk   924: }
                    925: 
1.35      frystyk   926: PUBLIC void HTAnchor_setHeaderParsed (HTParentAnchor * me)
1.23      frystyk   927: {
                    928:     if (me) me->header_parsed = YES;
1.2       timbl     929: }
                    930: 
1.17      frystyk   931: /*     Clear Header Information
                    932: **     ------------------------
                    933: */
1.35      frystyk   934: PUBLIC void HTAnchor_clearHeader (HTParentAnchor * me)
1.2       timbl     935: {
1.17      frystyk   936:     me->methods = METHOD_INVALID;
1.48      frystyk   937:     if (me->content_encoding) {
                    938:        HTList_delete(me->content_encoding);
                    939:        me->content_encoding = HTList_new();
                    940:     }
1.17      frystyk   941:     if (me->content_language) {
                    942:        HTList_delete(me->content_language);
                    943:        me->content_language = HTList_new();
1.9       frystyk   944:     }
1.51      frystyk   945:     HT_FREE(me->content_base);
                    946:     HT_FREE(me->content_location);
1.17      frystyk   947:     me->content_length = -1;                                     /* Invalid */
1.48      frystyk   948:     me->transfer = NULL;
1.17      frystyk   949:     me->content_type = WWW_UNKNOWN;
1.52      frystyk   950:     if (me->type_parameters) {
                    951:        HTAssocList_delete(me->type_parameters);
                    952:        me->type_parameters = NULL;
                    953:     }    
1.28      frystyk   954:     me->date = (time_t) -1;
                    955:     me->expires = (time_t) -1;
                    956:     me->last_modified = (time_t) -1;
1.17      frystyk   957:     
1.43      frystyk   958:     HT_FREE(me->derived_from);
                    959:     HT_FREE(me->version);
1.18      frystyk   960: 
                    961:     if (me->extra_headers) {
                    962:        HTList *cur = me->extra_headers;
                    963:        char *pres;
                    964:        while ((pres = (char *) HTList_nextObject(cur)))
1.43      frystyk   965:            HT_FREE(pres);
1.18      frystyk   966:        HTList_delete(me->extra_headers);
                    967:        me->extra_headers = NULL;
                    968:     }
1.17      frystyk   969:     me->header_parsed = NO;                                  /* All cleared */
1.1       timbl     970: }

Webmaster