1 /* Collect URLs from HTML source.
2 Copyright (C) 1998, 2000 Free Software Foundation, Inc.
4 This file is part of Wget.
6 This program 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 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
34 #include "html-parse.h"
42 enum tag_category { TC_LINK, TC_SPEC };
44 /* Here we try to categorize the known tags. Each tag has its ID and
45 cetegory. Category TC_LINK means that one or more of its
46 attributes contain links that should be retrieved. TC_SPEC means
47 that the tag is specific in some way, and has to be handled
51 enum tag_category category;
56 { "applet", TC_LINK },
62 { "bgsound", TC_LINK },
72 { "iframe", TC_LINK },
83 #define TAG_OVERLAY 15
84 { "overlay", TC_LINK },
86 { "script", TC_LINK },
96 /* Flags for specific url-attr pairs handled through TC_LINK: */
98 /* This tag points to an external document not necessary for rendering this
99 document (i.e. it's not an inlined image, stylesheet, etc.). */
100 #define AF_EXTERNAL 1
103 /* For tags handled by TC_LINK: attributes that contain URLs to
107 const char *attr_name;
109 } url_tag_attr_map[] = {
110 { TAG_A, "href", AF_EXTERNAL },
111 { TAG_APPLET, "code", 0 },
112 { TAG_AREA, "href", AF_EXTERNAL },
113 { TAG_BGSOUND, "src", 0 },
114 { TAG_BODY, "background", 0 },
115 { TAG_EMBED, "src", 0 },
116 { TAG_FIG, "src", 0 },
117 { TAG_FRAME, "src", 0 },
118 { TAG_IFRAME, "src", 0 },
119 { TAG_IMG, "href", 0 },
120 { TAG_IMG, "lowsrc", 0 },
121 { TAG_IMG, "src", 0 },
122 { TAG_INPUT, "src", 0 },
123 { TAG_LAYER, "src", 0 },
124 { TAG_OVERLAY, "src", 0 },
125 { TAG_SCRIPT, "src", 0 },
126 { TAG_TABLE, "background", 0 },
127 { TAG_TD, "background", 0 },
128 { TAG_TH, "background", 0 }
131 /* The lists of interesting tags and attributes are built dynamically,
132 from the information above. However, some places in the code refer
133 to the attributes not mentioned here. We add them manually. */
134 static const char *additional_attributes[] = {
135 "rel", /* for TAG_LINK */
136 "http-equiv", /* for TAG_META */
137 "name", /* for TAG_META */
138 "content" /* for TAG_META */
141 static const char **interesting_tags;
142 static const char **interesting_attributes;
145 init_interesting (void)
147 /* Init the variables interesting_tags and interesting_attributes
148 that are used by the HTML parser to know which tags and
149 attributes we're interested in. We initialize this only once,
150 for performance reasons.
152 Here we also make sure that what we put in interesting_tags
153 matches the user's preferences as specified through --ignore-tags
154 and --follow-tags. */
158 int size = ARRAY_SIZE (known_tags);
159 interesting_tags = (const char **)xmalloc ((size + 1) * sizeof (char *));
161 for (i = 0; i < size; i++)
163 const char *name = known_tags[i].name;
165 /* Normally here we could say:
166 interesting_tags[i] = name;
167 But we need to respect the settings of --ignore-tags and
168 --follow-tags, so the code gets a bit hairier. */
172 /* --ignore-tags was specified. Do not match these
173 specific tags. --ignore-tags takes precedence over
174 --follow-tags, so we process --ignore first and fall
175 through if there's no match. */
177 for (j = 0; opt.ignore_tags[j] != NULL; j++)
178 /* Loop through all the tags this user doesn't care about. */
179 if (strcasecmp(opt.ignore_tags[j], name) == EQ)
190 /* --follow-tags was specified. Only match these specific tags, so
191 continue back to top of for if we don't match one of them. */
193 for (j = 0; opt.follow_tags[j] != NULL; j++)
194 /* Loop through all the tags this user cares about. */
195 if (strcasecmp(opt.follow_tags[j], name) == EQ)
201 continue; /* wasn't one of the explicitly desired tags */
204 /* If we get to here, --follow-tags isn't being used or the
205 tag is among the ones that are followed, and --ignore-tags,
206 if specified, didn't include this tag, so it's an
207 "interesting" one. */
208 interesting_tags[ind++] = name;
210 interesting_tags[ind] = NULL;
213 /* The same for attributes, except we loop through url_tag_attr_map.
214 Here we also need to make sure that the list of attributes is
215 unique, and to include the attributes from additional_attributes. */
218 const char **att = xmalloc ((ARRAY_SIZE (additional_attributes) + 1)
220 /* First copy the "additional" attributes. */
221 for (i = 0; i < ARRAY_SIZE (additional_attributes); i++)
222 att[i] = additional_attributes[i];
225 for (i = 0; i < ARRAY_SIZE (url_tag_attr_map); i++)
228 const char *look_for = url_tag_attr_map[i].attr_name;
229 for (j = 0; j < ind - 1; j++)
230 if (!strcmp (att[j], look_for))
237 att = xrealloc (att, (ind + 2) * sizeof (*att));
238 att[ind++] = look_for;
242 interesting_attributes = att;
247 find_tag (const char *tag_name)
251 /* This is linear search; if the number of tags grow, we can switch
254 for (i = 0; i < ARRAY_SIZE (known_tags); i++)
256 int cmp = strcasecmp (known_tags[i].name, tag_name);
257 /* known_tags are sorted alphabetically, so we can
267 /* Find the value of attribute named NAME in the taginfo TAG. If the
268 attribute is not present, return NULL. If ATTRID is non-NULL, the
269 exact identity of the attribute will be returned. */
271 find_attr (struct taginfo *tag, const char *name, int *attrid)
274 for (i = 0; i < tag->nattrs; i++)
275 if (!strcasecmp (tag->attrs[i].name, name))
279 return tag->attrs[i].value;
284 struct collect_urls_closure {
285 char *text; /* HTML text. */
286 char *base; /* Base URI of the document, possibly
287 changed through <base href=...>. */
288 urlpos *head, *tail; /* List of URLs */
289 const char *parent_base; /* Base of the current document. */
290 const char *document_file; /* File name of this document. */
291 int dash_p_leaf_HTML; /* Whether -p is specified, and this
292 document is the "leaf" node of the
294 int nofollow; /* whether NOFOLLOW was specified in a
295 <meta name=robots> tag. */
298 /* Resolve LINK_URI and append it to closure->tail. TAG and ATTRID
299 are the necessary context to store the position and size. */
302 handle_link (struct collect_urls_closure *closure, const char *link_uri,
303 struct taginfo *tag, int attrid)
305 int no_proto = !has_proto (link_uri);
308 const char *base = closure->base ? closure->base : closure->parent_base;
311 char *fragment = strrchr (link_uri, '#');
315 /* Nullify the fragment identifier, i.e. everything after the
316 last occurrence of `#', inclusive. This copying is
317 relatively inefficient, but it doesn't matter because
318 fragment identifiers don't come up all that often. */
319 int hashlen = fragment - link_uri;
320 char *p = alloca (hashlen + 1);
321 memcpy (p, link_uri, hashlen);
330 /* We have no base, and the link does not have a protocol or
331 a host attached to it. Nothing we can do. */
332 /* #### Should we print a warning here? Wget 1.5.x used to. */
336 complete_uri = xstrdup (link_uri);
339 complete_uri = url_concat (base, link_uri);
341 DEBUGP (("%s: merge(\"%s\", \"%s\") -> %s\n",
342 closure->document_file, base ? base : "(null)",
343 link_uri, complete_uri));
345 newel = (urlpos *)xmalloc (sizeof (urlpos));
347 memset (newel, 0, sizeof (*newel));
349 newel->url = complete_uri;
350 newel->pos = tag->attrs[attrid].value_raw_beginning - closure->text;
351 newel->size = tag->attrs[attrid].value_raw_size;
353 /* A URL is relative if the host and protocol are not named, and the
354 name does not start with `/'. */
355 if (no_proto && *link_uri != '/')
356 newel->link_relative_p = 1;
358 newel->link_complete_p = 1;
362 closure->tail->next = newel;
363 closure->tail = newel;
366 closure->tail = closure->head = newel;
369 /* #### Document what this does.
370 #### It would be nice to split this into several functions. */
373 collect_tags_mapper (struct taginfo *tag, void *arg)
375 struct collect_urls_closure *closure = (struct collect_urls_closure *)arg;
376 int tagid = find_tag (tag->name);
377 assert (tagid != -1);
379 switch (known_tags[tagid].category)
384 int size = ARRAY_SIZE (url_tag_attr_map);
385 for (i = 0; i < size; i++)
386 if (url_tag_attr_map[i].tagid == tagid)
388 /* We've found the index of url_tag_attr_map where the
389 attributes of our tags begin. Now, look for every one of
390 them, and handle it. */
391 for (; (i < size && url_tag_attr_map[i].tagid == tagid); i++)
395 if (closure->dash_p_leaf_HTML
396 && (url_tag_attr_map[i].flags & AF_EXTERNAL))
397 /* If we're at a -p leaf node, we don't want to retrieve
398 links to references we know are external to this document,
399 such as <a href=...>. */
402 /* This find_attr() buried in a loop may seem inefficient
403 (O(n^2)), but it's not, since the number of attributes
404 (n) we loop over is extremely small. In the worst case
405 of IMG with all its possible attributes, n^2 will be
407 attr_value = find_attr (tag, url_tag_attr_map[i].attr_name, &id);
409 handle_link (closure, attr_value, tag, id);
418 char *newbase = find_attr (tag, "href", NULL);
422 xfree (closure->base);
423 if (closure->parent_base)
424 closure->base = url_concat (closure->parent_base, newbase);
426 closure->base = xstrdup (newbase);
432 char *rel = find_attr (tag, "rel", NULL);
433 char *href = find_attr (tag, "href", &id);
436 /* In the normal case, all <link href=...> tags are
439 In the special case of when -p is active, however,
440 and we're at a leaf node (relative to the -l
441 max. depth) in the HTML document tree, the only
442 <LINK> tag we'll follow is a <LINK REL=
443 "stylesheet">, as it'll be necessary for displaying
444 this document properly. We won't follow other
445 <LINK> tags, like <LINK REL="home">, for instance,
446 as they refer to external documents. */
447 if (!closure->dash_p_leaf_HTML
448 || (rel && !strcasecmp (rel, "stylesheet")))
449 handle_link (closure, href, tag, id);
454 /* Some pages use a META tag to specify that the page be
455 refreshed by a new page after a given number of seconds.
456 The general format for this is:
458 <meta http-equiv=Refresh content="NUMBER; URL=index2.html">
460 So we just need to skip past the "NUMBER; URL=" garbage
461 to get to the URL. */
464 char *name = find_attr (tag, "name", NULL);
465 char *http_equiv = find_attr (tag, "http-equiv", &id);
466 if (http_equiv && !strcasecmp (http_equiv, "refresh"))
468 char *refresh = find_attr (tag, "content", NULL);
477 if (!(TOUPPER (*p) == 'U'
478 && TOUPPER (*(p + 1)) == 'R'
479 && TOUPPER (*(p + 2)) == 'L'
485 offset = p - refresh;
486 tag->attrs[id].value_raw_beginning += offset;
487 tag->attrs[id].value_raw_size -= offset;
488 handle_link (closure, p, tag, id);
490 else if (name && !strcasecmp (name, "robots"))
492 /* Handle stuff like:
493 <meta name="robots" content="index,nofollow"> */
494 char *content = find_attr (tag, "content", NULL);
497 if (!strcasecmp (content, "none"))
498 closure->nofollow = 1;
503 /* Find the next occurrence of ',' or the end of
505 char *end = strchr (content, ',');
509 end = content + strlen (content);
510 if (!strncasecmp (content, "nofollow", end - content))
511 closure->nofollow = 1;
519 /* Category is TC_SPEC, but tag name is unhandled. This
527 /* Scan FILE, retrieving links to HTML documents from it. Each link is
529 Similar to get_urls_file, but for HTML files. FILE is scanned as
530 an HTML document. get_urls_html() constructs the URLs from the
533 If SILENT is non-zero, do not barf on baseless relative links. */
535 get_urls_html (const char *file, const char *this_url, int dash_p_leaf_HTML,
536 int *meta_disallow_follow)
538 struct file_memory *fm;
539 struct collect_urls_closure closure;
542 fm = read_file (file);
545 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
548 DEBUGP (("Loaded %s (size %ld).\n", file, fm->length));
550 closure.text = fm->content;
551 closure.head = closure.tail = NULL;
553 closure.parent_base = this_url ? this_url : opt.base_href;
554 closure.document_file = file;
555 closure.dash_p_leaf_HTML = dash_p_leaf_HTML;
556 closure.nofollow = 0;
558 if (!interesting_tags)
561 map_html_tags (fm->content, fm->length, interesting_tags,
562 interesting_attributes, collect_tags_mapper, &closure);
564 DEBUGP (("no-follow in %s: %d\n", file, closure.nofollow));
565 if (meta_disallow_follow)
566 *meta_disallow_follow = closure.nofollow;
568 FREE_MAYBE (closure.base);
574 cleanup_html_url (void)
576 FREE_MAYBE (interesting_tags);
577 FREE_MAYBE (interesting_attributes);