📄 convert.c
字号:
/* Conversion of links to local files. Copyright (C) 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 "convert.h"#include "url.h"#include "recur.h"#include "utils.h"#include "hash.h"#include "ptimer.h"#include "res.h"static struct hash_table *dl_file_url_map;struct hash_table *dl_url_file_map;/* Set of HTML files downloaded in this Wget run, used for link conversion after Wget is done. */struct hash_table *downloaded_html_set;static void convert_links (const char *, struct urlpos *);/* This function is called when the retrieval is done to convert the links that have been downloaded. It has to be called at the end of the retrieval, because only then does Wget know conclusively which URLs have been downloaded, and which not, so it can tell which direction to convert to. The "direction" means that the URLs to the files that have been downloaded get converted to the relative URL which will point to that file. And the other URLs get converted to the remote URL on the server. All the downloaded HTMLs are kept in downloaded_html_files, and downloaded URLs in urls_downloaded. All the information is extracted from these two lists. */voidconvert_all_links (void){ int i; double secs; int file_count = 0; struct ptimer *timer = ptimer_new (); int cnt; char **file_array; cnt = 0; if (downloaded_html_set) cnt = hash_table_count (downloaded_html_set); if (cnt == 0) goto cleanup; file_array = alloca_array (char *, cnt); string_set_to_array (downloaded_html_set, file_array); for (i = 0; i < cnt; i++) { struct urlpos *urls, *cur_url; char *url; char *file = file_array[i]; /* Determine the URL of the HTML file. get_urls_html will need it. */ url = hash_table_get (dl_file_url_map, file); if (!url) { DEBUGP (("Apparently %s has been removed.\n", file)); continue; } DEBUGP (("Scanning %s (from %s)\n", file, url)); /* Parse the HTML file... */ urls = get_urls_html (file, url, NULL); /* We don't respect meta_disallow_follow here because, even if the file is not followed, we might still want to convert the links that have been followed from other files. */ for (cur_url = urls; cur_url; cur_url = cur_url->next) { char *local_name; struct url *u = cur_url->url; if (cur_url->link_base_p) { /* Base references have been resolved by our parser, so we turn the base URL into an empty string. (Perhaps we should remove the tag entirely?) */ cur_url->convert = CO_NULLIFY_BASE; continue; } /* We decide the direction of conversion according to whether a URL was downloaded. Downloaded URLs will be converted ABS2REL, whereas non-downloaded will be converted REL2ABS. */ local_name = hash_table_get (dl_url_file_map, u->url); /* Decide on the conversion type. */ if (local_name) { /* We've downloaded this URL. Convert it to relative form. We do this even if the URL already is in relative form, because our directory structure may not be identical to that on the server (think `-nd', `--cut-dirs', etc.) */ cur_url->convert = CO_CONVERT_TO_RELATIVE; cur_url->local_name = xstrdup (local_name); DEBUGP (("will convert url %s to local %s\n", u->url, local_name)); } else { /* We haven't downloaded this URL. If it's not already complete (including a full host name), convert it to that form, so it can be reached while browsing this HTML locally. */ if (!cur_url->link_complete_p) cur_url->convert = CO_CONVERT_TO_COMPLETE; cur_url->local_name = NULL; DEBUGP (("will convert url %s to complete\n", u->url)); } } /* Convert the links in the file. */ convert_links (file, urls); ++file_count; /* Free the data. */ free_urlpos (urls); } secs = ptimer_measure (timer); logprintf (LOG_VERBOSE, _("Converted %d files in %s seconds.\n"), file_count, print_decimal (secs));cleanup: ptimer_destroy (timer);}static void write_backup_file (const char *, downloaded_file_t);static const char *replace_attr (const char *, int, FILE *, const char *);static const char *replace_attr_refresh_hack (const char *, int, FILE *, const char *, int);static char *local_quote_string (const char *);static char *construct_relative (const char *, const char *);/* Change the links in one HTML file. LINKS is a list of links in the document, along with their positions and the desired direction of the conversion. */static voidconvert_links (const char *file, struct urlpos *links){ struct file_memory *fm; FILE *fp; const char *p; downloaded_file_t downloaded_file_return; struct urlpos *link; int to_url_count = 0, to_file_count = 0; logprintf (LOG_VERBOSE, _("Converting %s... "), file); { /* First we do a "dry run": go through the list L and see whether any URL needs to be converted in the first place. If not, just leave the file alone. */ int dry_count = 0; struct urlpos *dry; for (dry = links; dry; dry = dry->next) if (dry->convert != CO_NOCONVERT) ++dry_count; if (!dry_count) { logputs (LOG_VERBOSE, _("nothing to do.\n")); return; } } fm = read_file (file); if (!fm) { logprintf (LOG_NOTQUIET, _("Cannot convert links in %s: %s\n"), file, strerror (errno)); return; } downloaded_file_return = downloaded_file (CHECK_FOR_FILE, file); if (opt.backup_converted && downloaded_file_return) write_backup_file (file, downloaded_file_return); /* Before opening the file for writing, unlink the file. This is important if the data in FM is mmaped. In such case, nulling the file, which is what fopen() below does, would make us read all zeroes from the mmaped region. */ if (unlink (file) < 0 && errno != ENOENT) { logprintf (LOG_NOTQUIET, _("Unable to delete `%s': %s\n"), file, strerror (errno)); read_file_free (fm); return; } /* Now open the file for writing. */ fp = fopen (file, "wb"); if (!fp) { logprintf (LOG_NOTQUIET, _("Cannot convert links in %s: %s\n"), file, strerror (errno)); read_file_free (fm); return; } /* Here we loop through all the URLs in file, replacing those of them that are downloaded with relative references. */ p = fm->content; for (link = links; link; link = link->next) { char *url_start = fm->content + link->pos; if (link->pos >= fm->length) { DEBUGP (("Something strange is going on. Please investigate.")); break; } /* If the URL is not to be converted, skip it. */ if (link->convert == CO_NOCONVERT) { DEBUGP (("Skipping %s at position %d.\n", link->url->url, link->pos)); continue; } /* Echo the file contents, up to the offending URL's opening quote, to the outfile. */ fwrite (p, 1, url_start - p, fp); p = url_start; switch (link->convert) { case CO_CONVERT_TO_RELATIVE: /* Convert absolute URL to relative. */ { char *newname = construct_relative (file, link->local_name); char *quoted_newname = local_quote_string (newname); if (!link->link_refresh_p) p = replace_attr (p, link->size, fp, quoted_newname); else p = replace_attr_refresh_hack (p, link->size, fp, quoted_newname, link->refresh_timeout); DEBUGP (("TO_RELATIVE: %s to %s at position %d in %s.\n", link->url->url, newname, link->pos, file)); xfree (newname); xfree (quoted_newname); ++to_file_count; break; } case CO_CONVERT_TO_COMPLETE: /* Convert the link to absolute URL. */ { char *newlink = link->url->url; char *quoted_newlink = html_quote_string (newlink); if (!link->link_refresh_p) p = replace_attr (p, link->size, fp, quoted_newlink); else p = replace_attr_refresh_hack (p, link->size, fp, quoted_newlink, link->refresh_timeout); DEBUGP (("TO_COMPLETE: <something> to %s at position %d in %s.\n", newlink, link->pos, file)); xfree (quoted_newlink); ++to_url_count; break; } case CO_NULLIFY_BASE: /* Change the base href to "". */ p = replace_attr (p, link->size, fp, ""); break; case CO_NOCONVERT: abort (); break; } } /* Output the rest of the file. */ if (p - fm->content < fm->length) fwrite (p, 1, fm->length - (p - fm->content), fp); fclose (fp); read_file_free (fm); logprintf (LOG_VERBOSE, "%d-%d\n", to_file_count, to_url_count);}/* Construct and return a link that points from BASEFILE to LINKFILE. Both files should be local file names, BASEFILE of the referrering file, and LINKFILE of the referred file. Examples: cr("foo", "bar") -> "bar" cr("A/foo", "A/bar") -> "bar" cr("A/foo", "A/B/bar") -> "B/bar" cr("A/X/foo", "A/Y/bar") -> "../Y/bar" cr("X/", "Y/bar") -> "../Y/bar" (trailing slash does matter in BASE) Both files should be absolute or relative, otherwise strange results might ensue. The function makes no special efforts to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -