stream: minor cookie cleanup

Avoid global state (reload cookie file every time), actually free
loaded cookies, use mp_get_user_path() for cookie file.
This commit is contained in:
wm4 2013-12-22 13:28:55 +01:00
parent 3a637d411f
commit 8b7048b7d5
2 changed files with 23 additions and 26 deletions

View File

@ -50,24 +50,14 @@ typedef struct cookie_list_type {
struct cookie_list_type *next; struct cookie_list_type *next;
} cookie_list_t; } cookie_list_t;
/* Pointer to the linked list of cookies */
static struct cookie_list_type *cookie_list = NULL;
/* Like strdup, but stops at anything <31. */ /* Like strdup, but stops at anything <31. */
static char *col_dup(const char *src) static char *col_dup(void *talloc_ctx, const char *src)
{ {
char *dst;
int length = 0; int length = 0;
while (src[length] > 31) while (src[length] > 31)
length++; length++;
dst = malloc(length + 1); return talloc_strndup(talloc_ctx, src, length);
strncpy(dst, src, length);
dst[length] = 0;
return dst;
} }
/* Finds the start of all the columns */ /* Finds the start of all the columns */
@ -138,27 +128,27 @@ err_out:
} }
/* Loads a cookies.txt file into a linked list. */ /* Loads a cookies.txt file into a linked list. */
static struct cookie_list_type *load_cookies_from(struct mp_log *log, static struct cookie_list_type *load_cookies_from(void *ctx,
const char *filename, struct mp_log *log,
struct cookie_list_type const char *filename)
*list)
{ {
char *ptr, *file; char *ptr, *file;
int64_t length; int64_t length;
ptr = file = load_file(log, filename, &length); ptr = file = load_file(log, filename, &length);
if (!ptr) if (!ptr)
return list; return NULL;
struct cookie_list_type *list = NULL;
while (*ptr) { while (*ptr) {
char *cols[7]; char *cols[7];
if (parse_line(&ptr, cols)) { if (parse_line(&ptr, cols)) {
struct cookie_list_type *new; struct cookie_list_type *new;
new = malloc(sizeof(cookie_list_t)); new = talloc_zero(ctx, cookie_list_t);
new->name = col_dup(cols[5]); new->name = col_dup(new, cols[5]);
new->value = col_dup(cols[6]); new->value = col_dup(new, cols[6]);
new->path = col_dup(cols[2]); new->path = col_dup(new, cols[2]);
new->domain = col_dup(cols[0]); new->domain = col_dup(new, cols[0]);
new->secure = (*(cols[3]) == 't') || (*(cols[3]) == 'T'); new->secure = (*(cols[3]) == 't') || (*(cols[3]) == 'T');
new->next = list; new->next = list;
list = new; list = new;
@ -173,10 +163,11 @@ static struct cookie_list_type *load_cookies_from(struct mp_log *log,
// separated by newlines. // separated by newlines.
char *cookies_lavf(void *talloc_ctx, struct mp_log *log, char *file) char *cookies_lavf(void *talloc_ctx, struct mp_log *log, char *file)
{ {
if (!cookie_list && file && file[0]) void *tmp = talloc_new(NULL);
cookie_list = load_cookies_from(log, file, NULL); struct cookie_list_type *list = NULL;
if (file && file[0])
list = load_cookies_from(tmp, log, file);
struct cookie_list_type *list = cookie_list;
char *res = talloc_strdup(talloc_ctx, ""); char *res = talloc_strdup(talloc_ctx, "");
while (list) { while (list) {
@ -186,5 +177,6 @@ char *cookies_lavf(void *talloc_ctx, struct mp_log *log, char *file)
list = list->next; list = list->next;
} }
talloc_free(tmp);
return res; return res;
} }

View File

@ -22,6 +22,7 @@
#include "config.h" #include "config.h"
#include "options/options.h" #include "options/options.h"
#include "options/path.h"
#include "common/msg.h" #include "common/msg.h"
#include "stream.h" #include "stream.h"
#include "options/m_option.h" #include "options/m_option.h"
@ -184,7 +185,11 @@ static int open_f(stream_t *stream, int mode)
av_dict_set(&dict, "user-agent", opts->network_useragent, 0); av_dict_set(&dict, "user-agent", opts->network_useragent, 0);
if (opts->network_cookies_enabled) { if (opts->network_cookies_enabled) {
char *file = opts->network_cookies_file; char *file = opts->network_cookies_file;
av_dict_set(&dict, "cookies", cookies_lavf(temp, stream->log, file), 0); if (file && file[0])
file = mp_get_user_path(temp, stream->global, file);
char *cookies = cookies_lavf(temp, stream->log, file);
if (cookies && cookies[0])
av_dict_set(&dict, "cookies", cookies, 0);
} }
av_dict_set(&dict, "tls_verify", opts->network_tls_verify ? "1" : "0", 0); av_dict_set(&dict, "tls_verify", opts->network_tls_verify ? "1" : "0", 0);
if (opts->network_tls_ca_file) if (opts->network_tls_ca_file)