1 /* Collect URLs from HTML source.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007 Free Software Foundation, Inc.
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
20 Additional permission under GNU GPL version 3 section 7
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
40 #include "html-parse.h"
45 #include "recur.h" /* declaration of get_urls_html */
49 typedef void (*tag_handler_t) (int, struct taginfo *, struct map_context *);
51 #define DECLARE_TAG_HANDLER(fun) \
52 static void fun (int, struct taginfo *, struct map_context *)
54 DECLARE_TAG_HANDLER (tag_find_urls);
55 DECLARE_TAG_HANDLER (tag_handle_base);
56 DECLARE_TAG_HANDLER (tag_handle_form);
57 DECLARE_TAG_HANDLER (tag_handle_link);
58 DECLARE_TAG_HANDLER (tag_handle_meta);
85 /* The list of known tags and functions used for handling them. Most
86 tags are simply harvested for URLs. */
87 static struct known_tag {
90 tag_handler_t handler;
92 { TAG_A, "a", tag_find_urls },
93 { TAG_APPLET, "applet", tag_find_urls },
94 { TAG_AREA, "area", tag_find_urls },
95 { TAG_BASE, "base", tag_handle_base },
96 { TAG_BGSOUND, "bgsound", tag_find_urls },
97 { TAG_BODY, "body", tag_find_urls },
98 { TAG_EMBED, "embed", tag_find_urls },
99 { TAG_FIG, "fig", tag_find_urls },
100 { TAG_FORM, "form", tag_handle_form },
101 { TAG_FRAME, "frame", tag_find_urls },
102 { TAG_IFRAME, "iframe", tag_find_urls },
103 { TAG_IMG, "img", tag_find_urls },
104 { TAG_INPUT, "input", tag_find_urls },
105 { TAG_LAYER, "layer", tag_find_urls },
106 { TAG_LINK, "link", tag_handle_link },
107 { TAG_META, "meta", tag_handle_meta },
108 { TAG_OBJECT, "object", tag_find_urls },
109 { TAG_OVERLAY, "overlay", tag_find_urls },
110 { TAG_SCRIPT, "script", tag_find_urls },
111 { TAG_TABLE, "table", tag_find_urls },
112 { TAG_TD, "td", tag_find_urls },
113 { TAG_TH, "th", tag_find_urls }
116 /* tag_url_attributes documents which attributes of which tags contain
117 URLs to harvest. It is used by tag_find_urls. */
119 /* Defines for the FLAGS. */
121 /* The link is "inline", i.e. needs to be retrieved for this document
122 to be correctly rendered. Inline links include inlined images,
123 stylesheets, children frames, etc. */
124 #define ATTR_INLINE 1
126 /* The link is expected to yield HTML contents. It's important not to
127 try to follow HTML obtained by following e.g. <img src="...">
128 regardless of content-type. Doing this causes infinite loops for
129 "images" that return non-404 error pages with links to the same
133 /* For tags handled by tag_find_urls: attributes that contain URLs to
137 const char *attr_name;
139 } tag_url_attributes[] = {
140 { TAG_A, "href", ATTR_HTML },
141 { TAG_APPLET, "code", ATTR_INLINE },
142 { TAG_AREA, "href", ATTR_HTML },
143 { TAG_BGSOUND, "src", ATTR_INLINE },
144 { TAG_BODY, "background", ATTR_INLINE },
145 { TAG_EMBED, "href", ATTR_HTML },
146 { TAG_EMBED, "src", ATTR_INLINE | ATTR_HTML },
147 { TAG_FIG, "src", ATTR_INLINE },
148 { TAG_FRAME, "src", ATTR_INLINE | ATTR_HTML },
149 { TAG_IFRAME, "src", ATTR_INLINE | ATTR_HTML },
150 { TAG_IMG, "href", ATTR_INLINE },
151 { TAG_IMG, "lowsrc", ATTR_INLINE },
152 { TAG_IMG, "src", ATTR_INLINE },
153 { TAG_INPUT, "src", ATTR_INLINE },
154 { TAG_LAYER, "src", ATTR_INLINE | ATTR_HTML },
155 { TAG_OBJECT, "data", ATTR_INLINE },
156 { TAG_OVERLAY, "src", ATTR_INLINE | ATTR_HTML },
157 { TAG_SCRIPT, "src", ATTR_INLINE },
158 { TAG_TABLE, "background", ATTR_INLINE },
159 { TAG_TD, "background", ATTR_INLINE },
160 { TAG_TH, "background", ATTR_INLINE }
163 /* The lists of interesting tags and attributes are built dynamically,
164 from the information above. However, some places in the code refer
165 to the attributes not mentioned here. We add them manually. */
166 static const char *additional_attributes[] = {
167 "rel", /* used by tag_handle_link */
168 "http-equiv", /* used by tag_handle_meta */
169 "name", /* used by tag_handle_meta */
170 "content", /* used by tag_handle_meta */
171 "action" /* used by tag_handle_form */
174 static struct hash_table *interesting_tags;
175 static struct hash_table *interesting_attributes;
178 init_interesting (void)
180 /* Init the variables interesting_tags and interesting_attributes
181 that are used by the HTML parser to know which tags and
182 attributes we're interested in. We initialize this only once,
183 for performance reasons.
185 Here we also make sure that what we put in interesting_tags
186 matches the user's preferences as specified through --ignore-tags
187 and --follow-tags. */
190 interesting_tags = make_nocase_string_hash_table (countof (known_tags));
192 /* First, add all the tags we know hot to handle, mapped to their
193 respective entries in known_tags. */
194 for (i = 0; i < countof (known_tags); i++)
195 hash_table_put (interesting_tags, known_tags[i].name, known_tags + i);
197 /* Then remove the tags ignored through --ignore-tags. */
201 for (ignored = opt.ignore_tags; *ignored; ignored++)
202 hash_table_remove (interesting_tags, *ignored);
205 /* If --follow-tags is specified, use only those tags. */
208 /* Create a new table intersecting --follow-tags and known_tags,
209 and use it as interesting_tags. */
210 struct hash_table *intersect = make_nocase_string_hash_table (0);
212 for (followed = opt.follow_tags; *followed; followed++)
214 struct known_tag *t = hash_table_get (interesting_tags, *followed);
216 continue; /* ignore unknown --follow-tags entries. */
217 hash_table_put (intersect, *followed, t);
219 hash_table_destroy (interesting_tags);
220 interesting_tags = intersect;
223 /* Add the attributes we care about. */
224 interesting_attributes = make_nocase_string_hash_table (10);
225 for (i = 0; i < countof (additional_attributes); i++)
226 hash_table_put (interesting_attributes, additional_attributes[i], "1");
227 for (i = 0; i < countof (tag_url_attributes); i++)
228 hash_table_put (interesting_attributes,
229 tag_url_attributes[i].attr_name, "1");
232 /* Find the value of attribute named NAME in the taginfo TAG. If the
233 attribute is not present, return NULL. If ATTRIND is non-NULL, the
234 index of the attribute in TAG will be stored there. */
237 find_attr (struct taginfo *tag, const char *name, int *attrind)
240 for (i = 0; i < tag->nattrs; i++)
241 if (!strcasecmp (tag->attrs[i].name, name))
245 return tag->attrs[i].value;
251 char *text; /* HTML text. */
252 char *base; /* Base URI of the document, possibly
253 changed through <base href=...>. */
254 const char *parent_base; /* Base of the current document. */
255 const char *document_file; /* File name of this document. */
256 bool nofollow; /* whether NOFOLLOW was specified in a
257 <meta name=robots> tag. */
259 struct urlpos *head, *tail; /* List of URLs that is being
263 /* Append LINK_URI to the urlpos structure that is being built.
265 LINK_URI will be merged with the current document base. TAG and
266 ATTRIND are the necessary context to store the position and
269 static struct urlpos *
270 append_url (const char *link_uri,
271 struct taginfo *tag, int attrind, struct map_context *ctx)
273 int link_has_scheme = url_has_scheme (link_uri);
274 struct urlpos *newel;
275 const char *base = ctx->base ? ctx->base : ctx->parent_base;
280 DEBUGP (("%s: no base, merge will use \"%s\".\n",
281 ctx->document_file, link_uri));
283 if (!link_has_scheme)
285 /* Base URL is unavailable, and the link does not have a
286 location attached to it -- we have to give up. Since
287 this can only happen when using `--force-html -i', print
289 logprintf (LOG_NOTQUIET,
290 _("%s: Cannot resolve incomplete link %s.\n"),
291 ctx->document_file, link_uri);
295 url = url_parse (link_uri, NULL);
298 DEBUGP (("%s: link \"%s\" doesn't parse.\n",
299 ctx->document_file, link_uri));
305 /* Merge BASE with LINK_URI, but also make sure the result is
306 canonicalized, i.e. that "../" have been resolved.
307 (parse_url will do that for us.) */
309 char *complete_uri = uri_merge (base, link_uri);
311 DEBUGP (("%s: merge(\"%s\", \"%s\") -> %s\n",
312 ctx->document_file, base, link_uri, complete_uri));
314 url = url_parse (complete_uri, NULL);
317 DEBUGP (("%s: merged link \"%s\" doesn't parse.\n",
318 ctx->document_file, complete_uri));
319 xfree (complete_uri);
322 xfree (complete_uri);
325 DEBUGP (("appending \"%s\" to urlpos.\n", url->url));
327 newel = xnew0 (struct urlpos);
329 newel->pos = tag->attrs[attrind].value_raw_beginning - ctx->text;
330 newel->size = tag->attrs[attrind].value_raw_size;
332 /* A URL is relative if the host is not named, and the name does not
334 if (!link_has_scheme && *link_uri != '/')
335 newel->link_relative_p = 1;
336 else if (link_has_scheme)
337 newel->link_complete_p = 1;
341 ctx->tail->next = newel;
345 ctx->tail = ctx->head = newel;
350 /* All the tag_* functions are called from collect_tags_mapper, as
351 specified by KNOWN_TAGS. */
353 /* Default tag handler: collect URLs from attributes specified for
354 this tag by tag_url_attributes. */
357 tag_find_urls (int tagid, struct taginfo *tag, struct map_context *ctx)
362 for (i = 0; i < countof (tag_url_attributes); i++)
363 if (tag_url_attributes[i].tagid == tagid)
365 /* We've found the index of tag_url_attributes where the
366 attributes of our tag begin. */
370 assert (first != -1);
372 /* Loop over the "interesting" attributes of this tag. In this
373 example, it will loop over "src" and "lowsrc".
375 <img src="foo.png" lowsrc="bar.png">
377 This has to be done in the outer loop so that the attributes are
378 processed in the same order in which they appear in the page.
379 This is required when converting links. */
381 for (attrind = 0; attrind < tag->nattrs; attrind++)
383 /* Find whether TAG/ATTRIND is a combination that contains a
385 char *link = tag->attrs[attrind].value;
386 const int size = countof (tag_url_attributes);
388 /* If you're cringing at the inefficiency of the nested loops,
389 remember that they both iterate over a very small number of
390 items. The worst-case inner loop is for the IMG tag, which
391 has three attributes. */
392 for (i = first; i < size && tag_url_attributes[i].tagid == tagid; i++)
394 if (0 == strcasecmp (tag->attrs[attrind].name,
395 tag_url_attributes[i].attr_name))
397 struct urlpos *up = append_url (link, tag, attrind, ctx);
400 int flags = tag_url_attributes[i].flags;
401 if (flags & ATTR_INLINE)
402 up->link_inline_p = 1;
403 if (flags & ATTR_HTML)
404 up->link_expect_html = 1;
411 /* Handle the BASE tag, for <base href=...>. */
414 tag_handle_base (int tagid, struct taginfo *tag, struct map_context *ctx)
416 struct urlpos *base_urlpos;
418 char *newbase = find_attr (tag, "href", &attrind);
422 base_urlpos = append_url (newbase, tag, attrind, ctx);
425 base_urlpos->ignore_when_downloading = 1;
426 base_urlpos->link_base_p = 1;
430 if (ctx->parent_base)
431 ctx->base = uri_merge (ctx->parent_base, newbase);
433 ctx->base = xstrdup (newbase);
436 /* Mark the URL found in <form action=...> for conversion. */
439 tag_handle_form (int tagid, struct taginfo *tag, struct map_context *ctx)
442 char *action = find_attr (tag, "action", &attrind);
445 struct urlpos *up = append_url (action, tag, attrind, ctx);
447 up->ignore_when_downloading = 1;
451 /* Handle the LINK tag. It requires special handling because how its
452 links will be followed in -p mode depends on the REL attribute. */
455 tag_handle_link (int tagid, struct taginfo *tag, struct map_context *ctx)
458 char *href = find_attr (tag, "href", &attrind);
460 /* All <link href="..."> link references are external, except those
461 known not to be, such as style sheet and shortcut icon:
463 <link rel="stylesheet" href="...">
464 <link rel="shortcut icon" href="...">
468 struct urlpos *up = append_url (href, tag, attrind, ctx);
471 char *rel = find_attr (tag, "rel", NULL);
473 && (0 == strcasecmp (rel, "stylesheet")
474 || 0 == strcasecmp (rel, "shortcut icon")))
475 up->link_inline_p = 1;
477 /* The external ones usually point to HTML pages, such as
478 <link rel="next" href="..."> */
479 up->link_expect_html = 1;
484 /* Handle the META tag. This requires special handling because of the
485 refresh feature and because of robot exclusion. */
488 tag_handle_meta (int tagid, struct taginfo *tag, struct map_context *ctx)
490 char *name = find_attr (tag, "name", NULL);
491 char *http_equiv = find_attr (tag, "http-equiv", NULL);
493 if (http_equiv && 0 == strcasecmp (http_equiv, "refresh"))
495 /* Some pages use a META tag to specify that the page be
496 refreshed by a new page after a given number of seconds. The
497 general format for this is:
499 <meta http-equiv=Refresh content="NUMBER; URL=index2.html">
501 So we just need to skip past the "NUMBER; URL=" garbage to
504 struct urlpos *entry;
509 char *refresh = find_attr (tag, "content", &attrind);
513 for (p = refresh; ISDIGIT (*p); p++)
514 timeout = 10 * timeout + *p - '0';
520 if (!( TOUPPER (*p) == 'U'
521 && TOUPPER (*(p + 1)) == 'R'
522 && TOUPPER (*(p + 2)) == 'L'
529 entry = append_url (p, tag, attrind, ctx);
532 entry->link_refresh_p = 1;
533 entry->refresh_timeout = timeout;
534 entry->link_expect_html = 1;
537 else if (name && 0 == strcasecmp (name, "robots"))
539 /* Handle stuff like:
540 <meta name="robots" content="index,nofollow"> */
541 char *content = find_attr (tag, "content", NULL);
544 if (!strcasecmp (content, "none"))
545 ctx->nofollow = true;
550 /* Find the next occurrence of ',' or the end of
552 char *end = strchr (content, ',');
556 end = content + strlen (content);
557 if (!strncasecmp (content, "nofollow", end - content))
558 ctx->nofollow = true;
565 /* Dispatch the tag handler appropriate for the tag we're mapping
566 over. See known_tags[] for definition of tag handlers. */
569 collect_tags_mapper (struct taginfo *tag, void *arg)
571 struct map_context *ctx = (struct map_context *)arg;
573 /* Find the tag in our table of tags. This must not fail because
574 map_html_tags only returns tags found in interesting_tags. */
575 struct known_tag *t = hash_table_get (interesting_tags, tag->name);
578 t->handler (t->tagid, tag, ctx);
581 /* Analyze HTML tags FILE and construct a list of URLs referenced from
582 it. It merges relative links in FILE with URL. It is aware of
583 <base href=...> and does the right thing. */
586 get_urls_html (const char *file, const char *url, bool *meta_disallow_follow)
588 struct file_memory *fm;
589 struct map_context ctx;
593 fm = read_file (file);
596 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
599 DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length)));
601 ctx.text = fm->content;
602 ctx.head = ctx.tail = NULL;
604 ctx.parent_base = url ? url : opt.base_href;
605 ctx.document_file = file;
606 ctx.nofollow = false;
608 if (!interesting_tags)
611 /* Specify MHT_TRIM_VALUES because of buggy HTML generators that
612 generate <a href=" foo"> instead of <a href="foo"> (browsers
613 ignore spaces as well.) If you really mean space, use &32; or
614 %20. MHT_TRIM_VALUES also causes squashing of embedded newlines,
615 e.g. in <img src="foo.[newline]html">. Such newlines are also
616 ignored by IE and Mozilla and are presumably introduced by
617 writing HTML with editors that force word wrap. */
618 flags = MHT_TRIM_VALUES;
619 if (opt.strict_comments)
620 flags |= MHT_STRICT_COMMENTS;
622 map_html_tags (fm->content, fm->length, collect_tags_mapper, &ctx, flags,
623 interesting_tags, interesting_attributes);
625 DEBUGP (("no-follow in %s: %d\n", file, ctx.nofollow));
626 if (meta_disallow_follow)
627 *meta_disallow_follow = ctx.nofollow;
629 xfree_null (ctx.base);
634 /* This doesn't really have anything to do with HTML, but it's similar
635 to get_urls_html, so we put it here. */
638 get_urls_file (const char *file)
640 struct file_memory *fm;
641 struct urlpos *head, *tail;
642 const char *text, *text_end;
645 fm = read_file (file);
648 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
651 DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length)));
655 text_end = fm->content + fm->length;
656 while (text < text_end)
660 struct urlpos *entry;
663 const char *line_beg = text;
664 const char *line_end = memchr (text, '\n', text_end - text);
671 /* Strip whitespace from the beginning and end of line. */
672 while (line_beg < line_end && ISSPACE (*line_beg))
674 while (line_end > line_beg && ISSPACE (*(line_end - 1)))
677 if (line_beg == line_end)
680 /* The URL is in the [line_beg, line_end) region. */
682 /* We must copy the URL to a zero-terminated string, and we
683 can't use alloca because we're in a loop. *sigh*. */
684 url_text = strdupdelim (line_beg, line_end);
688 /* Merge opt.base_href with URL. */
689 char *merged = uri_merge (opt.base_href, url_text);
694 url = url_parse (url_text, &up_error_code);
697 logprintf (LOG_NOTQUIET, _("%s: Invalid URL %s: %s\n"),
698 file, url_text, url_error (up_error_code));
704 entry = xnew0 (struct urlpos);
718 cleanup_html_url (void)
720 /* Destroy the hash tables. The hash table keys and values are not
721 allocated by this code, so we don't need to free them here. */
722 if (interesting_tags)
723 hash_table_destroy (interesting_tags);
724 if (interesting_attributes)
725 hash_table_destroy (interesting_attributes);