Skip to content

Commit

Permalink
Consolidate http cookie addition
Browse files Browse the repository at this point in the history
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Nov 6, 2009
1 parent 5324000 commit 50e9810
Showing 1 changed file with 45 additions and 65 deletions.
110 changes: 45 additions & 65 deletions http.c
Expand Up @@ -49,6 +49,45 @@
* 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)
Expand Down Expand Up @@ -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;
Expand All @@ -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"))
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 50e9810

Please sign in to comment.