2003-12-08 13:25:35 +00:00
|
|
|
/*
|
|
|
|
* HTTP Cookies
|
|
|
|
* Reads Netscape and Mozilla cookies.txt files
|
|
|
|
*
|
2010-01-30 22:26:47 +00:00
|
|
|
* Copyright (c) 2003 Dave Lambley <mplayer@davel.me.uk>
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2010-01-30 22:26:47 +00:00
|
|
|
*
|
2017-05-11 05:29:01 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2010-01-30 22:26:47 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2010-01-30 22:26:47 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-05-11 05:29:01 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2010-01-30 22:26:47 +00:00
|
|
|
*
|
2017-05-11 05:29:01 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2003-12-08 13:25:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2006-06-04 22:41:27 +00:00
|
|
|
#include <inttypes.h>
|
2003-12-08 13:25:35 +00:00
|
|
|
|
2023-01-09 20:31:20 +00:00
|
|
|
#include "stream/stream.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2003-12-08 13:25:35 +00:00
|
|
|
#include "cookies.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2003-12-08 13:25:35 +00:00
|
|
|
|
|
|
|
#define MAX_COOKIES 20
|
|
|
|
|
|
|
|
typedef struct cookie_list_type {
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
char *domain;
|
|
|
|
char *path;
|
|
|
|
|
|
|
|
int secure;
|
|
|
|
|
|
|
|
struct cookie_list_type *next;
|
|
|
|
} cookie_list_t;
|
|
|
|
|
|
|
|
/* Like strdup, but stops at anything <31. */
|
2013-12-22 12:28:55 +00:00
|
|
|
static char *col_dup(void *talloc_ctx, const char *src)
|
2003-12-08 13:25:35 +00:00
|
|
|
{
|
|
|
|
int length = 0;
|
|
|
|
while (src[length] > 31)
|
2014-04-13 16:00:51 +00:00
|
|
|
length++;
|
2003-12-08 13:25:35 +00:00
|
|
|
|
2013-12-22 12:28:55 +00:00
|
|
|
return talloc_strndup(talloc_ctx, src, length);
|
2003-12-08 13:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Finds the start of all the columns */
|
2014-01-19 10:53:03 +00:00
|
|
|
static int parse_line(char **ptr, char *cols[7])
|
2003-12-08 13:25:35 +00:00
|
|
|
{
|
|
|
|
int col;
|
|
|
|
cols[0] = *ptr;
|
|
|
|
|
|
|
|
for (col = 1; col < 7; col++) {
|
2014-04-13 16:00:51 +00:00
|
|
|
for (; (**ptr) > 31; (*ptr)++);
|
|
|
|
if (**ptr == 0)
|
|
|
|
return 0;
|
|
|
|
(*ptr)++;
|
|
|
|
if ((*ptr)[-1] != 9)
|
|
|
|
return 0;
|
|
|
|
cols[col] = (*ptr);
|
2003-12-08 13:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loads a cookies.txt file into a linked list. */
|
2013-12-22 12:28:55 +00:00
|
|
|
static struct cookie_list_type *load_cookies_from(void *ctx,
|
2023-01-09 20:31:20 +00:00
|
|
|
struct mpv_global *global,
|
2013-12-22 12:28:55 +00:00
|
|
|
struct mp_log *log,
|
|
|
|
const char *filename)
|
2003-12-08 13:25:35 +00:00
|
|
|
{
|
2023-01-09 20:31:20 +00:00
|
|
|
mp_verbose(log, "Loading cookie file: %s\n", filename);
|
|
|
|
bstr data = stream_read_file(filename, ctx, global, 1000000);
|
|
|
|
if (!data.start) {
|
|
|
|
mp_verbose(log, "Error reading\n");
|
2014-04-13 16:00:51 +00:00
|
|
|
return NULL;
|
2023-01-09 20:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bstr_xappend(ctx, &data, (struct bstr){"", 1}); // null-terminate
|
|
|
|
char *ptr = data.start;
|
2003-12-08 13:25:35 +00:00
|
|
|
|
2013-12-22 12:28:55 +00:00
|
|
|
struct cookie_list_type *list = NULL;
|
2012-10-30 17:28:34 +00:00
|
|
|
while (*ptr) {
|
2014-04-13 16:00:51 +00:00
|
|
|
char *cols[7];
|
|
|
|
if (parse_line(&ptr, cols)) {
|
|
|
|
struct cookie_list_type *new;
|
|
|
|
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;
|
|
|
|
}
|
2003-12-08 13:25:35 +00:00
|
|
|
}
|
2023-01-09 20:31:20 +00:00
|
|
|
|
2003-12-08 13:25:35 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2013-01-24 15:05:52 +00:00
|
|
|
// Return a cookies string as expected by lavf (libavformat/http.c). The format
|
|
|
|
// is like a Set-Cookie header (http://curl.haxx.se/rfc/cookie_spec.html),
|
|
|
|
// separated by newlines.
|
2023-01-09 20:31:20 +00:00
|
|
|
char *cookies_lavf(void *talloc_ctx,
|
|
|
|
struct mpv_global *global,
|
|
|
|
struct mp_log *log,
|
|
|
|
const char *file)
|
2013-01-24 15:05:52 +00:00
|
|
|
{
|
2013-12-22 12:28:55 +00:00
|
|
|
void *tmp = talloc_new(NULL);
|
|
|
|
struct cookie_list_type *list = NULL;
|
|
|
|
if (file && file[0])
|
2023-01-09 20:31:20 +00:00
|
|
|
list = load_cookies_from(tmp, global, log, file);
|
2013-01-24 15:05:52 +00:00
|
|
|
|
2013-12-22 12:11:22 +00:00
|
|
|
char *res = talloc_strdup(talloc_ctx, "");
|
2013-01-24 15:05:52 +00:00
|
|
|
|
|
|
|
while (list) {
|
|
|
|
res = talloc_asprintf_append_buffer(res,
|
|
|
|
"%s=%s; path=%s; domain=%s; %s\n", list->name, list->value,
|
|
|
|
list->path, list->domain, list->secure ? "secure" : "");
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:28:55 +00:00
|
|
|
talloc_free(tmp);
|
2013-01-24 15:05:52 +00:00
|
|
|
return res;
|
|
|
|
}
|