]> sjero.net Git - wget/blob - src/html-url.c
[svn] Handle <base href=...> when converting links.
[wget] / src / html-url.c
1 /* Collect URLs from HTML source.
2    Copyright (C) 1998, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU Wget.
5
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #ifdef HAVE_STRING_H
24 # include <string.h>
25 #else
26 # include <strings.h>
27 #endif
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <assert.h>
31
32 #include "wget.h"
33 #include "html-parse.h"
34 #include "url.h"
35 #include "utils.h"
36
37 #ifndef errno
38 extern int errno;
39 #endif
40
41 enum tag_category { TC_LINK, TC_SPEC };
42
43 /* Here we try to categorize the known tags.  Each tag has its ID and
44    cetegory.  Category TC_LINK means that one or more of its
45    attributes contain links that should be retrieved.  TC_SPEC means
46    that the tag is specific in some way, and has to be handled
47    specially. */
48 static struct {
49   const char *name;
50   enum tag_category category;
51 } known_tags[] = {
52 #define TAG_A           0
53   { "a",        TC_LINK },
54 #define TAG_APPLET      1
55   { "applet",   TC_LINK },
56 #define TAG_AREA        2
57   { "area",     TC_LINK },
58 #define TAG_BASE        3
59   { "base",     TC_SPEC },
60 #define TAG_BGSOUND     4
61   { "bgsound",  TC_LINK },
62 #define TAG_BODY        5
63   { "body",     TC_LINK },
64 #define TAG_EMBED       6
65   { "embed",    TC_LINK },
66 #define TAG_FIG         7
67   { "fig",      TC_LINK },
68 #define TAG_FRAME       8
69   { "frame",    TC_LINK },
70 #define TAG_IFRAME      9
71   { "iframe",   TC_LINK },
72 #define TAG_IMG         10
73   { "img",      TC_LINK },
74 #define TAG_INPUT       11
75   { "input",    TC_LINK },
76 #define TAG_LAYER       12
77   { "layer",    TC_LINK },
78 #define TAG_LINK        13
79   { "link",     TC_SPEC },
80 #define TAG_META        14
81   { "meta",     TC_SPEC },
82 #define TAG_OVERLAY     15
83   { "overlay",  TC_LINK },
84 #define TAG_SCRIPT      16
85   { "script",   TC_LINK },
86 #define TAG_TABLE       17
87   { "table",    TC_LINK },
88 #define TAG_TD          18
89   { "td",       TC_LINK },
90 #define TAG_TH          19
91   { "th",       TC_LINK }
92 };
93
94
95 /* Flags for specific url-attr pairs handled through TC_LINK: */
96
97 /* This tag points to an external document not necessary for rendering this 
98    document (i.e. it's not an inlined image, stylesheet, etc.). */
99 #define AF_EXTERNAL 1
100
101
102 /* For tags handled by TC_LINK: attributes that contain URLs to
103    download. */
104 static struct {
105   int tagid;
106   const char *attr_name;
107   int flags;
108 } url_tag_attr_map[] = {
109   { TAG_A,              "href",         AF_EXTERNAL },
110   { TAG_APPLET,         "code",         0 },
111   { TAG_AREA,           "href",         AF_EXTERNAL },
112   { TAG_BGSOUND,        "src",          0 },
113   { TAG_BODY,           "background",   0 },
114   { TAG_EMBED,          "src",          0 },
115   { TAG_FIG,            "src",          0 },
116   { TAG_FRAME,          "src",          0 },
117   { TAG_IFRAME,         "src",          0 },
118   { TAG_IMG,            "href",         0 },
119   { TAG_IMG,            "lowsrc",       0 },
120   { TAG_IMG,            "src",          0 },
121   { TAG_INPUT,          "src",          0 },
122   { TAG_LAYER,          "src",          0 },
123   { TAG_OVERLAY,        "src",          0 },
124   { TAG_SCRIPT,         "src",          0 },
125   { TAG_TABLE,          "background",   0 },
126   { TAG_TD,             "background",   0 },
127   { TAG_TH,             "background",   0 }
128 };
129
130 /* The lists of interesting tags and attributes are built dynamically,
131    from the information above.  However, some places in the code refer
132    to the attributes not mentioned here.  We add them manually.  */
133 static const char *additional_attributes[] = {
134   "rel",                        /* for TAG_LINK */
135   "http-equiv",                 /* for TAG_META */
136   "name",                       /* for TAG_META */
137   "content"                     /* for TAG_META */
138 };
139
140 static const char **interesting_tags;
141 static const char **interesting_attributes;
142
143 void
144 init_interesting (void)
145 {
146   /* Init the variables interesting_tags and interesting_attributes
147      that are used by the HTML parser to know which tags and
148      attributes we're interested in.  We initialize this only once,
149      for performance reasons.
150
151      Here we also make sure that what we put in interesting_tags
152      matches the user's preferences as specified through --ignore-tags
153      and --follow-tags.  */
154
155   {
156     int i, ind = 0;
157     int size = ARRAY_SIZE (known_tags);
158     interesting_tags = (const char **)xmalloc ((size + 1) * sizeof (char *));
159
160     for (i = 0; i < size; i++)
161       {
162         const char *name = known_tags[i].name;
163
164         /* Normally here we could say:
165            interesting_tags[i] = name;
166            But we need to respect the settings of --ignore-tags and
167            --follow-tags, so the code gets a bit hairier.  */
168
169         if (opt.ignore_tags)
170           {
171             /* --ignore-tags was specified.  Do not match these
172                specific tags.  --ignore-tags takes precedence over
173                --follow-tags, so we process --ignore first and fall
174                through if there's no match. */
175             int j, lose = 0;
176             for (j = 0; opt.ignore_tags[j] != NULL; j++)
177               /* Loop through all the tags this user doesn't care about. */
178               if (strcasecmp(opt.ignore_tags[j], name) == EQ)
179                 {
180                   lose = 1;
181                   break;
182                 }
183             if (lose)
184               continue;
185           }
186
187         if (opt.follow_tags)
188           {
189             /* --follow-tags was specified.  Only match these specific tags, so
190                continue back to top of for if we don't match one of them. */
191             int j, win = 0;
192             for (j = 0; opt.follow_tags[j] != NULL; j++)
193               /* Loop through all the tags this user cares about. */
194               if (strcasecmp(opt.follow_tags[j], name) == EQ)
195                 {
196                   win = 1;
197                   break;
198                 }
199             if (!win)
200               continue;  /* wasn't one of the explicitly desired tags */
201           }
202
203         /* If we get to here, --follow-tags isn't being used or the
204            tag is among the ones that are followed, and --ignore-tags,
205            if specified, didn't include this tag, so it's an
206            "interesting" one. */
207         interesting_tags[ind++] = name;
208       }
209     interesting_tags[ind] = NULL;
210   }
211
212   /* The same for attributes, except we loop through url_tag_attr_map.
213      Here we also need to make sure that the list of attributes is
214      unique, and to include the attributes from additional_attributes.  */
215   {
216     int i, ind;
217     const char **att = xmalloc ((ARRAY_SIZE (additional_attributes) + 1)
218                                 * sizeof (char *));
219     /* First copy the "additional" attributes. */
220     for (i = 0; i < ARRAY_SIZE (additional_attributes); i++)
221       att[i] = additional_attributes[i];
222     ind = i;
223     att[ind] = NULL;
224     for (i = 0; i < ARRAY_SIZE (url_tag_attr_map); i++)
225       {
226         int j, seen = 0;
227         const char *look_for = url_tag_attr_map[i].attr_name;
228         for (j = 0; j < ind - 1; j++)
229           if (!strcmp (att[j], look_for))
230             {
231               seen = 1;
232               break;
233             }
234         if (!seen)
235           {
236             att = xrealloc (att, (ind + 2) * sizeof (*att));
237             att[ind++] = look_for;
238             att[ind] = NULL;
239           }
240       }
241     interesting_attributes = att;
242   }
243 }
244
245 static int
246 find_tag (const char *tag_name)
247 {
248   int i;
249
250   /* This is linear search; if the number of tags grow, we can switch
251      to binary search.  */
252
253   for (i = 0; i < ARRAY_SIZE (known_tags); i++)
254     {
255       int cmp = strcasecmp (known_tags[i].name, tag_name);
256       /* known_tags are sorted alphabetically, so we can
257          micro-optimize.  */
258       if (cmp > 0)
259         break;
260       else if (cmp == 0)
261         return i;
262     }
263   return -1;
264 }
265
266 /* Find the value of attribute named NAME in the taginfo TAG.  If the
267    attribute is not present, return NULL.  If ATTRID is non-NULL, the
268    exact identity of the attribute will be returned.  */
269 static char *
270 find_attr (struct taginfo *tag, const char *name, int *attrid)
271 {
272   int i;
273   for (i = 0; i < tag->nattrs; i++)
274     if (!strcasecmp (tag->attrs[i].name, name))
275       {
276         if (attrid)
277           *attrid = i;
278         return tag->attrs[i].value;
279       }
280   return NULL;
281 }
282
283 struct collect_urls_closure {
284   char *text;                   /* HTML text. */
285   char *base;                   /* Base URI of the document, possibly
286                                    changed through <base href=...>. */
287   struct urlpos *head, *tail;   /* List of URLs */
288   const char *parent_base;      /* Base of the current document. */
289   const char *document_file;    /* File name of this document. */
290   int dash_p_leaf_HTML;         /* Whether -p is specified, and this
291                                    document is the "leaf" node of the
292                                    HTML tree. */
293   int nofollow;                 /* whether NOFOLLOW was specified in a
294                                    <meta name=robots> tag. */
295 };
296
297 /* Resolve LINK_URI and append it to closure->tail.  TAG and ATTRID
298    are the necessary context to store the position and size.  */
299
300 static struct urlpos *
301 handle_link (struct collect_urls_closure *closure, const char *link_uri,
302              struct taginfo *tag, int attrid)
303 {
304   int link_has_scheme = url_has_scheme (link_uri);
305   struct urlpos *newel;
306   const char *base = closure->base ? closure->base : closure->parent_base;
307   struct url *url;
308
309   if (!base)
310     {
311       DEBUGP (("%s: no base, merge will use \"%s\".\n",
312                closure->document_file, link_uri));
313
314       if (!link_has_scheme)
315         {
316           /* We have no base, and the link does not have a host
317              attached to it.  Nothing we can do.  */
318           /* #### Should we print a warning here?  Wget 1.5.x used to.  */
319           return NULL;
320         }
321
322       url = url_parse (link_uri, NULL);
323       if (!url)
324         {
325           DEBUGP (("%s: link \"%s\" doesn't parse.\n",
326                    closure->document_file, link_uri));
327           return NULL;
328         }
329     }
330   else
331     {
332       /* Merge BASE with LINK_URI, but also make sure the result is
333          canonicalized, i.e. that "../" have been resolved.
334          (parse_url will do that for us.) */
335
336       char *complete_uri = uri_merge (base, link_uri);
337
338       DEBUGP (("%s: merge(\"%s\", \"%s\") -> %s\n",
339                closure->document_file, base, link_uri, complete_uri));
340
341       url = url_parse (complete_uri, NULL);
342       if (!url)
343         {
344           DEBUGP (("%s: merged link \"%s\" doesn't parse.\n",
345                    closure->document_file, complete_uri));
346           xfree (complete_uri);
347           return NULL;
348         }
349       xfree (complete_uri);
350     }
351
352   newel = (struct urlpos *)xmalloc (sizeof (struct urlpos));
353
354   memset (newel, 0, sizeof (*newel));
355   newel->next = NULL;
356   newel->url = url;
357   newel->pos = tag->attrs[attrid].value_raw_beginning - closure->text;
358   newel->size = tag->attrs[attrid].value_raw_size;
359
360   /* A URL is relative if the host is not named, and the name does not
361      start with `/'.  */
362   if (!link_has_scheme && *link_uri != '/')
363     newel->link_relative_p = 1;
364   else if (link_has_scheme)
365     newel->link_complete_p = 1;
366
367   if (closure->tail)
368     {
369       closure->tail->next = newel;
370       closure->tail = newel;
371     }
372   else
373     closure->tail = closure->head = newel;
374
375   return newel;
376 }
377
378 /* Examine name and attributes of TAG and take appropriate action.
379    What will be done depends on TAG's category and attribute values.
380    Tags of TC_LINK category have attributes that contain links to
381    follow; tags of TC_SPEC category need to be handled specially.
382
383    #### It would be nice to split this into several functions.  */
384
385 static void
386 collect_tags_mapper (struct taginfo *tag, void *arg)
387 {
388   struct collect_urls_closure *closure = (struct collect_urls_closure *)arg;
389   int tagid = find_tag (tag->name);
390   assert (tagid != -1);
391
392   switch (known_tags[tagid].category)
393     {
394     case TC_LINK:
395       {
396         int i, id, first;
397         int size = ARRAY_SIZE (url_tag_attr_map);
398         for (i = 0; i < size; i++)
399           if (url_tag_attr_map[i].tagid == tagid)
400             break;
401         /* We've found the index of url_tag_attr_map where the
402            attributes of our tags begin.  Now, look for every one of
403            them, and handle it.  */
404         /* Need to process the attributes in the order they appear in
405            the tag, as this is required if we convert links.  */
406         first = i;
407         for (id = 0; id < tag->nattrs; id++)
408           {
409             /* This nested loop may seem inefficient (O(n^2)), but it's
410                not, since the number of attributes (n) we loop over is
411                extremely small.  In the worst case of IMG with all its
412                possible attributes, n^2 will be only 9.  */
413             for (i = first; (i < size && url_tag_attr_map[i].tagid == tagid);
414                  i++)
415               {
416                 char *attr_value;
417                 if (closure->dash_p_leaf_HTML
418                     && (url_tag_attr_map[i].flags & AF_EXTERNAL))
419                   /* If we're at a -p leaf node, we don't want to retrieve
420                      links to references we know are external to this document,
421                      such as <a href=...>.  */
422                   continue;
423
424                 if (!strcasecmp (tag->attrs[id].name,
425                                  url_tag_attr_map[i].attr_name))
426                   {
427                     attr_value = tag->attrs[id].value;
428                     if (attr_value)
429                       handle_link (closure, attr_value, tag, id);
430                   }
431               }
432           }
433       }
434       break;
435     case TC_SPEC:
436       switch (tagid)
437         {
438         case TAG_BASE:
439           {
440             struct urlpos *base_urlpos;
441             int id;
442             char *newbase = find_attr (tag, "href", &id);
443             if (!newbase)
444               break;
445
446             base_urlpos = handle_link (closure, newbase, tag, id);
447             if (!base_urlpos)
448               break;
449             base_urlpos->ignore_when_downloading = 1;
450             base_urlpos->link_base_p = 1;
451
452             if (closure->base)
453               xfree (closure->base);
454             if (closure->parent_base)
455               closure->base = uri_merge (closure->parent_base, newbase);
456             else
457               closure->base = xstrdup (newbase);
458           }
459           break;
460         case TAG_LINK:
461           {
462             int id;
463             char *rel  = find_attr (tag, "rel", NULL);
464             char *href = find_attr (tag, "href", &id);
465             if (href)
466               {
467                 /* In the normal case, all <link href=...> tags are
468                    fair game.
469
470                    In the special case of when -p is active, however,
471                    and we're at a leaf node (relative to the -l
472                    max. depth) in the HTML document tree, the only
473                    <LINK> tag we'll follow is a <LINK REL=
474                    "stylesheet">, as it'll be necessary for displaying
475                    this document properly.  We won't follow other
476                    <LINK> tags, like <LINK REL="home">, for instance,
477                    as they refer to external documents.  */
478                 if (!closure->dash_p_leaf_HTML
479                     || (rel && !strcasecmp (rel, "stylesheet")))
480                   handle_link (closure, href, tag, id);
481               }
482           }
483           break;
484         case TAG_META:
485           /* Some pages use a META tag to specify that the page be
486              refreshed by a new page after a given number of seconds.
487              The general format for this is:
488
489              <meta http-equiv=Refresh content="NUMBER; URL=index2.html">
490
491              So we just need to skip past the "NUMBER; URL=" garbage
492              to get to the URL.  */
493           {
494             int id;
495             char *name = find_attr (tag, "name", NULL);
496             char *http_equiv = find_attr (tag, "http-equiv", &id);
497             if (http_equiv && !strcasecmp (http_equiv, "refresh"))
498               {
499                 char *refresh = find_attr (tag, "content", NULL);
500                 char *p = refresh;
501                 int offset;
502                 while (ISDIGIT (*p))
503                   ++p;
504                 if (*p++ != ';')
505                   return;
506                 while (ISSPACE (*p))
507                   ++p;
508                 if (!(TOUPPER (*p) == 'U'
509                       && TOUPPER (*(p + 1)) == 'R'
510                       && TOUPPER (*(p + 2)) == 'L'
511                       && *(p + 3) == '='))
512                   return;
513                 p += 4;
514                 while (ISSPACE (*p))
515                   ++p;
516                 offset = p - refresh;
517                 tag->attrs[id].value_raw_beginning += offset;
518                 tag->attrs[id].value_raw_size -= offset;
519                 handle_link (closure, p, tag, id);
520               }
521             else if (name && !strcasecmp (name, "robots"))
522               {
523                 /* Handle stuff like:
524                    <meta name="robots" content="index,nofollow"> */
525                 char *content = find_attr (tag, "content", NULL);
526                 if (!content)
527                   return;
528                 if (!strcasecmp (content, "none"))
529                   closure->nofollow = 1;
530                 else
531                   {
532                     while (*content)
533                       {
534                         /* Find the next occurrence of ',' or the end of
535                            the string.  */
536                         char *end = strchr (content, ',');
537                         if (end)
538                           ++end;
539                         else
540                           end = content + strlen (content);
541                         if (!strncasecmp (content, "nofollow", end - content))
542                           closure->nofollow = 1;
543                         content = end;
544                       }
545                   }
546               }
547           }
548           break;
549         default:
550           /* Category is TC_SPEC, but tag name is unhandled.  This
551              must not be.  */
552           abort ();
553         }
554       break;
555     }
556 }
557
558 /* Analyze HTML tags FILE and construct a list of URLs referenced from
559    it.  It merges relative links in FILE with URL.  It is aware of
560    <base href=...> and does the right thing.
561
562    If dash_p_leaf_HTML is non-zero, only the elements needed to render
563    FILE ("non-external" links) will be returned.  */
564 struct urlpos *
565 get_urls_html (const char *file, const char *url, int dash_p_leaf_HTML,
566                int *meta_disallow_follow)
567 {
568   struct file_memory *fm;
569   struct collect_urls_closure closure;
570
571   /* Load the file. */
572   fm = read_file (file);
573   if (!fm)
574     {
575       logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
576       return NULL;
577     }
578   DEBUGP (("Loaded %s (size %ld).\n", file, fm->length));
579
580   closure.text = fm->content;
581   closure.head = closure.tail = NULL;
582   closure.base = NULL;
583   closure.parent_base = url ? url : opt.base_href;
584   closure.document_file = file;
585   closure.dash_p_leaf_HTML = dash_p_leaf_HTML;
586   closure.nofollow = 0;
587
588   if (!interesting_tags)
589     init_interesting ();
590
591   map_html_tags (fm->content, fm->length, interesting_tags,
592                  interesting_attributes, collect_tags_mapper, &closure);
593
594   DEBUGP (("no-follow in %s: %d\n", file, closure.nofollow));
595   if (meta_disallow_follow)
596     *meta_disallow_follow = closure.nofollow;
597
598   FREE_MAYBE (closure.base);
599   read_file_free (fm);
600   return closure.head;
601 }
602
603 void
604 cleanup_html_url (void)
605 {
606   FREE_MAYBE (interesting_tags);
607   FREE_MAYBE (interesting_attributes);
608 }