📄 recur.c
字号:
already spent the two extra (pseudo-)levels that it affords us, so we need to bail out. */ DEBUGP (("Not descending further; at depth %d, max. %d.\n", depth, opt.reclevel)); descend = 0; } } /* If the downloaded document was HTML, parse it and enqueue the links it contains. */ if (descend) { int meta_disallow_follow = 0; struct urlpos *children = get_urls_html (file, url, &meta_disallow_follow); if (opt.use_robots && meta_disallow_follow) { free_urlpos (children); children = NULL; } if (children) { struct urlpos *child = children; struct url *url_parsed = url_parsed = url_parse (url, NULL); assert (url_parsed != NULL); for (; child; child = child->next) { if (child->ignore_when_downloading) continue; if (dash_p_leaf_HTML && !child->link_inline_p) continue; if (download_child_p (child, url_parsed, depth, start_url_parsed, blacklist)) { url_enqueue (queue, xstrdup (child->url->url), xstrdup (url), depth + 1, child->link_expect_html); /* We blacklist the URL we have enqueued, because we don't want to enqueue (and hence download) the same URL twice. */ string_set_add (blacklist, child->url->url); } } url_free (url_parsed); free_urlpos (children); } } if (opt.delete_after || (file && !acceptable (file))) { /* Either --delete-after was specified, or we loaded this otherwise rejected (e.g. by -R) HTML file just so we could harvest its hyperlinks -- in either case, delete the local file. */ DEBUGP (("Removing file due to %s in recursive_retrieve():\n", opt.delete_after ? "--delete-after" : "recursive rejection criteria")); logprintf (LOG_VERBOSE, (opt.delete_after ? _("Removing %s.\n") : _("Removing %s since it should be rejected.\n")), file); if (unlink (file)) logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno)); register_delete_file (file); } xfree (url); FREE_MAYBE (referer); FREE_MAYBE (file); } /* If anything is left of the queue due to a premature exit, free it now. */ { char *d1, *d2; int d3, d4; while (url_dequeue (queue, (const char **)&d1, (const char **)&d2, &d3, &d4)) { xfree (d1); FREE_MAYBE (d2); } } url_queue_delete (queue); if (start_url_parsed) url_free (start_url_parsed); string_set_free (blacklist); if (opt.quota && total_downloaded_bytes > opt.quota) return QUOTEXC; else if (status == FWRITEERR) return FWRITEERR; else return RETROK;}/* Based on the context provided by retrieve_tree, decide whether a URL is to be descended to. This is only ever called from retrieve_tree, but is in a separate function for clarity. The most expensive checks (such as those for robots) are memoized by storing these URLs to BLACKLIST. This may or may not help. It will help if those URLs are encountered many times. */static intdownload_child_p (const struct urlpos *upos, struct url *parent, int depth, struct url *start_url_parsed, struct hash_table *blacklist){ struct url *u = upos->url; const char *url = u->url; int u_scheme_like_http; DEBUGP (("Deciding whether to enqueue \"%s\".\n", url)); if (string_set_contains (blacklist, url)) { DEBUGP (("Already on the black list.\n")); goto out; } /* Several things to check for: 1. if scheme is not http, and we don't load it 2. check for relative links (if relative_only is set) 3. check for domain 4. check for no-parent 5. check for excludes && includes 6. check for suffix 7. check for same host (if spanhost is unset), with possible gethostbyname baggage 8. check for robots.txt Addendum: If the URL is FTP, and it is to be loaded, only the domain and suffix settings are "stronger". Note that .html files will get loaded regardless of suffix rules (but that is remedied later with unlink) unless the depth equals the maximum depth. More time- and memory- consuming tests should be put later on the list. */ /* Determine whether URL under consideration has a HTTP-like scheme. */ u_scheme_like_http = schemes_are_similar_p (u->scheme, SCHEME_HTTP); /* 1. Schemes other than HTTP are normally not recursed into. */ if (!u_scheme_like_http && !(u->scheme == SCHEME_FTP && opt.follow_ftp)) { DEBUGP (("Not following non-HTTP schemes.\n")); goto out; } /* 2. If it is an absolute link and they are not followed, throw it out. */ if (u_scheme_like_http) if (opt.relative_only && !upos->link_relative_p) { DEBUGP (("It doesn't really look like a relative link.\n")); goto out; } /* 3. If its domain is not to be accepted/looked-up, chuck it out. */ if (!accept_domain (u)) { DEBUGP (("The domain was not accepted.\n")); goto out; } /* 4. Check for parent directory. If we descended to a different host or changed the scheme, ignore opt.no_parent. Also ignore it for documents needed to display the parent page when in -p mode. */ if (opt.no_parent && schemes_are_similar_p (u->scheme, start_url_parsed->scheme) && 0 == strcasecmp (u->host, start_url_parsed->host) && u->port == start_url_parsed->port && !(opt.page_requisites && upos->link_inline_p)) { if (!frontcmp (start_url_parsed->dir, u->dir)) { DEBUGP (("Going to \"%s\" would escape \"%s\" with no_parent on.\n", u->dir, start_url_parsed->dir)); goto out; } } /* 5. If the file does not match the acceptance list, or is on the rejection list, chuck it out. The same goes for the directory exclusion and inclusion lists. */ if (opt.includes || opt.excludes) { if (!accdir (u->dir, ALLABS)) { DEBUGP (("%s (%s) is excluded/not-included.\n", url, u->dir)); goto out; } } /* 6. Check for acceptance/rejection rules. We ignore these rules for directories (no file name to match) and for HTML documents, which might lead to other files that do need to be downloaded. That is, unless we've exhausted the recursion depth anyway. */ if (u->file[0] != '\0' && !(has_html_suffix_p (u->file) && depth != INFINITE_RECURSION && depth < opt.reclevel - 1)) { if (!acceptable (u->file)) { DEBUGP (("%s (%s) does not match acc/rej rules.\n", url, u->file)); goto out; } } /* 7. */ if (schemes_are_similar_p (u->scheme, parent->scheme)) if (!opt.spanhost && 0 != strcasecmp (parent->host, u->host)) { DEBUGP (("This is not the same hostname as the parent's (%s and %s).\n", u->host, parent->host)); goto out; } /* 8. */ if (opt.use_robots && u_scheme_like_http) { struct robot_specs *specs = res_get_specs (u->host, u->port); if (!specs) { char *rfile; if (res_retrieve_file (url, &rfile)) { specs = res_parse_from_file (rfile); xfree (rfile); } else { /* If we cannot get real specs, at least produce dummy ones so that we can register them and stop trying to retrieve them. */ specs = res_parse ("", 0); } res_register_specs (u->host, u->port, specs); } /* Now that we have (or don't have) robots.txt specs, we can check what they say. */ if (!res_match_path (specs, u->path)) { DEBUGP (("Not following %s because robots.txt forbids it.\n", url)); string_set_add (blacklist, url); goto out; } } /* The URL has passed all the tests. It can be placed in the download queue. */ DEBUGP (("Decided to load it.\n")); return 1; out: DEBUGP (("Decided NOT to load it.\n")); return 0;}/* This function determines whether we will consider downloading the children of a URL whose download resulted in a redirection, possibly to another host, etc. It is needed very rarely, and thus it is merely a simple-minded wrapper around download_child_p. */static intdescend_redirect_p (const char *redirected, const char *original, int depth, struct url *start_url_parsed, struct hash_table *blacklist){ struct url *orig_parsed, *new_parsed; struct urlpos *upos; int success; orig_parsed = url_parse (original, NULL); assert (orig_parsed != NULL); new_parsed = url_parse (redirected, NULL); assert (new_parsed != NULL); upos = xmalloc (sizeof (struct urlpos)); memset (upos, 0, sizeof (*upos)); upos->url = new_parsed; success = download_child_p (upos, orig_parsed, depth, start_url_parsed, blacklist); url_free (orig_parsed); url_free (new_parsed); xfree (upos); if (!success) DEBUGP (("Redirection \"%s\" failed the test.\n", redirected)); return success;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -