FFmpeg
Loading...
Searching...
No Matches
httpcookie.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <inttypes.h>
20#include <stdio.h>
21
22#include "libavutil/macros.h"
23#include "libavutil/mem.h"
25#include "libavformat/http.h"
26
27static const char *const cookies[] = {
28 "sid=secret",
29 "sid= secret ; Path= /a ; Domain= .example.com",
30 "domain=localhost",
31 "path=secret; Domain=example.com; Path=/",
32 "sid=secret; =domain=example.com",
33 "sid=secret; Path=abc",
34 "sid=secret; Path=/a; Path=\"/\"",
35 "sid=secret; Secure; HttpOnly",
36 "sid=secret; Max-Age=0; Expires=Wed, 09 Jun 2100 10:18:14 GMT",
37 "sid=secret; Max-Age=-1",
38 "sid=secret; Max-Age=0; Max-Age=bad",
39 "sid=secret; Expires=Sunday, 06-Nov-1994 08:49:37 GMT; Expires=bad",
40 "sid=secret; Domain=example.com; Domain=",
41 "sid=secret; Max-Age=-; Expires=Wed, 09 Jun 2100 10:18:14 GMT",
42 "sid=secret; Max-Age=1x; Expires=Sunday, 06-Nov-1994 08:49:37 GMT",
43 "sid=sec\tret",
44 "sid=sec\rret",
45 "si\x7f" "d=secret",
46 "sid",
47 "=secret",
48 "; sid=secret",
49 "",
50};
51
52static const char *const dates[] = {
53 "Wed, 21 Oct 2015 07:28:00 GMT",
54 "Wednesday, 21-Oct-15 07:28:00 GMT",
55 "Wed Oct 21 07:28:00 2015",
56 "Thu, 01 Jan 1970 00:00:00 GMT",
57 "Thu, 29 Feb 2024 12:00:00 GMT",
58 "Thu, 29 Feb 2023 12:00:00 GMT",
59 "Wed, 21 Oct 1600 07:28:00 GMT",
60 "Wed, 21 Oct 2015 24:00:00 GMT",
61 "120",
62};
63
64int main(void)
65{
66 char *bound = av_strdup("sid=secret\ndomain=localhost\nsid=secret; Domain=example.com\n"
67 "sid=secret; @hostonly=other\r\n");
68
69 for (int i = 0; i < FF_ARRAY_ELEMS(cookies); i++) {
70 AVDictionary *pair = NULL, *attributes = NULL;
71 const AVDictionaryEntry *e = NULL;
72 const char *domain;
73 int ret = ff_http_parse_set_cookie(cookies[i], &pair, &attributes);
74
75 printf("%d:", i);
76 if (ret < 0) {
77 printf(" %s\n", ret == AVERROR_INVALIDDATA ? "no cookie" : "error");
78 } else {
79 e = av_dict_iterate(pair, NULL);
80 printf(" [%s]=[%s]", e->key, e->value);
81 for (e = NULL; (e = av_dict_iterate(attributes, e)); )
82 printf(" [%s]=[%s]", e->key, e->value);
83 domain = ff_http_cookie_domain(attributes);
84 printf(" domain=%s expiry=%"PRId64"\n", domain ? domain : "-",
85 FFMIN(ff_http_cookie_expiry(attributes), INT32_MAX));
86 }
87 av_dict_free(&pair);
88 av_dict_free(&attributes);
89 }
90
91 for (int i = 0; i < FF_ARRAY_ELEMS(dates); i++) {
92 struct tm tm;
93
94 if (ff_http_parse_date(dates[i], &tm) < 0)
95 printf("%s: invalid\n", dates[i]);
96 else
97 printf("%s: %"PRId64"\n", dates[i], (int64_t)av_timegm(&tm));
98 }
99
100 if (!bound || ff_http_bind_cookies_to_host(&bound, "example.com") < 0)
101 return 1;
102 printf("%s", bound);
103 av_free(bound);
104 return 0;
105}
static double bound(const double threshold, const double val)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
__device__ int printf(const char *,...)
int ret
Definition ffplay.c:391
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:43
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
const char * ff_http_cookie_domain(const AVDictionary *cookie_params)
Definition httpcookie.c:187
int64_t ff_http_cookie_expiry(const AVDictionary *attributes)
Definition httpcookie.c:195
int ff_http_parse_set_cookie(const char *set_cookie, AVDictionary **cookie, AVDictionary **attributes)
Parse a Set-Cookie style line.
Definition httpcookie.c:132
int ff_http_parse_date(const char *date_str, struct tm *buf)
Parse an HTTP date like "Wed, 21 Oct 2015 07:28:00 GMT".
Definition httpcookie.c:69
int ff_http_bind_cookies_to_host(char **cookies, const char *host)
Restrict the cookies without a Domain to host.
Definition httpcookie.c:211
Utility Preprocessor macros.
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
#define av_strdup(s)
Definition ops_static.c:55
time_t av_timegm(struct tm *tm)
Convert the decomposed UTC time in tm to a time_t value, saturated to the range of time_t.
Definition parseutils.c:573
misc parsing utilities
#define FF_ARRAY_ELEMS(a)
char * key
Definition dict.h:91
char * value
Definition dict.h:92
#define av_free(p)
static const char *const cookies[]
Definition httpcookie.c:27
int main(void)
Definition httpcookie.c:64
static const char *const dates[]
Definition httpcookie.c:52