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

📄 recur.c

📁 一个从网络上自动下载文件的自由工具
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Handling of recursive HTTP retrieving.   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,   2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.This file is part of GNU Wget.GNU Wget is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 3 of the License, or (at your option) any later version.GNU Wget is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with Wget.  If not, see <http://www.gnu.org/licenses/>.Additional permission under GNU GPL version 3 section 7If you modify this program, or any covered work, by linking orcombining it with the OpenSSL project's OpenSSL library (or amodified version of that library), containing parts covered by theterms of the OpenSSL or SSLeay licenses, the Free Software Foundationgrants you additional permission to convey the resulting work.Corresponding Source for a non-source form of such a combinationshall include the source code for the parts of OpenSSL used as wellas that of the covered work.  */#include <config.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef HAVE_UNISTD_H# include <unistd.h>#endif /* HAVE_UNISTD_H */#include <errno.h>#include <assert.h>#include "wget.h"#include "url.h"#include "recur.h"#include "utils.h"#include "retr.h"#include "ftp.h"#include "host.h"#include "hash.h"#include "res.h"#include "convert.h"#include "spider.h"/* Functions for maintaining the URL queue.  */struct queue_element {  const char *url;              /* the URL to download */  const char *referer;          /* the referring document */  int depth;                    /* the depth */  bool html_allowed;            /* whether the document is allowed to                                   be treated as HTML. */  struct queue_element *next;   /* next element in queue */};struct url_queue {  struct queue_element *head;  struct queue_element *tail;  int count, maxcount;};/* Create a URL queue. */static struct url_queue *url_queue_new (void){  struct url_queue *queue = xnew0 (struct url_queue);  return queue;}/* Delete a URL queue. */static voidurl_queue_delete (struct url_queue *queue){  xfree (queue);}/* Enqueue a URL in the queue.  The queue is FIFO: the items will be   retrieved ("dequeued") from the queue in the order they were placed   into it.  */static voidurl_enqueue (struct url_queue *queue,             const char *url, const char *referer, int depth, bool html_allowed){  struct queue_element *qel = xnew (struct queue_element);  qel->url = url;  qel->referer = referer;  qel->depth = depth;  qel->html_allowed = html_allowed;  qel->next = NULL;  ++queue->count;  if (queue->count > queue->maxcount)    queue->maxcount = queue->count;  DEBUGP (("Enqueuing %s at depth %d\n", url, depth));  DEBUGP (("Queue count %d, maxcount %d.\n", queue->count, queue->maxcount));  if (queue->tail)    queue->tail->next = qel;  queue->tail = qel;  if (!queue->head)    queue->head = queue->tail;}/* Take a URL out of the queue.  Return true if this operation   succeeded, or false if the queue is empty.  */static boolurl_dequeue (struct url_queue *queue,             const char **url, const char **referer, int *depth,             bool *html_allowed){  struct queue_element *qel = queue->head;  if (!qel)    return false;  queue->head = queue->head->next;  if (!queue->head)    queue->tail = NULL;  *url = qel->url;  *referer = qel->referer;  *depth = qel->depth;  *html_allowed = qel->html_allowed;  --queue->count;  DEBUGP (("Dequeuing %s at depth %d\n", qel->url, qel->depth));  DEBUGP (("Queue count %d, maxcount %d.\n", queue->count, queue->maxcount));  xfree (qel);  return true;}static bool download_child_p (const struct urlpos *, struct url *, int,                              struct url *, struct hash_table *);static bool descend_redirect_p (const char *, const char *, int,                                struct url *, struct hash_table *);/* Retrieve a part of the web beginning with START_URL.  This used to   be called "recursive retrieval", because the old function was   recursive and implemented depth-first search.  retrieve_tree on the   other hand implements breadth-search traversal of the tree, which   results in much nicer ordering of downloads.   The algorithm this function uses is simple:   1. put START_URL in the queue.   2. while there are URLs in the queue:     3. get next URL from the queue.     4. download it.     5. if the URL is HTML and its depth does not exceed maximum depth,        get the list of URLs embedded therein.     6. for each of those URLs do the following:       7. if the URL is not one of those downloaded before, and if it          satisfies the criteria specified by the various command-line          options, add it to the queue. */uerr_tretrieve_tree (const char *start_url){  uerr_t status = RETROK;  /* The queue of URLs we need to load. */  struct url_queue *queue;  /* The URLs we do not wish to enqueue, because they are already in     the queue, but haven't been downloaded yet.  */  struct hash_table *blacklist;  int up_error_code;  struct url *start_url_parsed = url_parse (start_url, &up_error_code);  if (!start_url_parsed)    {      logprintf (LOG_NOTQUIET, "%s: %s.\n", start_url,                 url_error (up_error_code));      return URLERROR;    }  queue = url_queue_new ();  blacklist = make_string_hash_table (0);  /* Enqueue the starting URL.  Use start_url_parsed->url rather than     just URL so we enqueue the canonical form of the URL.  */  url_enqueue (queue, xstrdup (start_url_parsed->url), NULL, 0, true);  string_set_add (blacklist, start_url_parsed->url);  while (1)    {      bool descend = false;      char *url, *referer, *file = NULL;      int depth;      bool html_allowed;      bool dash_p_leaf_HTML = false;      if (opt.quota && total_downloaded_bytes > opt.quota)        break;      if (status == FWRITEERR)        break;      /* Get the next URL from the queue... */      if (!url_dequeue (queue,                        (const char **)&url, (const char **)&referer,                        &depth, &html_allowed))        break;      /* ...and download it.  Note that this download is in most cases         unconditional, as download_child_p already makes sure a file         doesn't get enqueued twice -- and yet this check is here, and         not in download_child_p.  This is so that if you run `wget -r         URL1 URL2', and a random URL is encountered once under URL1         and again under URL2, but at a different (possibly smaller)         depth, we want the URL's children to be taken into account         the second time.  */      if (dl_url_file_map && hash_table_contains (dl_url_file_map, url))        {          file = xstrdup (hash_table_get (dl_url_file_map, url));          DEBUGP (("Already downloaded \"%s\", reusing it from \"%s\".\n",                   url, file));          if (html_allowed              && downloaded_html_set              && string_set_contains (downloaded_html_set, file))            descend = true;        }      else        {          int dt = 0;          char *redirected = NULL;          status = retrieve_url (url, &file, &redirected, referer, &dt, false);          if (html_allowed && file && status == RETROK              && (dt & RETROKF) && (dt & TEXTHTML))            descend = true;          if (redirected)            {              /* We have been redirected, possibly to another host, or                 different path, or wherever.  Check whether we really                 want to follow it.  */              if (descend)                {                  if (!descend_redirect_p (redirected, url, depth,                                           start_url_parsed, blacklist))                    descend = false;                  else                    /* Make sure that the old pre-redirect form gets                       blacklisted. */                    string_set_add (blacklist, url);                }              xfree (url);              url = redirected;            }        }      if (opt.spider)        {          visited_url (url, referer);        }      if (descend          && depth >= opt.reclevel && opt.reclevel != INFINITE_RECURSION)        {          if (opt.page_requisites              && (depth == opt.reclevel || depth == opt.reclevel + 1))            {              /* When -p is specified, we are allowed to exceed the                 maximum depth, but only for the "inline" links,                 i.e. those that are needed to display the page.                 Originally this could exceed the depth at most by                 one, but we allow one more level so that the leaf                 pages that contain frames can be loaded                 correctly.  */              dash_p_leaf_HTML = true;            }          else            {              /* Either -p wasn't specified or it was and we've                 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 = false;            }        }      /* If the downloaded document was HTML, parse it and enqueue the         links it contains. */      if (descend)        {          bool meta_disallow_follow = false;          struct urlpos *children            = get_urls_html (file, url, &meta_disallow_follow);          if (opt.use_robots && meta_disallow_follow)            {              free_urlpos (children);              children = NULL;

⌨️ 快捷键说明

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