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