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