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