Annotation of Amaya/amaya/AHTURLTools.c, revision 1.150
1.7 cvs 1: /*
2: *
1.133 vatton 3: * (c) COPYRIGHT MIT and INRIA, 1996-2002
1.7 cvs 4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
1.9 cvs 7:
1.10 cvs 8: /*
9: * AHTURLTools.c: contains all the functions for testing, manipulating,
1.25 cvs 10: * and normalizing URLs. It also contains a local copy of the libWWW
11: * URL parsing functions.
1.10 cvs 12: *
13: * Authors: J. Kahan, I. Vatton
1.106 cvs 14: * R. Guetari: Windows.
1.10 cvs 15: *
16: */
1.7 cvs 17:
1.15 cvs 18: #define THOT_EXPORT extern
1.3 cvs 19: #include "amaya.h"
20:
1.8 cvs 21: #include "init_f.h"
22: #include "AHTURLTools_f.h"
1.100 kahan 23: #include "query_f.h"
1.8 cvs 24:
1.24 cvs 25: #define MAX_PRINT_URL_LENGTH 50
1.106 cvs 26: typedef struct _HTURI
27: {
28: char *access; /* Now known as "scheme" */
29: char *host;
30: char *absolute;
31: char *relative;
32: char *fragment;
1.29 cvs 33: } HTURI;
1.24 cvs 34:
1.28 cvs 35:
36: /*----------------------------------------------------------------------
37: ConvertToLowerCase
38: Converts a string to lowercase.
39: ----------------------------------------------------------------------*/
1.124 vatton 40: void ConvertToLowerCase (char *string)
1.28 cvs 41: {
42: int i;
1.93 cvs 43:
1.28 cvs 44: if (!string)
45: return;
46:
1.106 cvs 47: for (i = 0; string[i] != EOS; i++)
1.123 vatton 48: string[i] = tolower (string[i]);
1.28 cvs 49: }
1.22 cvs 50:
1.8 cvs 51: /*----------------------------------------------------------------------
1.75 cvs 52: EscapeChar
53: writes the equivalent escape code of a char in a string
54: ----------------------------------------------------------------------*/
1.109 cvs 55: void EscapeChar (char *string, char c)
1.75 cvs 56: {
1.109 cvs 57: unsigned int i;
58:
59: i = (unsigned char) c & 0xFF;
60: sprintf (string, "%02x", i);
1.75 cvs 61: }
62:
63: /*----------------------------------------------------------------------
1.96 cvs 64: UnEscapeChar
65: writes the equivalent hex code to a %xx coded char
66: ----------------------------------------------------------------------*/
1.109 cvs 67: static char UnEscapeChar (char c)
1.96 cvs 68: {
1.106 cvs 69: return c >= '0' && c <= '9' ? c - '0'
70: : c >= 'A' && c <= 'F' ? c - 'A' + 10
71: : c - 'a' + 10; /* accept small letters just in case */
1.96 cvs 72: }
73:
74: /*----------------------------------------------------------------------
1.75 cvs 75: EscapeURL
76: Takes a URL and escapes all protected chars into
77: %xx sequences. Also, removes any leading white spaces
78: Returns either NULL or a new buffer, which must be freed by the caller
79: ----------------------------------------------------------------------*/
1.106 cvs 80: char *EscapeURL (const char *url)
81: {
82: char *buffer;
83: int buffer_len;
84: int buffer_free_mem;
85: char *ptr;
86: int new_chars;
1.75 cvs 87: void *status;
88:
89: if (url && *url)
90: {
1.106 cvs 91: buffer_free_mem = strlen (url) + 20;
92: buffer = TtaGetMemory (buffer_free_mem + 1);
1.107 kahan 93: ptr = (char *) url;
1.75 cvs 94: buffer_len = 0;
95:
96: while (*ptr)
97: {
98: switch (*ptr)
99: {
100: /* put here below all the chars that need to
101: be escaped into %xx */
1.81 cvs 102: case 0x20: /* space */
1.143 vatton 103: /*case 0x26:*/ /* & */
1.140 kahan 104: case 0x27: /* antislash */
1.75 cvs 105: new_chars = 3;
106: break;
107:
108: default:
1.122 kahan 109: if ((unsigned char )*ptr > 127)
110: new_chars = 3;
111: else
112: new_chars = 1;
1.75 cvs 113: break;
114: }
115:
116: /* see if we need extra room in the buffer */
117: if (new_chars > buffer_free_mem)
118: {
1.76 cvs 119: buffer_free_mem = 20;
1.106 cvs 120: status = TtaRealloc (buffer, sizeof (char)
1.75 cvs 121: * (buffer_len + buffer_free_mem + 1));
122: if (status)
1.114 cvs 123: buffer = (char *) status;
1.106 cvs 124: else
125: {
126: /* @@ maybe we should do some other behavior here, like
127: freeing the buffer and return a void thing */
128: buffer[buffer_len] = EOS;
129: break;
130: }
1.75 cvs 131: }
132: /* escape the char */
1.140 kahan 133: if (new_chars == 3)
134: {
1.106 cvs 135: buffer[buffer_len] = '%';
1.75 cvs 136: EscapeChar (&buffer[buffer_len+1], *ptr);
137: }
1.140 kahan 138: else
139: buffer[buffer_len] = *ptr;
140:
141: /* update the status */
142: buffer_len += new_chars;
143: buffer_free_mem -= new_chars;
144: /* examine the next char */
145: ptr++;
146: }
147: buffer[buffer_len] = EOS;
148: }
149: else
150: buffer = NULL;
151:
152: return (buffer);
153: }
154:
155: /*----------------------------------------------------------------------
156: EscapeXML
157: Takes a string and escapes all protected chars into entity
158: sequences.
159: Returns either NULL or a new buffer, which must be freed by the caller
160: ----------------------------------------------------------------------*/
161: char *EscapeXML (const char *string)
162: {
163: char *buffer;
164: int buffer_len;
165: int buffer_free_mem;
166: char *ptr;
167: char *entity = NULL;
168: int new_chars;
169: void *status;
170:
171: if (string && *string)
172: {
173: buffer_free_mem = strlen (string) + 20;
174: buffer = TtaGetMemory (buffer_free_mem + 1);
175: ptr = (char *) string;
176: buffer_len = 0;
177:
178: while (*ptr)
179: {
180: switch (*ptr)
181: {
182: case 0x26: /* & */
183: entity = "&";
1.141 kahan 184: new_chars = sizeof (entity) - 1;
185: break;
1.140 kahan 186:
1.141 kahan 187: case '<': /* < */
1.140 kahan 188: entity = "<";
1.141 kahan 189: new_chars = sizeof (entity) - 1;
1.140 kahan 190: break;
191:
1.141 kahan 192: case '>': /* > */
1.140 kahan 193: entity = ">";
1.141 kahan 194: new_chars = sizeof (entity) - 1;
195: break;
196:
197: case '"': /* "e; */
198: entity = ""e;";
199: new_chars = sizeof (entity) - 1;
1.140 kahan 200: break;
201:
202: default:
203: new_chars = 1;
204: break;
205: }
206:
207: /* see if we need extra room in the buffer */
208: if (new_chars > buffer_free_mem)
209: {
210: buffer_free_mem = 20;
211: status = TtaRealloc (buffer, sizeof (char)
212: * (buffer_len + buffer_free_mem + 1));
213: if (status)
214: buffer = (char *) status;
215: else
216: {
217: /* @@ maybe we should do some other behavior here, like
218: freeing the buffer and return a void thing */
219: buffer[buffer_len] = EOS;
220: break;
221: }
222: }
223: /* escape the char */
224: if (entity)
225: {
226: sprintf (&buffer[buffer_len], "%s", entity);
227: entity = NULL;
228: }
1.75 cvs 229: else
230: buffer[buffer_len] = *ptr;
231:
232: /* update the status */
233: buffer_len += new_chars;
234: buffer_free_mem -= new_chars;
235: /* examine the next char */
236: ptr++;
237: }
1.106 cvs 238: buffer[buffer_len] = EOS;
1.75 cvs 239: }
1.76 cvs 240: else
241: buffer = NULL;
242:
1.75 cvs 243: return (buffer);
1.122 kahan 244: }
245:
1.75 cvs 246:
247: /*----------------------------------------------------------------------
1.11 cvs 248: ExplodeURL
1.8 cvs 249: ----------------------------------------------------------------------*/
1.106 cvs 250: void ExplodeURL (char *url, char **proto, char **host, char **dir,
251: char **file)
1.8 cvs 252: {
1.33 cvs 253: char *curr, *temp;
254: char used_sep;
1.32 cvs 255:
1.33 cvs 256: if (url && strchr (url, URL_SEP))
257: used_sep = URL_SEP;
258: else
259: used_sep = DIR_SEP;
1.8 cvs 260:
261: if ((url == NULL) || (proto == NULL) || (host == NULL) ||
262: (dir == NULL) || (file == NULL))
263: return;
264:
265: /* initialize every pointer */
266: *proto = *host = *dir = *file = NULL;
267:
268: /* skip any leading space */
269: while ((*url == SPACE) || (*url == TAB))
270: url++;
1.9 cvs 271: curr = url;
272: if (*curr == 0)
1.8 cvs 273: goto finished;
274:
275: /* go to the end of the URL */
1.68 cvs 276: while ((*curr != EOS) && (*curr != SPACE) && (*curr != BSPACE) &&
277: (*curr != __CR__) && (*curr != EOL))
1.9 cvs 278: curr++;
1.8 cvs 279:
280: /* mark the end of the chain */
1.9 cvs 281: *curr = EOS;
282: curr--;
283: if (curr <= url)
1.8 cvs 284: goto finished;
285:
286: /* search the next DIR_SEP indicating the beginning of the file name */
287: do
1.11 cvs 288: curr--;
1.33 cvs 289: while ((curr >= url) && (*curr != used_sep));
1.11 cvs 290:
1.9 cvs 291: if (curr < url)
1.8 cvs 292: goto finished;
1.9 cvs 293: *file = curr + 1;
1.8 cvs 294:
295: /* mark the end of the dir */
1.9 cvs 296: *curr = EOS;
297: curr--;
298: if (curr < url)
1.8 cvs 299: goto finished;
300:
1.29 cvs 301: /* search for the DIR_STR indicating the host name start */
1.33 cvs 302: while ((curr > url) && ((*curr != used_sep) || (*(curr + 1) != used_sep)))
1.9 cvs 303: curr--;
1.8 cvs 304:
305: /* if we found it, separate the host name from the directory */
1.102 kahan 306: if ((*curr == used_sep) && (*(curr + 1) == used_sep))
1.8 cvs 307: {
1.9 cvs 308: *host = temp = curr + 2;
1.33 cvs 309: while ((*temp != 0) && (*temp != used_sep))
1.8 cvs 310: temp++;
1.33 cvs 311: if (*temp == used_sep)
1.8 cvs 312: {
313: *temp = EOS;
314: *dir = temp + 1;
315: }
316: }
317: else
1.11 cvs 318: *dir = curr;
319:
1.9 cvs 320: if (curr <= url)
1.8 cvs 321: goto finished;
322:
323: /* mark the end of the proto */
1.9 cvs 324: *curr = EOS;
325: curr--;
326: if (curr < url)
1.8 cvs 327: goto finished;
328:
1.106 cvs 329: if (*curr == ':')
1.8 cvs 330: {
1.9 cvs 331: *curr = EOS;
332: curr--;
1.8 cvs 333: }
334: else
335: goto finished;
1.11 cvs 336:
1.9 cvs 337: if (curr < url)
1.8 cvs 338: goto finished;
1.9 cvs 339: while ((curr > url) && (isalpha (*curr)))
340: curr--;
341: *proto = curr;
1.8 cvs 342:
343: finished:;
344:
345: #ifdef AMAYA_DEBUG
346: fprintf (stderr, "ExplodeURL(%s)\n\t", url);
347: if (*proto)
348: fprintf (stderr, "proto : %s, ", *proto);
349: if (*host)
350: fprintf (stderr, "host : %s, ", *host);
351: if (*dir)
352: fprintf (stderr, "dir : %s, ", *dir);
353: if (*file)
354: fprintf (stderr, "file : %s ", *file);
355: fprintf (stderr, "\n");
356: #endif
357:
358: }
1.3 cvs 359:
1.116 kahan 360: /*----------------------------------------------------------------------
361: PicTypeToMime
362: Converts a Thot PicType into the equivalent MIME type. If no convertion
363: is possible, it returns NULL.
364: ----------------------------------------------------------------------*/
365: char *PicTypeToMIME (PicType contentType)
366: {
367: char *mime_type;
368:
369: switch (contentType)
370: {
371: case xbm_type:
372: mime_type ="image/x-xbitmap";
373: break;
374: case eps_type:
375: mime_type ="application/postscript";
376: break;
377: case xpm_type:
378: mime_type ="image/x-xpicmap";
379: break;
380: case gif_type:
381: mime_type ="image/gif";
382: break;
383: case jpeg_type:
384: mime_type ="image/jpeg";
385: break;
386: case png_type:
387: mime_type ="image/png";
388: break;
389: case svg_type:
1.138 kahan 390: mime_type =AM_SVG_MIME_TYPE;
1.116 kahan 391: break;
392: case unknown_type:
393: default:
394: mime_type = NULL;
395: }
396:
397: return mime_type;
398: }
1.61 cvs 399:
400: /*----------------------------------------------------------------------
1.117 kahan 401: ImageElement
402: Returns the element (image parameter) and URL (url parameter) of an
403: image in a docImage document. The user must free the memory associated
1.120 kahan 404: with the url parameter if the function is succesful.
405: If the url parameter is NULL, we won't initialize it.
1.117 kahan 406: Returns TRUE if succesful, FALSE otherwise.
407: ----------------------------------------------------------------------*/
408: ThotBool ImageElement (Document doc, char **url, Element *image)
409: {
410: Element el, imgEl;
411: Attribute attr, srcAttr;
412: AttributeType attrType;
413: int length;
414: char *value;
415:
416: if (DocumentTypes[doc] != docImage)
417: return FALSE;
418:
419: /* find the value of the src attribute */
420: attrType.AttrSSchema = TtaGetSSchema ("HTML", doc);
421: attrType.AttrTypeNum = HTML_ATTR_SRC;
422: el = TtaGetRootElement (doc);
423: TtaSearchAttribute (attrType, SearchInTree, el, &imgEl, &srcAttr);
424:
425: if (!imgEl)
426: return FALSE;
427: *image = imgEl;
428:
1.120 kahan 429: if (url)
430: {
431: attr = TtaGetAttribute (imgEl, attrType);
432: length = TtaGetTextAttributeLength (srcAttr) + 1;
433: value = TtaGetMemory (length);
434: TtaGiveTextAttributeValue (srcAttr, value, &length);
435: *url = value;
436: }
1.117 kahan 437: return TRUE;
438: }
439:
440: /*----------------------------------------------------------------------
441: DocImageMimeType
442: Returns the MIME type of a docImage document.
443: ----------------------------------------------------------------------*/
444: char *DocImageMimeType (Document doc)
445: {
446: char *mime_type;
447: LoadedImageDesc *pImage;
448: PicType type;
449: Element image;
450:
451: if (DocumentTypes[doc] != docImage)
452: return NULL;
453:
454: mime_type = NULL;
455: if (!IsHTTPPath (DocumentURLs[doc]))
456: {
457: /* it is a local image */
1.120 kahan 458: if (ImageElement (doc, NULL, &image))
1.117 kahan 459: {
460: type = TtaGetPictureType (image);
461: mime_type = PicTypeToMIME (type);
462: }
463: }
464: else
465: {
466: /* find the value of the src attribute */
467: pImage = ImageURLs;
468: while (pImage != NULL)
469: {
470: if (pImage->document == doc)
471: {
472: if (pImage->content_type)
473: mime_type = pImage->content_type;
474: else if (pImage->elImage && pImage->elImage->currentElement)
475: {
476: type = TtaGetPictureType (pImage->elImage->currentElement);
477: mime_type = PicTypeToMIME (type);
478: }
479: break;
480: }
481: }
482: }
483: return (mime_type);
484: }
485:
1.4 cvs 486: /*----------------------------------------------------------------------
1.9 cvs 487: IsHTMLName
488: returns TRUE if path points to an HTML resource.
1.4 cvs 489: ----------------------------------------------------------------------*/
1.109 cvs 490: ThotBool IsHTMLName (const char *path)
1.106 cvs 491: {
1.136 cvs 492: char temppath[MAX_LENGTH];
493: char suffix[MAX_LENGTH];
494: char nsuffix[MAX_LENGTH];
495: int i;
1.5 cvs 496:
1.101 cvs 497: if (!path)
498: return (FALSE);
1.5 cvs 499:
1.106 cvs 500: strcpy (temppath, path);
1.124 vatton 501: TtaExtractSuffix (temppath, suffix);
1.101 cvs 502: i = 0;
1.106 cvs 503: while (suffix[i] != EOS)
1.101 cvs 504: {
505: /* Normalize the suffix */
506: i = 0;
1.106 cvs 507: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101 cvs 508: {
1.123 vatton 509: nsuffix[i] = tolower (suffix[i]);
1.101 cvs 510: i++;
511: }
1.106 cvs 512: nsuffix[i] = EOS;
513: if (!strcmp (nsuffix, "html") ||
514: !strcmp (nsuffix, "htm") ||
515: !strcmp (nsuffix, "shtml") ||
516: !strcmp (nsuffix, "jsp") ||
517: !strcmp (nsuffix, "xht") ||
518: !strcmp (nsuffix, "xhtm") ||
1.144 cvs 519: !strcmp (nsuffix, "lhtml") ||
1.106 cvs 520: !strcmp (nsuffix, "xhtml"))
1.101 cvs 521: return (TRUE);
1.106 cvs 522: else if (!strcmp (nsuffix, "gz"))
1.101 cvs 523: {
524: /* take into account compressed files */
1.124 vatton 525: TtaExtractSuffix (temppath, suffix);
1.101 cvs 526: /* Normalize the suffix */
527: i = 0;
1.106 cvs 528: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101 cvs 529: {
1.123 vatton 530: nsuffix[i] = tolower (suffix[i]);
1.101 cvs 531: i++;
532: }
1.106 cvs 533: nsuffix[i] = EOS;
534: if (!strcmp (nsuffix, "html") ||
535: !strcmp (nsuffix, "htm") ||
536: !strcmp (nsuffix, "shtml") ||
537: !strcmp (nsuffix, "jsp") ||
538: !strcmp (nsuffix, "xht") ||
539: !strcmp (nsuffix, "xhtm") ||
1.144 cvs 540: !strcmp (nsuffix, "lhtml") ||
1.106 cvs 541: !strcmp (nsuffix, "xhtml"))
1.101 cvs 542: return (TRUE);
543: else
544: return (FALSE);
545: }
546: else
547: /* check if there is another suffix */
1.124 vatton 548: TtaExtractSuffix (temppath, suffix);
1.101 cvs 549: }
1.88 cvs 550: return (FALSE);
1.3 cvs 551: }
552:
1.4 cvs 553: /*----------------------------------------------------------------------
1.136 cvs 554: IsMathMLName
555: returns TRUE if path points to an MathML resource.
1.56 cvs 556: ----------------------------------------------------------------------*/
1.136 cvs 557: ThotBool IsMathMLName (const char *path)
1.56 cvs 558: {
1.136 cvs 559: char temppath[MAX_LENGTH];
560: char suffix[MAX_LENGTH];
1.56 cvs 561:
562: if (!path)
563: return (FALSE);
564:
1.106 cvs 565: strcpy (temppath, path);
1.124 vatton 566: TtaExtractSuffix (temppath, suffix);
1.56 cvs 567:
1.136 cvs 568: if (!strcasecmp (suffix, "mml"))
1.56 cvs 569: return (TRUE);
1.106 cvs 570: else if (!strcmp (suffix, "gz"))
1.56 cvs 571: {
572: /* take into account compressed files */
1.124 vatton 573: TtaExtractSuffix (temppath, suffix);
1.136 cvs 574: if (!strcasecmp (suffix, "mml"))
1.60 cvs 575: return (TRUE);
576: else
577: return (FALSE);
578: }
579: else
580: return (FALSE);
581: }
582:
583: /*----------------------------------------------------------------------
1.136 cvs 584: IsSVGName
585: returns TRUE if path points to an SVG resource.
1.133 vatton 586: ----------------------------------------------------------------------*/
1.136 cvs 587: ThotBool IsSVGName (const char *path)
1.133 vatton 588: {
1.136 cvs 589: char temppath[MAX_LENGTH];
590: char suffix[MAX_LENGTH];
1.133 vatton 591:
592: if (!path)
593: return (FALSE);
594:
595: strcpy (temppath, path);
596: TtaExtractSuffix (temppath, suffix);
597:
1.136 cvs 598: if (!strcasecmp (suffix, "svg"))
1.133 vatton 599: return (TRUE);
600: else if (!strcmp (suffix, "gz"))
601: {
602: /* take into account compressed files */
603: TtaExtractSuffix (temppath, suffix);
1.136 cvs 604: if (!strcasecmp (suffix, "svg"))
1.133 vatton 605: return (TRUE);
606: else
607: return (FALSE);
608: }
609: else
610: return (FALSE);
611: }
612:
613: /*----------------------------------------------------------------------
1.136 cvs 614: IsXMLName
615: returns TRUE if path points to an XML resource.
1.103 cvs 616: ----------------------------------------------------------------------*/
1.136 cvs 617: ThotBool IsXMLName (const char *path)
1.103 cvs 618: {
1.136 cvs 619: char temppath[MAX_LENGTH];
620: char suffix[MAX_LENGTH];
1.103 cvs 621:
622: if (!path)
623: return (FALSE);
624:
1.106 cvs 625: strcpy (temppath, path);
1.124 vatton 626: TtaExtractSuffix (temppath, suffix);
1.103 cvs 627:
1.136 cvs 628: if (!strcasecmp (suffix, "xml") ||
629: !strcasecmp (suffix, "xht") ||
630: !strcmp (suffix, "xhtm") ||
1.145 kahan 631: !strcmp (suffix, "xhtml") ||
632: !strcmp (suffix, "smi"))
1.103 cvs 633: return (TRUE);
1.106 cvs 634: else if (!strcmp (suffix, "gz"))
1.103 cvs 635: {
636: /* take into account compressed files */
1.124 vatton 637: TtaExtractSuffix (temppath, suffix);
1.136 cvs 638: if (!strcasecmp (suffix, "xml") ||
639: !strcasecmp (suffix, "xht") ||
640: !strcmp (suffix, "xhtm") ||
1.145 kahan 641: !strcmp (suffix, "xhtml") ||
642: !strcmp (suffix, "smi"))
1.103 cvs 643: return (TRUE);
644: else
645: return (FALSE);
646: }
647: else
648: return (FALSE);
649: }
650:
651: /*----------------------------------------------------------------------
1.136 cvs 652: IsUndisplayedName
653: returns TRUE if path points to an undisplayed resource.
1.103 cvs 654: ----------------------------------------------------------------------*/
1.136 cvs 655: ThotBool IsUndisplayedName (const char *path)
1.103 cvs 656: {
1.106 cvs 657: char temppath[MAX_LENGTH];
658: char suffix[MAX_LENGTH];
1.103 cvs 659:
660: if (!path)
661: return (FALSE);
662:
1.106 cvs 663: strcpy (temppath, path);
1.124 vatton 664: TtaExtractSuffix (temppath, suffix);
1.103 cvs 665:
1.136 cvs 666: if (!strcasecmp (suffix, "exe") ||
667: !strcasecmp (suffix, "zip") ||
668: !strcasecmp (suffix, "ppt") ||
669: !strcasecmp (suffix, "pdf") ||
670: !strcasecmp (suffix, "ps") ||
671: !strcasecmp (suffix, "eps") ||
672: !strcasecmp (suffix, "tar") ||
673: !strcasecmp (suffix, "tgz") ||
674: !strcasecmp (suffix, "ddl") ||
675: !strcasecmp (suffix, "o"))
1.103 cvs 676: return (TRUE);
1.106 cvs 677: else if (!strcmp (suffix, "gz"))
1.103 cvs 678: {
679: /* take into account compressed files */
1.124 vatton 680: TtaExtractSuffix (temppath, suffix);
1.136 cvs 681: if (!strcasecmp (suffix, "exe") ||
682: !strcasecmp (suffix, "zip") ||
683: !strcasecmp (suffix, "ppt") ||
684: !strcasecmp (suffix, "pdf") ||
685: !strcasecmp (suffix, "ps") ||
686: !strcasecmp (suffix, "eps") ||
687: !strcasecmp (suffix, "tar") ||
688: !strcasecmp (suffix, "ddl") ||
689: !strcasecmp (suffix, "o"))
1.103 cvs 690: return (TRUE);
691: else
692: return (FALSE);
693: }
694: else
695: return (FALSE);
696: }
697:
698: /*----------------------------------------------------------------------
1.60 cvs 699: IsCSSName
700: returns TRUE if path points to an XML resource.
701: ----------------------------------------------------------------------*/
1.111 cvs 702: ThotBool IsCSSName (const char *path)
1.60 cvs 703: {
1.106 cvs 704: char temppath[MAX_LENGTH];
705: char suffix[MAX_LENGTH];
1.60 cvs 706:
707: if (!path)
708: return (FALSE);
709:
1.106 cvs 710: strcpy (temppath, path);
1.124 vatton 711: TtaExtractSuffix (temppath, suffix);
1.60 cvs 712:
1.106 cvs 713: if (!strcasecmp (suffix, "css"))
1.60 cvs 714: return (TRUE);
1.106 cvs 715: else if (!strcmp (suffix, "gz"))
1.60 cvs 716: {
717: /* take into account compressed files */
1.124 vatton 718: TtaExtractSuffix (temppath, suffix);
1.106 cvs 719: if (!strcasecmp (suffix, "css"))
1.56 cvs 720: return (TRUE);
721: else
722: return (FALSE);
723: }
724: else
725: return (FALSE);
726: }
727:
728: /*----------------------------------------------------------------------
1.9 cvs 729: IsImageName
730: returns TRUE if path points to an image resource.
1.4 cvs 731: ----------------------------------------------------------------------*/
1.111 cvs 732: ThotBool IsImageName (const char *path)
1.106 cvs 733: {
734: char temppath[MAX_LENGTH];
735: char suffix[MAX_LENGTH];
736: char nsuffix[MAX_LENGTH];
1.5 cvs 737: int i;
738:
739: if (!path)
1.13 cvs 740: return (FALSE);
1.5 cvs 741:
1.106 cvs 742: strcpy (temppath, path);
1.124 vatton 743: TtaExtractSuffix (temppath, suffix);
1.5 cvs 744:
745: /* Normalize the suffix */
746: i = 0;
1.106 cvs 747: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13 cvs 748: {
1.123 vatton 749: nsuffix[i] = tolower (suffix[i]);
1.13 cvs 750: i++;
751: }
1.106 cvs 752: nsuffix[i] = EOS;
753: if ((!strcmp (nsuffix, "gif")) || (!strcmp (nsuffix, "xbm")) ||
754: (!strcmp (nsuffix, "xpm")) || (!strcmp (nsuffix, "jpg")) ||
755: (!strcmp (nsuffix, "png")) || (!strcmp (nsuffix, "au")))
1.39 cvs 756: return (TRUE);
757: return (FALSE);
1.3 cvs 758: }
759:
1.4 cvs 760: /*----------------------------------------------------------------------
1.58 cvs 761: IsImageType
762: returns TRUE if type points to an image resource.
763: ----------------------------------------------------------------------*/
1.111 cvs 764: ThotBool IsImageType (const char *type)
1.58 cvs 765: {
1.106 cvs 766: char temptype[MAX_LENGTH];
1.58 cvs 767: int i;
768:
769: if (!type)
770: return (FALSE);
771:
1.106 cvs 772: strcpy (temptype, type);
1.58 cvs 773: /* Normalize the type */
774: i = 0;
1.106 cvs 775: while (temptype[i] != EOS)
1.58 cvs 776: {
777: temptype[i] = tolower (temptype[i]);
778: i++;
779: }
1.111 cvs 780: if (!strcmp (temptype, "gif") || !strcmp (temptype, "x-xbitmap") ||
781: !strcmp (temptype, "x-xpixmap") || !strcmp (temptype, "jpeg") ||
782: !strcmp (temptype, "png"))
1.58 cvs 783: return (TRUE);
784: return (FALSE);
785: }
786:
787: /*----------------------------------------------------------------------
1.9 cvs 788: IsTextName
1.4 cvs 789: ----------------------------------------------------------------------*/
1.111 cvs 790: ThotBool IsTextName (const char *path)
1.106 cvs 791: {
792: char temppath[MAX_LENGTH];
793: char suffix[MAX_LENGTH];
794: char nsuffix[MAX_LENGTH];
1.5 cvs 795: int i;
796:
797: if (!path)
1.13 cvs 798: return (FALSE);
1.5 cvs 799:
1.106 cvs 800: strcpy (temppath, path);
1.124 vatton 801: TtaExtractSuffix (temppath, suffix);
1.5 cvs 802:
803: /* Normalize the suffix */
804: i = 0;
1.106 cvs 805: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.5 cvs 806: {
1.25 cvs 807: nsuffix[i] = tolower (suffix[i]);
1.5 cvs 808: i++;
809: }
1.106 cvs 810: nsuffix[i] = EOS;
1.5 cvs 811:
1.111 cvs 812: if (!strcmp (nsuffix, "txt") || !strcmp (nsuffix, "dtd"))
1.13 cvs 813: return (TRUE);
1.106 cvs 814: else if (!strcmp (nsuffix, "gz"))
1.13 cvs 815: {
1.39 cvs 816: /* take into account compressed files */
1.124 vatton 817: TtaExtractSuffix (temppath, suffix);
1.13 cvs 818: /* Normalize the suffix */
819: i = 0;
1.106 cvs 820: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13 cvs 821: {
1.25 cvs 822: nsuffix[i] = tolower (suffix[i]);
1.13 cvs 823: i++;
824: }
1.106 cvs 825: nsuffix[i] = EOS;
1.111 cvs 826: if (!strcmp (nsuffix, "txt") || !strcmp (nsuffix, "dtd"))
1.13 cvs 827: return (TRUE);
828: else
829: return (FALSE);
830: }
831: else
832: return (FALSE);
1.3 cvs 833: }
834:
1.4 cvs 835: /*----------------------------------------------------------------------
1.9 cvs 836: IsHTTPPath
837: returns TRUE if path is in fact an http URL.
1.4 cvs 838: ----------------------------------------------------------------------*/
1.112 cvs 839: ThotBool IsHTTPPath (const char *path)
1.3 cvs 840: {
1.5 cvs 841: if (!path)
842: return FALSE;
1.3 cvs 843:
1.106 cvs 844: if ((!strncmp (path, "http:", 5) != 0)
845: || (AHTFTPURL_flag () && !strncmp (path, "ftp:", 4))
846: || !strncmp (path, "internal:", 9))
1.58 cvs 847: return TRUE;
848: return FALSE;
1.3 cvs 849: }
850:
1.4 cvs 851: /*----------------------------------------------------------------------
1.9 cvs 852: IsWithParameters
853: returns TRUE if url has a concatenated query string.
1.4 cvs 854: ----------------------------------------------------------------------*/
1.133 vatton 855: ThotBool IsWithParameters (const char *url)
1.3 cvs 856: {
1.5 cvs 857: int i;
1.3 cvs 858:
1.9 cvs 859: if ((!url) || (url[0] == EOS))
1.5 cvs 860: return FALSE;
1.3 cvs 861:
1.9 cvs 862: i = strlen (url) - 1;
863: while (i > 0 && url[i--] != '?')
1.5 cvs 864: if (i < 0)
865: return FALSE;
1.3 cvs 866:
1.5 cvs 867: /* There is a parameter */
868: return TRUE;
1.3 cvs 869: }
870:
1.4 cvs 871: /*----------------------------------------------------------------------
1.9 cvs 872: IsW3Path
873: returns TRUE if path is in fact a URL.
1.4 cvs 874: ----------------------------------------------------------------------*/
1.133 vatton 875: ThotBool IsW3Path (const char *path)
1.106 cvs 876: {
877: if (strncmp (path, "http:", 5) &&
878: strncmp (path, "ftp:", 4) &&
879: strncmp (path, "telnet:", 7) &&
880: strncmp (path, "wais:", 5) &&
881: strncmp (path, "news:", 5) &&
882: strncmp (path, "gopher:", 7) &&
883: strncmp (path, "mailto:", 7) &&
1.132 cheyroul 884: strncmp (path, "archie:", 7) &&
885: strncmp (path, "https:", 6))
1.72 cvs 886: return FALSE;
887: return TRUE;
1.3 cvs 888: }
889:
1.4 cvs 890: /*----------------------------------------------------------------------
1.90 cvs 891: IsFilePath
892: returns TRUE if path is in fact a URL.
893: ----------------------------------------------------------------------*/
1.133 vatton 894: ThotBool IsFilePath (const char *path)
1.90 cvs 895: {
1.106 cvs 896: if (strncmp (path, "file:", 5))
1.90 cvs 897: return FALSE;
898: return TRUE;
899: }
900:
901: /*----------------------------------------------------------------------
1.9 cvs 902: IsValidProtocol
903: returns true if the url protocol is supported by Amaya.
1.4 cvs 904: ----------------------------------------------------------------------*/
1.133 vatton 905: ThotBool IsValidProtocol (const char *url)
1.106 cvs 906: {
907: if (!strncmp (url, "http:", 5)
908: || !strncmp (url, "internal:", 9)
909: || (AHTFTPURL_flag () && !strncmp (url, "ftp:", 4)))
1.22 cvs 910: /* experimental */
1.24 cvs 911: /*** || !strncmp (path, "news:", 5)***/
1.8 cvs 912: return (TRUE);
1.5 cvs 913: else
1.8 cvs 914: return (FALSE);
1.3 cvs 915: }
916:
1.31 cvs 917:
918: /*----------------------------------------------------------------------
919: GetBaseURL
920: normalizes orgName according to a base associated with doc, and
921: following the standard URL format rules.
922: The function returns the base used to solve relative URL and SRC:
923: - the base of the document,
924: - or the document path (without document name).
925: ----------------------------------------------------------------------*/
1.106 cvs 926: char *GetBaseURL (Document doc)
1.31 cvs 927: {
928: Element el;
929: ElementType elType;
930: AttributeType attrType;
931: Attribute attr;
1.106 cvs 932: char *ptr, *basename;
1.31 cvs 933: int length;
934:
1.113 cvs 935: if (doc == 0 || !DocumentURLs[doc])
1.110 cvs 936: return NULL;
1.148 kahan 937: /* the other functions expect basename to have no more than MAX_LENGTH chars */
1.106 cvs 938: basename = TtaGetMemory (MAX_LENGTH);
1.148 kahan 939: basename[0] = EOS;
1.31 cvs 940: length = MAX_LENGTH -1;
1.148 kahan 941:
942: /* do we have a location header? */
943: if (DocumentMeta[doc] && DocumentMeta[doc]->full_content_location
944: && DocumentMeta[doc]->full_content_location[0] != EOS)
1.65 cvs 945: {
1.148 kahan 946: strncpy (basename, DocumentMeta[doc]->full_content_location, MAX_LENGTH-1);
947: basename[MAX_LENGTH-1] = EOS;
948: length = strlen (basename);
949: }
950: else
951: {
952: /* is it a HTML document ? */
953: elType.ElSSchema = TtaGetDocumentSSchema (doc);
954: if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
955: /* it's a HTML document */
1.113 cvs 956: {
1.148 kahan 957: /* get the document element */
958: el = TtaGetMainRoot (doc);
959: /* search the BASE element */
960: elType.ElTypeNum = HTML_EL_HEAD;
961: el = TtaSearchTypedElement (elType, SearchForward, el);
962: if (el)
963: /* there is a HEAD element */
964: {
965: /* look for a BASE element within the HEAD */
966: elType.ElTypeNum = HTML_EL_BASE;
967: el = TtaSearchTypedElement (elType, SearchInTree, el);
968: }
969: if (el)
970: {
971: /* The document has a BASE element. Get the HREF attribute of the
972: BASE element */
973: attrType.AttrSSchema = elType.ElSSchema;
974: attrType.AttrTypeNum = HTML_ATTR_HREF_;
975: attr = TtaGetAttribute (el, attrType);
976: if (attr)
977: {
978: /* Use the base path of the document */
979: TtaGiveTextAttributeValue (attr, basename, &length);
980: }
981: }
1.113 cvs 982: }
1.148 kahan 983: }
984:
985: if (basename[0] != EOS)
986: {
987: /* base and orgName have to be separated by a DIR_SEP */
988: length--;
989: if (basename[0] != EOS && basename[length] != URL_SEP &&
990: basename[length] != DIR_SEP)
991: /* verify if the base has the form "protocol://server:port" */
1.31 cvs 992: {
1.148 kahan 993: ptr = AmayaParseUrl (basename, "", AMAYA_PARSE_ACCESS |
994: AMAYA_PARSE_HOST |
995: AMAYA_PARSE_PUNCTUATION);
996: if (ptr && !strcmp (ptr, basename))
1.31 cvs 997: {
1.148 kahan 998: /* it has this form, complete it by adding a URL_STR */
999: if (strchr (basename, DIR_SEP))
1000: strcat (basename, DIR_STR);
1001: else
1002: strcat (basename, URL_STR);
1003: length++;
1.31 cvs 1004: }
1.149 kahan 1005: else if (!ptr || ptr[0] == EOS)
1006: {
1007: /* no host was detected, we may have a relative URL. We test
1008: if it begins with a URL_SEP, DIR_SEP or period. If yes, it's
1009: relative. */
1010: if (! (basename[0] == '.' || basename[0] == URL_SEP
1011: || basename[0] == DIR_SEP))
1012: basename[0] = EOS;
1013: }
1.148 kahan 1014: if (ptr)
1015: TtaFreeMemory (ptr);
1.31 cvs 1016: }
1.113 cvs 1017: }
1018:
1.148 kahan 1019: /* there was no base element and no location header, we use the DocumentURL */
1020: if (basename[0] == EOS)
1021: {
1022: strncpy (basename, DocumentURLs[doc], MAX_LENGTH-1);
1023: basename[MAX_LENGTH-1] = EOS;
1024: }
1025:
1.31 cvs 1026: /* Remove anything after the last DIR_SEP char. If no such char is found,
1027: * then search for the first ":" char, hoping that what's before that is a
1028: * protocol. If found, end the string there. If neither char is found,
1029: * then discard the whole base element.
1030: */
1.106 cvs 1031: length = strlen (basename) - 1;
1.31 cvs 1032: /* search for the last DIR_SEP char */
1.106 cvs 1033: while (length >= 0 && basename[length] != URL_SEP && basename[length] != DIR_SEP)
1.31 cvs 1034: length--;
1035: if (length >= 0)
1036: /* found the last DIR_SEP char, end the string there */
1.106 cvs 1037: basename[length + 1] = EOS;
1.31 cvs 1038: else
1039: /* search for the first PATH_STR char */
1040: {
1.106 cvs 1041: for (length = 0; basename[length] != ':' &&
1042: basename[length] != EOS; length ++);
1043: if (basename[length] == ':')
1.31 cvs 1044: /* found, so end the string there */
1.106 cvs 1045: basename[length + 1] = EOS;
1.31 cvs 1046: else
1047: /* not found, discard the base */
1.106 cvs 1048: basename[0] = EOS;
1.31 cvs 1049: }
1050: return (basename);
1051: }
1052:
1053:
1.4 cvs 1054: /*----------------------------------------------------------------------
1.40 cvs 1055: GetLocalPath
1056: Allocate and return the local document path associated to the url
1057: ----------------------------------------------------------------------*/
1.150 ! vatton 1058: char *GetLocalPath (Document doc, char *url)
1.106 cvs 1059: {
1060: char *ptr;
1061: char *n;
1062: char *documentname;
1063: char url_sep;
1.83 cvs 1064: int len;
1.67 cvs 1065: ThotBool noFile;
1.40 cvs 1066:
1067: if (url != NULL)
1068: {
1069: /* check whether the file name exists */
1.106 cvs 1070: len = strlen (url) - 1;
1.71 cvs 1071: if (IsW3Path (url))
1.106 cvs 1072: url_sep = '/';
1.41 cvs 1073: else
1.106 cvs 1074: url_sep = DIR_SEP;
1.41 cvs 1075: noFile = (url[len] == url_sep);
1.40 cvs 1076: if (noFile)
1.106 cvs 1077: url[len] = EOS;
1078: ptr = TtaGetMemory (MAX_LENGTH);
1079: documentname = TtaGetMemory (MAX_LENGTH);
1.78 cvs 1080: TtaExtractName (url, ptr, documentname);
1.106 cvs 1081: sprintf (ptr, "%s%s%d%s", TempFileDirectory, DIR_STR, doc, DIR_STR);
1.40 cvs 1082: if (!TtaCheckDirectory (ptr))
1083: /* directory did not exist */
1.72 cvs 1084: TtaMakeDirectory (ptr);
1.47 cvs 1085:
1086: /* don't include the query string within document name */
1.106 cvs 1087: n = strrchr (documentname, '?');
1.47 cvs 1088: if (n != NULL)
1.106 cvs 1089: *n = EOS;
1.46 cvs 1090: /* don't include ':' within document name */
1.106 cvs 1091: n = strchr (documentname, ':');
1.46 cvs 1092: if (n != NULL)
1.106 cvs 1093: *n = EOS;
1.69 cvs 1094: /* if after all this operations document name
1095: is empty, let's use noname.html instead */
1.106 cvs 1096: if (documentname[0] == EOS)
1097: strcat (ptr, "noname.html");
1.69 cvs 1098: else
1.106 cvs 1099: strcat (ptr, documentname);
1.40 cvs 1100: TtaFreeMemory (documentname);
1101: /* restore the url */
1102: if (noFile)
1.41 cvs 1103: url[len] = url_sep;
1.40 cvs 1104: return (ptr);
1105: }
1106: else
1107: return (NULL);
1108: }
1109:
1.73 cvs 1110: /*----------------------------------------------------------------------
1.79 cvs 1111: ExtractTarget extract the target name from document nane.
1112: ----------------------------------------------------------------------*/
1.150 ! vatton 1113: void ExtractTarget (char *aName, char *target)
1.79 cvs 1114: {
1.106 cvs 1115: int lg, i;
1116: char *ptr;
1117: char *oldptr;
1.79 cvs 1118:
1119: if (!target || !aName)
1120: /* bad target */
1121: return;
1122:
1.106 cvs 1123: target[0] = EOS;
1124: lg = strlen (aName);
1.79 cvs 1125: if (lg)
1126: {
1127: /* the name is not empty */
1128: oldptr = ptr = &aName[0];
1129: do
1130: {
1.106 cvs 1131: ptr = strrchr (oldptr, '#');
1.79 cvs 1132: if (ptr)
1133: oldptr = &ptr[1];
1134: }
1135: while (ptr);
1136:
1137: i = (int) (oldptr) - (int) (aName); /* name length */
1138: if (i > 1)
1139: {
1.106 cvs 1140: aName[i - 1] = EOS;
1.79 cvs 1141: if (i != lg)
1.106 cvs 1142: strcpy (target, oldptr);
1.79 cvs 1143: }
1144: }
1145: }
1146:
1147: /*----------------------------------------------------------------------
1.90 cvs 1148: RemoveNewLines (text)
1149: Removes any '\n' chars that are found in text.
1150: Returns TRUE if it did the operation, FALSE otherwise.
1.73 cvs 1151: ----------------------------------------------------------------------*/
1.106 cvs 1152: ThotBool RemoveNewLines (char *text)
1153: {
1154: ThotBool change = FALSE;
1155: char *src;
1156: char *dest;
1.90 cvs 1157:
1158: src = text;
1159: dest = text;
1.115 kahan 1160:
1161: /* remove any preceding whitespace */
1162: while (*src && *src == ' ')
1163: {
1164: src++;
1165: change = 1;
1166: }
1167:
1.90 cvs 1168: while (*src)
1169: {
1170: switch (*src)
1171: {
1.106 cvs 1172: case '\n':
1.90 cvs 1173: /* don't copy the newline */
1174: change = 1;
1175: break;
1176: default:
1177: *dest = *src;
1178: dest++;
1179: break;
1180: }
1181: src++;
1182: }
1183: /* copy the last EOS char */
1184: *dest = *src;
1185:
1186: return (change);
1187: }
1188:
1189: /*----------------------------------------------------------------------
1190: CleanCopyFileURL
1191: Copies a file url from a src string to destination string.
1.97 cvs 1192: convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
1193: we want to do).
1.90 cvs 1194: ----------------------------------------------------------------------*/
1.106 cvs 1195: static void CleanCopyFileURL (char *dest, char *src,
1196: ConvertionType convertion)
1.90 cvs 1197: {
1198: while (*src)
1.89 cvs 1199: {
1.90 cvs 1200: switch (*src)
1.89 cvs 1201: {
1202: #ifdef _WINDOWS
1.106 cvs 1203: case URL_SEP:
1.96 cvs 1204: /* make DIR_SEP transformation */
1.97 cvs 1205: if (convertion & AM_CONV_URL_SEP)
1.106 cvs 1206: *dest = DIR_SEP;
1.96 cvs 1207: else
1208: *dest = *src;
1.90 cvs 1209: dest++;
1.96 cvs 1210: src++;
1.90 cvs 1211: break;
1.89 cvs 1212: #endif /* _WINDOWS */
1.96 cvs 1213:
1.106 cvs 1214: case '%':
1.97 cvs 1215: if (convertion & AM_CONV_PERCENT)
1.96 cvs 1216: {
1.97 cvs 1217: /* (code adapted from libwww's HTUnEscape function */
1.96 cvs 1218: src++;
1.106 cvs 1219: if (*src != EOS)
1.97 cvs 1220: {
1221: *dest = UnEscapeChar (*src) * 16;
1222: src++;
1223: }
1.106 cvs 1224: if (*src != EOS)
1.97 cvs 1225: {
1226: *dest = *dest + UnEscapeChar (*src);
1227: src++;
1228: }
1229: dest++;
1.96 cvs 1230: }
1.97 cvs 1231: else
1.96 cvs 1232: {
1.97 cvs 1233: *dest = *src;
1234: dest++;
1.96 cvs 1235: src++;
1236: }
1237: break;
1238:
1.90 cvs 1239: default:
1240: *dest = *src;
1.89 cvs 1241: dest++;
1.96 cvs 1242: src++;
1.90 cvs 1243: break;
1.89 cvs 1244: }
1245: }
1.90 cvs 1246: /* copy the EOS char */
1247: *dest = *src;
1.73 cvs 1248: }
1.40 cvs 1249:
1250: /*----------------------------------------------------------------------
1.9 cvs 1251: NormalizeURL
1252: normalizes orgName according to a base associated with doc, and
1253: following the standard URL format rules.
1.113 cvs 1254: if doc is < 0, use as a base the URL of the document that contains
1255: (or contained) the elements that are now in the copy/cut buffer.
1.53 cvs 1256: if doc is 0 and otherPath not NULL, normalizes orgName according to this
1257: other path.
1.9 cvs 1258: The function returns the new complete and normalized URL
1.12 cvs 1259: or file name path (newName) and the name of the document (docName).
1.9 cvs 1260: N.B. If the function can't find out what's the docName, it assigns
1261: the name "noname.html".
1.4 cvs 1262: ----------------------------------------------------------------------*/
1.106 cvs 1263: void NormalizeURL (char *orgName, Document doc, char *newName,
1264: char *docName, char *otherPath)
1265: {
1266: char *basename;
1267: char tempOrgName[MAX_LENGTH];
1268: char *ptr;
1269: char used_sep;
1.84 cvs 1270: int length;
1271: ThotBool check;
1.5 cvs 1272:
1.110 cvs 1273: #ifdef _WINDOWS
1.44 cvs 1274: int ndx;
1.110 cvs 1275: #endif /* _WINDOWS */
1.44 cvs 1276:
1.5 cvs 1277: if (!newName || !docName)
1278: return;
1.18 cvs 1279:
1.113 cvs 1280: if (doc < 0)
1281: basename = TtaStrdup (SavedDocumentURL);
1282: else if (doc > 0)
1.53 cvs 1283: basename = GetBaseURL (doc);
1284: else if (otherPath != NULL)
1.108 cvs 1285: basename = TtaStrdup (otherPath);
1.32 cvs 1286: else
1.53 cvs 1287: basename = NULL;
1.32 cvs 1288:
1.18 cvs 1289: /*
1.31 cvs 1290: * Clean orgName
1291: * Make sure we have a complete orgName, without any leading or trailing
1292: * white spaces, or trailinbg new lines
1293: */
1.5 cvs 1294: ptr = orgName;
1.18 cvs 1295: /* skip leading white space and new line characters */
1.106 cvs 1296: while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
1297: strncpy (tempOrgName, ptr, MAX_LENGTH -1);
1298: tempOrgName[MAX_LENGTH -1] = EOS;
1.18 cvs 1299: /*
1.31 cvs 1300: * Make orgName a complete URL
1301: * If the URL does not include a protocol, then try to calculate
1302: * one using the doc's base element (if it exists),
1303: */
1.106 cvs 1304: if (tempOrgName[0] == EOS)
1.53 cvs 1305: {
1.106 cvs 1306: newName[0] = EOS;
1307: docName[0] = EOS;
1.53 cvs 1308: TtaFreeMemory (basename);
1309: return;
1310: }
1.49 cvs 1311:
1312: /* clean trailing white space */
1.106 cvs 1313: length = strlen (tempOrgName) - 1;
1314: while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
1.53 cvs 1315: {
1.106 cvs 1316: tempOrgName[length] = EOS;
1.53 cvs 1317: length--;
1318: }
1.50 cvs 1319:
1.55 cvs 1320: /* remove extra dot (which dot???) */
1321: /* ugly, but faster than a strcmp */
1.106 cvs 1322: if (tempOrgName[length] == '.'
1323: && (length == 0 || tempOrgName[length-1] != '.'))
1324: tempOrgName[length] = EOS;
1.50 cvs 1325:
1.94 cvs 1326: if (IsW3Path (tempOrgName))
1.53 cvs 1327: {
1328: /* the name is complete, go to the Sixth Step */
1.106 cvs 1329: strcpy (newName, tempOrgName);
1.53 cvs 1330: SimplifyUrl (&newName);
1331: /* verify if the URL has the form "protocol://server:port" */
1.110 cvs 1332: ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS |
1333: AMAYA_PARSE_HOST |
1334: AMAYA_PARSE_PUNCTUATION);
1335: if (ptr && !strcmp (ptr, newName))
1336: /* it has this form, we complete it by adding a DIR_STR */
1.106 cvs 1337: strcat (newName, URL_STR);
1.49 cvs 1338:
1.53 cvs 1339: if (ptr)
1.50 cvs 1340: TtaFreeMemory (ptr);
1.53 cvs 1341: }
1.113 cvs 1342: else if (basename == NULL)
1.53 cvs 1343: /* the name is complete, go to the Sixth Step */
1.106 cvs 1344: strcpy (newName, tempOrgName);
1.53 cvs 1345: else
1346: {
1.31 cvs 1347: /* Calculate the absolute URL, using the base or document URL */
1.110 cvs 1348: #ifdef _WINDOWS
1.53 cvs 1349: if (!IsW3Path (basename))
1350: {
1.106 cvs 1351: length = strlen (tempOrgName);
1.53 cvs 1352: for (ndx = 0; ndx < length; ndx++)
1.106 cvs 1353: if (tempOrgName [ndx] == '/')
1354: tempOrgName [ndx] = '\\';
1.53 cvs 1355: }
1.110 cvs 1356: #endif /* _WINDOWS */
1.25 cvs 1357: ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53 cvs 1358: if (ptr)
1359: {
1360: SimplifyUrl (&ptr);
1.106 cvs 1361: strcpy (newName, ptr);
1.53 cvs 1362: TtaFreeMemory (ptr);
1363: }
1364: else
1.106 cvs 1365: newName[0] = EOS;
1.53 cvs 1366: }
1.36 cvs 1367:
1368: TtaFreeMemory (basename);
1.18 cvs 1369: /*
1.31 cvs 1370: * Prepare the docname that will refer to this ressource in the
1371: * .amaya directory. If the new URL finishes on DIR_SEP, then use
1372: * noname.html as a default ressource name
1.18 cvs 1373: */
1.106 cvs 1374: if (newName[0] != EOS)
1.53 cvs 1375: {
1.106 cvs 1376: length = strlen (newName) - 1;
1377: if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
1.53 cvs 1378: {
1379: used_sep = newName[length];
1380: check = TRUE;
1381: while (check)
1382: {
1.50 cvs 1383: length--;
1384: while (length >= 0 && newName[length] != used_sep)
1.53 cvs 1385: length--;
1.106 cvs 1386: if (!strncmp (&newName[length+1], "..", 2))
1.53 cvs 1387: {
1.106 cvs 1388: newName[length+1] = EOS;
1.53 cvs 1389: /* remove also previous directory */
1390: length--;
1391: while (length >= 0 && newName[length] != used_sep)
1392: length--;
1.106 cvs 1393: if (strncmp (&newName[length+1], "//", 2))
1.131 cheyroul 1394: /* don't remove server name */
1.106 cvs 1395: newName[length+1] = EOS;
1.53 cvs 1396: }
1.106 cvs 1397: else if (!strncmp (&newName[length+1], ".", 1))
1398: newName[length+1] = EOS;
1.50 cvs 1399: else
1.53 cvs 1400: check = FALSE;
1401: }
1402: /* docname was not comprised inside the URL, so let's */
1403: /* assign the default ressource name */
1.106 cvs 1404: strcpy (docName, "noname.html");
1.53 cvs 1405: }
1406: else
1407: { /* docname is comprised inside the URL */
1.110 cvs 1408: while (length >= 0 && newName[length] != URL_SEP &&
1409: newName[length] != DIR_SEP)
1.53 cvs 1410: length--;
1411: if (length < 0)
1.106 cvs 1412: strcpy (docName, newName);
1.53 cvs 1413: else
1.106 cvs 1414: strcpy (docName, &newName[length+1]);
1.53 cvs 1415: }
1416: }
1417: else
1.106 cvs 1418: docName[0] = EOS;
1.18 cvs 1419: }
1.3 cvs 1420:
1.4 cvs 1421: /*----------------------------------------------------------------------
1.9 cvs 1422: IsSameHost
1.4 cvs 1423: ----------------------------------------------------------------------*/
1.106 cvs 1424: ThotBool IsSameHost (const char *url1, const char *url2)
1.3 cvs 1425: {
1.106 cvs 1426: char *basename_ptr1, *basename_ptr2;
1427: ThotBool result;
1.3 cvs 1428:
1.106 cvs 1429: basename_ptr1 = AmayaParseUrl (url1, "",
1430: AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1431: basename_ptr2 = AmayaParseUrl (url2, "",
1432: AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3 cvs 1433:
1.106 cvs 1434: if (strcmp (basename_ptr1, basename_ptr2))
1435: result = FALSE;
1436: else
1437: result = TRUE;
1438: TtaFreeMemory (basename_ptr1);
1439: TtaFreeMemory (basename_ptr2);
1440: return (result);
1.3 cvs 1441: }
1442:
1443:
1.4 cvs 1444: /*----------------------------------------------------------------------
1.22 cvs 1445: HasKnownFileSuffix
1446: returns TRUE if path points to a file ending with a suffix.
1447: ----------------------------------------------------------------------*/
1.106 cvs 1448: ThotBool HasKnownFileSuffix (const char *path)
1449: {
1450: char *root;
1451: char temppath[MAX_LENGTH];
1452: char suffix[MAX_LENGTH];
1.22 cvs 1453:
1.106 cvs 1454: if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22 cvs 1455: return (FALSE);
1456:
1.106 cvs 1457: root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22 cvs 1458:
1459: if (root)
1460: {
1.106 cvs 1461: strcpy (temppath, root);
1.25 cvs 1462: TtaFreeMemory (root);
1.22 cvs 1463: /* Get the suffix */
1.124 vatton 1464: TtaExtractSuffix (temppath, suffix);
1.22 cvs 1465:
1.106 cvs 1466: if( suffix[0] == EOS)
1.22 cvs 1467: /* no suffix */
1468: return (FALSE);
1469:
1470: /* Normalize the suffix */
1471: ConvertToLowerCase (suffix);
1472:
1.106 cvs 1473: if (!strcmp (suffix, "gz"))
1.22 cvs 1474: /* skip the compressed suffix */
1475: {
1.124 vatton 1476: TtaExtractSuffix (temppath, suffix);
1.106 cvs 1477: if(suffix[0] == EOS)
1.22 cvs 1478: /* no suffix */
1479: return (FALSE);
1480: /* Normalize the suffix */
1481: ConvertToLowerCase (suffix);
1482: }
1483:
1.106 cvs 1484: if (strcmp (suffix, "gif") &&
1485: strcmp (suffix, "xbm") &&
1486: strcmp (suffix, "xpm") &&
1487: strcmp (suffix, "jpg") &&
1488: strcmp (suffix, "pdf") &&
1489: strcmp (suffix, "png") &&
1490: strcmp (suffix, "tgz") &&
1491: strcmp (suffix, "xpg") &&
1492: strcmp (suffix, "xpd") &&
1493: strcmp (suffix, "ps") &&
1494: strcmp (suffix, "au") &&
1495: strcmp (suffix, "html") &&
1496: strcmp (suffix, "htm") &&
1497: strcmp (suffix, "shtml") &&
1498: strcmp (suffix, "xht") &&
1499: strcmp (suffix, "xhtm") &&
1500: strcmp (suffix, "xhtml") &&
1501: strcmp (suffix, "txt") &&
1502: strcmp (suffix, "css") &&
1503: strcmp (suffix, "eps"))
1.22 cvs 1504: return (FALSE);
1505: else
1506: return (TRUE);
1507: }
1508: else
1509: return (FALSE);
1510: }
1511:
1512:
1513: /*----------------------------------------------------------------------
1.24 cvs 1514: ChopURL
1515: Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL).
1516: If inputURL is bigger than that size, outputURL receives
1517: MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...",
1518: and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
1519: If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
1520: copied into outputURL.
1521: N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
1522: chars.
1523: ----------------------------------------------------------------------*/
1.106 cvs 1524: void ChopURL (char *outputURL, const char *inputURL)
1.24 cvs 1525: {
1526: int len;
1.9 cvs 1527:
1.106 cvs 1528: len = strlen (inputURL);
1.24 cvs 1529: if (len <= MAX_PRINT_URL_LENGTH)
1.106 cvs 1530: strcpy (outputURL, inputURL);
1.24 cvs 1531: else
1532: /* make a truncated urlName on the status window */
1533: {
1.106 cvs 1534: strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
1535: outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
1536: strcat (outputURL, "...");
1537: strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24 cvs 1538: }
1.25 cvs 1539: }
1540:
1541:
1542: /*----------------------------------------------------------------------
1543: scan
1.47 cvs 1544: Scan a filename for its constituents
1.25 cvs 1545: -----------------------------------
1546:
1547: On entry,
1548: name points to a document name which may be incomplete.
1549: On exit,
1550: absolute or relative may be nonzero (but not both).
1551: host, fragment and access may be nonzero if they were specified.
1552: Any which are nonzero point to zero terminated strings.
1553: ----------------------------------------------------------------------*/
1.106 cvs 1554: static void scan (char *name, HTURI *parts)
1.25 cvs 1555: {
1.106 cvs 1556: char * p;
1557: char * after_access = name;
1.32 cvs 1558:
1.43 cvs 1559: memset (parts, '\0', sizeof (HTURI));
1.28 cvs 1560: /* Look for fragment identifier */
1.106 cvs 1561: if ((p = strchr(name, '#')) != NULL)
1.28 cvs 1562: {
1.106 cvs 1563: *p++ = '\0';
1.28 cvs 1564: parts->fragment = p;
1.25 cvs 1565: }
1566:
1.28 cvs 1567: for (p=name; *p; p++)
1568: {
1.106 cvs 1569: if (*p == URL_SEP || *p == DIR_SEP || *p == '#' || *p == '?')
1.28 cvs 1570: break;
1.106 cvs 1571: if (*p == ':')
1.28 cvs 1572: {
1573: *p = 0;
1574: parts->access = after_access; /* Scheme has been specified */
1575:
1576: /* The combination of gcc, the "-O" flag and the HP platform is
1577: unhealthy. The following three lines is a quick & dirty fix, but is
1578: not recommended. Rather, turn off "-O". */
1579:
1580: /* after_access = p;*/
1581: /* while (*after_access == 0)*/
1582: /* after_access++;*/
1583: after_access = p+1;
1.106 cvs 1584: if (!strcasecmp("URL", parts->access))
1.28 cvs 1585: /* Ignore IETF's URL: pre-prefix */
1586: parts->access = NULL;
1587: else
1.25 cvs 1588: break;
1589: }
1590: }
1591:
1592: p = after_access;
1.43 cvs 1593: if (*p == URL_SEP || *p == DIR_SEP)
1.28 cvs 1594: {
1.43 cvs 1595: if (p[1] == URL_SEP)
1.28 cvs 1596: {
1.25 cvs 1597: parts->host = p+2; /* host has been specified */
1.28 cvs 1598: *p = 0; /* Terminate access */
1599: /* look for end of host name if any */
1.106 cvs 1600: p = strchr (parts->host, URL_SEP);
1.28 cvs 1601: if (p)
1602: {
1.106 cvs 1603: *p = EOS; /* Terminate host */
1.25 cvs 1604: parts->absolute = p+1; /* Root has been found */
1.28 cvs 1605: }
1606: }
1607: else
1608: /* Root found but no host */
1609: parts->absolute = p+1;
1610: }
1611: else
1612: {
1.25 cvs 1613: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28 cvs 1614: }
1.25 cvs 1615: }
1616:
1617:
1618: /*----------------------------------------------------------------------
1.28 cvs 1619: AmayaParseUrl: parse a Name relative to another name
1620:
1621: This returns those parts of a name which are given (and requested)
1622: substituting bits from the related name where necessary.
1.25 cvs 1623:
1.28 cvs 1624: On entry,
1.25 cvs 1625: aName A filename given
1626: relatedName A name relative to which aName is to be parsed. Give
1627: it an empty string if aName is absolute.
1628: wanted A mask for the bits which are wanted.
1629:
1.28 cvs 1630: On exit,
1.25 cvs 1631: returns A pointer to a malloc'd string which MUST BE FREED
1632: ----------------------------------------------------------------------*/
1.106 cvs 1633: char *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
1634: {
1635: char *return_value;
1636: char result[MAX_LENGTH];
1637: char name[MAX_LENGTH];
1638: char rel[MAX_LENGTH];
1639: char *p, *access;
1.29 cvs 1640: HTURI given, related;
1641: int len;
1.106 cvs 1642: char used_sep;
1643: char *used_str;
1.32 cvs 1644:
1.106 cvs 1645: if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33 cvs 1646: {
1.106 cvs 1647: used_str = DIR_STR;
1648: used_sep = DIR_SEP;
1.33 cvs 1649: }
1.32 cvs 1650: else
1.33 cvs 1651: {
1.106 cvs 1652: used_str = URL_STR;
1653: used_sep = URL_SEP;
1.33 cvs 1654: }
1.32 cvs 1655:
1.29 cvs 1656: /* Make working copies of input strings to cut up: */
1657: return_value = NULL;
1658: result[0] = 0; /* Clear string */
1.106 cvs 1659: strcpy (name, aName);
1.29 cvs 1660: if (relatedName != NULL)
1.106 cvs 1661: strcpy (rel, relatedName);
1.29 cvs 1662: else
1.106 cvs 1663: relatedName[0] = EOS;
1.29 cvs 1664:
1665: scan (name, &given);
1666: scan (rel, &related);
1667: access = given.access ? given.access : related.access;
1668: if (wanted & AMAYA_PARSE_ACCESS)
1669: if (access)
1670: {
1.106 cvs 1671: strcat (result, access);
1.29 cvs 1672: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1673: strcat (result, ":");
1.29 cvs 1674: }
1675:
1676: if (given.access && related.access)
1677: /* If different, inherit nothing. */
1.106 cvs 1678: if (strcmp (given.access, related.access) != 0)
1.29 cvs 1679: {
1680: related.host = 0;
1681: related.absolute = 0;
1682: related.relative = 0;
1683: related.fragment = 0;
1684: }
1685:
1686: if (wanted & AMAYA_PARSE_HOST)
1687: if(given.host || related.host)
1688: {
1689: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1690: strcat (result, "//");
1691: strcat (result, given.host ? given.host : related.host);
1.29 cvs 1692: }
1693:
1694: if (given.host && related.host)
1695: /* If different hosts, inherit no path. */
1.106 cvs 1696: if (strcmp (given.host, related.host) != 0)
1.29 cvs 1697: {
1698: related.absolute = 0;
1699: related.relative = 0;
1700: related.fragment = 0;
1701: }
1702:
1703: if (wanted & AMAYA_PARSE_PATH)
1704: {
1705: if (given.absolute)
1706: {
1707: /* All is given */
1708: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1709: strcat (result, used_str);
1710: strcat (result, given.absolute);
1.25 cvs 1711: }
1.29 cvs 1712: else if (related.absolute)
1713: {
1714: /* Adopt path not name */
1.106 cvs 1715: strcat (result, used_str);
1716: strcat (result, related.absolute);
1.29 cvs 1717: if (given.relative)
1718: {
1719: /* Search part? */
1.106 cvs 1720: p = strchr (result, '?');
1.29 cvs 1721: if (!p)
1.106 cvs 1722: p=result+strlen(result)-1;
1.33 cvs 1723: for (; *p!=used_sep; p--); /* last / */
1.29 cvs 1724: /* Remove filename */
1725: p[1]=0;
1726: /* Add given one */
1.106 cvs 1727: strcat (result, given.relative);
1.25 cvs 1728: }
1729: }
1.29 cvs 1730: else if (given.relative)
1731: /* what we've got */
1.106 cvs 1732: strcat (result, given.relative);
1.29 cvs 1733: else if (related.relative)
1.106 cvs 1734: strcat (result, related.relative);
1.29 cvs 1735: else
1736: /* No inheritance */
1.106 cvs 1737: strcat (result, used_str);
1.25 cvs 1738: }
1.29 cvs 1739:
1740: if (wanted & AMAYA_PARSE_ANCHOR)
1741: if (given.fragment || related.fragment)
1742: {
1743: if (given.absolute && given.fragment)
1744: {
1745: /*Fixes for relURLs...*/
1746: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1747: strcat (result, "#");
1748: strcat (result, given.fragment);
1.29 cvs 1749: }
1750: else if (!(given.absolute) && !(given.fragment))
1.106 cvs 1751: strcat (result, "");
1.29 cvs 1752: else
1753: {
1.110 cvs 1754: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1755: strcat (result, "#");
1.110 cvs 1756: strcat (result, given.fragment ? given.fragment : related.fragment);
1.29 cvs 1757: }
1758: }
1.106 cvs 1759: len = strlen (result);
1760: if ((return_value = TtaGetMemory (len + 1)) != NULL)
1761: strcpy (return_value, result);
1.29 cvs 1762: return (return_value); /* exactly the right length */
1.25 cvs 1763: }
1764:
1765: /*----------------------------------------------------------------------
1766: HTCanon
1767: Canonicalizes the URL in the following manner starting from the host
1768: pointer:
1769:
1770: 1) The host name is converted to lowercase
1771: 2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
1772:
1773: Return: OK The position of the current path part of the URL
1774: which might be the old one or a new one.
1775:
1776: ----------------------------------------------------------------------*/
1.106 cvs 1777: static char *HTCanon (char **filename, char *host)
1778: {
1779: char *newname = NULL;
1780: char used_sep;
1781: char *path;
1782: char *strptr;
1783: char *port;
1784: char *access = host-3;
1785:
1786: if (*filename && strchr (*filename, URL_SEP))
1787: used_sep = URL_SEP;
1.33 cvs 1788: else
1.106 cvs 1789: used_sep = DIR_SEP;
1.32 cvs 1790:
1.110 cvs 1791: while (access > *filename && *(access - 1) != used_sep) /* Find access method */
1.25 cvs 1792: access--;
1.110 cvs 1793: if ((path = strchr (host, used_sep)) == NULL) /* Find path */
1.106 cvs 1794: path = host + strlen (host);
1795: if ((strptr = strchr (host, '@')) != NULL && strptr < path) /* UserId */
1.82 cvs 1796: host = strptr;
1.110 cvs 1797: if ((port = strchr (host, ':')) != NULL && port > path) /* Port number */
1.82 cvs 1798: port = NULL;
1.25 cvs 1799:
1800: strptr = host; /* Convert to lower-case */
1.82 cvs 1801: while (strptr < path)
1.33 cvs 1802: {
1.123 vatton 1803: *strptr = tolower (*strptr);
1.82 cvs 1804: strptr++;
1.33 cvs 1805: }
1.25 cvs 1806:
1807: /* Does the URL contain a full domain name? This also works for a
1808: numerical host name. The domain name is already made lower-case
1809: and without a trailing dot. */
1810: {
1.106 cvs 1811: char *dot = port ? port : path;
1812: if (dot > *filename && *--dot == '.')
1.33 cvs 1813: {
1.106 cvs 1814: char *orig = dot;
1815: char *dest = dot + 1;
1.82 cvs 1816: while ((*orig++ = *dest++));
1817: if (port) port--;
1.33 cvs 1818: path--;
1.25 cvs 1819: }
1820: }
1821: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33 cvs 1822: if (port)
1823: {
1.82 cvs 1824: if (!*(port+1) || *(port+1) == used_sep)
1.33 cvs 1825: {
1826: if (!newname)
1827: {
1.106 cvs 1828: char *orig = port;
1829: char *dest = port + 1;
1.82 cvs 1830: while ((*orig++ = *dest++));
1.33 cvs 1831: }
1832: }
1.106 cvs 1833: else if ((!strncmp (access, "http", 4) &&
1834: (*(port + 1) == '8' &&
1835: *(port+2) == '0' &&
1.82 cvs 1836: (*(port+3) == used_sep || !*(port + 3)))) ||
1.106 cvs 1837: (!strncmp (access, "gopher", 6) &&
1838: (*(port+1) == '7' &&
1839: *(port+2) == '0' &&
1.82 cvs 1840: (*(port+3) == used_sep || !*(port+3)))) ||
1.106 cvs 1841: (!strncmp (access, "ftp", 3) &&
1842: (*(port+1) == '2' &&
1843: *(port + 2) == '1' &&
1.82 cvs 1844: (*(port+3) == used_sep || !*(port+3))))) {
1.33 cvs 1845: if (!newname)
1846: {
1.106 cvs 1847: char *orig = port;
1848: char *dest = port + 3;
1.33 cvs 1849: while((*orig++ = *dest++));
1850: /* Update path position, Henry Minsky */
1851: path -= 3;
1.25 cvs 1852: }
1.33 cvs 1853: }
1854: else if (newname)
1.106 cvs 1855: strncat (newname, port, (int) (path - port));
1.33 cvs 1856: }
1.25 cvs 1857:
1.33 cvs 1858: if (newname)
1859: {
1.106 cvs 1860: char *newpath = newname + strlen (newname);
1861: strcat (newname, path);
1.25 cvs 1862: path = newpath;
1.28 cvs 1863: /* Free old copy */
1864: TtaFreeMemory(*filename);
1.25 cvs 1865: *filename = newname;
1.33 cvs 1866: }
1.25 cvs 1867: return path;
1868: }
1869:
1870:
1871: /*----------------------------------------------------------------------
1.29 cvs 1872: SimplifyUrl: simplify a URI
1.32 cvs 1873: A URI is allowed to contain the sequence xxx/../ which may be
1874: replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28 cvs 1875: Simplification helps us recognize duplicate URIs.
1.25 cvs 1876:
1.28 cvs 1877: Thus, /etc/junk/../fred becomes /etc/fred
1878: /etc/junk/./fred becomes /etc/junk/fred
1.25 cvs 1879:
1.28 cvs 1880: but we should NOT change
1881: http://fred.xxx.edu/../..
1.25 cvs 1882:
1883: or ../../albert.html
1884:
1.28 cvs 1885: In order to avoid empty URLs the following URLs become:
1.25 cvs 1886:
1887: /fred/.. becomes /fred/..
1888: /fred/././.. becomes /fred/..
1889: /fred/.././junk/.././ becomes /fred/..
1890:
1.28 cvs 1891: If more than one set of `://' is found (several proxies in cascade) then
1892: only the part after the last `://' is simplified.
1.25 cvs 1893:
1.28 cvs 1894: Returns: A string which might be the old one or a new one.
1.25 cvs 1895: ----------------------------------------------------------------------*/
1.106 cvs 1896: void SimplifyUrl (char **url)
1897: {
1898: char *path;
1899: char *access;
1900: char *newptr;
1901: char *p;
1902: char *orig, *dest, *end;
1.28 cvs 1903:
1.106 cvs 1904: char used_sep;
1.77 cvs 1905: ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
1906: something/../ simplification in relative URLs when they start with a ../ */
1.32 cvs 1907:
1.28 cvs 1908: if (!url || !*url)
1909: return;
1910:
1.106 cvs 1911: if (strchr (*url, URL_SEP))
1912: used_sep = URL_SEP;
1.32 cvs 1913: else
1.106 cvs 1914: used_sep = DIR_SEP;
1.32 cvs 1915:
1.77 cvs 1916: /* should we simplify double dot? */
1917: path = *url;
1.106 cvs 1918: if (*path == '.' && *(path + 1) == '.')
1.77 cvs 1919: ddot_simplify = FALSE;
1920: else
1921: ddot_simplify = TRUE;
1922:
1.28 cvs 1923: /* Find any scheme name */
1.106 cvs 1924: if ((path = strstr (*url, "://")) != NULL)
1.33 cvs 1925: {
1926: /* Find host name */
1.28 cvs 1927: access = *url;
1.123 vatton 1928: while (access < path && (*access = tolower (*access)))
1.82 cvs 1929: access++;
1.28 cvs 1930: path += 3;
1.106 cvs 1931: while ((newptr = strstr (path, "://")) != NULL)
1.82 cvs 1932: /* For proxies */
1.106 cvs 1933: path = newptr + 3;
1.82 cvs 1934: /* We have a host name */
1.84 cvs 1935: path = HTCanon (url, path);
1.25 cvs 1936: }
1.106 cvs 1937: else if ((path = strstr (*url, ":/")) != NULL)
1.28 cvs 1938: path += 2;
1939: else
1940: path = *url;
1.84 cvs 1941: if (*path == used_sep && *(path+1) == used_sep)
1.28 cvs 1942: /* Some URLs start //<foo> */
1943: path += 1;
1.94 cvs 1944: else if (IsFilePath (path))
1945: {
1946: /* doesn't need to do anything more */
1947: return;
1948: }
1.106 cvs 1949: else if (!strncmp (path, "news:", 5))
1.28 cvs 1950: {
1.106 cvs 1951: newptr = strchr (path+5, '@');
1.28 cvs 1952: if (!newptr)
1953: newptr = path + 5;
1954: while (*newptr)
1955: {
1956: /* Make group or host lower case */
1.123 vatton 1957: *newptr = tolower (*newptr);
1.28 cvs 1958: newptr++;
1.25 cvs 1959: }
1.28 cvs 1960: /* Doesn't need to do any more */
1961: return;
1.25 cvs 1962: }
1.130 cheyroul 1963:
1.126 cheyroul 1964:
1.28 cvs 1965: if ((p = path))
1966: {
1.106 cvs 1967: if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
1968: (end = strchr (path, '#'))))
1969: end = path + strlen (path);
1.28 cvs 1970:
1971: /* Parse string second time to simplify */
1972: p = path;
1973: while (p < end)
1974: {
1.110 cvs 1975: /* if we're pointing to a char, it's safe to reactivate the
1976: ../ convertion */
1.106 cvs 1977: if (!ddot_simplify && *p != '.' && *p != used_sep)
1.77 cvs 1978: ddot_simplify = TRUE;
1979:
1.33 cvs 1980: if (*p==used_sep)
1.28 cvs 1981: {
1.106 cvs 1982: if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28 cvs 1983: {
1984: orig = p + 1;
1.84 cvs 1985: dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52 cvs 1986: while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28 cvs 1987: end = orig - 1;
1988: }
1.106 cvs 1989: else if (ddot_simplify && *(p+1) == '.' && *(p+2) == '.'
1.77 cvs 1990: && (*(p+3) == used_sep || !*(p+3)))
1.28 cvs 1991: {
1992: newptr = p;
1.52 cvs 1993: while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
1994: if (*newptr == used_sep)
1995: orig = newptr + 1;
1.28 cvs 1996: else
1.52 cvs 1997: orig = newptr;
1998:
1999: dest = (*(p+3) != used_sep) ? p+3 : p+4;
2000: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
2001: end = orig-1;
2002: /* Start again with prev slash */
2003: p = newptr;
1.28 cvs 2004: }
1.33 cvs 2005: else if (*(p+1) == used_sep)
1.28 cvs 2006: {
1.33 cvs 2007: while (*(p+1) == used_sep)
1.28 cvs 2008: {
2009: orig = p;
2010: dest = p + 1;
2011: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2012: end = orig-1;
2013: }
2014: }
2015: else
1.25 cvs 2016: p++;
1.28 cvs 2017: }
2018: else
2019: p++;
1.25 cvs 2020: }
2021: }
1.51 cvs 2022: /*
2023: ** Check for host/../.. kind of things
2024: */
1.106 cvs 2025: if (*path == used_sep && *(path+1) == '.' && *(path+2) == '.'
1.77 cvs 2026: && (!*(path+3) || *(path+3) == used_sep))
1.106 cvs 2027: *(path+1) = EOS;
1.28 cvs 2028: return;
2029: }
2030:
2031:
2032: /*----------------------------------------------------------------------
1.96 cvs 2033: NormalizeFile normalizes local names.
1.28 cvs 2034: Return TRUE if target and src differ.
2035: ----------------------------------------------------------------------*/
1.106 cvs 2036: ThotBool NormalizeFile (char *src, char *target, ConvertionType convertion)
1.28 cvs 2037: {
1.110 cvs 2038: #ifndef _WINDOWS
1.106 cvs 2039: char *s;
1.93 cvs 2040: int i;
1.110 cvs 2041: #endif /* !_WINDOWS */
1.82 cvs 2042: ThotBool change;
1.90 cvs 2043: int start_index; /* the first char that we'll copy */
1.28 cvs 2044:
1.54 cvs 2045: change = FALSE;
1.90 cvs 2046: start_index = 0;
2047:
1.106 cvs 2048: if (!src || src[0] == EOS)
1.96 cvs 2049: {
1.106 cvs 2050: target[0] = EOS;
1.96 cvs 2051: return FALSE;
2052: }
1.90 cvs 2053:
2054: /* @@ do I need file: or file:/ here? */
1.106 cvs 2055: if (strncmp (src, "file:", 5) == 0)
1.28 cvs 2056: {
1.90 cvs 2057: /* remove the prefix file: */
2058: start_index += 5;
2059:
2060: /* remove the localhost prefix */
1.106 cvs 2061: if (strncmp (&src[start_index], "//localhost/", 12) == 0)
1.94 cvs 2062: start_index += 11;
2063:
2064: /* remove the first two slashes in / / /path */
2065: while (src[start_index] &&
1.106 cvs 2066: src[start_index] == '/'
2067: && src[start_index + 1] == '/')
1.94 cvs 2068: start_index++;
2069:
2070: #ifdef _WINDOWS
2071: /* remove any extra slash before the drive name */
1.106 cvs 2072: if (src[start_index] == '/'
2073: &&src[start_index+2] == ':')
1.94 cvs 2074: start_index++;
2075: #endif /* _WINDOWS */
1.90 cvs 2076:
1.106 cvs 2077: if (src[start_index] == EOS)
1.90 cvs 2078: /* if there's nothing afterwards, add a DIR_STR */
1.106 cvs 2079: strcpy (target, DIR_STR);
1.90 cvs 2080: else
1.97 cvs 2081: /* as we're inside a file: URL, we'll apply all the convertions
2082: we know */
2083: CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96 cvs 2084:
2085: change = TRUE;
2086: }
1.97 cvs 2087: else if (convertion != AM_CONV_NONE)
1.96 cvs 2088: {
2089: /* we are following a "local" relative link, we do all the
2090: convertions except for the HOME_DIR ~ one */
1.97 cvs 2091: CleanCopyFileURL (target, src, convertion);
1.28 cvs 2092: }
1.90 cvs 2093: #ifndef _WINDOWS
1.106 cvs 2094: else if (src[0] == '~')
1.53 cvs 2095: {
1.96 cvs 2096: /* it must be a URL typed in a text input field */
2097: /* do the HOME_DIR ~ substitution */
1.82 cvs 2098: s = TtaGetEnvString ("HOME");
1.106 cvs 2099: strcpy (target, s);
1.90 cvs 2100: #if 0
1.96 cvs 2101: /* JK: invalidated this part of the code as it's simpler
2102: to add the DIR_SEP whenever we have something to add
2103: to the path rather than adding it systematically */
1.106 cvs 2104: if (src[1] != DIR_SEP)
2105: strcat (target, DIR_STR);
1.90 cvs 2106: #endif
1.106 cvs 2107: i = strlen (target);
2108: strcpy (&target[i], &src[1]);
1.54 cvs 2109: change = TRUE;
1.53 cvs 2110: }
1.90 cvs 2111: #endif /* _WINDOWS */
1.28 cvs 2112: else
1.96 cvs 2113: /* leave it as it is */
1.106 cvs 2114: strcpy (target, src);
1.96 cvs 2115:
1.28 cvs 2116: /* remove /../ and /./ */
1.29 cvs 2117: SimplifyUrl (&target);
1.54 cvs 2118: if (!change)
1.106 cvs 2119: change = strcmp (src, target);
1.28 cvs 2120: return (change);
1.25 cvs 2121: }
2122:
1.28 cvs 2123:
1.25 cvs 2124: /*----------------------------------------------------------------------
1.31 cvs 2125: MakeRelativeURL: make relative name
1.25 cvs 2126:
1.28 cvs 2127: This function creates and returns a string which gives an expression of
2128: one address as related to another. Where there is no relation, an absolute
2129: address is retured.
1.25 cvs 2130:
1.28 cvs 2131: On entry,
1.25 cvs 2132: Both names must be absolute, fully qualified names of nodes
2133: (no fragment bits)
2134:
1.28 cvs 2135: On exit,
1.25 cvs 2136: The return result points to a newly allocated name which, if
2137: parsed by AmayaParseUrl relative to relatedName, will yield aName.
2138: The caller is responsible for freeing the resulting name later.
2139: ----------------------------------------------------------------------*/
1.106 cvs 2140: char *MakeRelativeURL (char *aName, char *relatedName)
2141: {
2142: char *return_value;
2143: char result[MAX_LENGTH];
2144: char *p;
2145: char *q;
2146: char *after_access;
2147: char *last_slash = NULL;
2148: int slashes, levels, len;
1.110 cvs 2149: #ifdef _WINDOWS
1.44 cvs 2150: int ndx;
1.110 cvs 2151: #endif /* _WINDOWS */
1.44 cvs 2152:
1.29 cvs 2153: if (aName == NULL || relatedName == NULL)
2154: return (NULL);
2155:
2156: slashes = 0;
2157: after_access = NULL;
2158: p = aName;
2159: q = relatedName;
1.147 vatton 2160: len = 0;
2161: for (; *p && !strncasecmp (p, q, 1); p++, q++, len++)
1.27 cvs 2162: {
2163: /* Find extent of match */
1.106 cvs 2164: if (*p == ':')
1.146 cvs 2165: {
2166: after_access = p + 1;
1.147 vatton 2167: if (len == 1)
2168: /* it's a local Windows path like c:... */
2169: slashes++;
1.146 cvs 2170: }
1.28 cvs 2171: if (*p == DIR_SEP)
1.27 cvs 2172: {
1.29 cvs 2173: /* memorize the last slash position and count them */
1.27 cvs 2174: last_slash = p;
2175: slashes++;
1.25 cvs 2176: }
2177: }
2178:
1.31 cvs 2179: /* q, p point to the first non-matching character or zero */
1.106 cvs 2180: if (*q == EOS)
1.31 cvs 2181: {
2182: /* New name is a subset of the related name */
2183: /* exactly the right length */
1.106 cvs 2184: len = strlen (p);
2185: if ((return_value = TtaGetMemory (len + 1)) != NULL)
2186: strcpy (return_value, p);
1.31 cvs 2187: }
2188: else if ((slashes < 2 && after_access == NULL)
2189: || (slashes < 3 && after_access != NULL))
2190: {
2191: /* Two names whitout common path */
2192: /* exactly the right length */
1.106 cvs 2193: len = strlen (aName);
2194: if ((return_value = TtaGetMemory (len + 1)) != NULL)
2195: strcpy (return_value, aName);
1.31 cvs 2196: }
2197: else
2198: {
2199: /* Some path in common */
1.106 cvs 2200: if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
1.31 cvs 2201: /* just the same server */
1.106 cvs 2202: strcpy (result, last_slash);
1.31 cvs 2203: else
2204: {
2205: levels= 0;
1.106 cvs 2206: for (; *q && *q != '#' && *q != ';' && *q != '?'; q++)
1.31 cvs 2207: if (*q == DIR_SEP)
2208: levels++;
2209:
1.106 cvs 2210: result[0] = EOS;
1.31 cvs 2211: for (;levels; levels--)
1.106 cvs 2212: strcat (result, "../");
2213: strcat (result, last_slash+1);
1.31 cvs 2214: }
1.52 cvs 2215:
2216: if (!*result)
1.106 cvs 2217: strcat (result, "./");
1.52 cvs 2218:
1.31 cvs 2219: /* exactly the right length */
1.106 cvs 2220: len = strlen (result);
2221: if ((return_value = TtaGetMemory (len + 1)) != NULL)
2222: strcpy (return_value, result);
1.52 cvs 2223:
1.25 cvs 2224: }
1.110 cvs 2225: #ifdef _WINDOWS
1.106 cvs 2226: len = strlen (return_value);
1.44 cvs 2227: for (ndx = 0; ndx < len; ndx ++)
1.106 cvs 2228: if (return_value[ndx] == '\\')
2229: return_value[ndx] = '/' ;
1.110 cvs 2230: #endif /* _WINDOWS */
1.29 cvs 2231: return (return_value);
1.24 cvs 2232: }
1.35 cvs 2233:
1.104 kahan 2234: /*----------------------------------------------------------------------
2235: AM_GetFileSize
2236: Returns TRUE and the filesize in the 2nd parameter.
2237: Otherwise, in case of a system error, returns FALSE, with a
2238: filesize of 0L.
2239: ---------------------------------------------------------------------*/
1.106 cvs 2240: ThotBool AM_GetFileSize (char *filename, unsigned long *file_size)
1.104 kahan 2241: {
1.106 cvs 2242: ThotFileHandle handle = ThotFile_BADHANDLE;
2243: ThotFileInfo info;
1.35 cvs 2244:
1.104 kahan 2245: *file_size = 0L;
2246: if (!TtaFileExist (filename))
2247: return FALSE;
2248:
2249: handle = TtaFileOpen (filename, ThotFile_READWRITE);
2250: if (handle == ThotFile_BADHANDLE)
2251: /* ThotFile_BADHANDLE */
2252: return FALSE;
2253: if (TtaFileStat (handle, &info) == 0)
2254: /* bad stat */
2255: info.size = 0L;
2256: TtaFileClose (handle);
2257: *file_size = (unsigned long) info.size;
2258: return TRUE;
2259: }
1.139 kahan 2260:
2261: /*----------------------------------------------------------------------
2262: AM_UseXHTMLMimeType
2263: Returns TRUE if the user has configured Amaya to use this MIME type,
2264: FALSE otherwise.
2265: ---------------------------------------------------------------------*/
2266: ThotBool AM_UseXHTMLMimeType (void)
2267: {
2268: ThotBool xhtml_mimetype;
2269:
2270: /* does the user wants to use the new MIME type? */
2271: TtaGetEnvBoolean ("ENABLE_XHTML_MIMETYPE", &xhtml_mimetype);
2272:
2273: return (xhtml_mimetype);
2274: }
Webmaster