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"
48 #include "recur.h" /* declaration of get_urls_html */
56 typedef void (*tag_handler_t) PARAMS ((int, struct taginfo *,
57 struct map_context *));
59 #define DECLARE_TAG_HANDLER(fun) \
60 static void fun PARAMS ((int, struct taginfo *, struct map_context *))
62 DECLARE_TAG_HANDLER (tag_find_urls);
63 DECLARE_TAG_HANDLER (tag_handle_base);
64 DECLARE_TAG_HANDLER (tag_handle_form);
65 DECLARE_TAG_HANDLER (tag_handle_link);
66 DECLARE_TAG_HANDLER (tag_handle_meta);
93 /* The list of known tags and functions used for handling them. Most
94 tags are simply harvested for URLs. */
95 static struct known_tag {
98 tag_handler_t handler;
100 { TAG_A, "a", tag_find_urls },
101 { TAG_APPLET, "applet", tag_find_urls },
102 { TAG_AREA, "area", tag_find_urls },
103 { TAG_BASE, "base", tag_handle_base },
104 { TAG_BGSOUND, "bgsound", tag_find_urls },
105 { TAG_BODY, "body", tag_find_urls },
106 { TAG_EMBED, "embed", tag_find_urls },
107 { TAG_FIG, "fig", tag_find_urls },
108 { TAG_FORM, "form", tag_handle_form },
109 { TAG_FRAME, "frame", tag_find_urls },
110 { TAG_IFRAME, "iframe", tag_find_urls },
111 { TAG_IMG, "img", tag_find_urls },
112 { TAG_INPUT, "input", tag_find_urls },
113 { TAG_LAYER, "layer", tag_find_urls },
114 { TAG_LINK, "link", tag_handle_link },
115 { TAG_META, "meta", tag_handle_meta },
116 { TAG_OBJECT, "object", tag_find_urls },
117 { TAG_OVERLAY, "overlay", tag_find_urls },
118 { TAG_SCRIPT, "script", tag_find_urls },
119 { TAG_TABLE, "table", tag_find_urls },
120 { TAG_TD, "td", tag_find_urls },
121 { TAG_TH, "th", tag_find_urls }
124 /* tag_url_attributes documents which attributes of which tags contain
125 URLs to harvest. It is used by tag_find_urls. */
127 /* Defines for the FLAGS. */
129 /* The link is "inline", i.e. needs to be retrieved for this document
130 to be correctly rendered. Inline links include inlined images,
131 stylesheets, children frames, etc. */
132 #define ATTR_INLINE 1
134 /* The link is expected to yield HTML contents. It's important not to
135 try to follow HTML obtained by following e.g. <img src="...">
136 regardless of content-type. Doing this causes infinite loops for
137 "images" that return non-404 error pages with links to the same
141 /* For tags handled by tag_find_urls: attributes that contain URLs to
145 const char *attr_name;
147 } tag_url_attributes[] = {
148 { TAG_A, "href", ATTR_HTML },
149 { TAG_APPLET, "code", ATTR_INLINE },
150 { TAG_AREA, "href", ATTR_HTML },
151 { TAG_BGSOUND, "src", ATTR_INLINE },
152 { TAG_BODY, "background", ATTR_INLINE },
153 { TAG_EMBED, "href", ATTR_HTML },
154 { TAG_EMBED, "src", ATTR_INLINE | ATTR_HTML },
155 { TAG_FIG, "src", ATTR_INLINE },
156 { TAG_FRAME, "src", ATTR_INLINE | ATTR_HTML },
157 { TAG_IFRAME, "src", ATTR_INLINE | ATTR_HTML },
158 { TAG_IMG, "href", ATTR_INLINE },
159 { TAG_IMG, "lowsrc", ATTR_INLINE },
160 { TAG_IMG, "src", ATTR_INLINE },
161 { TAG_INPUT, "src", ATTR_INLINE },
162 { TAG_LAYER, "src", ATTR_INLINE | ATTR_HTML },
163 { TAG_OBJECT, "data", ATTR_INLINE },
164 { TAG_OVERLAY, "src", ATTR_INLINE | ATTR_HTML },
165 { TAG_SCRIPT, "src", ATTR_INLINE },
166 { TAG_TABLE, "background", ATTR_INLINE },
167 { TAG_TD, "background", ATTR_INLINE },
168 { TAG_TH, "background", ATTR_INLINE }
171 /* The lists of interesting tags and attributes are built dynamically,
172 from the information above. However, some places in the code refer
173 to the attributes not mentioned here. We add them manually. */
174 static const char *additional_attributes[] = {
175 "rel", /* used by tag_handle_link */
176 "http-equiv", /* used by tag_handle_meta */
177 "name", /* used by tag_handle_meta */
178 "content", /* used by tag_handle_meta */
179 "action" /* used by tag_handle_form */
182 struct hash_table *interesting_tags;
183 struct hash_table *interesting_attributes;
186 init_interesting (void)
188 /* Init the variables interesting_tags and interesting_attributes
189 that are used by the HTML parser to know which tags and
190 attributes we're interested in. We initialize this only once,
191 for performance reasons.
193 Here we also make sure that what we put in interesting_tags
194 matches the user's preferences as specified through --ignore-tags
195 and --follow-tags. */
198 interesting_tags = make_nocase_string_hash_table (countof (known_tags));
200 /* First, add all the tags we know hot to handle, mapped to their
201 respective entries in known_tags. */
202 for (i = 0; i < countof (known_tags); i++)
203 hash_table_put (interesting_tags, known_tags[i].name, known_tags + i);
205 /* Then remove the tags ignored through --ignore-tags. */
209 for (ignored = opt.ignore_tags; *ignored; ignored++)
210 hash_table_remove (interesting_tags, *ignored);
213 /* If --follow-tags is specified, use only those tags. */
216 /* Create a new table intersecting --follow-tags and known_tags,
217 and use it as interesting_tags. */
218 struct hash_table *intersect = make_nocase_string_hash_table (0);
220 for (followed = opt.follow_tags; *followed; followed++)
222 struct known_tag *t = hash_table_get (interesting_tags, *followed);
224 continue; /* ignore unknown --follow-tags entries. */
225 hash_table_put (intersect, *followed, t);
227 hash_table_destroy (interesting_tags);
228 interesting_tags = intersect;
231 /* Add the attributes we care about. */
232 interesting_attributes = make_nocase_string_hash_table (10);
233 for (i = 0; i < countof (additional_attributes); i++)
234 hash_table_put (interesting_attributes, additional_attributes[i], "1");
235 for (i = 0; i < countof (tag_url_attributes); i++)
236 hash_table_put (interesting_attributes,
237 tag_url_attributes[i].attr_name, "1");
240 /* Find the value of attribute named NAME in the taginfo TAG. If the
241 attribute is not present, return NULL. If ATTRIND is non-NULL, the
242 index of the attribute in TAG will be stored there. */
245 find_attr (struct taginfo *tag, const char *name, int *attrind)
248 for (i = 0; i < tag->nattrs; i++)
249 if (!strcasecmp (tag->attrs[i].name, name))
253 return tag->attrs[i].value;
259 char *text; /* HTML text. */
260 char *base; /* Base URI of the document, possibly
261 changed through <base href=...>. */
262 const char *parent_base; /* Base of the current document. */
263 const char *document_file; /* File name of this document. */
264 int nofollow; /* whether NOFOLLOW was specified in a
265 <meta name=robots> tag. */
267 struct urlpos *head, *tail; /* List of URLs that is being
271 /* Append LINK_URI to the urlpos structure that is being built.
273 LINK_URI will be merged with the current document base. TAG and
274 ATTRIND are the necessary context to store the position and
277 static struct urlpos *
278 append_url (const char *link_uri,
279 struct taginfo *tag, int attrind, struct map_context *ctx)
281 int link_has_scheme = url_has_scheme (link_uri);
282 struct urlpos *newel;
283 const char *base = ctx->base ? ctx->base : ctx->parent_base;
288 DEBUGP (("%s: no base, merge will use \"%s\".\n",
289 ctx->document_file, link_uri));
291 if (!link_has_scheme)
293 /* Base URL is unavailable, and the link does not have a
294 location attached to it -- we have to give up. Since
295 this can only happen when using `--force-html -i', print
297 logprintf (LOG_NOTQUIET,
298 _("%s: Cannot resolve incomplete link %s.\n"),
299 ctx->document_file, link_uri);
303 url = url_parse (link_uri, NULL);
306 DEBUGP (("%s: link \"%s\" doesn't parse.\n",
307 ctx->document_file, link_uri));
313 /* Merge BASE with LINK_URI, but also make sure the result is
314 canonicalized, i.e. that "../" have been resolved.
315 (parse_url will do that for us.) */
317 char *complete_uri = uri_merge (base, link_uri);
319 DEBUGP (("%s: merge(\"%s\", \"%s\") -> %s\n",
320 ctx->document_file, base, link_uri, complete_uri));
322 url = url_parse (complete_uri, NULL);
325 DEBUGP (("%s: merged link \"%s\" doesn't parse.\n",
326 ctx->document_file, complete_uri));
327 xfree (complete_uri);
330 xfree (complete_uri);
333 DEBUGP (("appending \"%s\" to urlpos.\n", url->url));
335 newel = xnew0 (struct urlpos);
337 newel->pos = tag->attrs[attrind].value_raw_beginning - ctx->text;
338 newel->size = tag->attrs[attrind].value_raw_size;
340 /* A URL is relative if the host is not named, and the name does not
342 if (!link_has_scheme && *link_uri != '/')
343 newel->link_relative_p = 1;
344 else if (link_has_scheme)
345 newel->link_complete_p = 1;
349 ctx->tail->next = newel;
353 ctx->tail = ctx->head = newel;
358 /* All the tag_* functions are called from collect_tags_mapper, as
359 specified by KNOWN_TAGS. */
361 /* Default tag handler: collect URLs from attributes specified for
362 this tag by tag_url_attributes. */
365 tag_find_urls (int tagid, struct taginfo *tag, struct map_context *ctx)
370 for (i = 0; i < countof (tag_url_attributes); i++)
371 if (tag_url_attributes[i].tagid == tagid)
373 /* We've found the index of tag_url_attributes where the
374 attributes of our tag begin. */
378 assert (first != -1);
380 /* Loop over the "interesting" attributes of this tag. In this
381 example, it will loop over "src" and "lowsrc".
383 <img src="foo.png" lowsrc="bar.png">
385 This has to be done in the outer loop so that the attributes are
386 processed in the same order in which they appear in the page.
387 This is required when converting links. */
389 for (attrind = 0; attrind < tag->nattrs; attrind++)
391 /* Find whether TAG/ATTRIND is a combination that contains a
393 char *link = tag->attrs[attrind].value;
394 const int size = countof (tag_url_attributes);
396 /* If you're cringing at the inefficiency of the nested loops,
397 remember that they both iterate over a very small number of
398 items. The worst-case inner loop is for the IMG tag, which
399 has three attributes. */
400 for (i = first; i < size && tag_url_attributes[i].tagid == tagid; i++)
402 if (0 == strcasecmp (tag->attrs[attrind].name,
403 tag_url_attributes[i].attr_name))
405 struct urlpos *up = append_url (link, tag, attrind, ctx);
408 int flags = tag_url_attributes[i].flags;
409 if (flags & ATTR_INLINE)
410 up->link_inline_p = 1;
411 if (flags & ATTR_HTML)
412 up->link_expect_html = 1;
419 /* Handle the BASE tag, for <base href=...>. */
422 tag_handle_base (int tagid, struct taginfo *tag, struct map_context *ctx)
424 struct urlpos *base_urlpos;
426 char *newbase = find_attr (tag, "href", &attrind);
430 base_urlpos = append_url (newbase, tag, attrind, ctx);
433 base_urlpos->ignore_when_downloading = 1;
434 base_urlpos->link_base_p = 1;
438 if (ctx->parent_base)
439 ctx->base = uri_merge (ctx->parent_base, newbase);
441 ctx->base = xstrdup (newbase);
444 /* Mark the URL found in <form action=...> for conversion. */
447 tag_handle_form (int tagid, struct taginfo *tag, struct map_context *ctx)
450 char *action = find_attr (tag, "action", &attrind);
453 struct urlpos *up = append_url (action, tag, attrind, ctx);
455 up->ignore_when_downloading = 1;
459 /* Handle the LINK tag. It requires special handling because how its
460 links will be followed in -p mode depends on the REL attribute. */
463 tag_handle_link (int tagid, struct taginfo *tag, struct map_context *ctx)
466 char *href = find_attr (tag, "href", &attrind);
468 /* All <link href="..."> link references are external, except those
469 known not to be, such as style sheet and shortcut icon:
471 <link rel="stylesheet" href="...">
472 <link rel="shortcut icon" href="...">
476 struct urlpos *up = append_url (href, tag, attrind, ctx);
479 char *rel = find_attr (tag, "rel", NULL);
481 && (0 == strcasecmp (rel, "stylesheet")
482 || 0 == strcasecmp (rel, "shortcut icon")))
483 up->link_inline_p = 1;
488 /* Handle the META tag. This requires special handling because of the
489 refresh feature and because of robot exclusion. */
492 tag_handle_meta (int tagid, struct taginfo *tag, struct map_context *ctx)
494 char *name = find_attr (tag, "name", NULL);
495 char *http_equiv = find_attr (tag, "http-equiv", NULL);
497 if (http_equiv && 0 == strcasecmp (http_equiv, "refresh"))
499 /* Some pages use a META tag to specify that the page be
500 refreshed by a new page after a given number of seconds. The
501 general format for this is:
503 <meta http-equiv=Refresh content="NUMBER; URL=index2.html">
505 So we just need to skip past the "NUMBER; URL=" garbage to
508 struct urlpos *entry;
513 char *refresh = find_attr (tag, "content", &attrind);
517 for (p = refresh; ISDIGIT (*p); p++)
518 timeout = 10 * timeout + *p - '0';
524 if (!( TOUPPER (*p) == 'U'
525 && TOUPPER (*(p + 1)) == 'R'
526 && TOUPPER (*(p + 2)) == 'L'
533 entry = append_url (p, tag, attrind, ctx);
536 entry->link_refresh_p = 1;
537 entry->refresh_timeout = timeout;
538 entry->link_expect_html = 1;
541 else if (name && 0 == strcasecmp (name, "robots"))
543 /* Handle stuff like:
544 <meta name="robots" content="index,nofollow"> */
545 char *content = find_attr (tag, "content", NULL);
548 if (!strcasecmp (content, "none"))
554 /* Find the next occurrence of ',' or the end of
556 char *end = strchr (content, ',');
560 end = content + strlen (content);
561 if (!strncasecmp (content, "nofollow", end - content))
569 /* Dispatch the tag handler appropriate for the tag we're mapping
570 over. See known_tags[] for definition of tag handlers. */
573 collect_tags_mapper (struct taginfo *tag, void *arg)
575 struct map_context *ctx = (struct map_context *)arg;
577 /* Find the tag in our table of tags. This must not fail because
578 map_html_tags only returns tags found in interesting_tags. */
579 struct known_tag *t = hash_table_get (interesting_tags, tag->name);
582 t->handler (t->tagid, tag, ctx);
585 /* Analyze HTML tags FILE and construct a list of URLs referenced from
586 it. It merges relative links in FILE with URL. It is aware of
587 <base href=...> and does the right thing. */
590 get_urls_html (const char *file, const char *url, int *meta_disallow_follow)
592 struct file_memory *fm;
593 struct map_context ctx;
597 fm = read_file (file);
600 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
603 DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length)));
605 ctx.text = fm->content;
606 ctx.head = ctx.tail = NULL;
608 ctx.parent_base = url ? url : opt.base_href;
609 ctx.document_file = file;
612 if (!interesting_tags)
615 /* Specify MHT_TRIM_VALUES because of buggy HTML generators that
616 generate <a href=" foo"> instead of <a href="foo"> (browsers
617 ignore spaces as well.) If you really mean space, use &32; or
618 %20. MHT_TRIM_VALUES also causes squashing of embedded newlines,
619 e.g. in <img src="foo.[newline]html">. Such newlines are also
620 ignored by IE and Mozilla and are presumably introduced by
621 writing HTML with editors that force word wrap. */
622 flags = MHT_TRIM_VALUES;
623 if (opt.strict_comments)
624 flags |= MHT_STRICT_COMMENTS;
626 map_html_tags (fm->content, fm->length, collect_tags_mapper, &ctx, flags,
627 interesting_tags, interesting_attributes);
629 DEBUGP (("no-follow in %s: %d\n", file, ctx.nofollow));
630 if (meta_disallow_follow)
631 *meta_disallow_follow = ctx.nofollow;
633 xfree_null (ctx.base);
638 /* This doesn't really have anything to do with HTML, but it's similar
639 to get_urls_html, so we put it here. */
642 get_urls_file (const char *file)
644 struct file_memory *fm;
645 struct urlpos *head, *tail;
646 const char *text, *text_end;
649 fm = read_file (file);
652 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
655 DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length)));
659 text_end = fm->content + fm->length;
660 while (text < text_end)
664 struct urlpos *entry;
667 const char *line_beg = text;
668 const char *line_end = memchr (text, '\n', text_end - text);
675 /* Strip whitespace from the beginning and end of line. */
676 while (line_beg < line_end && ISSPACE (*line_beg))
678 while (line_end > line_beg && ISSPACE (*(line_end - 1)))
681 if (line_beg == line_end)
684 /* The URL is in the [line_beg, line_end) region. */
686 /* We must copy the URL to a zero-terminated string, and we
687 can't use alloca because we're in a loop. *sigh*. */
688 url_text = strdupdelim (line_beg, line_end);
692 /* Merge opt.base_href with URL. */
693 char *merged = uri_merge (opt.base_href, url_text);
698 url = url_parse (url_text, &up_error_code);
701 logprintf (LOG_NOTQUIET, "%s: Invalid URL %s: %s\n",
702 file, url_text, url_error (up_error_code));
708 entry = xnew0 (struct urlpos);
723 cleanup_html_url (void)
725 /* Destroy the hash tables. The hash table keys and values are not
726 allocated by this code, so we don't need to free them here. */
727 if (interesting_tags)
728 hash_table_destroy (interesting_tags);
729 if (interesting_attributes)
730 hash_table_destroy (interesting_attributes);