1 /* HTAnchor.c
2 ** HYPERTEXT "ANCHOR" OBJECT
3 **
4 ** (c) COPYRIGHT MIT 1995.
5 ** Please first read the full copyright statement in the file COPYRIGH.
6 ** @(#) $Id$
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.
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
15 ** 3-May-1995 (HF), Added a lot of methods and other stuff made an object
16 ** July 1996 Patch for adding hash of children Michael Farrar
17 */
18
19 /* Library include files */
20 #include "wwwsys.h"
21 #include "WWWUtil.h"
22 #include "HTFormat.h"
23 #include "HTParse.h"
24 #include "HTMethod.h"
25 #include "HTWWWStr.h"
26 #include "HTAncMan.h" /* Implemented here */
27
28 #define PARENT_HASH_SIZE HT_XL_HASH_SIZE
29 #define CHILD_HASH_SIZE HT_L_HASH_SIZE
30
31 PRIVATE HTList **adult_table=0; /* Point to table of lists of all parents */
32
33 /* ------------------------------------------------------------------------- */
34 /* Creation Methods */
35 /* ------------------------------------------------------------------------- */
36
37 /*
38 ** Do not use "new" by itself outside this module. In order to enforce
39 ** consistency, we insist that you furnish more information about the
40 ** anchor you are creating : use newWithParent or newWithAddress.
41 */
HTParentAnchor_new(void)42 PRIVATE HTParentAnchor * HTParentAnchor_new (void)
43 {
44 HTParentAnchor *newAnchor;
45 if ((newAnchor = (HTParentAnchor *) HT_CALLOC(1, sizeof (HTParentAnchor))) == NULL)
46 HT_OUTOFMEM("HTParentAnchor_new");
47 newAnchor->parent = newAnchor;
48 newAnchor->content_type = WWW_UNKNOWN;
49 newAnchor->mainLink.method = METHOD_INVALID;
50 newAnchor->content_length = -1; /* howcome 6 dec 95 */
51 newAnchor->date = (time_t) -1;
52 newAnchor->expires = (time_t) -1;
53 newAnchor->last_modified = (time_t) -1;
54 newAnchor->age = (time_t) -1;
55 return newAnchor;
56 }
57
58
HTChildAnchor_new(void)59 PRIVATE HTChildAnchor * HTChildAnchor_new (void)
60 {
61 HTChildAnchor *child;
62 if ((child = (HTChildAnchor *) HT_CALLOC(1, sizeof(HTChildAnchor))) == NULL)
63 HT_OUTOFMEM("HTChildAnchor_new");
64 return child;
65 }
66
67 /* Create new or find old child anchor
68 ** -----------------------------------
69 **
70 ** Me one is for a new anchor being edited into an existing
71 ** document. The parent anchor must already exist. All
72 ** children without tags (no NAME attribut) points to the same NULL
73 ** child.
74 ** Children are now hashed for performance reasons. Thanks to
75 ** Michael Farrar
76 */
HTAnchor_findChild(HTParentAnchor * parent,const char * tag)77 PUBLIC HTChildAnchor * HTAnchor_findChild (HTParentAnchor * parent,
78 const char * tag)
79 {
80 HTChildAnchor * child = NULL;
81 HTList * kids = NULL;
82 if (!parent) {
83 HTTRACE(ANCH_TRACE, "Child Anchor Bad argument\n");
84 return NULL;
85 }
86
87 /* Find a hash for this tag (if any) */
88 {
89 int hash = 0;
90 /*
91 ** If tag is empty then use hash value 0
92 */
93 if (tag) {
94 const char * ptr = tag;
95 for(; *ptr; ptr++)
96 hash = (int) ((hash*3 + (*(unsigned char*)ptr)) % CHILD_HASH_SIZE);
97 }
98 if (!parent->children) {
99 if (!(parent->children = (HTList **)
100 HT_CALLOC(CHILD_HASH_SIZE, sizeof(HTList *))))
101 HT_OUTOFMEM("HTAnchor_findChild");
102 }
103 if (!parent->children[hash]) parent->children[hash] = HTList_new();
104 kids = parent->children[hash];
105 }
106
107 /* First search list of children to see if tag is already there */
108 if (tag && *tag) {
109 HTList * cur = kids;
110 while ((child = (HTChildAnchor *) HTList_nextObject(cur))) {
111 if (child->tag && !strcmp(child->tag, tag)) {
112 HTTRACE(ANCH_TRACE, "Child Anchor %p of parent %p with name `%s' already exists.\n" _
113 (void *) child _ (void *) parent _ tag);
114 return child;
115 }
116 }
117 }
118
119 /* If not found then create a new child anchor */
120 child = HTChildAnchor_new();
121 HTList_addObject(kids, (void *) child);
122 child->parent = parent;
123 if (tag) StrAllocCopy(child->tag, tag);
124 HTTRACE(ANCH_TRACE, "Child Anchor New Anchor %p named `%s' is child of %p\n" _
125 (void *) child _ tag ? tag : (const char *) "" _ (void *)parent);
126 return child;
127 }
128
129
130 /* Create new or find old named anchor
131 ** -----------------------------------
132 **
133 ** Me one is for a reference which is found in a document, and might
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 */
HTAnchor_findAddress(const char * address)138 PUBLIC HTAnchor * HTAnchor_findAddress (const char * address)
139 {
140 char *tag = HTParse (address, "", PARSE_VIEW); /* Any tags? */
141
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) {
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);
149 HT_FREE(addr);
150 HT_FREE(tag);
151 return (HTAnchor *) child;
152 } else { /* Else check whether we have this node */
153 int hash;
154 const char *p;
155 HTList * adults;
156 HTList *grownups;
157 HTParentAnchor * foundAnchor;
158 char *newaddr = NULL;
159 StrAllocCopy(newaddr, address); /* Get our own copy */
160 HT_FREE(tag);
161 newaddr = HTSimplify(&newaddr);
162
163 /* Select list from hash table */
164 for(p=newaddr, hash=0; *p; p++)
165 hash = (int) ((hash * 3 + (*(unsigned char*)p)) % PARENT_HASH_SIZE);
166 if (!adult_table) {
167 if ((adult_table = (HTList* *) HT_CALLOC(PARENT_HASH_SIZE, sizeof(HTList*))) == NULL)
168 HT_OUTOFMEM("HTAnchor_findAddress");
169 }
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))){
176 if (!strcmp(foundAnchor->address, newaddr)) {
177 HTTRACE(ANCH_TRACE, "Find Parent. %p with address `%s' already exists.\n" _
178 (void*) foundAnchor _ newaddr);
179 HT_FREE(newaddr); /* We already have it */
180 return (HTAnchor *) foundAnchor;
181 }
182 }
183
184 /* Node not found : create new anchor. */
185 foundAnchor = HTParentAnchor_new();
186 foundAnchor->address = newaddr; /* Remember our copy */
187 HTList_addObject (adults, foundAnchor);
188 HTTRACE(ANCH_TRACE, "Find Parent. %p with hash %d and address `%s' created\n" _ (void*)foundAnchor _ hash _ newaddr);
189 return (HTAnchor *) foundAnchor;
190 }
191 }
192
193 /* Create or find a child anchor with a possible link
194 ** --------------------------------------------------
195 **
196 ** Create new anchor with a given parent and possibly
197 ** a name, and possibly a link to a _relatively_ named anchor.
198 ** All parameters EXCEPT parent can be NULL
199 */
HTAnchor_findChildAndLink(HTParentAnchor * parent,const char * tag,const char * href,HTLinkType ltype)200 PUBLIC HTChildAnchor * HTAnchor_findChildAndLink (HTParentAnchor * parent,
201 const char * tag,
202 const char * href,
203 HTLinkType ltype)
204 {
205 HTChildAnchor * child = HTAnchor_findChild(parent, tag);
206 if (child && href && *href) {
207 char * relative_to = HTAnchor_expandedAddress((HTAnchor *) parent);
208 char * parsed_address = HTParse(href, relative_to, PARSE_ALL);
209 HTAnchor * dest = HTAnchor_findAddress(parsed_address);
210 HTLink_add((HTAnchor *) child, dest, ltype, METHOD_INVALID);
211 HT_FREE(parsed_address);
212 HT_FREE(relative_to);
213 }
214 return child;
215 }
216
217 /* ------------------------------------------------------------------------- */
218 /* Link Methods */
219 /* ------------------------------------------------------------------------- */
220
221 /*
222 ** Upgrade the link to the main destination and and downgrade the
223 ** current main link to the list
224 */
HTAnchor_mainLink(HTAnchor * me)225 PUBLIC HTLink * HTAnchor_mainLink (HTAnchor * me)
226 {
227 return me ? &(me->mainLink) : NULL;
228 }
229
HTAnchor_setMainLink(HTAnchor * me,HTLink * movingLink)230 PUBLIC BOOL HTAnchor_setMainLink (HTAnchor * me, HTLink * movingLink)
231 {
232 if (!(me && me->links && movingLink &&
233 HTList_removeObject(me->links, movingLink)))
234 return NO;
235 else {
236 /* First push current main link onto top of links list */
237 HTLink * newLink = HTLink_new();
238 memcpy ((void *) newLink, & me->mainLink, sizeof (HTLink));
239 HTList_addObject (me->links, newLink);
240
241 /* Now make movingLink the new main link, and delete it */
242 memcpy ((void *) &me->mainLink, movingLink, sizeof (HTLink));
243 HTLink_delete(movingLink);
244 return YES;
245 }
246 }
247
248 /*
249 ** Handling sub links
250 */
HTAnchor_subLinks(HTAnchor * anchor)251 PUBLIC HTList * HTAnchor_subLinks (HTAnchor * anchor)
252 {
253 return anchor ? anchor->links : NULL;
254 }
255
HTAnchor_setSubLinks(HTAnchor * anchor,HTList * list)256 PUBLIC BOOL HTAnchor_setSubLinks (HTAnchor * anchor, HTList * list)
257 {
258 if (anchor) {
259 anchor->links = list;
260 return YES;
261 }
262 return NO;
263 }
264
265 /*
266 ** Returns the main destination of this anchor
267 */
HTAnchor_followMainLink(HTAnchor * me)268 PUBLIC HTAnchor * HTAnchor_followMainLink (HTAnchor * me)
269 {
270 return me ? HTLink_destination(&me->mainLink) : NULL;
271 }
272
273 /*
274 ** Returns a link with a given link type or NULL if nothing found
275 */
HTAnchor_findLinkType(HTAnchor * me,HTLinkType type)276 PUBLIC HTLink * HTAnchor_findLinkType (HTAnchor * me, HTLinkType type)
277 {
278 if (me) {
279 HTLink * link = HTAnchor_mainLink(me);
280 HTList * sublinks = HTAnchor_subLinks(me);
281 if (link && HTLink_type(link) == type)
282 return link;
283 else if (sublinks) {
284 while ((link = (HTLink *) HTList_nextObject (sublinks)))
285 if (HTLink_type(link) == type) return link;
286 }
287 }
288 return NULL;
289 }
290
291 /* ------------------------------------------------------------------------- */
292 /* Deletion Methods */
293 /* ------------------------------------------------------------------------- */
294
295 /* Delete an anchor and possibly related things (auto garbage collection)
296 ** --------------------------------------------
297 **
298 ** The anchor is only deleted if the corresponding document is not loaded.
299 ** All outgoing links from parent and children are deleted, and this
300 ** anchor is removed from the sources list of all its targets.
301 ** We also try to delete the targets whose documents are not loaded.
302 ** If this anchor's source list is empty, we delete it and its children.
303 */
304
305 /* Deletes all the memory allocated in a parent anchor and returns any
306 ** hyperdoc object hanging of this anchor
307 */
delete_parent(HTParentAnchor * me)308 PRIVATE void * delete_parent (HTParentAnchor * me)
309 {
310 void * doc = me->document;
311
312 /* Remove link and address information */
313 if (me->links) {
314 HTList *cur = me->links;
315 HTLink *pres;
316 while ((pres = (HTLink *) HTList_nextObject(cur)))
317 HTLink_delete(pres);
318 HTList_delete(me->links);
319 }
320
321 /* Remove children */
322 if (me->children) {
323 int cnt = 0;
324 for (; cnt<CHILD_HASH_SIZE; cnt++) {
325 if (me->children[cnt]) HTList_delete(me->children[cnt]);
326 }
327 HT_FREE(me->children);
328 }
329
330 HTList_delete (me->sources);
331 HTList_delete (me->variants);
332 HT_FREE(me->physical);
333 HT_FREE(me->address);
334
335 /* Then remove entity header information (metainformation) */
336 HTAnchor_clearHeader(me);
337
338 HT_FREE(me);
339 return doc;
340 }
341
342
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
345 */
delete_family(HTAnchor * me)346 PRIVATE void * delete_family (HTAnchor * me)
347 {
348 HTParentAnchor * parent = NULL;
349 if (!me) {
350 HTTRACE(ANCH_TRACE, "AnchorDelete No anchor found\n");
351 return NULL;
352 }
353 parent = me->parent;
354 HTTRACE(ANCH_TRACE, "AnchorDelete Remove parent %p and children\n" _ parent);
355
356 /* Delete children */
357 if (parent->children) {
358 int cnt = 0;
359 for (; cnt<CHILD_HASH_SIZE; cnt++) {
360 HTList * kids = parent->children[cnt];
361 if (kids) {
362 HTChildAnchor * child;
363 while ((child=(HTChildAnchor*)HTList_removeLastObject(kids))) {
364 HT_FREE(child->tag);
365 if (child->links) {
366 HTList * cur = child->links;
367 HTLink * pres;
368 while ((pres = (HTLink *) HTList_nextObject(cur)))
369 HTLink_delete(pres);
370 HTList_delete(child->links);
371 }
372 HT_FREE(child);
373 }
374 HTList_delete(kids);
375 parent->children[cnt] = NULL;
376 }
377 }
378 }
379 return delete_parent(parent);
380 }
381
382
383 /* DELETE ALL ANCHORS
384 ** ------------------
385 ** Deletes all anchors and return a list of all the HyperDocs found.
386 ** It is for the application to delete any HyperDocs.
387 ** If NULL then no hyperdocs are returned
388 ** Return YES if OK, else NO
389 */
HTAnchor_deleteAll(HTList * documents)390 PUBLIC BOOL HTAnchor_deleteAll (HTList * documents)
391 {
392 int cnt;
393 HTList *cur;
394 if (!adult_table)
395 return NO;
396 for (cnt=0; cnt<PARENT_HASH_SIZE; cnt++) {
397 if ((cur = adult_table[cnt])) {
398 HTParentAnchor *pres;
399 while ((pres = (HTParentAnchor *) HTList_nextObject(cur)) != NULL){
400 void * doc = delete_family((HTAnchor *) pres);
401 if (doc && documents) HTList_addObject(documents, doc);
402 }
403 }
404 HTList_delete(adult_table[cnt]);
405 }
406 HT_FREE(adult_table);
407 return YES;
408 }
409
410 /*
411 ** Deletes all the metadata associated with anchors but doesn't
412 ** delete the anchor link structure itself. This is much safer
413 ** than deleting the complete anchor structure as this represents the
414 ** complete Web the application has been in touch with
415 */
HTAnchor_clearAll(HTList * documents)416 PUBLIC BOOL HTAnchor_clearAll (HTList * documents)
417 {
418 int cnt;
419 HTList * cur;
420 if (!adult_table) return NO;
421 for (cnt=0; cnt<PARENT_HASH_SIZE; cnt++) {
422 if ((cur = adult_table[cnt])) {
423 HTParentAnchor * pres;
424 while ((pres = (HTParentAnchor *) HTList_nextObject(cur))) {
425
426 /* Then remove entity header information */
427 HTAnchor_clearHeader(pres);
428
429 /* Delete the physical address */
430 HT_FREE(pres->physical);
431
432 /* Register if we have a document on this anchor */
433 if (documents && pres->document)
434 HTList_addObject(documents, pres->document);
435 }
436 }
437 }
438 return YES;
439 }
440
delete_links(HTAnchor * me)441 PRIVATE void delete_links (HTAnchor * me)
442 {
443 if (! me)
444 return;
445
446 /* Recursively try to delete target anchors */
447 if (me->mainLink.dest) {
448 HTParentAnchor *parent = me->mainLink.dest->parent;
449 HTList_removeObject (parent->sources, me);
450 if (! parent->document) /* Test here to avoid calling overhead */
451 HTAnchor_delete (parent);
452 }
453 if (me->links) { /* Extra destinations */
454 HTLink *target;
455 while ((target = (HTLink *) HTList_removeLastObject (me->links))) {
456 HTParentAnchor *parent = target->dest->parent;
457 HTList_removeObject (parent->sources, me);
458 if (! parent->document) /* Test here to avoid calling overhead */
459 HTAnchor_delete (parent);
460 }
461 }
462 }
463
HTAnchor_delete(HTParentAnchor * me)464 PUBLIC BOOL HTAnchor_delete (HTParentAnchor * me)
465 {
466 /* Don't delete if document is loaded */
467 if (!me || me->document) {
468 HTTRACE(ANCH_TRACE, "Anchor...... Not deleted\n");
469 return NO;
470 }
471
472 /* Recursively try to delete target anchors */
473 delete_links ((HTAnchor *) me);
474
475 if (!HTList_isEmpty(me->sources)) { /* There are still incoming links */
476
477 /*
478 ** Delete all outgoing links from children, if any
479 */
480 if (me->children) {
481 int cnt = 0;
482 for (; cnt<CHILD_HASH_SIZE; cnt++) {
483 HTList * kids = me->children[cnt];
484 if (kids) {
485 HTChildAnchor * child;
486 while ((child = (HTChildAnchor *) HTList_nextObject(kids)))
487 delete_links((HTAnchor *) child);
488 return NO; /* Parent not deleted */
489 }
490 }
491 }
492
493 /*
494 ** No more incoming links : kill everything
495 ** First, recursively delete children
496 */
497 if (me->children) {
498 int cnt = 0;
499 for (; cnt<CHILD_HASH_SIZE; cnt++) {
500 HTList * kids = me->children[cnt];
501 if (kids) {
502 HTChildAnchor * child;
503 while ((child=(HTChildAnchor *) HTList_removeLastObject(kids)))
504 delete_links((HTAnchor *) child);
505 HT_FREE(child->tag);
506 HT_FREE(child);
507 }
508 }
509 }
510 }
511
512 /* 2001/03/06: Bug fix by Serge Adda <sAdda@infovista.com>
513 HTAnchor_delete wasn't removing the reference to the deleted
514 anchor. This caused a bug whenever requesting another anchor
515 for the same URL.
516 */
517 if (adult_table) {
518 int hash;
519 const char *p;
520 HTList * adults;
521 HTList * grownups;
522 HTList * last;
523 HTParentAnchor * foundAnchor;
524
525 /* Select list from hash table */
526 for(p=me->address, hash=0; *p; p++)
527 hash = (int) ((hash * 3 + (*(unsigned char*)p)) %
528 PARENT_HASH_SIZE);
529 adults = adult_table[hash];
530
531 /* Search list for anchor */
532 grownups = adults;
533 last = grownups;
534 while ((foundAnchor = (HTParentAnchor *)
535 HTList_nextObject(grownups))){
536 if (!strcmp(foundAnchor->address, me->address)) {
537 HTList_quickRemoveElement (grownups, last);
538 break;
539 }
540 last = grownups;
541 }
542 }
543
544 /* Now kill myself */
545 delete_parent(me);
546 return YES; /* Parent deleted */
547 #if 0
548 if (! HTList_isEmpty (me->sources)) { /* There are still incoming links */
549 /* Delete all outgoing links from children, if any */
550 HTList *kids = me->children;
551 while ((child = (HTChildAnchor *) HTList_nextObject (kids)))
552 delete_links ((HTAnchor *) child);
553 return NO; /* Parent not deleted */
554 }
555
556 /* No more incoming links : kill everything */
557 /* First, recursively delete children */
558 while ((child = (HTChildAnchor *) HTList_removeLastObject (me->children))) {
559 delete_links ((HTAnchor *) child);
560 HT_FREE(child->tag);
561 HT_FREE(child);
562 }
563 #endif
564 }
565
566 /* FLATTEN ALL ANCHORS
567 ** -------------------
568 ** Flattens the anchor web structure into an array.
569 ** This is useful for calculating statistics, sorting
570 ** the parent anchors etc.
571 **
572 ** The caller can indicate the size of the array (total
573 ** number of anchors if known - otherwise 0).
574 **
575 ** Return an array that must be freed by the caller or
576 ** NULL if no anchors.
577 */
HTAnchor_getArray(int growby)578 PUBLIC HTArray * HTAnchor_getArray (int growby)
579 {
580 int cnt;
581 HTArray * array = NULL;
582 HTList * cur = NULL;
583 if (!adult_table) return NULL;
584
585 /* Allocate an array for the anchors */
586 if (growby <= 0) growby = PARENT_HASH_SIZE;
587 array = HTArray_new(growby);
588
589 /* Traverse anchor structure */
590 for (cnt=0; cnt<PARENT_HASH_SIZE; cnt++) {
591 if ((cur = adult_table[cnt])) {
592 HTParentAnchor * pres = NULL;
593 while ((pres = (HTParentAnchor *) HTList_nextObject(cur)) != NULL) {
594 if (HTArray_addObject(array, pres) == NO) {
595 HTTRACE(ANCH_TRACE, "Anchor...... Can't add object %p to array %p\n" _
596 pres _ array);
597 break;
598 }
599 }
600 }
601 }
602 return array;
603 }
604
605 /* ------------------------------------------------------------------------- */
606 /* Data Access Methods */
607 /* ------------------------------------------------------------------------- */
608
HTAnchor_parent(HTAnchor * me)609 PUBLIC HTParentAnchor * HTAnchor_parent (HTAnchor * me)
610 {
611 return me ? me->parent : NULL;
612 }
613
HTAnchor_setDocument(HTParentAnchor * me,void * doc)614 PUBLIC void HTAnchor_setDocument (HTParentAnchor * me, void * doc)
615 {
616 if (me) me->document = doc;
617 }
618
HTAnchor_document(HTParentAnchor * me)619 PUBLIC void * HTAnchor_document (HTParentAnchor * me)
620 {
621 return me ? me->document : NULL;
622 }
623
HTAnchor_address(HTAnchor * me)624 PUBLIC char * HTAnchor_address (HTAnchor * me)
625 {
626 char *addr = NULL;
627 if (me) {
628 if (((HTParentAnchor *) me == me->parent) ||
629 !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
630 StrAllocCopy (addr, me->parent->address);
631 }
632 else { /* it's a named child */
633 if ((addr = (char *) HT_MALLOC(2 + strlen (me->parent->address) + \
634 strlen (((HTChildAnchor *) me)->tag))) == NULL)
635 HT_OUTOFMEM("HTAnchor_address");
636 sprintf (addr, "%s#%s", me->parent->address,
637 ((HTChildAnchor *) me)->tag);
638 }
639 }
640 return addr;
641 }
642
643 /*
644 ** We resolve the child address with respect to either a base URL,
645 ** a content-location, or to the request-URI
646 */
HTAnchor_expandedAddress(HTAnchor * me)647 PUBLIC char * HTAnchor_expandedAddress (HTAnchor * me)
648 {
649 char *addr = NULL;
650 if (me) {
651 HTParentAnchor * parent = me->parent;
652 char * base = HTAnchor_base(parent);
653 if (((HTParentAnchor *) me == me->parent) ||
654 !((HTChildAnchor *) me)->tag) { /* it's an adult or no tag */
655 StrAllocCopy(addr, base);
656 } else { /* it's a named child */
657 if ((addr = (char *) HT_MALLOC(2 + strlen(base) + strlen(((HTChildAnchor *) me)->tag))) == NULL)
658 HT_OUTOFMEM("HTAnchor_address");
659 sprintf (addr, "%s#%s", base, ((HTChildAnchor *) me)->tag);
660 }
661 }
662 return addr;
663 }
664
665 /* Physical Address
666 ** ----------------
667 */
HTAnchor_physical(HTParentAnchor * me)668 PUBLIC char * HTAnchor_physical (HTParentAnchor * me)
669 {
670 return me ? me->physical ? me->physical : me->address : NULL;
671 }
672
HTAnchor_setPhysical(HTParentAnchor * me,char * physical)673 PUBLIC void HTAnchor_setPhysical (HTParentAnchor * me, char * physical)
674 {
675 if (!me || !physical) {
676 HTTRACE(ANCH_TRACE, "HTAnchor.... setPhysical, called with null argument\n");
677 return;
678 }
679 StrAllocCopy(me->physical, physical);
680 }
681
HTAnchor_clearPhysical(HTParentAnchor * me)682 PUBLIC void HTAnchor_clearPhysical(HTParentAnchor * me)
683 {
684 if (me) HT_FREE(me->physical);
685 }
686
687 /*
688 ** Children information
689 */
HTAnchor_hasChildren(HTParentAnchor * me)690 PUBLIC BOOL HTAnchor_hasChildren (HTParentAnchor * me)
691 {
692 return (me && me->children);
693 }
694
695 /*
696 ** Fix up a simple routine to see if this anchor is a (ChildAnchor *)
697 ** Seem to be doing it all over the place, so simplify!
698 */
HTAnchor_isChild(HTAnchor * me)699 PUBLIC BOOL HTAnchor_isChild (HTAnchor * me)
700 {
701 return (me && (HTParentAnchor *) me != me->parent);
702 }
703
HTAnchor_view(HTAnchor * me)704 PUBLIC char * HTAnchor_view (HTAnchor * me)
705 {
706 char * view = NULL;
707 if (me && (HTParentAnchor *) me != me->parent && ((HTChildAnchor *) me)->tag)
708 StrAllocCopy(view, ((HTChildAnchor *) me)->tag);
709 return view;
710 }
711
712 /* ------------------------------------------------------------------------- */
713 /* Entity Header Information */
714 /* ------------------------------------------------------------------------- */
715
716 /*
717 ** Take the relevant infomration from the response object and cache it
718 ** in the anchor object. We inherit the information that is already
719 ** parsed in the response along with the unparsed headers.
720 */
HTAnchor_update(HTParentAnchor * me,HTResponse * response)721 PUBLIC BOOL HTAnchor_update (HTParentAnchor * me, HTResponse * response)
722 {
723 if (me && response) {
724 HTCachable cachable = HTResponse_isCachable(response);
725
726 if (cachable == HT_CACHE_ETAG) {
727 char * etag = HTResponse_etag(response);
728 HTTRACE(ANCH_TRACE, "HTAnchor.... Updating etag for %p\n" _ me);
729 if (etag) {
730 HTAnchor_setEtag(me, etag);
731 return YES;
732 }
733
734 } else if (cachable == HT_CACHE_NOT_MODIFIED) {
735 HTTRACE(ANCH_TRACE, "HTAnchor.... Information is up to date for %p\n" _ me);
736 return YES;
737
738 } else if (cachable == HT_CACHE_ALL) {
739 char * etag = HTResponse_etag(response);
740 HTTRACE(ANCH_TRACE, "HTAnchor.... Updating metainformation for %p\n" _ me);
741
742 /*
743 ** The content length and type is already parsed at this point
744 ** in time. We also check for format parameters like charset etc.
745 ** and copy the contents in the anchor object
746 */
747 me->content_length = HTResponse_length(response);
748 me->content_type = HTResponse_format(response);
749 me->type_parameters = HTResponse_formatParam(response);
750 me->content_encoding = HTResponse_encoding(response);
751
752 /* Don't forget the etag as well */
753 if (etag) HTAnchor_setEtag(me, etag);
754
755 /*
756 ** Inherit all the unparsed headers - we may need them later!
757 */
758 if (me->headers) HTAssocList_delete(me->headers);
759 me->headers = HTResponse_handOverHeader(response);
760
761 /*
762 ** Notifify the response object not to delete the lists that we
763 ** have inherited in the anchor object
764 */
765 HTResponse_isCached(response, YES);
766
767 /*
768 ** Set the datestamp of when the anchor was updated if we didn't
769 ** get any in the response
770 */
771 if (!HTAssocList_findObject(me->headers, "date"))
772 HTAnchor_setDate(me, time(NULL));
773
774 return YES;
775 }
776 }
777 return NO;
778 }
779
780 /*
781 ** Variants. If this anchor has any variants then keep them in a list
782 ** so that we can find them later. The list is simply a list of
783 ** parent anchors.
784 */
HTAnchor_variants(HTParentAnchor * me)785 PUBLIC HTList * HTAnchor_variants (HTParentAnchor * me)
786 {
787 return me ? me->variants : NULL;
788 }
789
HTAnchor_addVariant(HTParentAnchor * me,HTParentAnchor * variant)790 PUBLIC BOOL HTAnchor_addVariant (HTParentAnchor * me,
791 HTParentAnchor * variant)
792 {
793 if (me && variant) {
794 if (!me->variants) me->variants = HTList_new();
795 return HTList_addObject(me->variants, variant);
796 }
797 return NO;
798 }
799
HTAnchor_deleteVariant(HTParentAnchor * me,HTParentAnchor * variant)800 PUBLIC BOOL HTAnchor_deleteVariant (HTParentAnchor * me,
801 HTParentAnchor * variant)
802 {
803 return (me && variant) ? HTList_removeObject(me->variants, variant) : NO;
804 }
805
806 /*
807 ** Is this resource an index?
808 */
HTAnchor_clearIndex(HTParentAnchor * me)809 PUBLIC void HTAnchor_clearIndex (HTParentAnchor * me)
810 {
811 if (me) me->isIndex = NO;
812 }
813
HTAnchor_setIndex(HTParentAnchor * me)814 PUBLIC void HTAnchor_setIndex (HTParentAnchor * me)
815 {
816 if (me) me->isIndex = YES;
817 }
818
HTAnchor_isIndex(HTParentAnchor * me)819 PUBLIC BOOL HTAnchor_isIndex (HTParentAnchor * me)
820 {
821 return me ? me->isIndex : NO;
822 }
823
824 /* Content Base
825 ** ------------
826 */
HTAnchor_base(HTParentAnchor * me)827 PUBLIC char * HTAnchor_base (HTParentAnchor * me)
828 {
829 if (me) {
830 if (me->content_base) return me->content_base;
831 if (me->headers) {
832 char * base = HTAssocList_findObject(me->headers, "content-base");
833 /*
834 ** If no base is found then take the content-location if this
835 ** is present and is absolute, else use the Request-URI.
836 */
837 if (base) StrAllocCopy(me->content_base, HTStrip(base));
838 }
839
840 /*
841 ** Try the content location if any
842 */
843 {
844 char * location = HTAnchor_location(me);
845 StrAllocCopy(me->content_base,
846 (location && HTURL_isAbsolute(location)) ?
847 location : me->address);
848 }
849 return me->content_base;
850 }
851 return NULL;
852 }
853
HTAnchor_setBase(HTParentAnchor * me,char * base)854 PUBLIC BOOL HTAnchor_setBase (HTParentAnchor * me, char * base)
855 {
856 if (me && base) {
857 StrAllocCopy(me->content_base, base);
858 return YES;
859 }
860 return NO;
861 }
862
863 /* Content Location
864 ** ----------------
865 */
HTAnchor_location(HTParentAnchor * me)866 PUBLIC char * HTAnchor_location (HTParentAnchor * me)
867 {
868 if (me) {
869 if (me->content_location)
870 return *me->content_location ? me->content_location : NULL;
871 if (me->headers) {
872 char * location = HTAssocList_findObject(me->headers, "content-location");
873 StrAllocCopy(me->content_location, location ? HTStrip(location) : "");
874 return me->content_location;
875 }
876 }
877 return NULL;
878 }
879
880 /*
881 ** Expand the location relative to the base URL if any, otherwise the
882 ** anchor address it self
883 */
HTAnchor_setLocation(HTParentAnchor * me,char * location)884 PUBLIC BOOL HTAnchor_setLocation (HTParentAnchor * me, char * location)
885 {
886 if (me && location) {
887 char * base = HTAnchor_base(me);
888 if (!base) base = me->address;
889 me->content_location = HTParse(location, base, PARSE_ALL);
890 return YES;
891 }
892 return NO;
893 }
894
895 /* Meta tags
896 ** ---------
897 */
HTAnchor_meta(HTParentAnchor * me)898 PUBLIC HTAssocList * HTAnchor_meta (HTParentAnchor * me)
899 {
900 return me ? me->meta_tags : NULL;
901 }
902
HTAnchor_addMeta(HTParentAnchor * me,const char * name,const char * value)903 PUBLIC BOOL HTAnchor_addMeta (HTParentAnchor * me,
904 const char * name, const char * value)
905 {
906 if (me) {
907 if (!me->meta_tags) me->meta_tags = HTAssocList_new();
908 return HTAssocList_replaceObject(me->meta_tags, name, value);
909 }
910 return NO;
911 }
912
913 /*
914 ** robots meta tag
915 */
HTAnchor_robots(HTParentAnchor * me)916 PUBLIC char * HTAnchor_robots (HTParentAnchor * me)
917 {
918 if (me && me->meta_tags) {
919 char * robots = HTAssocList_findObject(me->meta_tags, "robots");
920 return robots;
921 }
922 return NULL;
923 }
924
925 /* Content-Type
926 ** ------------
927 */
HTAnchor_format(HTParentAnchor * me)928 PUBLIC HTFormat HTAnchor_format (HTParentAnchor * me)
929 {
930 return me ? me->content_type : NULL;
931 }
932
HTAnchor_setFormat(HTParentAnchor * me,HTFormat form)933 PUBLIC void HTAnchor_setFormat (HTParentAnchor * me, HTFormat form)
934 {
935 if (me) me->content_type = form;
936 }
937
HTAnchor_formatParam(HTParentAnchor * me)938 PUBLIC HTAssocList * HTAnchor_formatParam (HTParentAnchor * me)
939 {
940 return me ? me->type_parameters : NULL;
941 }
942
HTAnchor_addFormatParam(HTParentAnchor * me,const char * name,const char * value)943 PUBLIC BOOL HTAnchor_addFormatParam (HTParentAnchor * me,
944 const char * name, const char * value)
945 {
946 if (me) {
947 if (!me->type_parameters) me->type_parameters = HTAssocList_new();
948 return HTAssocList_replaceObject(me->type_parameters, name, value);
949 }
950 return NO;
951 }
952
953 /*
954 ** Charset parameter to Content-Type
955 */
HTAnchor_charset(HTParentAnchor * me)956 PUBLIC HTCharset HTAnchor_charset (HTParentAnchor * me)
957 {
958 if (me && me->type_parameters) {
959 char * charset = HTAssocList_findObject(me->type_parameters,"charset");
960 return HTAtom_for(charset);
961 }
962 return NULL;
963 }
964
HTAnchor_setCharset(HTParentAnchor * me,HTCharset charset)965 PUBLIC BOOL HTAnchor_setCharset (HTParentAnchor * me, HTCharset charset)
966 {
967 return HTAnchor_addFormatParam(me, "charset", HTAtom_name(charset));
968 }
969
970 /*
971 ** Level parameter to Content-Type
972 */
HTAnchor_level(HTParentAnchor * me)973 PUBLIC HTLevel HTAnchor_level (HTParentAnchor * me)
974 {
975 if (me && me->type_parameters) {
976 char * level = HTAssocList_findObject(me->type_parameters, "level");
977 return HTAtom_for(level);
978 }
979 return NULL;
980 }
981
HTAnchor_setLevel(HTParentAnchor * me,HTLevel level)982 PUBLIC BOOL HTAnchor_setLevel (HTParentAnchor * me, HTLevel level)
983 {
984 return HTAnchor_addFormatParam(me, "level", HTAtom_name(level));
985 }
986
987 /*
988 ** Content Encoding
989 */
HTAnchor_encoding(HTParentAnchor * me)990 PUBLIC HTList * HTAnchor_encoding (HTParentAnchor * me)
991 {
992 return me ? me->content_encoding : NULL;
993 }
994
HTAnchor_addEncoding(HTParentAnchor * me,HTEncoding encoding)995 PUBLIC BOOL HTAnchor_addEncoding (HTParentAnchor * me, HTEncoding encoding)
996 {
997 if (me && encoding) {
998 if (!me->content_encoding) me->content_encoding = HTList_new();
999 return HTList_addObject(me->content_encoding, encoding);
1000 }
1001 return NO;
1002 }
1003
HTAnchor_deleteEncoding(HTParentAnchor * me,HTEncoding encoding)1004 PUBLIC BOOL HTAnchor_deleteEncoding (HTParentAnchor * me, HTEncoding encoding)
1005 {
1006 return (me && me->content_encoding && encoding) ?
1007 HTList_removeObject(me->content_encoding, encoding) : NO;
1008 }
1009
HTAnchor_deleteEncodingAll(HTParentAnchor * me)1010 PUBLIC BOOL HTAnchor_deleteEncodingAll (HTParentAnchor * me)
1011 {
1012 if (me && me->content_encoding) {
1013 HTList_delete(me->content_encoding);
1014 me->content_encoding = NULL;
1015 return YES;
1016 }
1017 return NO;
1018 }
1019
1020 /*
1021 ** Content Language
1022 */
HTAnchor_language(HTParentAnchor * me)1023 PUBLIC HTList * HTAnchor_language (HTParentAnchor * me)
1024 {
1025 if (me) {
1026 if (me->content_language == NULL && me->headers) {
1027 char * value = HTAssocList_findObject(me->headers, "content-language");
1028 char * field;
1029 if (!me->content_language) me->content_language = HTList_new();
1030 while ((field = HTNextField(&value)) != NULL) {
1031 char * lc = field;
1032 while ((*lc = TOLOWER(*lc))) lc++;
1033 HTList_addObject(me->content_language, HTAtom_for(field));
1034 }
1035 }
1036 return me->content_language;
1037 }
1038 return NULL;
1039 }
1040
HTAnchor_addLanguage(HTParentAnchor * me,HTLanguage language)1041 PUBLIC BOOL HTAnchor_addLanguage (HTParentAnchor * me, HTLanguage language)
1042 {
1043 if (me && language) {
1044 if (!me->content_language) me->content_language = HTList_new();
1045 return HTList_addObject(me->content_language, language);
1046 }
1047 return NO;
1048 }
1049
HTAnchor_deleteLanguageAll(HTParentAnchor * me)1050 PUBLIC BOOL HTAnchor_deleteLanguageAll (HTParentAnchor * me)
1051 {
1052 if (me && me->content_language) {
1053 HTList_delete(me->content_language);
1054 me->content_language = NULL;
1055 return YES;
1056 }
1057 return NO;
1058 }
1059
1060 /*
1061 ** Content Length
1062 */
HTAnchor_length(HTParentAnchor * me)1063 PUBLIC long int HTAnchor_length (HTParentAnchor * me)
1064 {
1065 return me ? me->content_length : -1;
1066 }
1067
HTAnchor_setLength(HTParentAnchor * me,long int length)1068 PUBLIC void HTAnchor_setLength (HTParentAnchor * me, long int length)
1069 {
1070 if (me) me->content_length = length;
1071 }
1072
HTAnchor_addLength(HTParentAnchor * me,long int deltalength)1073 PUBLIC void HTAnchor_addLength (HTParentAnchor * me, long int deltalength)
1074 {
1075 if (me) {
1076 if (me->content_length < 0)
1077 me->content_length = deltalength;
1078 else
1079 me->content_length += deltalength;
1080 }
1081 }
1082
1083 /*
1084 ** Content Transfer Encoding
1085 */
HTAnchor_contentTransferEncoding(HTParentAnchor * me)1086 PUBLIC HTEncoding HTAnchor_contentTransferEncoding (HTParentAnchor * me)
1087 {
1088 return me ? me->cte : NULL;
1089 }
1090
HTAnchor_setContentTransferEncoding(HTParentAnchor * me,HTEncoding cte)1091 PUBLIC void HTAnchor_setContentTransferEncoding (HTParentAnchor * me, HTEncoding cte)
1092 {
1093 if (me) me->cte = cte;
1094 }
1095
1096 /*
1097 ** Allowed methods (Allow)
1098 */
HTAnchor_allow(HTParentAnchor * me)1099 PUBLIC HTMethod HTAnchor_allow (HTParentAnchor * me)
1100 {
1101 if (me) {
1102 if (me->allow == 0 && me->headers) {
1103 char * value = HTAssocList_findObject(me->headers, "allow");
1104 char * field;
1105
1106 /*
1107 ** We treat methods allowed on this object as case insensitive
1108 ** in case we receive the information over the net - that is -
1109 ** in the Allow header.
1110 */
1111 while ((field = HTNextField(&value)) != NULL) {
1112 HTMethod new_method;
1113 if ((new_method = HTMethod_enum(field)) != METHOD_INVALID)
1114 me->allow |= new_method;
1115 }
1116 }
1117 return me->allow;
1118 }
1119 return METHOD_INVALID;
1120 }
1121
HTAnchor_setAllow(HTParentAnchor * me,HTMethod methodset)1122 PUBLIC void HTAnchor_setAllow (HTParentAnchor * me, HTMethod methodset)
1123 {
1124 if (me) me->allow = methodset;
1125 }
1126
HTAnchor_appendAllow(HTParentAnchor * me,HTMethod methodset)1127 PUBLIC void HTAnchor_appendAllow (HTParentAnchor * me, HTMethod methodset)
1128 {
1129 if (me) me->allow |= methodset;
1130 }
1131
1132 /*
1133 ** Title
1134 */
HTAnchor_title(HTParentAnchor * me)1135 PUBLIC const char * HTAnchor_title (HTParentAnchor * me)
1136 {
1137 if (me) {
1138 if (me->title)
1139 return *me->title ? me->title : NULL;
1140 if (me->headers) {
1141 char * value = HTAssocList_findObject(me->headers, "title");
1142 char * title;
1143 if ((title = HTNextField(&value))) StrAllocCopy(me->title, title);
1144 return me->title;
1145 }
1146 }
1147 return NULL;
1148 }
1149
HTAnchor_setTitle(HTParentAnchor * me,const char * title)1150 PUBLIC void HTAnchor_setTitle (HTParentAnchor * me, const char * title)
1151 {
1152 if (me && title) {
1153 char * ptr;
1154 StrAllocCopy(me->title, title);
1155 ptr = me->title;
1156 while (*ptr) {
1157 if (isspace((int) *ptr)) *ptr = ' ';
1158 ptr++;
1159 }
1160 }
1161 }
1162
HTAnchor_appendTitle(HTParentAnchor * me,const char * title)1163 PUBLIC void HTAnchor_appendTitle (HTParentAnchor * me, const char * title)
1164 {
1165 if (me && title) StrAllocCat(me->title, title);
1166 }
1167
1168 /*
1169 ** Version
1170 */
HTAnchor_version(HTParentAnchor * me)1171 PUBLIC char * HTAnchor_version (HTParentAnchor * me)
1172 {
1173 if (me) {
1174 if (me->version)
1175 return *me->version ? me->version : NULL;
1176 if (me->headers) {
1177 char * value = HTAssocList_findObject(me->headers, "version");
1178 char * version;
1179 if ((version = HTNextField(&value)))
1180 StrAllocCopy(me->version, version);
1181 return me->version;
1182 }
1183 }
1184 return NULL;
1185 }
1186
HTAnchor_setVersion(HTParentAnchor * me,const char * version)1187 PUBLIC void HTAnchor_setVersion (HTParentAnchor * me, const char * version)
1188 {
1189 if (me && version) StrAllocCopy(me->version, version);
1190 }
1191
1192 /*
1193 ** Derived from
1194 */
HTAnchor_derived(HTParentAnchor * me)1195 PUBLIC char * HTAnchor_derived (HTParentAnchor * me)
1196 {
1197 if (me) {
1198 if (me->derived_from)
1199 return *me->derived_from ? me->derived_from : NULL;
1200 if (me->headers) {
1201 char * value = HTAssocList_findObject(me->headers, "derived-from");
1202 char * derived_from;
1203 if ((derived_from = HTNextField(&value)))
1204 StrAllocCopy(me->derived_from, derived_from);
1205 return me->derived_from;
1206 }
1207 }
1208 return NULL;
1209 }
1210
HTAnchor_setDerived(HTParentAnchor * me,const char * derived_from)1211 PUBLIC void HTAnchor_setDerived (HTParentAnchor * me, const char *derived_from)
1212 {
1213 if (me && derived_from) StrAllocCopy(me->derived_from, derived_from);
1214 }
1215
1216 /*
1217 ** Content MD5
1218 */
HTAnchor_md5(HTParentAnchor * me)1219 PUBLIC char * HTAnchor_md5 (HTParentAnchor * me)
1220 {
1221 if (me) {
1222 if (me->content_md5)
1223 return *me->content_md5 ? me->content_md5 : NULL;
1224 if (me->headers) {
1225 char * value = HTAssocList_findObject(me->headers, "content-md5");
1226 char * md5;
1227 if ((md5 = HTNextField(&value))) StrAllocCopy(me->content_md5,md5);
1228 return me->content_md5;
1229 }
1230 }
1231 return NULL;
1232 }
1233
HTAnchor_setMd5(HTParentAnchor * me,const char * hash)1234 PUBLIC BOOL HTAnchor_setMd5 (HTParentAnchor * me, const char * hash)
1235 {
1236 if (me && hash) {
1237 StrAllocCopy(me->content_md5, hash);
1238 return YES;
1239 }
1240 return NO;
1241 }
1242
1243 /*
1244 ** Date
1245 */
HTAnchor_date(HTParentAnchor * me)1246 PUBLIC time_t HTAnchor_date (HTParentAnchor * me)
1247 {
1248 if (me) {
1249 if (me->date == (time_t) -1 && me->headers) {
1250 char * value = HTAssocList_findObject(me->headers, "date");
1251 if (value) me->date = HTParseTime(value, NULL, YES);
1252 }
1253 return me->date;
1254 }
1255 return (time_t) -1;
1256 }
1257
HTAnchor_setDate(HTParentAnchor * me,const time_t date)1258 PUBLIC void HTAnchor_setDate (HTParentAnchor * me, const time_t date)
1259 {
1260 if (me) me->date = date;
1261 }
1262
1263 /*
1264 ** Expires
1265 */
HTAnchor_expires(HTParentAnchor * me)1266 PUBLIC time_t HTAnchor_expires (HTParentAnchor * me)
1267 {
1268 if (me) {
1269 if (me->expires == (time_t) -1 && me->headers) {
1270 char * value = HTAssocList_findObject(me->headers, "expires");
1271 if (value) me->expires = HTParseTime(value, NULL, YES);
1272 }
1273 return me->expires;
1274 }
1275 return (time_t) -1;
1276 }
1277
HTAnchor_setExpires(HTParentAnchor * me,const time_t expires)1278 PUBLIC void HTAnchor_setExpires (HTParentAnchor * me, const time_t expires)
1279 {
1280 if (me) me->expires = expires;
1281 }
1282
1283 /*
1284 ** Last Modified
1285 */
HTAnchor_lastModified(HTParentAnchor * me)1286 PUBLIC time_t HTAnchor_lastModified (HTParentAnchor * me)
1287 {
1288 if (me) {
1289 if (me->last_modified == (time_t) -1 && me->headers) {
1290 char * value = HTAssocList_findObject(me->headers,"last-modified");
1291 if (value) me->last_modified = HTParseTime(value, NULL, YES);
1292 }
1293 return me->last_modified;
1294 }
1295 return (time_t) -1;
1296 }
1297
HTAnchor_setLastModified(HTParentAnchor * me,const time_t lm)1298 PUBLIC void HTAnchor_setLastModified (HTParentAnchor * me, const time_t lm)
1299 {
1300 if (me) me->last_modified = lm;
1301 }
1302
1303 /*
1304 ** Age
1305 */
HTAnchor_age(HTParentAnchor * me)1306 PUBLIC time_t HTAnchor_age (HTParentAnchor * me)
1307 {
1308 if (me) {
1309 if (me->age == (time_t) -1 && me->headers) {
1310 char * value = HTAssocList_findObject(me->headers, "age");
1311 if (value) me->age = atol(value);
1312 }
1313 return me->age;
1314 }
1315 return (time_t) -1;
1316 }
1317
HTAnchor_setAge(HTParentAnchor * me,const time_t age)1318 PUBLIC void HTAnchor_setAge (HTParentAnchor * me, const time_t age)
1319 {
1320 if (me) me->age = age;
1321 }
1322
1323 /*
1324 ** Entity Tag
1325 */
HTAnchor_etag(HTParentAnchor * me)1326 PUBLIC char * HTAnchor_etag (HTParentAnchor * me)
1327 {
1328 if (me) {
1329 if (me->etag)
1330 return *me->etag ? me->etag : NULL;
1331 if (me->headers) {
1332 char * value = HTAssocList_findObject(me->headers, "etag");
1333 char * etag;
1334 if ((etag = HTNextField(&value))) StrAllocCopy(me->etag, etag);
1335 return me->etag;
1336 }
1337 }
1338
1339 return me ? me->etag : NULL;
1340 }
1341
HTAnchor_setEtag(HTParentAnchor * me,const char * etag)1342 PUBLIC void HTAnchor_setEtag (HTParentAnchor * me, const char * etag)
1343 {
1344 /* JK: add a new etag if it doesn't exist or if the value has changed */
1345 if (me && etag && ((me->etag == NULL) || strcmp (me->etag, etag)))
1346 StrAllocCopy(me->etag, etag);
1347 }
1348
HTAnchor_isEtagWeak(HTParentAnchor * me)1349 PUBLIC BOOL HTAnchor_isEtagWeak (HTParentAnchor * me)
1350 {
1351 return (me && me->etag && !strncasecomp(me->etag, "W/", 2));
1352 }
1353
1354 /*
1355 ** Original headers (if any)
1356 */
HTAnchor_header(HTParentAnchor * me)1357 PUBLIC HTAssocList * HTAnchor_header (HTParentAnchor * me)
1358 {
1359 return me ? me->headers : NULL;
1360 }
1361
HTAnchor_setHeader(HTParentAnchor * me,HTAssocList * headers)1362 PUBLIC BOOL HTAnchor_setHeader (HTParentAnchor * me, HTAssocList * headers)
1363 {
1364 if (me) {
1365 me->headers = headers;
1366 return YES;
1367 }
1368 return NO;
1369 }
1370
1371 /*
1372 ** Validate anchor values and finish up parsing
1373 */
HTAnchor_setHeaderParsed(HTParentAnchor * me)1374 PUBLIC void HTAnchor_setHeaderParsed (HTParentAnchor * me)
1375 {
1376 if (me) {
1377 HTTRACE(ANCH_TRACE, "HTAnchor.... Anchor is parsed\n");
1378 me->header_parsed = YES;
1379 }
1380 }
1381
HTAnchor_headerParsed(HTParentAnchor * me)1382 PUBLIC BOOL HTAnchor_headerParsed (HTParentAnchor * me)
1383 {
1384 return (me ? me->header_parsed : NO);
1385 }
1386
1387 /* Clear Header Information
1388 ** ------------------------
1389 */
HTAnchor_clearHeader(HTParentAnchor * me)1390 PUBLIC void HTAnchor_clearHeader (HTParentAnchor * me)
1391 {
1392 HTTRACE(ANCH_TRACE, "HTAnchor.... Clear all header information\n");
1393 me->allow = METHOD_INVALID;
1394 if (me->content_encoding) {
1395 HTList_delete(me->content_encoding);
1396 me->content_encoding = NULL;
1397 }
1398 if (me->content_language) {
1399 HTList_delete(me->content_language);
1400 me->content_language = NULL;
1401 }
1402 HT_FREE(me->content_base);
1403 HT_FREE(me->content_location);
1404 me->content_length = -1; /* Invalid */
1405
1406 /* Delete the title */
1407 HT_FREE(me->title);
1408
1409 /* Clear the content type */
1410 me->content_type = WWW_UNKNOWN;
1411 if (me->type_parameters) {
1412 HTAssocList_delete(me->type_parameters);
1413 me->type_parameters = NULL;
1414 }
1415
1416 /* Meta tags */
1417 if (me->meta_tags) {
1418 HTAssocList_delete(me->meta_tags);
1419 me->meta_tags = NULL;
1420 }
1421
1422 /* Dates etc. */
1423 me->date = (time_t) -1;
1424 me->expires = (time_t) -1;
1425 me->last_modified = (time_t) -1;
1426 me->age = (time_t) -1;
1427
1428 HT_FREE(me->derived_from);
1429 HT_FREE(me->version);
1430 HT_FREE(me->etag);
1431
1432 /* Delete any original headers */
1433 if (me->headers) HTAssocList_delete(me->headers);
1434 me->headers = NULL;
1435 }
1436