Coverage Report

Created: 2026-03-27 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/stream/cookies.c
Line
Count
Source
1
/*
2
 * HTTP Cookies
3
 * Reads Netscape and Mozilla cookies.txt files
4
 *
5
 * Copyright (c) 2003 Dave Lambley <mplayer@davel.me.uk>
6
 *
7
 * This file is part of mpv.
8
 *
9
 * mpv is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * mpv is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <fcntl.h>
26
#include <string.h>
27
#include <sys/types.h>
28
#include <inttypes.h>
29
30
#include "stream/stream.h"
31
#include "options/options.h"
32
#include "cookies.h"
33
#include "common/msg.h"
34
35
#define MAX_COOKIES 20
36
37
typedef struct cookie_list_type {
38
    char *name;
39
    char *value;
40
    char *domain;
41
    char *path;
42
43
    int secure;
44
45
    struct cookie_list_type *next;
46
} cookie_list_t;
47
48
/* Like strdup, but stops at anything <31. */
49
static char *col_dup(void *talloc_ctx, const char *src)
50
0
{
51
0
    int length = 0;
52
0
    while (src[length] > 31)
53
0
        length++;
54
55
0
    return talloc_strndup(talloc_ctx, src, length);
56
0
}
57
58
/* Finds the start of all the columns */
59
static int parse_line(char **ptr, char *cols[7])
60
0
{
61
0
    int col;
62
0
    cols[0] = *ptr;
63
64
0
    for (col = 1; col < 7; col++) {
65
0
        for (; (**ptr) > 31; (*ptr)++);
66
0
        if (**ptr == 0)
67
0
            return 0;
68
0
        (*ptr)++;
69
0
        if ((*ptr)[-1] != 9)
70
0
            return 0;
71
0
        cols[col] = (*ptr);
72
0
    }
73
74
0
    return 1;
75
0
}
76
77
/* Loads a cookies.txt file into a linked list. */
78
static struct cookie_list_type *load_cookies_from(void *ctx,
79
                                                  struct mpv_global *global,
80
                                                  struct mp_log *log,
81
                                                  const char *filename)
82
0
{
83
0
    mp_verbose(log, "Loading cookie file: %s\n", filename);
84
0
    bstr data = stream_read_file(filename, ctx, global, 1000000);
85
0
    if (!data.start) {
86
0
        mp_verbose(log, "Error reading\n");
87
0
        return NULL;
88
0
    }
89
90
0
    bstr_xappend(ctx, &data, (struct bstr){"", 1}); // null-terminate
91
0
    char *ptr = data.start;
92
93
0
    struct cookie_list_type *list = NULL;
94
0
    while (*ptr) {
95
0
        char *cols[7];
96
0
        if (parse_line(&ptr, cols)) {
97
0
            struct cookie_list_type *new;
98
0
            new = talloc_zero(ctx, cookie_list_t);
99
0
            new->name = col_dup(new, cols[5]);
100
0
            new->value = col_dup(new, cols[6]);
101
0
            new->path = col_dup(new, cols[2]);
102
0
            new->domain = col_dup(new, cols[0]);
103
0
            new->secure = (*(cols[3]) == 't') || (*(cols[3]) == 'T');
104
0
            new->next = list;
105
0
            list = new;
106
0
        }
107
0
    }
108
109
0
    return list;
110
0
}
111
112
// Return a cookies string as expected by lavf (libavformat/http.c). The format
113
// is like a Set-Cookie header (http://curl.haxx.se/rfc/cookie_spec.html),
114
// separated by newlines.
115
char *cookies_lavf(void *talloc_ctx,
116
                   struct mpv_global *global,
117
                   struct mp_log *log,
118
                   const char *file)
119
3
{
120
3
    void *tmp = talloc_new(NULL);
121
3
    struct cookie_list_type *list = NULL;
122
3
    if (file && file[0])
123
0
        list = load_cookies_from(tmp, global, log, file);
124
125
3
    char *res = talloc_strdup(talloc_ctx, "");
126
127
3
    while (list) {
128
0
        res = talloc_asprintf_append_buffer(res,
129
0
                    "%s=%s; path=%s; domain=%s; %s\n", list->name, list->value,
130
0
                    list->path, list->domain, list->secure ? "secure" : "");
131
0
        list = list->next;
132
0
    }
133
134
3
    talloc_free(tmp);
135
3
    return res;
136
3
}