Skip to content

Commit

Permalink
httpclient: Make it possible to override and clear cookies using HTTP…
Browse files Browse the repository at this point in the history
… inspector callbacks

Fixes #2618
  • Loading branch information
andoma committed May 18, 2015
1 parent f6a6052 commit b7be5b0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/ecmascript/es_io.c
Expand Up @@ -595,9 +595,12 @@ es_http_inspector_set_cookie(duk_context *ctx)
es_context_t *ec = es_get(ctx);
http_request_inspection_t *hri = get_hri(ctx);
const char *key = duk_safe_to_string(ctx, 0);
const char *value = duk_safe_to_string(ctx, 1);
es_debug(ec, "Inspector setting cookie %s = %s",
key, value);
const char *value = duk_get_string(ctx, 1);
if(value) {
es_debug(ec, "Inspector setting cookie %s = %s", key, value);
} else {
es_debug(ec, "Inspector clearing cookie %s", key);
}
http_client_set_cookie(hri, key, value);
return 0;
}
Expand Down
10 changes: 10 additions & 0 deletions src/fileaccess/fa_http.c
Expand Up @@ -681,6 +681,14 @@ http_cookie_append(const char *req_host, const char *req_path,
if(http_header_get(extra_cookies, hc->hc_name))
continue;

// Skip overridden headers
LIST_FOREACH(hh, extra_cookies, hh_link) {
if(!strcmp(hh->hh_key, hc->hc_name))
break;
}
if(hh != NULL)
continue;

htsbuf_append(&hq, s, strlen(s));
htsbuf_append(&hq, hc->hc_name, strlen(hc->hc_name));
htsbuf_append(&hq, "=", 1);
Expand All @@ -691,6 +699,8 @@ http_cookie_append(const char *req_host, const char *req_path,
hts_mutex_unlock(&http_cookies_mutex);

LIST_FOREACH(hh, extra_cookies, hh_link) {
if(hh->hh_value == NULL)
continue;
htsbuf_append(&hq, s, strlen(s));
htsbuf_append(&hq, hh->hh_key, strlen(hh->hh_key));
htsbuf_append(&hq, "=", 1);
Expand Down
6 changes: 3 additions & 3 deletions src/networking/http.c
Expand Up @@ -80,7 +80,7 @@ void
http_header_add(struct http_header_list *headers, const char *key,
const char *value, int append)
{
http_header_add_alloced(headers, key, strdup(value), append);
http_header_add_alloced(headers, key, value ? strdup(value) : NULL, append);
}

/**
Expand All @@ -92,7 +92,7 @@ http_header_add_lws(struct http_header_list *headers, const char *data)
http_header_t *hh;
int cl;
hh = LIST_FIRST(headers);
if(hh == NULL)
if(hh == NULL || hh->hh_value == NULL)
return;

cl = strlen(hh->hh_value);
Expand Down Expand Up @@ -148,7 +148,7 @@ http_header_merge(struct http_header_list *dst,
http_header_add(dst, hhs->hh_key, hhs->hh_value, 0);
} else {
free(hhd->hh_value);
hhd->hh_value = strdup(hhs->hh_value);
hhd->hh_value = hhs->hh_value ? strdup(hhs->hh_value) : NULL;
}
}
}
Expand Down

0 comments on commit b7be5b0

Please sign in to comment.