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