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