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