1 /* Collect URLs from HTML source.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2008, 2009, 2010 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. */
39 #include "html-parse.h"
48 typedef void (*tag_handler_t) (int, struct taginfo *, struct map_context *);
50 #define DECLARE_TAG_HANDLER(fun) \
51 static void fun (int, struct taginfo *, struct map_context *)
53 DECLARE_TAG_HANDLER (tag_find_urls);
54 DECLARE_TAG_HANDLER (tag_handle_base);
55 DECLARE_TAG_HANDLER (tag_handle_form);
56 DECLARE_TAG_HANDLER (tag_handle_link);
57 DECLARE_TAG_HANDLER (tag_handle_meta);
84 /* The list of known tags and functions used for handling them. Most
85 tags are simply harvested for URLs. */
86 static struct known_tag {
89 tag_handler_t handler;
91 { TAG_A, "a", tag_find_urls },
92 { TAG_APPLET, "applet", tag_find_urls },
93 { TAG_AREA, "area", tag_find_urls },
94 { TAG_BASE, "base", tag_handle_base },
95 { TAG_BGSOUND, "bgsound", tag_find_urls },
96 { TAG_BODY, "body", tag_find_urls },
97 { TAG_EMBED, "embed", tag_find_urls },
98 { TAG_FIG, "fig", tag_find_urls },
99 { TAG_FORM, "form", tag_handle_form },
100 { TAG_FRAME, "frame", tag_find_urls },
101 { TAG_IFRAME, "iframe", tag_find_urls },
102 { TAG_IMG, "img", tag_find_urls },
103 { TAG_INPUT, "input", tag_find_urls },
104 { TAG_LAYER, "layer", tag_find_urls },
105 { TAG_LINK, "link", tag_handle_link },
106 { TAG_META, "meta", tag_handle_meta },
107 { TAG_OBJECT, "object", tag_find_urls },
108 { TAG_OVERLAY, "overlay", tag_find_urls },
109 { TAG_SCRIPT, "script", tag_find_urls },
110 { TAG_TABLE, "table", tag_find_urls },
111 { TAG_TD, "td", tag_find_urls },
112 { TAG_TH, "th", tag_find_urls }
115 /* tag_url_attributes documents which attributes of which tags contain
116 URLs to harvest. It is used by tag_find_urls. */
118 /* Defines for the FLAGS. */
120 /* The link is "inline", i.e. needs to be retrieved for this document
121 to be correctly rendered. Inline links include inlined images,
122 stylesheets, children frames, etc. */
123 #define ATTR_INLINE 1
125 /* The link is expected to yield HTML contents. It's important not to
126 try to follow HTML obtained by following e.g. <img src="...">
127 regardless of content-type. Doing this causes infinite loops for
128 "images" that return non-404 error pages with links to the same
132 /* For tags handled by tag_find_urls: attributes that contain URLs to
136 const char *attr_name;
138 } tag_url_attributes[] = {
139 { TAG_A, "href", ATTR_HTML },
140 { TAG_APPLET, "code", ATTR_INLINE },
141 { TAG_AREA, "href", ATTR_HTML },
142 { TAG_BGSOUND, "src", ATTR_INLINE },
143 { TAG_BODY, "background", ATTR_INLINE },
144 { TAG_EMBED, "href", ATTR_HTML },
145 { TAG_EMBED, "src", ATTR_INLINE | ATTR_HTML },
146 { TAG_FIG, "src", ATTR_INLINE },
147 { TAG_FRAME, "src", ATTR_INLINE | ATTR_HTML },
148 { TAG_IFRAME, "src", ATTR_INLINE | ATTR_HTML },
149 { TAG_IMG, "href", ATTR_INLINE },
150 { TAG_IMG, "lowsrc", ATTR_INLINE },
151 { TAG_IMG, "src", ATTR_INLINE },
152 { TAG_INPUT, "src", ATTR_INLINE },
153 { TAG_LAYER, "src", ATTR_INLINE | ATTR_HTML },
154 { TAG_OBJECT, "data", ATTR_INLINE },
155 { TAG_OVERLAY, "src", ATTR_INLINE | ATTR_HTML },
156 { TAG_SCRIPT, "src", ATTR_INLINE },
157 { TAG_TABLE, "background", ATTR_INLINE },
158 { TAG_TD, "background", ATTR_INLINE },
159 { TAG_TH, "background", ATTR_INLINE }
162 /* The lists of interesting tags and attributes are built dynamically,
163 from the information above. However, some places in the code refer
164 to the attributes not mentioned here. We add them manually. */
165 static const char *additional_attributes[] = {
166 "rel", /* used by tag_handle_link */
167 "http-equiv", /* used by tag_handle_meta */
168 "name", /* used by tag_handle_meta */
169 "content", /* used by tag_handle_meta */
170 "action", /* used by tag_handle_form */
171 "style" /* used by check_style_attr */
174 static struct hash_table *interesting_tags;
175 static struct hash_table *interesting_attributes;
177 /* Will contains the (last) charset found in 'http-equiv=content-type'
179 static char *meta_charset;
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 hash_table_put (interesting_attributes, additional_attributes[i], "1");
231 for (i = 0; i < countof (tag_url_attributes); i++)
232 hash_table_put (interesting_attributes,
233 tag_url_attributes[i].attr_name, "1");
236 /* Find the value of attribute named NAME in the taginfo TAG. If the
237 attribute is not present, return NULL. If ATTRIND is non-NULL, the
238 index of the attribute in TAG will be stored there. */
241 find_attr (struct taginfo *tag, const char *name, int *attrind)
244 for (i = 0; i < tag->nattrs; i++)
245 if (!strcasecmp (tag->attrs[i].name, name))
249 return tag->attrs[i].value;
254 /* used for calls to append_url */
255 #define ATTR_POS(tag, attrind, ctx) \
256 (tag->attrs[attrind].value_raw_beginning - ctx->text)
257 #define ATTR_SIZE(tag, attrind) \
258 (tag->attrs[attrind].value_raw_size)
260 /* Append LINK_URI to the urlpos structure that is being built.
262 LINK_URI will be merged with the current document base.
266 append_url (const char *link_uri, int position, int size,
267 struct map_context *ctx)
269 int link_has_scheme = url_has_scheme (link_uri);
270 struct urlpos *newel;
271 const char *base = ctx->base ? ctx->base : ctx->parent_base;
276 DEBUGP (("%s: no base, merge will use \"%s\".\n",
277 ctx->document_file, link_uri));
279 if (!link_has_scheme)
281 /* Base URL is unavailable, and the link does not have a
282 location attached to it -- we have to give up. Since
283 this can only happen when using `--force-html -i', print
285 logprintf (LOG_NOTQUIET,
286 _("%s: Cannot resolve incomplete link %s.\n"),
287 ctx->document_file, link_uri);
291 url = url_parse (link_uri, NULL, NULL, false);
294 DEBUGP (("%s: link \"%s\" doesn't parse.\n",
295 ctx->document_file, link_uri));
301 /* Merge BASE with LINK_URI, but also make sure the result is
302 canonicalized, i.e. that "../" have been resolved.
303 (parse_url will do that for us.) */
305 char *complete_uri = uri_merge (base, link_uri);
307 DEBUGP (("%s: merge(%s, %s) -> %s\n",
308 quotearg_n_style (0, escape_quoting_style, ctx->document_file),
310 quote_n (2, link_uri),
311 quotearg_n_style (3, escape_quoting_style, complete_uri)));
313 url = url_parse (complete_uri, NULL, NULL, false);
316 DEBUGP (("%s: merged link \"%s\" doesn't parse.\n",
317 ctx->document_file, complete_uri));
318 xfree (complete_uri);
321 xfree (complete_uri);
324 DEBUGP (("appending %s to urlpos.\n", quote (url->url)));
326 newel = xnew0 (struct urlpos);
328 newel->pos = position;
331 /* A URL is relative if the host is not named, and the name does not
333 if (!link_has_scheme && *link_uri != '/')
334 newel->link_relative_p = 1;
335 else if (link_has_scheme)
336 newel->link_complete_p = 1;
338 /* Append the new URL maintaining the order by position. */
339 if (ctx->head == NULL)
343 struct urlpos *it, *prev = NULL;
346 while (it && position > it->pos)
364 check_style_attr (struct taginfo *tag, struct map_context *ctx)
369 char *style = find_attr (tag, "style", &attrind);
373 /* raw pos and raw size include the quotes, skip them when they are
375 raw_start = ATTR_POS (tag, attrind, ctx);
376 raw_len = ATTR_SIZE (tag, attrind);
377 if( *(char *)(ctx->text + raw_start) == '\''
378 || *(char *)(ctx->text + raw_start) == '"')
387 get_urls_css (ctx, raw_start, raw_len);
390 /* All the tag_* functions are called from collect_tags_mapper, as
391 specified by KNOWN_TAGS. */
393 /* Default tag handler: collect URLs from attributes specified for
394 this tag by tag_url_attributes. */
397 tag_find_urls (int tagid, struct taginfo *tag, struct map_context *ctx)
403 for (i = 0; i < countof (tag_url_attributes); i++)
404 if (tag_url_attributes[i].tagid == tagid)
406 /* We've found the index of tag_url_attributes where the
407 attributes of our tag begin. */
411 assert (first != -1);
413 /* Loop over the "interesting" attributes of this tag. In this
414 example, it will loop over "src" and "lowsrc".
416 <img src="foo.png" lowsrc="bar.png">
418 This has to be done in the outer loop so that the attributes are
419 processed in the same order in which they appear in the page.
420 This is required when converting links. */
422 for (attrind = 0; attrind < tag->nattrs; attrind++)
424 /* Find whether TAG/ATTRIND is a combination that contains a
426 char *link = tag->attrs[attrind].value;
427 const size_t size = countof (tag_url_attributes);
429 /* If you're cringing at the inefficiency of the nested loops,
430 remember that they both iterate over a very small number of
431 items. The worst-case inner loop is for the IMG tag, which
432 has three attributes. */
433 for (i = first; i < size && tag_url_attributes[i].tagid == tagid; i++)
435 if (0 == strcasecmp (tag->attrs[attrind].name,
436 tag_url_attributes[i].attr_name))
438 struct urlpos *up = append_url (link, ATTR_POS(tag,attrind,ctx),
439 ATTR_SIZE(tag,attrind), ctx);
442 int flags = tag_url_attributes[i].flags;
443 if (flags & ATTR_INLINE)
444 up->link_inline_p = 1;
445 if (flags & ATTR_HTML)
446 up->link_expect_html = 1;
453 /* Handle the BASE tag, for <base href=...>. */
456 tag_handle_base (int tagid, struct taginfo *tag, struct map_context *ctx)
458 struct urlpos *base_urlpos;
460 char *newbase = find_attr (tag, "href", &attrind);
464 base_urlpos = append_url (newbase, ATTR_POS(tag,attrind,ctx),
465 ATTR_SIZE(tag,attrind), ctx);
468 base_urlpos->ignore_when_downloading = 1;
469 base_urlpos->link_base_p = 1;
473 if (ctx->parent_base)
474 ctx->base = uri_merge (ctx->parent_base, newbase);
476 ctx->base = xstrdup (newbase);
479 /* Mark the URL found in <form action=...> for conversion. */
482 tag_handle_form (int tagid, struct taginfo *tag, struct map_context *ctx)
485 char *action = find_attr (tag, "action", &attrind);
489 struct urlpos *up = append_url (action, ATTR_POS(tag,attrind,ctx),
490 ATTR_SIZE(tag,attrind), ctx);
492 up->ignore_when_downloading = 1;
496 /* Handle the LINK tag. It requires special handling because how its
497 links will be followed in -p mode depends on the REL attribute. */
500 tag_handle_link (int tagid, struct taginfo *tag, struct map_context *ctx)
503 char *href = find_attr (tag, "href", &attrind);
505 /* All <link href="..."> link references are external, except those
506 known not to be, such as style sheet and shortcut icon:
508 <link rel="stylesheet" href="...">
509 <link rel="shortcut icon" href="...">
513 struct urlpos *up = append_url (href, ATTR_POS(tag,attrind,ctx),
514 ATTR_SIZE(tag,attrind), ctx);
517 char *rel = find_attr (tag, "rel", NULL);
520 if (0 == strcasecmp (rel, "stylesheet"))
522 up->link_inline_p = 1;
523 up->link_expect_css = 1;
525 else if (0 == strcasecmp (rel, "shortcut icon"))
527 up->link_inline_p = 1;
531 /* The external ones usually point to HTML pages, such as
532 <link rel="next" href="..."> */
533 up->link_expect_html = 1;
538 /* Handle the META tag. This requires special handling because of the
539 refresh feature and because of robot exclusion. */
542 tag_handle_meta (int tagid, struct taginfo *tag, struct map_context *ctx)
544 char *name = find_attr (tag, "name", NULL);
545 char *http_equiv = find_attr (tag, "http-equiv", NULL);
547 if (http_equiv && 0 == strcasecmp (http_equiv, "refresh"))
549 /* Some pages use a META tag to specify that the page be
550 refreshed by a new page after a given number of seconds. The
551 general format for this is:
553 <meta http-equiv=Refresh content="NUMBER; URL=index2.html">
555 So we just need to skip past the "NUMBER; URL=" garbage to
558 struct urlpos *entry;
563 char *refresh = find_attr (tag, "content", &attrind);
567 for (p = refresh; c_isdigit (*p); p++)
568 timeout = 10 * timeout + *p - '0';
572 while (c_isspace (*p))
574 if (!( c_toupper (*p) == 'U'
575 && c_toupper (*(p + 1)) == 'R'
576 && c_toupper (*(p + 2)) == 'L'
580 while (c_isspace (*p))
583 entry = append_url (p, ATTR_POS(tag,attrind,ctx),
584 ATTR_SIZE(tag,attrind), ctx);
587 entry->link_refresh_p = 1;
588 entry->refresh_timeout = timeout;
589 entry->link_expect_html = 1;
592 else if (http_equiv && 0 == strcasecmp (http_equiv, "content-type"))
594 /* Handle stuff like:
595 <meta http-equiv="Content-Type" content="text/html; charset=CHARSET"> */
598 char *content = find_attr (tag, "content", NULL);
602 mcharset = parse_charset (content);
606 xfree_null (meta_charset);
607 meta_charset = mcharset;
609 else if (name && 0 == strcasecmp (name, "robots"))
611 /* Handle stuff like:
612 <meta name="robots" content="index,nofollow"> */
613 char *content = find_attr (tag, "content", NULL);
616 if (!strcasecmp (content, "none"))
617 ctx->nofollow = true;
623 /* Skip any initial whitespace. */
624 content += strspn (content, " \f\n\r\t\v");
625 /* Find the next occurrence of ',' or whitespace,
626 * or the end of the string. */
627 end = content + strcspn (content, ", \f\n\r\t\v");
628 if (!strncasecmp (content, "nofollow", end - content))
629 ctx->nofollow = true;
630 /* Skip past the next comma, if any. */
635 end = strchr (end, ',');
639 end = content + strlen (content);
647 /* Dispatch the tag handler appropriate for the tag we're mapping
648 over. See known_tags[] for definition of tag handlers. */
651 collect_tags_mapper (struct taginfo *tag, void *arg)
653 struct map_context *ctx = (struct map_context *)arg;
655 /* Find the tag in our table of tags. This must not fail because
656 map_html_tags only returns tags found in interesting_tags.
658 I've changed this for now, I'm passing NULL as interesting_tags
659 to map_html_tags. This way we can check all tags for a style
662 struct known_tag *t = hash_table_get (interesting_tags, tag->name);
665 t->handler (t->tagid, tag, ctx);
667 check_style_attr (tag, ctx);
669 if (tag->end_tag_p && (0 == strcasecmp (tag->name, "style")) &&
670 tag->contents_begin && tag->contents_end)
673 get_urls_css (ctx, tag->contents_begin - ctx->text,
674 tag->contents_end - tag->contents_begin);
678 /* Analyze HTML tags FILE and construct a list of URLs referenced from
679 it. It merges relative links in FILE with URL. It is aware of
680 <base href=...> and does the right thing. */
683 get_urls_html (const char *file, const char *url, bool *meta_disallow_follow,
686 struct file_memory *fm;
687 struct map_context ctx;
691 fm = wget_read_file (file);
694 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
697 DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length)));
699 ctx.text = fm->content;
702 ctx.parent_base = url ? url : opt.base_href;
703 ctx.document_file = file;
704 ctx.nofollow = false;
706 if (!interesting_tags)
709 /* Specify MHT_TRIM_VALUES because of buggy HTML generators that
710 generate <a href=" foo"> instead of <a href="foo"> (browsers
711 ignore spaces as well.) If you really mean space, use &32; or
712 %20. MHT_TRIM_VALUES also causes squashing of embedded newlines,
713 e.g. in <img src="foo.[newline]html">. Such newlines are also
714 ignored by IE and Mozilla and are presumably introduced by
715 writing HTML with editors that force word wrap. */
716 flags = MHT_TRIM_VALUES;
717 if (opt.strict_comments)
718 flags |= MHT_STRICT_COMMENTS;
720 /* the NULL here used to be interesting_tags */
721 map_html_tags (fm->content, fm->length, collect_tags_mapper, &ctx, flags,
722 NULL, interesting_attributes);
724 /* If meta charset isn't null, override content encoding */
725 if (iri && meta_charset)
726 set_content_encoding (iri, meta_charset);
728 DEBUGP (("no-follow in %s: %d\n", file, ctx.nofollow));
729 if (meta_disallow_follow)
730 *meta_disallow_follow = ctx.nofollow;
732 xfree_null (ctx.base);
733 wget_read_file_free (fm);
737 /* This doesn't really have anything to do with HTML, but it's similar
738 to get_urls_html, so we put it here. */
741 get_urls_file (const char *file)
743 struct file_memory *fm;
744 struct urlpos *head, *tail;
745 const char *text, *text_end;
748 fm = wget_read_file (file);
751 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
754 DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length)));
758 text_end = fm->content + fm->length;
759 while (text < text_end)
763 struct urlpos *entry;
766 const char *line_beg = text;
767 const char *line_end = memchr (text, '\n', text_end - text);
774 /* Strip whitespace from the beginning and end of line. */
775 while (line_beg < line_end && c_isspace (*line_beg))
777 while (line_end > line_beg && c_isspace (*(line_end - 1)))
780 if (line_beg == line_end)
783 /* The URL is in the [line_beg, line_end) region. */
785 /* We must copy the URL to a zero-terminated string, and we
786 can't use alloca because we're in a loop. *sigh*. */
787 url_text = strdupdelim (line_beg, line_end);
791 /* Merge opt.base_href with URL. */
792 char *merged = uri_merge (opt.base_href, url_text);
797 url = url_parse (url_text, &up_error_code, NULL, false);
800 char *error = url_error (url_text, up_error_code);
801 logprintf (LOG_NOTQUIET, _("%s: Invalid URL %s: %s\n"),
802 file, url_text, error);
809 entry = xnew0 (struct urlpos);
818 wget_read_file_free (fm);
823 cleanup_html_url (void)
825 /* Destroy the hash tables. The hash table keys and values are not
826 allocated by this code, so we don't need to free them here. */
827 if (interesting_tags)
828 hash_table_destroy (interesting_tags);
829 if (interesting_attributes)
830 hash_table_destroy (interesting_attributes);