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