Annotation of Amaya/amaya/AHTURLTools.c, revision 1.99
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)
1.99 ! kahan 617: || !ustrncmp (path, TEXT("ftp:"), 4)
1.67 cvs 618: || !ustrncmp (path, TEXT("internal:"), 9))
1.58 cvs 619: return TRUE;
620: return FALSE;
1.3 cvs 621: }
622:
1.4 cvs 623: /*----------------------------------------------------------------------
1.9 cvs 624: IsWithParameters
625: returns TRUE if url has a concatenated query string.
1.4 cvs 626: ----------------------------------------------------------------------*/
1.3 cvs 627: #ifdef __STDC__
1.66 cvs 628: ThotBool IsWithParameters (const char *url)
1.3 cvs 629: #else /* __STDC__ */
1.66 cvs 630: ThotBool IsWithParameters (url)
1.34 cvs 631: const char *url;
1.3 cvs 632: #endif /* __STDC__ */
633: {
1.5 cvs 634: int i;
1.3 cvs 635:
1.9 cvs 636: if ((!url) || (url[0] == EOS))
1.5 cvs 637: return FALSE;
1.3 cvs 638:
1.9 cvs 639: i = strlen (url) - 1;
640: while (i > 0 && url[i--] != '?')
1.5 cvs 641: if (i < 0)
642: return FALSE;
1.3 cvs 643:
1.5 cvs 644: /* There is a parameter */
645: return TRUE;
1.3 cvs 646: }
647:
1.4 cvs 648: /*----------------------------------------------------------------------
1.9 cvs 649: IsW3Path
650: returns TRUE if path is in fact a URL.
1.4 cvs 651: ----------------------------------------------------------------------*/
1.3 cvs 652: #ifdef __STDC__
1.84 cvs 653: ThotBool IsW3Path (const CHAR_T* path)
1.3 cvs 654: #else /* __STDC__ */
1.67 cvs 655: ThotBool IsW3Path (path)
1.84 cvs 656: const CHAR_T* path;
1.3 cvs 657: #endif /* __STDC__ */
658: {
1.84 cvs 659: if (ustrncmp (path, TEXT("http:"), 5) &&
660: ustrncmp (path, TEXT("ftp:"), 4) &&
661: ustrncmp (path, TEXT("telnet:"), 7) &&
662: ustrncmp (path, TEXT("wais:"), 5) &&
663: ustrncmp (path, TEXT("news:"), 5) &&
664: ustrncmp (path, TEXT("gopher:"), 7) &&
665: ustrncmp (path, TEXT("mailto:"), 7) &&
666: ustrncmp (path, TEXT("archie:"), 7))
1.72 cvs 667: return FALSE;
668: return TRUE;
1.3 cvs 669: }
670:
1.4 cvs 671: /*----------------------------------------------------------------------
1.90 cvs 672: IsFilePath
673: returns TRUE if path is in fact a URL.
674: ----------------------------------------------------------------------*/
675: #ifdef __STDC__
1.98 kahan 676: ThotBool IsFilePath (const CHAR_T* path)
1.90 cvs 677: #else /* __STDC__ */
1.98 kahan 678: ThotBool IsFilePath (path)
1.90 cvs 679: const CHAR_T* path;
680: #endif /* __STDC__ */
681: {
682: if (ustrncmp (path, TEXT("file:"), 5))
683: return FALSE;
684: return TRUE;
685: }
686:
687: /*----------------------------------------------------------------------
1.9 cvs 688: IsValidProtocol
689: returns true if the url protocol is supported by Amaya.
1.4 cvs 690: ----------------------------------------------------------------------*/
1.3 cvs 691: #ifdef __STDC__
1.67 cvs 692: ThotBool IsValidProtocol (const STRING url)
1.3 cvs 693: #else /* __STDC__ */
1.67 cvs 694: ThotBool IsValidProtocol (url)
695: const STRING url;
1.3 cvs 696: #endif /* __STDC__ */
697: {
1.67 cvs 698: if (!ustrncmp (url, TEXT("http:"), 5)
1.69 cvs 699: || !ustrncmp (url, TEXT("internal:"), 9)
1.70 cvs 700: || !ustrncmp (url, TEXT("ftp:"), 4))
1.22 cvs 701: /* experimental */
1.58 cvs 702: /*** || !strncmp (url, "ftp:", 4) ***/
1.24 cvs 703: /*** || !strncmp (path, "news:", 5)***/
1.8 cvs 704: return (TRUE);
1.5 cvs 705: else
1.8 cvs 706: return (FALSE);
1.3 cvs 707: }
708:
1.31 cvs 709:
710: /*----------------------------------------------------------------------
711: GetBaseURL
712: normalizes orgName according to a base associated with doc, and
713: following the standard URL format rules.
714: The function returns the base used to solve relative URL and SRC:
715: - the base of the document,
716: - or the document path (without document name).
717: ----------------------------------------------------------------------*/
718: #ifdef __STDC__
1.84 cvs 719: CHAR_T* GetBaseURL (Document doc)
1.31 cvs 720: #else /* __STDC__ */
1.84 cvs 721: CHAR_T* GetBaseURL (doc)
1.31 cvs 722: Document doc;
723: #endif /* __STDC__ */
724: {
725: Element el;
726: ElementType elType;
727: AttributeType attrType;
728: Attribute attr;
1.84 cvs 729: CHAR_T *ptr, *basename;
1.31 cvs 730: int length;
731:
1.57 cvs 732: /* @@@ irene */
733: if (!DocumentURLs[doc])
734: return NULL;
1.67 cvs 735: basename = TtaAllocString (MAX_LENGTH);
736: ustrncpy (basename, DocumentURLs[doc], MAX_LENGTH-1);
1.84 cvs 737: basename[MAX_LENGTH-1] = WC_EOS;
1.31 cvs 738: length = MAX_LENGTH -1;
739: /* get the root element */
740: el = TtaGetMainRoot (doc);
741: /* search the BASE element */
742: elType.ElSSchema = TtaGetDocumentSSchema (doc);
1.65 cvs 743: elType.ElTypeNum = HTML_EL_HEAD;
744: el = TtaSearchTypedElement (elType, SearchForward, el);
745: if (el)
746: {
747: elType.ElTypeNum = HTML_EL_BASE;
748: el = TtaSearchTypedElement (elType, SearchInTree, el);
749: }
1.31 cvs 750: if (el)
751: {
752: /* The document has a BASE element -> Get the HREF attribute */
753: attrType.AttrSSchema = elType.ElSSchema;
754: attrType.AttrTypeNum = HTML_ATTR_HREF_;
755: attr = TtaGetAttribute (el, attrType);
756: if (attr)
757: {
758: /* Use the base path of the document */
759: TtaGiveTextAttributeValue (attr, basename, &length);
760: /* base and orgName have to be separated by a DIR_SEP */
761: length--;
1.84 cvs 762: if (basename[0] != WC_EOS && basename[length] != WC_URL_SEP && basename[length] != WC_DIR_SEP)
1.31 cvs 763: /* verify if the base has the form "protocol://server:port" */
764: {
1.84 cvs 765: ptr = AmayaParseUrl (basename, TEXT(""), AMAYA_PARSE_ACCESS |
1.33 cvs 766: AMAYA_PARSE_HOST |
767: AMAYA_PARSE_PUNCTUATION);
1.67 cvs 768: if (ptr && !ustrcmp (ptr, basename))
1.31 cvs 769: {
1.43 cvs 770: /* it has this form, we complete it by adding a URL_STR */
1.84 cvs 771: if (ustrchr (basename, WC_DIR_SEP))
772: ustrcat (basename, WC_DIR_STR);
1.43 cvs 773: else
1.84 cvs 774: ustrcat (basename, WC_URL_STR);
1.31 cvs 775: length++;
776: }
777: if (ptr)
778: TtaFreeMemory (ptr);
779: }
780: }
1.33 cvs 781: }
782:
1.31 cvs 783: /* Remove anything after the last DIR_SEP char. If no such char is found,
784: * then search for the first ":" char, hoping that what's before that is a
785: * protocol. If found, end the string there. If neither char is found,
786: * then discard the whole base element.
787: */
1.67 cvs 788: length = ustrlen (basename) - 1;
1.31 cvs 789: /* search for the last DIR_SEP char */
1.84 cvs 790: while (length >= 0 && basename[length] != WC_URL_SEP && basename[length] != WC_DIR_SEP)
1.31 cvs 791: length--;
792: if (length >= 0)
793: /* found the last DIR_SEP char, end the string there */
1.84 cvs 794: basename[length + 1] = WC_EOS;
1.31 cvs 795: else
796: /* search for the first PATH_STR char */
797: {
1.67 cvs 798: for (length = 0; basename[length] != TEXT(':') &&
1.84 cvs 799: basename[length] != WC_EOS; length ++);
1.67 cvs 800: if (basename[length] == TEXT(':'))
1.31 cvs 801: /* found, so end the string there */
1.84 cvs 802: basename[length + 1] = WC_EOS;
1.31 cvs 803: else
804: /* not found, discard the base */
1.84 cvs 805: basename[0] = WC_EOS;
1.31 cvs 806: }
807: return (basename);
808: }
809:
810:
1.4 cvs 811: /*----------------------------------------------------------------------
1.40 cvs 812: GetLocalPath
813: Allocate and return the local document path associated to the url
814: ----------------------------------------------------------------------*/
815: #ifdef __STDC__
1.84 cvs 816: CHAR_T* GetLocalPath (Document doc, CHAR_T* url)
1.40 cvs 817: #else /* __STDC__ */
1.84 cvs 818: CHAR_T* GetLocalPath (doc, url)
1.40 cvs 819: Document doc;
1.84 cvs 820: CHAR_T* url;
1.40 cvs 821: #endif /* __STDC__ */
822: {
1.84 cvs 823: CHAR_T* ptr;
824: CHAR_T* n;
825: CHAR_T* documentname;
826: CHAR_T url_sep;
1.83 cvs 827: int len;
1.67 cvs 828: ThotBool noFile;
1.40 cvs 829:
830: if (url != NULL)
831: {
832: /* check whether the file name exists */
1.84 cvs 833: len = ustrlen (url) - 1;
1.71 cvs 834: if (IsW3Path (url))
1.84 cvs 835: url_sep = TEXT('/');
1.41 cvs 836: else
1.84 cvs 837: url_sep = WC_DIR_SEP;
1.41 cvs 838: noFile = (url[len] == url_sep);
1.40 cvs 839: if (noFile)
1.84 cvs 840: url[len] = WC_EOS;
841: ptr = TtaAllocString (MAX_LENGTH);
842: documentname = TtaAllocString (MAX_LENGTH);
1.78 cvs 843: TtaExtractName (url, ptr, documentname);
1.84 cvs 844: usprintf (ptr, TEXT("%s%s%d%s"), TempFileDirectory, WC_DIR_STR, doc, WC_DIR_STR);
1.40 cvs 845: if (!TtaCheckDirectory (ptr))
846: /* directory did not exist */
1.72 cvs 847: TtaMakeDirectory (ptr);
1.47 cvs 848:
849: /* don't include the query string within document name */
1.84 cvs 850: n = ustrrchr (documentname, TEXT('?'));
1.47 cvs 851: if (n != NULL)
1.84 cvs 852: *n = WC_EOS;
1.46 cvs 853: /* don't include ':' within document name */
1.84 cvs 854: n = ustrchr (documentname, TEXT(':'));
1.46 cvs 855: if (n != NULL)
1.84 cvs 856: *n = WC_EOS;
1.69 cvs 857: /* if after all this operations document name
858: is empty, let's use noname.html instead */
1.84 cvs 859: if (documentname[0] == WC_EOS)
860: ustrcat (ptr, TEXT("noname.html"));
1.69 cvs 861: else
1.84 cvs 862: ustrcat (ptr, documentname);
1.40 cvs 863: TtaFreeMemory (documentname);
864: /* restore the url */
865: if (noFile)
1.41 cvs 866: url[len] = url_sep;
1.40 cvs 867: return (ptr);
868: }
869: else
870: return (NULL);
871: }
872:
1.73 cvs 873: /*----------------------------------------------------------------------
1.79 cvs 874: ExtractTarget extract the target name from document nane.
875: ----------------------------------------------------------------------*/
876: #ifdef __STDC__
1.84 cvs 877: void ExtractTarget (CHAR_T* aName, CHAR_T* target)
1.79 cvs 878: #else
879: void ExtractTarget (aName, target)
1.84 cvs 880: CHAR_T* aName;
881: CHAR_T* target;
1.79 cvs 882: #endif
883: {
1.82 cvs 884: int lg, i;
1.84 cvs 885: CHAR_T* ptr;
886: CHAR_T* oldptr;
1.79 cvs 887:
888: if (!target || !aName)
889: /* bad target */
890: return;
891:
1.84 cvs 892: target[0] = WC_EOS;
893: lg = ustrlen (aName);
1.79 cvs 894: if (lg)
895: {
896: /* the name is not empty */
897: oldptr = ptr = &aName[0];
898: do
899: {
1.84 cvs 900: ptr = ustrrchr (oldptr, TEXT('#'));
1.79 cvs 901: if (ptr)
902: oldptr = &ptr[1];
903: }
904: while (ptr);
905:
906: i = (int) (oldptr) - (int) (aName); /* name length */
907: if (i > 1)
908: {
1.84 cvs 909: aName[i - 1] = WC_EOS;
1.79 cvs 910: if (i != lg)
1.84 cvs 911: ustrcpy (target, oldptr);
1.79 cvs 912: }
913: }
914: }
915:
916: /*----------------------------------------------------------------------
1.90 cvs 917: RemoveNewLines (text)
918: Removes any '\n' chars that are found in text.
919: Returns TRUE if it did the operation, FALSE otherwise.
1.73 cvs 920: ----------------------------------------------------------------------*/
921: #ifdef __STDC__
1.90 cvs 922: ThotBool RemoveNewLines (CHAR_T *text)
1.73 cvs 923: #else
1.90 cvs 924: ThotBool RemoveNewLines (text)
925: CHAR_T *text;
926:
1.73 cvs 927: #endif /* __STDC__ */
928: {
1.90 cvs 929: ThotBool change = FALSE;
930: CHAR_T *src;
931: CHAR_T *dest;
932:
933: src = text;
934: dest = text;
935:
936: while (*src)
937: {
938: switch (*src)
939: {
940: case TEXT('\n'):
941: /* don't copy the newline */
942: change = 1;
943: break;
944: default:
945: *dest = *src;
946: dest++;
947: break;
948: }
949: src++;
950: }
951: /* copy the last EOS char */
952: *dest = *src;
953:
954: return (change);
955: }
956:
957: /*----------------------------------------------------------------------
958: CleanCopyFileURL
959: Copies a file url from a src string to destination string.
1.97 cvs 960: convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
961: we want to do).
1.90 cvs 962: ----------------------------------------------------------------------*/
963: #ifdef __STDC__
1.97 cvs 964: static void CleanCopyFileURL (CHAR_T *dest, CHAR_T *src, ConvertionType convertion)
1.90 cvs 965: #else
1.97 cvs 966: static void CleanCopyFileURL (dest, src, convertion)
1.90 cvs 967: CHAR_T* dest;
968: CHAR_T* src;
1.97 cvs 969: ConvertionType convertion;
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 */
1.97 cvs 980: if (convertion & AM_CONV_URL_SEP)
1.96 cvs 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('%'):
1.97 cvs 990: if (convertion & AM_CONV_PERCENT)
1.96 cvs 991: {
1.97 cvs 992: /* (code adapted from libwww's HTUnEscape function */
1.96 cvs 993: src++;
1.97 cvs 994: if (*src != WC_EOS)
995: {
996: *dest = UnEscapeChar (*src) * 16;
997: src++;
998: }
999: if (*src != WC_EOS)
1000: {
1001: *dest = *dest + UnEscapeChar (*src);
1002: src++;
1003: }
1004: dest++;
1.96 cvs 1005: }
1.97 cvs 1006: else
1.96 cvs 1007: {
1.97 cvs 1008: *dest = *src;
1009: dest++;
1.96 cvs 1010: src++;
1011: }
1012: break;
1013:
1.90 cvs 1014: default:
1015: *dest = *src;
1.89 cvs 1016: dest++;
1.96 cvs 1017: src++;
1.90 cvs 1018: break;
1.89 cvs 1019: }
1020: }
1.90 cvs 1021: /* copy the EOS char */
1022: *dest = *src;
1.73 cvs 1023: }
1.40 cvs 1024:
1025: /*----------------------------------------------------------------------
1.9 cvs 1026: NormalizeURL
1027: normalizes orgName according to a base associated with doc, and
1028: following the standard URL format rules.
1.53 cvs 1029: if doc is 0 and otherPath not NULL, normalizes orgName according to this
1030: other path.
1.9 cvs 1031: The function returns the new complete and normalized URL
1.12 cvs 1032: or file name path (newName) and the name of the document (docName).
1.9 cvs 1033: N.B. If the function can't find out what's the docName, it assigns
1034: the name "noname.html".
1.4 cvs 1035: ----------------------------------------------------------------------*/
1.3 cvs 1036: #ifdef __STDC__
1.84 cvs 1037: void NormalizeURL (CHAR_T* orgName, Document doc, CHAR_T* newName, CHAR_T* docName, CHAR_T* otherPath)
1.3 cvs 1038: #else /* __STDC__ */
1.53 cvs 1039: void NormalizeURL (orgName, doc, newName, docName, otherPath)
1.84 cvs 1040: CHAR_T* orgName;
1.3 cvs 1041: Document doc;
1.84 cvs 1042: CHAR_T* newName;
1043: CHAR_T* docName;
1044: CHAR_T* otherPath;
1.3 cvs 1045: #endif /* __STDC__ */
1046: {
1.84 cvs 1047: CHAR_T* basename;
1048: CHAR_T tempOrgName[MAX_LENGTH];
1049: CHAR_T* ptr;
1050: CHAR_T used_sep;
1051: int length;
1052: ThotBool check;
1.5 cvs 1053:
1.44 cvs 1054: # ifdef _WINDOWS
1055: int ndx;
1056: # endif /* _WINDOWS */
1057:
1.5 cvs 1058: if (!newName || !docName)
1059: return;
1.18 cvs 1060:
1.32 cvs 1061: if (doc != 0)
1.53 cvs 1062: basename = GetBaseURL (doc);
1063: else if (otherPath != NULL)
1.84 cvs 1064: basename = TtaWCSdup (otherPath);
1.32 cvs 1065: else
1.53 cvs 1066: basename = NULL;
1.32 cvs 1067:
1.18 cvs 1068: /*
1.31 cvs 1069: * Clean orgName
1070: * Make sure we have a complete orgName, without any leading or trailing
1071: * white spaces, or trailinbg new lines
1072: */
1.5 cvs 1073: ptr = orgName;
1.18 cvs 1074: /* skip leading white space and new line characters */
1.84 cvs 1075: while ((*ptr == WC_SPACE || *ptr == WC_EOL) && *ptr++ != WC_EOS);
1076: ustrncpy (tempOrgName, ptr, MAX_LENGTH -1);
1077: tempOrgName[MAX_LENGTH -1] = WC_EOS;
1.18 cvs 1078: /*
1.31 cvs 1079: * Make orgName a complete URL
1080: * If the URL does not include a protocol, then try to calculate
1081: * one using the doc's base element (if it exists),
1082: */
1.84 cvs 1083: if (tempOrgName[0] == WC_EOS)
1.53 cvs 1084: {
1.84 cvs 1085: newName[0] = WC_EOS;
1.53 cvs 1086: TtaFreeMemory (basename);
1087: return;
1088: }
1.49 cvs 1089:
1090: /* clean trailing white space */
1.84 cvs 1091: length = ustrlen (tempOrgName) - 1;
1092: while (tempOrgName[length] == WC_SPACE && tempOrgName[length] == WC_EOL)
1.53 cvs 1093: {
1.84 cvs 1094: tempOrgName[length] = WC_EOS;
1.53 cvs 1095: length--;
1096: }
1.50 cvs 1097:
1.55 cvs 1098: /* remove extra dot (which dot???) */
1099: /* ugly, but faster than a strcmp */
1.84 cvs 1100: if (tempOrgName[length] == TEXT('.')
1101: && (length == 0 || tempOrgName[length-1] != TEXT('.')))
1102: tempOrgName[length] = WC_EOS;
1.50 cvs 1103:
1.94 cvs 1104: if (IsW3Path (tempOrgName))
1.53 cvs 1105: {
1106: /* the name is complete, go to the Sixth Step */
1.84 cvs 1107: ustrcpy (newName, tempOrgName);
1.53 cvs 1108: SimplifyUrl (&newName);
1109: /* verify if the URL has the form "protocol://server:port" */
1.84 cvs 1110: ptr = AmayaParseUrl (newName, TEXT(""), AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1111: if (ptr && !ustrcmp (ptr, newName)) /* it has this form, we complete it by adding a DIR_STR */
1112: ustrcat (newName, WC_URL_STR);
1.49 cvs 1113:
1.53 cvs 1114: if (ptr)
1.50 cvs 1115: TtaFreeMemory (ptr);
1.53 cvs 1116: }
1117: else if ( basename == NULL)
1118: /* the name is complete, go to the Sixth Step */
1.84 cvs 1119: ustrcpy (newName, tempOrgName);
1.53 cvs 1120: else
1121: {
1.31 cvs 1122: /* Calculate the absolute URL, using the base or document URL */
1.44 cvs 1123: # ifdef _WINDOWS
1.53 cvs 1124: if (!IsW3Path (basename))
1125: {
1.67 cvs 1126: length = ustrlen (tempOrgName);
1.53 cvs 1127: for (ndx = 0; ndx < length; ndx++)
1.67 cvs 1128: if (tempOrgName [ndx] == TEXT('/'))
1129: tempOrgName [ndx] = TEXT('\\');
1.53 cvs 1130: }
1.44 cvs 1131: # endif /* _WINDOWS */
1.25 cvs 1132: ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53 cvs 1133: if (ptr)
1134: {
1135: SimplifyUrl (&ptr);
1.84 cvs 1136: ustrcpy (newName, ptr);
1.53 cvs 1137: TtaFreeMemory (ptr);
1138: }
1139: else
1.84 cvs 1140: newName[0] = WC_EOS;
1.53 cvs 1141: }
1.36 cvs 1142:
1143: TtaFreeMemory (basename);
1.18 cvs 1144: /*
1.31 cvs 1145: * Prepare the docname that will refer to this ressource in the
1146: * .amaya directory. If the new URL finishes on DIR_SEP, then use
1147: * noname.html as a default ressource name
1.18 cvs 1148: */
1.84 cvs 1149: if (newName[0] != WC_EOS)
1.53 cvs 1150: {
1.84 cvs 1151: length = ustrlen (newName) - 1;
1152: if (newName[length] == WC_URL_SEP || newName[length] == WC_DIR_SEP)
1.53 cvs 1153: {
1154: used_sep = newName[length];
1155: check = TRUE;
1156: while (check)
1157: {
1.50 cvs 1158: length--;
1159: while (length >= 0 && newName[length] != used_sep)
1.53 cvs 1160: length--;
1.84 cvs 1161: if (!ustrncmp (&newName[length+1], TEXT(".."), 2))
1.53 cvs 1162: {
1.84 cvs 1163: newName[length+1] = WC_EOS;
1.53 cvs 1164: /* remove also previous directory */
1165: length--;
1166: while (length >= 0 && newName[length] != used_sep)
1167: length--;
1.84 cvs 1168: if (ustrncmp (&newName[length+1], TEXT("//"), 2))
1.53 cvs 1169: /* don't remove server name */
1.84 cvs 1170: newName[length+1] = WC_EOS;
1.53 cvs 1171: }
1.84 cvs 1172: else if (!ustrncmp (&newName[length+1], TEXT("."), 1))
1173: newName[length+1] = WC_EOS;
1.50 cvs 1174: else
1.53 cvs 1175: check = FALSE;
1176: }
1.84 cvs 1177: ustrcpy (docName, TEXT("noname.html"));
1.53 cvs 1178: /* docname was not comprised inside the URL, so let's */
1179: /* assign the default ressource name */
1.84 cvs 1180: ustrcpy (docName, TEXT("noname.html"));
1.53 cvs 1181: }
1182: else
1183: { /* docname is comprised inside the URL */
1.84 cvs 1184: while (length >= 0 && newName[length] != WC_URL_SEP && newName[length] != WC_DIR_SEP)
1.53 cvs 1185: length--;
1186: if (length < 0)
1.84 cvs 1187: ustrcpy (docName, newName);
1.53 cvs 1188: else
1.84 cvs 1189: ustrcpy (docName, &newName[length+1]);
1.53 cvs 1190: }
1191: }
1192: else
1.84 cvs 1193: docName[0] = WC_EOS;
1.18 cvs 1194: }
1.3 cvs 1195:
1.4 cvs 1196: /*----------------------------------------------------------------------
1.9 cvs 1197: IsSameHost
1.4 cvs 1198: ----------------------------------------------------------------------*/
1.3 cvs 1199: #ifdef __STDC__
1.67 cvs 1200: ThotBool IsSameHost (const STRING url1, const STRING url2)
1.3 cvs 1201: #else /* __STDC__ */
1.67 cvs 1202: ThotBool IsSameHost (url1, url2)
1203: const STRING url1;
1204: const STRING url2;
1.3 cvs 1205: #endif /* __STDC__ */
1206: {
1.67 cvs 1207: STRING basename_ptr1, basename_ptr2;
1208: ThotBool result;
1.3 cvs 1209:
1.84 cvs 1210: basename_ptr1 = AmayaParseUrl (url1, TEXT(""), AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.86 cvs 1211: basename_ptr2 = AmayaParseUrl (url2, TEXT(""), AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3 cvs 1212:
1.67 cvs 1213: if (ustrcmp (basename_ptr1, basename_ptr2))
1.8 cvs 1214: result = FALSE;
1.5 cvs 1215: else
1.8 cvs 1216: result = TRUE;
1.3 cvs 1217:
1.25 cvs 1218: TtaFreeMemory (basename_ptr1);
1219: TtaFreeMemory (basename_ptr2);
1.5 cvs 1220: return (result);
1.3 cvs 1221: }
1222:
1223:
1.4 cvs 1224: /*----------------------------------------------------------------------
1.22 cvs 1225: HasKnownFileSuffix
1226: returns TRUE if path points to a file ending with a suffix.
1227: ----------------------------------------------------------------------*/
1228: #ifdef __STDC__
1.67 cvs 1229: ThotBool HasKnownFileSuffix (const STRING path)
1.22 cvs 1230: #else /* __STDC__ */
1.67 cvs 1231: ThotBool HasKnownFileSuffix (path)
1232: const STRING path;
1.22 cvs 1233: #endif /* __STDC__ */
1234: {
1.67 cvs 1235: STRING root;
1236: CHAR_T temppath[MAX_LENGTH];
1237: CHAR_T suffix[MAX_LENGTH];
1.22 cvs 1238:
1.87 cvs 1239: if (!path || path[0] == WC_EOS || path[ustrlen(path)] == WC_DIR_SEP)
1.22 cvs 1240: return (FALSE);
1241:
1.84 cvs 1242: root = AmayaParseUrl(path, TEXT(""), AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22 cvs 1243:
1244: if (root)
1245: {
1.67 cvs 1246: ustrcpy (temppath, root);
1.25 cvs 1247: TtaFreeMemory (root);
1.22 cvs 1248: /* Get the suffix */
1249: ExtractSuffix (temppath, suffix);
1250:
1.87 cvs 1251: if( suffix[0] == WC_EOS)
1.22 cvs 1252: /* no suffix */
1253: return (FALSE);
1254:
1255: /* Normalize the suffix */
1256: ConvertToLowerCase (suffix);
1257:
1.67 cvs 1258: if (!ustrcmp (suffix, TEXT("gz")))
1.22 cvs 1259: /* skip the compressed suffix */
1260: {
1261: ExtractSuffix (temppath, suffix);
1.87 cvs 1262: if(suffix[0] == WC_EOS)
1.22 cvs 1263: /* no suffix */
1264: return (FALSE);
1265: /* Normalize the suffix */
1266: ConvertToLowerCase (suffix);
1267: }
1268:
1.67 cvs 1269: if (ustrcmp (suffix, TEXT("gif")) &&
1270: ustrcmp (suffix, TEXT("xbm")) &&
1271: ustrcmp (suffix, TEXT("xpm")) &&
1272: ustrcmp (suffix, TEXT("jpg")) &&
1273: ustrcmp (suffix, TEXT("pdf")) &&
1274: ustrcmp (suffix, TEXT("png")) &&
1275: ustrcmp (suffix, TEXT("tgz")) &&
1276: ustrcmp (suffix, TEXT("xpg")) &&
1277: ustrcmp (suffix, TEXT("xpd")) &&
1278: ustrcmp (suffix, TEXT("ps")) &&
1279: ustrcmp (suffix, TEXT("au")) &&
1280: ustrcmp (suffix, TEXT("html")) &&
1281: ustrcmp (suffix, TEXT("htm")) &&
1282: ustrcmp (suffix, TEXT("shtml")) &&
1283: ustrcmp (suffix, TEXT("xht")) &&
1284: ustrcmp (suffix, TEXT("xhtm")) &&
1285: ustrcmp (suffix, TEXT("xhtml")) &&
1286: ustrcmp (suffix, TEXT("txt")) &&
1287: ustrcmp (suffix, TEXT("css")) &&
1288: ustrcmp (suffix, TEXT("eps")))
1.22 cvs 1289: return (FALSE);
1290: else
1291: return (TRUE);
1292: }
1293: else
1294: return (FALSE);
1295: }
1296:
1297:
1298: /*----------------------------------------------------------------------
1.24 cvs 1299: ChopURL
1300: Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL).
1301: If inputURL is bigger than that size, outputURL receives
1302: MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...",
1303: and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
1304: If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
1305: copied into outputURL.
1306: N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
1307: chars.
1308: ----------------------------------------------------------------------*/
1309: #ifdef __STDC__
1.86 cvs 1310: void ChopURL (CHAR_T* outputURL, const CHAR_T* inputURL)
1.24 cvs 1311: #else
1312: void ChopURL (outputURL, inputURL)
1.86 cvs 1313: CHAR_T* outputURL;
1314: const CHAR_T* inputURL;
1.24 cvs 1315: #endif
1.22 cvs 1316:
1.24 cvs 1317: {
1318: int len;
1.9 cvs 1319:
1.86 cvs 1320: len = ustrlen (inputURL);
1.24 cvs 1321: if (len <= MAX_PRINT_URL_LENGTH)
1.86 cvs 1322: ustrcpy (outputURL, inputURL);
1.24 cvs 1323: else
1324: /* make a truncated urlName on the status window */
1325: {
1.86 cvs 1326: ustrncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
1327: outputURL [MAX_PRINT_URL_LENGTH / 2] = WC_EOS;
1328: ustrcat (outputURL, TEXT("..."));
1329: ustrcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24 cvs 1330: }
1.25 cvs 1331: }
1332:
1333:
1334: /*----------------------------------------------------------------------
1335: scan
1.47 cvs 1336: Scan a filename for its constituents
1.25 cvs 1337: -----------------------------------
1338:
1339: On entry,
1340: name points to a document name which may be incomplete.
1341: On exit,
1342: absolute or relative may be nonzero (but not both).
1343: host, fragment and access may be nonzero if they were specified.
1344: Any which are nonzero point to zero terminated strings.
1345: ----------------------------------------------------------------------*/
1346: #ifdef __STDC__
1.67 cvs 1347: static void scan (STRING name, HTURI * parts)
1.25 cvs 1348: #else /* __STDC__ */
1349: static void scan (name, parts)
1.67 cvs 1350: STRING name;
1351: HTURI *parts;
1.25 cvs 1352:
1353: #endif /* __STDC__ */
1354: {
1.67 cvs 1355: STRING p;
1356: STRING after_access = name;
1.32 cvs 1357:
1.43 cvs 1358: memset (parts, '\0', sizeof (HTURI));
1.28 cvs 1359: /* Look for fragment identifier */
1.67 cvs 1360: if ((p = ustrchr(name, TEXT('#'))) != NULL)
1.28 cvs 1361: {
1.67 cvs 1362: *p++ = TEXT('\0');
1.28 cvs 1363: parts->fragment = p;
1.25 cvs 1364: }
1365:
1.28 cvs 1366: for (p=name; *p; p++)
1367: {
1.67 cvs 1368: if (*p == URL_SEP || *p == DIR_SEP || *p == TEXT('#') || *p == TEXT('?'))
1.28 cvs 1369: break;
1.67 cvs 1370: if (*p == TEXT(':'))
1.28 cvs 1371: {
1372: *p = 0;
1373: parts->access = after_access; /* Scheme has been specified */
1374:
1375: /* The combination of gcc, the "-O" flag and the HP platform is
1376: unhealthy. The following three lines is a quick & dirty fix, but is
1377: not recommended. Rather, turn off "-O". */
1378:
1379: /* after_access = p;*/
1380: /* while (*after_access == 0)*/
1381: /* after_access++;*/
1382: after_access = p+1;
1.67 cvs 1383: if (!ustrcasecmp(TEXT("URL"), parts->access))
1.28 cvs 1384: /* Ignore IETF's URL: pre-prefix */
1385: parts->access = NULL;
1386: else
1.25 cvs 1387: break;
1388: }
1389: }
1390:
1391: p = after_access;
1.43 cvs 1392: if (*p == URL_SEP || *p == DIR_SEP)
1.28 cvs 1393: {
1.43 cvs 1394: if (p[1] == URL_SEP)
1.28 cvs 1395: {
1.25 cvs 1396: parts->host = p+2; /* host has been specified */
1.28 cvs 1397: *p = 0; /* Terminate access */
1398: /* look for end of host name if any */
1.67 cvs 1399: p = ustrchr (parts->host, URL_SEP);
1.28 cvs 1400: if (p)
1401: {
1.87 cvs 1402: *p = WC_EOS; /* Terminate host */
1.25 cvs 1403: parts->absolute = p+1; /* Root has been found */
1.28 cvs 1404: }
1405: }
1406: else
1407: /* Root found but no host */
1408: parts->absolute = p+1;
1409: }
1410: else
1411: {
1.25 cvs 1412: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28 cvs 1413: }
1.25 cvs 1414: }
1415:
1416:
1417: /*----------------------------------------------------------------------
1.28 cvs 1418: AmayaParseUrl: parse a Name relative to another name
1419:
1420: This returns those parts of a name which are given (and requested)
1421: substituting bits from the related name where necessary.
1.25 cvs 1422:
1.28 cvs 1423: On entry,
1.25 cvs 1424: aName A filename given
1425: relatedName A name relative to which aName is to be parsed. Give
1426: it an empty string if aName is absolute.
1427: wanted A mask for the bits which are wanted.
1428:
1.28 cvs 1429: On exit,
1.25 cvs 1430: returns A pointer to a malloc'd string which MUST BE FREED
1431: ----------------------------------------------------------------------*/
1432: #ifdef __STDC__
1.84 cvs 1433: CHAR_T* AmayaParseUrl (const CHAR_T* aName, CHAR_T* relatedName, int wanted)
1.25 cvs 1434: #else /* __STDC__ */
1.84 cvs 1435: CHAR_T* AmayaParseUrl (aName, relatedName, wanted)
1436: const CHAR_T* aName;
1437: CHAR_T* relatedName;
1.28 cvs 1438: int wanted;
1.25 cvs 1439:
1440: #endif /* __STDC__ */
1441: {
1.84 cvs 1442: CHAR_T* return_value;
1.67 cvs 1443: CHAR_T result[MAX_LENGTH];
1444: CHAR_T name[MAX_LENGTH];
1445: CHAR_T rel[MAX_LENGTH];
1.84 cvs 1446: CHAR_T *p, *access;
1.29 cvs 1447: HTURI given, related;
1448: int len;
1.67 cvs 1449: CHAR_T used_sep;
1.84 cvs 1450: CHAR_T* used_str;
1.32 cvs 1451:
1.84 cvs 1452: if (ustrchr (aName, WC_DIR_SEP) || ustrchr (relatedName, WC_DIR_SEP))
1.33 cvs 1453: {
1.84 cvs 1454: used_str = WC_DIR_STR;
1455: used_sep = WC_DIR_SEP;
1.33 cvs 1456: }
1.32 cvs 1457: else
1.33 cvs 1458: {
1.84 cvs 1459: used_str = WC_URL_STR;
1460: used_sep = WC_URL_SEP;
1.33 cvs 1461: }
1.32 cvs 1462:
1.29 cvs 1463: /* Make working copies of input strings to cut up: */
1464: return_value = NULL;
1465: result[0] = 0; /* Clear string */
1.67 cvs 1466: ustrcpy (name, aName);
1.29 cvs 1467: if (relatedName != NULL)
1.67 cvs 1468: ustrcpy (rel, relatedName);
1.29 cvs 1469: else
1.84 cvs 1470: relatedName[0] = WC_EOS;
1.29 cvs 1471:
1472: scan (name, &given);
1473: scan (rel, &related);
1474: access = given.access ? given.access : related.access;
1475: if (wanted & AMAYA_PARSE_ACCESS)
1476: if (access)
1477: {
1.67 cvs 1478: ustrcat (result, access);
1.29 cvs 1479: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.67 cvs 1480: ustrcat (result, TEXT(":"));
1.29 cvs 1481: }
1482:
1483: if (given.access && related.access)
1484: /* If different, inherit nothing. */
1.67 cvs 1485: if (ustrcmp (given.access, related.access) != 0)
1.29 cvs 1486: {
1487: related.host = 0;
1488: related.absolute = 0;
1489: related.relative = 0;
1490: related.fragment = 0;
1491: }
1492:
1493: if (wanted & AMAYA_PARSE_HOST)
1494: if(given.host || related.host)
1495: {
1496: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.67 cvs 1497: ustrcat (result, TEXT("//"));
1498: ustrcat (result, given.host ? given.host : related.host);
1.29 cvs 1499: }
1500:
1501: if (given.host && related.host)
1502: /* If different hosts, inherit no path. */
1.67 cvs 1503: if (ustrcmp (given.host, related.host) != 0)
1.29 cvs 1504: {
1505: related.absolute = 0;
1506: related.relative = 0;
1507: related.fragment = 0;
1508: }
1509:
1510: if (wanted & AMAYA_PARSE_PATH)
1511: {
1512: if (given.absolute)
1513: {
1514: /* All is given */
1515: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67 cvs 1516: ustrcat (result, used_str);
1517: ustrcat (result, given.absolute);
1.25 cvs 1518: }
1.29 cvs 1519: else if (related.absolute)
1520: {
1521: /* Adopt path not name */
1.67 cvs 1522: ustrcat (result, used_str);
1523: ustrcat (result, related.absolute);
1.29 cvs 1524: if (given.relative)
1525: {
1526: /* Search part? */
1.67 cvs 1527: p = ustrchr (result, TEXT('?'));
1.29 cvs 1528: if (!p)
1.67 cvs 1529: p=result+ustrlen(result)-1;
1.33 cvs 1530: for (; *p!=used_sep; p--); /* last / */
1.29 cvs 1531: /* Remove filename */
1532: p[1]=0;
1533: /* Add given one */
1.67 cvs 1534: ustrcat (result, given.relative);
1.25 cvs 1535: }
1536: }
1.29 cvs 1537: else if (given.relative)
1538: /* what we've got */
1.67 cvs 1539: ustrcat (result, given.relative);
1.29 cvs 1540: else if (related.relative)
1.67 cvs 1541: ustrcat (result, related.relative);
1.29 cvs 1542: else
1543: /* No inheritance */
1.67 cvs 1544: ustrcat (result, used_str);
1.25 cvs 1545: }
1.29 cvs 1546:
1547: if (wanted & AMAYA_PARSE_ANCHOR)
1548: if (given.fragment || related.fragment)
1549: {
1550: if (given.absolute && given.fragment)
1551: {
1552: /*Fixes for relURLs...*/
1553: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67 cvs 1554: ustrcat (result, TEXT("#"));
1555: ustrcat (result, given.fragment);
1.29 cvs 1556: }
1557: else if (!(given.absolute) && !(given.fragment))
1.84 cvs 1558: ustrcat (result, TEXT(""));
1.29 cvs 1559: else
1560: {
1561: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.67 cvs 1562: ustrcat (result, TEXT("#"));
1563: ustrcat (result, given.fragment ? given.fragment : related.fragment);
1.29 cvs 1564: }
1565: }
1.67 cvs 1566: len = ustrlen (result);
1567: if ((return_value = TtaAllocString (len + 1)) != NULL)
1568: ustrcpy (return_value, result);
1.29 cvs 1569: return (return_value); /* exactly the right length */
1.25 cvs 1570: }
1571:
1572: /*----------------------------------------------------------------------
1573: HTCanon
1574: Canonicalizes the URL in the following manner starting from the host
1575: pointer:
1576:
1577: 1) The host name is converted to lowercase
1578: 2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
1579:
1580: Return: OK The position of the current path part of the URL
1581: which might be the old one or a new one.
1582:
1583: ----------------------------------------------------------------------*/
1584: #ifdef __STDC__
1.84 cvs 1585: static CHAR_T* HTCanon (CHAR_T** filename, CHAR_T* host)
1.25 cvs 1586: #else /* __STDC__ */
1.67 cvs 1587: static STRING HTCanon (filename, host)
1.84 cvs 1588: CHAR_T** filename;
1589: CHAR_T* host;
1.25 cvs 1590: #endif /* __STDC__ */
1591: {
1.84 cvs 1592: CHAR_T* newname = NULL;
1593: CHAR_T used_sep;
1594: CHAR_T* path;
1595: CHAR_T* strptr;
1596: CHAR_T* port;
1597: CHAR_T* access = host-3;
1.32 cvs 1598:
1599:
1.84 cvs 1600: if (*filename && ustrchr (*filename, WC_URL_SEP))
1.33 cvs 1601: {
1.84 cvs 1602: used_sep = WC_URL_SEP;
1.33 cvs 1603: }
1604: else
1605: {
1.84 cvs 1606: used_sep = WC_DIR_SEP;
1.33 cvs 1607: }
1.32 cvs 1608:
1.82 cvs 1609: while (access > *filename && *(access - 1) != used_sep) /* Find access method */
1.25 cvs 1610: access--;
1.84 cvs 1611: if ((path = ustrchr (host, used_sep)) == NULL) /* Find path */
1612: path = host + ustrlen (host);
1613: if ((strptr = ustrchr (host, TEXT('@'))) != NULL && strptr < path) /* UserId */
1.82 cvs 1614: host = strptr;
1.84 cvs 1615: if ((port = ustrchr (host, TEXT(':'))) != NULL && port > path) /* Port number */
1.82 cvs 1616: port = NULL;
1.25 cvs 1617:
1618: strptr = host; /* Convert to lower-case */
1.82 cvs 1619: while (strptr < path)
1.33 cvs 1620: {
1.84 cvs 1621: *strptr = utolower (*strptr);
1.82 cvs 1622: strptr++;
1.33 cvs 1623: }
1.25 cvs 1624:
1625: /* Does the URL contain a full domain name? This also works for a
1626: numerical host name. The domain name is already made lower-case
1627: and without a trailing dot. */
1628: {
1.84 cvs 1629: CHAR_T* dot = port ? port : path;
1630: if (dot > *filename && *--dot == TEXT('.'))
1.33 cvs 1631: {
1.84 cvs 1632: CHAR_T* orig = dot;
1633: CHAR_T* dest = dot + 1;
1.82 cvs 1634: while ((*orig++ = *dest++));
1635: if (port) port--;
1.33 cvs 1636: path--;
1.25 cvs 1637: }
1638: }
1639: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33 cvs 1640: if (port)
1641: {
1.82 cvs 1642: if (!*(port+1) || *(port+1) == used_sep)
1.33 cvs 1643: {
1644: if (!newname)
1645: {
1.84 cvs 1646: CHAR_T* orig = port;
1647: CHAR_T* dest = port + 1;
1.82 cvs 1648: while ((*orig++ = *dest++));
1.33 cvs 1649: }
1650: }
1.84 cvs 1651: else if ((!ustrncmp (access, TEXT("http"), 4) &&
1652: (*(port + 1) == TEXT('8') &&
1653: *(port+2) == TEXT('0') &&
1.82 cvs 1654: (*(port+3) == used_sep || !*(port + 3)))) ||
1.84 cvs 1655: (!ustrncmp (access, TEXT("gopher"), 6) &&
1656: (*(port+1) == TEXT('7') &&
1657: *(port+2) == TEXT('0') &&
1.82 cvs 1658: (*(port+3) == used_sep || !*(port+3)))) ||
1.84 cvs 1659: (!ustrncmp (access, TEXT("ftp"), 3) &&
1660: (*(port+1) == TEXT('2') &&
1661: *(port + 2) == TEXT('1') &&
1.82 cvs 1662: (*(port+3) == used_sep || !*(port+3))))) {
1.33 cvs 1663: if (!newname)
1664: {
1.84 cvs 1665: CHAR_T* orig = port;
1666: CHAR_T* dest = port + 3;
1.33 cvs 1667: while((*orig++ = *dest++));
1668: /* Update path position, Henry Minsky */
1669: path -= 3;
1.25 cvs 1670: }
1.33 cvs 1671: }
1672: else if (newname)
1.84 cvs 1673: ustrncat (newname, port, (int) (path - port));
1.33 cvs 1674: }
1.25 cvs 1675:
1.33 cvs 1676: if (newname)
1677: {
1.84 cvs 1678: CHAR_T* newpath = newname + ustrlen (newname);
1679: ustrcat (newname, path);
1.25 cvs 1680: path = newpath;
1.28 cvs 1681: /* Free old copy */
1682: TtaFreeMemory(*filename);
1.25 cvs 1683: *filename = newname;
1.33 cvs 1684: }
1.25 cvs 1685: return path;
1686: }
1687:
1688:
1689: /*----------------------------------------------------------------------
1.29 cvs 1690: SimplifyUrl: simplify a URI
1.32 cvs 1691: A URI is allowed to contain the sequence xxx/../ which may be
1692: replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28 cvs 1693: Simplification helps us recognize duplicate URIs.
1.25 cvs 1694:
1.28 cvs 1695: Thus, /etc/junk/../fred becomes /etc/fred
1696: /etc/junk/./fred becomes /etc/junk/fred
1.25 cvs 1697:
1.28 cvs 1698: but we should NOT change
1699: http://fred.xxx.edu/../..
1.25 cvs 1700:
1701: or ../../albert.html
1702:
1.28 cvs 1703: In order to avoid empty URLs the following URLs become:
1.25 cvs 1704:
1705: /fred/.. becomes /fred/..
1706: /fred/././.. becomes /fred/..
1707: /fred/.././junk/.././ becomes /fred/..
1708:
1.28 cvs 1709: If more than one set of `://' is found (several proxies in cascade) then
1710: only the part after the last `://' is simplified.
1.25 cvs 1711:
1.28 cvs 1712: Returns: A string which might be the old one or a new one.
1.25 cvs 1713: ----------------------------------------------------------------------*/
1714: #ifdef __STDC__
1.84 cvs 1715: void SimplifyUrl (CHAR_T** url)
1.25 cvs 1716: #else /* __STDC__ */
1.29 cvs 1717: void SimplifyUrl (url)
1.84 cvs 1718: CHAR_T** url;
1.25 cvs 1719: #endif /* __STDC__ */
1720: {
1.84 cvs 1721: CHAR_T* path;
1722: CHAR_T* access;
1723: CHAR_T* newptr;
1724: CHAR_T* p;
1725: CHAR_T *orig, *dest, *end;
1.28 cvs 1726:
1.85 cvs 1727: CHAR_T used_sep;
1.77 cvs 1728: ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
1729: something/../ simplification in relative URLs when they start with a ../ */
1.32 cvs 1730:
1731:
1.28 cvs 1732: if (!url || !*url)
1733: return;
1734:
1.84 cvs 1735: if (ustrchr (*url, WC_URL_SEP))
1.33 cvs 1736: {
1.84 cvs 1737: used_sep = WC_URL_SEP;
1.33 cvs 1738: }
1.32 cvs 1739: else
1.33 cvs 1740: {
1.84 cvs 1741: used_sep = WC_DIR_SEP;
1.33 cvs 1742: }
1.32 cvs 1743:
1.77 cvs 1744: /* should we simplify double dot? */
1745: path = *url;
1.84 cvs 1746: if (*path == TEXT('.') && *(path + 1) == TEXT('.'))
1.77 cvs 1747: ddot_simplify = FALSE;
1748: else
1749: ddot_simplify = TRUE;
1750:
1.28 cvs 1751: /* Find any scheme name */
1.84 cvs 1752: if ((path = ustrstr (*url, TEXT("://"))) != NULL)
1.33 cvs 1753: {
1754: /* Find host name */
1.28 cvs 1755: access = *url;
1.84 cvs 1756: while (access < path && (*access = utolower (*access)))
1.82 cvs 1757: access++;
1.28 cvs 1758: path += 3;
1.84 cvs 1759: while ((newptr = ustrstr (path, TEXT ("://"))) != NULL)
1.82 cvs 1760: /* For proxies */
1761: path = newptr+3;
1762: /* We have a host name */
1.84 cvs 1763: path = HTCanon (url, path);
1.25 cvs 1764: }
1.84 cvs 1765: else if ((path = ustrstr (*url, TEXT(":/"))) != NULL)
1.28 cvs 1766: path += 2;
1767: else
1768: path = *url;
1.25 cvs 1769:
1.84 cvs 1770: if (*path == used_sep && *(path+1) == used_sep)
1.28 cvs 1771: /* Some URLs start //<foo> */
1772: path += 1;
1.94 cvs 1773: else if (IsFilePath (path))
1774: {
1775: /* doesn't need to do anything more */
1776: return;
1777: }
1.84 cvs 1778: else if (!ustrncmp (path, TEXT("news:"), 5))
1.28 cvs 1779: {
1.84 cvs 1780: newptr = ustrchr (path+5, TEXT('@'));
1.28 cvs 1781: if (!newptr)
1782: newptr = path + 5;
1783: while (*newptr)
1784: {
1785: /* Make group or host lower case */
1.84 cvs 1786: *newptr = utolower (*newptr);
1.28 cvs 1787: newptr++;
1.25 cvs 1788: }
1.28 cvs 1789: /* Doesn't need to do any more */
1790: return;
1.25 cvs 1791: }
1.28 cvs 1792:
1793: if ((p = path))
1794: {
1.67 cvs 1795: if (!((end = ustrchr (path, TEXT(';'))) || (end = ustrchr (path, TEXT('?'))) ||
1796: (end = ustrchr (path, TEXT('#')))))
1797: end = path + ustrlen (path);
1.28 cvs 1798:
1799: /* Parse string second time to simplify */
1800: p = path;
1801: while (p < end)
1802: {
1.77 cvs 1803: /* if we're pointing to a char, it's safe to reactivate the ../ convertion */
1804: if (!ddot_simplify && *p != TEXT('.') && *p != used_sep)
1805: ddot_simplify = TRUE;
1806:
1.33 cvs 1807: if (*p==used_sep)
1.28 cvs 1808: {
1.67 cvs 1809: if (p > *url && *(p+1) == TEXT('.') && (*(p+2) == used_sep || !*(p+2)))
1.28 cvs 1810: {
1811: orig = p + 1;
1.84 cvs 1812: dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52 cvs 1813: while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28 cvs 1814: end = orig - 1;
1815: }
1.77 cvs 1816: else if (ddot_simplify && *(p+1) == TEXT('.') && *(p+2) == TEXT('.')
1817: && (*(p+3) == used_sep || !*(p+3)))
1.28 cvs 1818: {
1819: newptr = p;
1.52 cvs 1820: while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
1821: if (*newptr == used_sep)
1822: orig = newptr + 1;
1.28 cvs 1823: else
1.52 cvs 1824: orig = newptr;
1825:
1826: dest = (*(p+3) != used_sep) ? p+3 : p+4;
1827: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
1828: end = orig-1;
1829: /* Start again with prev slash */
1830: p = newptr;
1.28 cvs 1831: }
1.33 cvs 1832: else if (*(p+1) == used_sep)
1.28 cvs 1833: {
1.33 cvs 1834: while (*(p+1) == used_sep)
1.28 cvs 1835: {
1836: orig = p;
1837: dest = p + 1;
1838: while ((*orig++ = *dest++)); /* Remove multiple /'s */
1839: end = orig-1;
1840: }
1841: }
1842: else
1.25 cvs 1843: p++;
1.28 cvs 1844: }
1845: else
1846: p++;
1.25 cvs 1847: }
1848: }
1.51 cvs 1849:
1850: /*
1851: ** Check for host/../.. kind of things
1852: */
1.77 cvs 1853: if (*path == used_sep && *(path+1) == TEXT('.') && *(path+2) == TEXT('.')
1854: && (!*(path+3) || *(path+3) == used_sep))
1.87 cvs 1855: *(path+1) = WC_EOS;
1.51 cvs 1856:
1.28 cvs 1857: return;
1858: }
1859:
1860:
1861: /*----------------------------------------------------------------------
1.96 cvs 1862: NormalizeFile normalizes local names.
1.28 cvs 1863: Return TRUE if target and src differ.
1864: ----------------------------------------------------------------------*/
1865: #ifdef __STDC__
1.97 cvs 1866: ThotBool NormalizeFile (CHAR_T* src, CHAR_T* target, ConvertionType convertion)
1.28 cvs 1867: #else
1.97 cvs 1868: ThotBool NormalizeFile (src, target, convertion)
1.84 cvs 1869: CHAR_T* src;
1870: CHAR_T* target;
1.97 cvs 1871: ConvertionType convertion;
1.28 cvs 1872:
1873: #endif
1874: {
1.93 cvs 1875: # ifndef _WINDOWS
1.90 cvs 1876: CHAR_T *s;
1.93 cvs 1877: int i;
1878: # endif /* !_WINDOWS */
1.82 cvs 1879: ThotBool change;
1.90 cvs 1880: int start_index; /* the first char that we'll copy */
1.28 cvs 1881:
1.54 cvs 1882: change = FALSE;
1.90 cvs 1883: start_index = 0;
1884:
1885: if (!src || src[0] == WC_EOS)
1.96 cvs 1886: {
1887: target[0] = WC_EOS;
1888: return FALSE;
1889: }
1.90 cvs 1890:
1891: /* @@ do I need file: or file:/ here? */
1.84 cvs 1892: if (ustrncmp (src, TEXT("file:"), 5) == 0)
1.28 cvs 1893: {
1.90 cvs 1894: /* remove the prefix file: */
1895: start_index += 5;
1896:
1897: /* remove the localhost prefix */
1.94 cvs 1898: if (ustrncmp (&src[start_index], TEXT("//localhost/"), 12) == 0)
1899: start_index += 11;
1900:
1901: /* remove the first two slashes in / / /path */
1902: while (src[start_index] &&
1903: src[start_index] == TEXT('/')
1904: && src[start_index + 1] == TEXT('/'))
1905: start_index++;
1906:
1907: #ifdef _WINDOWS
1908: /* remove any extra slash before the drive name */
1909: if (src[start_index] == TEXT('/')
1910: &&src[start_index+2] == TEXT(':'))
1911: start_index++;
1912: #endif /* _WINDOWS */
1.90 cvs 1913:
1914: if (src[start_index] == WC_EOS)
1915: /* if there's nothing afterwards, add a DIR_STR */
1916: ustrcpy (target, WC_DIR_STR);
1917: else
1.97 cvs 1918: /* as we're inside a file: URL, we'll apply all the convertions
1919: we know */
1920: CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96 cvs 1921:
1922: change = TRUE;
1923: }
1.97 cvs 1924: else if (convertion != AM_CONV_NONE)
1.96 cvs 1925: {
1926: /* we are following a "local" relative link, we do all the
1927: convertions except for the HOME_DIR ~ one */
1.97 cvs 1928: CleanCopyFileURL (target, src, convertion);
1.28 cvs 1929: }
1.90 cvs 1930: #ifndef _WINDOWS
1.84 cvs 1931: else if (src[0] == TEXT('~'))
1.53 cvs 1932: {
1.96 cvs 1933: /* it must be a URL typed in a text input field */
1934: /* do the HOME_DIR ~ substitution */
1.82 cvs 1935: s = TtaGetEnvString ("HOME");
1.84 cvs 1936: ustrcpy (target, s);
1.90 cvs 1937: #if 0
1.96 cvs 1938: /* JK: invalidated this part of the code as it's simpler
1939: to add the DIR_SEP whenever we have something to add
1940: to the path rather than adding it systematically */
1.84 cvs 1941: if (src[1] != WC_DIR_SEP)
1942: ustrcat (target, WC_DIR_STR);
1.90 cvs 1943: #endif
1944: i = ustrlen (target);
1.92 cvs 1945: ustrcpy (&target[i], &src[1]);
1.54 cvs 1946: change = TRUE;
1.53 cvs 1947: }
1.90 cvs 1948: #endif /* _WINDOWS */
1.28 cvs 1949: else
1.96 cvs 1950: /* leave it as it is */
1.92 cvs 1951: ustrcpy (target, src);
1.96 cvs 1952:
1.28 cvs 1953: /* remove /../ and /./ */
1.29 cvs 1954: SimplifyUrl (&target);
1.54 cvs 1955: if (!change)
1.84 cvs 1956: change = ustrcmp (src, target);
1.28 cvs 1957: return (change);
1.25 cvs 1958: }
1959:
1.28 cvs 1960:
1.25 cvs 1961: /*----------------------------------------------------------------------
1.31 cvs 1962: MakeRelativeURL: make relative name
1.25 cvs 1963:
1.28 cvs 1964: This function creates and returns a string which gives an expression of
1965: one address as related to another. Where there is no relation, an absolute
1966: address is retured.
1.25 cvs 1967:
1.28 cvs 1968: On entry,
1.25 cvs 1969: Both names must be absolute, fully qualified names of nodes
1970: (no fragment bits)
1971:
1.28 cvs 1972: On exit,
1.25 cvs 1973: The return result points to a newly allocated name which, if
1974: parsed by AmayaParseUrl relative to relatedName, will yield aName.
1975: The caller is responsible for freeing the resulting name later.
1976: ----------------------------------------------------------------------*/
1977: #ifdef __STDC__
1.67 cvs 1978: STRING MakeRelativeURL (STRING aName, STRING relatedName)
1.25 cvs 1979: #else /* __STDC__ */
1.67 cvs 1980: STRING MakeRelativeURL (aName, relatedName)
1981: STRING aName;
1982: STRING relatedName;
1.25 cvs 1983: #endif /* __STDC__ */
1984: {
1.87 cvs 1985: CHAR_T* return_value;
1.67 cvs 1986: CHAR_T result[MAX_LENGTH];
1.87 cvs 1987: CHAR_T* p;
1988: CHAR_T* q;
1989: CHAR_T* after_access;
1990: CHAR_T* last_slash = NULL;
1.29 cvs 1991: int slashes, levels, len;
1992:
1.44 cvs 1993: # ifdef _WINDOWS
1994: int ndx;
1995: # endif /* _WINDOWS */
1996:
1.29 cvs 1997: if (aName == NULL || relatedName == NULL)
1998: return (NULL);
1999:
2000: slashes = 0;
2001: after_access = NULL;
2002: p = aName;
2003: q = relatedName;
2004: for (; *p && (*p == *q); p++, q++)
1.27 cvs 2005: {
2006: /* Find extent of match */
1.67 cvs 2007: if (*p == TEXT(':'))
1.29 cvs 2008: after_access = p + 1;
1.28 cvs 2009: if (*p == DIR_SEP)
1.27 cvs 2010: {
1.29 cvs 2011: /* memorize the last slash position and count them */
1.27 cvs 2012: last_slash = p;
2013: slashes++;
1.25 cvs 2014: }
2015: }
2016:
1.31 cvs 2017: /* q, p point to the first non-matching character or zero */
1.87 cvs 2018: if (*q == WC_EOS)
1.31 cvs 2019: {
2020: /* New name is a subset of the related name */
2021: /* exactly the right length */
1.67 cvs 2022: len = ustrlen (p);
2023: if ((return_value = TtaAllocString (len + 1)) != NULL)
2024: ustrcpy (return_value, p);
1.31 cvs 2025: }
2026: else if ((slashes < 2 && after_access == NULL)
2027: || (slashes < 3 && after_access != NULL))
2028: {
2029: /* Two names whitout common path */
2030: /* exactly the right length */
1.67 cvs 2031: len = ustrlen (aName);
2032: if ((return_value = TtaAllocString (len + 1)) != NULL)
2033: ustrcpy (return_value, aName);
1.31 cvs 2034: }
2035: else
2036: {
2037: /* Some path in common */
1.67 cvs 2038: if (slashes == 3 && ustrncmp (aName, TEXT("http:"), 5) == 0)
1.31 cvs 2039: /* just the same server */
1.67 cvs 2040: ustrcpy (result, last_slash);
1.31 cvs 2041: else
2042: {
2043: levels= 0;
1.67 cvs 2044: for (; *q && *q != TEXT('#') && *q != TEXT(';') && *q != TEXT('?'); q++)
1.31 cvs 2045: if (*q == DIR_SEP)
2046: levels++;
2047:
1.87 cvs 2048: result[0] = WC_EOS;
1.31 cvs 2049: for (;levels; levels--)
1.67 cvs 2050: ustrcat (result, TEXT("../"));
2051: ustrcat (result, last_slash+1);
1.31 cvs 2052: }
1.52 cvs 2053:
2054: if (!*result)
1.67 cvs 2055: ustrcat (result, TEXT("./"));
1.52 cvs 2056:
1.31 cvs 2057: /* exactly the right length */
1.67 cvs 2058: len = ustrlen (result);
2059: if ((return_value = TtaAllocString (len + 1)) != NULL)
2060: ustrcpy (return_value, result);
1.52 cvs 2061:
1.25 cvs 2062: }
1.44 cvs 2063: # ifdef _WINDOWS
1.67 cvs 2064: len = ustrlen (return_value);
1.44 cvs 2065: for (ndx = 0; ndx < len; ndx ++)
1.67 cvs 2066: if (return_value[ndx] == TEXT('\\'))
2067: return_value[ndx] = TEXT('/') ;
1.44 cvs 2068: # endif /* _WINDOWS */
1.29 cvs 2069: return (return_value);
1.24 cvs 2070: }
1.35 cvs 2071:
2072:
Webmaster