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