summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-12-22 13:28:55 +0100
committerwm4 <wm4@nowhere>2013-12-22 13:29:16 +0100
commit8b7048b7d54d7e1a163d6efc994dbd3792812334 (patch)
treee9bc3ad625d5a4b1c7e58bc307624719c758c0fd
parent3a637d411f603fd82ee0bf8a181ea20d60e7c88f (diff)
downloadmpv-8b7048b7d54d7e1a163d6efc994dbd3792812334.tar.bz2
mpv-8b7048b7d54d7e1a163d6efc994dbd3792812334.tar.xz
stream: minor cookie cleanup
Avoid global state (reload cookie file every time), actually free loaded cookies, use mp_get_user_path() for cookie file.
-rw-r--r--stream/cookies.c42
-rw-r--r--stream/stream_lavf.c7
2 files changed, 23 insertions, 26 deletions
diff --git a/stream/cookies.c b/stream/cookies.c
index 50f515f911..4b5b7b2e14 100644
--- a/stream/cookies.c
+++ b/stream/cookies.c
@@ -50,24 +50,14 @@ typedef struct cookie_list_type {
struct cookie_list_type *next;
} 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. */
-static char *col_dup(const char *src)
+static char *col_dup(void *talloc_ctx, const char *src)
{
- char *dst;
int length = 0;
-
while (src[length] > 31)
length++;
- dst = malloc(length + 1);
- strncpy(dst, src, length);
- dst[length] = 0;
-
- return dst;
+ return talloc_strndup(talloc_ctx, src, length);
}
/* Finds the start of all the columns */
@@ -138,27 +128,27 @@ err_out:
}
/* Loads a cookies.txt file into a linked list. */
-static struct cookie_list_type *load_cookies_from(struct mp_log *log,
- const char *filename,
- struct cookie_list_type
- *list)
+static struct cookie_list_type *load_cookies_from(void *ctx,
+ struct mp_log *log,
+ const char *filename)
{
char *ptr, *file;
int64_t length;
ptr = file = load_file(log, filename, &length);
if (!ptr)
- return list;
+ return NULL;
+ struct cookie_list_type *list = NULL;
while (*ptr) {
char *cols[7];
if (parse_line(&ptr, cols)) {
struct cookie_list_type *new;
- new = malloc(sizeof(cookie_list_t));
- new->name = col_dup(cols[5]);
- new->value = col_dup(cols[6]);
- new->path = col_dup(cols[2]);
- new->domain = col_dup(cols[0]);
+ new = talloc_zero(ctx, cookie_list_t);
+ new->name = col_dup(new, cols[5]);
+ new->value = col_dup(new, cols[6]);
+ new->path = col_dup(new, cols[2]);
+ new->domain = col_dup(new, cols[0]);
new->secure = (*(cols[3]) == 't') || (*(cols[3]) == 'T');
new->next = list;
list = new;
@@ -173,10 +163,11 @@ static struct cookie_list_type *load_cookies_from(struct mp_log *log,
// separated by newlines.
char *cookies_lavf(void *talloc_ctx, struct mp_log *log, char *file)
{
- if (!cookie_list && file && file[0])
- cookie_list = load_cookies_from(log, file, NULL);
+ void *tmp = talloc_new(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, "");
while (list) {
@@ -186,5 +177,6 @@ char *cookies_lavf(void *talloc_ctx, struct mp_log *log, char *file)
list = list->next;
}
+ talloc_free(tmp);
return res;
}
diff --git a/stream/stream_lavf.c b/stream/stream_lavf.c
index 645fcb5c6c..da57b10e82 100644
--- a/stream/stream_lavf.c
+++ b/stream/stream_lavf.c
@@ -22,6 +22,7 @@
#include "config.h"
#include "options/options.h"
+#include "options/path.h"
#include "common/msg.h"
#include "stream.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);
if (opts->network_cookies_enabled) {
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);
if (opts->network_tls_ca_file)