⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recur.c

📁 Wget很好的处理了http和ftp的下载,很值得学习的经典代码
💻 C
📖 第 1 页 / 共 2 页
字号:
            }          if (children)            {              struct urlpos *child = children;              struct url *url_parsed = url_parsed = url_parse (url, NULL);              char *referer_url = url;              bool strip_auth = (url_parsed != NULL                                 && url_parsed->user != NULL);              assert (url_parsed != NULL);              /* Strip auth info if present */              if (strip_auth)                referer_url = url_string (url_parsed, URL_AUTH_HIDE);              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 (referer_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);                    }                }              if (strip_auth)                xfree (referer_url);              url_free (url_parsed);              free_urlpos (children);            }        }      if (file           && (opt.delete_after               || opt.spider /* opt.recursive is implicitely true */              || !acceptable (file)))        {          /* Either --delete-after was specified, or we loaded this             (otherwise unneeded because of --spider or rejected by -R)              HTML file just to 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" :                   (opt.spider ? "--spider" :                     "recursive rejection criteria")));          logprintf (LOG_VERBOSE,                     (opt.delete_after || opt.spider                      ? _("Removing %s.\n")                      : _("Removing %s since it should be rejected.\n")),                     file);          if (unlink (file))            logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));          logputs (LOG_VERBOSE, "\n");          register_delete_file (file);        }      xfree (url);      xfree_null (referer);      xfree_null (file);    }  /* If anything is left of the queue due to a premature exit, free it     now.  */  {    char *d1, *d2;    int d3;    bool d4;    while (url_dequeue (queue,                        (const char **)&d1, (const char **)&d2, &d3, &d4))      {        xfree (d1);        xfree_null (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 booldownload_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;  bool u_scheme_like_http;  DEBUGP (("Deciding whether to enqueue \"%s\".\n", url));  if (string_set_contains (blacklist, url))    {      if (opt.spider)         {          char *referrer = url_string (parent, URL_AUTH_HIDE_PASSWD);          DEBUGP (("download_child_p: parent->url is: `%s'\n", parent->url));          visited_url (url, referrer);          xfree (referrer);        }      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 (!subdir_p (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))        {          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 non-leaf HTMLs,     which can lead to other files that do need to be downloaded.  (-p     automatically implies non-leaf because with -p we can, if     necesary, overstep the maximum depth to get the page requisites.)  */  if (u->file[0] != '\0'      && !(has_html_suffix_p (u->file)           /* The exception only applies to non-leaf HTMLs (but -p              always implies non-leaf because we can overstep the              maximum depth to get the requisites): */           && (/* non-leaf */               opt.reclevel == INFINITE_RECURSION               /* also non-leaf */               || depth < opt.reclevel - 1               /* -p, which implies non-leaf (see above) */               || opt.page_requisites)))    {      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 true; out:  DEBUGP (("Decided NOT to load it.\n"));  return false;}/* 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 booldescend_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;  bool success;  orig_parsed = url_parse (original, NULL);  assert (orig_parsed != NULL);  new_parsed = url_parse (redirected, NULL);  assert (new_parsed != NULL);  upos = xnew0 (struct urlpos);  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;}/* vim:set sts=2 sw=2 cino+={s: */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -