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