Annotation of libwww/Library/src/HTAnchor.c, revision 1.60
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.60 ! frystyk 6: ** @(#) $Id: HTAnchor.c,v 1.59 1996/09/08 22:08:06 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.59 frystyk 53: newAnchor->age = (time_t) -1;
1.16 frystyk 54: return newAnchor;
1.1 timbl 55: }
56:
1.16 frystyk 57:
1.35 frystyk 58: PRIVATE HTChildAnchor * HTChildAnchor_new (void)
1.1 timbl 59: {
1.43 frystyk 60: HTChildAnchor *child;
61: if ((child = (HTChildAnchor *) HT_CALLOC(1, sizeof(HTChildAnchor))) == NULL)
62: HT_OUTOFMEM("HTChildAnchor_new");
1.33 frystyk 63: return child;
1.1 timbl 64: }
65:
1.17 frystyk 66: /* Create new or find old child anchor
67: ** -----------------------------------
1.1 timbl 68: **
1.3 timbl 69: ** Me one is for a new anchor being edited into an existing
1.17 frystyk 70: ** document. The parent anchor must already exist. All
71: ** children without tags (no NAME attribut) points to the same NULL
72: ** child.
1.54 frystyk 73: ** Children are now hashed for performance reasons. Thanks to
74: ** Michael Farrar
1.1 timbl 75: */
1.35 frystyk 76: PUBLIC HTChildAnchor * HTAnchor_findChild (HTParentAnchor * parent,
1.46 frystyk 77: const char * tag)
1.1 timbl 78: {
1.54 frystyk 79: HTChildAnchor * child = NULL;
80: HTList * kids = NULL;
1.17 frystyk 81: if (!parent) {
1.54 frystyk 82: if (ANCH_TRACE) HTTrace("Child Anchor Bad argument\n");
1.17 frystyk 83: return NULL;
84: }
1.1 timbl 85:
1.54 frystyk 86: /* Find a hash for this tag (if any) */
87: {
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: }
1.55 frystyk 97: if (!parent->children) {
98: if (!(parent->children = (HTList **)
1.54 frystyk 99: HT_CALLOC(CHILD_HASH_SIZE, sizeof(HTList *))))
100: HT_OUTOFMEM("HTAnchor_findChild");
101: }
1.55 frystyk 102: if (!parent->children[hash]) parent->children[hash] = HTList_new();
103: kids = parent->children[hash];
1.54 frystyk 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))) {
1.56 frystyk 110: if (child->tag && !strcmp(child->tag, tag)) {
1.54 frystyk 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.59 frystyk 320: HTAnchor_clearHeader(me);
1.57 frystyk 321:
1.43 frystyk 322: HT_FREE(me);
1.19 frystyk 323: return doc;
324: }
325:
326:
1.41 frystyk 327: /* Delete a parent anchor and all its children. If a hyperdoc object
328: ** is found hanging off the parent anchor then this is returned
1.19 frystyk 329: */
1.41 frystyk 330: PRIVATE void * delete_family (HTAnchor * me)
1.19 frystyk 331: {
332: HTParentAnchor *parent = me->parent;
333: if (ANCH_TRACE)
1.44 eric 334: HTTrace("AnchorDelete Remove parent %p and children\n", parent);
1.19 frystyk 335: if (!me) {
336: if (ANCH_TRACE)
1.44 eric 337: HTTrace("AnchorDelete No anchor found\n");
1.19 frystyk 338: return NULL;
339: }
340:
341: /* Delete children */
342: if (parent->children) {
1.54 frystyk 343: int cnt = 0;
344: for (; cnt<CHILD_HASH_SIZE; cnt++) {
345: HTList * kids = parent->children[cnt];
346: if (kids) {
347: HTChildAnchor * child;
348: while ((child=(HTChildAnchor*)HTList_removeLastObject(kids))) {
349: HT_FREE(child->tag);
350: if (child->links) {
351: HTList * cur = child->links;
352: HTLink * pres;
353: while ((pres = (HTLink *) HTList_nextObject(cur)))
354: HTLink_delete(pres);
355: HTList_delete(child->links);
356: }
357: HT_FREE(child);
358: }
359: HTList_delete(kids);
360: parent->children[cnt] = NULL;
1.26 frystyk 361: }
1.19 frystyk 362: }
363: }
364: return delete_parent(parent);
365: }
366:
367:
368: /* DELETE ALL ANCHORS
369: ** ------------------
370: ** Deletes all anchors and return a list of all the HyperDocs found.
371: ** It is for the application to delete any HyperDocs.
1.39 frystyk 372: ** If NULL then no hyperdocs are returned
1.19 frystyk 373: ** Return YES if OK, else NO
374: */
1.35 frystyk 375: PUBLIC BOOL HTAnchor_deleteAll (HTList * documents)
1.19 frystyk 376: {
377: int cnt;
378: HTList *cur;
1.39 frystyk 379: if (!adult_table)
1.19 frystyk 380: return NO;
381: for (cnt=0; cnt<HASH_SIZE; cnt++) {
382: if ((cur = adult_table[cnt])) {
383: HTParentAnchor *pres;
384: while ((pres = (HTParentAnchor *) HTList_nextObject(cur)) != NULL){
1.41 frystyk 385: void * doc = delete_family((HTAnchor *) pres);
386: if (doc && documents) HTList_addObject(documents, doc);
1.19 frystyk 387: }
388: }
389: HTList_delete(adult_table[cnt]);
390: }
1.43 frystyk 391: HT_FREE(adult_table);
1.19 frystyk 392: return YES;
393: }
394:
395:
1.54 frystyk 396: PRIVATE void delete_links (HTAnchor * me)
1.1 timbl 397: {
1.3 timbl 398: if (! me)
1.1 timbl 399: return;
400:
401: /* Recursively try to delete target anchors */
1.3 timbl 402: if (me->mainLink.dest) {
403: HTParentAnchor *parent = me->mainLink.dest->parent;
404: HTList_removeObject (parent->sources, me);
1.1 timbl 405: if (! parent->document) /* Test here to avoid calling overhead */
406: HTAnchor_delete (parent);
407: }
1.3 timbl 408: if (me->links) { /* Extra destinations */
1.1 timbl 409: HTLink *target;
1.12 frystyk 410: while ((target = (HTLink *) HTList_removeLastObject (me->links))) {
1.1 timbl 411: HTParentAnchor *parent = target->dest->parent;
1.3 timbl 412: HTList_removeObject (parent->sources, me);
1.1 timbl 413: if (! parent->document) /* Test here to avoid calling overhead */
414: HTAnchor_delete (parent);
415: }
416: }
417: }
418:
1.35 frystyk 419: PUBLIC BOOL HTAnchor_delete (HTParentAnchor * me)
1.1 timbl 420: {
1.54 frystyk 421: /* Don't delete if document is loaded */
422: if (!me || me->document) {
423: if (ANCH_TRACE) HTTrace("Anchor...... Not deleted\n");
424: return NO;
425: }
426:
427: /* Recursively try to delete target anchors */
428: delete_links ((HTAnchor *) me);
1.1 timbl 429:
1.54 frystyk 430: if (!HTList_isEmpty(me->sources)) { /* There are still incoming links */
431:
432: /*
433: ** Delete all outgoing links from children, if any
434: */
435: if (me->children) {
436: int cnt = 0;
437: for (; cnt<CHILD_HASH_SIZE; cnt++) {
438: HTList * kids = me->children[cnt];
439: if (kids) {
440: HTChildAnchor * child;
441: while ((child = (HTChildAnchor *) HTList_nextObject(kids)))
442: delete_links((HTAnchor *) child);
443: return NO; /* Parent not deleted */
444: }
445: }
446: }
1.1 timbl 447:
1.54 frystyk 448: /*
449: ** No more incoming links : kill everything
450: ** First, recursively delete children
451: */
452: if (me->children) {
453: int cnt = 0;
454: for (; cnt<CHILD_HASH_SIZE; cnt++) {
455: HTList * kids = me->children[cnt];
456: if (kids) {
457: HTChildAnchor * child;
458: while ((child=(HTChildAnchor *) HTList_removeLastObject(kids)))
459: delete_links((HTAnchor *) child);
460: HT_FREE(child->tag);
461: HT_FREE(child);
462: }
463: }
464: }
465: }
1.1 timbl 466:
1.54 frystyk 467: /* Now kill myself */
468: delete_parent(me);
469: return YES; /* Parent deleted */
470: #if 0
1.3 timbl 471: if (! HTList_isEmpty (me->sources)) { /* There are still incoming links */
1.1 timbl 472: /* Delete all outgoing links from children, if any */
1.3 timbl 473: HTList *kids = me->children;
1.12 frystyk 474: while ((child = (HTChildAnchor *) HTList_nextObject (kids)))
1.54 frystyk 475: delete_links ((HTAnchor *) child);
1.1 timbl 476: return NO; /* Parent not deleted */
477: }
478:
479: /* No more incoming links : kill everything */
480: /* First, recursively delete children */
1.12 frystyk 481: while ((child = (HTChildAnchor *) HTList_removeLastObject (me->children))) {
1.54 frystyk 482: delete_links ((HTAnchor *) child);
1.43 frystyk 483: HT_FREE(child->tag);
484: HT_FREE(child);
1.1 timbl 485: }
1.54 frystyk 486: #endif
1.1 timbl 487: }
488:
1.17 frystyk 489: /* ------------------------------------------------------------------------- */
490: /* Data Access Methods */
491: /* ------------------------------------------------------------------------- */
1.1 timbl 492:
1.35 frystyk 493: PUBLIC HTParentAnchor * HTAnchor_parent (HTAnchor * me)
1.1 timbl 494: {
1.17 frystyk 495: return me ? me->parent : NULL;
1.1 timbl 496: }
497:
1.41 frystyk 498: PUBLIC void HTAnchor_setDocument (HTParentAnchor * me, void * doc)
1.1 timbl 499: {
1.41 frystyk 500: if (me) me->document = doc;
1.1 timbl 501: }
502:
1.41 frystyk 503: PUBLIC void * HTAnchor_document (HTParentAnchor * me)
1.1 timbl 504: {
1.17 frystyk 505: return me ? me->document : NULL;
1.1 timbl 506: }
507:
1.53 frystyk 508: PUBLIC char * HTAnchor_address (HTAnchor * me)
509: {
510: char *addr = NULL;
511: if (me) {
512: if (((HTParentAnchor *) me == me->parent) ||
513: !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
514: StrAllocCopy (addr, me->parent->address);
515: }
516: else { /* it's a named child */
517: if ((addr = (char *) HT_MALLOC(2 + strlen (me->parent->address) + \
518: strlen (((HTChildAnchor *) me)->tag))) == NULL)
519: HT_OUTOFMEM("HTAnchor_address");
520: sprintf (addr, "%s#%s", me->parent->address,
521: ((HTChildAnchor *) me)->tag);
522: }
523: }
524: return addr;
525: }
526:
1.51 frystyk 527: /*
1.53 frystyk 528: ** We resolve the child address with respect to either a base URL,
529: ** a content-location, or to the request-URI
1.51 frystyk 530: */
1.53 frystyk 531: PUBLIC char * HTAnchor_expandedAddress (HTAnchor * me)
1.1 timbl 532: {
1.17 frystyk 533: char *addr = NULL;
534: if (me) {
1.53 frystyk 535: HTParentAnchor * parent = me->parent;
536: char * base = parent->content_location ? parent->content_location :
537: parent->content_base ? parent->content_base : parent->address;
1.17 frystyk 538: if (((HTParentAnchor *) me == me->parent) ||
539: !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
1.52 frystyk 540: StrAllocCopy(addr, base);
1.51 frystyk 541: } else { /* it's a named child */
1.52 frystyk 542: if ((addr = (char *) HT_MALLOC(2 + strlen(base) + strlen(((HTChildAnchor *) me)->tag))) == NULL)
1.43 frystyk 543: HT_OUTOFMEM("HTAnchor_address");
1.52 frystyk 544: sprintf (addr, "%s#%s", base, ((HTChildAnchor *) me)->tag);
1.17 frystyk 545: }
1.1 timbl 546: }
1.17 frystyk 547: return addr;
1.1 timbl 548: }
549:
1.52 frystyk 550: /* Physical Address
551: ** ----------------
552: */
553: PUBLIC char * HTAnchor_physical (HTParentAnchor * me)
554: {
1.53 frystyk 555: return me ? me->physical ? me->physical : me->address : NULL;
1.52 frystyk 556: }
557:
1.53 frystyk 558: PUBLIC void HTAnchor_setPhysical (HTParentAnchor * me, char * physical)
1.52 frystyk 559: {
560: if (!me || !physical) {
561: if (ANCH_TRACE)
562: HTTrace("HTAnchor.... setPhysical, called with null argument\n");
563: return;
564: }
565: StrAllocCopy(me->physical, physical);
566: }
567:
1.53 frystyk 568: PUBLIC void HTAnchor_clearPhysical(HTParentAnchor * me)
569: {
570: if (me) HT_FREE(me->physical);
571: }
572:
573: /*
574: ** Children information
575: */
1.35 frystyk 576: PUBLIC BOOL HTAnchor_hasChildren (HTParentAnchor * me)
1.17 frystyk 577: {
1.54 frystyk 578: return (me && me->children);
1.17 frystyk 579: }
1.1 timbl 580:
1.59 frystyk 581: /*
582: ** Check whether we can cache this object or not.
1.52 frystyk 583: */
1.59 frystyk 584: PUBLIC BOOL HTAnchor_cachable (HTParentAnchor * me)
1.52 frystyk 585: {
1.59 frystyk 586: if (me) {
587:
588: /* We may already have decided that this object is not cachable */
589: if (me->cachable == NO) return NO;
590:
591: /* We don't cache negotiated resources for the moment */
592: if (me->variants) return NO;
593:
594: /*
595: ** Check if we should cache this object or not. We are very liberale
596: ** in that we cache everything except if we explicit are told not to
597: ** cache (no-store, no-cache). In all other cases we can get around
598: ** it by forcing revalidation
599: */
600: if (me->cache_control) {
601: char * token;
602: if ((token=HTAssocList_findObject(me->cache_control, "no-store")))
603: return NO;
604: if ((token=HTAssocList_findObject(me->cache_control, "no-cache")))
605: if (!*token) return NO;
606: }
607:
608: /* Cache everything else */
609: return YES;
610: }
611: return NO;
1.52 frystyk 612: }
613:
1.59 frystyk 614: PUBLIC BOOL HTAnchor_setCachable (HTParentAnchor * me, BOOL mode)
1.52 frystyk 615: {
1.59 frystyk 616: if (me) {
617: me->cachable = mode;
618: return YES;
619: }
620: return NO;
1.52 frystyk 621: }
622:
623: /* ------------------------------------------------------------------------- */
624: /* Entity Header Information */
625: /* ------------------------------------------------------------------------- */
626:
627: /*
628: ** Variants. If this anchor has any variants then keep them in a list
629: ** so that we can find them later. The list is simply a list of
630: ** parent anchors.
631: */
632: PUBLIC HTList * HTAnchor_variants (HTParentAnchor * me)
1.17 frystyk 633: {
1.52 frystyk 634: return me ? me->variants : NULL;
1.17 frystyk 635: }
1.1 timbl 636:
1.52 frystyk 637: PUBLIC BOOL HTAnchor_addVariant (HTParentAnchor * me,
638: HTParentAnchor * variant)
1.1 timbl 639: {
1.52 frystyk 640: if (me && variant) {
641: if (!me->variants) me->variants = HTList_new();
642: return HTList_addObject(me->variants, variant);
643: }
644: return NO;
1.17 frystyk 645: }
646:
1.52 frystyk 647: PUBLIC BOOL HTAnchor_deleteVariant (HTParentAnchor * me,
648: HTParentAnchor * variant)
1.17 frystyk 649: {
1.52 frystyk 650: return (me && variant) ? HTList_removeObject(me->variants, variant) : NO;
1.9 frystyk 651: }
1.1 timbl 652:
1.52 frystyk 653: /*
1.59 frystyk 654: ** Cache control directives that we haev received
655: */
656: PUBLIC BOOL HTAnchor_addCacheControl (HTParentAnchor * anchor,
657: char * token, char * value)
658: {
659: if (anchor) {
660: if (!anchor->cache_control) anchor->cache_control = HTAssocList_new();
661: return HTAssocList_addObject(anchor->cache_control, token, value);
662: }
663: return NO;
664: }
665:
666: PUBLIC BOOL HTAnchor_deleteCacheControl (HTParentAnchor * anchor)
667: {
668: if (anchor && anchor->cache_control) {
669: HTAssocList_delete(anchor->cache_control);
670: anchor->cache_control = NULL;
671: return YES;
672: }
673: return NO;
674: }
675:
676: PUBLIC HTAssocList * HTAnchor_cacheControl (HTParentAnchor * anchor)
677: {
678: return (anchor ? anchor->cache_control : NULL);
679: }
680:
681: PUBLIC time_t HTAnchor_maxAge (HTParentAnchor * anchor)
682: {
683: if (anchor && anchor->cache_control) {
684: char * token = HTAssocList_findObject(anchor->cache_control, "max-age");
685: if (token) return atol(token);
686: }
687: return (time_t) -1;
688: }
689:
690: PUBLIC BOOL HTAnchor_mustRevalidate (HTParentAnchor * anchor)
691: {
692: return anchor && anchor->cache_control &&
693: (HTAssocList_findObject(anchor->cache_control, "must-revalidate") != NULL);
694: }
695:
696: PUBLIC char * HTAnchor_noCache (HTParentAnchor * anchor)
697: {
698: return (anchor && anchor->cache_control) ?
699: HTAssocList_findObject(anchor->cache_control, "no-cache") : NULL;
700: }
701:
702: /*
1.52 frystyk 703: ** Is this resource an index?
1.17 frystyk 704: */
1.52 frystyk 705: PUBLIC void HTAnchor_clearIndex (HTParentAnchor * me)
706: {
707: if (me) me->isIndex = NO;
708: }
1.17 frystyk 709:
1.52 frystyk 710: PUBLIC void HTAnchor_setIndex (HTParentAnchor * me)
1.1 timbl 711: {
1.52 frystyk 712: if (me) me->isIndex = YES;
1.1 timbl 713: }
714:
1.52 frystyk 715: PUBLIC BOOL HTAnchor_isIndex (HTParentAnchor * me)
1.1 timbl 716: {
1.52 frystyk 717: return me ? me->isIndex : NO;
1.27 frystyk 718: }
719:
1.52 frystyk 720: /* Content Base
1.51 frystyk 721: ** ------------
722: */
723: PUBLIC char * HTAnchor_base (HTParentAnchor * me)
724: {
725: return me ? me->content_base : NULL;
726: }
727:
728: PUBLIC BOOL HTAnchor_setBase (HTParentAnchor * me, char * base)
729: {
730: if (!me || !base) {
731: if (ANCH_TRACE)
732: HTTrace("HTAnchor.... set base called with null argument\n");
733: return NO;
734: }
735: StrAllocCopy(me->content_base, base);
736: return YES;
737: }
738:
1.52 frystyk 739: /* Content Location
740: ** ----------------
1.27 frystyk 741: */
1.52 frystyk 742: PUBLIC char * HTAnchor_location (HTParentAnchor * me)
1.27 frystyk 743: {
1.52 frystyk 744: return me ? me->content_location : NULL;
1.27 frystyk 745: }
746:
1.53 frystyk 747: /*
748: ** Expand the location relative to the base URL if any, otherwise the
749: ** anchor address it self
750: */
1.52 frystyk 751: PUBLIC BOOL HTAnchor_setLocation (HTParentAnchor * me, char * location)
1.27 frystyk 752: {
1.53 frystyk 753: if (me && location) {
754: char * base = me->content_base ? me->content_base : me->address;
755: me->content_location = HTParse(location, base, PARSE_ALL);
756: return YES;
1.52 frystyk 757: }
1.53 frystyk 758: return NO;
1.1 timbl 759: }
760:
1.52 frystyk 761: /* Content-Type
762: ** ------------
1.17 frystyk 763: */
1.35 frystyk 764: PUBLIC HTFormat HTAnchor_format (HTParentAnchor * me)
1.17 frystyk 765: {
766: return me ? me->content_type : NULL;
767: }
1.1 timbl 768:
1.35 frystyk 769: PUBLIC void HTAnchor_setFormat (HTParentAnchor * me, HTFormat form)
1.1 timbl 770: {
1.17 frystyk 771: if (me) me->content_type = form;
1.1 timbl 772: }
773:
1.52 frystyk 774: PUBLIC HTAssocList * HTAnchor_formatParam (HTParentAnchor * me)
775: {
776: return me ? me->type_parameters : NULL;
777: }
778:
779: PUBLIC BOOL HTAnchor_addFormatParam (HTParentAnchor * me,
780: const char * name, const char * value)
781: {
782: if (me) {
783: if (!me->type_parameters) me->type_parameters = HTAssocList_new();
784: return HTAssocList_replaceObject(me->type_parameters, name, value);
785: }
786: return NO;
787: }
788:
1.17 frystyk 789: /*
790: ** Charset parameter to Content-Type
1.1 timbl 791: */
1.35 frystyk 792: PUBLIC HTCharset HTAnchor_charset (HTParentAnchor * me)
1.1 timbl 793: {
1.52 frystyk 794: if (me && me->type_parameters) {
795: char * charset = HTAssocList_findObject(me->type_parameters,"charset");
796: return HTAtom_for(charset);
797: }
798: return NULL;
1.1 timbl 799: }
800:
1.52 frystyk 801: PUBLIC BOOL HTAnchor_setCharset (HTParentAnchor * me, HTCharset charset)
1.1 timbl 802: {
1.52 frystyk 803: return HTAnchor_addFormatParam(me, "charset", HTAtom_name(charset));
1.1 timbl 804: }
805:
1.17 frystyk 806: /*
1.20 frystyk 807: ** Level parameter to Content-Type
808: */
1.35 frystyk 809: PUBLIC HTLevel HTAnchor_level (HTParentAnchor * me)
1.20 frystyk 810: {
1.52 frystyk 811: if (me && me->type_parameters) {
812: char * level = HTAssocList_findObject(me->type_parameters, "level");
813: return HTAtom_for(level);
814: }
815: return NULL;
1.20 frystyk 816: }
817:
1.52 frystyk 818: PUBLIC BOOL HTAnchor_setLevel (HTParentAnchor * me, HTLevel level)
1.20 frystyk 819: {
1.52 frystyk 820: return HTAnchor_addFormatParam(me, "level", HTAtom_name(level));
1.20 frystyk 821: }
822:
823: /*
1.17 frystyk 824: ** Content Encoding
825: */
1.47 frystyk 826: PUBLIC HTList * HTAnchor_encoding (HTParentAnchor * me)
1.1 timbl 827: {
1.17 frystyk 828: return me ? me->content_encoding : NULL;
1.1 timbl 829: }
830:
1.47 frystyk 831: PUBLIC BOOL HTAnchor_addEncoding (HTParentAnchor * me, HTEncoding encoding)
1.17 frystyk 832: {
1.47 frystyk 833: if (me && encoding) {
834: if (!me->content_encoding) me->content_encoding = HTList_new();
835: return HTList_addObject(me->content_encoding, encoding);
836: }
837: return NO;
1.17 frystyk 838: }
839:
840: /*
1.21 frystyk 841: ** Content Language
842: */
1.47 frystyk 843: PUBLIC HTList * HTAnchor_language (HTParentAnchor * me)
1.21 frystyk 844: {
845: return me ? me->content_language : NULL;
846: }
847:
1.47 frystyk 848: PUBLIC BOOL HTAnchor_addLanguage (HTParentAnchor * me, HTLanguage language)
1.21 frystyk 849: {
1.47 frystyk 850: if (me && language) {
851: if (!me->content_language) me->content_language = HTList_new();
852: return HTList_addObject(me->content_language, language);
853: }
854: return NO;
1.21 frystyk 855: }
856:
857: /*
1.17 frystyk 858: ** Content Transfer Encoding
1.1 timbl 859: */
1.48 frystyk 860: PUBLIC HTEncoding HTAnchor_transfer (HTParentAnchor * me)
1.17 frystyk 861: {
1.48 frystyk 862: return me ? me->transfer : NULL;
1.17 frystyk 863: }
1.1 timbl 864:
1.48 frystyk 865: PUBLIC void HTAnchor_setTransfer (HTParentAnchor * me, HTEncoding transfer)
1.17 frystyk 866: {
1.48 frystyk 867: if (me) me->transfer = transfer;
1.17 frystyk 868: }
869:
870: /*
871: ** Content Length
872: */
1.35 frystyk 873: PUBLIC long int HTAnchor_length (HTParentAnchor * me)
1.1 timbl 874: {
1.17 frystyk 875: return me ? me->content_length : -1;
1.1 timbl 876: }
877:
1.35 frystyk 878: PUBLIC void HTAnchor_setLength (HTParentAnchor * me, long int length)
1.17 frystyk 879: {
880: if (me) me->content_length = length;
881: }
1.1 timbl 882:
1.49 frystyk 883: PUBLIC void HTAnchor_addLength (HTParentAnchor * me, long int deltalength)
884: {
885: if (me) {
886: if (me->content_length < 0)
887: me->content_length = deltalength;
888: else
889: me->content_length += deltalength;
890: }
891: }
892:
1.17 frystyk 893: /*
894: ** Allowed methods (Allow)
1.1 timbl 895: */
1.45 frystyk 896: PUBLIC HTMethod HTAnchor_methods (HTParentAnchor * me)
1.17 frystyk 897: {
1.32 frystyk 898: return me ? me->methods : METHOD_INVALID;
1.17 frystyk 899: }
1.1 timbl 900:
1.45 frystyk 901: PUBLIC void HTAnchor_setMethods (HTParentAnchor * me, HTMethod methodset)
1.1 timbl 902: {
1.17 frystyk 903: if (me) me->methods = methodset;
1.1 timbl 904: }
905:
1.45 frystyk 906: PUBLIC void HTAnchor_appendMethods (HTParentAnchor * me, HTMethod methodset)
1.1 timbl 907: {
1.32 frystyk 908: if (me) me->methods |= methodset;
1.1 timbl 909: }
910:
1.17 frystyk 911: /*
912: ** Title
1.2 timbl 913: */
1.46 frystyk 914: PUBLIC const char * HTAnchor_title (HTParentAnchor * me)
1.1 timbl 915: {
1.17 frystyk 916: return me ? me->title : NULL;
917: }
1.1 timbl 918:
1.46 frystyk 919: PUBLIC void HTAnchor_setTitle (HTParentAnchor * me, const char * title)
1.17 frystyk 920: {
921: if (me && title) StrAllocCopy(me->title, title);
1.2 timbl 922: }
923:
1.46 frystyk 924: PUBLIC void HTAnchor_appendTitle (HTParentAnchor * me, const char * title)
1.17 frystyk 925: {
926: if (me && title) StrAllocCat(me->title, title);
927: }
1.2 timbl 928:
1.17 frystyk 929: /*
930: ** Version
1.2 timbl 931: */
1.41 frystyk 932: PUBLIC char * HTAnchor_version (HTParentAnchor * me)
1.17 frystyk 933: {
934: return me ? me->version : NULL;
935: }
1.2 timbl 936:
1.46 frystyk 937: PUBLIC void HTAnchor_setVersion (HTParentAnchor * me, const char * version)
1.2 timbl 938: {
1.17 frystyk 939: if (me && version) StrAllocCopy(me->version, version);
1.2 timbl 940: }
941:
1.17 frystyk 942: /*
943: ** Derived from
1.2 timbl 944: */
1.41 frystyk 945: PUBLIC char * HTAnchor_derived (HTParentAnchor * me)
1.17 frystyk 946: {
947: return me ? me->derived_from : NULL;
948: }
949:
1.46 frystyk 950: PUBLIC void HTAnchor_setDerived (HTParentAnchor * me, const char *derived_from)
1.17 frystyk 951: {
952: if (me && derived_from) StrAllocCopy(me->derived_from, derived_from);
953: }
1.2 timbl 954:
1.17 frystyk 955: /*
1.52 frystyk 956: ** Content MD5
957: */
958: PUBLIC char * HTAnchor_md5 (HTParentAnchor * me)
959: {
960: return me ? me->content_md5 : NULL;
961: }
962:
963: PUBLIC void HTAnchor_setMd5 (HTParentAnchor * me, const char * hash)
964: {
965: if (me && hash) StrAllocCopy(me->content_md5, hash);
966: }
967:
968: /*
1.28 frystyk 969: ** Date
970: */
1.41 frystyk 971: PUBLIC time_t HTAnchor_date (HTParentAnchor * me)
972: {
973: return me ? me->date : -1;
974: }
975:
1.46 frystyk 976: PUBLIC void HTAnchor_setDate (HTParentAnchor * me, const time_t date)
1.28 frystyk 977: {
1.41 frystyk 978: if (me) me->date = date;
1.28 frystyk 979: }
980:
981: /*
982: ** Expires
983: */
1.41 frystyk 984: PUBLIC time_t HTAnchor_expires (HTParentAnchor * me)
985: {
986: return me ? me->expires : -1;
987: }
988:
1.46 frystyk 989: PUBLIC void HTAnchor_setExpires (HTParentAnchor * me, const time_t expires)
1.28 frystyk 990: {
1.41 frystyk 991: if (me) me->expires = expires;
1.28 frystyk 992: }
993:
994: /*
995: ** Last Modified
996: */
1.41 frystyk 997: PUBLIC time_t HTAnchor_lastModified (HTParentAnchor * me)
998: {
999: return me ? me->last_modified : -1;
1000: }
1001:
1.46 frystyk 1002: PUBLIC void HTAnchor_setLastModified (HTParentAnchor * me, const time_t lm)
1.28 frystyk 1003: {
1.41 frystyk 1004: if (me) me->last_modified = lm;
1.28 frystyk 1005: }
1006:
1007: /*
1.59 frystyk 1008: ** Age
1009: */
1010: PUBLIC time_t HTAnchor_age (HTParentAnchor * me)
1011: {
1012: return me ? me->age : -1;
1013: }
1014:
1015: PUBLIC void HTAnchor_setAge (HTParentAnchor * me, const time_t age)
1016: {
1017: if (me) me->age = age;
1018: }
1019:
1020: /*
1.52 frystyk 1021: ** Entity Tag
1022: */
1023: PUBLIC char * HTAnchor_etag (HTParentAnchor * me)
1024: {
1025: return me ? me->etag : NULL;
1026: }
1027:
1028: PUBLIC void HTAnchor_setEtag (HTParentAnchor * me, const char * etag)
1029: {
1030: if (me && etag) StrAllocCopy(me->etag, etag);
1031: }
1032:
1033: PUBLIC BOOL HTAnchor_isEtagWeak (HTParentAnchor * me)
1034: {
1035: return (me && me->etag && !strncasecomp(me->etag, "W/", 2));
1036: }
1037:
1.59 frystyk 1038: #if 0
1.52 frystyk 1039: /*
1.17 frystyk 1040: ** Extra Header List of unknown headers
1041: */
1.60 ! frystyk 1042: PRIVATE HTList * HTAnchor_Extra (HTParentAnchor * me)
1.2 timbl 1043: {
1.17 frystyk 1044: return me ? me->extra_headers : NULL;
1.2 timbl 1045: }
1046:
1.60 ! frystyk 1047: PRIVATE void HTAnchor_addExtra (HTParentAnchor * me, const char * header)
1.2 timbl 1048: {
1.17 frystyk 1049: if (me) {
1.18 frystyk 1050: char *newhead = NULL;
1051: StrAllocCopy(newhead, header);
1.17 frystyk 1052: if (!me->extra_headers)
1053: me->extra_headers = HTList_new();
1.18 frystyk 1054: HTList_addObject(me->extra_headers, (void *) newhead);
1.17 frystyk 1055: }
1.2 timbl 1056: }
1.59 frystyk 1057: #endif
1.2 timbl 1058:
1.23 frystyk 1059: /*
1.59 frystyk 1060: ** Validate anchor values and finish up parsing
1.2 timbl 1061: */
1.59 frystyk 1062: PUBLIC void HTAnchor_setHeaderParsed (HTParentAnchor * me)
1.2 timbl 1063: {
1.59 frystyk 1064: if (me) {
1065:
1066: /*
1067: ** If the server did not send a date then use the current time
1068: */
1069: if (me->date < 0) me->date = time(NULL);
1070:
1071: /*
1072: ** If we don't get a Last-Modified header then set it to date
1073: */
1074: if (me->last_modified < 0) me->last_modified = me->date;
1075: me->header_parsed = YES;
1076: }
1.23 frystyk 1077: }
1078:
1.59 frystyk 1079: PUBLIC BOOL HTAnchor_headerParsed (HTParentAnchor * me)
1.23 frystyk 1080: {
1.59 frystyk 1081: return (me ? me->header_parsed : NO);
1.2 timbl 1082: }
1083:
1.17 frystyk 1084: /* Clear Header Information
1085: ** ------------------------
1086: */
1.35 frystyk 1087: PUBLIC void HTAnchor_clearHeader (HTParentAnchor * me)
1.2 timbl 1088: {
1.17 frystyk 1089: me->methods = METHOD_INVALID;
1.48 frystyk 1090: if (me->content_encoding) {
1091: HTList_delete(me->content_encoding);
1092: me->content_encoding = HTList_new();
1093: }
1.17 frystyk 1094: if (me->content_language) {
1095: HTList_delete(me->content_language);
1096: me->content_language = HTList_new();
1.9 frystyk 1097: }
1.51 frystyk 1098: HT_FREE(me->content_base);
1099: HT_FREE(me->content_location);
1.17 frystyk 1100: me->content_length = -1; /* Invalid */
1.48 frystyk 1101: me->transfer = NULL;
1.57 frystyk 1102:
1.59 frystyk 1103: /* Delete the title */
1104: HT_FREE(me->title);
1105:
1.57 frystyk 1106: /* Clear the content type */
1.17 frystyk 1107: me->content_type = WWW_UNKNOWN;
1.52 frystyk 1108: if (me->type_parameters) {
1109: HTAssocList_delete(me->type_parameters);
1110: me->type_parameters = NULL;
1111: }
1.57 frystyk 1112:
1.59 frystyk 1113: /* Cache controls */
1114: me->cachable = NO;
1115: if (me->cache_control) {
1116: HTAssocList_delete(me->cache_control);
1117: me->cache_control = NULL;
1118: }
1119:
1.57 frystyk 1120: /* Dates etc. */
1.28 frystyk 1121: me->date = (time_t) -1;
1122: me->expires = (time_t) -1;
1123: me->last_modified = (time_t) -1;
1.59 frystyk 1124: me->age = (time_t) -1;
1.17 frystyk 1125:
1.43 frystyk 1126: HT_FREE(me->derived_from);
1127: HT_FREE(me->version);
1.58 frystyk 1128: HT_FREE(me->etag);
1.18 frystyk 1129:
1.59 frystyk 1130: #if 0
1.18 frystyk 1131: if (me->extra_headers) {
1132: HTList *cur = me->extra_headers;
1133: char *pres;
1134: while ((pres = (char *) HTList_nextObject(cur)))
1.43 frystyk 1135: HT_FREE(pres);
1.18 frystyk 1136: HTList_delete(me->extra_headers);
1137: me->extra_headers = NULL;
1138: }
1.17 frystyk 1139: me->header_parsed = NO; /* All cleared */
1.59 frystyk 1140: #endif
1.1 timbl 1141: }
Webmaster