📄 recur.c
字号:
/* Handling of recursive HTTP retrieving. Copyright (C) 1995, 1996, 1997, 2000, 2001 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 2 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, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.In addition, as a special exception, the Free Software Foundationgives permission to link the code of its release of Wget with theOpenSSL project's "OpenSSL" library (or with modified versions of itthat use the same license as the "OpenSSL" library), and distributethe linked executables. You must obey the GNU General Public Licensein all respects for all of the code used other than "OpenSSL". If youmodify this file, you may extend this exception to your version of thefile, but you are not obligated to do so. If you do not wish to doso, delete this exception statement from your version. */#include <config.h>#include <stdio.h>#include <stdlib.h>#ifdef HAVE_STRING_H# include <string.h>#else# include <strings.h>#endif /* HAVE_STRING_H */#ifdef HAVE_UNISTD_H# include <unistd.h>#endif /* HAVE_UNISTD_H */#include <errno.h>#include <assert.h>#include <sys/types.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"#ifndef errnoextern int errno;#endifextern char *version_string;extern LARGE_INT total_downloaded_bytes;extern struct hash_table *dl_url_file_map;extern struct hash_table *downloaded_html_set;/* 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 */ unsigned int html_allowed :1; /* 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 = xmalloc (sizeof (*queue)); memset (queue, '\0', sizeof (*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, int html_allowed){ struct queue_element *qel = xmalloc (sizeof (*qel)); 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 1 if this operation succeeded, or 0 if the queue is empty. */static inturl_dequeue (struct url_queue *queue, const char **url, const char **referer, int *depth, int *html_allowed){ struct queue_element *qel = queue->head; if (!qel) return 0; 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 1;}static int download_child_p PARAMS ((const struct urlpos *, struct url *, int, struct url *, struct hash_table *));static int descend_redirect_p PARAMS ((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, 1); string_set_add (blacklist, start_url_parsed->url); while (1) { int descend = 0; char *url, *referer, *file = NULL; int depth, html_allowed; boolean 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 = 1; } else { int dt = 0; char *redirected = NULL; int oldrec = opt.recursive; opt.recursive = 0; status = retrieve_url (url, &file, &redirected, referer, &dt); opt.recursive = oldrec; if (html_allowed && file && status == RETROK && (dt & RETROKF) && (dt & TEXTHTML)) descend = 1; 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 = 0; else /* Make sure that the old pre-redirect form gets blacklisted. */ string_set_add (blacklist, url); } xfree (url); url = redirected; } } 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -