../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
26 / 1422
1.8%
Branches:
25 / 1191
2.1%
Functions:
1 / 60
1.7%

libavformat/http.c
Line Branch Exec Source
1 /*
2 * HTTP protocol for ffmpeg client
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdbool.h>
23
24 #include "config.h"
25 #include "config_components.h"
26
27 #include <string.h>
28 #include <time.h>
29 #if CONFIG_ZLIB
30 #include <zlib.h>
31 #endif /* CONFIG_ZLIB */
32
33 #include "libavutil/avassert.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/bprint.h"
36 #include "libavutil/getenv_utf8.h"
37 #include "libavutil/macros.h"
38 #include "libavutil/mem.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/time.h"
41 #include "libavutil/parseutils.h"
42
43 #include "avformat.h"
44 #include "http.h"
45 #include "httpauth.h"
46 #include "internal.h"
47 #include "network.h"
48 #include "os_support.h"
49 #include "url.h"
50 #include "version.h"
51
52 /* XXX: POST protocol is not completely implemented because ffmpeg uses
53 * only a subset of it. */
54
55 /* The IO buffer size is unrelated to the max URL size in itself, but needs
56 * to be large enough to fit the full request headers (including long
57 * path names). */
58 #define BUFFER_SIZE (MAX_URL_SIZE + HTTP_HEADERS_SIZE)
59 #define MAX_REDIRECTS 8
60 #define MAX_CACHED_REDIRECTS 32
61 #define HTTP_SINGLE 1
62 #define HTTP_MUTLI 2
63 #define MAX_DATE_LEN 19
64 #define WHITESPACES " \n\t\r"
65 typedef enum {
66 LOWER_PROTO,
67 READ_HEADERS,
68 WRITE_REPLY_HEADERS,
69 FINISH
70 }HandshakeState;
71
72 typedef struct HTTPContext {
73 const AVClass *class;
74 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
75
76 /*************************
77 * Configuration options *
78 *************************/
79 uint64_t off, end_off; /* `off` is also mutated by seeking / reading */
80 char *location;
81 char *http_proxy;
82 char *headers;
83 char *mime_type;
84 char *http_version;
85 char *user_agent;
86 char *referer;
87 char *content_type;
88 int seekable; /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
89 int chunked_post;
90 int multiple_requests; /**< A flag which indicates if we use persistent connections. */
91 uint8_t *post_data;
92 int post_datalen;
93 char *cookies; ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
94 char *host;
95 int icy;
96 char *icy_metadata_headers;
97 char *icy_metadata_packet;
98 AVDictionary *metadata;
99 /* -1 = try to send if applicable, 0 = always disabled, 1 = always enabled */
100 int send_expect_100;
101 char *method;
102 int reconnect;
103 int reconnect_at_eof;
104 int reconnect_on_network_error;
105 int reconnect_streamed;
106 int reconnect_max_retries;
107 int reconnect_delay_max;
108 int reconnect_delay_total_max;
109 char *reconnect_on_http_error;
110 int listen;
111 char *resource;
112 int reply_code;
113 int short_seek_size;
114 int max_redirects;
115 int respect_retry_after;
116 uint64_t request_size;
117 uint64_t initial_request_size;
118
119 /**********************
120 * Context-wide state *
121 **********************/
122 HTTPAuthState auth_state; /* auth_state.auth_type is also a config option */
123 HTTPAuthState proxy_auth_state;
124 uint64_t filesize;
125 uint64_t oc_filesize; /* Complete size (200 Content-Length or Content-Range total) */
126 int end_of_resource; /* The end of an unknown-size resource was reached */
127 int is_akamai;
128 int is_mediagateway;
129 /* A dictionary containing cookies keyed by cookie name */
130 AVDictionary *cookie_dict;
131 AVDictionary *chained_options;
132 AVDictionary *redirect_cache;
133
134 /* Connection statistics */
135 int nb_connections;
136 int nb_requests;
137 int nb_retries;
138 int nb_reconnects;
139 int nb_redirects;
140 int64_t sum_latency; /* divide by nb_requests */
141 int64_t max_latency;
142
143 /************************
144 * Per-connection state *
145 ************************/
146 URLContext *hd;
147 char *uri;
148 char *new_location;
149 int http_code;
150 int64_t expires;
151 /* Used if "Transfer-Encoding: chunked" otherwise -1. */
152 uint64_t chunksize;
153 int chunkend;
154 uint64_t range_end;
155 /* Set if the server correctly handles Connection: close and will close
156 * the connection after feeding us the content. */
157 int willclose;
158 /* A flag which indicates if the end of chunked encoding has been sent. */
159 int end_chunked_post;
160 /* A flag which indicates we have finished to read POST reply. */
161 int end_header;
162 /* how much data was read since the last ICY metadata packet */
163 uint64_t icy_data_read;
164 /* after how many bytes of read data a new metadata packet will be found */
165 uint64_t icy_metaint;
166 #if CONFIG_ZLIB
167 int compressed;
168 z_stream inflate_stream;
169 uint8_t *inflate_buffer;
170 #endif /* CONFIG_ZLIB */
171 unsigned int retry_after;
172 int initial_requests; /* whether or not to limit requests to initial_request_size */
173
174 /* Temporary during header parsing */
175 uint64_t filesize_from_content_range;
176 /* Reply is a partial response with unknown total ("bytes A-B/" + '*'). */
177 int content_range_unknown_size;
178 int line_count;
179
180 /******************
181 * Listener state *
182 ******************/
183 /* URLContext *hd; */
184 HandshakeState handshake_step;
185 int is_multi_client;
186 int is_connected_server;
187 } HTTPContext;
188
189 #define OFFSET(x) offsetof(HTTPContext, x)
190 #define D AV_OPT_FLAG_DECODING_PARAM
191 #define E AV_OPT_FLAG_ENCODING_PARAM
192 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
193
194 static const AVOption http_options[] = {
195 { "seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D },
196 { "chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
197 { "http_proxy", "set HTTP proxy to tunnel through", OFFSET(http_proxy), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
198 { "headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
199 { "content_type", "set a specific content type for the POST messages", OFFSET(content_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
200 { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
201 { "referer", "override referer header", OFFSET(referer), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
202 { "multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D | E },
203 { "request_size", "size (in bytes) of requests to make", OFFSET(request_size), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
204 { "initial_request_size", "size (in bytes) of initial requests made during probing / header parsing", OFFSET(initial_request_size), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
205 { "post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D | E },
206 { "mime_type", "export the MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
207 { "http_version", "export the http response version", OFFSET(http_version), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
208 { "cookies", "set cookies to be sent in applicable future requests, use newline delimited Set-Cookie HTTP field value syntax", OFFSET(cookies), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
209 { "icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, D },
210 { "icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
211 { "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
212 { "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
213 { "auth_type", "HTTP authentication type", OFFSET(auth_state.auth_type), AV_OPT_TYPE_INT, { .i64 = HTTP_AUTH_NONE }, HTTP_AUTH_NONE, HTTP_AUTH_BASIC, D | E, .unit = "auth_type"},
214 { "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, .unit = "auth_type"},
215 { "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 0, 0, D | E, .unit = "auth_type"},
216 { "send_expect_100", "Force sending an Expect: 100-continue header for POST", OFFSET(send_expect_100), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, E },
217 { "location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
218 { "offset", "initial byte offset", OFFSET(off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
219 { "end_offset", "try to limit the request to bytes preceding this offset", OFFSET(end_off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
220 { "method", "Override the HTTP method or set the expected HTTP method from a client", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
221 { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
222 { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
223 { "reconnect_on_network_error", "auto reconnect in case of tcp/tls error during connect", OFFSET(reconnect_on_network_error), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
224 { "reconnect_on_http_error", "list of http status codes to reconnect on", OFFSET(reconnect_on_http_error), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
225 { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
226 { "reconnect_delay_max", "max reconnect delay in seconds after which to give up", OFFSET(reconnect_delay_max), AV_OPT_TYPE_INT, { .i64 = 120 }, 0, UINT_MAX/1000/1000, D },
227 { "reconnect_max_retries", "the max number of times to retry a connection", OFFSET(reconnect_max_retries), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D },
228 { "reconnect_delay_total_max", "max total reconnect delay in seconds after which to give up", OFFSET(reconnect_delay_total_max), AV_OPT_TYPE_INT, { .i64 = 256 }, 0, UINT_MAX/1000/1000, D },
229 { "respect_retry_after", "respect the Retry-After header when retrying connections", OFFSET(respect_retry_after), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, D },
230 { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
231 { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
232 { "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
233 { "short_seek_size", "Threshold to favor readahead over seek.", OFFSET(short_seek_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
234 { "max_redirects", "Maximum number of redirects", OFFSET(max_redirects), AV_OPT_TYPE_INT, { .i64 = MAX_REDIRECTS }, 0, INT_MAX, D },
235 { NULL }
236 };
237
238 static int http_connect(URLContext *h, const char *path, const char *local_path,
239 const char *hoststr, const char *auth,
240 const char *proxyauth);
241 static int http_read_header(URLContext *h);
242 static int bind_cookies_to_host(HTTPContext *s);
243 static int http_shutdown(URLContext *h, int flags);
244
245 ✗ void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
246 {
247 ✗ memcpy(&((HTTPContext *)dest->priv_data)->auth_state,
248 ✗ &((HTTPContext *)src->priv_data)->auth_state,
249 sizeof(HTTPAuthState));
250 ✗ memcpy(&((HTTPContext *)dest->priv_data)->proxy_auth_state,
251 ✗ &((HTTPContext *)src->priv_data)->proxy_auth_state,
252 sizeof(HTTPAuthState));
253 ✗ }
254
255 ✗ static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
256 {
257 ✗ const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
258 char *env_http_proxy, *env_no_proxy;
259 char *hashmark;
260 char hostname[1024], hoststr[1024], proto[10], tmp_host[1024];
261 ✗ char auth[1024], proxyauth[1024] = "";
262 char path1[MAX_URL_SIZE], sanitized_path[MAX_URL_SIZE + 1];
263 char buf[1024], urlbuf[MAX_URL_SIZE];
264 ✗ int port, use_proxy, err = 0;
265 ✗ HTTPContext *s = h->priv_data;
266
267 ✗ av_url_split(proto, sizeof(proto), auth, sizeof(auth),
268 hostname, sizeof(hostname), &port,
269 ✗ path1, sizeof(path1), s->location);
270
271 ✗ av_freep(&s->host);
272 ✗ s->host = av_strdup(hostname);
273
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (!s->host)
274 ✗ return AVERROR(ENOMEM);
275
276 ✗ av_strlcpy(tmp_host, hostname, sizeof(tmp_host));
277 // In case of an IPv6 address, we need to strip the Zone ID,
278 // if any. We do it at the first % sign, as percent encoding
279 // can be used in the Zone ID itself.
280
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (strchr(tmp_host, ':'))
281 ✗ tmp_host[strcspn(tmp_host, "%")] = '\0';
282 ✗ ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, tmp_host, port, NULL);
283
284 ✗ env_http_proxy = getenv_utf8("http_proxy");
285
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ proxy_path = s->http_proxy ? s->http_proxy : env_http_proxy;
286
287 ✗ env_no_proxy = getenv_utf8("no_proxy");
288
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 22 not taken.
✗ use_proxy = !ff_http_match_no_proxy(env_no_proxy, hostname) &&
289
0/4
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 22 not taken.
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ proxy_path && av_strstart(proxy_path, "http://", NULL);
290 ✗ freeenv_utf8(env_no_proxy);
291
292
0/4
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 29 not taken.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 29 not taken.
✗ if (h->protocol_whitelist && av_match_list(proto, h->protocol_whitelist, ',') <= 0) {
293 ✗ av_log(h, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", proto, h->protocol_whitelist);
294 ✗ return AVERROR(EINVAL);
295 }
296
297
0/4
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 34 not taken.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 34 not taken.
✗ if (h->protocol_blacklist && av_match_list(proto, h->protocol_blacklist, ',') > 0) {
298 ✗ av_log(h, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", proto, h->protocol_blacklist);
299 ✗ return AVERROR(EINVAL);
300 }
301
302
0/2
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 41 not taken.
✗ if (!strcmp(proto, "https")) {
303 ✗ lower_proto = "tls";
304 ✗ use_proxy = 0;
305
0/2
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
✗ if (port < 0)
306 ✗ port = 443;
307 /* pass http_proxy to underlying protocol */
308
0/2
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 43 not taken.
✗ if (s->http_proxy) {
309 ✗ err = av_dict_set(options, "http_proxy", s->http_proxy, 0);
310
0/2
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 43 not taken.
✗ if (err < 0)
311 ✗ goto end;
312 }
313
0/2
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 43 not taken.
✗ } else if (strcmp(proto, "http")) {
314 ✗ err = AVERROR(EINVAL);
315 ✗ goto end;
316 }
317
318
0/2
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 45 not taken.
✗ if (port < 0)
319 ✗ port = 80;
320
321 ✗ hashmark = strchr(path1, '#');
322
0/2
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 47 not taken.
✗ if (hashmark)
323 ✗ *hashmark = '\0';
324
325
0/2
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 49 not taken.
✗ if (path1[0] == '\0') {
326 ✗ path = "/";
327
0/2
✗ Branch 49 → 50 not taken.
✗ Branch 49 → 51 not taken.
✗ } else if (path1[0] == '?') {
328 ✗ snprintf(sanitized_path, sizeof(sanitized_path), "/%s", path1);
329 ✗ path = sanitized_path;
330 } else {
331 ✗ path = path1;
332 }
333 ✗ local_path = path;
334
0/2
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 55 not taken.
✗ if (use_proxy) {
335 /* Reassemble the request URL without auth string - we don't
336 * want to leak the auth to the proxy. */
337 ✗ ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
338 path1);
339 ✗ path = urlbuf;
340 ✗ av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
341 hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
342 }
343
344 ✗ ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
345
346
0/2
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 58 not taken.
✗ if (!s->hd) {
347 ✗ s->nb_connections++;
348 ✗ err = ffurl_open_whitelist(&s->hd, buf, AVIO_FLAG_READ_WRITE,
349 ✗ &h->interrupt_callback, options,
350 h->protocol_whitelist, h->protocol_blacklist, h);
351 }
352
353 ✗ end:
354 ✗ freeenv_utf8(env_http_proxy);
355
0/2
✗ Branch 60 → 61 not taken.
✗ Branch 60 → 62 not taken.
✗ return err < 0 ? err : http_connect(
356 h, path, local_path, hoststr, auth, proxyauth);
357 }
358
359 ✗ static int http_should_reconnect(HTTPContext *s, int err)
360 {
361 const char *status_group;
362 char http_code[4];
363
364
0/3
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ Branch 2 → 5 not taken.
✗ switch (err) {
365 ✗ case AVERROR_HTTP_BAD_REQUEST:
366 case AVERROR_HTTP_UNAUTHORIZED:
367 case AVERROR_HTTP_FORBIDDEN:
368 case AVERROR_HTTP_NOT_FOUND:
369 case AVERROR_HTTP_TOO_MANY_REQUESTS:
370 case AVERROR_HTTP_OTHER_4XX:
371 ✗ status_group = "4xx";
372 ✗ break;
373
374 ✗ case AVERROR_HTTP_SERVER_ERROR:
375 ✗ status_group = "5xx";
376 ✗ break;
377
378 ✗ default:
379 ✗ return s->reconnect_on_network_error;
380 }
381
382
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (!s->reconnect_on_http_error)
383 ✗ return 0;
384
385
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ if (av_match_list(status_group, s->reconnect_on_http_error, ',') > 0)
386 ✗ return 1;
387
388 ✗ snprintf(http_code, sizeof(http_code), "%d", s->http_code);
389
390 ✗ return av_match_list(http_code, s->reconnect_on_http_error, ',') > 0;
391 }
392
393 ✗ static char *redirect_cache_get(HTTPContext *s)
394 {
395 AVDictionaryEntry *re;
396 int64_t expiry;
397 char *delim;
398
399 ✗ re = av_dict_get(s->redirect_cache, s->location, NULL, AV_DICT_MATCH_CASE);
400
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (!re) {
401 ✗ return NULL;
402 }
403
404 ✗ delim = strchr(re->value, ';');
405
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (!delim) {
406 ✗ return NULL;
407 }
408
409 ✗ expiry = strtoll(re->value, NULL, 10);
410
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ if (time(NULL) > expiry) {
411 ✗ return NULL;
412 }
413
414 ✗ return delim + 1;
415 }
416
417 ✗ static int redirect_cache_set(HTTPContext *s, const char *source, const char *dest, int64_t expiry)
418 {
419 char *value;
420 int ret;
421
422 ✗ value = av_asprintf("%"PRIi64";%s", expiry, dest);
423
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (!value) {
424 ✗ return AVERROR(ENOMEM);
425 }
426
427 ✗ ret = av_dict_set(&s->redirect_cache, source, value, AV_DICT_MATCH_CASE | AV_DICT_DONT_STRDUP_VAL);
428
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (ret < 0)
429 ✗ return ret;
430
431 ✗ return 0;
432 }
433
434 /* The redirects that http_open_cnx() follows. */
435 ✗ static int is_redirect(int http_code)
436 {
437
0/6
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 7 not taken.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 7 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ return http_code == 301 || http_code == 302 || http_code == 303 ||
438
0/4
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 7 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ http_code == 307 || http_code == 308;
439 }
440
441 /* A 416 reply for a resource of unknown size marks its end, unless it states
442 * a length past the refused offset. */
443 ✗ static int is_end_of_resource(HTTPContext *s, int err, uint64_t off)
444 {
445
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 8 not taken.
✗ return err == AVERROR_HTTP_OTHER_4XX && s->http_code == 416 &&
446
0/4
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 8 not taken.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 8 not taken.
✗ s->oc_filesize == UINT64_MAX &&
447
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ (s->filesize_from_content_range == UINT64_MAX ||
448
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ s->filesize_from_content_range <= off);
449 }
450
451 /* Return non zero if error. With end_is_eof, the caller reports the end of a
452 * resource of unknown size as EOF, and the 416 that marks it is retried only
453 * if reconnect_on_http_error lists 416 by number. */
454 ✗ static int http_open_cnx(URLContext *h, AVDictionary **options, int end_is_eof)
455 {
456 HTTPAuthType cur_auth_type, cur_proxy_auth_type;
457 ✗ HTTPContext *s = h->priv_data;
458 ✗ int ret, conn_attempts = 1, auth_attempts = 0, redirects = 0;
459 ✗ int reconnect_delay = 0;
460 ✗ int reconnect_delay_total = 0;
461 ✗ uint64_t off = s->off;
462 ✗ uint64_t filesize = s->filesize;
463 ✗ uint64_t oc_filesize = s->oc_filesize;
464 char *cached;
465
466 ✗ redo:
467 /* every attempt starts with the complete size known before the call */
468 ✗ s->oc_filesize = oc_filesize;
469
470 ✗ cached = redirect_cache_get(s);
471
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 13 not taken.
✗ if (cached) {
472
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (redirects++ >= s->max_redirects) {
473 ✗ ret = AVERROR(EIO);
474 ✗ goto fail;
475 }
476
477 ✗ av_free(s->location);
478 ✗ s->location = av_strdup(cached);
479
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (!s->location) {
480 ✗ ret = AVERROR(ENOMEM);
481 ✗ goto fail;
482 }
483 ✗ goto redo;
484 }
485
486 ✗ av_dict_copy(options, s->chained_options, 0);
487
488 ✗ cur_auth_type = s->auth_state.auth_type;
489 ✗ cur_proxy_auth_type = s->auth_state.auth_type;
490
491 ✗ off = s->off;
492 ✗ s->http_code = 0;
493 ✗ ret = http_open_cnx_internal(h, options);
494
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 42 not taken.
✗ if (ret < 0) {
495
0/4
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 27 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 23 not taken.
✗ if (!http_should_reconnect(s, ret) ||
496
0/4
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 23 not taken.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 27 not taken.
✗ (end_is_eof && is_end_of_resource(s, ret, off) &&
497 ✗ !av_match_list("416", s->reconnect_on_http_error, ',')) ||
498
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 27 not taken.
✗ reconnect_delay > s->reconnect_delay_max ||
499
0/4
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 27 not taken.
✗ (s->reconnect_max_retries >= 0 && conn_attempts > s->reconnect_max_retries) ||
500
0/2
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ reconnect_delay_total > s->reconnect_delay_total_max)
501 ✗ goto fail;
502
503 /* Both fields here are in seconds. */
504
0/4
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 33 not taken.
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 33 not taken.
✗ if (s->respect_retry_after && s->retry_after > 0) {
505 ✗ reconnect_delay = s->retry_after;
506
0/2
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 32 not taken.
✗ if (reconnect_delay > s->reconnect_delay_max)
507 ✗ goto fail;
508 ✗ s->retry_after = 0;
509 ✗ s->nb_retries++;
510 }
511
512 ✗ av_log(h, AV_LOG_WARNING, "Will %s at %"PRIu64" in %d second(s).\n",
513
0/2
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 35 not taken.
✗ s->willclose ? "reconnect" : "retry", off, reconnect_delay);
514 ✗ ret = ff_network_sleep_interruptible(1000U * 1000 * reconnect_delay, &h->interrupt_callback);
515
0/2
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 40 not taken.
✗ if (ret != AVERROR(ETIMEDOUT))
516 ✗ goto fail;
517 ✗ reconnect_delay_total += reconnect_delay;
518 ✗ reconnect_delay = 1 + 2 * reconnect_delay;
519 ✗ s->nb_reconnects++;
520 ✗ conn_attempts++;
521
522 /* restore the offset (http_connect resets it) */
523 ✗ s->off = off;
524
525 ✗ ffurl_closep(&s->hd);
526 ✗ goto redo;
527 }
528
529 ✗ auth_attempts++;
530
0/2
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 50 not taken.
✗ if (s->http_code == 401) {
531
0/4
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 45 not taken.
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 49 not taken.
✗ if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
532
0/4
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 49 not taken.
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 49 not taken.
✗ s->auth_state.auth_type != HTTP_AUTH_NONE && auth_attempts < 4) {
533 ✗ ffurl_closep(&s->hd);
534 ✗ goto redo;
535 } else
536 ✗ goto fail;
537 }
538
0/2
✗ Branch 50 → 51 not taken.
✗ Branch 50 → 58 not taken.
✗ if (s->http_code == 407) {
539
0/4
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 53 not taken.
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 57 not taken.
✗ if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
540
0/4
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 57 not taken.
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 57 not taken.
✗ s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && auth_attempts < 4) {
541 ✗ ffurl_closep(&s->hd);
542 ✗ goto redo;
543 } else
544 ✗ goto fail;
545 }
546
0/4
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 77 not taken.
✗ Branch 60 → 61 not taken.
✗ Branch 60 → 77 not taken.
✗ if (is_redirect(s->http_code) && s->new_location) {
547 /* url moved, get next */
548 ✗ ffurl_closep(&s->hd);
549
0/2
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 64 not taken.
✗ if (redirects++ >= s->max_redirects) {
550 ✗ ret = AVERROR(EIO);
551 ✗ goto fail;
552 }
553
554
0/2
✗ Branch 64 → 65 not taken.
✗ Branch 64 → 70 not taken.
✗ if (!s->expires) {
555
0/4
✗ Branch 65 → 66 not taken.
✗ Branch 65 → 67 not taken.
✗ Branch 66 → 67 not taken.
✗ Branch 66 → 68 not taken.
✗ s->expires = (s->http_code == 301 || s->http_code == 308) ? INT64_MAX : -1;
556 }
557
558
0/4
✗ Branch 71 → 72 not taken.
✗ Branch 71 → 75 not taken.
✗ Branch 73 → 74 not taken.
✗ Branch 73 → 75 not taken.
✗ if (s->expires > time(NULL) && av_dict_count(s->redirect_cache) < MAX_CACHED_REDIRECTS) {
559 ✗ redirect_cache_set(s, s->location, s->new_location, s->expires);
560 }
561
562 ✗ av_free(s->location);
563 ✗ s->location = s->new_location;
564 ✗ s->new_location = NULL;
565 ✗ s->nb_redirects++;
566
567 /* Restart the authentication process with the new target, which
568 * might use a different auth mechanism. */
569 ✗ memset(&s->auth_state, 0, sizeof(s->auth_state));
570 ✗ auth_attempts = 0;
571 ✗ goto redo;
572 }
573 ✗ return 0;
574
575 ✗ fail:
576 ✗ s->off = off;
577 ✗ s->filesize = filesize;
578 ✗ s->oc_filesize = oc_filesize;
579
0/2
✗ Branch 78 → 79 not taken.
✗ Branch 78 → 80 not taken.
✗ if (s->hd)
580 ✗ ffurl_closep(&s->hd);
581
0/2
✗ Branch 80 → 81 not taken.
✗ Branch 80 → 82 not taken.
✗ if (ret < 0)
582 ✗ return ret;
583 ✗ return ff_http_averror(s->http_code, AVERROR(EIO));
584 }
585
586 ✗ int ff_http_do_new_request(URLContext *h, const char *uri) {
587 ✗ return ff_http_do_new_request2(h, uri, NULL);
588 }
589
590 ✗ int ff_http_do_new_request2(URLContext *h, const char *uri, AVDictionary **opts)
591 {
592 ✗ HTTPContext *s = h->priv_data;
593 ✗ AVDictionary *options = NULL;
594 int ret;
595 char hostname1[1024], hostname2[1024], proto1[10], proto2[10];
596 int port1, port2;
597
598
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 5 not taken.
✗ if (!h->prot ||
599
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 6 not taken.
✗ !(!strcmp(h->prot->name, "http") ||
600
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ !strcmp(h->prot->name, "https")))
601 ✗ return AVERROR(EINVAL);
602
603 ✗ av_url_split(proto1, sizeof(proto1), NULL, 0,
604 hostname1, sizeof(hostname1), &port1,
605 ✗ NULL, 0, s->location);
606 ✗ av_url_split(proto2, sizeof(proto2), NULL, 0,
607 hostname2, sizeof(hostname2), &port2,
608 NULL, 0, uri);
609
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 11 not taken.
✗ if (strcmp(proto1, proto2) != 0) {
610 ✗ av_log(h, AV_LOG_INFO, "Cannot reuse HTTP connection for different protocol %s vs %s\n",
611 proto1, proto2);
612 ✗ return AVERROR(EINVAL);
613 }
614
0/4
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 15 not taken.
✗ if (port1 != port2 || strncmp(hostname1, hostname2, sizeof(hostname2)) != 0) {
615 ✗ av_log(h, AV_LOG_INFO, "Cannot reuse HTTP connection for different host: %s:%d != %s:%d\n",
616 hostname1, port1,
617 hostname2, port2
618 );
619 ✗ return AVERROR(EINVAL);
620 }
621
622
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 19 not taken.
✗ if (!s->end_chunked_post) {
623 ✗ ret = http_shutdown(h, h->flags);
624
0/2
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ if (ret < 0)
625 ✗ return ret;
626 }
627
628
0/2
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ if (s->willclose)
629 ✗ return AVERROR_EOF;
630
631 ✗ s->end_chunked_post = 0;
632 ✗ s->chunkend = 0;
633 ✗ s->range_end = 0;
634 ✗ s->off = 0;
635 ✗ s->oc_filesize = UINT64_MAX;
636 ✗ s->end_of_resource = 0;
637 ✗ s->icy_data_read = 0;
638
639 ✗ av_free(s->location);
640 ✗ s->location = av_strdup(uri);
641
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ if (!s->location)
642 ✗ return AVERROR(ENOMEM);
643
644 ✗ av_free(s->uri);
645 ✗ s->uri = av_strdup(uri);
646
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ if (!s->uri)
647 ✗ return AVERROR(ENOMEM);
648
649
0/2
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 32 not taken.
✗ if ((ret = av_opt_set_dict(s, opts)) < 0)
650 ✗ return ret;
651
652
0/2
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 34 not taken.
✗ av_log(s, AV_LOG_INFO, "Opening \'%s\' for %s\n", uri, h->flags & AVIO_FLAG_WRITE ? "writing" : "reading");
653 ✗ ret = http_open_cnx(h, &options, 0);
654 ✗ av_dict_free(&options);
655 ✗ return ret;
656 }
657
658 ✗ const char* ff_http_get_new_location(URLContext *h)
659 {
660 ✗ HTTPContext *s = h->priv_data;
661 ✗ return s->new_location;
662 }
663
664 ✗ static int http_write_reply(URLContext* h, int status_code)
665 {
666 ✗ int ret, body = 0, reply_code, message_len;
667 const char *reply_text, *content_type;
668 ✗ HTTPContext *s = h->priv_data;
669 char message[BUFFER_SIZE];
670 ✗ content_type = "text/plain";
671
672
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (status_code < 0)
673 ✗ body = 1;
674
0/7
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ Branch 4 → 7 not taken.
✗ Branch 4 → 8 not taken.
✗ Branch 4 → 9 not taken.
✗ Branch 4 → 13 not taken.
✗ Branch 4 → 14 not taken.
✗ switch (status_code) {
675 ✗ case AVERROR_HTTP_BAD_REQUEST:
676 case 400:
677 ✗ reply_code = 400;
678 ✗ reply_text = "Bad Request";
679 ✗ break;
680 ✗ case AVERROR_HTTP_FORBIDDEN:
681 case 403:
682 ✗ reply_code = 403;
683 ✗ reply_text = "Forbidden";
684 ✗ break;
685 ✗ case AVERROR_HTTP_NOT_FOUND:
686 case 404:
687 ✗ reply_code = 404;
688 ✗ reply_text = "Not Found";
689 ✗ break;
690 ✗ case AVERROR_HTTP_TOO_MANY_REQUESTS:
691 case 429:
692 ✗ reply_code = 429;
693 ✗ reply_text = "Too Many Requests";
694 ✗ break;
695 ✗ case 200:
696 ✗ reply_code = 200;
697 ✗ reply_text = "OK";
698
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ content_type = s->content_type ? s->content_type : "application/octet-stream";
699 ✗ break;
700 ✗ case AVERROR_HTTP_SERVER_ERROR:
701 case 500:
702 ✗ reply_code = 500;
703 ✗ reply_text = "Internal server error";
704 ✗ break;
705 ✗ default:
706 ✗ return AVERROR(EINVAL);
707 }
708
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 20 not taken.
✗ if (body) {
709 ✗ s->chunked_post = 0;
710 ✗ message_len = snprintf(message, sizeof(message),
711 "HTTP/1.1 %03d %s\r\n"
712 "Content-Type: %s\r\n"
713 "Content-Length: %zu\r\n"
714 "%s"
715 "\r\n"
716 "%03d %s\r\n",
717 reply_code,
718 reply_text,
719 content_type,
720 ✗ strlen(reply_text) + 6, // 3 digit status code + space + \r\n
721
0/2
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ s->headers ? s->headers : "",
722 reply_code,
723 reply_text);
724 } else {
725 ✗ s->chunked_post = 1;
726 ✗ message_len = snprintf(message, sizeof(message),
727 "HTTP/1.1 %03d %s\r\n"
728 "Content-Type: %s\r\n"
729 "Transfer-Encoding: chunked\r\n"
730 "%s"
731 "\r\n",
732 reply_code,
733 reply_text,
734 content_type,
735
0/2
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ s->headers ? s->headers : "");
736 }
737 ✗ av_log(h, AV_LOG_TRACE, "HTTP reply header: \n%s----\n", message);
738
0/2
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ if ((ret = ffurl_write(s->hd, message, message_len)) < 0)
739 ✗ return ret;
740 ✗ return 0;
741 }
742
743 ✗ static void handle_http_errors(URLContext *h, int error)
744 {
745
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 5 not taken.
✗ av_assert0(error < 0);
746 ✗ http_write_reply(h, error);
747 ✗ }
748
749 ✗ static int http_handshake(URLContext *c)
750 {
751 int ret, err;
752 ✗ HTTPContext *ch = c->priv_data;
753 ✗ URLContext *cl = ch->hd;
754
0/5
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 10 not taken.
✗ Branch 2 → 16 not taken.
✗ Branch 2 → 21 not taken.
✗ Branch 2 → 22 not taken.
✗ switch (ch->handshake_step) {
755 ✗ case LOWER_PROTO:
756 ✗ av_log(c, AV_LOG_TRACE, "Lower protocol\n");
757
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if ((ret = ffurl_handshake(cl)) > 0)
758 ✗ return 2 + ret;
759
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ if (ret < 0)
760 ✗ return ret;
761 ✗ ch->handshake_step = READ_HEADERS;
762 ✗ ch->is_connected_server = 1;
763 ✗ return 2;
764 ✗ case READ_HEADERS:
765 ✗ av_log(c, AV_LOG_TRACE, "Read headers\n");
766
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 15 not taken.
✗ if ((err = http_read_header(c)) < 0) {
767 ✗ handle_http_errors(c, err);
768 ✗ return err;
769 }
770 ✗ ch->handshake_step = WRITE_REPLY_HEADERS;
771 ✗ return 1;
772 ✗ case WRITE_REPLY_HEADERS:
773 ✗ av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
774
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ if ((err = http_write_reply(c, ch->reply_code)) < 0)
775 ✗ return err;
776 ✗ ch->handshake_step = FINISH;
777 ✗ return 1;
778 ✗ case FINISH:
779 ✗ return 0;
780 }
781 // this should never be reached.
782 ✗ return AVERROR(EINVAL);
783 }
784
785 ✗ static int http_listen(URLContext *h, const char *uri, int flags,
786 AVDictionary **options) {
787 ✗ HTTPContext *s = h->priv_data;
788 int ret;
789 char hostname[1024], proto[10];
790 char lower_url[100];
791 ✗ const char *lower_proto = "tcp";
792 int port;
793 ✗ av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
794 NULL, 0, uri);
795
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (!strcmp(proto, "https"))
796 ✗ lower_proto = "tls";
797 ✗ ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
798 NULL);
799
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
800 ✗ goto fail;
801
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if ((ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
802 ✗ &h->interrupt_callback, options,
803 h->protocol_whitelist, h->protocol_blacklist, h
804 )) < 0)
805 ✗ goto fail;
806 ✗ s->handshake_step = LOWER_PROTO;
807
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ if (s->listen == HTTP_SINGLE) { /* single client */
808 ✗ s->reply_code = 200;
809
0/2
✗ Branch 16 → 15 not taken.
✗ Branch 16 → 17 not taken.
✗ while ((ret = http_handshake(h)) > 0);
810 }
811 ✗ fail:
812 ✗ av_dict_free(&s->chained_options);
813 ✗ av_dict_free(&s->cookie_dict);
814 ✗ return ret;
815 }
816
817 ✗ static int http_open(URLContext *h, const char *uri, int flags,
818 AVDictionary **options)
819 {
820 ✗ HTTPContext *s = h->priv_data;
821 int ret;
822
823
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if( s->seekable == 1 )
824 ✗ h->is_streamed = 0;
825 else
826 ✗ h->is_streamed = 1;
827
828
0/4
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ s->initial_requests = s->seekable != 0 && s->initial_request_size > 0;
829 ✗ s->filesize = UINT64_MAX;
830 ✗ s->oc_filesize = UINT64_MAX;
831 ✗ s->end_of_resource = 0;
832
833 ✗ s->location = av_strdup(uri);
834
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (!s->location)
835 ✗ return AVERROR(ENOMEM);
836
837 ✗ s->uri = av_strdup(uri);
838
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if (!s->uri)
839 ✗ return AVERROR(ENOMEM);
840
841
0/4
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 19 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ if (s->cookies && (ret = bind_cookies_to_host(s)) < 0)
842 ✗ goto bail_out;
843
844
0/2
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ if (options)
845 ✗ av_dict_copy(&s->chained_options, *options, 0);
846
847
0/2
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 29 not taken.
✗ if (s->headers) {
848 ✗ int len = strlen(s->headers);
849
0/4
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 29 not taken.
✗ if (len < 2 || strcmp("\r\n", s->headers + len - 2)) {
850 ✗ av_log(h, AV_LOG_WARNING,
851 "No trailing CRLF found in HTTP header. Adding it.\n");
852 ✗ ret = av_reallocp(&s->headers, len + 3);
853
0/2
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ if (ret < 0)
854 ✗ goto bail_out;
855 ✗ s->headers[len] = '\r';
856 ✗ s->headers[len + 1] = '\n';
857 ✗ s->headers[len + 2] = '\0';
858 }
859 }
860
861
0/2
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 32 not taken.
✗ if (s->listen) {
862 ✗ return http_listen(h, uri, flags, options);
863 }
864 ✗ ret = http_open_cnx(h, options, 0);
865 ✗ bail_out:
866
0/2
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 41 not taken.
✗ if (ret < 0) {
867 #if CONFIG_ZLIB
868 ✗ inflateEnd(&s->inflate_stream);
869 #endif
870 ✗ av_dict_free(&s->chained_options);
871 ✗ av_dict_free(&s->cookie_dict);
872 ✗ av_dict_free(&s->redirect_cache);
873 ✗ av_freep(&s->new_location);
874 ✗ av_freep(&s->uri);
875 ✗ av_freep(&s->host);
876 }
877 ✗ return ret;
878 }
879
880 ✗ static int http_accept(URLContext *s, URLContext **c)
881 {
882 int ret;
883 ✗ HTTPContext *sc = s->priv_data;
884 HTTPContext *cc;
885 ✗ URLContext *sl = sc->hd;
886 ✗ URLContext *cl = NULL;
887
888
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 5 not taken.
✗ av_assert0(sc->listen);
889
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
890 ✗ goto fail;
891 ✗ cc = (*c)->priv_data;
892
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ if ((ret = ffurl_accept(sl, &cl)) < 0)
893 ✗ goto fail;
894 ✗ cc->hd = cl;
895 ✗ cc->is_multi_client = 1;
896 ✗ return 0;
897 ✗ fail:
898
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ if (c) {
899 ✗ ffurl_closep(c);
900 }
901 ✗ return ret;
902 }
903
904 ✗ static int http_getc(HTTPContext *s)
905 {
906 int len;
907
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 9 not taken.
✗ if (s->buf_ptr >= s->buf_end) {
908 ✗ len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
909
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (len < 0) {
910 ✗ return len;
911
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ } else if (len == 0) {
912 ✗ return AVERROR_EOF;
913 } else {
914 ✗ s->buf_ptr = s->buffer;
915 ✗ s->buf_end = s->buffer + len;
916 }
917 }
918 ✗ return *s->buf_ptr++;
919 }
920
921 ✗ static int http_get_line(HTTPContext *s, char *line, int line_size)
922 {
923 int ch;
924 char *q;
925
926 ✗ q = line;
927 for (;;) {
928 ✗ ch = http_getc(s);
929
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (ch < 0)
930 ✗ return ch;
931
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 11 not taken.
✗ if (ch == '\n') {
932 /* process line */
933
0/4
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 10 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (q > line && q[-1] == '\r')
934 ✗ q--;
935 ✗ *q = '\0';
936
937 ✗ return 0;
938 } else {
939
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ if ((q - line) < line_size - 1)
940 ✗ *q++ = ch;
941 }
942 }
943 }
944
945 22 int ff_http_parse_status_line(void *logctx, const char *line, HTTPStatusLine *st)
946 {
947 const char *p;
948
949 22 memset(st, 0, sizeof(*st));
950
951
2/2
✓ Branch 3 → 4 taken 15 times.
✓ Branch 3 → 8 taken 7 times.
22 if (!av_strstart(line, "HTTP/", &p) ||
952
4/6
✓ Branch 4 → 5 taken 14 times.
✓ Branch 4 → 8 taken 1 time.
✓ Branch 5 → 6 taken 14 times.
✗ Branch 5 → 8 not taken.
✓ Branch 6 → 7 taken 14 times.
✗ Branch 6 → 8 not taken.
15 !av_isdigit(p[0]) || p[1] != '.' || !av_isdigit(p[2]) ||
953
2/2
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 10 taken 13 times.
14 !av_isspace(p[3])) {
954 9 av_log(logctx, AV_LOG_ERROR, "Malformed HTTP status line.\n");
955 9 return AVERROR_INVALIDDATA;
956 }
957
958 13 av_strlcpy(st->version, p, sizeof(st->version));
959 13 st->willclose = !strcmp(st->version, "1.0");
960
961 13 p += 3;
962
2/2
✓ Branch 13 → 12 taken 13 times.
✓ Branch 13 → 14 taken 13 times.
26 while (av_isspace(*p))
963 13 p++;
964
965 /* RFC 9112 mandates a space after the code, but a bare "HTTP/1.1 200"
966 * is common enough in the wild to be worth accepting. */
967
5/6
✓ Branch 14 → 15 taken 11 times.
✓ Branch 14 → 19 taken 2 times.
✓ Branch 15 → 16 taken 11 times.
✗ Branch 15 → 19 not taken.
✓ Branch 16 → 17 taken 10 times.
✓ Branch 16 → 19 taken 1 time.
13 if (!av_isdigit(p[0]) || !av_isdigit(p[1]) || !av_isdigit(p[2]) ||
968
4/4
✓ Branch 17 → 18 taken 9 times.
✓ Branch 17 → 21 taken 1 time.
✓ Branch 18 → 19 taken 2 times.
✓ Branch 18 → 21 taken 7 times.
10 (p[3] && !av_isspace(p[3]))) {
969 5 av_log(logctx, AV_LOG_ERROR, "Malformed HTTP status code.\n");
970 5 return AVERROR_INVALIDDATA;
971 }
972
973 8 st->code = 100 * (p[0] - '0') + 10 * (p[1] - '0') + p[2] - '0';
974
4/4
✓ Branch 21 → 22 taken 7 times.
✓ Branch 21 → 23 taken 1 time.
✓ Branch 22 → 23 taken 1 time.
✓ Branch 22 → 25 taken 6 times.
8 if (st->code < 100 || st->code > 599) {
975 2 av_log(logctx, AV_LOG_ERROR, "HTTP status code %d out of range.\n",
976 st->code);
977 2 return AVERROR_INVALIDDATA;
978 }
979 6 p += 3;
980
2/2
✓ Branch 27 → 26 taken 5 times.
✓ Branch 27 → 28 taken 6 times.
11 while (av_isspace(*p))
981 5 p++;
982 6 st->reason = p;
983
984 6 av_log(logctx, AV_LOG_TRACE, "http_code=%d\n", st->code);
985
986 6 return 0;
987 }
988
989 ✗ static int check_http_code(URLContext *h, int http_code, const char *end)
990 {
991 ✗ HTTPContext *s = h->priv_data;
992 /* error codes are 4xx and 5xx, but regard 401 as a success, so we
993 * don't abort until all headers have been parsed. */
994
0/6
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 11 not taken.
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 11 not taken.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (http_code >= 400 && http_code < 600 &&
995
0/4
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 11 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
996
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 11 not taken.
✗ (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
997 ✗ av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
998 ✗ return ff_http_averror(http_code, AVERROR(EIO));
999 }
1000 ✗ return 0;
1001 }
1002
1003 ✗ static int parse_location(HTTPContext *s, const char *p)
1004 {
1005 char redirected_location[MAX_URL_SIZE];
1006 ✗ ff_make_absolute_url(redirected_location, sizeof(redirected_location),
1007 ✗ s->location, p);
1008 ✗ av_freep(&s->new_location);
1009 ✗ s->new_location = av_strdup(redirected_location);
1010
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (!s->new_location)
1011 ✗ return AVERROR(ENOMEM);
1012 ✗ return 0;
1013 }
1014
1015 /* "bytes $from-$to/$document_size" */
1016 ✗ static int parse_content_range(URLContext *h, const char *p)
1017 {
1018 ✗ HTTPContext *s = h->priv_data;
1019 const char *slash, *end;
1020
1021
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 16 not taken.
✗ if (!strncmp(p, "bytes ", 6)) {
1022 ✗ p += 6;
1023 ✗ s->off = strtoull(p, NULL, 10);
1024 ✗ s->range_end = 0;
1025
0/4
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 10 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 10 not taken.
✗ if ((end = strchr(p, '-')) && strlen(end) > 0) {
1026 ✗ s->range_end = strtoull(end + 1, NULL, 10) + 1;
1027
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 10 not taken.
✗ if (s->range_end <= s->off) {
1028 ✗ av_log(h, AV_LOG_ERROR, "Invalid Content-Range: end %"PRIu64" before start %"PRIu64"\n",
1029 ✗ s->range_end - 1, s->off);
1030 ✗ return AVERROR_INVALIDDATA;
1031 }
1032 }
1033
0/4
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 16 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 16 not taken.
✗ if ((slash = strchr(p, '/')) && strlen(slash) > 0) {
1034
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 15 not taken.
✗ if (av_isdigit(slash[1]))
1035 ✗ s->filesize_from_content_range = strtoull(slash + 1, NULL, 10);
1036 else
1037 ✗ s->content_range_unknown_size = 1; /* unknown total size */
1038 }
1039 }
1040
0/6
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 20 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
1041 ✗ h->is_streamed = 0; /* we _can_ in fact seek */
1042 ✗ return 0;
1043 }
1044
1045 ✗ static int parse_content_encoding(URLContext *h, const char *p)
1046 {
1047
0/4
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 6 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 16 not taken.
✗ if (!av_strncasecmp(p, "gzip", 4) ||
1048 ✗ !av_strncasecmp(p, "deflate", 7)) {
1049 #if CONFIG_ZLIB
1050 ✗ HTTPContext *s = h->priv_data;
1051
1052 ✗ s->compressed = 1;
1053 ✗ inflateEnd(&s->inflate_stream);
1054
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 11 not taken.
✗ if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
1055 ✗ av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
1056 s->inflate_stream.msg);
1057 ✗ return AVERROR(ENOSYS);
1058 }
1059
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 15 not taken.
✗ if (zlibCompileFlags() & (1 << 17)) {
1060 ✗ av_log(h, AV_LOG_WARNING,
1061 "Your zlib was compiled without gzip support.\n");
1062 ✗ return AVERROR(ENOSYS);
1063 }
1064 #else
1065 av_log(h, AV_LOG_WARNING,
1066 "Compressed (%s) content, need zlib with gzip support\n", p);
1067 return AVERROR(ENOSYS);
1068 #endif /* CONFIG_ZLIB */
1069
0/2
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ } else if (!av_strncasecmp(p, "identity", 8)) {
1070 // The normal, no-encoding case (although servers shouldn't include
1071 // the header at all if this is the case).
1072 } else {
1073 ✗ av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
1074 }
1075 ✗ return 0;
1076 }
1077
1078 // Concat all Icy- header lines
1079 ✗ static int parse_icy(HTTPContext *s, const char *tag, const char *p)
1080 {
1081 ✗ int len = 4 + strlen(p) + strlen(tag);
1082 ✗ int is_first = !s->icy_metadata_headers;
1083 int ret;
1084
1085 ✗ av_dict_set(&s->metadata, tag, p, 0);
1086
1087
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (s->icy_metadata_headers)
1088 ✗ len += strlen(s->icy_metadata_headers);
1089
1090
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
1091 ✗ return ret;
1092
1093
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (is_first)
1094 ✗ *s->icy_metadata_headers = '\0';
1095
1096 ✗ av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
1097
1098 ✗ return 0;
1099 }
1100
1101 ✗ static int parse_http_date(const char *date_str, struct tm *buf)
1102 {
1103 char date_buf[MAX_DATE_LEN];
1104 ✗ int i, j, date_buf_len = MAX_DATE_LEN-1;
1105 char *date;
1106
1107 // strip off any punctuation or whitespace
1108
0/4
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ Branch 12 → 3 not taken.
✗ Branch 12 → 13 not taken.
✗ for (i = 0, j = 0; date_str[i] != '\0' && j < date_buf_len; i++) {
1109
0/4
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 9 not taken.
✗ if ((date_str[i] >= '0' && date_str[i] <= '9') ||
1110
0/4
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 9 not taken.
✗ (date_str[i] >= 'A' && date_str[i] <= 'Z') ||
1111
0/4
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 10 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ (date_str[i] >= 'a' && date_str[i] <= 'z')) {
1112 ✗ date_buf[j] = date_str[i];
1113 ✗ j++;
1114 }
1115 }
1116 ✗ date_buf[j] = '\0';
1117 ✗ date = date_buf;
1118
1119 // move the string beyond the day of week
1120
0/6
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ Branch 17 → 14 not taken.
✗ Branch 17 → 18 not taken.
✗ while ((*date < '0' || *date > '9') && *date != '\0')
1121 ✗ date++;
1122
1123
0/2
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ return av_small_strptime(date, "%d%b%Y%H%M%S", buf) ? 0 : AVERROR(EINVAL);
1124 }
1125
1126 ✗ static int parse_set_cookie(const char *set_cookie, AVDictionary **dict)
1127 {
1128 char *param, *next_param, *cstr, *back;
1129 ✗ char *saveptr = NULL;
1130
1131
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (!set_cookie[0])
1132 ✗ return 0;
1133
1134
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (!(cstr = av_strdup(set_cookie)))
1135 ✗ return AVERROR(EINVAL);
1136
1137 // strip any trailing whitespace
1138 ✗ back = &cstr[strlen(cstr)-1];
1139
0/2
✗ Branch 11 → 8 not taken.
✗ Branch 11 → 12 not taken.
✗ while (strchr(WHITESPACES, *back)) {
1140 ✗ *back='\0';
1141
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (back == cstr)
1142 ✗ break;
1143 ✗ back--;
1144 }
1145
1146 ✗ next_param = cstr;
1147
0/2
✗ Branch 28 → 13 not taken.
✗ Branch 28 → 29 not taken.
✗ while ((param = av_strtok(next_param, ";", &saveptr))) {
1148 char *name, *value;
1149 ✗ next_param = NULL;
1150 ✗ param += strspn(param, WHITESPACES);
1151
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 26 not taken.
✗ if ((name = av_strtok(param, "=", &value))) {
1152 ✗ char *end = name + strlen(name);
1153
0/4
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ Branch 18 → 16 not taken.
✗ Branch 18 → 19 not taken.
✗ while (end > name && strchr(WHITESPACES, end[-1]))
1154 ✗ *--end = '\0';
1155
0/4
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 26 not taken.
✗ if (av_dict_set(dict, name, value ? value : "", 0) < 0) {
1156 ✗ av_free(cstr);
1157 ✗ return -1;
1158 }
1159 }
1160 }
1161
1162 ✗ av_free(cstr);
1163 ✗ return 0;
1164 }
1165
1166 ✗ static const char *cookie_domain(const AVDictionary *cookie_params)
1167 {
1168 ✗ const AVDictionaryEntry *e = av_dict_get(cookie_params, "domain", NULL, 0);
1169
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ const char *domain = e ? e->value + (e->value[0] == '.') : "";
1170
1171
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ return *domain ? domain : NULL;
1172 }
1173
1174 ✗ static int host_is_ip_literal(const char *host)
1175 {
1176
0/4
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ return !host[strspn(host, "0123456789.")] || strchr(host, ':');
1177 }
1178
1179 ✗ static int host_in_cookie_domain(const char *host, const char *domain)
1180 {
1181 ✗ int offset = strlen(host) - strlen(domain);
1182
1183
0/6
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 10 not taken.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 10 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 9 not taken.
✗ return offset >= 0 && !av_strcasecmp(host + offset, domain) &&
1184
0/4
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 10 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ (!offset || (host[offset - 1] == '.' && !host_is_ip_literal(host)));
1185 }
1186
1187 ✗ static int parse_cookie(HTTPContext *s, const char *p, const char *host,
1188 AVDictionary **cookies)
1189 {
1190 ✗ AVDictionary *new_params = NULL;
1191 const AVDictionaryEntry *e, *cookie_entry;
1192 const char *eql;
1193 char *name, *value;
1194 int len;
1195
1196 // ensure the cookie is parsable
1197
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 6 not taken.
✗ if (parse_set_cookie(p, &new_params)) {
1198 ✗ av_dict_free(&new_params);
1199 ✗ return -1;
1200 }
1201
1202 // if there is no cookie value there is nothing to parse
1203 ✗ cookie_entry = av_dict_iterate(new_params, NULL);
1204
0/4
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 11 not taken.
✗ if (!cookie_entry || !cookie_entry->value) {
1205 ✗ av_dict_free(&new_params);
1206 ✗ return -1;
1207 }
1208
1209 // ensure the cookie is not expired or older than an existing value
1210
0/4
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 42 not taken.
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 42 not taken.
✗ if ((e = av_dict_get(new_params, "expires", NULL, 0)) && e->value) {
1211 ✗ struct tm new_tm = {0};
1212
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 40 not taken.
✗ if (!parse_http_date(e->value, &new_tm)) {
1213 AVDictionaryEntry *e2;
1214
1215 // if the cookie has already expired ignore it
1216
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 21 not taken.
✗ if (av_timegm(&new_tm) < av_gettime() / 1000000) {
1217 ✗ av_dict_free(&new_params);
1218 ✗ return 0;
1219 }
1220
1221 // only replace an older cookie with the same name
1222 ✗ e2 = av_dict_get(*cookies, cookie_entry->key, NULL, 0);
1223
0/4
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 40 not taken.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 40 not taken.
✗ if (e2 && e2->value) {
1224 ✗ AVDictionary *old_params = NULL;
1225
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 38 not taken.
✗ if (!parse_set_cookie(p, &old_params)) {
1226 ✗ e2 = av_dict_get(old_params, "expires", NULL, 0);
1227
0/4
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 38 not taken.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 38 not taken.
✗ if (e2 && e2->value) {
1228 ✗ struct tm old_tm = {0};
1229
0/2
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 37 not taken.
✗ if (!parse_http_date(e->value, &old_tm)) {
1230
0/2
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 37 not taken.
✗ if (av_timegm(&new_tm) < av_timegm(&old_tm)) {
1231 ✗ av_dict_free(&new_params);
1232 ✗ av_dict_free(&old_params);
1233 ✗ return -1;
1234 }
1235 }
1236 }
1237 }
1238 ✗ av_dict_free(&old_params);
1239 }
1240 }
1241 }
1242
0/2
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 44 not taken.
✗ const char *domain = host ? cookie_domain(new_params) : NULL;
1243
0/4
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 48 not taken.
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 48 not taken.
✗ int host_only = host && !domain;
1244
0/4
✗ Branch 49 → 50 not taken.
✗ Branch 49 → 55 not taken.
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 55 not taken.
✗ if (domain && !host_in_cookie_domain(host, domain)) {
1245 ✗ av_log(s, AV_LOG_WARNING, "Ignoring cookie for domain %s set by %s\n", domain, host);
1246 ✗ av_dict_free(&new_params);
1247 ✗ return 0;
1248 }
1249 ✗ av_dict_free(&new_params);
1250
1251 // duplicate the cookie name (dict will dupe the value)
1252
0/2
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 58 not taken.
✗ if (!(eql = strchr(p, '='))) return AVERROR(EINVAL);
1253
0/2
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 61 not taken.
✗ if (!(name = av_strndup(p, eql - p))) return AVERROR(ENOMEM);
1254
1255 // add the cookie to the dictionary
1256 ✗ len = strlen(eql);
1257
0/4
✗ Branch 63 → 64 not taken.
✗ Branch 63 → 65 not taken.
✗ Branch 64 → 62 not taken.
✗ Branch 64 → 65 not taken.
✗ while (len && strchr(WHITESPACES, eql[len - 1]))
1258 ✗ len--;
1259
0/4
✗ Branch 65 → 66 not taken.
✗ Branch 65 → 67 not taken.
✗ Branch 68 → 69 not taken.
✗ Branch 68 → 70 not taken.
✗ value = av_asprintf("%.*s%s%s", len, eql, host_only ? "; @hostonly=" : "", host_only ? host : "");
1260
0/2
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 75 not taken.
✗ if (!value) {
1261 ✗ av_free(name);
1262 ✗ return AVERROR(ENOMEM);
1263 }
1264 ✗ av_dict_set(cookies, name, value, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
1265
1266 ✗ return 0;
1267 }
1268
1269 /* Cookies without a Domain apply only to the host they are given for. */
1270 ✗ static int bind_cookies_to_host(HTTPContext *s)
1271 {
1272 ✗ char host[1024], *cookies, *cookie, *next, *saveptr = NULL, *bound;
1273 AVBPrint bp;
1274 ✗ int ret = 0;
1275
1276 ✗ av_url_split(NULL, 0, NULL, 0, host, sizeof(host), NULL, NULL, 0, s->location);
1277
1278 ✗ next = cookies = av_strdup(s->cookies);
1279
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (!cookies)
1280 ✗ return AVERROR(ENOMEM);
1281
1282 ✗ av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
1283
0/2
✗ Branch 33 → 7 not taken.
✗ Branch 33 → 34 not taken.
✗ while ((cookie = av_strtok(next, "\n", &saveptr))) {
1284 ✗ AVDictionary *params = NULL;
1285 ✗ int len = strlen(cookie), host_only;
1286
1287 ✗ next = NULL;
1288
0/4
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ Branch 10 → 8 not taken.
✗ Branch 10 → 11 not taken.
✗ while (len && strchr(WHITESPACES, cookie[len - 1]))
1289 ✗ len--;
1290
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ if ((ret = parse_set_cookie(cookie, &params)) < 0) {
1291 ✗ av_dict_free(&params);
1292 ✗ break;
1293 }
1294
0/4
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 19 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ host_only = !cookie_domain(params) && !av_dict_get(params, "@hostonly", NULL, 0);
1295 ✗ av_dict_free(&params);
1296
0/4
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ av_bprintf(&bp, "%.*s%s%s\n", len, cookie, host_only ? "; @hostonly=" : "", host_only ? host : "");
1297 }
1298 ✗ av_free(cookies);
1299
0/4
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 39 not taken.
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 39 not taken.
✗ if (ret >= 0 && !av_bprint_is_complete(&bp))
1300 ✗ ret = AVERROR(ENOMEM);
1301
0/2
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 42 not taken.
✗ if (ret < 0) {
1302 ✗ av_bprint_finalize(&bp, NULL);
1303 ✗ return ret;
1304 }
1305
0/2
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 45 not taken.
✗ if ((ret = av_bprint_finalize(&bp, &bound)) < 0)
1306 ✗ return ret;
1307 ✗ av_free(s->cookies);
1308 ✗ s->cookies = bound;
1309 ✗ return 0;
1310 }
1311
1312 ✗ static int cookie_string(AVDictionary *dict, char **cookies)
1313 {
1314 ✗ const AVDictionaryEntry *e = NULL;
1315 ✗ int len = 1;
1316
1317 // determine how much memory is needed for the cookies string
1318
0/2
✗ Branch 5 → 3 not taken.
✗ Branch 5 → 6 not taken.
✗ while ((e = av_dict_iterate(dict, e)))
1319 ✗ len += strlen(e->key) + strlen(e->value) + 1;
1320
1321 // reallocate the cookies
1322 ✗ e = NULL;
1323
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (*cookies) av_free(*cookies);
1324 ✗ *cookies = av_malloc(len);
1325
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ if (!*cookies) return AVERROR(ENOMEM);
1326 ✗ *cookies[0] = '\0';
1327
1328 // write out the cookies
1329
0/2
✗ Branch 14 → 12 not taken.
✗ Branch 14 → 15 not taken.
✗ while ((e = av_dict_iterate(dict, e)))
1330 ✗ av_strlcatf(*cookies, len, "%s%s\n", e->key, e->value);
1331
1332 ✗ return 0;
1333 }
1334
1335 ✗ static void parse_expires(HTTPContext *s, const char *p)
1336 {
1337 struct tm tm;
1338
1339
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 6 not taken.
✗ if (!parse_http_date(p, &tm)) {
1340 ✗ s->expires = av_timegm(&tm);
1341 }
1342 ✗ }
1343
1344 ✗ static void parse_cache_control(HTTPContext *s, const char *p)
1345 {
1346 char *age;
1347 int offset;
1348
1349 /* give 'Expires' higher priority over 'Cache-Control' */
1350
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (s->expires) {
1351 ✗ return;
1352 }
1353
1354
0/4
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ if (av_stristr(p, "no-cache") || av_stristr(p, "no-store")) {
1355 ✗ s->expires = -1;
1356 ✗ return;
1357 }
1358
1359 ✗ age = av_stristr(p, "s-maxage=");
1360 ✗ offset = 9;
1361
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 13 not taken.
✗ if (!age) {
1362 ✗ age = av_stristr(p, "max-age=");
1363 ✗ offset = 8;
1364 }
1365
1366
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 16 not taken.
✗ if (age) {
1367 ✗ s->expires = time(NULL) + atoi(age + offset);
1368 }
1369 }
1370
1371 ✗ static int process_line(URLContext *h, char *line, int line_count, int *parsed_http_code)
1372 {
1373 ✗ HTTPContext *s = h->priv_data;
1374
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ const char *auto_method = h->flags & AVIO_FLAG_READ ? "POST" : "GET";
1375 char *tag, *p, *method, *resource, *version;
1376 int ret;
1377
1378 /* end of header */
1379
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (line[0] == '\0') {
1380 ✗ s->end_header = 1;
1381 ✗ return 0;
1382 }
1383
1384 ✗ p = line;
1385
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 68 not taken.
✗ if (line_count == 0) {
1386
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 54 not taken.
✗ if (s->is_connected_server) {
1387 // HTTP method
1388 ✗ method = p;
1389
0/4
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ Branch 12 → 10 not taken.
✗ Branch 12 → 13 not taken.
✗ while (*p && !av_isspace(*p))
1390 ✗ p++;
1391
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if (!av_isspace(*p))
1392 ✗ return AVERROR_HTTP_BAD_REQUEST;
1393 ✗ *(p++) = '\0';
1394 ✗ av_log(h, AV_LOG_TRACE, "Received method: %s\n", method);
1395
0/2
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 21 not taken.
✗ if (s->method) {
1396
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 29 not taken.
✗ if (av_strcasecmp(s->method, method)) {
1397 ✗ av_log(h, AV_LOG_ERROR, "Received and expected HTTP method do not match. (%s expected, %s received)\n",
1398 s->method, method);
1399 ✗ return AVERROR_HTTP_BAD_REQUEST;
1400 }
1401 } else {
1402 // use autodetected HTTP method to expect
1403 ✗ av_log(h, AV_LOG_TRACE, "Autodetected %s HTTP method\n", auto_method);
1404
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 26 not taken.
✗ if (av_strcasecmp(auto_method, method)) {
1405 ✗ av_log(h, AV_LOG_ERROR, "Received and autodetected HTTP method did not match "
1406 "(%s autodetected %s received)\n", auto_method, method);
1407 ✗ return AVERROR_HTTP_BAD_REQUEST;
1408 }
1409
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ if (!(s->method = av_strdup(method)))
1410 ✗ return AVERROR(ENOMEM);
1411 }
1412
1413 // HTTP resource
1414
0/2
✗ Branch 31 → 30 not taken.
✗ Branch 31 → 32 not taken.
✗ while (av_isspace(*p))
1415 ✗ p++;
1416 ✗ resource = p;
1417
0/4
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 36 not taken.
✗ Branch 35 → 33 not taken.
✗ Branch 35 → 36 not taken.
✗ while (*p && !av_isspace(*p))
1418 ✗ p++;
1419
0/2
✗ Branch 36 → 37 not taken.
✗ Branch 36 → 38 not taken.
✗ if (!av_isspace(*p))
1420 ✗ return AVERROR_HTTP_BAD_REQUEST;
1421 ✗ *(p++) = '\0';
1422 ✗ av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
1423
0/2
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 42 not taken.
✗ if (!(s->resource = av_strdup(resource)))
1424 ✗ return AVERROR(ENOMEM);
1425
1426 // HTTP version
1427
0/2
✗ Branch 44 → 43 not taken.
✗ Branch 44 → 45 not taken.
✗ while (av_isspace(*p))
1428 ✗ p++;
1429 ✗ version = p;
1430
0/4
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 49 not taken.
✗ Branch 48 → 46 not taken.
✗ Branch 48 → 49 not taken.
✗ while (*p && !av_isspace(*p))
1431 ✗ p++;
1432 ✗ *p = '\0';
1433
0/2
✗ Branch 50 → 51 not taken.
✗ Branch 50 → 53 not taken.
✗ if (av_strncasecmp(version, "HTTP/", 5)) {
1434 ✗ av_log(h, AV_LOG_ERROR, "Malformed HTTP version string.\n");
1435 ✗ return AVERROR_HTTP_BAD_REQUEST;
1436 }
1437 ✗ av_log(h, AV_LOG_TRACE, "HTTP version string: %s\n", version);
1438 } else {
1439 HTTPStatusLine st;
1440
1441
0/2
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 57 not taken.
✗ if ((ret = ff_http_parse_status_line(h, p, &st)) < 0)
1442 ✗ return ret;
1443
1444 /* Only ever set: a keep-alive decision made earlier must survive. */
1445
0/2
✗ Branch 57 → 58 not taken.
✗ Branch 57 → 59 not taken.
✗ if (st.willclose)
1446 ✗ s->willclose = 1;
1447
1448 ✗ av_freep(&s->http_version);
1449
0/2
✗ Branch 61 → 62 not taken.
✗ Branch 61 → 63 not taken.
✗ if (!(s->http_version = av_strdup(st.version)))
1450 ✗ return AVERROR(ENOMEM);
1451
1452 ✗ s->http_code = st.code;
1453
1454 ✗ *parsed_http_code = 1;
1455
1456
0/2
✗ Branch 64 → 65 not taken.
✗ Branch 64 → 66 not taken.
✗ if ((ret = check_http_code(h, s->http_code, st.reason)) < 0)
1457 ✗ return ret;
1458 }
1459 } else {
1460
0/4
✗ Branch 70 → 71 not taken.
✗ Branch 70 → 72 not taken.
✗ Branch 71 → 69 not taken.
✗ Branch 71 → 72 not taken.
✗ while (*p != '\0' && *p != ':')
1461 ✗ p++;
1462
0/2
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 74 not taken.
✗ if (*p != ':')
1463 ✗ return 1;
1464
1465 ✗ *p = '\0';
1466 ✗ tag = line;
1467 ✗ p++;
1468
0/2
✗ Branch 76 → 75 not taken.
✗ Branch 76 → 77 not taken.
✗ while (av_isspace(*p))
1469 ✗ p++;
1470
0/2
✗ Branch 78 → 79 not taken.
✗ Branch 78 → 82 not taken.
✗ if (!av_strcasecmp(tag, "Location")) {
1471
0/2
✗ Branch 80 → 81 not taken.
✗ Branch 80 → 167 not taken.
✗ if ((ret = parse_location(s, p)) < 0)
1472 ✗ return ret;
1473
0/2
✗ Branch 83 → 84 not taken.
✗ Branch 83 → 87 not taken.
✗ } else if (!av_strcasecmp(tag, "Content-Length") &&
1474
0/2
✗ Branch 84 → 85 not taken.
✗ Branch 84 → 87 not taken.
✗ s->filesize == UINT64_MAX) {
1475 ✗ s->filesize = strtoull(p, NULL, 10);
1476
0/2
✗ Branch 88 → 89 not taken.
✗ Branch 88 → 92 not taken.
✗ } else if (!av_strcasecmp(tag, "Content-Range")) {
1477
0/2
✗ Branch 90 → 91 not taken.
✗ Branch 90 → 167 not taken.
✗ if ((ret = parse_content_range(h, p)) < 0)
1478 ✗ return ret;
1479
0/2
✗ Branch 93 → 94 not taken.
✗ Branch 93 → 97 not taken.
✗ } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
1480
0/2
✗ Branch 94 → 95 not taken.
✗ Branch 94 → 97 not taken.
✗ !strncmp(p, "bytes", 5) &&
1481
0/2
✗ Branch 95 → 96 not taken.
✗ Branch 95 → 97 not taken.
✗ s->seekable == -1) {
1482 ✗ h->is_streamed = 0;
1483
0/4
✗ Branch 98 → 99 not taken.
✗ Branch 98 → 102 not taken.
✗ Branch 100 → 101 not taken.
✗ Branch 100 → 102 not taken.
✗ } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
1484 ✗ !av_strncasecmp(p, "chunked", 7)) {
1485 ✗ s->filesize = UINT64_MAX;
1486 ✗ s->chunksize = 0;
1487
0/2
✗ Branch 103 → 104 not taken.
✗ Branch 103 → 105 not taken.
✗ } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
1488 ✗ ff_http_auth_handle_header(&s->auth_state, tag, p);
1489
0/2
✗ Branch 106 → 107 not taken.
✗ Branch 106 → 108 not taken.
✗ } else if (!av_strcasecmp(tag, "Authentication-Info")) {
1490 ✗ ff_http_auth_handle_header(&s->auth_state, tag, p);
1491
0/2
✗ Branch 109 → 110 not taken.
✗ Branch 109 → 111 not taken.
✗ } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
1492 ✗ ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
1493
0/2
✗ Branch 112 → 113 not taken.
✗ Branch 112 → 116 not taken.
✗ } else if (!av_strcasecmp(tag, "Connection")) {
1494
0/2
✗ Branch 114 → 115 not taken.
✗ Branch 114 → 167 not taken.
✗ if (!av_strcasecmp(p, "close"))
1495 ✗ s->willclose = 1;
1496
0/2
✗ Branch 117 → 118 not taken.
✗ Branch 117 → 124 not taken.
✗ } else if (!av_strcasecmp(tag, "Server")) {
1497
0/2
✗ Branch 119 → 120 not taken.
✗ Branch 119 → 121 not taken.
✗ if (!av_strcasecmp(p, "AkamaiGHost")) {
1498 ✗ s->is_akamai = 1;
1499
0/2
✗ Branch 122 → 123 not taken.
✗ Branch 122 → 167 not taken.
✗ } else if (!av_strncasecmp(p, "MediaGateway", 12)) {
1500 ✗ s->is_mediagateway = 1;
1501 }
1502
0/2
✗ Branch 125 → 126 not taken.
✗ Branch 125 → 129 not taken.
✗ } else if (!av_strcasecmp(tag, "Content-Type")) {
1503 ✗ av_free(s->mime_type);
1504 ✗ s->mime_type = av_get_token((const char **)&p, ";");
1505
0/2
✗ Branch 130 → 131 not taken.
✗ Branch 130 → 134 not taken.
✗ } else if (!av_strcasecmp(tag, "Set-Cookie")) {
1506
0/2
✗ Branch 132 → 133 not taken.
✗ Branch 132 → 167 not taken.
✗ if (parse_cookie(s, p, s->host, &s->cookie_dict))
1507 ✗ av_log(h, AV_LOG_WARNING, "Unable to parse '%s'\n", p);
1508
0/2
✗ Branch 135 → 136 not taken.
✗ Branch 135 → 138 not taken.
✗ } else if (!av_strcasecmp(tag, "Icy-MetaInt")) {
1509 ✗ s->icy_metaint = strtoull(p, NULL, 10);
1510
0/2
✗ Branch 139 → 140 not taken.
✗ Branch 139 → 143 not taken.
✗ } else if (!av_strncasecmp(tag, "Icy-", 4)) {
1511
0/2
✗ Branch 141 → 142 not taken.
✗ Branch 141 → 167 not taken.
✗ if ((ret = parse_icy(s, tag, p)) < 0)
1512 ✗ return ret;
1513
0/2
✗ Branch 144 → 145 not taken.
✗ Branch 144 → 148 not taken.
✗ } else if (!av_strcasecmp(tag, "Content-Encoding")) {
1514
0/2
✗ Branch 146 → 147 not taken.
✗ Branch 146 → 167 not taken.
✗ if ((ret = parse_content_encoding(h, p)) < 0)
1515 ✗ return ret;
1516
0/2
✗ Branch 149 → 150 not taken.
✗ Branch 149 → 151 not taken.
✗ } else if (!av_strcasecmp(tag, "Expires")) {
1517 ✗ parse_expires(s, p);
1518
0/2
✗ Branch 152 → 153 not taken.
✗ Branch 152 → 154 not taken.
✗ } else if (!av_strcasecmp(tag, "Cache-Control")) {
1519 ✗ parse_cache_control(s, p);
1520
0/2
✗ Branch 155 → 156 not taken.
✗ Branch 155 → 167 not taken.
✗ } else if (!av_strcasecmp(tag, "Retry-After")) {
1521 /* The header can be either an integer that represents seconds, or a date. */
1522 struct tm tm;
1523 ✗ int date_ret = parse_http_date(p, &tm);
1524
0/2
✗ Branch 157 → 158 not taken.
✗ Branch 157 → 164 not taken.
✗ if (!date_ret) {
1525 ✗ time_t retry = av_timegm(&tm);
1526 ✗ int64_t now = av_gettime() / 1000000;
1527 ✗ int64_t diff = ((int64_t) retry) - now;
1528
0/2
✗ Branch 160 → 161 not taken.
✗ Branch 160 → 162 not taken.
✗ s->retry_after = (unsigned int) FFMAX(0, diff);
1529 } else {
1530 ✗ s->retry_after = strtoul(p, NULL, 10);
1531 }
1532 }
1533 }
1534 ✗ return 1;
1535 }
1536
1537 /**
1538 * Create a string containing cookie values for use as a HTTP cookie header
1539 * field value for a particular path and domain from the cookie values stored in
1540 * the HTTP protocol context. The cookie string is stored in *cookies, and may
1541 * be NULL if there are no valid cookies.
1542 *
1543 * @return a negative value if an error condition occurred, 0 otherwise
1544 */
1545 ✗ static int get_cookies(HTTPContext *s, char **cookies, const char *path)
1546 {
1547 // cookie strings will look like Set-Cookie header field values. Multiple
1548 // Set-Cookie fields will result in multiple values delimited by a newline
1549 ✗ int ret = 0;
1550 char *cookie, *set_cookies, *next;
1551 ✗ char *saveptr = NULL;
1552
1553 // destroy any cookies in the dictionary.
1554 ✗ av_dict_free(&s->cookie_dict);
1555
1556
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (!s->cookies)
1557 ✗ return 0;
1558
1559 ✗ next = set_cookies = av_strdup(s->cookies);
1560
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (!next)
1561 ✗ return AVERROR(ENOMEM);
1562
1563 ✗ *cookies = NULL;
1564
0/4
✗ Branch 70 → 71 not taken.
✗ Branch 70 → 72 not taken.
✗ Branch 71 → 9 not taken.
✗ Branch 71 → 72 not taken.
✗ while ((cookie = av_strtok(next, "\n", &saveptr)) && !ret) {
1565 ✗ AVDictionary *cookie_params = NULL;
1566 const AVDictionaryEntry *cookie_entry, *e;
1567 const char *domain, *eql;
1568
1569 ✗ next = NULL;
1570 // store the cookie in a dict in case it is updated in the response
1571
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (parse_cookie(s, cookie, NULL, &s->cookie_dict))
1572 ✗ av_log(s, AV_LOG_WARNING, "Unable to parse '%s'\n", cookie);
1573
1574 // continue on to the next cookie if this one cannot be parsed
1575
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if (parse_set_cookie(cookie, &cookie_params))
1576 ✗ goto skip_cookie;
1577
1578 // if the cookie has no value, skip it
1579 ✗ cookie_entry = av_dict_iterate(cookie_params, NULL);
1580 ✗ eql = strchr(cookie, '=');
1581
0/6
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 20 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 20 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ if (!cookie_entry || !eql || eql == cookie + strspn(cookie, WHITESPACES) ||
1582
0/2
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ memchr(cookie, ';', eql - cookie))
1583 ✗ goto skip_cookie;
1584
1585
0/2
✗ Branch 29 → 22 not taken.
✗ Branch 29 → 30 not taken.
✗ for (e = cookie_entry; (e = av_dict_iterate(cookie_params, e)); )
1586
0/4
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 27 not taken.
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 27 not taken.
✗ if (!av_strcasecmp(e->key, "secure") && !av_stristart(s->location, "https:", NULL))
1587 ✗ goto skip_cookie;
1588
1589 // if the cookie has expired, don't add it
1590
0/4
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 40 not taken.
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 40 not taken.
✗ if ((e = av_dict_get(cookie_params, "expires", NULL, 0)) && e->value) {
1591 ✗ struct tm tm_buf = {0};
1592
0/2
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 39 not taken.
✗ if (!parse_http_date(e->value, &tm_buf)) {
1593
0/2
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 39 not taken.
✗ if (av_timegm(&tm_buf) < av_gettime() / 1000000)
1594 ✗ goto skip_cookie;
1595 }
1596 }
1597
1598 // if no domain in the cookie assume it applied to this request
1599 ✗ domain = cookie_domain(cookie_params);
1600
0/4
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 45 not taken.
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 45 not taken.
✗ if (domain && !host_in_cookie_domain(s->host, domain))
1601 ✗ goto skip_cookie;
1602
1603
0/4
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 50 not taken.
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✗ if ((e = av_dict_get(cookie_params, "@hostonly", NULL, 0)) && av_strcasecmp(e->value, s->host))
1604 ✗ goto skip_cookie;
1605
1606 // if a cookie path is provided, ensure the request path is within that path
1607 ✗ e = av_dict_get(cookie_params, "path", NULL, 0);
1608
0/2
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 59 not taken.
✗ if (e) {
1609 ✗ size_t len = strlen(e->value);
1610
0/4
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 58 not taken.
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 59 not taken.
✗ if (strncmp(path, e->value, len) ||
1611
0/6
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 59 not taken.
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 59 not taken.
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 59 not taken.
✗ (len && path[len] && path[len] != '/' && path[len] != '?' &&
1612
0/2
✗ Branch 57 → 58 not taken.
✗ Branch 57 → 59 not taken.
✗ e->value[len - 1] != '/'))
1613 ✗ goto skip_cookie;
1614 }
1615
1616 // cookie parameters match, so copy the value
1617
0/2
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 62 not taken.
✗ if (!*cookies) {
1618 ✗ *cookies = av_asprintf("%s=%s", cookie_entry->key, cookie_entry->value);
1619 } else {
1620 ✗ char *tmp = *cookies;
1621 ✗ *cookies = av_asprintf("%s; %s=%s", tmp, cookie_entry->key, cookie_entry->value);
1622 ✗ av_free(tmp);
1623 }
1624
0/2
✗ Branch 64 → 65 not taken.
✗ Branch 64 → 66 not taken.
✗ if (!*cookies)
1625 ✗ ret = AVERROR(ENOMEM);
1626
1627 ✗ skip_cookie:
1628 ✗ av_dict_free(&cookie_params);
1629 }
1630
1631 ✗ av_free(set_cookies);
1632
1633 ✗ return ret;
1634 }
1635
1636 ✗ static inline int has_header(const char *str, const char *header)
1637 {
1638 /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
1639
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (!str)
1640 ✗ return 0;
1641
0/4
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
1642 }
1643
1644 ✗ static int http_read_header(URLContext *h)
1645 {
1646 ✗ HTTPContext *s = h->priv_data;
1647 char line[MAX_URL_SIZE];
1648 ✗ int err = 0, http_err = 0;
1649
1650 ✗ av_freep(&s->new_location);
1651 ✗ s->expires = 0;
1652 ✗ s->chunksize = UINT64_MAX;
1653 ✗ s->filesize_from_content_range = UINT64_MAX;
1654 ✗ s->content_range_unknown_size = 0;
1655
1656 ✗ for (;;) {
1657 ✗ int parsed_http_code = 0;
1658
1659
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 9 not taken.
✗ if ((err = http_get_line(s, line, sizeof(line))) < 0) {
1660 ✗ av_log(h, AV_LOG_ERROR, "Error reading HTTP response: %s\n",
1661 ✗ av_err2str(err));
1662 ✗ return err;
1663 }
1664
1665 ✗ av_log(h, AV_LOG_TRACE, "header='%s'\n", line);
1666
1667 ✗ err = process_line(h, line, s->line_count, &parsed_http_code);
1668
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 17 not taken.
✗ if (err < 0) {
1669
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ if (parsed_http_code) {
1670 ✗ http_err = err;
1671 } else {
1672 /* Prefer to return HTTP code error if we've already seen one. */
1673
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ if (http_err)
1674 ✗ return http_err;
1675 else
1676 ✗ return err;
1677 }
1678 }
1679
0/2
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ if (err == 0)
1680 ✗ break;
1681 ✗ s->line_count++;
1682 }
1683
0/2
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ if (http_err)
1684 ✗ return http_err;
1685
1686 // filesize from Content-Range can always be used, even if using chunked Transfer-Encoding
1687
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ if (s->filesize_from_content_range != UINT64_MAX)
1688 ✗ s->filesize = s->filesize_from_content_range;
1689
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 27 not taken.
✗ else if (s->content_range_unknown_size)
1690 ✗ s->filesize = s->oc_filesize;
1691
1692 /* Remember a concrete complete size for later unknown-total ("*") replies. */
1693
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ if (s->filesize_from_content_range != UINT64_MAX)
1694 ✗ s->oc_filesize = s->filesize_from_content_range;
1695
0/4
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 32 not taken.
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 32 not taken.
✗ else if (s->http_code == 200 && s->filesize != UINT64_MAX)
1696 ✗ s->oc_filesize = s->filesize;
1697
1698
0/6
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 36 not taken.
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 36 not taken.
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 36 not taken.
✗ if (s->seekable == -1 && s->is_mediagateway && s->filesize == 2000000000)
1699 ✗ h->is_streamed = 1; /* we can in fact _not_ seek */
1700
1701
0/2
✗ Branch 36 → 37 not taken.
✗ Branch 36 → 38 not taken.
✗ if (h->is_streamed)
1702 ✗ s->initial_requests = 0; /* unable to use partial requests */
1703
1704 // add any new cookies into the existing cookie string
1705 ✗ cookie_string(s->cookie_dict, &s->cookies);
1706 ✗ av_dict_free(&s->cookie_dict);
1707
1708 ✗ return err;
1709 }
1710
1711 /**
1712 * Escape unsafe characters in path in order to pass them safely to the HTTP
1713 * request. Insipred by the algorithm in GNU wget:
1714 * - escape "%" characters not followed by two hex digits
1715 * - escape all "unsafe" characters except which are also "reserved"
1716 * - pass through everything else
1717 */
1718 ✗ static void bprint_escaped_path(AVBPrint *bp, const char *path)
1719 {
1720 #define NEEDS_ESCAPE(ch) \
1721 ((ch) <= ' ' || (ch) >= '\x7f' || \
1722 (ch) == '"' || (ch) == '%' || (ch) == '<' || (ch) == '>' || (ch) == '\\' || \
1723 (ch) == '^' || (ch) == '`' || (ch) == '{' || (ch) == '}' || (ch) == '|')
1724
0/2
✗ Branch 27 → 3 not taken.
✗ Branch 27 → 28 not taken.
✗ while (*path) {
1725 char buf[1024];
1726 ✗ char *q = buf;
1727
0/4
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ Branch 24 → 4 not taken.
✗ Branch 24 → 25 not taken.
✗ while (*path && q - buf < sizeof(buf) - 4) {
1728
0/6
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 8 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (path[0] == '%' && av_isxdigit(path[1]) && av_isxdigit(path[2])) {
1729 ✗ *q++ = *path++;
1730 ✗ *q++ = *path++;
1731 ✗ *q++ = *path++;
1732
0/24
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 20 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 20 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 20 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 20 not taken.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 20 not taken.
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 20 not taken.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 20 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 20 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 20 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 20 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ } else if (NEEDS_ESCAPE(*path)) {
1733 ✗ q += snprintf(q, 4, "%%%02X", (uint8_t)*path++);
1734 } else {
1735 ✗ *q++ = *path++;
1736 }
1737 }
1738 ✗ av_bprint_append_data(bp, buf, q - buf);
1739 }
1740 ✗ }
1741
1742 ✗ static uint64_t request_size(URLContext *h)
1743 {
1744 ✗ HTTPContext *s = h->priv_data;
1745
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (s->initial_requests)
1746 ✗ return s->initial_request_size;
1747 ✗ return s->request_size;
1748 }
1749
1750 ✗ static int http_connect(URLContext *h, const char *path, const char *local_path,
1751 const char *hoststr, const char *auth,
1752 const char *proxyauth)
1753 {
1754 ✗ HTTPContext *s = h->priv_data;
1755 int post, err;
1756 AVBPrint request;
1757 ✗ char *authstr = NULL, *proxyauthstr = NULL;
1758 ✗ uint64_t off = s->off;
1759 const char *method;
1760 ✗ int send_expect_100 = 0;
1761 ✗ int keep_alive = 1;
1762
1763 ✗ av_bprint_init_for_buffer(&request, s->buffer, sizeof(s->buffer));
1764
1765 /* send http header */
1766 ✗ post = h->flags & AVIO_FLAG_WRITE;
1767
1768
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (s->post_data) {
1769 /* force POST method and disable chunked encoding when
1770 * custom HTTP post data is set */
1771 ✗ post = 1;
1772 ✗ s->chunked_post = 0;
1773 }
1774
1775
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (s->method)
1776 ✗ method = s->method;
1777 else
1778
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ method = post ? "POST" : "GET";
1779
1780 ✗ authstr = ff_http_auth_create_response(&s->auth_state, auth,
1781 local_path, method);
1782 ✗ proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
1783 local_path, method);
1784
1785
0/4
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 20 not taken.
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 20 not taken.
✗ if (post && !s->post_data) {
1786
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ if (s->send_expect_100 != -1) {
1787 ✗ send_expect_100 = s->send_expect_100;
1788 } else {
1789 ✗ send_expect_100 = 0;
1790 /* The user has supplied authentication but we don't know the auth type,
1791 * send Expect: 100-continue to get the 401 response including the
1792 * WWW-Authenticate header, or an 100 continue if no auth actually
1793 * is needed. */
1794
0/4
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 20 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 20 not taken.
✗ if (auth && *auth &&
1795
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ s->auth_state.auth_type == HTTP_AUTH_NONE)
1796 ✗ send_expect_100 = 1;
1797 }
1798 }
1799
1800 ✗ av_bprintf(&request, "%s ", method);
1801 ✗ bprint_escaped_path(&request, path);
1802 ✗ av_bprintf(&request, " HTTP/1.1\r\n");
1803
1804
0/4
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 26 not taken.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ if (post && s->chunked_post)
1805 ✗ av_bprintf(&request, "Transfer-Encoding: chunked\r\n");
1806 /* set default headers if needed */
1807
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ if (!has_header(s->headers, "\r\nUser-Agent: "))
1808 ✗ av_bprintf(&request, "User-Agent: %s\r\n", s->user_agent);
1809
0/2
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 33 not taken.
✗ if (s->referer) {
1810 /* set default headers if needed */
1811
0/2
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ if (!has_header(s->headers, "\r\nReferer: "))
1812 ✗ av_bprintf(&request, "Referer: %s\r\n", s->referer);
1813 }
1814
0/2
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 36 not taken.
✗ if (!has_header(s->headers, "\r\nAccept: "))
1815 ✗ av_bprintf(&request, "Accept: */*\r\n");
1816 // Note: we send the Range header on purpose, even when we're probing,
1817 // since it allows us to detect more reliably if a (non-conforming)
1818 // server supports seeking by analysing the reply headers.
1819 ✗ int is_partial_request = 0;
1820
0/10
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 57 not taken.
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 57 not taken.
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 42 not taken.
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 42 not taken.
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 57 not taken.
✗ if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable != 0)) {
1821 ✗ av_bprintf(&request, "Range: bytes=%"PRIu64"-", s->off);
1822 ✗ uint64_t req_size = request_size(h);
1823
0/4
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 54 not taken.
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 54 not taken.
✗ if (req_size && s->seekable != 0) {
1824 ✗ uint64_t target_off = s->off + req_size;
1825
0/2
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 48 not taken.
✗ if (target_off < s->off) /* overflow */
1826 ✗ target_off = UINT64_MAX;
1827
0/2
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✗ if (s->end_off)
1828 ✗ target_off = FFMIN(target_off, s->end_off);
1829
0/2
✗ Branch 50 → 51 not taken.
✗ Branch 50 → 53 not taken.
✗ if (target_off != UINT64_MAX) {
1830 ✗ av_bprintf(&request, "%"PRId64, target_off - 1);
1831 ✗ is_partial_request = 1;
1832 }
1833
0/2
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 56 not taken.
✗ } else if (s->end_off)
1834 ✗ av_bprintf(&request, "%"PRId64, s->end_off - 1);
1835 ✗ av_bprintf(&request, "\r\n");
1836 }
1837
0/4
✗ Branch 57 → 58 not taken.
✗ Branch 57 → 61 not taken.
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 61 not taken.
✗ if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
1838 ✗ av_bprintf(&request, "Expect: 100-continue\r\n");
1839
1840
0/2
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 70 not taken.
✗ if (!has_header(s->headers, "\r\nConnection: ")) {
1841 ✗ keep_alive = s->multiple_requests > 0;
1842
0/4
✗ Branch 63 → 64 not taken.
✗ Branch 63 → 66 not taken.
✗ Branch 64 → 65 not taken.
✗ Branch 64 → 66 not taken.
✗ if (s->multiple_requests < 0 /* auto */ && is_partial_request)
1843 ✗ keep_alive = 1;
1844
0/2
✗ Branch 66 → 67 not taken.
✗ Branch 66 → 68 not taken.
✗ av_bprintf(&request, "Connection: %s\r\n", keep_alive ? "keep-alive" : "close");
1845 }
1846
1847
0/2
✗ Branch 71 → 72 not taken.
✗ Branch 71 → 73 not taken.
✗ if (!has_header(s->headers, "\r\nHost: "))
1848 ✗ av_bprintf(&request, "Host: %s\r\n", hoststr);
1849
0/4
✗ Branch 74 → 75 not taken.
✗ Branch 74 → 77 not taken.
✗ Branch 75 → 76 not taken.
✗ Branch 75 → 77 not taken.
✗ if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
1850 ✗ av_bprintf(&request, "Content-Length: %d\r\n", s->post_datalen);
1851
1852
0/4
✗ Branch 78 → 79 not taken.
✗ Branch 78 → 81 not taken.
✗ Branch 79 → 80 not taken.
✗ Branch 79 → 81 not taken.
✗ if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
1853 ✗ av_bprintf(&request, "Content-Type: %s\r\n", s->content_type);
1854
0/4
✗ Branch 82 → 83 not taken.
✗ Branch 82 → 90 not taken.
✗ Branch 83 → 84 not taken.
✗ Branch 83 → 90 not taken.
✗ if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
1855 ✗ char *cookies = NULL;
1856
0/4
✗ Branch 85 → 86 not taken.
✗ Branch 85 → 89 not taken.
✗ Branch 86 → 87 not taken.
✗ Branch 86 → 89 not taken.
✗ if (!get_cookies(s, &cookies, local_path) && cookies) {
1857 ✗ av_bprintf(&request, "Cookie: %s\r\n", cookies);
1858 ✗ av_free(cookies);
1859 }
1860 }
1861
0/4
✗ Branch 91 → 92 not taken.
✗ Branch 91 → 94 not taken.
✗ Branch 92 → 93 not taken.
✗ Branch 92 → 94 not taken.
✗ if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
1862 ✗ av_bprintf(&request, "Icy-MetaData: 1\r\n");
1863
1864 /* now add in custom headers */
1865
0/2
✗ Branch 94 → 95 not taken.
✗ Branch 94 → 96 not taken.
✗ if (s->headers)
1866 ✗ av_bprintf(&request, "%s", s->headers);
1867
1868
0/2
✗ Branch 96 → 97 not taken.
✗ Branch 96 → 98 not taken.
✗ if (authstr)
1869 ✗ av_bprintf(&request, "%s", authstr);
1870
0/2
✗ Branch 98 → 99 not taken.
✗ Branch 98 → 100 not taken.
✗ if (proxyauthstr)
1871 ✗ av_bprintf(&request, "Proxy-%s", proxyauthstr);
1872 ✗ av_bprintf(&request, "\r\n");
1873
1874 ✗ av_log(h, AV_LOG_DEBUG, "request: %s\n", request.str);
1875
1876
0/2
✗ Branch 103 → 104 not taken.
✗ Branch 103 → 106 not taken.
✗ if (!av_bprint_is_complete(&request)) {
1877 ✗ av_log(h, AV_LOG_ERROR, "overlong headers\n");
1878 ✗ err = AVERROR(EINVAL);
1879 ✗ goto done;
1880 }
1881
1882
0/2
✗ Branch 107 → 108 not taken.
✗ Branch 107 → 109 not taken.
✗ if ((err = ffurl_write(s->hd, request.str, request.len)) < 0)
1883 ✗ goto done;
1884
1885
0/2
✗ Branch 109 → 110 not taken.
✗ Branch 109 → 113 not taken.
✗ if (s->post_data)
1886
0/2
✗ Branch 111 → 112 not taken.
✗ Branch 111 → 113 not taken.
✗ if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
1887 ✗ goto done;
1888
1889 /* init input buffer */
1890 ✗ s->buf_ptr = s->buffer;
1891 ✗ s->buf_end = s->buffer;
1892 ✗ s->line_count = 0;
1893 ✗ s->off = 0;
1894 ✗ s->icy_data_read = 0;
1895 ✗ s->chunkend = 0;
1896 ✗ s->filesize = UINT64_MAX;
1897 ✗ s->range_end = 0;
1898 ✗ s->willclose = !keep_alive;
1899 ✗ s->end_chunked_post = 0;
1900 ✗ s->end_header = 0;
1901 #if CONFIG_ZLIB
1902 ✗ s->compressed = 0;
1903 #endif
1904
0/6
✗ Branch 113 → 114 not taken.
✗ Branch 113 → 117 not taken.
✗ Branch 114 → 115 not taken.
✗ Branch 114 → 117 not taken.
✗ Branch 115 → 116 not taken.
✗ Branch 115 → 117 not taken.
✗ if (post && !s->post_data && !send_expect_100) {
1905 /* Pretend that it did work. We didn't read any header yet, since
1906 * we've still to send the POST data, but the code calling this
1907 * function will check http_code after we return. */
1908 ✗ s->http_code = 200;
1909 ✗ err = 0;
1910 ✗ goto done;
1911 }
1912
1913 /* wait for header */
1914 ✗ int64_t latency = av_gettime();
1915 ✗ err = http_read_header(h);
1916 ✗ latency = av_gettime() - latency;
1917
0/2
✗ Branch 120 → 121 not taken.
✗ Branch 120 → 122 not taken.
✗ if (err < 0)
1918 ✗ goto done;
1919
1920 ✗ s->nb_requests++;
1921 ✗ s->sum_latency += latency;
1922 ✗ s->max_latency = FFMAX(s->max_latency, latency);
1923
1924 /* Redirects and authentication challenges are answered with another
1925 * request, whose reply is checked instead. */
1926
0/4
✗ Branch 122 → 123 not taken.
✗ Branch 122 → 125 not taken.
✗ Branch 124 → 125 not taken.
✗ Branch 124 → 127 not taken.
✗ if ((s->new_location && is_redirect(s->http_code)) ||
1927
0/4
✗ Branch 125 → 126 not taken.
✗ Branch 125 → 127 not taken.
✗ Branch 126 → 127 not taken.
✗ Branch 126 → 128 not taken.
✗ s->http_code == 401 || s->http_code == 407)
1928 ✗ s->off = off;
1929
1930
0/2
✗ Branch 128 → 129 not taken.
✗ Branch 128 → 131 not taken.
✗ if (off != s->off) {
1931 ✗ av_log(h, AV_LOG_ERROR,
1932 "Unexpected offset: expected %"PRIu64", got %"PRIu64"\n",
1933 off, s->off);
1934 ✗ err = AVERROR(EIO);
1935 ✗ goto done;
1936 }
1937
1938 ✗ err = 0;
1939 ✗ done:
1940 ✗ av_freep(&authstr);
1941 ✗ av_freep(&proxyauthstr);
1942 ✗ return err;
1943 }
1944
1945 ✗ static uint64_t requested_end(HTTPContext *s)
1946 {
1947
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ return s->end_off ? FFMIN(s->end_off, s->filesize) : s->filesize;
1948 }
1949
1950 ✗ static int http_buf_read(URLContext *h, uint8_t *buf, int size)
1951 {
1952 ✗ HTTPContext *s = h->priv_data;
1953 int len;
1954
1955
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (!s->hd)
1956 ✗ return AVERROR(EIO);
1957
1958
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 43 not taken.
✗ if (s->chunksize != UINT64_MAX) {
1959
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (s->chunkend) {
1960 ✗ return AVERROR_EOF;
1961 }
1962
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 42 not taken.
✗ if (!s->chunksize) {
1963 char line[32];
1964 int err;
1965
1966 do {
1967
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if ((err = http_get_line(s, line, sizeof(line))) < 0)
1968 ✗ return err;
1969
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ } while (!*line); /* skip CR LF from last chunk */
1970
1971 ✗ s->chunksize = strtoull(line, NULL, 16);
1972
1973 ✗ av_log(h, AV_LOG_TRACE,
1974 "Chunked encoding data size: %"PRIu64"\n",
1975 s->chunksize);
1976
1977
0/2
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 37 not taken.
✗ if (!s->chunksize) {
1978 /* a partial reply that ends at its range end is continued */
1979
0/6
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 22 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 22 not taken.
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ int more = s->range_end && s->off == s->range_end &&
1980 ✗ s->range_end < requested_end(s);
1981
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 31 not taken.
✗ if (s->multiple_requests) {
1982 do {
1983
0/2
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ if ((err = http_get_line(s, line, sizeof(line))) < 0)
1984 ✗ return err;
1985
0/2
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ } while (*line);
1986 ✗ s->chunkend = !more;
1987 } else {
1988 ✗ av_log(h, AV_LOG_DEBUG, "Last chunk received, closing conn\n");
1989 ✗ ffurl_closep(&s->hd);
1990 }
1991
0/2
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 35 not taken.
✗ return more ? AVERROR(EAGAIN) : 0;
1992 }
1993
0/2
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 40 not taken.
✗ else if (s->chunksize == UINT64_MAX) {
1994 ✗ av_log(h, AV_LOG_ERROR, "Invalid chunk size %"PRIu64"\n",
1995 s->chunksize);
1996 ✗ return AVERROR(EINVAL);
1997 }
1998 }
1999 ✗ size = FFMIN(size, s->chunksize);
2000 }
2001
2002 /* read bytes from input buffer first */
2003 ✗ len = s->buf_end - s->buf_ptr;
2004
0/2
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 47 not taken.
✗ if (len > 0) {
2005
0/2
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 46 not taken.
✗ if (len > size)
2006 ✗ len = size;
2007 ✗ memcpy(buf, s->buf_ptr, len);
2008 ✗ s->buf_ptr += len;
2009 } else {
2010 ✗ uint64_t file_end = requested_end(s);
2011
0/2
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✗ uint64_t target_end = s->range_end ? s->range_end : file_end;
2012
0/6
✗ Branch 51 → 52 not taken.
✗ Branch 51 → 53 not taken.
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 55 not taken.
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 55 not taken.
✗ if ((!s->willclose || s->chunksize == UINT64_MAX) && s->off >= file_end)
2013 ✗ return AVERROR_EOF;
2014
0/4
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 58 not taken.
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 58 not taken.
✗ if (s->off == target_end && target_end < file_end)
2015 ✗ return AVERROR(EAGAIN); /* reached end of content range */
2016 ✗ len = ffurl_read(s->hd, buf, size);
2017
0/4
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 61 not taken.
✗ Branch 60 → 61 not taken.
✗ Branch 60 → 66 not taken.
✗ if ((!len || len == AVERROR_EOF) &&
2018
0/6
✗ Branch 61 → 62 not taken.
✗ Branch 61 → 63 not taken.
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 66 not taken.
✗ Branch 63 → 64 not taken.
✗ Branch 63 → 66 not taken.
✗ (!s->willclose || s->chunksize == UINT64_MAX) && s->off < target_end) {
2019 ✗ av_log(h, AV_LOG_ERROR,
2020 "Stream ends prematurely at %"PRIu64", should be %"PRIu64"\n",
2021 s->off, target_end
2022 );
2023 ✗ return AVERROR(EIO);
2024 }
2025 }
2026
0/2
✗ Branch 66 → 67 not taken.
✗ Branch 66 → 73 not taken.
✗ if (len > 0) {
2027 ✗ s->off += len;
2028
0/4
✗ Branch 67 → 68 not taken.
✗ Branch 67 → 73 not taken.
✗ Branch 68 → 69 not taken.
✗ Branch 68 → 73 not taken.
✗ if (s->chunksize > 0 && s->chunksize != UINT64_MAX) {
2029
0/2
✗ Branch 69 → 70 not taken.
✗ Branch 69 → 72 not taken.
✗ av_assert0(s->chunksize >= len);
2030 ✗ s->chunksize -= len;
2031 }
2032 }
2033 ✗ return len;
2034 }
2035
2036 #if CONFIG_ZLIB
2037 #define DECOMPRESS_BUF_SIZE (256 * 1024)
2038 ✗ static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
2039 {
2040 ✗ HTTPContext *s = h->priv_data;
2041 int ret;
2042
2043
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 6 not taken.
✗ if (!s->inflate_buffer) {
2044 ✗ s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
2045
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (!s->inflate_buffer)
2046 ✗ return AVERROR(ENOMEM);
2047 }
2048
2049
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 13 not taken.
✗ if (s->inflate_stream.avail_in == 0) {
2050 ✗ int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
2051
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (read == AVERROR(EAGAIN)) {
2052 ✗ s->chunkend = 1;
2053 ✗ return AVERROR_EOF;
2054 }
2055
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (read <= 0)
2056 ✗ return read;
2057 ✗ s->inflate_stream.next_in = s->inflate_buffer;
2058 ✗ s->inflate_stream.avail_in = read;
2059 }
2060
2061 ✗ s->inflate_stream.avail_out = size;
2062 ✗ s->inflate_stream.next_out = buf;
2063
2064 ✗ ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
2065
0/4
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 17 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ if (ret != Z_OK && ret != Z_STREAM_END)
2066 ✗ av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n",
2067 ret, s->inflate_stream.msg);
2068
2069 ✗ return size - s->inflate_stream.avail_out;
2070 }
2071 #endif /* CONFIG_ZLIB */
2072
2073 /* Record the end of the resource if err is the 416 that marks it. */
2074 ✗ static int http_end_of_resource(HTTPContext *s, int err)
2075 {
2076
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ if (!is_end_of_resource(s, err, s->off))
2077 ✗ return 0;
2078 ✗ s->oc_filesize = s->filesize_from_content_range;
2079 ✗ s->filesize = s->oc_filesize;
2080 ✗ s->end_of_resource = 1;
2081 ✗ return 1;
2082 }
2083
2084 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect);
2085
2086 ✗ static int http_read_stream(URLContext *h, uint8_t *buf, int size)
2087 {
2088 ✗ HTTPContext *s = h->priv_data;
2089 int err, read_ret;
2090 int64_t seek_ret;
2091 ✗ int reconnect_delay = 0;
2092 ✗ int reconnect_delay_total = 0;
2093 ✗ int conn_attempts = 1;
2094
2095
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (s->end_of_resource)
2096 ✗ return AVERROR_EOF;
2097
2098
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 10 not taken.
✗ if (!s->hd)
2099
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ return s->off < requested_end(s) ? AVERROR(EIO) : AVERROR_EOF;
2100
2101
0/4
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 15 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 15 not taken.
✗ if (s->end_chunked_post && !s->end_header) {
2102 ✗ err = http_read_header(h);
2103
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if (err < 0)
2104 ✗ return err;
2105 }
2106
2107 #if CONFIG_ZLIB
2108
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 18 not taken.
✗ if (s->compressed)
2109 ✗ return http_buf_read_compressed(h, buf, size);
2110 #endif /* CONFIG_ZLIB */
2111
2112 ✗ retry:
2113 ✗ read_ret = http_buf_read(h, buf, size);
2114
0/2
✗ Branch 79 → 21 not taken.
✗ Branch 79 → 80 not taken.
✗ while (read_ret < 0) {
2115
0/2
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ uint64_t target = h->is_streamed ? 0 : s->off;
2116 ✗ uint64_t file_end = requested_end(s);
2117
0/4
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 28 not taken.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ bool is_premature = file_end > 0 && s->off < file_end;
2118
2119
0/2
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 31 not taken.
✗ if (read_ret == AVERROR_EXIT)
2120 ✗ break;
2121
0/2
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 43 not taken.
✗ else if (read_ret == AVERROR(EAGAIN)) {
2122 /* send new request for more data on existing connection */
2123 ✗ AVDictionary *options = NULL;
2124
0/2
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 34 not taken.
✗ if (s->willclose)
2125 ✗ ffurl_closep(&s->hd);
2126 ✗ s->initial_requests = 0; /* continue streaming uninterrupted from now on */
2127 ✗ read_ret = http_open_cnx(h, &options, 1);
2128 ✗ av_dict_free(&options);
2129
0/2
✗ Branch 36 → 37 not taken.
✗ Branch 36 → 38 not taken.
✗ if (read_ret == 0)
2130 ✗ goto retry;
2131
0/2
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 41 not taken.
✗ if (http_end_of_resource(s, read_ret)) {
2132 ✗ read_ret = AVERROR_EOF;
2133 ✗ break;
2134 }
2135 }
2136
2137
0/4
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 46 not taken.
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 46 not taken.
✗ if (h->is_streamed && !s->reconnect_streamed)
2138 ✗ break;
2139
2140
0/4
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 48 not taken.
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 53 not taken.
✗ if (!(s->reconnect && is_premature) &&
2141
0/4
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 50 not taken.
✗ Branch 49 → 50 not taken.
✗ Branch 49 → 53 not taken.
✗ !(s->reconnect_at_eof && read_ret == AVERROR_EOF)) {
2142
0/2
✗ Branch 50 → 51 not taken.
✗ Branch 50 → 52 not taken.
✗ if (is_premature)
2143 ✗ return AVERROR(EIO);
2144 else
2145 ✗ break;
2146 }
2147
2148
0/6
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 57 not taken.
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 56 not taken.
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 57 not taken.
✗ if (reconnect_delay > s->reconnect_delay_max || (s->reconnect_max_retries >= 0 && conn_attempts > s->reconnect_max_retries) ||
2149
0/2
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 58 not taken.
✗ reconnect_delay_total > s->reconnect_delay_total_max)
2150 ✗ return AVERROR(EIO);
2151
2152
0/2
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 61 not taken.
✗ av_log(h, AV_LOG_WARNING, "Will %s at %"PRIu64" in %d second(s), error=%s.\n", s->willclose ? "reconnect" : "retry",
2153 ✗ s->off, reconnect_delay, av_err2str(read_ret));
2154 ✗ err = ff_network_sleep_interruptible(1000U*1000*reconnect_delay, &h->interrupt_callback);
2155
0/2
✗ Branch 64 → 65 not taken.
✗ Branch 64 → 66 not taken.
✗ if (err != AVERROR(ETIMEDOUT))
2156 ✗ return err;
2157 ✗ reconnect_delay_total += reconnect_delay;
2158 ✗ reconnect_delay = 1 + 2*reconnect_delay;
2159 ✗ conn_attempts++;
2160 ✗ seek_ret = http_seek_internal(h, target, SEEK_SET, 1);
2161
0/4
✗ Branch 67 → 68 not taken.
✗ Branch 67 → 72 not taken.
✗ Branch 68 → 69 not taken.
✗ Branch 68 → 72 not taken.
✗ if (seek_ret >= 0 && seek_ret != target) {
2162 ✗ ffurl_closep(&s->hd);
2163 ✗ av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRIu64".\n", target);
2164 ✗ return read_ret;
2165 }
2166
0/2
✗ Branch 72 → 73 not taken.
✗ Branch 72 → 74 not taken.
✗ if (s->end_of_resource) {
2167 ✗ read_ret = AVERROR_EOF;
2168 ✗ break;
2169 }
2170
2171 ✗ read_ret = http_buf_read(h, buf, size);
2172 }
2173
2174 ✗ return read_ret;
2175 }
2176
2177 // Like http_read_stream(), but no short reads.
2178 // Assumes partial reads are an error.
2179 ✗ static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
2180 {
2181 ✗ int pos = 0;
2182
0/2
✗ Branch 7 → 3 not taken.
✗ Branch 7 → 8 not taken.
✗ while (pos < size) {
2183 ✗ int len = http_read_stream(h, buf + pos, size - pos);
2184
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (len < 0)
2185 ✗ return len;
2186 ✗ pos += len;
2187 }
2188 ✗ return pos;
2189 }
2190
2191 ✗ static int store_icy(URLContext *h, int size)
2192 {
2193 ✗ HTTPContext *s = h->priv_data;
2194 /* until next metadata packet */
2195 uint64_t remaining;
2196
2197
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (s->icy_metaint < s->icy_data_read)
2198 ✗ return AVERROR_INVALIDDATA;
2199 ✗ remaining = s->icy_metaint - s->icy_data_read;
2200
2201
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 20 not taken.
✗ if (!remaining) {
2202 /* The metadata packet is variable sized. It has a 1 byte header
2203 * which sets the length of the packet (divided by 16). If it's 0,
2204 * the metadata doesn't change. After the packet, icy_metaint bytes
2205 * of normal data follows. */
2206 uint8_t ch;
2207 ✗ int len = http_read_stream_all(h, &ch, 1);
2208
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (len < 0)
2209 ✗ return len;
2210
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 18 not taken.
✗ if (ch > 0) {
2211 char data[255 * 16 + 1];
2212 int ret;
2213 ✗ len = ch * 16;
2214 ✗ ret = http_read_stream_all(h, data, len);
2215
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (ret < 0)
2216 ✗ return ret;
2217 ✗ data[len] = 0;
2218
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 15 not taken.
✗ if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
2219 ✗ return ret;
2220 ✗ ff_http_parse_icy_packet(h, &s->metadata, data);
2221 }
2222 ✗ s->icy_data_read = 0;
2223 ✗ remaining = s->icy_metaint;
2224 }
2225
2226 ✗ return FFMIN(size, remaining);
2227 }
2228
2229 ✗ static int http_read(URLContext *h, uint8_t *buf, int size)
2230 {
2231 ✗ HTTPContext *s = h->priv_data;
2232
2233
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 6 not taken.
✗ if (s->icy_metaint > 0) {
2234 ✗ size = store_icy(h, size);
2235
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
✗ if (size < 0)
2236 ✗ return size;
2237 }
2238
2239 ✗ size = http_read_stream(h, buf, size);
2240
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ if (size > 0)
2241 ✗ s->icy_data_read += size;
2242 ✗ return size;
2243 }
2244
2245 /* used only when posting data */
2246 ✗ static int http_write(URLContext *h, const uint8_t *buf, int size)
2247 {
2248 ✗ char temp[11] = ""; /* 32-bit hex + CRLF + nul */
2249 int ret;
2250 ✗ char crlf[] = "\r\n";
2251 ✗ HTTPContext *s = h->priv_data;
2252
2253
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 5 not taken.
✗ if (!s->chunked_post) {
2254 /* non-chunked data is sent without any special encoding */
2255 ✗ return ffurl_write(s->hd, buf, size);
2256 }
2257
2258 /* silently ignore zero-size data since chunk encoding that would
2259 * signal EOF */
2260
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 13 not taken.
✗ if (size > 0) {
2261 /* upload data using chunked encoding */
2262 ✗ snprintf(temp, sizeof(temp), "%x\r\n", size);
2263
2264
0/4
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 12 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 12 not taken.
✗ if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
2265
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ (ret = ffurl_write(s->hd, buf, size)) < 0 ||
2266 ✗ (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
2267 ✗ return ret;
2268 }
2269 ✗ return size;
2270 }
2271
2272 ✗ static int http_shutdown(URLContext *h, int flags)
2273 {
2274 ✗ int ret = 0;
2275 ✗ char footer[] = "0\r\n\r\n";
2276 ✗ HTTPContext *s = h->priv_data;
2277
2278 /* signal end of chunked encoding if used */
2279
0/4
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 7 not taken.
✗ if (((flags & AVIO_FLAG_WRITE) && s->chunked_post) ||
2280
0/6
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 17 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 17 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 17 not taken.
✗ ((flags & AVIO_FLAG_READ) && s->chunked_post && s->listen)) {
2281 ✗ ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
2282 ✗ ret = ret > 0 ? 0 : ret;
2283 /* flush the receive buffer when it is write only mode */
2284
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 16 not taken.
✗ if (!(flags & AVIO_FLAG_READ)) {
2285 char buf[1024];
2286 int read_ret;
2287 ✗ s->hd->flags |= AVIO_FLAG_NONBLOCK;
2288 ✗ read_ret = ffurl_read(s->hd, buf, sizeof(buf));
2289 ✗ s->hd->flags &= ~AVIO_FLAG_NONBLOCK;
2290
0/4
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 15 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 15 not taken.
✗ if (read_ret < 0 && read_ret != AVERROR(EAGAIN)) {
2291 ✗ av_log(h, AV_LOG_ERROR, "URL read error: %s\n", av_err2str(read_ret));
2292 ✗ ret = read_ret;
2293 }
2294 }
2295 ✗ s->end_chunked_post = 1;
2296 }
2297
2298 ✗ return ret;
2299 }
2300
2301 ✗ static int http_close(URLContext *h)
2302 {
2303 ✗ int ret = 0;
2304 ✗ HTTPContext *s = h->priv_data;
2305
2306 #if CONFIG_ZLIB
2307 ✗ inflateEnd(&s->inflate_stream);
2308 ✗ av_freep(&s->inflate_buffer);
2309 #endif /* CONFIG_ZLIB */
2310
2311
0/4
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 7 not taken.
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (s->hd && !s->end_chunked_post)
2312 /* Close the write direction by sending the end of chunked encoding. */
2313 ✗ ret = http_shutdown(h, h->flags);
2314
2315
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ if (s->hd)
2316 ✗ ffurl_closep(&s->hd);
2317 ✗ av_dict_free(&s->chained_options);
2318 ✗ av_dict_free(&s->cookie_dict);
2319 ✗ av_dict_free(&s->redirect_cache);
2320 ✗ av_freep(&s->new_location);
2321 ✗ av_freep(&s->uri);
2322 ✗ av_freep(&s->host);
2323
2324 ✗ av_log(h, AV_LOG_DEBUG, "Statistics: %d connection%s, %d request%s, %d retr%s, %d reconnection%s, %d redirect%s\n",
2325
0/2
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
✗ s->nb_connections, s->nb_connections == 1 ? "" : "s",
2326
0/2
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 26 not taken.
✗ s->nb_requests, s->nb_requests == 1 ? "" : "s",
2327
0/2
✗ Branch 21 → 22 not taken.
✗ Branch 21 → 23 not taken.
✗ s->nb_retries, s->nb_retries == 1 ? "y" : "ies",
2328
0/2
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ s->nb_reconnects, s->nb_reconnects == 1 ? "" : "s",
2329
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ s->nb_redirects, s->nb_redirects == 1 ? "" : "s");
2330
2331
0/2
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ if (s->nb_requests > 0) {
2332 ✗ av_log(h, AV_LOG_DEBUG, "Latency: %.2f ms avg, %.2f ms max\n",
2333 ✗ 1e-3 * s->sum_latency / s->nb_requests,
2334 ✗ 1e-3 * s->max_latency);
2335 }
2336 ✗ return ret;
2337 }
2338
2339 ✗ static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
2340 {
2341 ✗ HTTPContext *s = h->priv_data;
2342 ✗ URLContext *old_hd = NULL;
2343 ✗ uint64_t old_off = s->off;
2344 ✗ uint64_t old_range_end = s->range_end;
2345 ✗ uint64_t old_chunksize = s->chunksize;
2346 ✗ int old_chunkend = s->chunkend;
2347 ✗ int old_willclose = s->willclose;
2348 #if CONFIG_ZLIB
2349 ✗ int old_compressed = s->compressed;
2350 #endif
2351 ✗ int old_end_of_resource = s->end_of_resource;
2352 uint8_t old_buf[BUFFER_SIZE];
2353 int old_buf_size, ret;
2354 ✗ AVDictionary *options = NULL;
2355 uint8_t discard[4096];
2356
2357
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 7 not taken.
✗ if (whence == AVSEEK_SIZE)
2358
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 5 not taken.
✗ return s->filesize == UINT64_MAX ? AVERROR(ENOSYS) : s->filesize;
2359
0/4
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 10 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ else if ((s->filesize == UINT64_MAX && whence == SEEK_END))
2360 ✗ return AVERROR(ENOSYS);
2361
2362
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (whence == SEEK_CUR)
2363 ✗ off += s->off;
2364
0/2
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ else if (whence == SEEK_END)
2365 ✗ off += s->filesize;
2366
0/2
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ else if (whence != SEEK_SET)
2367 ✗ return AVERROR(EINVAL);
2368
0/2
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ if (off < 0)
2369 ✗ return AVERROR(EINVAL);
2370
0/6
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 22 not taken.
✗ Branch 19 → 20 not taken.
✗ Branch 19 → 22 not taken.
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ if (!force_reconnect && off == s->off && s->hd)
2371 ✗ return s->off;
2372
0/4
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 25 not taken.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ if (off && h->is_streamed)
2373 ✗ return AVERROR(ENOSYS);
2374 ✗ s->off = off;
2375 ✗ s->end_of_resource = 0;
2376
2377 /* do not try to make a new connection if seeking past the end of the file */
2378
0/2
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 29 not taken.
✗ if (s->off >= requested_end(s)) {
2379 /* nothing of the old reply belongs to this offset */
2380 ✗ ffurl_closep(&s->hd);
2381 ✗ s->buf_ptr = s->buf_end = s->buffer;
2382 ✗ return s->off;
2383 }
2384
2385 /* if the location changed (redirect), revert to the original uri */
2386
0/2
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 35 not taken.
✗ if (strcmp(s->uri, s->location)) {
2387 char *new_uri;
2388 ✗ new_uri = av_strdup(s->uri);
2389
0/2
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
✗ if (!new_uri)
2390 ✗ return AVERROR(ENOMEM);
2391 ✗ av_free(s->location);
2392 ✗ s->location = new_uri;
2393 }
2394
2395 /* we save the old context in case the seek fails */
2396 ✗ old_buf_size = s->buf_end - s->buf_ptr;
2397 ✗ memcpy(old_buf, s->buf_ptr, old_buf_size);
2398
2399 /* try to reuse existing connection for small seeks */
2400 ✗ int short_seek = ffurl_get_short_seek(h);
2401 ✗ uint64_t old_read_pos = old_off + old_buf_size;
2402
0/8
✗ Branch 36 → 37 not taken.
✗ Branch 36 → 59 not taken.
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 59 not taken.
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 59 not taken.
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 59 not taken.
✗ if (s->hd && !s->willclose && s->range_end && short_seek > 0 &&
2403
0/2
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 59 not taken.
✗ old_read_pos + short_seek >= s->range_end)
2404 ✗ {
2405 ✗ uint64_t remaining = s->range_end - old_read_pos;
2406 av_assert1(remaining <= short_seek);
2407
2408 /* drain remaining data left on the wire from previous request */
2409 ✗ av_log(h, AV_LOG_DEBUG, "Soft-seeking to offset %"PRIu64" by draining "
2410 "%"PRIu64" remaining byte(s)\n", s->off, remaining);
2411
0/2
✗ Branch 51 → 43 not taken.
✗ Branch 51 → 52 not taken.
✗ while (remaining) {
2412 ✗ ret = ffurl_read(s->hd, discard, FFMIN(remaining, sizeof(discard)));
2413
0/8
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 48 not taken.
✗ Branch 45 → 46 not taken.
✗ Branch 45 → 48 not taken.
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 50 not taken.
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 50 not taken.
✗ if (ret < 0 || ret == AVERROR_EOF || (ret == 0 && remaining)) {
2414 /* connection broken or stuck, need to reopen */
2415 ✗ ffurl_closep(&s->hd);
2416 ✗ break;
2417 }
2418 ✗ remaining -= ret;
2419 }
2420
2421 ✗ ret = http_open_cnx(h, &options, 1);
2422
0/4
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 56 not taken.
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 57 not taken.
✗ if (ret >= 0 || http_end_of_resource(s, ret)) {
2423 ✗ goto done;
2424 } else {
2425 /* fall back to normal reconnection */
2426 ✗ ffurl_closep(&s->hd);
2427 ✗ old_hd = NULL;
2428 }
2429 } else {
2430 /* can't soft seek; always open new connection */
2431 ✗ old_hd = s->hd;
2432 ✗ s->hd = NULL;
2433 }
2434
2435
0/4
✗ Branch 61 → 62 not taken.
✗ Branch 61 → 69 not taken.
✗ Branch 63 → 64 not taken.
✗ Branch 63 → 65 not taken.
✗ if ((ret = http_open_cnx(h, &options, 1)) < 0 &&
2436 ✗ !http_end_of_resource(s, ret)) {
2437 /* if it fails, continue on old connection if possible */
2438
0/2
✗ Branch 65 → 66 not taken.
✗ Branch 65 → 67 not taken.
✗ if (old_hd) {
2439 ✗ memcpy(s->buffer, old_buf, old_buf_size);
2440 ✗ s->buf_ptr = s->buffer;
2441 ✗ s->buf_end = s->buffer + old_buf_size;
2442 ✗ s->hd = old_hd;
2443 ✗ s->range_end = old_range_end;
2444 ✗ s->chunksize = old_chunksize;
2445 ✗ s->chunkend = old_chunkend;
2446 ✗ s->willclose = old_willclose;
2447 #if CONFIG_ZLIB
2448 ✗ s->compressed = old_compressed;
2449 #endif
2450 }
2451 ✗ s->off = old_off;
2452 ✗ s->end_of_resource = old_end_of_resource;
2453 ✗ av_dict_free(&options);
2454 ✗ return ret;
2455 }
2456
2457 ✗ done:
2458 ✗ av_dict_free(&options);
2459 ✗ ffurl_close(old_hd);
2460 ✗ return off;
2461 }
2462
2463 ✗ static int64_t http_seek(URLContext *h, int64_t off, int whence)
2464 {
2465 ✗ return http_seek_internal(h, off, whence, 0);
2466 }
2467
2468 ✗ static int http_get_file_handle(URLContext *h)
2469 {
2470 ✗ HTTPContext *s = h->priv_data;
2471 ✗ return ffurl_get_file_handle(s->hd);
2472 }
2473
2474 ✗ static int http_get_short_seek(URLContext *h)
2475 {
2476 ✗ HTTPContext *s = h->priv_data;
2477
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (s->short_seek_size >= 1)
2478 ✗ return s->short_seek_size;
2479 ✗ return ffurl_get_short_seek(s->hd);
2480 }
2481
2482 #define HTTP_CLASS(flavor) \
2483 static const AVClass flavor ## _context_class = { \
2484 .class_name = # flavor, \
2485 .item_name = av_default_item_name, \
2486 .option = http_options, \
2487 .version = LIBAVUTIL_VERSION_INT, \
2488 }
2489
2490 #if CONFIG_HTTP_PROTOCOL
2491 HTTP_CLASS(http);
2492
2493 const URLProtocol ff_http_protocol = {
2494 .name = "http",
2495 .url_open2 = http_open,
2496 .url_accept = http_accept,
2497 .url_handshake = http_handshake,
2498 .url_read = http_read,
2499 .url_write = http_write,
2500 .url_seek = http_seek,
2501 .url_close = http_close,
2502 .url_get_file_handle = http_get_file_handle,
2503 .url_get_short_seek = http_get_short_seek,
2504 .url_shutdown = http_shutdown,
2505 .priv_data_size = sizeof(HTTPContext),
2506 .priv_data_class = &http_context_class,
2507 .flags = URL_PROTOCOL_FLAG_NETWORK,
2508 .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy,data"
2509 };
2510 #endif /* CONFIG_HTTP_PROTOCOL */
2511
2512 #if CONFIG_HTTPS_PROTOCOL
2513 HTTP_CLASS(https);
2514
2515 const URLProtocol ff_https_protocol = {
2516 .name = "https",
2517 .url_open2 = http_open,
2518 .url_read = http_read,
2519 .url_write = http_write,
2520 .url_seek = http_seek,
2521 .url_close = http_close,
2522 .url_get_file_handle = http_get_file_handle,
2523 .url_get_short_seek = http_get_short_seek,
2524 .url_shutdown = http_shutdown,
2525 .priv_data_size = sizeof(HTTPContext),
2526 .priv_data_class = &https_context_class,
2527 .flags = URL_PROTOCOL_FLAG_NETWORK,
2528 .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy"
2529 };
2530 #endif /* CONFIG_HTTPS_PROTOCOL */
2531
2532 #if CONFIG_HTTPPROXY_PROTOCOL
2533 ✗ static int http_proxy_close(URLContext *h)
2534 {
2535 ✗ HTTPContext *s = h->priv_data;
2536
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (s->hd)
2537 ✗ ffurl_closep(&s->hd);
2538 ✗ return 0;
2539 }
2540
2541 ✗ static int http_proxy_open(URLContext *h, const char *uri, int flags)
2542 {
2543 ✗ HTTPContext *s = h->priv_data;
2544 char hostname[1024], hoststr[1024];
2545 char auth[1024], pathbuf[1024], *path;
2546 char lower_url[100];
2547 ✗ int port, ret = 0, auth_attempts = 0;
2548 HTTPAuthType cur_auth_type;
2549 char *authstr;
2550
2551
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if( s->seekable == 1 )
2552 ✗ h->is_streamed = 0;
2553 else
2554 ✗ h->is_streamed = 1;
2555
2556 ✗ av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
2557 pathbuf, sizeof(pathbuf), uri);
2558 ✗ ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
2559 ✗ path = pathbuf;
2560
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 9 not taken.
✗ if (*path == '/')
2561 ✗ path++;
2562
2563 ✗ ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
2564 NULL);
2565 ✗ redo:
2566 ✗ ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
2567 ✗ &h->interrupt_callback, NULL,
2568 h->protocol_whitelist, h->protocol_blacklist, h);
2569
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ if (ret < 0)
2570 ✗ return ret;
2571
2572 ✗ authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
2573 path, "CONNECT");
2574 ✗ snprintf(s->buffer, sizeof(s->buffer),
2575 "CONNECT %s HTTP/1.1\r\n"
2576 "Host: %s\r\n"
2577 "Connection: close\r\n"
2578 "%s%s"
2579 "\r\n",
2580 path,
2581 hoststr,
2582
0/4
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ authstr ? "Proxy-" : "", authstr ? authstr : "");
2583 ✗ av_freep(&authstr);
2584
2585
0/2
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
2586 ✗ goto fail;
2587
2588 ✗ s->buf_ptr = s->buffer;
2589 ✗ s->buf_end = s->buffer;
2590 ✗ s->line_count = 0;
2591 ✗ s->filesize = UINT64_MAX;
2592 ✗ cur_auth_type = s->proxy_auth_state.auth_type;
2593
2594 /* Note: This uses buffering, potentially reading more than the
2595 * HTTP header. If tunneling a protocol where the server starts
2596 * the conversation, we might buffer part of that here, too.
2597 * Reading that requires using the proper ffurl_read() function
2598 * on this URLContext, not using the fd directly (as the tls
2599 * protocol does). This shouldn't be an issue for tls though,
2600 * since the client starts the conversation there, so there
2601 * is no extra data that we might buffer up here.
2602 */
2603 ✗ ret = http_read_header(h);
2604
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 27 not taken.
✗ if (ret < 0)
2605 ✗ goto fail;
2606
2607 ✗ auth_attempts++;
2608
0/4
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 34 not taken.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ if (s->http_code == 407 &&
2609
0/2
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 34 not taken.
✗ (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
2610
0/4
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 34 not taken.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 34 not taken.
✗ s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && auth_attempts < 2) {
2611 ✗ ffurl_closep(&s->hd);
2612 ✗ goto redo;
2613 }
2614
2615
0/2
✗ Branch 34 → 35 not taken.
✗ Branch 34 → 36 not taken.
✗ if (s->http_code < 400)
2616 ✗ return 0;
2617 ✗ ret = ff_http_averror(s->http_code, AVERROR(EIO));
2618
2619 ✗ fail:
2620 ✗ http_proxy_close(h);
2621 ✗ return ret;
2622 }
2623
2624 ✗ static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
2625 {
2626 ✗ HTTPContext *s = h->priv_data;
2627 ✗ return ffurl_write(s->hd, buf, size);
2628 }
2629
2630 const URLProtocol ff_httpproxy_protocol = {
2631 .name = "httpproxy",
2632 .url_open = http_proxy_open,
2633 .url_read = http_buf_read,
2634 .url_write = http_proxy_write,
2635 .url_close = http_proxy_close,
2636 .url_get_file_handle = http_get_file_handle,
2637 .priv_data_size = sizeof(HTTPContext),
2638 .flags = URL_PROTOCOL_FLAG_NETWORK,
2639 };
2640 #endif /* CONFIG_HTTPPROXY_PROTOCOL */
2641