]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
cookie: simplifications
authorDaniel Stenberg <daniel@haxx.se>
Thu, 28 Aug 2025 09:42:49 +0000 (11:42 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 28 Aug 2025 15:52:07 +0000 (17:52 +0200)
- add Curl_secure_context(), to have it determined in a single place.

- tweak the Curl_cookie_getlist() proto. Move some logic into the
  function - at is only called in a single place. Instead of forcing the
  caller to do it.

- make 'is_ip' a const

Closes #18419

lib/cookie.c
lib/cookie.h
lib/http.c

index 99b5e43d648b4e30ec4e4c27c65c52d3be114889..29252da41a68883ed88f810bb0e7fa5bde084c26 100644 (file)
@@ -1294,6 +1294,14 @@ static int cookie_sort_ct(const void *p1, const void *p2)
   return (c2->creationtime > c1->creationtime) ? 1 : -1;
 }
 
+bool Curl_secure_context(struct connectdata *conn, const char *host)
+{
+  return conn->handler->protocol&(CURLPROTO_HTTPS|CURLPROTO_WSS) ||
+    curl_strequal("localhost", host) ||
+    !strcmp(host, "127.0.0.1") ||
+    !strcmp(host, "::1");
+}
+
 /*
  * Curl_cookie_getlist
  *
@@ -1306,15 +1314,17 @@ static int cookie_sort_ct(const void *p1, const void *p2)
  * Returns 0 when there is a list returned. Otherwise non-zero.
  */
 int Curl_cookie_getlist(struct Curl_easy *data,
-                        struct CookieInfo *ci,
-                        const char *host, const char *path,
-                        bool secure,
+                        struct connectdata *conn,
+                        const char *host,
                         struct Curl_llist *list)
 {
   size_t matches = 0;
-  bool is_ip;
+  const bool is_ip = Curl_host_is_ipnum(host);
   const size_t myhash = cookiehash(host);
   struct Curl_llist_node *n;
+  const bool secure = Curl_secure_context(conn, host);
+  struct CookieInfo *ci = data->cookies;
+  const char *path = data->state.up.path;
 
   Curl_llist_init(list, NULL);
 
@@ -1324,9 +1334,6 @@ int Curl_cookie_getlist(struct Curl_easy *data,
   /* at first, remove expired cookies */
   remove_expired(ci);
 
-  /* check if host is an IP(v4|v6) address */
-  is_ip = Curl_host_is_ipnum(host);
-
   for(n = Curl_llist_head(&ci->cookielist[myhash]);
       n; n = Curl_node_next(n)) {
     struct Cookie *co = Curl_node_elem(n);
index 7af65073cd9c0f7fdf99c32def75a5cce5e194ec..99aa20af7c4b9d869fd25823edd923b1ef455467 100644 (file)
@@ -105,21 +105,21 @@ struct CookieInfo {
 #define MAX_COOKIE_SEND_AMOUNT 150
 
 struct Curl_easy;
+struct connectdata;
+
 /*
  * Add a cookie to the internal list of cookies. The domain and path arguments
  * are only used if the header boolean is TRUE.
  */
 
+bool Curl_secure_context(struct connectdata *conn, const char *host);
 struct Cookie *Curl_cookie_add(struct Curl_easy *data,
                                struct CookieInfo *c, bool header,
                                bool noexpiry, const char *lineptr,
                                const char *domain, const char *path,
                                bool secure);
-
-int Curl_cookie_getlist(struct Curl_easy *data,
-                        struct CookieInfo *c, const char *host,
-                        const char *path, bool secure,
-                        struct Curl_llist *list);
+int Curl_cookie_getlist(struct Curl_easy *data, struct connectdata *conn,
+                        const char *host, struct Curl_llist *list);
 void Curl_cookie_clearall(struct CookieInfo *cookies);
 void Curl_cookie_clearsess(struct CookieInfo *cookies);
 
index bf584e093d40bf9d8ab89a147e53e511b48b4791..3f8c69e49232b9d68398ed0b0cf8ce5ff4dc2cf0 100644 (file)
@@ -2442,14 +2442,8 @@ static CURLcode http_cookies(struct Curl_easy *data,
     if(data->cookies && data->state.cookie_engine) {
       const char *host = data->state.aptr.cookiehost ?
         data->state.aptr.cookiehost : conn->host.name;
-      const bool secure_context =
-        conn->handler->protocol&(CURLPROTO_HTTPS|CURLPROTO_WSS) ||
-        curl_strequal("localhost", host) ||
-        !strcmp(host, "127.0.0.1") ||
-        !strcmp(host, "::1");
       Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
-      rc = Curl_cookie_getlist(data, data->cookies, host, data->state.up.path,
-                               secure_context, &list);
+      rc = Curl_cookie_getlist(data, conn, host, &list);
       Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);
     }
     if(!rc) {
@@ -3334,14 +3328,8 @@ static CURLcode http_header_s(struct Curl_easy *data,
      * real peer hostname. */
     const char *host = data->state.aptr.cookiehost ?
       data->state.aptr.cookiehost : conn->host.name;
-    const bool secure_context =
-      conn->handler->protocol&(CURLPROTO_HTTPS|CURLPROTO_WSS) ||
-      curl_strequal("localhost", host) ||
-      !strcmp(host, "127.0.0.1") ||
-      !strcmp(host, "::1");
-
-    Curl_share_lock(data, CURL_LOCK_DATA_COOKIE,
-                    CURL_LOCK_ACCESS_SINGLE);
+    const bool secure_context = Curl_secure_context(conn, host);
+    Curl_share_lock(data, CURL_LOCK_DATA_COOKIE, CURL_LOCK_ACCESS_SINGLE);
     Curl_cookie_add(data, data->cookies, TRUE, FALSE, v, host,
                     data->state.up.path, secure_context);
     Curl_share_unlock(data, CURL_LOCK_DATA_COOKIE);