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 <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22#include <time.h>
23
24#include "libavutil/avstring.h"
25#include "libavutil/bprint.h"
26#include "libavutil/common.h"
27#include "libavutil/mem.h"
29
30#include "http.h"
31
32#define WHITESPACES " \n\t\r"
33
34static int is_date_delimiter(uint8_t c)
35{
36 return c == 0x09 || (c >= 0x20 && c <= 0x2F) || (c >= 0x3B && c <= 0x40) ||
37 (c >= 0x5B && c <= 0x60) || (c >= 0x7B && c <= 0x7E);
38}
39
40/* Parse min to max digits not followed by a digit, return their number or 0. */
41static int parse_digits(const char *str, int min, int max, int *val)
42{
43 int n;
44
45 for (n = *val = 0; n < max && av_isdigit(str[n]); n++)
46 *val = *val * 10 + str[n] - '0';
47 return n >= min && !av_isdigit(str[n]) ? n : 0;
48}
49
50static int parse_time(const char *str, struct tm *tm)
51{
52 int n, m;
53
54 return (n = parse_digits(str, 1, 2, &tm->tm_hour)) && str[n++] == ':' &&
55 (m = parse_digits(str + n, 1, 2, &tm->tm_min)) && str[n + m++] == ':' &&
56 parse_digits(str + n + m, 1, 2, &tm->tm_sec);
57}
58
59static int parse_month(const char *str)
60{
61 static const char months[] = "janfebmaraprmayjunjulaugsepoctnovdec";
62
63 for (int i = 0; i < 12; i++)
64 if (!av_strncasecmp(str, months + 3 * i, 3))
65 return i;
66 return -1;
67}
68
69int ff_http_parse_date(const char *date_str, struct tm *buf)
70{
71 static const uint8_t month_days[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
72 int found_time = 0, found_day = 0, found_month = 0, found_year = 0, month, year;
73 struct tm tm = { 0 };
74
75 // cookie-date algorithm of RFC 6265 5.1.1, it accepts all HTTP-date formats too
76 while (*date_str) {
77 while (*date_str && is_date_delimiter(*date_str))
78 date_str++;
79 if (!*date_str)
80 break;
81 if (!found_time && parse_time(date_str, &tm)) {
82 found_time = 1;
83 } else if (!found_day && parse_digits(date_str, 1, 2, &tm.tm_mday)) {
84 found_day = 1;
85 } else if (!found_month && (month = parse_month(date_str)) >= 0) {
86 tm.tm_mon = month;
87 found_month = 1;
88 } else if (!found_year && parse_digits(date_str, 2, 4, &year)) {
89 found_year = 1;
90 }
91 while (*date_str && !is_date_delimiter(*date_str))
92 date_str++;
93 }
94
95 if (!found_time || !found_day || !found_month || !found_year)
96 return AVERROR(EINVAL);
97 year += year < 70 ? 2000 : year < 100 ? 1900 : 0;
98 if (year < 1601 || tm.tm_hour > 23 || tm.tm_min > 59 || tm.tm_sec > 59 ||
99 tm.tm_mday < 1 || tm.tm_mday > month_days[tm.tm_mon] ||
100 (tm.tm_mon == 1 && tm.tm_mday == 29 && (year % 4 || (year % 100 == 0 && year % 400))))
101 return AVERROR(EINVAL);
102 tm.tm_year = year - 1900;
103 *buf = tm;
104 return 0;
105}
106
107static char *trim(char *str)
108{
109 char *end;
110
111 str += strspn(str, WHITESPACES);
112 end = str + strlen(str);
113 while (end > str && strchr(WHITESPACES, end[-1]))
114 *--end = '\0';
115 return str;
116}
117
118static int has_control_char(const char *str)
119{
120 for (; *str; str++)
121 if (((uint8_t)*str < 0x20 && *str != '\t') || *str == 0x7f)
122 return 1;
123 return 0;
124}
125
126static int is_delta_seconds(const char *str)
127{
128 str += *str == '-';
129 return *str && !str[strspn(str, "0123456789")];
130}
131
132int ff_http_parse_set_cookie(const char *set_cookie, AVDictionary **cookie, AVDictionary **attributes)
133{
134 char *param, *next_param, *cstr, *eql;
135 char *saveptr = NULL;
136 int ret;
137
138 if (!(cstr = av_strdup(set_cookie)))
139 return AVERROR(ENOMEM);
140
141 // a line with control characters other than HTAB holds no cookie (RFC 6265bis 5.6)
142 param = trim(cstr);
143 if (has_control_char(param)) {
144 av_free(cstr);
145 return AVERROR_INVALIDDATA;
146 }
147
148 // the name and value of the cookie come before the first ';'
149 next_param = strchr(param, ';');
150 if (next_param)
151 *next_param++ = '\0';
152 if (!(eql = strchr(param, '=')) || eql == param) {
153 av_free(cstr);
154 return AVERROR_INVALIDDATA;
155 }
156 *eql = '\0';
157 ret = av_dict_set(cookie, trim(param), trim(eql + 1), 0);
158
159 while (ret >= 0 && (param = av_strtok(next_param, ";", &saveptr))) {
160 char *value = strchr(param, '=');
161 next_param = NULL;
162 if (value)
163 *value++ = '\0';
164 param = trim(param);
165 value = value ? trim(value) : NULL;
166 // an invalid Max-Age is ignored (RFC 6265 5.2.2)
167 if (!av_strcasecmp(param, "max-age") && (!value || !is_delta_seconds(value)))
168 continue;
169 // an Expires without a valid date and an empty Domain are ignored (RFC 6265 5.2.1, 5.2.3)
170 if (!av_strcasecmp(param, "expires") && (!value || ff_http_parse_date(value, &(struct tm){ 0 }) < 0))
171 continue;
172 if (!av_strcasecmp(param, "domain") && (!value || !*value))
173 continue;
174 if (*param)
175 ret = av_dict_set(attributes, param, value ? value : "", 0);
176 }
177
178 av_free(cstr);
179 return ret < 0 ? ret : 0;
180}
181
183{
184 return !host[strspn(host, "0123456789.")] || strchr(host, ':');
185}
186
187const char *ff_http_cookie_domain(const AVDictionary *cookie_params)
188{
189 const AVDictionaryEntry *e = av_dict_get(cookie_params, "domain", NULL, 0);
190 const char *domain = e ? e->value + (e->value[0] == '.') : "";
191
192 return *domain ? domain : NULL;
193}
194
196{
197 const AVDictionaryEntry *max_age = av_dict_get(attributes, "max-age", NULL, 0);
198 const AVDictionaryEntry *expires = av_dict_get(attributes, "expires", NULL, 0);
199 struct tm tm = { 0 };
200
201 // Max-Age takes precedence over Expires (RFC 6265 5.3)
202 if (max_age) {
203 int64_t delta = strtoll(max_age->value, NULL, 10);
204 return delta > 0 ? av_sat_add64(time(NULL), delta) : 1;
205 }
206 if (expires && !ff_http_parse_date(expires->value, &tm))
207 return FFMAX(av_timegm(&tm), 1);
208 return 0;
209}
210
212{
213 char *copy, *cookie, *next, *saveptr = NULL, *bound;
214 AVBPrint bp;
215 int ret = 0;
216
217 next = copy = av_strdup(*cookies);
218 if (!copy)
219 return AVERROR(ENOMEM);
220
222 while ((cookie = av_strtok(next, "\n", &saveptr))) {
223 AVDictionary *pair = NULL, *params = NULL;
224 int len = strlen(cookie), host_only;
225
226 next = NULL;
227 while (len && strchr(WHITESPACES, cookie[len - 1]))
228 len--;
229 ret = ff_http_parse_set_cookie(cookie, &pair, &params);
230 host_only = !ff_http_cookie_domain(params) && !av_dict_get(params, "@hostonly", NULL, 0);
231 av_dict_free(&pair);
233 if (ret == AVERROR(ENOMEM))
234 break;
235 ret = 0;
236 av_bprintf(&bp, "%.*s%s%s\n", len, cookie, host_only ? "; @hostonly=" : "", host_only ? host : "");
237 }
238 av_free(copy);
239 if (ret >= 0 && !av_bprint_is_complete(&bp))
240 ret = AVERROR(ENOMEM);
241 if (ret < 0) {
243 return ret;
244 }
245 if ((ret = av_bprint_finalize(&bp, &bound)) < 0)
246 return ret;
248 *cookies = bound;
249 return 0;
250}
SwsAArch64OpImplParams params
Definition ops.c:52
static double val(void *priv, double ch)
Definition aeval.c:77
static double bound(const double threshold, const double val)
static const char months[12][4]
Definition avidec.c:349
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:121
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
AVBPrint public header.
#define AV_BPRINT_SIZE_UNLIMITED
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
common internal and external API header
#define av_sat_add64
Definition common.h:139
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define min(a, b)
#define max(a, b)
double value
Definition eval.c:102
int ret
Definition ffplay.c:391
#define WHITESPACES
Definition graphparser.c:35
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition bprint.h:218
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:61
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition avstring.c:179
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition avstring.h:202
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition avstring.c:218
const char * ff_http_cookie_domain(const AVDictionary *cookie_params)
Definition httpcookie.c:187
static int parse_month(const char *str)
Definition httpcookie.c:59
int ff_http_host_is_ip_literal(const char *host)
Definition httpcookie.c:182
static int parse_digits(const char *str, int min, int max, int *val)
Definition httpcookie.c:41
static int is_date_delimiter(uint8_t c)
Definition httpcookie.c:34
int64_t ff_http_cookie_expiry(const AVDictionary *attributes)
Definition httpcookie.c:195
static char * trim(char *str)
Definition httpcookie.c:107
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
static int has_control_char(const char *str)
Definition httpcookie.c:118
static int parse_time(const char *str, struct tm *tm)
Definition httpcookie.c:50
int ff_http_bind_cookies_to_host(char **cookies, const char *host)
Restrict the cookies without a Domain to host.
Definition httpcookie.c:211
static int is_delta_seconds(const char *str)
Definition httpcookie.c:126
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
char * host
Definition mscl.c:249
#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
char * value
Definition dict.h:92
#define av_free(p)
static const char *const cookies[]
Definition httpcookie.c:27
static void copy(const float *p1, float *p2, const int length)
float delta
int len
static double c[64]