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