Consolidate http cookie addition
authorDavid Woodhouse <David.Woodhouse@intel.com>
Fri, 6 Nov 2009 11:16:08 +0000 (11:16 +0000)
committerDavid Woodhouse <David.Woodhouse@intel.com>
Fri, 6 Nov 2009 11:16:08 +0000 (11:16 +0000)
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
http.c

diff --git a/http.c b/http.c
index cd0dd92..1af5473 100644 (file)
--- a/http.c
+++ b/http.c
  * provided by their caller.
  */
 
+int http_add_cookie(struct openconnect_info *vpninfo, const char *option, const char *value)
+{
+       struct vpn_option *new, **this;
+
+       if (*value) {
+               new = malloc(sizeof(*new));
+               if (!new) {
+                       vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
+                       return -ENOMEM;
+               }
+               new->next = NULL;
+               new->option = strdup(option);
+               new->value = strdup(value);
+       } else {
+               /* Kill cookie; don't replace it */
+               new = NULL;
+       }
+       for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
+               if (!strcmp(option, (*this)->option)) {
+                       /* Replace existing cookie */
+                       if (new)
+                               new->next = (*this)->next;
+                       else
+                               new = (*this)->next;
+                       
+                       free((*this)->option);
+                       free((*this)->value);
+                       free(*this);
+                       *this = new;
+                       break;
+               }
+       }
+       if (new && !*this) {
+               *this = new;
+               new->next = NULL;
+       }
+       return 0;
+}
+
 static int process_http_response(struct openconnect_info *vpninfo, int *result,
                                 int (*header_cb)(struct openconnect_info *, char *, char *),
                                 char *body, int buf_len)
@@ -113,9 +152,9 @@ static int process_http_response(struct openconnect_info *vpninfo, int *result,
                        }
                }
                if (!strcmp(buf, "Set-Cookie")) {
-                       struct vpn_option *new, **this;
                        char *semicolon = strchr(colon, ';');
                        char *equals = strchr(colon, '=');
+                       int ret;
 
                        if (semicolon)
                                *semicolon = 0;
@@ -126,38 +165,9 @@ static int process_http_response(struct openconnect_info *vpninfo, int *result,
                        }
                        *(equals++) = 0;
 
-                       if (*equals) {
-                               new = malloc(sizeof(*new));
-                               if (!new) {
-                                       vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
-                                       return -ENOMEM;
-                               }
-                               new->next = NULL;
-                               new->option = strdup(colon);
-                               new->value = strdup(equals);
-                       } else {
-                               /* Kill cookie; don't replace it */
-                               new = NULL;
-                       }
-                       for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
-                               if (!strcmp(colon, (*this)->option)) {
-                                       /* Replace existing cookie */
-                                       if (new)
-                                               new->next = (*this)->next;
-                                       else
-                                               new = (*this)->next;
-
-                                       free((*this)->option);
-                                       free((*this)->value);
-                                       free(*this);
-                                       *this = new;
-                                       break;
-                               }
-                       }
-                       if (new && !*this) {
-                               *this = new;
-                               new->next = NULL;
-                       }
+                       ret = http_add_cookie(vpninfo, colon, equals);
+                       if (ret)
+                               return ret;
                }
                if (!strcmp(buf, "Transfer-Encoding")) {
                        if (!strcmp(colon, "chunked"))
@@ -396,38 +406,8 @@ static int run_csd_script(struct openconnect_info *vpninfo, char *buf, int bufle
        vpninfo->csd_waiturl = NULL;
        vpninfo->csd_scriptname = strdup(fname);
 
-       /* AB: add cookie "sdesktop=__TOKEN__" */
-       {
-               struct vpn_option *new, **this;
-               new = malloc(sizeof(*new));
-               if (!new) {
-                       vpninfo->progress(vpninfo, PRG_ERR, "No memory for allocating cookies\n");
-                       return -ENOMEM;
-               }
-               new->next = NULL;
-               new->option = strdup("sdesktop");
-               new->value = strdup(vpninfo->csd_token);
-
-               for (this = &vpninfo->cookies; *this; this = &(*this)->next) {
-                       if (!strcmp(new->option, (*this)->option)) {
-                               /* Replace existing cookie */
-                               if (new)
-                                       new->next = (*this)->next;
-                               else
-                                       new = (*this)->next;
-
-                               free((*this)->option);
-                               free((*this)->value);
-                               free(*this);
-                               *this = new;
-                               break;
-                       }
-               }
-               if (new && !*this) {
-                       *this = new;
-                       new->next = NULL;
-               }
-       }
+       http_add_cookie(vpninfo, "sdesktop", vpninfo->csd_token);
+
        return 0;
 }