]> sjero.net Git - wget/blobdiff - src/cookies.c
[svn] Use new macros xnew, xnew0, xnew_array, and xnew0_array in various places.
[wget] / src / cookies.c
index 0caa53d18d12f71d4563113c252976d16bc58b6c..07cfdc8ad14721b2353420424502cac6e041748c 100644 (file)
@@ -77,8 +77,8 @@ time_t http_atotm PARAMS ((const char *));
    the cookies.  */
 
 struct cookie_jar {
-  /* Mapping between domains and their corresponding cookies.  */
-  struct hash_table *chains_by_domain;
+  /* Cookie chains indexed by domain.  */
+  struct hash_table *chains;
 
   int cookie_count;            /* number of cookies in the jar. */
 };
@@ -90,8 +90,8 @@ time_t cookies_now;
 struct cookie_jar *
 cookie_jar_new (void)
 {
-  struct cookie_jar *jar = xmalloc (sizeof (struct cookie_jar));
-  jar->chains_by_domain = make_nocase_string_hash_table (0);
+  struct cookie_jar *jar = xnew (struct cookie_jar);
+  jar->chains = make_nocase_string_hash_table (0);
   jar->cookie_count = 0;
   return jar;
 }
@@ -118,8 +118,6 @@ struct cookie {
   char *attr;                  /* cookie attribute name */
   char *value;                 /* cookie attribute value */
 
-  struct cookie_jar *jar;      /* pointer back to the cookie jar, for
-                                  convenience. */
   struct cookie *next;         /* used for chaining of cookies in the
                                   same domain. */
 };
@@ -132,8 +130,7 @@ struct cookie {
 static struct cookie *
 cookie_new (void)
 {
-  struct cookie *cookie = xmalloc (sizeof (struct cookie));
-  memset (cookie, '\0', sizeof (struct cookie));
+  struct cookie *cookie = xnew0 (struct cookie);
 
   /* Both cookie->permanent and cookie->expiry_time are now 0.  By
      default, we assume that the cookie is non-permanent and valid
@@ -157,10 +154,10 @@ delete_cookie (struct cookie *cookie)
 \f
 /* Functions for storing cookies.
 
-   All cookies can be reached beginning with jar->chains_by_domain.
-   The key in that table is the domain name, and the value is a linked
-   list of all cookies from that domain.  Every new cookie is placed
-   on the head of the list.  */
+   All cookies can be reached beginning with jar->chains.  The key in
+   that table is the domain name, and the value is a linked list of
+   all cookies from that domain.  Every new cookie is placed on the
+   head of the list.  */
 
 /* Find and return a cookie in JAR whose domain, path, and attribute
    name correspond to COOKIE.  If found, PREVPTR will point to the
@@ -175,7 +172,7 @@ find_matching_cookie (struct cookie_jar *jar, struct cookie *cookie,
 {
   struct cookie *chain, *prev;
 
-  chain = hash_table_get (jar->chains_by_domain, cookie->domain);
+  chain = hash_table_get (jar->chains, cookie->domain);
   if (!chain)
     goto nomatch;
 
@@ -209,7 +206,7 @@ store_cookie (struct cookie_jar *jar, struct cookie *cookie)
   struct cookie *chain_head;
   char *chain_key;
 
-  if (hash_table_get_pair (jar->chains_by_domain, cookie->domain,
+  if (hash_table_get_pair (jar->chains, cookie->domain,
                           &chain_key, &chain_head))
     {
       /* A chain of cookies in this domain already exists.  Check for
@@ -252,7 +249,7 @@ store_cookie (struct cookie_jar *jar, struct cookie *cookie)
       chain_key = xstrdup (cookie->domain);
     }
 
-  hash_table_put (jar->chains_by_domain, chain_key, cookie);
+  hash_table_put (jar->chains, chain_key, cookie);
   ++jar->cookie_count;
 
   DEBUGP (("\nStored cookie %s %d%s %s %s %d %s %s %s\n",
@@ -277,7 +274,7 @@ discard_matching_cookie (struct cookie_jar *jar, struct cookie *cookie)
 {
   struct cookie *prev, *victim;
 
-  if (!hash_table_count (jar->chains_by_domain))
+  if (!hash_table_count (jar->chains))
     /* No elements == nothing to discard. */
     return;
 
@@ -294,18 +291,18 @@ discard_matching_cookie (struct cookie_jar *jar, struct cookie *cookie)
          char *chain_key = NULL;
          int res;
 
-         res = hash_table_get_pair (jar->chains_by_domain, victim->domain,
+         res = hash_table_get_pair (jar->chains, victim->domain,
                                     &chain_key, NULL);
          assert (res != 0);
          if (!victim->next)
            {
              /* VICTIM was the only cookie in the chain.  Destroy the
                 chain and deallocate the chain key.  */
-             hash_table_remove (jar->chains_by_domain, victim->domain);
+             hash_table_remove (jar->chains, victim->domain);
              xfree (chain_key);
            }
          else
-           hash_table_put (jar->chains_by_domain, chain_key, victim->next);
+           hash_table_put (jar->chains, chain_key, victim->next);
        }
       delete_cookie (victim);
       DEBUGP (("Discarded old cookie.\n"));
@@ -902,7 +899,7 @@ find_chains_of_host (struct cookie_jar *jar, const char *host,
   int passes, passcnt;
 
   /* Bail out quickly if there are no cookies in the jar.  */
-  if (!hash_table_count (jar->chains_by_domain))
+  if (!hash_table_count (jar->chains))
     return 0;
 
   if (numeric_address_p (host))
@@ -922,7 +919,7 @@ find_chains_of_host (struct cookie_jar *jar, const char *host,
      srk.fer.hr's, then fer.hr's.  */
   while (1)
     {
-      struct cookie *chain = hash_table_get (jar->chains_by_domain, host);
+      struct cookie *chain = hash_table_get (jar->chains, host);
       if (chain)
        dest[dest_count++] = chain;
       if (++passcnt >= passes)
@@ -1434,7 +1431,7 @@ cookie_jar_save (struct cookie_jar *jar, const char *file)
   fprintf (fp, "# Generated by Wget on %s.\n", datetime_str (NULL));
   fputs ("# Edit at your own risk.\n\n", fp);
 
-  hash_table_map (jar->chains_by_domain, save_cookies_mapper, fp);
+  hash_table_map (jar->chains, save_cookies_mapper, fp);
 
   if (ferror (fp))
     logprintf (LOG_NOTQUIET, _("Error writing to `%s': %s\n"),
@@ -1460,7 +1457,7 @@ nuke_cookie_chain (void *value, void *key, void *arg)
   struct cookie_jar *jar = (struct cookie_jar *)arg;
 
   /* Remove the chain from the table and free the key. */
-  hash_table_remove (jar->chains_by_domain, chain_key);
+  hash_table_remove (jar->chains, chain_key);
   xfree (chain_key);
 
   /* Then delete all the cookies in the chain. */
@@ -1480,8 +1477,8 @@ nuke_cookie_chain (void *value, void *key, void *arg)
 void
 cookie_jar_delete (struct cookie_jar *jar)
 {
-  hash_table_map (jar->chains_by_domain, nuke_cookie_chain, jar);
-  hash_table_destroy (jar->chains_by_domain);
+  hash_table_map (jar->chains, nuke_cookie_chain, jar);
+  hash_table_destroy (jar->chains);
   xfree (jar);
 }
 \f