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