]> sjero.net Git - wget/blob - src/recur.c
[svn] Descend into HTML files we've already downloaded.
[wget] / src / recur.c
1 /* Handling of recursive HTTP retrieving.
2    Copyright (C) 1995, 1996, 1997, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GNU Wget.
5
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #ifdef HAVE_STRING_H
25 # include <string.h>
26 #else
27 # include <strings.h>
28 #endif /* HAVE_STRING_H */
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif /* HAVE_UNISTD_H */
32 #include <errno.h>
33 #include <assert.h>
34 #include <sys/types.h>
35
36 #include "wget.h"
37 #include "url.h"
38 #include "recur.h"
39 #include "utils.h"
40 #include "retr.h"
41 #include "ftp.h"
42 #include "fnmatch.h"
43 #include "host.h"
44 #include "hash.h"
45 #include "res.h"
46
47 #ifndef errno
48 extern int errno;
49 #endif
50
51 extern char *version_string;
52
53 static struct hash_table *dl_file_url_map;
54 static struct hash_table *dl_url_file_map;
55
56 /* List of HTML files downloaded in this Wget run, used for link
57    conversion after Wget is done.  The list and the set contain the
58    same information, except the list maintains the order.  Perhaps I
59    should get rid of the list, it's there for historical reasons.  */
60 static slist *downloaded_html_list;
61 static struct hash_table *downloaded_html_set;
62
63 static void register_delete_file PARAMS ((const char *));
64 \f
65 /* Functions for maintaining the URL queue.  */
66
67 struct queue_element {
68   const char *url;
69   const char *referer;
70   int depth;
71   struct queue_element *next;
72 };
73
74 struct url_queue {
75   struct queue_element *head;
76   struct queue_element *tail;
77   int count, maxcount;
78 };
79
80 /* Create a URL queue. */
81
82 static struct url_queue *
83 url_queue_new (void)
84 {
85   struct url_queue *queue = xmalloc (sizeof (*queue));
86   memset (queue, '\0', sizeof (*queue));
87   return queue;
88 }
89
90 /* Delete a URL queue. */
91
92 static void
93 url_queue_delete (struct url_queue *queue)
94 {
95   xfree (queue);
96 }
97
98 /* Enqueue a URL in the queue.  The queue is FIFO: the items will be
99    retrieved ("dequeued") from the queue in the order they were placed
100    into it.  */
101
102 static void
103 url_enqueue (struct url_queue *queue,
104              const char *url, const char *referer, int depth)
105 {
106   struct queue_element *qel = xmalloc (sizeof (*qel));
107   qel->url = url;
108   qel->referer = referer;
109   qel->depth = depth;
110   qel->next = NULL;
111
112   ++queue->count;
113   if (queue->count > queue->maxcount)
114     queue->maxcount = queue->count;
115
116   DEBUGP (("Enqueuing %s at depth %d\n", url, depth));
117   DEBUGP (("Queue count %d, maxcount %d.\n", queue->count, queue->maxcount));
118
119   if (queue->tail)
120     queue->tail->next = qel;
121   queue->tail = qel;
122
123   if (!queue->head)
124     queue->head = queue->tail;
125 }
126
127 /* Take a URL out of the queue.  Return 1 if this operation succeeded,
128    or 0 if the queue is empty.  */
129
130 static int
131 url_dequeue (struct url_queue *queue,
132              const char **url, const char **referer, int *depth)
133 {
134   struct queue_element *qel = queue->head;
135
136   if (!qel)
137     return 0;
138
139   queue->head = queue->head->next;
140   if (!queue->head)
141     queue->tail = NULL;
142
143   *url = qel->url;
144   *referer = qel->referer;
145   *depth = qel->depth;
146
147   --queue->count;
148
149   DEBUGP (("Dequeuing %s at depth %d\n", qel->url, qel->depth));
150   DEBUGP (("Queue count %d, maxcount %d.\n", queue->count, queue->maxcount));
151
152   xfree (qel);
153   return 1;
154 }
155 \f
156 static int download_child_p PARAMS ((const struct urlpos *, struct url *, int,
157                                      struct url *, struct hash_table *));
158 static int descend_redirect_p PARAMS ((const char *, const char *, int,
159                                        struct url *, struct hash_table *));
160
161
162 /* Retrieve a part of the web beginning with START_URL.  This used to
163    be called "recursive retrieval", because the old function was
164    recursive and implemented depth-first search.  retrieve_tree on the
165    other hand implements breadth-search traversal of the tree, which
166    results in much nicer ordering of downloads.
167
168    The algorithm this function uses is simple:
169
170    1. put START_URL in the queue.
171    2. while there are URLs in the queue:
172
173      3. get next URL from the queue.
174      4. download it.
175      5. if the URL is HTML and its depth does not exceed maximum depth,
176         get the list of URLs embedded therein.
177      6. for each of those URLs do the following:
178
179        7. if the URL is not one of those downloaded before, and if it
180           satisfies the criteria specified by the various command-line
181           options, add it to the queue. */
182
183 uerr_t
184 retrieve_tree (const char *start_url)
185 {
186   uerr_t status = RETROK;
187
188   /* The queue of URLs we need to load. */
189   struct url_queue *queue = url_queue_new ();
190
191   /* The URLs we do not wish to enqueue, because they are already in
192      the queue, but haven't been downloaded yet.  */
193   struct hash_table *blacklist = make_string_hash_table (0);
194
195   /* We'll need various components of this, so better get it over with
196      now. */
197   struct url *start_url_parsed = url_parse (start_url, NULL);
198
199   url_enqueue (queue, xstrdup (start_url), NULL, 0);
200   string_set_add (blacklist, start_url);
201
202   while (1)
203     {
204       int descend = 0;
205       char *url, *referer, *file = NULL;
206       int depth;
207       boolean dash_p_leaf_HTML = FALSE;
208
209       if (downloaded_exceeds_quota ())
210         break;
211       if (status == FWRITEERR)
212         break;
213
214       /* Get the next URL from the queue... */
215
216       if (!url_dequeue (queue,
217                         (const char **)&url, (const char **)&referer,
218                         &depth))
219         break;
220
221       /* ...and download it.  Note that this download is in most cases
222          unconditional, as download_child_p already makes sure a file
223          doesn't get enqueued twice -- and yet this check is here, and
224          not in download_child_p.  This is so that if you run `wget -r
225          URL1 URL2', and a random URL is encountered once under URL1
226          and again under URL2, but at a different (possibly smaller)
227          depth, we want the URL's children to be taken into account
228          the second time.  */
229       if (dl_url_file_map && hash_table_contains (dl_url_file_map, url))
230         {
231           file = hash_table_get (dl_url_file_map, url);
232
233           DEBUGP (("Already downloaded \"%s\", reusing it from \"%s\".\n",
234                    url, file));
235
236           /* #### This check might be horribly slow when downloading
237              sites with a huge number of HTML docs.  Use a hash table
238              instead!  Thankfully, it gets tripped only when you use
239              `wget -r URL1 URL2 ...', as explained above.  */
240
241           if (string_set_contains (downloaded_html_set, file))
242             descend = 1;
243         }
244       else
245         {
246           int dt = 0;
247           char *redirected = NULL;
248           int oldrec = opt.recursive;
249
250           opt.recursive = 0;
251           status = retrieve_url (url, &file, &redirected, referer, &dt);
252           opt.recursive = oldrec;
253
254           if (file && status == RETROK
255               && (dt & RETROKF) && (dt & TEXTHTML))
256             descend = 1;
257
258           if (redirected)
259             {
260               /* We have been redirected, possibly to another host, or
261                  different path, or wherever.  Check whether we really
262                  want to follow it.  */
263               if (descend)
264                 {
265                   if (!descend_redirect_p (redirected, url, depth,
266                                            start_url_parsed, blacklist))
267                     descend = 0;
268                   else
269                     /* Make sure that the old pre-redirect form gets
270                        blacklisted. */
271                     string_set_add (blacklist, url);
272                 }
273
274               xfree (url);
275               url = redirected;
276             }
277         }
278
279       if (descend
280           && depth >= opt.reclevel && opt.reclevel != INFINITE_RECURSION)
281         {
282           if (opt.page_requisites
283               && (depth == opt.reclevel || depth == opt.reclevel + 1))
284             {
285               /* When -p is specified, we are allowed to exceed the
286                  maximum depth, but only for the "inline" links,
287                  i.e. those that are needed to display the page.
288                  Originally this could exceed the depth at most by
289                  one, but we allow one more level so that the leaf
290                  pages that contain frames can be loaded
291                  correctly.  */
292               dash_p_leaf_HTML = TRUE;
293             }
294           else
295             {
296               /* Either -p wasn't specified or it was and we've
297                  already spent the two extra (pseudo-)levels that it
298                  affords us, so we need to bail out. */
299               DEBUGP (("Not descending further; at depth %d, max. %d.\n",
300                        depth, opt.reclevel));
301               descend = 0;
302             }
303         }
304
305       /* If the downloaded document was HTML, parse it and enqueue the
306          links it contains. */
307
308       if (descend)
309         {
310           int meta_disallow_follow = 0;
311           struct urlpos *children
312             = get_urls_html (file, url, &meta_disallow_follow);
313
314           if (opt.use_robots && meta_disallow_follow)
315             {
316               free_urlpos (children);
317               children = NULL;
318             }
319
320           if (children)
321             {
322               struct urlpos *child = children;
323               struct url *url_parsed = url_parsed = url_parse (url, NULL);
324               assert (url_parsed != NULL);
325
326               for (; child; child = child->next)
327                 {
328                   if (child->ignore_when_downloading)
329                     continue;
330                   if (dash_p_leaf_HTML && !child->link_inline_p)
331                     continue;
332                   if (download_child_p (child, url_parsed, depth, start_url_parsed,
333                                         blacklist))
334                     {
335                       url_enqueue (queue, xstrdup (child->url->url),
336                                    xstrdup (url), depth + 1);
337                       /* We blacklist the URL we have enqueued, because we
338                          don't want to enqueue (and hence download) the
339                          same URL twice.  */
340                       string_set_add (blacklist, child->url->url);
341                     }
342                 }
343
344               url_free (url_parsed);
345               free_urlpos (children);
346             }
347         }
348
349       if (opt.delete_after || (file && !acceptable (file)))
350         {
351           /* Either --delete-after was specified, or we loaded this
352              otherwise rejected (e.g. by -R) HTML file just so we
353              could harvest its hyperlinks -- in either case, delete
354              the local file. */
355           DEBUGP (("Removing file due to %s in recursive_retrieve():\n",
356                    opt.delete_after ? "--delete-after" :
357                    "recursive rejection criteria"));
358           logprintf (LOG_VERBOSE,
359                      (opt.delete_after
360                       ? _("Removing %s.\n")
361                       : _("Removing %s since it should be rejected.\n")),
362                      file);
363           if (unlink (file))
364             logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));
365           register_delete_file (file);
366         }
367
368       xfree (url);
369       FREE_MAYBE (referer);
370       FREE_MAYBE (file);
371     }
372
373   /* If anything is left of the queue due to a premature exit, free it
374      now.  */
375   {
376     char *d1, *d2;
377     int d3;
378     while (url_dequeue (queue, (const char **)&d1, (const char **)&d2, &d3))
379       {
380         xfree (d1);
381         FREE_MAYBE (d2);
382       }
383   }
384   url_queue_delete (queue);
385
386   if (start_url_parsed)
387     url_free (start_url_parsed);
388   string_set_free (blacklist);
389
390   if (downloaded_exceeds_quota ())
391     return QUOTEXC;
392   else if (status == FWRITEERR)
393     return FWRITEERR;
394   else
395     return RETROK;
396 }
397
398 /* Based on the context provided by retrieve_tree, decide whether a
399    URL is to be descended to.  This is only ever called from
400    retrieve_tree, but is in a separate function for clarity.
401
402    The most expensive checks (such as those for robots) are memoized
403    by storing these URLs to BLACKLIST.  This may or may not help.  It
404    will help if those URLs are encountered many times.  */
405
406 static int
407 download_child_p (const struct urlpos *upos, struct url *parent, int depth,
408                   struct url *start_url_parsed, struct hash_table *blacklist)
409 {
410   struct url *u = upos->url;
411   const char *url = u->url;
412
413   DEBUGP (("Deciding whether to enqueue \"%s\".\n", url));
414
415   if (string_set_contains (blacklist, url))
416     {
417       DEBUGP (("Already on the black list.\n"));
418       goto out;
419     }
420
421   /* Several things to check for:
422      1. if scheme is not http, and we don't load it
423      2. check for relative links (if relative_only is set)
424      3. check for domain
425      4. check for no-parent
426      5. check for excludes && includes
427      6. check for suffix
428      7. check for same host (if spanhost is unset), with possible
429      gethostbyname baggage
430      8. check for robots.txt
431
432      Addendum: If the URL is FTP, and it is to be loaded, only the
433      domain and suffix settings are "stronger".
434
435      Note that .html files will get loaded regardless of suffix rules
436      (but that is remedied later with unlink) unless the depth equals
437      the maximum depth.
438
439      More time- and memory- consuming tests should be put later on
440      the list.  */
441
442   /* 1. Schemes other than HTTP are normally not recursed into. */
443   if (u->scheme != SCHEME_HTTP
444       && !(u->scheme == SCHEME_FTP && opt.follow_ftp))
445     {
446       DEBUGP (("Not following non-HTTP schemes.\n"));
447       goto out;
448     }
449
450   /* 2. If it is an absolute link and they are not followed, throw it
451      out.  */
452   if (u->scheme == SCHEME_HTTP)
453     if (opt.relative_only && !upos->link_relative_p)
454       {
455         DEBUGP (("It doesn't really look like a relative link.\n"));
456         goto out;
457       }
458
459   /* 3. If its domain is not to be accepted/looked-up, chuck it
460      out.  */
461   if (!accept_domain (u))
462     {
463       DEBUGP (("The domain was not accepted.\n"));
464       goto out;
465     }
466
467   /* 4. Check for parent directory.
468
469      If we descended to a different host or changed the scheme, ignore
470      opt.no_parent.  Also ignore it for documents needed to display
471      the parent page when in -p mode.  */
472   if (opt.no_parent
473       && u->scheme == start_url_parsed->scheme
474       && 0 == strcasecmp (u->host, start_url_parsed->host)
475       && u->port == start_url_parsed->port
476       && !(opt.page_requisites && upos->link_inline_p))
477     {
478       if (!frontcmp (start_url_parsed->dir, u->dir))
479         {
480           DEBUGP (("Going to \"%s\" would escape \"%s\" with no_parent on.\n",
481                    u->dir, start_url_parsed->dir));
482           goto out;
483         }
484     }
485
486   /* 5. If the file does not match the acceptance list, or is on the
487      rejection list, chuck it out.  The same goes for the directory
488      exclusion and inclusion lists.  */
489   if (opt.includes || opt.excludes)
490     {
491       if (!accdir (u->dir, ALLABS))
492         {
493           DEBUGP (("%s (%s) is excluded/not-included.\n", url, u->dir));
494           goto out;
495         }
496     }
497
498   /* 6. */
499   {
500     char *suf;
501     /* Check for acceptance/rejection rules.  We ignore these rules
502        for HTML documents because they might lead to other files which
503        need to be downloaded.  Of course, we don't know which
504        documents are HTML before downloading them, so we guess.
505
506        A file is subject to acceptance/rejection rules if:
507
508        * u->file is not "" (i.e. it is not a directory)
509        and either:
510          + there is no file suffix,
511          + or there is a suffix, but is not "html" or "htm",
512          + both:
513            - recursion is not infinite,
514            - and we are at its very end. */
515
516     if (u->file[0] != '\0'
517         && ((suf = suffix (url)) == NULL
518             || (0 != strcmp (suf, "html") && 0 != strcmp (suf, "htm"))
519             || (opt.reclevel != INFINITE_RECURSION && depth >= opt.reclevel)))
520       {
521         if (!acceptable (u->file))
522           {
523             DEBUGP (("%s (%s) does not match acc/rej rules.\n",
524                      url, u->file));
525             goto out;
526           }
527       }
528   }
529
530   /* 7. */
531   if (u->scheme == parent->scheme)
532     if (!opt.spanhost && 0 != strcasecmp (parent->host, u->host))
533       {
534         DEBUGP (("This is not the same hostname as the parent's (%s and %s).\n",
535                  u->host, parent->host));
536         goto out;
537       }
538
539   /* 8. */
540   if (opt.use_robots && u->scheme == SCHEME_HTTP)
541     {
542       struct robot_specs *specs = res_get_specs (u->host, u->port);
543       if (!specs)
544         {
545           char *rfile;
546           if (res_retrieve_file (url, &rfile))
547             {
548               specs = res_parse_from_file (rfile);
549               xfree (rfile);
550             }
551           else
552             {
553               /* If we cannot get real specs, at least produce
554                  dummy ones so that we can register them and stop
555                  trying to retrieve them.  */
556               specs = res_parse ("", 0);
557             }
558           res_register_specs (u->host, u->port, specs);
559         }
560
561       /* Now that we have (or don't have) robots.txt specs, we can
562          check what they say.  */
563       if (!res_match_path (specs, u->path))
564         {
565           DEBUGP (("Not following %s because robots.txt forbids it.\n", url));
566           string_set_add (blacklist, url);
567           goto out;
568         }
569     }
570
571   /* The URL has passed all the tests.  It can be placed in the
572      download queue. */
573   DEBUGP (("Decided to load it.\n"));
574
575   return 1;
576
577  out:
578   DEBUGP (("Decided NOT to load it.\n"));
579
580   return 0;
581 }
582
583 /* This function determines whether we will consider downloading the
584    children of a URL whose download resulted in a redirection,
585    possibly to another host, etc.  It is needed very rarely, and thus
586    it is merely a simple-minded wrapper around download_child_p.  */
587
588 static int
589 descend_redirect_p (const char *redirected, const char *original, int depth,
590                     struct url *start_url_parsed, struct hash_table *blacklist)
591 {
592   struct url *orig_parsed, *new_parsed;
593   struct urlpos *upos;
594   int success;
595
596   orig_parsed = url_parse (original, NULL);
597   assert (orig_parsed != NULL);
598
599   new_parsed = url_parse (redirected, NULL);
600   assert (new_parsed != NULL);
601
602   upos = xmalloc (sizeof (struct urlpos));
603   memset (upos, 0, sizeof (*upos));
604   upos->url = new_parsed;
605
606   success = download_child_p (upos, orig_parsed, depth,
607                               start_url_parsed, blacklist);
608
609   url_free (orig_parsed);
610   url_free (new_parsed);
611   xfree (upos);
612
613   if (!success)
614     DEBUGP (("Redirection \"%s\" failed the test.\n", redirected));
615
616   return success;
617 }
618
619 \f
620 #define ENSURE_TABLES_EXIST do {                        \
621   if (!dl_file_url_map)                                 \
622     dl_file_url_map = make_string_hash_table (0);       \
623   if (!dl_url_file_map)                                 \
624     dl_url_file_map = make_string_hash_table (0);       \
625 } while (0)
626
627 /* Return 1 if S1 and S2 are the same, except for "/index.html".  The
628    three cases in which it returns one are (substitute any substring
629    for "foo"):
630
631    m("foo/index.html", "foo/")  ==> 1
632    m("foo/", "foo/index.html")  ==> 1
633    m("foo", "foo/index.html")   ==> 1
634    m("foo", "foo/"              ==> 1
635    m("foo", "foo")              ==> 1  */
636
637 static int
638 match_except_index (const char *s1, const char *s2)
639 {
640   int i;
641   const char *lng;
642
643   /* Skip common substring. */
644   for (i = 0; *s1 && *s2 && *s1 == *s2; s1++, s2++, i++)
645     ;
646   if (i == 0)
647     /* Strings differ at the very beginning -- bail out.  We need to
648        check this explicitly to avoid `lng - 1' reading outside the
649        array.  */
650     return 0;
651
652   if (!*s1 && !*s2)
653     /* Both strings hit EOF -- strings are equal. */
654     return 1;
655   else if (*s1 && *s2)
656     /* Strings are randomly different, e.g. "/foo/bar" and "/foo/qux". */
657     return 0;
658   else if (*s1)
659     /* S1 is the longer one. */
660     lng = s1;
661   else
662     /* S2 is the longer one. */
663     lng = s2;
664
665   /* foo            */            /* foo/           */
666   /* foo/index.html */  /* or */  /* foo/index.html */
667   /*    ^           */            /*     ^          */
668
669   if (*lng != '/')
670     /* The right-hand case. */
671     --lng;
672
673   if (*lng == '/' && *(lng + 1) == '\0')
674     /* foo  */
675     /* foo/ */
676     return 1;
677
678   return 0 == strcmp (lng, "/index.html");
679 }
680
681 static int
682 dissociate_urls_from_file_mapper (void *key, void *value, void *arg)
683 {
684   char *mapping_url = (char *)key;
685   char *mapping_file = (char *)value;
686   char *file = (char *)arg;
687
688   if (0 == strcmp (mapping_file, file))
689     {
690       hash_table_remove (dl_url_file_map, mapping_url);
691       xfree (mapping_url);
692       xfree (mapping_file);
693     }
694
695   /* Continue mapping. */
696   return 0;
697 }
698
699 /* Remove all associations from various URLs to FILE from dl_url_file_map. */
700
701 static void
702 dissociate_urls_from_file (const char *file)
703 {
704   hash_table_map (dl_url_file_map, dissociate_urls_from_file_mapper,
705                   (char *)file);
706 }
707
708 /* Register that URL has been successfully downloaded to FILE.  This
709    is used by the link conversion code to convert references to URLs
710    to references to local files.  It is also being used to check if a
711    URL has already been downloaded.  */
712
713 void
714 register_download (const char *url, const char *file)
715 {
716   char *old_file, *old_url;
717
718   ENSURE_TABLES_EXIST;
719
720   /* With some forms of retrieval, it is possible, although not likely
721      or particularly desirable.  If both are downloaded, the second
722      download will override the first one.  When that happens,
723      dissociate the old file name from the URL.  */
724
725   if (hash_table_get_pair (dl_file_url_map, file, &old_file, &old_url))
726     {
727       if (0 == strcmp (url, old_url))
728         /* We have somehow managed to download the same URL twice.
729            Nothing to do.  */
730         return;
731
732       if (match_except_index (url, old_url)
733           && !hash_table_contains (dl_url_file_map, url))
734         /* The two URLs differ only in the "index.html" ending.  For
735            example, one is "http://www.server.com/", and the other is
736            "http://www.server.com/index.html".  Don't remove the old
737            one, just add the new one as a non-canonical entry.  */
738         goto url_only;
739
740       hash_table_remove (dl_file_url_map, file);
741       xfree (old_file);
742       xfree (old_url);
743
744       /* Remove all the URLs that point to this file.  Yes, there can
745          be more than one such URL, because we store redirections as
746          multiple entries in dl_url_file_map.  For example, if URL1
747          redirects to URL2 which gets downloaded to FILE, we map both
748          URL1 and URL2 to FILE in dl_url_file_map.  (dl_file_url_map
749          only points to URL2.)  When another URL gets loaded to FILE,
750          we want both URL1 and URL2 dissociated from it.
751
752          This is a relatively expensive operation because it performs
753          a linear search of the whole hash table, but it should be
754          called very rarely, only when two URLs resolve to the same
755          file name, *and* the "<file>.1" extensions are turned off.
756          In other words, almost never.  */
757       dissociate_urls_from_file (file);
758     }
759
760   hash_table_put (dl_file_url_map, xstrdup (file), xstrdup (url));
761
762  url_only:
763   /* A URL->FILE mapping is not possible without a FILE->URL mapping.
764      If the latter were present, it should have been removed by the
765      above `if'.  So we could write:
766
767          assert (!hash_table_contains (dl_url_file_map, url));
768
769      The above is correct when running in recursive mode where the
770      same URL always resolves to the same file.  But if you do
771      something like:
772
773          wget URL URL
774
775      then the first URL will resolve to "FILE", and the other to
776      "FILE.1".  In that case, FILE.1 will not be found in
777      dl_file_url_map, but URL will still point to FILE in
778      dl_url_file_map.  */
779   if (hash_table_get_pair (dl_url_file_map, url, &old_url, &old_file))
780     {
781       hash_table_remove (dl_url_file_map, url);
782       xfree (old_url);
783       xfree (old_file);
784     }
785
786   hash_table_put (dl_url_file_map, xstrdup (url), xstrdup (file));
787 }
788
789 /* Register that FROM has been redirected to TO.  This assumes that TO
790    is successfully downloaded and already registered using
791    register_download() above.  */
792
793 void
794 register_redirection (const char *from, const char *to)
795 {
796   char *file;
797
798   ENSURE_TABLES_EXIST;
799
800   file = hash_table_get (dl_url_file_map, to);
801   assert (file != NULL);
802   if (!hash_table_contains (dl_url_file_map, from))
803     hash_table_put (dl_url_file_map, xstrdup (from), xstrdup (file));
804 }
805
806 /* Register that the file has been deleted. */
807
808 static void
809 register_delete_file (const char *file)
810 {
811   char *old_url, *old_file;
812
813   ENSURE_TABLES_EXIST;
814
815   if (!hash_table_get_pair (dl_file_url_map, file, &old_file, &old_url))
816     return;
817
818   hash_table_remove (dl_file_url_map, file);
819   xfree (old_file);
820   xfree (old_url);
821   dissociate_urls_from_file (file);
822 }
823
824 /* Register that FILE is an HTML file that has been downloaded. */
825
826 void
827 register_html (const char *url, const char *file)
828 {
829   if (!downloaded_html_set)
830     downloaded_html_set = make_string_hash_table (0);
831   else if (hash_table_contains (downloaded_html_set, file))
832     return;
833
834   /* The set and the list should use the same copy of FILE, but the
835      slist interface insists on strduping the string it gets.  Oh
836      well. */
837   string_set_add (downloaded_html_set, file);
838   downloaded_html_list = slist_prepend (downloaded_html_list, file);
839 }
840
841 /* This function is called when the retrieval is done to convert the
842    links that have been downloaded.  It has to be called at the end of
843    the retrieval, because only then does Wget know conclusively which
844    URLs have been downloaded, and which not, so it can tell which
845    direction to convert to.
846
847    The "direction" means that the URLs to the files that have been
848    downloaded get converted to the relative URL which will point to
849    that file.  And the other URLs get converted to the remote URL on
850    the server.
851
852    All the downloaded HTMLs are kept in downloaded_html_files, and
853    downloaded URLs in urls_downloaded.  All the information is
854    extracted from these two lists.  */
855
856 void
857 convert_all_links (void)
858 {
859   slist *html;
860   long msecs;
861   int file_count = 0;
862
863   struct wget_timer *timer = wtimer_new ();
864
865   /* Destructively reverse downloaded_html_files to get it in the right order.
866      recursive_retrieve() used slist_prepend() consistently.  */
867   downloaded_html_list = slist_nreverse (downloaded_html_list);
868
869   for (html = downloaded_html_list; html; html = html->next)
870     {
871       struct urlpos *urls, *cur_url;
872       char *url;
873       char *file = html->string;
874
875       /* Determine the URL of the HTML file.  get_urls_html will need
876          it.  */
877       url = hash_table_get (dl_file_url_map, file);
878       if (!url)
879         {
880           DEBUGP (("Apparently %s has been removed.\n", file));
881           continue;
882         }
883
884       DEBUGP (("Scanning %s (from %s)\n", file, url));
885
886       /* Parse the HTML file...  */
887       urls = get_urls_html (file, url, NULL);
888
889       /* We don't respect meta_disallow_follow here because, even if
890          the file is not followed, we might still want to convert the
891          links that have been followed from other files.  */
892
893       for (cur_url = urls; cur_url; cur_url = cur_url->next)
894         {
895           char *local_name;
896           struct url *u = cur_url->url;
897
898           if (cur_url->link_base_p)
899             {
900               /* Base references have been resolved by our parser, so
901                  we turn the base URL into an empty string.  (Perhaps
902                  we should remove the tag entirely?)  */
903               cur_url->convert = CO_NULLIFY_BASE;
904               continue;
905             }
906
907           /* We decide the direction of conversion according to whether
908              a URL was downloaded.  Downloaded URLs will be converted
909              ABS2REL, whereas non-downloaded will be converted REL2ABS.  */
910           local_name = hash_table_get (dl_url_file_map, u->url);
911
912           /* Decide on the conversion type.  */
913           if (local_name)
914             {
915               /* We've downloaded this URL.  Convert it to relative
916                  form.  We do this even if the URL already is in
917                  relative form, because our directory structure may
918                  not be identical to that on the server (think `-nd',
919                  `--cut-dirs', etc.)  */
920               cur_url->convert = CO_CONVERT_TO_RELATIVE;
921               cur_url->local_name = xstrdup (local_name);
922               DEBUGP (("will convert url %s to local %s\n", u->url, local_name));
923             }
924           else
925             {
926               /* We haven't downloaded this URL.  If it's not already
927                  complete (including a full host name), convert it to
928                  that form, so it can be reached while browsing this
929                  HTML locally.  */
930               if (!cur_url->link_complete_p)
931                 cur_url->convert = CO_CONVERT_TO_COMPLETE;
932               cur_url->local_name = NULL;
933               DEBUGP (("will convert url %s to complete\n", u->url));
934             }
935         }
936
937       /* Convert the links in the file.  */
938       convert_links (file, urls);
939       ++file_count;
940
941       /* Free the data.  */
942       free_urlpos (urls);
943     }
944
945   msecs = wtimer_elapsed (timer);
946   wtimer_delete (timer);
947   logprintf (LOG_VERBOSE, _("Converted %d files in %.2f seconds.\n"),
948              file_count, (double)msecs / 1000);
949 }
950
951 /* Cleanup the data structures associated with recursive retrieving
952    (the variables above).  */
953 void
954 recursive_cleanup (void)
955 {
956   if (dl_file_url_map)
957     {
958       free_keys_and_values (dl_file_url_map);
959       hash_table_destroy (dl_file_url_map);
960       dl_file_url_map = NULL;
961     }
962   if (dl_url_file_map)
963     {
964       free_keys_and_values (dl_url_file_map);
965       hash_table_destroy (dl_url_file_map);
966       dl_url_file_map = NULL;
967     }
968   if (downloaded_html_set)
969     string_set_free (downloaded_html_set);
970   slist_free (downloaded_html_list);
971   downloaded_html_list = NULL;
972 }