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