Annotation of Amaya/amaya/AHTURLTools.c, revision 1.6
1.4 cvs 1: /***
2: *** Copyright (c) 1996 INRIA, All rights reserved
3: ***/
1.3 cvs 4:
5: #include "amaya.h"
6:
7: #include "dialog.h"
8: #include "content.h"
9: #include "view.h"
10: #include "interface.h"
11: #include "message.h"
12: #include "AHTMemConv.h"
13: #include "init.h"
1.5 cvs 14: #include "AHTURLTools.h" /* defined here */
1.3 cvs 15:
1.4 cvs 16: /*----------------------------------------------------------------------
17: IsHTMLName
18: ----------------------------------------------------------------------*/
1.3 cvs 19:
20: #ifdef __STDC__
21: boolean IsHTMLName (char *path)
22: #else /* __STDC__ */
23: boolean IsHTMLName (path)
24: char *path;
25:
26: #endif /* __STDC__ */
27: {
1.5 cvs 28: char temppath[MAX_LENGTH];
29: char suffix[MAX_LENGTH];
30: char nsuffix[MAX_LENGTH];
31: int i;
32:
33: if (!path)
34: return FALSE;
35:
36: strcpy (temppath, path);
37: ExtractSuffix (temppath, suffix);
38:
39: /* Normalize the suffix */
40: i = 0;
41: while (suffix[i] != EOS)
42: nsuffix[i] = TOLOWER (suffix[i++]);
43: nsuffix[i] = EOS;
44: if ((strcmp (nsuffix, "html")) &&
45: (strcmp (nsuffix, "htm")) &&
46: (strcmp (nsuffix, "shtml")))
47: return FALSE;
48: return TRUE;
1.3 cvs 49: }
50:
1.4 cvs 51: /*----------------------------------------------------------------------
52: IsImageName
53: ----------------------------------------------------------------------*/
1.3 cvs 54:
55: #ifdef __STDC__
56: boolean IsImageName (char *path)
57: #else /* __STDC__ */
58: boolean IsImageName (path)
59: char *path;
60:
61: #endif /* __STDC__ */
62: {
1.5 cvs 63: char temppath[MAX_LENGTH];
64: char suffix[MAX_LENGTH];
65: char nsuffix[MAX_LENGTH];
66: int i;
67:
68: if (!path)
69: return FALSE;
70:
71: strcpy (temppath, path);
72: ExtractSuffix (temppath, suffix);
73:
74: /* Normalize the suffix */
75: i = 0;
76: while (suffix[i] != EOS)
77: nsuffix[i] = TOLOWER (suffix[i++]);
78: nsuffix[i] = EOS;
79: if ((strcmp (nsuffix, "gif")) && (strcmp (nsuffix, "xbm")) &&
80: (strcmp (nsuffix, "xpm")) && (strcmp (nsuffix, "jpg")) &&
81: (strcmp (nsuffix, "png")) && (strcmp (nsuffix, "au")))
82: return FALSE;
83: return TRUE;
1.3 cvs 84: }
85:
1.4 cvs 86: /*----------------------------------------------------------------------
87: IsTextName
88: ----------------------------------------------------------------------*/
1.3 cvs 89:
90: #ifdef __STDC__
91: boolean IsTextName (char *path)
92: #else /* __STDC__ */
93: boolean IsTextName (path)
94: char *path;
95:
96: #endif /* __STDC__ */
97: {
1.5 cvs 98: char temppath[MAX_LENGTH];
99: char suffix[MAX_LENGTH];
100: char nsuffix[MAX_LENGTH];
101: int i;
102:
103: if (!path)
104: return FALSE;
105:
106: strcpy (temppath, path);
107: ExtractSuffix (temppath, suffix);
108:
109: /* Normalize the suffix */
110: i = 0;
111: while (suffix[i] != EOS)
112: {
113: nsuffix[i] = TOLOWER (suffix[i]);
114: i++;
115: }
116: nsuffix[i] = EOS;
117:
118: if ((strcmp (nsuffix, "gif")) && (strcmp (nsuffix, "xbm")) &&
119: (strcmp (nsuffix, "xpm")) && (strcmp (nsuffix, "jpg")) &&
120: (strcmp (nsuffix, "pdf")) && (strcmp (nsuffix, "png")) &&
121: (strcmp (nsuffix, "Z")) && (strcmp (nsuffix, "gz")) &&
122: (strcmp (nsuffix, "tgz")) && (strcmp (nsuffix, "xpg")) &&
123: (strcmp (nsuffix, "xpd")) && (strcmp (nsuffix, "ps")) &&
124: (strcmp (nsuffix, "au")))
125: return TRUE;
126: return FALSE;
1.3 cvs 127: }
128:
1.4 cvs 129: /*----------------------------------------------------------------------
130: IsHTTPPath
131: ----------------------------------------------------------------------*/
1.3 cvs 132:
133: #ifdef __STDC__
134: boolean IsHTTPPath (char *path)
135: #else /* __STDC__ */
136: boolean IsHTTPPath (path)
137: char *path;
138:
139: #endif /* __STDC__ */
140: {
1.5 cvs 141: if (!path)
142: return FALSE;
1.3 cvs 143:
1.5 cvs 144: if (strncmp (path, "http:", 5) != 0)
145: return FALSE;
146: return TRUE;
1.3 cvs 147: }
148:
1.4 cvs 149: /*----------------------------------------------------------------------
150: IsWithParameters
151: ----------------------------------------------------------------------*/
1.3 cvs 152:
153: #ifdef __STDC__
154: boolean IsWithParameters (char *path)
155: #else /* __STDC__ */
156: boolean IsWithParameters (path)
157: char *path;
158:
159: #endif /* __STDC__ */
160: {
1.5 cvs 161: int i;
1.3 cvs 162:
1.5 cvs 163: if ((!path) || (path[0] == EOS))
164: return FALSE;
1.3 cvs 165:
1.5 cvs 166: i = strlen (path) - 1;
167: while (i > 0 && path[i--] != '?')
168: if (i < 0)
169: return FALSE;
1.3 cvs 170:
1.5 cvs 171: /* There is a parameter */
172: return TRUE;
1.3 cvs 173: }
174:
1.4 cvs 175: /*----------------------------------------------------------------------
176: IsW3Path
177: ----------------------------------------------------------------------*/
1.3 cvs 178:
179: #ifdef __STDC__
180: boolean IsW3Path (char *path)
181: #else /* __STDC__ */
182: boolean IsW3Path (path)
183: char *path;
184:
185: #endif /* __STDC__ */
186: {
1.5 cvs 187: if ((strncmp (path, "http:", 5)) && (strncmp (path, "ftp:", 4)) &&
188: (strncmp (path, "telnet:", 7)) && (strncmp (path, "wais:", 5)) &&
189: (strncmp (path, "news:", 5)) && (strncmp (path, "gopher:", 7)) &&
190: (strncmp (path, "mailto:", 7)) && (strncmp (path, "archie:", 7)))
191: return FALSE;
192: return TRUE;
1.3 cvs 193: }
194:
1.4 cvs 195: /*----------------------------------------------------------------------
196: IsValidProtocol
197: ----------------------------------------------------------------------*/
1.3 cvs 198:
199: #ifdef __STDC__
200: boolean IsValidProtocol (char *path)
201: #else /* __STDC__ */
202: boolean IsValidProtocol (path)
203: char *path;
204:
205: #endif /* __STDC__ */
206: {
1.5 cvs 207: if (!strncmp (path, "http:", 5)
1.3 cvs 208: /***|| !strncmp (path, "ftp:", 4)
1.5 cvs 209: || !strncmp (path, "news:", 5)***/ )
210: return (YES);
211: else
212: return (NO);
1.3 cvs 213: }
214:
1.4 cvs 215: /*----------------------------------------------------------------------
216: IsValidNormalizeURL says which URL's may be normalized
217: ----------------------------------------------------------------------*/
1.3 cvs 218:
219: #ifdef __STDC__
220: boolean IsValidNormalizeURL (char *path)
221: #else /* __STDC__ */
222: boolean IsValidNormalizeURL (path)
223: char *path;
224:
225: #endif /* __STDC__ */
226: {
1.5 cvs 227: if (strchr (path, ':') && !strncmp (path, "http:", 5))
228: return (YES);
229: else
230: return (NO);
1.3 cvs 231: }
232:
233:
1.4 cvs 234: /*----------------------------------------------------------------------
235: NormalizeURL provides the new complete and normalized URL or file
236: name path and the name of the document.
237: orgName is the original requested name.
238: doc identifies the document which provides the original
239: name.
240: newName is the resulting URL of file name.
241: docName is the resulting document name.
242: ----------------------------------------------------------------------*/
1.3 cvs 243:
244: #ifdef __STDC__
245: void NormalizeURL (char *orgName, Document doc, char *newName, char *docName)
246: #else /* __STDC__ */
247: void NormalizeURL (orgName, doc, newName, docName)
248: char *orgName;
249: Document doc;
250: char *newName;
251: char *docName;
252:
253: #endif /* __STDC__ */
254: {
1.5 cvs 255: char basename[MAX_LENGTH];
256: char tempname[MAX_LENGTH];
257: int i;
258: char *ptr;
259: char *basename_ptr;
260: int basename_flag;
261: Element el;
262: ElementType elType;
263: AttributeType attrType;
264: Attribute attrHREF;
265: int length;
266:
267: /* Fix up orgName, by erasing leading and trailing white space */
268: if (!newName || !docName)
269: return;
270: ptr = orgName;
271: while (*ptr == ' ' && *ptr++ != EOS) ;
272: strcpy (tempname, ptr);
273: ptr = strchr (tempname, ' ');
274: if (ptr)
275: *ptr = EOS;
276:
277: /*
278: ** the following block to take into account the BASE element.
279: ** This is not very optimized, as this procedure is repeated for
280: ** each element which is retrieved. A better way would be to
281: ** move this higher up in the function call hierarchy.
282: */
283: if (IsValidNormalizeURL (tempname) && doc)
284: {
285: length = MAX_LENGTH;
286: /* get the root element */
287: el = TtaGetMainRoot (doc);
288:
289: /* search the BASE element */
290: elType.ElSSchema = TtaGetDocumentSSchema (doc);
291: elType.ElTypeNum = HTML_EL_BASE;
292: el = TtaSearchTypedElement (elType, SearchInTree, el);
293: if (el)
294: {
295: /*
296: ** The document has a BASE element
297: ** Get the HREF attribute of the BASE Element
1.3 cvs 298: */
1.5 cvs 299: attrType.AttrSSchema = elType.ElSSchema;
300: attrType.AttrTypeNum = HTML_ATTR_HREF_;
301: attrHREF = TtaGetAttribute (el, attrType);
302: if (attrHREF)
303: {
304: /*
305: ** Use the base path of the document
306: ** To do: verify length of the buffer
307: ** length > TtaGetTextAttributeLength (attrHREF) + strlen (orgName)
308: */
309: TtaGiveTextAttributeValue (attrHREF, basename, &length);
310:
311: /*
312: ** base and orgName have to be separated by a DIR_SEP
313: */
314: if (basename[strlen (basename) - 1] != DIR_SEP && tempname[0] != DIR_SEP)
315: strcat (basename, DIR_STR);
316: }
317: }
318: else
319: basename[0] = EOS;
320: }
321: else
322: basename[0] = EOS;
323:
324: if (basename[0] == EOS)
325: {
326: /*
327: ** There is no BASE element in that document.
328: ** A temporary fix as TtaExtractName does not tolerate a name
329: ** ending in /. Here, we reinsert the slash, in order to
330: ** parse the name in the following two lines. A bit
331: ** redundant and has to be reviewed.
332: */
333: if (DocumentURLs[(int) doc])
334: {
335: basename_ptr = HTParse (DocumentURLs[(int) doc], "", PARSE_ALL);
336: basename_flag = TRUE;
337: }
338: else
339: {
340: basename_ptr = "";
341: basename_flag = FALSE;
342: }
343: }
344: else
345: {
346: basename_ptr = HTParse (basename, "", PARSE_ALL);
347: basename_flag = TRUE;
348: } /* if-else tempname */
349:
350: ptr = HTParse (tempname, basename_ptr, PARSE_ALL);
351: if (basename_flag)
352: HT_FREE (basename_ptr);
353: if (ptr)
354: {
355: ptr = HTSimplify (&ptr);
356: strcpy (newName, ptr);
357: HT_FREE (ptr);
358: }
359: else
360: newName[0] = EOS;
361:
362: i = strlen (newName) - 1;
363: if (i > 0)
364: {
365: /*
366: ** A temporary fix for an interfacing problem:
367: ** TtaExtractName does not tolerate url's finished on DIR_SEP
368: */
369: ptr = strrchr (newName, DIR_SEP);
370: if (ptr)
371: ptr++;
372: if (ptr && *ptr != EOS)
373: strcpy (docName, ptr);
374: else
375: /*
376: ** The docname was not comprised inside the URL, so let's
377: ** assign a "noname.html" name :)
378: */
379: strcpy (docName, "noname.html");
380:
381: /*
382: ** A temporary fix for an interfacing problem:
383: ** TtaExtractName does not tolerate url's finished on DIR_SEP
384: */
385: if (newName[i] == DIR_SEP)
386: newName[i] = EOS;
387: }
1.3 cvs 388: }
389:
1.4 cvs 390: /*----------------------------------------------------------------------
391: IsSameHost
392: ----------------------------------------------------------------------*/
1.3 cvs 393:
394: #ifdef __STDC__
395: boolean IsSameHost (char *url1, char *url2)
396: #else /* __STDC__ */
397: boolean IsSameHost (url1, url2)
398: char *path;
399:
400: #endif /* __STDC__ */
401: {
1.5 cvs 402: char *basename_ptr1, *basename_ptr2;
403: boolean result;
1.3 cvs 404:
1.5 cvs 405: basename_ptr1 = HTParse (url1, "", PARSE_ACCESS | PARSE_HOST | PARSE_PUNCTUATION);
406: basename_ptr2 = HTParse (url2, "", PARSE_ACCESS | PARSE_HOST | PARSE_PUNCTUATION);
1.3 cvs 407:
1.5 cvs 408: if (strcmp (basename_ptr1, basename_ptr2))
409: result = NO;
410: else
411: result = YES;
1.3 cvs 412:
1.5 cvs 413: HT_FREE (basename_ptr1);
414: HT_FREE (basename_ptr2);
1.3 cvs 415:
1.5 cvs 416: return (result);
1.3 cvs 417: }
418:
419:
1.4 cvs 420: /*----------------------------------------------------------------------
421: AHTMakeRelativeURL
422: ----------------------------------------------------------------------*/
1.3 cvs 423:
424: #ifdef __STDC__
1.5 cvs 425: char *AHTMakeRelativeName (char *url, char *base_url)
1.3 cvs 426: #else /* __STDC__ */
1.5 cvs 427: char *AHTMakeRelativeName (url, base_url)
428: char url;
429: char base_url;
430:
1.3 cvs 431: #endif /* __STDC__ */
432: {
1.5 cvs 433: char *base_ptr, *url_ptr;
434: char *result;
435:
436: /* verify if we are in the same host */
1.3 cvs 437:
1.5 cvs 438: base_ptr = HTParse (base_url, "", PARSE_ACCESS | PARSE_HOST | PARSE_PUNCTUATION);
439: url_ptr = HTParse (url, "", PARSE_ACCESS | PARSE_HOST | PARSE_PUNCTUATION);
1.3 cvs 440:
1.5 cvs 441: if (!strcmp (base_ptr, url_ptr))
442: {
443: HT_FREE (base_ptr);
444: HT_FREE (url_ptr);
1.3 cvs 445:
1.5 cvs 446: /* Normalize the URLs */
1.3 cvs 447:
1.5 cvs 448: base_ptr = HTParse (base_url, "", PARSE_ALL);
449: url_ptr = HTParse (url, "", PARSE_ALL);
1.3 cvs 450:
1.5 cvs 451: /* Use libwww to make relative name */
1.3 cvs 452:
1.5 cvs 453: result = HTRelative (url_ptr, base_ptr);
454: HT_FREE (base_ptr);
455: HT_FREE (url_ptr);
456: }
457: else
458: result = (char *) NULL;
1.3 cvs 459:
1.5 cvs 460: return (result);
1.3 cvs 461: }
Webmaster