1 /* Collect URLs from HTML source.
2 Copyright (C) 1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
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.
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.
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.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
43 #include "html-parse.h"
55 typedef void (*tag_handler_t) PARAMS ((int, struct taginfo *,
56 struct map_context *));
58 #define DECLARE_TAG_HANDLER(fun) \
59 static void fun PARAMS ((int, struct taginfo *, struct map_context *))
61 DECLARE_TAG_HANDLER (tag_find_urls);
62 DECLARE_TAG_HANDLER (tag_handle_base);
63 DECLARE_TAG_HANDLER (tag_handle_form);
64 DECLARE_TAG_HANDLER (tag_handle_link);
65 DECLARE_TAG_HANDLER (tag_handle_meta);
91 /* The list of known tags and functions used for handling them. Most
92 tags are simply harvested for URLs. */
93 static struct known_tag {
96 tag_handler_t handler;
98 { TAG_A, "a", tag_find_urls },
99 { TAG_APPLET, "applet", tag_find_urls },
100 { TAG_AREA, "area", tag_find_urls },
101 { TAG_BASE, "base", tag_handle_base },
102 { TAG_BGSOUND, "bgsound", tag_find_urls },
103 { TAG_BODY, "body", tag_find_urls },
104 { TAG_EMBED, "embed", tag_find_urls },
105 { TAG_FIG, "fig", tag_find_urls },
106 { TAG_FORM, "form", tag_handle_form },
107 { TAG_FRAME, "frame", tag_find_urls },
108 { TAG_IFRAME, "iframe", tag_find_urls },
109 { TAG_IMG, "img", tag_find_urls },
110 { TAG_INPUT, "input", tag_find_urls },
111 { TAG_LAYER, "layer", tag_find_urls },
112 { TAG_LINK, "link", tag_handle_link },
113 { TAG_META, "meta", tag_handle_meta },
114 { TAG_OVERLAY, "overlay", tag_find_urls },
115 { TAG_SCRIPT, "script", tag_find_urls },
116 { TAG_TABLE, "table", tag_find_urls },
117 { TAG_TD, "td", tag_find_urls },
118 { TAG_TH, "th", tag_find_urls }
121 /* tag_url_attributes documents which attributes of which tags contain
122 URLs to harvest. It is used by tag_find_urls. */
124 /* Defines for the FLAGS. */
126 /* The link is "inline", i.e. needs to be retrieved for this document
127 to be correctly rendered. Inline links include inlined images,
128 stylesheets, children frames, etc. */
129 #define ATTR_INLINE 1
131 /* The link is expected to yield HTML contents. It's important not to
132 try to follow HTML obtained by following e.g. <img src="...">
133 regardless of content-type. Doing this causes infinite loops for
134 "images" that return non-404 error pages with links to the same
138 /* For tags handled by tag_find_urls: attributes that contain URLs to
142 const char *attr_name;
144 } tag_url_attributes[] = {
145 { TAG_A, "href", ATTR_HTML },
146 { TAG_APPLET, "code", ATTR_INLINE },
147 { TAG_AREA, "href", ATTR_HTML },
148 { TAG_BGSOUND, "src", ATTR_INLINE },
149 { TAG_BODY, "background", ATTR_INLINE },
150 { TAG_EMBED, "href", ATTR_HTML },
151 { TAG_EMBED, "src", ATTR_INLINE | ATTR_HTML },
152 { TAG_FIG, "src", ATTR_INLINE },
153 { TAG_FRAME, "src", ATTR_INLINE | ATTR_HTML },
154 { TAG_IFRAME, "src", ATTR_INLINE | ATTR_HTML },
155 { TAG_IMG, "href", ATTR_INLINE },
156 { TAG_IMG, "lowsrc", ATTR_INLINE },
157 { TAG_IMG, "src", ATTR_INLINE },
158 { TAG_INPUT, "src", ATTR_INLINE },
159 { TAG_LAYER, "src", ATTR_INLINE | ATTR_HTML },
160 { TAG_OVERLAY, "src", ATTR_INLINE | ATTR_HTML },
161 { TAG_SCRIPT, "src", ATTR_INLINE },
162 { TAG_TABLE, "background", ATTR_INLINE },
163 { TAG_TD, "background", ATTR_INLINE },
164 { TAG_TH, "background", ATTR_INLINE }
167 /* The lists of interesting tags and attributes are built dynamically,
168 from the information above. However, some places in the code refer
169 to the attributes not mentioned here. We add them manually. */
170 static const char *additional_attributes[] = {
171 "rel", /* used by tag_handle_link */
172 "http-equiv", /* used by tag_handle_meta */
173 "name", /* used by tag_handle_meta */
174 "content", /* used by tag_handle_meta */
175 "action" /* used by tag_handle_form */
178 struct hash_table *interesting_tags;
179 struct hash_table *interesting_attributes;
182 init_interesting (void)
184 /* Init the variables interesting_tags and interesting_attributes
185 that are used by the HTML parser to know which tags and
186 attributes we're interested in. We initialize this only once,
187 for performance reasons.
189 Here we also make sure that what we put in interesting_tags
190 matches the user's preferences as specified through --ignore-tags
191 and --follow-tags. */
194 interesting_tags = make_nocase_string_hash_table (countof (known_tags));
196 /* First, add all the tags we know hot to handle, mapped to their
197 respective entries in known_tags. */
198 for (i = 0; i < countof (known_tags); i++)
199 hash_table_put (interesting_tags, known_tags[i].name, known_tags + i);
201 /* Then remove the tags ignored through --ignore-tags. */
205 for (ignored = opt.ignore_tags; *ignored; ignored++)
206 hash_table_remove (interesting_tags, *ignored);
209 /* If --follow-tags is specified, use only those tags. */
212 /* Create a new table intersecting --follow-tags and known_tags,
213 and use it as interesting_tags. */
214 struct hash_table *intersect = make_nocase_string_hash_table (0);
216 for (followed = opt.follow_tags; *followed; followed++)
218 struct known_tag *t = hash_table_get (interesting_tags, *followed);
220 continue; /* ignore unknown --follow-tags entries. */
221 hash_table_put (intersect, *followed, t);
223 hash_table_destroy (interesting_tags);
224 interesting_tags = intersect;
227 /* Add the attributes we care about. */
228 interesting_attributes = make_nocase_string_hash_table (10);
229 for (i = 0; i < countof (additional_attributes); i++)
230 string_set_add (interesting_attributes, additional_attributes[i]);
231 for (i = 0; i < countof (tag_url_attributes); i++)
232 string_set_add (interesting_attributes, tag_url_attributes[i].attr_name);
235 /* Find the value of attribute named NAME in the taginfo TAG. If the
236 attribute is not present, return NULL. If ATTRIND is non-NULL, the
237 index of the attribute in TAG will be stored there. */
240 find_attr (struct taginfo *tag, const char *name, int *attrind)
243 for (i = 0; i < tag->nattrs; i++)
244 if (!strcasecmp (tag->attrs[i].name, name))
248 return tag->attrs[i].value;
254 char *text; /* HTML text. */
255 char *base; /* Base URI of the document, possibly
256 changed through <base href=...>. */
257 const char *parent_base; /* Base of the current document. */
258 const char *document_file; /* File name of this document. */
259 int nofollow; /* whether NOFOLLOW was specified in a
260 <meta name=robots> tag. */
262 struct urlpos *head, *tail; /* List of URLs that is being
266 /* Append LINK_URI to the urlpos structure that is being built.
268 LINK_URI will be merged with the current document base. TAG and
269 ATTRIND are the necessary context to store the position and
272 static struct urlpos *
273 append_one_url (const char *link_uri,
274 struct taginfo *tag, int attrind, struct map_context *ctx)
276 int link_has_scheme = url_has_scheme (link_uri);
277 struct urlpos *newel;
278 const char *base = ctx->base ? ctx->base : ctx->parent_base;
283 DEBUGP (("%s: no base, merge will use \"%s\".\n",
284 ctx->document_file, link_uri));
286 if (!link_has_scheme)
288 /* Base URL is unavailable, and the link does not have a
289 location attached to it -- we have to give up. Since
290 this can only happen when using `--force-html -i', print
292 logprintf (LOG_NOTQUIET,
293 _("%s: Cannot resolve incomplete link %s.\n"),
294 ctx->document_file, link_uri);
298 url = url_parse (link_uri, NULL);
301 DEBUGP (("%s: link \"%s\" doesn't parse.\n",
302 ctx->document_file, link_uri));
308 /* Merge BASE with LINK_URI, but also make sure the result is
309 canonicalized, i.e. that "../" have been resolved.
310 (parse_url will do that for us.) */
312 char *complete_uri = uri_merge (base, link_uri);
314 DEBUGP (("%s: merge(\"%s\", \"%s\") -> %s\n",
315 ctx->document_file, base, link_uri, complete_uri));
317 url = url_parse (complete_uri, NULL);
320 DEBUGP (("%s: merged link \"%s\" doesn't parse.\n",
321 ctx->document_file, complete_uri));
322 xfree (complete_uri);
325 xfree (complete_uri);
328 DEBUGP (("appending \"%s\" to urlpos.\n", url->url));
330 newel = (struct urlpos *)xmalloc (sizeof (struct urlpos));
331 memset (newel, 0, sizeof (*newel));
335 newel->pos = tag->attrs[attrind].value_raw_beginning - ctx->text;
336 newel->size = tag->attrs[attrind].value_raw_size;
338 /* A URL is relative if the host is not named, and the name does not
340 if (!link_has_scheme && *link_uri != '/')
341 newel->link_relative_p = 1;
342 else if (link_has_scheme)
343 newel->link_complete_p = 1;
347 ctx->tail->next = newel;
351 ctx->tail = ctx->head = newel;
356 /* All the tag_* functions are called from collect_tags_mapper, as
357 specified by KNOWN_TAGS. */
359 /* Default tag handler: collect URLs from attributes specified for
360 this tag by tag_url_attributes. */
363 tag_find_urls (int tagid, struct taginfo *tag, struct map_context *ctx)
368 for (i = 0; i < countof (tag_url_attributes); i++)
369 if (tag_url_attributes[i].tagid == tagid)
371 /* We've found the index of tag_url_attributes where the
372 attributes of our tag begin. */
376 assert (first != -1);
378 /* Loop over the "interesting" attributes of this tag. In this
379 example, it will loop over "src" and "lowsrc".
381 <img src="foo.png" lowsrc="bar.png">
383 This has to be done in the outer loop so that the attributes are
384 processed in the same order in which they appear in the page.
385 This is required when converting links. */
387 for (attrind = 0; attrind < tag->nattrs; attrind++)
389 /* Find whether TAG/ATTRIND is a combination that contains a
391 char *link = tag->attrs[attrind].value;
392 const int size = countof (tag_url_attributes);
394 /* If you're cringing at the inefficiency of the nested loops,
395 remember that they both iterate over a very small number of
396 items. The worst-case inner loop is for the IMG tag, which
397 has three attributes. */
398 for (i = first; i < size && tag_url_attributes[i].tagid == tagid; i++)
400 if (0 == strcasecmp (tag->attrs[attrind].name,
401 tag_url_attributes[i].attr_name))
403 struct urlpos *up = append_one_url (link, tag, attrind, ctx);
406 int flags = tag_url_attributes[i].flags;
407 if (flags & ATTR_INLINE)
408 up->link_inline_p = 1;
409 if (flags & ATTR_HTML)
410 up->link_expect_html = 1;
417 /* Handle the BASE tag, for <base href=...>. */
420 tag_handle_base (int tagid, struct taginfo *tag, struct map_context *ctx)
422 struct urlpos *base_urlpos;
424 char *newbase = find_attr (tag, "href", &attrind);
428 base_urlpos = append_one_url (newbase, tag, attrind, ctx);
431 base_urlpos->ignore_when_downloading = 1;
432 base_urlpos->link_base_p = 1;
436 if (ctx->parent_base)
437 ctx->base = uri_merge (ctx->parent_base, newbase);
439 ctx->base = xstrdup (newbase);
442 /* Mark the URL found in <form action=...> for conversion. */
445 tag_handle_form (int tagid, struct taginfo *tag, struct map_context *ctx)
448 char *action = find_attr (tag, "action", &attrind);
451 struct urlpos *up = append_one_url (action, tag, attrind, ctx);
453 up->ignore_when_downloading = 1;
457 /* Handle the LINK tag. It requires special handling because how its
458 links will be followed in -p mode depends on the REL attribute. */
461 tag_handle_link (int tagid, struct taginfo *tag, struct map_context *ctx)
464 char *href = find_attr (tag, "href", &attrind);
466 /* All <link href="..."> link references are external, except those
467 known not to be, such as style sheet and shortcut icon:
469 <link rel="stylesheet" href="...">
470 <link rel="shortcut icon" href="...">
474 struct urlpos *up = append_one_url (href, tag, attrind, ctx);
477 char *rel = find_attr (tag, "rel", NULL);
479 && (0 == strcasecmp (rel, "stylesheet")
480 || 0 == strcasecmp (rel, "shortcut icon")))
481 up->link_inline_p = 1;
486 /* Handle the META tag. This requires special handling because of the
487 refresh feature and because of robot exclusion. */
490 tag_handle_meta (int tagid, struct taginfo *tag, struct map_context *ctx)
492 char *name = find_attr (tag, "name", NULL);
493 char *http_equiv = find_attr (tag, "http-equiv", NULL);
495 if (http_equiv && 0 == strcasecmp (http_equiv, "refresh"))
497 /* Some pages use a META tag to specify that the page be
498 refreshed by a new page after a given number of seconds. The
499 general format for this is:
501 <meta http-equiv=Refresh content="NUMBER; URL=index2.html">
503 So we just need to skip past the "NUMBER; URL=" garbage to
506 struct urlpos *entry;
511 char *refresh = find_attr (tag, "content", &attrind);
515 for (p = refresh; ISDIGIT (*p); p++)
516 timeout = 10 * timeout + *p - '0';
522 if (!( TOUPPER (*p) == 'U'
523 && TOUPPER (*(p + 1)) == 'R'
524 && TOUPPER (*(p + 2)) == 'L'
531 entry = append_one_url (p, tag, attrind, ctx);
534 entry->link_refresh_p = 1;
535 entry->refresh_timeout = timeout;
538 else if (name && 0 == strcasecmp (name, "robots"))
540 /* Handle stuff like:
541 <meta name="robots" content="index,nofollow"> */
542 char *content = find_attr (tag, "content", NULL);
545 if (!strcasecmp (content, "none"))
551 /* Find the next occurrence of ',' or the end of
553 char *end = strchr (content, ',');
557 end = content + strlen (content);
558 if (!strncasecmp (content, "nofollow", end - content))
566 /* Dispatch the tag handler appropriate for the tag we're mapping
567 over. See known_tags[] for definition of tag handlers. */
570 collect_tags_mapper (struct taginfo *tag, void *arg)
572 struct map_context *ctx = (struct map_context *)arg;
574 /* Find the tag in our table of tags. This must not fail because
575 map_html_tags only returns tags found in interesting_tags. */
576 struct known_tag *t = hash_table_get (interesting_tags, tag->name);
579 t->handler (t->tagid, tag, ctx);
582 /* Analyze HTML tags FILE and construct a list of URLs referenced from
583 it. It merges relative links in FILE with URL. It is aware of
584 <base href=...> and does the right thing. */
587 get_urls_html (const char *file, const char *url, int *meta_disallow_follow)
589 struct file_memory *fm;
590 struct map_context ctx;
594 fm = read_file (file);
597 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
600 DEBUGP (("Loaded %s (size %ld).\n", file, fm->length));
602 ctx.text = fm->content;
603 ctx.head = ctx.tail = NULL;
605 ctx.parent_base = url ? url : opt.base_href;
606 ctx.document_file = file;
609 if (!interesting_tags)
612 /* Specify MHT_TRIM_VALUES because of buggy HTML generators that
613 generate <a href=" foo"> instead of <a href="foo"> (Netscape
614 ignores spaces as well.) If you really mean space, use &32; or
616 flags = MHT_TRIM_VALUES;
617 if (opt.strict_comments)
618 flags |= MHT_STRICT_COMMENTS;
620 map_html_tags (fm->content, fm->length, collect_tags_mapper, &ctx, flags,
621 interesting_tags, interesting_attributes);
623 DEBUGP (("no-follow in %s: %d\n", file, ctx.nofollow));
624 if (meta_disallow_follow)
625 *meta_disallow_follow = ctx.nofollow;
627 FREE_MAYBE (ctx.base);
632 /* This doesn't really have anything to do with HTML, but it's similar
633 to get_urls_html, so we put it here. */
636 get_urls_file (const char *file)
638 struct file_memory *fm;
639 struct urlpos *head, *tail;
640 const char *text, *text_end;
643 fm = read_file (file);
646 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
649 DEBUGP (("Loaded %s (size %ld).\n", file, fm->length));
653 text_end = fm->content + fm->length;
654 while (text < text_end)
658 struct urlpos *entry;
661 const char *line_beg = text;
662 const char *line_end = memchr (text, '\n', text_end - text);
669 /* Strip whitespace from the beginning and end of line. */
670 while (line_beg < line_end && ISSPACE (*line_beg))
672 while (line_end > line_beg && ISSPACE (*(line_end - 1)))
675 if (line_beg == line_end)
678 /* The URL is in the [line_beg, line_end) region. */
680 /* We must copy the URL to a zero-terminated string, and we
681 can't use alloca because we're in a loop. *sigh*. */
682 url_text = strdupdelim (line_beg, line_end);
686 /* Merge opt.base_href with URL. */
687 char *merged = uri_merge (opt.base_href, url_text);
692 url = url_parse (url_text, &up_error_code);
695 logprintf (LOG_NOTQUIET, "%s: Invalid URL %s: %s\n",
696 file, url_text, url_error (up_error_code));
702 entry = (struct urlpos *)xmalloc (sizeof (struct urlpos));
703 memset (entry, 0, sizeof (*entry));
718 cleanup_html_url (void)
720 FREE_MAYBE (interesting_tags);
721 FREE_MAYBE (interesting_attributes);