FFmpeg  4.4.9
http.c
Go to the documentation of this file.
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 "config.h"
23 
24 #if CONFIG_ZLIB
25 #include <zlib.h>
26 #endif /* CONFIG_ZLIB */
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/bprint.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/time.h"
33 #include "libavutil/parseutils.h"
34 
35 #include "avformat.h"
36 #include "http.h"
37 #include "httpauth.h"
38 #include "internal.h"
39 #include "network.h"
40 #include "os_support.h"
41 #include "url.h"
42 
43 /* XXX: POST protocol is not completely implemented because ffmpeg uses
44  * only a subset of it. */
45 
46 /* The IO buffer size is unrelated to the max URL size in itself, but needs
47  * to be large enough to fit the full request headers (including long
48  * path names). */
49 #define BUFFER_SIZE (MAX_URL_SIZE + HTTP_HEADERS_SIZE)
50 #define MAX_REDIRECTS 8
51 #define HTTP_SINGLE 1
52 #define HTTP_MUTLI 2
53 #define MAX_EXPIRY 19
54 #define WHITESPACES " \n\t\r"
55 typedef enum {
59  FINISH
61 
62 typedef struct HTTPContext {
63  const AVClass *class;
65  unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
67  int http_code;
68  /* Used if "Transfer-Encoding: chunked" otherwise -1. */
69  uint64_t chunksize;
70  int chunkend;
71  uint64_t off, end_off, filesize;
72  char *location;
75  char *http_proxy;
76  char *headers;
77  char *mime_type;
78  char *http_version;
79  char *user_agent;
80  char *referer;
81  char *content_type;
82  /* Set if the server correctly handles Connection: close and will close
83  * the connection after feeding us the content. */
84  int willclose;
85  int seekable; /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
87  /* A flag which indicates if the end of chunked encoding has been sent. */
89  /* A flag which indicates we have finished to read POST reply. */
91  /* A flag which indicates if we use persistent connections. */
95  int is_akamai;
97  char *cookies; ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
98  /* A dictionary containing cookies keyed by cookie name */
100  char *host;
101  int icy;
102  /* how much data was read since the last ICY metadata packet */
103  uint64_t icy_data_read;
104  /* after how many bytes of read data a new metadata packet will be found */
105  uint64_t icy_metaint;
109 #if CONFIG_ZLIB
110  int compressed;
111  z_stream inflate_stream;
112  uint8_t *inflate_buffer;
113 #endif /* CONFIG_ZLIB */
115  /* -1 = try to send if applicable, 0 = always disabled, 1 = always enabled */
117  char *method;
124  int listen;
125  char *resource;
131 } HTTPContext;
132 
133 #define OFFSET(x) offsetof(HTTPContext, x)
134 #define D AV_OPT_FLAG_DECODING_PARAM
135 #define E AV_OPT_FLAG_ENCODING_PARAM
136 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
137 
138 static const AVOption options[] = {
139  { "seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D },
140  { "chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
141  { "http_proxy", "set HTTP proxy to tunnel through", OFFSET(http_proxy), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
142  { "headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
143  { "content_type", "set a specific content type for the POST messages", OFFSET(content_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
144  { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
145  { "referer", "override referer header", OFFSET(referer), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
146 #if FF_API_HTTP_USER_AGENT
147  { "user-agent", "use the \"user_agent\" option instead", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D|AV_OPT_FLAG_DEPRECATED },
148 #endif
149  { "multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D | E },
150  { "post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D | E },
151  { "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 },
152  { "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 },
153  { "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 },
154  { "icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, D },
155  { "icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
156  { "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
157  { "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
158  { "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, "auth_type"},
159  { "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, "auth_type"},
160  { "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 0, 0, D | E, "auth_type"},
161  { "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 },
162  { "location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
163  { "offset", "initial byte offset", OFFSET(off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
164  { "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 },
165  { "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 },
166  { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
167  { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
168  { "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 },
169  { "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 },
170  { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
171  { "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 },
172  { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
173  { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
174  { "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
175  { "max_redirects", "Maximum number of redirects", OFFSET(max_redirects), AV_OPT_TYPE_INT, { .i64 = MAX_REDIRECTS }, 0, INT_MAX, D },
176  { NULL }
177 };
178 
179 static int http_connect(URLContext *h, const char *path, const char *local_path,
180  const char *hoststr, const char *auth,
181  const char *proxyauth, int *new_location);
182 static int http_read_header(URLContext *h, int *new_location);
183 static int bind_cookies_to_host(HTTPContext *s);
184 static int http_shutdown(URLContext *h, int flags);
185 
187 {
188  memcpy(&((HTTPContext *)dest->priv_data)->auth_state,
189  &((HTTPContext *)src->priv_data)->auth_state,
190  sizeof(HTTPAuthState));
191  memcpy(&((HTTPContext *)dest->priv_data)->proxy_auth_state,
192  &((HTTPContext *)src->priv_data)->proxy_auth_state,
193  sizeof(HTTPAuthState));
194 }
195 
197 {
198  const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
199  char *hashmark;
200  char hostname[1024], hoststr[1024], proto[10];
201  char auth[1024], proxyauth[1024] = "";
202  char path1[MAX_URL_SIZE], sanitized_path[MAX_URL_SIZE + 1];
203  char buf[1024], urlbuf[MAX_URL_SIZE];
204  int port, use_proxy, err, location_changed = 0;
205  HTTPContext *s = h->priv_data;
206 
207  av_url_split(proto, sizeof(proto), auth, sizeof(auth),
208  hostname, sizeof(hostname), &port,
209  path1, sizeof(path1), s->location);
210  if (strpbrk(hostname, "\r\n"))
211  return AVERROR(EINVAL);
212 
213  av_freep(&s->host);
214  s->host = av_strdup(hostname);
215  if (!s->host)
216  return AVERROR(ENOMEM);
217 
218  ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
219 
220  proxy_path = s->http_proxy ? s->http_proxy : getenv("http_proxy");
221  use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
222  proxy_path && av_strstart(proxy_path, "http://", NULL);
223 
224  if (h->protocol_whitelist && av_match_list(proto, h->protocol_whitelist, ',') <= 0) {
225  av_log(h, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", proto, h->protocol_whitelist);
226  return AVERROR(EINVAL);
227  }
228 
229  if (h->protocol_blacklist && av_match_list(proto, h->protocol_blacklist, ',') > 0) {
230  av_log(h, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", proto, h->protocol_blacklist);
231  return AVERROR(EINVAL);
232  }
233 
234  if (!strcmp(proto, "https")) {
235  lower_proto = "tls";
236  use_proxy = 0;
237  if (port < 0)
238  port = 443;
239  /* pass http_proxy to underlying protocol */
240  if (s->http_proxy) {
241  err = av_dict_set(options, "http_proxy", s->http_proxy, 0);
242  if (err < 0)
243  return err;
244  }
245  } else if (strcmp(proto, "http")) {
246  return AVERROR(EINVAL);
247  }
248 
249  if (port < 0)
250  port = 80;
251 
252  hashmark = strchr(path1, '#');
253  if (hashmark)
254  *hashmark = '\0';
255 
256  if (path1[0] == '\0') {
257  path = "/";
258  } else if (path1[0] == '?') {
259  snprintf(sanitized_path, sizeof(sanitized_path), "/%s", path1);
260  path = sanitized_path;
261  } else {
262  path = path1;
263  }
264  local_path = path;
265  if (use_proxy) {
266  /* Reassemble the request URL without auth string - we don't
267  * want to leak the auth to the proxy. */
268  ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
269  path1);
270  path = urlbuf;
271  av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
272  hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
273  }
274 
275  ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
276 
277  if (s->hd && strcmp(s->hd->filename, buf))
278  ffurl_closep(&s->hd);
279 
280  if (!s->hd) {
282  &h->interrupt_callback, options,
283  h->protocol_whitelist, h->protocol_blacklist, h);
284  if (err < 0)
285  return err;
286  }
287 
288  err = http_connect(h, path, local_path, hoststr,
289  auth, proxyauth, &location_changed);
290  if (err < 0)
291  return err;
292 
293  return location_changed;
294 }
295 
296 static int http_should_reconnect(HTTPContext *s, int err)
297 {
298  const char *status_group;
299  char http_code[4];
300 
301  switch (err) {
307  status_group = "4xx";
308  break;
309 
311  status_group = "5xx";
312  break;
313 
314  default:
315  return s->reconnect_on_network_error;
316  }
317 
318  if (!s->reconnect_on_http_error)
319  return 0;
320 
321  if (av_match_list(status_group, s->reconnect_on_http_error, ',') > 0)
322  return 1;
323 
324  snprintf(http_code, sizeof(http_code), "%d", s->http_code);
325 
326  return av_match_list(http_code, s->reconnect_on_http_error, ',') > 0;
327 }
328 
329 /* return non zero if error */
331 {
332  HTTPAuthType cur_auth_type, cur_proxy_auth_type;
333  HTTPContext *s = h->priv_data;
334  int location_changed, attempts = 0, redirects = 0;
335  int reconnect_delay = 0;
336  uint64_t off;
337 
338 redo:
339  av_dict_copy(options, s->chained_options, 0);
340 
341  cur_auth_type = s->auth_state.auth_type;
342  cur_proxy_auth_type = s->auth_state.auth_type;
343 
344  off = s->off;
345  location_changed = http_open_cnx_internal(h, options);
346  if (location_changed < 0) {
347  if (!http_should_reconnect(s, location_changed) ||
348  reconnect_delay > s->reconnect_delay_max)
349  goto fail;
350 
351  av_log(h, AV_LOG_WARNING, "Will reconnect at %"PRIu64" in %d second(s).\n", off, reconnect_delay);
352  location_changed = ff_network_sleep_interruptible(1000U * 1000 * reconnect_delay, &h->interrupt_callback);
353  if (location_changed != AVERROR(ETIMEDOUT))
354  goto fail;
355  reconnect_delay = 1 + 2 * reconnect_delay;
356 
357  /* restore the offset (http_connect resets it) */
358  s->off = off;
359 
360  ffurl_closep(&s->hd);
361  goto redo;
362  }
363 
364  attempts++;
365  if (s->http_code == 401) {
366  if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
367  s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
368  ffurl_closep(&s->hd);
369  goto redo;
370  } else
371  goto fail;
372  }
373  if (s->http_code == 407) {
374  if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
375  s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
376  ffurl_closep(&s->hd);
377  goto redo;
378  } else
379  goto fail;
380  }
381  if ((s->http_code == 301 || s->http_code == 302 ||
382  s->http_code == 303 || s->http_code == 307 || s->http_code == 308) &&
383  location_changed == 1) {
384  /* url moved, get next */
385  ffurl_closep(&s->hd);
386  if (redirects++ >= s->max_redirects)
387  return AVERROR(EIO);
388  /* Restart the authentication process with the new target, which
389  * might use a different auth mechanism. */
390  memset(&s->auth_state, 0, sizeof(s->auth_state));
391  attempts = 0;
392  location_changed = 0;
393  goto redo;
394  }
395  return 0;
396 
397 fail:
398  if (s->hd)
399  ffurl_closep(&s->hd);
400  if (location_changed < 0)
401  return location_changed;
402  return ff_http_averror(s->http_code, AVERROR(EIO));
403 }
405 {
406  int ret = 0;
407  HTTPContext *s = h->priv_data;
408 
409  /* flush the receive buffer when it is write only mode */
410  char buf[1024];
411  int read_ret;
412  read_ret = ffurl_read(s->hd, buf, sizeof(buf));
413  if (read_ret < 0) {
414  ret = read_ret;
415  }
416 
417  return ret;
418 }
419 
420 int ff_http_do_new_request(URLContext *h, const char *uri) {
421  return ff_http_do_new_request2(h, uri, NULL);
422 }
423 
425 {
426  HTTPContext *s = h->priv_data;
428  int ret;
429  char hostname1[1024], hostname2[1024], proto1[10], proto2[10];
430  int port1, port2;
431 
432  if (!h->prot ||
433  !(!strcmp(h->prot->name, "http") ||
434  !strcmp(h->prot->name, "https")))
435  return AVERROR(EINVAL);
436 
437  av_url_split(proto1, sizeof(proto1), NULL, 0,
438  hostname1, sizeof(hostname1), &port1,
439  NULL, 0, s->location);
440  av_url_split(proto2, sizeof(proto2), NULL, 0,
441  hostname2, sizeof(hostname2), &port2,
442  NULL, 0, uri);
443  if (strcmp(proto1, proto2) != 0) {
444  av_log(h, AV_LOG_INFO, "Cannot reuse HTTP connection for different protocol %s vs %s\n",
445  proto1, proto2);
446  return AVERROR(EINVAL);
447  }
448  if (port1 != port2 || strncmp(hostname1, hostname2, sizeof(hostname2)) != 0) {
449  av_log(h, AV_LOG_INFO, "Cannot reuse HTTP connection for different host: %s:%d != %s:%d\n",
450  hostname1, port1,
451  hostname2, port2
452  );
453  return AVERROR(EINVAL);
454  }
455 
456  if (!s->end_chunked_post) {
457  ret = http_shutdown(h, h->flags);
458  if (ret < 0)
459  return ret;
460  }
461 
462  if (s->willclose)
463  return AVERROR_EOF;
464 
465  s->end_chunked_post = 0;
466  s->chunkend = 0;
467  s->off = 0;
468  s->icy_data_read = 0;
469  av_free(s->location);
470  s->location = av_strdup(uri);
471  if (!s->location)
472  return AVERROR(ENOMEM);
473 
474  if ((ret = av_opt_set_dict(s, opts)) < 0)
475  return ret;
476 
477  av_log(s, AV_LOG_INFO, "Opening \'%s\' for %s\n", uri, h->flags & AVIO_FLAG_WRITE ? "writing" : "reading");
478  ret = http_open_cnx(h, &options);
480  return ret;
481 }
482 
483 int ff_http_averror(int status_code, int default_averror)
484 {
485  switch (status_code) {
486  case 400: return AVERROR_HTTP_BAD_REQUEST;
487  case 401: return AVERROR_HTTP_UNAUTHORIZED;
488  case 403: return AVERROR_HTTP_FORBIDDEN;
489  case 404: return AVERROR_HTTP_NOT_FOUND;
490  default: break;
491  }
492  if (status_code >= 400 && status_code <= 499)
493  return AVERROR_HTTP_OTHER_4XX;
494  else if (status_code >= 500)
496  else
497  return default_averror;
498 }
499 
500 static int http_write_reply(URLContext* h, int status_code)
501 {
502  int ret, body = 0, reply_code, message_len;
503  const char *reply_text, *content_type;
504  HTTPContext *s = h->priv_data;
505  char message[BUFFER_SIZE];
506  content_type = "text/plain";
507 
508  if (status_code < 0)
509  body = 1;
510  switch (status_code) {
512  case 400:
513  reply_code = 400;
514  reply_text = "Bad Request";
515  break;
517  case 403:
518  reply_code = 403;
519  reply_text = "Forbidden";
520  break;
522  case 404:
523  reply_code = 404;
524  reply_text = "Not Found";
525  break;
526  case 200:
527  reply_code = 200;
528  reply_text = "OK";
529  content_type = s->content_type ? s->content_type : "application/octet-stream";
530  break;
532  case 500:
533  reply_code = 500;
534  reply_text = "Internal server error";
535  break;
536  default:
537  return AVERROR(EINVAL);
538  }
539  if (body) {
540  s->chunked_post = 0;
541  message_len = snprintf(message, sizeof(message),
542  "HTTP/1.1 %03d %s\r\n"
543  "Content-Type: %s\r\n"
544  "Content-Length: %"SIZE_SPECIFIER"\r\n"
545  "%s"
546  "\r\n"
547  "%03d %s\r\n",
548  reply_code,
549  reply_text,
550  content_type,
551  strlen(reply_text) + 6, // 3 digit status code + space + \r\n
552  s->headers ? s->headers : "",
553  reply_code,
554  reply_text);
555  } else {
556  s->chunked_post = 1;
557  message_len = snprintf(message, sizeof(message),
558  "HTTP/1.1 %03d %s\r\n"
559  "Content-Type: %s\r\n"
560  "Transfer-Encoding: chunked\r\n"
561  "%s"
562  "\r\n",
563  reply_code,
564  reply_text,
565  content_type,
566  s->headers ? s->headers : "");
567  }
568  av_log(h, AV_LOG_TRACE, "HTTP reply header: \n%s----\n", message);
569  if ((ret = ffurl_write(s->hd, message, message_len)) < 0)
570  return ret;
571  return 0;
572 }
573 
575 {
576  av_assert0(error < 0);
578 }
579 
581 {
582  int ret, err, new_location;
583  HTTPContext *ch = c->priv_data;
584  URLContext *cl = ch->hd;
585  switch (ch->handshake_step) {
586  case LOWER_PROTO:
587  av_log(c, AV_LOG_TRACE, "Lower protocol\n");
588  if ((ret = ffurl_handshake(cl)) > 0)
589  return 2 + ret;
590  if (ret < 0)
591  return ret;
593  ch->is_connected_server = 1;
594  return 2;
595  case READ_HEADERS:
596  av_log(c, AV_LOG_TRACE, "Read headers\n");
597  if ((err = http_read_header(c, &new_location)) < 0) {
598  handle_http_errors(c, err);
599  return err;
600  }
602  return 1;
603  case WRITE_REPLY_HEADERS:
604  av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
605  if ((err = http_write_reply(c, ch->reply_code)) < 0)
606  return err;
607  ch->handshake_step = FINISH;
608  return 1;
609  case FINISH:
610  return 0;
611  }
612  // this should never be reached.
613  return AVERROR(EINVAL);
614 }
615 
616 static int http_listen(URLContext *h, const char *uri, int flags,
617  AVDictionary **options) {
618  HTTPContext *s = h->priv_data;
619  int ret;
620  char hostname[1024], proto[10];
621  char lower_url[100];
622  const char *lower_proto = "tcp";
623  int port;
624  av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
625  NULL, 0, uri);
626  if (!strcmp(proto, "https"))
627  lower_proto = "tls";
628  ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
629  NULL);
630  if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
631  goto fail;
632  if ((ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
633  &h->interrupt_callback, options,
634  h->protocol_whitelist, h->protocol_blacklist, h
635  )) < 0)
636  goto fail;
637  s->handshake_step = LOWER_PROTO;
638  if (s->listen == HTTP_SINGLE) { /* single client */
639  s->reply_code = 200;
640  while ((ret = http_handshake(h)) > 0);
641  }
642 fail:
643  av_dict_free(&s->chained_options);
644  return ret;
645 }
646 
647 static int http_open(URLContext *h, const char *uri, int flags,
649 {
650  HTTPContext *s = h->priv_data;
651  int ret;
652 
653  if( s->seekable == 1 )
654  h->is_streamed = 0;
655  else
656  h->is_streamed = 1;
657 
658  s->filesize = UINT64_MAX;
659  s->location = av_strdup(uri);
660  if (!s->location)
661  return AVERROR(ENOMEM);
662 
663  if (s->cookies && (ret = bind_cookies_to_host(s)) < 0)
664  goto bail_out;
665 
666  if (options)
667  av_dict_copy(&s->chained_options, *options, 0);
668 
669  if (s->headers) {
670  int len = strlen(s->headers);
671  if (len < 2 || strcmp("\r\n", s->headers + len - 2)) {
673  "No trailing CRLF found in HTTP header. Adding it.\n");
674  ret = av_reallocp(&s->headers, len + 3);
675  if (ret < 0)
676  goto bail_out;
677  s->headers[len] = '\r';
678  s->headers[len + 1] = '\n';
679  s->headers[len + 2] = '\0';
680  }
681  }
682 
683  if (s->listen) {
684  return http_listen(h, uri, flags, options);
685  }
686  ret = http_open_cnx(h, options);
687 bail_out:
688  if (ret < 0) {
689  av_dict_free(&s->chained_options);
690  av_freep(&s->host);
691  }
692  return ret;
693 }
694 
696 {
697  int ret;
698  HTTPContext *sc = s->priv_data;
699  HTTPContext *cc;
700  URLContext *sl = sc->hd;
701  URLContext *cl = NULL;
702 
703  av_assert0(sc->listen);
704  if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
705  goto fail;
706  cc = (*c)->priv_data;
707  if ((ret = ffurl_accept(sl, &cl)) < 0)
708  goto fail;
709  cc->hd = cl;
710  cc->is_multi_client = 1;
711  return 0;
712 fail:
713  if (c) {
714  ffurl_closep(c);
715  }
716  return ret;
717 }
718 
719 static int http_getc(HTTPContext *s)
720 {
721  int len;
722  if (s->buf_ptr >= s->buf_end) {
723  len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
724  if (len < 0) {
725  return len;
726  } else if (len == 0) {
727  return AVERROR_EOF;
728  } else {
729  s->buf_ptr = s->buffer;
730  s->buf_end = s->buffer + len;
731  }
732  }
733  return *s->buf_ptr++;
734 }
735 
736 static int http_get_line(HTTPContext *s, char *line, int line_size)
737 {
738  int ch;
739  char *q;
740 
741  q = line;
742  for (;;) {
743  ch = http_getc(s);
744  if (ch < 0)
745  return ch;
746  if (ch == '\n') {
747  /* process line */
748  if (q > line && q[-1] == '\r')
749  q--;
750  *q = '\0';
751 
752  return 0;
753  } else {
754  if ((q - line) < line_size - 1)
755  *q++ = ch;
756  }
757  }
758 }
759 
760 static int check_http_code(URLContext *h, int http_code, const char *end)
761 {
762  HTTPContext *s = h->priv_data;
763  /* error codes are 4xx and 5xx, but regard 401 as a success, so we
764  * don't abort until all headers have been parsed. */
765  if (http_code >= 400 && http_code < 600 &&
766  (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
767  (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
768  end += strspn(end, SPACE_CHARS);
769  av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
770  return ff_http_averror(http_code, AVERROR(EIO));
771  }
772  return 0;
773 }
774 
775 static int parse_location(HTTPContext *s, const char *p)
776 {
777  char redirected_location[MAX_URL_SIZE], *new_loc;
778  ff_make_absolute_url(redirected_location, sizeof(redirected_location),
779  s->location, p);
780  new_loc = av_strdup(redirected_location);
781  if (!new_loc)
782  return AVERROR(ENOMEM);
783  av_free(s->location);
784  s->location = new_loc;
785  return 0;
786 }
787 
788 /* "bytes $from-$to/$document_size" */
789 static void parse_content_range(URLContext *h, const char *p)
790 {
791  HTTPContext *s = h->priv_data;
792  const char *slash;
793 
794  if (!strncmp(p, "bytes ", 6)) {
795  p += 6;
796  s->off = strtoull(p, NULL, 10);
797  if ((slash = strchr(p, '/')) && strlen(slash) > 0)
798  s->filesize = strtoull(slash + 1, NULL, 10);
799  }
800  if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
801  h->is_streamed = 0; /* we _can_ in fact seek */
802 }
803 
804 static int parse_content_encoding(URLContext *h, const char *p)
805 {
806  if (!av_strncasecmp(p, "gzip", 4) ||
807  !av_strncasecmp(p, "deflate", 7)) {
808 #if CONFIG_ZLIB
809  HTTPContext *s = h->priv_data;
810 
811  s->compressed = 1;
812  inflateEnd(&s->inflate_stream);
813  if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
814  av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
815  s->inflate_stream.msg);
816  return AVERROR(ENOSYS);
817  }
818  if (zlibCompileFlags() & (1 << 17)) {
820  "Your zlib was compiled without gzip support.\n");
821  return AVERROR(ENOSYS);
822  }
823 #else
825  "Compressed (%s) content, need zlib with gzip support\n", p);
826  return AVERROR(ENOSYS);
827 #endif /* CONFIG_ZLIB */
828  } else if (!av_strncasecmp(p, "identity", 8)) {
829  // The normal, no-encoding case (although servers shouldn't include
830  // the header at all if this is the case).
831  } else {
832  av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
833  }
834  return 0;
835 }
836 
837 // Concat all Icy- header lines
838 static int parse_icy(HTTPContext *s, const char *tag, const char *p)
839 {
840  int len = 4 + strlen(p) + strlen(tag);
841  int is_first = !s->icy_metadata_headers;
842  int ret;
843 
844  av_dict_set(&s->metadata, tag, p, 0);
845 
846  if (s->icy_metadata_headers)
847  len += strlen(s->icy_metadata_headers);
848 
849  if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
850  return ret;
851 
852  if (is_first)
853  *s->icy_metadata_headers = '\0';
854 
855  av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
856 
857  return 0;
858 }
859 
860 static int parse_set_cookie_expiry_time(const char *exp_str, struct tm *buf)
861 {
862  char exp_buf[MAX_EXPIRY];
863  int i, j, exp_buf_len = MAX_EXPIRY-1;
864  char *expiry;
865 
866  // strip off any punctuation or whitespace
867  for (i = 0, j = 0; exp_str[i] != '\0' && j < exp_buf_len; i++) {
868  if ((exp_str[i] >= '0' && exp_str[i] <= '9') ||
869  (exp_str[i] >= 'A' && exp_str[i] <= 'Z') ||
870  (exp_str[i] >= 'a' && exp_str[i] <= 'z')) {
871  exp_buf[j] = exp_str[i];
872  j++;
873  }
874  }
875  exp_buf[j] = '\0';
876  expiry = exp_buf;
877 
878  // move the string beyond the day of week
879  while ((*expiry < '0' || *expiry > '9') && *expiry != '\0')
880  expiry++;
881 
882  return av_small_strptime(expiry, "%d%b%Y%H%M%S", buf) ? 0 : AVERROR(EINVAL);
883 }
884 
885 static int parse_set_cookie(const char *set_cookie, AVDictionary **dict)
886 {
887  char *param, *next_param, *cstr, *back;
888  char *saveptr = NULL;
889 
890  if (!set_cookie[0])
891  return 0;
892 
893  if (!(cstr = av_strdup(set_cookie)))
894  return AVERROR(EINVAL);
895 
896  // strip any trailing whitespace
897  back = &cstr[strlen(cstr)-1];
898  while (strchr(WHITESPACES, *back)) {
899  *back='\0';
900  if (back == cstr)
901  break;
902  back--;
903  }
904 
905  next_param = cstr;
906  while ((param = av_strtok(next_param, ";", &saveptr))) {
907  char *name, *value;
908  next_param = NULL;
909  param += strspn(param, WHITESPACES);
910  if ((name = av_strtok(param, "=", &value))) {
911  char *end = name + strlen(name);
912  while (end > name && strchr(WHITESPACES, end[-1]))
913  *--end = '\0';
914  if (av_dict_set(dict, name, value ? value : "", 0) < 0) {
915  av_free(cstr);
916  return -1;
917  }
918  }
919  }
920 
921  av_free(cstr);
922  return 0;
923 }
924 
925 static const char *cookie_domain(const AVDictionary *cookie_params)
926 {
927  const AVDictionaryEntry *e = av_dict_get(cookie_params, "domain", NULL, 0);
928  const char *domain = e ? e->value + (e->value[0] == '.') : "";
929 
930  return *domain ? domain : NULL;
931 }
932 
933 static int host_is_ip_literal(const char *host)
934 {
935  return !host[strspn(host, "0123456789.")] || strchr(host, ':');
936 }
937 
938 static int host_in_cookie_domain(const char *host, const char *domain)
939 {
940  int offset = strlen(host) - strlen(domain);
941 
942  return offset >= 0 && !av_strcasecmp(host + offset, domain) &&
943  (!offset || (host[offset - 1] == '.' && !host_is_ip_literal(host)));
944 }
945 
946 static int parse_cookie(HTTPContext *s, const char *p, const char *host,
947  AVDictionary **cookies)
948 {
949  AVDictionary *new_params = NULL;
950  AVDictionaryEntry *e, *cookie_entry;
951  char *eql, *name, *value;
952  int len;
953 
954  // ensure the cookie is parsable
955  if (parse_set_cookie(p, &new_params)) {
956  av_dict_free(&new_params);
957  return -1;
958  }
959 
960  // if there is no cookie value there is nothing to parse
961  cookie_entry = av_dict_get(new_params, "", NULL, AV_DICT_IGNORE_SUFFIX);
962  if (!cookie_entry || !cookie_entry->value) {
963  av_dict_free(&new_params);
964  return -1;
965  }
966 
967  // ensure the cookie is not expired or older than an existing value
968  if ((e = av_dict_get(new_params, "expires", NULL, 0)) && e->value) {
969  struct tm new_tm = {0};
970  if (!parse_set_cookie_expiry_time(e->value, &new_tm)) {
971  AVDictionaryEntry *e2;
972 
973  // if the cookie has already expired ignore it
974  if (av_timegm(&new_tm) < av_gettime() / 1000000) {
975  av_dict_free(&new_params);
976  return 0;
977  }
978 
979  // only replace an older cookie with the same name
980  e2 = av_dict_get(*cookies, cookie_entry->key, NULL, 0);
981  if (e2 && e2->value) {
982  AVDictionary *old_params = NULL;
983  if (!parse_set_cookie(p, &old_params)) {
984  e2 = av_dict_get(old_params, "expires", NULL, 0);
985  if (e2 && e2->value) {
986  struct tm old_tm = {0};
987  if (!parse_set_cookie_expiry_time(e->value, &old_tm)) {
988  if (av_timegm(&new_tm) < av_timegm(&old_tm)) {
989  av_dict_free(&new_params);
990  av_dict_free(&old_params);
991  return -1;
992  }
993  }
994  }
995  }
996  av_dict_free(&old_params);
997  }
998  }
999  }
1000  const char *domain = host ? cookie_domain(new_params) : NULL;
1001  int host_only = host && !domain;
1002  if (domain && !host_in_cookie_domain(host, domain)) {
1003  av_log(s, AV_LOG_WARNING, "Ignoring cookie for domain %s set by %s\n", domain, host);
1004  av_dict_free(&new_params);
1005  return 0;
1006  }
1007  av_dict_free(&new_params);
1008 
1009  // duplicate the cookie name (dict will dupe the value)
1010  if (!(eql = strchr(p, '='))) return AVERROR(EINVAL);
1011  if (!(name = av_strndup(p, eql - p))) return AVERROR(ENOMEM);
1012 
1013  // add the cookie to the dictionary
1014  len = strlen(eql);
1015  while (len && strchr(WHITESPACES, eql[len - 1]))
1016  len--;
1017  value = av_asprintf("%.*s%s%s", len, eql, host_only ? "; @hostonly=" : "", host_only ? host : "");
1018  if (!value) {
1019  av_free(name);
1020  return AVERROR(ENOMEM);
1021  }
1023 
1024  return 0;
1025 }
1026 
1027 /* Cookies without a Domain apply only to the host they are given for. */
1029 {
1030  char host[1024], *cookies, *cookie, *next, *saveptr = NULL, *bound;
1031  AVBPrint bp;
1032  int ret = 0;
1033 
1034  av_url_split(NULL, 0, NULL, 0, host, sizeof(host), NULL, NULL, 0, s->location);
1035 
1036  next = cookies = av_strdup(s->cookies);
1037  if (!cookies)
1038  return AVERROR(ENOMEM);
1039 
1041  while ((cookie = av_strtok(next, "\n", &saveptr))) {
1042  AVDictionary *params = NULL;
1043  int len = strlen(cookie), host_only;
1044 
1045  next = NULL;
1046  while (len && strchr(WHITESPACES, cookie[len - 1]))
1047  len--;
1048  if ((ret = parse_set_cookie(cookie, &params)) < 0) {
1049  av_dict_free(&params);
1050  break;
1051  }
1052  host_only = !cookie_domain(params) && !av_dict_get(params, "@hostonly", NULL, 0);
1053  av_dict_free(&params);
1054  av_bprintf(&bp, "%.*s%s%s\n", len, cookie, host_only ? "; @hostonly=" : "", host_only ? host : "");
1055  }
1056  av_free(cookies);
1057  if (ret >= 0 && !av_bprint_is_complete(&bp))
1058  ret = AVERROR(ENOMEM);
1059  if (ret < 0) {
1060  av_bprint_finalize(&bp, NULL);
1061  return ret;
1062  }
1063  if ((ret = av_bprint_finalize(&bp, &bound)) < 0)
1064  return ret;
1065  av_free(s->cookies);
1066  s->cookies = bound;
1067  return 0;
1068 }
1069 
1070 static int cookie_string(AVDictionary *dict, char **cookies)
1071 {
1072  AVDictionaryEntry *e = NULL;
1073  int len = 1;
1074 
1075  // determine how much memory is needed for the cookies string
1076  while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
1077  len += strlen(e->key) + strlen(e->value) + 1;
1078 
1079  // reallocate the cookies
1080  e = NULL;
1081  if (*cookies) av_free(*cookies);
1082  *cookies = av_malloc(len);
1083  if (!*cookies) return AVERROR(ENOMEM);
1084  *cookies[0] = '\0';
1085 
1086  // write out the cookies
1087  while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
1088  av_strlcatf(*cookies, len, "%s%s\n", e->key, e->value);
1089 
1090  return 0;
1091 }
1092 
1093 static int process_line(URLContext *h, char *line, int line_count,
1094  int *new_location)
1095 {
1096  HTTPContext *s = h->priv_data;
1097  const char *auto_method = h->flags & AVIO_FLAG_READ ? "POST" : "GET";
1098  char *tag, *p, *end, *method, *resource, *version;
1099  int ret;
1100 
1101  /* end of header */
1102  if (line[0] == '\0') {
1103  s->end_header = 1;
1104  return 0;
1105  }
1106 
1107  p = line;
1108  if (line_count == 0) {
1109  if (s->is_connected_server) {
1110  // HTTP method
1111  method = p;
1112  while (*p && !av_isspace(*p))
1113  p++;
1114  if (!av_isspace(*p))
1115  return ff_http_averror(400, AVERROR(EIO));
1116  *(p++) = '\0';
1117  av_log(h, AV_LOG_TRACE, "Received method: %s\n", method);
1118  if (s->method) {
1119  if (av_strcasecmp(s->method, method)) {
1120  av_log(h, AV_LOG_ERROR, "Received and expected HTTP method do not match. (%s expected, %s received)\n",
1121  s->method, method);
1122  return ff_http_averror(400, AVERROR(EIO));
1123  }
1124  } else {
1125  // use autodetected HTTP method to expect
1126  av_log(h, AV_LOG_TRACE, "Autodetected %s HTTP method\n", auto_method);
1127  if (av_strcasecmp(auto_method, method)) {
1128  av_log(h, AV_LOG_ERROR, "Received and autodetected HTTP method did not match "
1129  "(%s autodetected %s received)\n", auto_method, method);
1130  return ff_http_averror(400, AVERROR(EIO));
1131  }
1132  if (!(s->method = av_strdup(method)))
1133  return AVERROR(ENOMEM);
1134  }
1135 
1136  // HTTP resource
1137  while (av_isspace(*p))
1138  p++;
1139  resource = p;
1140  while (*p && !av_isspace(*p))
1141  p++;
1142  if (!av_isspace(*p))
1143  return ff_http_averror(400, AVERROR(EIO));
1144  *(p++) = '\0';
1145  av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
1146  if (!(s->resource = av_strdup(resource)))
1147  return AVERROR(ENOMEM);
1148 
1149  // HTTP version
1150  while (av_isspace(*p))
1151  p++;
1152  version = p;
1153  while (*p && !av_isspace(*p))
1154  p++;
1155  *p = '\0';
1156  if (av_strncasecmp(version, "HTTP/", 5)) {
1157  av_log(h, AV_LOG_ERROR, "Malformed HTTP version string.\n");
1158  return ff_http_averror(400, AVERROR(EIO));
1159  }
1160  av_log(h, AV_LOG_TRACE, "HTTP version string: %s\n", version);
1161  } else {
1162  if (av_strncasecmp(p, "HTTP/1.0", 8) == 0)
1163  s->willclose = 1;
1164  while (*p != '/' && *p != '\0')
1165  p++;
1166  while (*p == '/')
1167  p++;
1168  av_freep(&s->http_version);
1169  s->http_version = av_strndup(p, 3);
1170  while (!av_isspace(*p) && *p != '\0')
1171  p++;
1172  while (av_isspace(*p))
1173  p++;
1174  s->http_code = strtol(p, &end, 10);
1175 
1176  av_log(h, AV_LOG_TRACE, "http_code=%d\n", s->http_code);
1177 
1178  if ((ret = check_http_code(h, s->http_code, end)) < 0)
1179  return ret;
1180  }
1181  } else {
1182  while (*p != '\0' && *p != ':')
1183  p++;
1184  if (*p != ':')
1185  return 1;
1186 
1187  *p = '\0';
1188  tag = line;
1189  p++;
1190  while (av_isspace(*p))
1191  p++;
1192  if (!av_strcasecmp(tag, "Location")) {
1193  if ((ret = parse_location(s, p)) < 0)
1194  return ret;
1195  *new_location = 1;
1196  } else if (!av_strcasecmp(tag, "Content-Length") &&
1197  s->filesize == UINT64_MAX) {
1198  s->filesize = strtoull(p, NULL, 10);
1199  } else if (!av_strcasecmp(tag, "Content-Range")) {
1200  parse_content_range(h, p);
1201  } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
1202  !strncmp(p, "bytes", 5) &&
1203  s->seekable == -1) {
1204  h->is_streamed = 0;
1205  } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
1206  !av_strncasecmp(p, "chunked", 7)) {
1207  s->filesize = UINT64_MAX;
1208  s->chunksize = 0;
1209  } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
1210  ff_http_auth_handle_header(&s->auth_state, tag, p);
1211  } else if (!av_strcasecmp(tag, "Authentication-Info")) {
1212  ff_http_auth_handle_header(&s->auth_state, tag, p);
1213  } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
1214  ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
1215  } else if (!av_strcasecmp(tag, "Connection")) {
1216  if (!strcmp(p, "close"))
1217  s->willclose = 1;
1218  } else if (!av_strcasecmp(tag, "Server")) {
1219  if (!av_strcasecmp(p, "AkamaiGHost")) {
1220  s->is_akamai = 1;
1221  } else if (!av_strncasecmp(p, "MediaGateway", 12)) {
1222  s->is_mediagateway = 1;
1223  }
1224  } else if (!av_strcasecmp(tag, "Content-Type")) {
1225  av_free(s->mime_type);
1226  s->mime_type = av_strdup(p);
1227  } else if (!av_strcasecmp(tag, "Set-Cookie")) {
1228  if (parse_cookie(s, p, s->host, &s->cookie_dict))
1229  av_log(h, AV_LOG_WARNING, "Unable to parse '%s'\n", p);
1230  } else if (!av_strcasecmp(tag, "Icy-MetaInt")) {
1231  s->icy_metaint = strtoull(p, NULL, 10);
1232  } else if (!av_strncasecmp(tag, "Icy-", 4)) {
1233  if ((ret = parse_icy(s, tag, p)) < 0)
1234  return ret;
1235  } else if (!av_strcasecmp(tag, "Content-Encoding")) {
1236  if ((ret = parse_content_encoding(h, p)) < 0)
1237  return ret;
1238  }
1239  }
1240  return 1;
1241 }
1242 
1243 /**
1244  * Create a string containing cookie values for use as a HTTP cookie header
1245  * field value for a particular path and domain from the cookie values stored in
1246  * the HTTP protocol context. The cookie string is stored in *cookies, and may
1247  * be NULL if there are no valid cookies.
1248  *
1249  * @return a negative value if an error condition occurred, 0 otherwise
1250  */
1251 static int get_cookies(HTTPContext *s, char **cookies, const char *path)
1252 {
1253  // cookie strings will look like Set-Cookie header field values. Multiple
1254  // Set-Cookie fields will result in multiple values delimited by a newline
1255  int ret = 0;
1256  char *cookie, *set_cookies, *next;
1257  char *saveptr = NULL;
1258 
1259  // destroy any cookies in the dictionary.
1260  av_dict_free(&s->cookie_dict);
1261 
1262  if (!s->cookies)
1263  return 0;
1264 
1265  next = set_cookies = av_strdup(s->cookies);
1266  if (!next)
1267  return AVERROR(ENOMEM);
1268 
1269  *cookies = NULL;
1270  while ((cookie = av_strtok(next, "\n", &saveptr)) && !ret) {
1271  AVDictionary *cookie_params = NULL;
1272  AVDictionaryEntry *cookie_entry, *e;
1273  const char *domain, *eql;
1274  size_t len;
1275 
1276  next = NULL;
1277  // store the cookie in a dict in case it is updated in the response
1278  if (parse_cookie(s, cookie, NULL, &s->cookie_dict))
1279  av_log(s, AV_LOG_WARNING, "Unable to parse '%s'\n", cookie);
1280 
1281  // continue on to the next cookie if this one cannot be parsed
1282  if (parse_set_cookie(cookie, &cookie_params))
1283  goto skip_cookie;
1284 
1285  // if the cookie has no value, skip it
1286  cookie_entry = av_dict_get(cookie_params, "", NULL, AV_DICT_IGNORE_SUFFIX);
1287  eql = strchr(cookie, '=');
1288  if (!cookie_entry || !eql || eql == cookie + strspn(cookie, WHITESPACES) ||
1289  memchr(cookie, ';', eql - cookie))
1290  goto skip_cookie;
1291 
1292  for (e = cookie_entry; (e = av_dict_get(cookie_params, "", e, AV_DICT_IGNORE_SUFFIX)); )
1293  if (!av_strcasecmp(e->key, "secure") && !av_stristart(s->location, "https:", NULL))
1294  goto skip_cookie;
1295 
1296  // if the cookie has expired, don't add it
1297  if ((e = av_dict_get(cookie_params, "expires", NULL, 0)) && e->value) {
1298  struct tm tm_buf = {0};
1299  if (!parse_set_cookie_expiry_time(e->value, &tm_buf)) {
1300  if (av_timegm(&tm_buf) < av_gettime() / 1000000)
1301  goto skip_cookie;
1302  }
1303  }
1304 
1305  // if no domain in the cookie assume it appied to this request
1306  domain = cookie_domain(cookie_params);
1307  if (domain && !host_in_cookie_domain(s->host, domain))
1308  goto skip_cookie;
1309 
1310  if ((e = av_dict_get(cookie_params, "@hostonly", NULL, 0)) && av_strcasecmp(e->value, s->host))
1311  goto skip_cookie;
1312 
1313  // ensure this cookie matches the path
1314  e = av_dict_get(cookie_params, "path", NULL, 0);
1315  if (!e)
1316  goto skip_cookie;
1317  len = strlen(e->value);
1318  if (av_strncasecmp(path, e->value, len) ||
1319  (len && path[len] && path[len] != '/' && path[len] != '?' &&
1320  e->value[len - 1] != '/'))
1321  goto skip_cookie;
1322 
1323  // cookie parameters match, so copy the value
1324  if (!*cookies) {
1325  *cookies = av_asprintf("%s=%s", cookie_entry->key, cookie_entry->value);
1326  } else {
1327  char *tmp = *cookies;
1328  *cookies = av_asprintf("%s; %s=%s", tmp, cookie_entry->key, cookie_entry->value);
1329  av_free(tmp);
1330  }
1331  if (!*cookies)
1332  ret = AVERROR(ENOMEM);
1333 
1334  skip_cookie:
1335  av_dict_free(&cookie_params);
1336  }
1337 
1338  av_free(set_cookies);
1339 
1340  return ret;
1341 }
1342 
1343 static inline int has_header(const char *str, const char *header)
1344 {
1345  /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
1346  if (!str)
1347  return 0;
1348  return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
1349 }
1350 
1351 static int http_read_header(URLContext *h, int *new_location)
1352 {
1353  HTTPContext *s = h->priv_data;
1354  char line[MAX_URL_SIZE];
1355  int err = 0;
1356 
1357  s->chunksize = UINT64_MAX;
1358 
1359  for (;;) {
1360  if ((err = http_get_line(s, line, sizeof(line))) < 0)
1361  return err;
1362 
1363  av_log(h, AV_LOG_TRACE, "header='%s'\n", line);
1364 
1365  err = process_line(h, line, s->line_count, new_location);
1366  if (err < 0)
1367  return err;
1368  if (err == 0)
1369  break;
1370  s->line_count++;
1371  }
1372 
1373  if (s->seekable == -1 && s->is_mediagateway && s->filesize == 2000000000)
1374  h->is_streamed = 1; /* we can in fact _not_ seek */
1375 
1376  // add any new cookies into the existing cookie string
1377  cookie_string(s->cookie_dict, &s->cookies);
1378  av_dict_free(&s->cookie_dict);
1379 
1380  return err;
1381 }
1382 
1383 /**
1384  * Escape unsafe characters in path in order to pass them safely to the HTTP
1385  * request. Insipred by the algorithm in GNU wget:
1386  * - escape "%" characters not followed by two hex digits
1387  * - escape all "unsafe" characters except which are also "reserved"
1388  * - pass through everything else
1389  */
1390 static void bprint_escaped_path(AVBPrint *bp, const char *path)
1391 {
1392 #define NEEDS_ESCAPE(ch) \
1393  ((ch) <= ' ' || (ch) >= '\x7f' || \
1394  (ch) == '"' || (ch) == '%' || (ch) == '<' || (ch) == '>' || (ch) == '\\' || \
1395  (ch) == '^' || (ch) == '`' || (ch) == '{' || (ch) == '}' || (ch) == '|')
1396  while (*path) {
1397  char buf[1024];
1398  char *q = buf;
1399  while (*path && q - buf < sizeof(buf) - 4) {
1400  if (path[0] == '%' && av_isxdigit(path[1]) && av_isxdigit(path[2])) {
1401  *q++ = *path++;
1402  *q++ = *path++;
1403  *q++ = *path++;
1404  } else if (NEEDS_ESCAPE(*path)) {
1405  q += snprintf(q, 4, "%%%02X", (uint8_t)*path++);
1406  } else {
1407  *q++ = *path++;
1408  }
1409  }
1410  av_bprint_append_data(bp, buf, q - buf);
1411  }
1412 }
1413 
1414 static int http_connect(URLContext *h, const char *path, const char *local_path,
1415  const char *hoststr, const char *auth,
1416  const char *proxyauth, int *new_location)
1417 {
1418  HTTPContext *s = h->priv_data;
1419  int post, err;
1420  AVBPrint request;
1421  char *authstr = NULL, *proxyauthstr = NULL;
1422  uint64_t off = s->off;
1423  const char *method;
1424  int send_expect_100 = 0;
1425 
1426  av_bprint_init_for_buffer(&request, s->buffer, sizeof(s->buffer));
1427 
1428  /* send http header */
1429  post = h->flags & AVIO_FLAG_WRITE;
1430 
1431  if (s->post_data) {
1432  /* force POST method and disable chunked encoding when
1433  * custom HTTP post data is set */
1434  post = 1;
1435  s->chunked_post = 0;
1436  }
1437 
1438  if (s->method)
1439  method = s->method;
1440  else
1441  method = post ? "POST" : "GET";
1442 
1443  authstr = ff_http_auth_create_response(&s->auth_state, auth,
1444  local_path, method);
1445  proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
1446  local_path, method);
1447 
1448  if (post && !s->post_data) {
1449  if (s->send_expect_100 != -1) {
1450  send_expect_100 = s->send_expect_100;
1451  } else {
1452  send_expect_100 = 0;
1453  /* The user has supplied authentication but we don't know the auth type,
1454  * send Expect: 100-continue to get the 401 response including the
1455  * WWW-Authenticate header, or an 100 continue if no auth actually
1456  * is needed. */
1457  if (auth && *auth &&
1458  s->auth_state.auth_type == HTTP_AUTH_NONE &&
1459  s->http_code != 401)
1460  send_expect_100 = 1;
1461  }
1462  }
1463 
1464  av_bprintf(&request, "%s ", method);
1465  bprint_escaped_path(&request, path);
1466  av_bprintf(&request, " HTTP/1.1\r\n");
1467 
1468  if (post && s->chunked_post)
1469  av_bprintf(&request, "Transfer-Encoding: chunked\r\n");
1470  /* set default headers if needed */
1471  if (!has_header(s->headers, "\r\nUser-Agent: "))
1472  av_bprintf(&request, "User-Agent: %s\r\n", s->user_agent);
1473  if (s->referer) {
1474  /* set default headers if needed */
1475  if (!has_header(s->headers, "\r\nReferer: "))
1476  av_bprintf(&request, "Referer: %s\r\n", s->referer);
1477  }
1478  if (!has_header(s->headers, "\r\nAccept: "))
1479  av_bprintf(&request, "Accept: */*\r\n");
1480  // Note: we send this on purpose even when s->off is 0 when we're probing,
1481  // since it allows us to detect more reliably if a (non-conforming)
1482  // server supports seeking by analysing the reply headers.
1483  if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable == -1)) {
1484  av_bprintf(&request, "Range: bytes=%"PRIu64"-", s->off);
1485  if (s->end_off)
1486  av_bprintf(&request, "%"PRId64, s->end_off - 1);
1487  av_bprintf(&request, "\r\n");
1488  }
1489  if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
1490  av_bprintf(&request, "Expect: 100-continue\r\n");
1491 
1492  if (!has_header(s->headers, "\r\nConnection: "))
1493  av_bprintf(&request, "Connection: %s\r\n", s->multiple_requests ? "keep-alive" : "close");
1494 
1495  if (!has_header(s->headers, "\r\nHost: "))
1496  av_bprintf(&request, "Host: %s\r\n", hoststr);
1497  if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
1498  av_bprintf(&request, "Content-Length: %d\r\n", s->post_datalen);
1499 
1500  if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
1501  av_bprintf(&request, "Content-Type: %s\r\n", s->content_type);
1502  if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
1503  char *cookies = NULL;
1504  if (!get_cookies(s, &cookies, local_path) && cookies) {
1505  av_bprintf(&request, "Cookie: %s\r\n", cookies);
1506  av_free(cookies);
1507  }
1508  }
1509  if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
1510  av_bprintf(&request, "Icy-MetaData: 1\r\n");
1511 
1512  /* now add in custom headers */
1513  if (s->headers)
1514  av_bprintf(&request, "%s", s->headers);
1515 
1516  if (authstr)
1517  av_bprintf(&request, "%s", authstr);
1518  if (proxyauthstr)
1519  av_bprintf(&request, "Proxy-%s", proxyauthstr);
1520  av_bprintf(&request, "\r\n");
1521 
1522  av_log(h, AV_LOG_DEBUG, "request: %s\n", request.str);
1523 
1524  if (!av_bprint_is_complete(&request)) {
1525  av_log(h, AV_LOG_ERROR, "overlong headers\n");
1526  err = AVERROR(EINVAL);
1527  goto done;
1528  }
1529 
1530  if ((err = ffurl_write(s->hd, request.str, request.len)) < 0)
1531  goto done;
1532 
1533  if (s->post_data)
1534  if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
1535  goto done;
1536 
1537  /* init input buffer */
1538  s->buf_ptr = s->buffer;
1539  s->buf_end = s->buffer;
1540  s->line_count = 0;
1541  s->off = 0;
1542  s->icy_data_read = 0;
1543  s->filesize = UINT64_MAX;
1544  s->willclose = 0;
1545  s->end_chunked_post = 0;
1546  s->end_header = 0;
1547 #if CONFIG_ZLIB
1548  s->compressed = 0;
1549 #endif
1550  if (post && !s->post_data && !send_expect_100) {
1551  /* Pretend that it did work. We didn't read any header yet, since
1552  * we've still to send the POST data, but the code calling this
1553  * function will check http_code after we return. */
1554  s->http_code = 200;
1555  err = 0;
1556  goto done;
1557  }
1558 
1559  /* wait for header */
1560  err = http_read_header(h, new_location);
1561  if (err < 0)
1562  goto done;
1563 
1564  if (*new_location)
1565  s->off = off;
1566 
1567  if (off != s->off) {
1569  "Unexpected offset: expected %"PRIu64", got %"PRIu64"\n",
1570  off, s->off);
1571  err = AVERROR(EIO);
1572  goto done;
1573  }
1574 
1575  err = 0;
1576 done:
1577  av_freep(&authstr);
1578  av_freep(&proxyauthstr);
1579  return err;
1580 }
1581 
1582 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
1583 {
1584  HTTPContext *s = h->priv_data;
1585  int len;
1586 
1587  if (!s->hd)
1588  return AVERROR(EIO);
1589 
1590  if (s->chunksize != UINT64_MAX) {
1591  if (s->chunkend) {
1592  return AVERROR_EOF;
1593  }
1594  if (!s->chunksize) {
1595  char line[32];
1596  int err;
1597 
1598  do {
1599  if ((err = http_get_line(s, line, sizeof(line))) < 0)
1600  return err;
1601  } while (!*line); /* skip CR LF from last chunk */
1602 
1603  s->chunksize = strtoull(line, NULL, 16);
1604 
1606  "Chunked encoding data size: %"PRIu64"\n",
1607  s->chunksize);
1608 
1609  if (!s->chunksize && s->multiple_requests) {
1610  http_get_line(s, line, sizeof(line)); // read empty chunk
1611  s->chunkend = 1;
1612  return 0;
1613  }
1614  else if (!s->chunksize) {
1615  av_log(h, AV_LOG_DEBUG, "Last chunk received, closing conn\n");
1616  ffurl_closep(&s->hd);
1617  return 0;
1618  }
1619  else if (s->chunksize == UINT64_MAX) {
1620  av_log(h, AV_LOG_ERROR, "Invalid chunk size %"PRIu64"\n",
1621  s->chunksize);
1622  return AVERROR(EINVAL);
1623  }
1624  }
1625  size = FFMIN(size, s->chunksize);
1626  }
1627 
1628  /* read bytes from input buffer first */
1629  len = s->buf_end - s->buf_ptr;
1630  if (len > 0) {
1631  if (len > size)
1632  len = size;
1633  memcpy(buf, s->buf_ptr, len);
1634  s->buf_ptr += len;
1635  } else {
1636  uint64_t target_end = s->end_off ? s->end_off : s->filesize;
1637  if ((!s->willclose || s->chunksize == UINT64_MAX) && s->off >= target_end)
1638  return AVERROR_EOF;
1639  len = ffurl_read(s->hd, buf, size);
1640  if ((!len || len == AVERROR_EOF) &&
1641  (!s->willclose || s->chunksize == UINT64_MAX) && s->off < target_end) {
1643  "Stream ends prematurely at %"PRIu64", should be %"PRIu64"\n",
1644  s->off, target_end
1645  );
1646  return AVERROR(EIO);
1647  }
1648  }
1649  if (len > 0) {
1650  s->off += len;
1651  if (s->chunksize > 0 && s->chunksize != UINT64_MAX) {
1652  av_assert0(s->chunksize >= len);
1653  s->chunksize -= len;
1654  }
1655  }
1656  return len;
1657 }
1658 
1659 #if CONFIG_ZLIB
1660 #define DECOMPRESS_BUF_SIZE (256 * 1024)
1661 static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
1662 {
1663  HTTPContext *s = h->priv_data;
1664  int ret;
1665 
1666  if (!s->inflate_buffer) {
1667  s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
1668  if (!s->inflate_buffer)
1669  return AVERROR(ENOMEM);
1670  }
1671 
1672  if (s->inflate_stream.avail_in == 0) {
1673  int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
1674  if (read <= 0)
1675  return read;
1676  s->inflate_stream.next_in = s->inflate_buffer;
1677  s->inflate_stream.avail_in = read;
1678  }
1679 
1680  s->inflate_stream.avail_out = size;
1681  s->inflate_stream.next_out = buf;
1682 
1683  ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
1684  if (ret != Z_OK && ret != Z_STREAM_END)
1685  av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n",
1686  ret, s->inflate_stream.msg);
1687 
1688  return size - s->inflate_stream.avail_out;
1689 }
1690 #endif /* CONFIG_ZLIB */
1691 
1692 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect);
1693 
1694 static int http_read_stream(URLContext *h, uint8_t *buf, int size)
1695 {
1696  HTTPContext *s = h->priv_data;
1697  int err, new_location, read_ret;
1698  int64_t seek_ret;
1699  int reconnect_delay = 0;
1700 
1701  if (!s->hd)
1702  return AVERROR_EOF;
1703 
1704  if (s->end_chunked_post && !s->end_header) {
1705  err = http_read_header(h, &new_location);
1706  if (err < 0)
1707  return err;
1708  }
1709 
1710 #if CONFIG_ZLIB
1711  if (s->compressed)
1712  return http_buf_read_compressed(h, buf, size);
1713 #endif /* CONFIG_ZLIB */
1714  read_ret = http_buf_read(h, buf, size);
1715  while (read_ret < 0) {
1716  uint64_t target = h->is_streamed ? 0 : s->off;
1717 
1718  if (read_ret == AVERROR_EXIT)
1719  break;
1720 
1721  if (h->is_streamed && !s->reconnect_streamed)
1722  break;
1723 
1724  if (!(s->reconnect && s->filesize > 0 && s->off < s->filesize) &&
1725  !(s->reconnect_at_eof && read_ret == AVERROR_EOF))
1726  break;
1727 
1728  if (reconnect_delay > s->reconnect_delay_max)
1729  return AVERROR(EIO);
1730 
1731  av_log(h, AV_LOG_WARNING, "Will reconnect at %"PRIu64" in %d second(s), error=%s.\n", s->off, reconnect_delay, av_err2str(read_ret));
1732  err = ff_network_sleep_interruptible(1000U*1000*reconnect_delay, &h->interrupt_callback);
1733  if (err != AVERROR(ETIMEDOUT))
1734  return err;
1735  reconnect_delay = 1 + 2*reconnect_delay;
1736  seek_ret = http_seek_internal(h, target, SEEK_SET, 1);
1737  if (seek_ret >= 0 && seek_ret != target) {
1738  av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRIu64".\n", target);
1739  return read_ret;
1740  }
1741 
1742  read_ret = http_buf_read(h, buf, size);
1743  }
1744 
1745  return read_ret;
1746 }
1747 
1748 // Like http_read_stream(), but no short reads.
1749 // Assumes partial reads are an error.
1751 {
1752  int pos = 0;
1753  while (pos < size) {
1754  int len = http_read_stream(h, buf + pos, size - pos);
1755  if (len < 0)
1756  return len;
1757  pos += len;
1758  }
1759  return pos;
1760 }
1761 
1762 static void update_metadata(URLContext *h, char *data)
1763 {
1764  char *key;
1765  char *val;
1766  char *end;
1767  char *next = data;
1768  HTTPContext *s = h->priv_data;
1769 
1770  while (*next) {
1771  key = next;
1772  val = strstr(key, "='");
1773  if (!val)
1774  break;
1775  end = strstr(val, "';");
1776  if (!end)
1777  break;
1778 
1779  *val = '\0';
1780  *end = '\0';
1781  val += 2;
1782 
1783  av_dict_set(&s->metadata, key, val, 0);
1784  av_log(h, AV_LOG_VERBOSE, "Metadata update for %s: %s\n", key, val);
1785 
1786  next = end + 2;
1787  }
1788 }
1789 
1790 static int store_icy(URLContext *h, int size)
1791 {
1792  HTTPContext *s = h->priv_data;
1793  /* until next metadata packet */
1794  uint64_t remaining;
1795 
1796  if (s->icy_metaint < s->icy_data_read)
1797  return AVERROR_INVALIDDATA;
1798  remaining = s->icy_metaint - s->icy_data_read;
1799 
1800  if (!remaining) {
1801  /* The metadata packet is variable sized. It has a 1 byte header
1802  * which sets the length of the packet (divided by 16). If it's 0,
1803  * the metadata doesn't change. After the packet, icy_metaint bytes
1804  * of normal data follows. */
1805  uint8_t ch;
1806  int len = http_read_stream_all(h, &ch, 1);
1807  if (len < 0)
1808  return len;
1809  if (ch > 0) {
1810  char data[255 * 16 + 1];
1811  int ret;
1812  len = ch * 16;
1813  ret = http_read_stream_all(h, data, len);
1814  if (ret < 0)
1815  return ret;
1816  data[len] = 0;
1817  if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
1818  return ret;
1820  }
1821  s->icy_data_read = 0;
1822  remaining = s->icy_metaint;
1823  }
1824 
1825  return FFMIN(size, remaining);
1826 }
1827 
1828 static int http_read(URLContext *h, uint8_t *buf, int size)
1829 {
1830  HTTPContext *s = h->priv_data;
1831 
1832  if (s->icy_metaint > 0) {
1833  size = store_icy(h, size);
1834  if (size < 0)
1835  return size;
1836  }
1837 
1838  size = http_read_stream(h, buf, size);
1839  if (size > 0)
1840  s->icy_data_read += size;
1841  return size;
1842 }
1843 
1844 /* used only when posting data */
1845 static int http_write(URLContext *h, const uint8_t *buf, int size)
1846 {
1847  char temp[11] = ""; /* 32-bit hex + CRLF + nul */
1848  int ret;
1849  char crlf[] = "\r\n";
1850  HTTPContext *s = h->priv_data;
1851 
1852  if (!s->chunked_post) {
1853  /* non-chunked data is sent without any special encoding */
1854  return ffurl_write(s->hd, buf, size);
1855  }
1856 
1857  /* silently ignore zero-size data since chunk encoding that would
1858  * signal EOF */
1859  if (size > 0) {
1860  /* upload data using chunked encoding */
1861  snprintf(temp, sizeof(temp), "%x\r\n", size);
1862 
1863  if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
1864  (ret = ffurl_write(s->hd, buf, size)) < 0 ||
1865  (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
1866  return ret;
1867  }
1868  return size;
1869 }
1870 
1871 static int http_shutdown(URLContext *h, int flags)
1872 {
1873  int ret = 0;
1874  char footer[] = "0\r\n\r\n";
1875  HTTPContext *s = h->priv_data;
1876 
1877  /* signal end of chunked encoding if used */
1878  if (((flags & AVIO_FLAG_WRITE) && s->chunked_post) ||
1879  ((flags & AVIO_FLAG_READ) && s->chunked_post && s->listen)) {
1880  ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
1881  ret = ret > 0 ? 0 : ret;
1882  /* flush the receive buffer when it is write only mode */
1883  if (!(flags & AVIO_FLAG_READ)) {
1884  char buf[1024];
1885  int read_ret;
1886  s->hd->flags |= AVIO_FLAG_NONBLOCK;
1887  read_ret = ffurl_read(s->hd, buf, sizeof(buf));
1888  s->hd->flags &= ~AVIO_FLAG_NONBLOCK;
1889  if (read_ret < 0 && read_ret != AVERROR(EAGAIN)) {
1890  av_log(h, AV_LOG_ERROR, "URL read error: %s\n", av_err2str(read_ret));
1891  ret = read_ret;
1892  }
1893  }
1894  s->end_chunked_post = 1;
1895  }
1896 
1897  return ret;
1898 }
1899 
1901 {
1902  int ret = 0;
1903  HTTPContext *s = h->priv_data;
1904 
1905 #if CONFIG_ZLIB
1906  inflateEnd(&s->inflate_stream);
1907  av_freep(&s->inflate_buffer);
1908 #endif /* CONFIG_ZLIB */
1909 
1910  if (s->hd && !s->end_chunked_post)
1911  /* Close the write direction by sending the end of chunked encoding. */
1912  ret = http_shutdown(h, h->flags);
1913 
1914  if (s->hd)
1915  ffurl_closep(&s->hd);
1916  av_dict_free(&s->chained_options);
1917  av_freep(&s->host);
1918  return ret;
1919 }
1920 
1921 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
1922 {
1923  HTTPContext *s = h->priv_data;
1924  URLContext *old_hd = s->hd;
1925  uint64_t old_off = s->off;
1926  uint8_t old_buf[BUFFER_SIZE];
1927  int old_buf_size, ret;
1929 
1930  if (whence == AVSEEK_SIZE)
1931  return s->filesize;
1932  else if (!force_reconnect &&
1933  ((whence == SEEK_CUR && off == 0) ||
1934  (whence == SEEK_SET && off == s->off)))
1935  return s->off;
1936  else if ((s->filesize == UINT64_MAX && whence == SEEK_END))
1937  return AVERROR(ENOSYS);
1938 
1939  if (whence == SEEK_CUR)
1940  off += s->off;
1941  else if (whence == SEEK_END)
1942  off += s->filesize;
1943  else if (whence != SEEK_SET)
1944  return AVERROR(EINVAL);
1945  if (off < 0)
1946  return AVERROR(EINVAL);
1947  s->off = off;
1948 
1949  if (s->off && h->is_streamed)
1950  return AVERROR(ENOSYS);
1951 
1952  /* do not try to make a new connection if seeking past the end of the file */
1953  if (s->end_off || s->filesize != UINT64_MAX) {
1954  uint64_t end_pos = s->end_off ? s->end_off : s->filesize;
1955  if (s->off >= end_pos)
1956  return s->off;
1957  }
1958 
1959  /* we save the old context in case the seek fails */
1960  old_buf_size = s->buf_end - s->buf_ptr;
1961  memcpy(old_buf, s->buf_ptr, old_buf_size);
1962  s->hd = NULL;
1963 
1964  /* if it fails, continue on old connection */
1965  if ((ret = http_open_cnx(h, &options)) < 0) {
1967  memcpy(s->buffer, old_buf, old_buf_size);
1968  s->buf_ptr = s->buffer;
1969  s->buf_end = s->buffer + old_buf_size;
1970  s->hd = old_hd;
1971  s->off = old_off;
1972  return ret;
1973  }
1975  ffurl_close(old_hd);
1976  return off;
1977 }
1978 
1979 static int64_t http_seek(URLContext *h, int64_t off, int whence)
1980 {
1981  return http_seek_internal(h, off, whence, 0);
1982 }
1983 
1985 {
1986  HTTPContext *s = h->priv_data;
1987  return ffurl_get_file_handle(s->hd);
1988 }
1989 
1991 {
1992  HTTPContext *s = h->priv_data;
1993  return ffurl_get_short_seek(s->hd);
1994 }
1995 
1996 #define HTTP_CLASS(flavor) \
1997 static const AVClass flavor ## _context_class = { \
1998  .class_name = # flavor, \
1999  .item_name = av_default_item_name, \
2000  .option = options, \
2001  .version = LIBAVUTIL_VERSION_INT, \
2002 }
2003 
2004 #if CONFIG_HTTP_PROTOCOL
2005 HTTP_CLASS(http);
2006 
2007 const URLProtocol ff_http_protocol = {
2008  .name = "http",
2009  .url_open2 = http_open,
2010  .url_accept = http_accept,
2011  .url_handshake = http_handshake,
2012  .url_read = http_read,
2013  .url_write = http_write,
2014  .url_seek = http_seek,
2015  .url_close = http_close,
2016  .url_get_file_handle = http_get_file_handle,
2017  .url_get_short_seek = http_get_short_seek,
2018  .url_shutdown = http_shutdown,
2019  .priv_data_size = sizeof(HTTPContext),
2020  .priv_data_class = &http_context_class,
2022  .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy,data"
2023 };
2024 #endif /* CONFIG_HTTP_PROTOCOL */
2025 
2026 #if CONFIG_HTTPS_PROTOCOL
2027 HTTP_CLASS(https);
2028 
2030  .name = "https",
2031  .url_open2 = http_open,
2032  .url_read = http_read,
2033  .url_write = http_write,
2034  .url_seek = http_seek,
2035  .url_close = http_close,
2036  .url_get_file_handle = http_get_file_handle,
2037  .url_get_short_seek = http_get_short_seek,
2038  .url_shutdown = http_shutdown,
2039  .priv_data_size = sizeof(HTTPContext),
2040  .priv_data_class = &https_context_class,
2042  .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy"
2043 };
2044 #endif /* CONFIG_HTTPS_PROTOCOL */
2045 
2046 #if CONFIG_HTTPPROXY_PROTOCOL
2047 static int http_proxy_close(URLContext *h)
2048 {
2049  HTTPContext *s = h->priv_data;
2050  if (s->hd)
2051  ffurl_closep(&s->hd);
2052  return 0;
2053 }
2054 
2055 static int http_proxy_open(URLContext *h, const char *uri, int flags)
2056 {
2057  HTTPContext *s = h->priv_data;
2058  char hostname[1024], hoststr[1024];
2059  char auth[1024], pathbuf[1024], *path;
2060  char lower_url[100];
2061  int port, ret = 0, attempts = 0;
2062  HTTPAuthType cur_auth_type;
2063  char *authstr;
2064  int new_loc;
2065 
2066  if( s->seekable == 1 )
2067  h->is_streamed = 0;
2068  else
2069  h->is_streamed = 1;
2070 
2071  av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
2072  pathbuf, sizeof(pathbuf), uri);
2073  ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
2074  path = pathbuf;
2075  if (*path == '/')
2076  path++;
2077  for (const char *p = path; *p; p++)
2078  if ((uint8_t)*p <= ' ' || *p == 0x7f)
2079  return AVERROR(EINVAL);
2080 
2081  ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
2082  NULL);
2083 redo:
2084  ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
2085  &h->interrupt_callback, NULL,
2086  h->protocol_whitelist, h->protocol_blacklist, h);
2087  if (ret < 0)
2088  return ret;
2089 
2090  authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
2091  path, "CONNECT");
2092  snprintf(s->buffer, sizeof(s->buffer),
2093  "CONNECT %s HTTP/1.1\r\n"
2094  "Host: %s\r\n"
2095  "Connection: close\r\n"
2096  "%s%s"
2097  "\r\n",
2098  path,
2099  hoststr,
2100  authstr ? "Proxy-" : "", authstr ? authstr : "");
2101  av_freep(&authstr);
2102 
2103  if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
2104  goto fail;
2105 
2106  s->buf_ptr = s->buffer;
2107  s->buf_end = s->buffer;
2108  s->line_count = 0;
2109  s->filesize = UINT64_MAX;
2110  cur_auth_type = s->proxy_auth_state.auth_type;
2111 
2112  /* Note: This uses buffering, potentially reading more than the
2113  * HTTP header. If tunneling a protocol where the server starts
2114  * the conversation, we might buffer part of that here, too.
2115  * Reading that requires using the proper ffurl_read() function
2116  * on this URLContext, not using the fd directly (as the tls
2117  * protocol does). This shouldn't be an issue for tls though,
2118  * since the client starts the conversation there, so there
2119  * is no extra data that we might buffer up here.
2120  */
2121  ret = http_read_header(h, &new_loc);
2122  if (ret < 0)
2123  goto fail;
2124 
2125  attempts++;
2126  if (s->http_code == 407 &&
2127  (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
2128  s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
2129  ffurl_closep(&s->hd);
2130  goto redo;
2131  }
2132 
2133  if (s->http_code < 400)
2134  return 0;
2135  ret = ff_http_averror(s->http_code, AVERROR(EIO));
2136 
2137 fail:
2138  http_proxy_close(h);
2139  return ret;
2140 }
2141 
2142 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
2143 {
2144  HTTPContext *s = h->priv_data;
2145  return ffurl_write(s->hd, buf, size);
2146 }
2147 
2149  .name = "httpproxy",
2150  .url_open = http_proxy_open,
2151  .url_read = http_buf_read,
2152  .url_write = http_proxy_write,
2153  .url_close = http_proxy_close,
2154  .url_get_file_handle = http_get_file_handle,
2155  .priv_data_size = sizeof(HTTPContext),
2157 };
2158 #endif /* CONFIG_HTTPPROXY_PROTOCOL */
static double val(void *priv, double ch)
Definition: aeval.c:76
static double bound(const double threshold, const double val)
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Main libavformat public API header.
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition: avio.c:237
int ffurl_alloc(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Create a URLContext for accessing to the resource indicated by url, but do not initiate the connectio...
Definition: avio.c:296
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf.
Definition: avio.c:404
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition: avio.c:309
int ffurl_get_short_seek(URLContext *h)
Return the current short seek threshold value for this URL.
Definition: avio.c:647
int ffurl_accept(URLContext *s, URLContext **c)
Accept an URLContext c on an URLContext s.
Definition: avio.c:229
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:441
int ffurl_close(URLContext *h)
Definition: avio.c:464
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
Definition: avio.c:418
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:623
#define AVIO_FLAG_READ
read-only
Definition: avio.h:674
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:531
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:675
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:676
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition: avio.h:693
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:101
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:245
#define AV_BPRINT_SIZE_UNLIMITED
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
double value
Definition: eval.c:100
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1666
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_BINARY
offset must point to a pointer immediately followed by an int for the length
Definition: opt.h:231
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_DICT
Definition: opt.h:232
@ AV_OPT_TYPE_BOOL
Definition: opt.h:242
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:4798
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:199
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:213
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:74
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
#define AV_DICT_DONT_STRDUP_KEY
Take ownership of a key that's been allocated with av_malloc() or another memory allocation function.
Definition: dict.h:72
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it.
Definition: dict.c:143
#define AVERROR_HTTP_FORBIDDEN
Definition: error.h:78
#define AVERROR_HTTP_BAD_REQUEST
Definition: error.h:76
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
#define AVERROR_HTTP_SERVER_ERROR
Definition: error.h:81
#define AVERROR_HTTP_OTHER_4XX
Definition: error.h:80
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
#define AVERROR_HTTP_UNAUTHORIZED
Definition: error.h:77
#define AVERROR(e)
Definition: error.h:43
#define AVERROR_HTTP_NOT_FOUND
Definition: error.h:79
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:161
char * av_strndup(const char *s, size_t len)
Duplicate a substring of a string.
Definition: mem.c:265
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition: avstring.c:186
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle.
Definition: avstring.c:56
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:215
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:227
int av_match_list(const char *name, const char *list, char separator)
Check if a name is in a list.
Definition: avstring.c:455
int av_stristart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str independent of case.
Definition: avstring.c:45
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:225
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:475
static int http_handshake(URLContext *c)
Definition: http.c:580
#define MAX_EXPIRY
Definition: http.c:53
HandshakeState
Definition: http.c:55
@ FINISH
Definition: http.c:59
@ LOWER_PROTO
Definition: http.c:56
@ READ_HEADERS
Definition: http.c:57
@ WRITE_REPLY_HEADERS
Definition: http.c:58
#define E
Definition: http.c:135
static int http_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: http.c:647
static int cookie_string(AVDictionary *dict, char **cookies)
Definition: http.c:1070
static int store_icy(URLContext *h, int size)
Definition: http.c:1790
static int parse_content_encoding(URLContext *h, const char *p)
Definition: http.c:804
static int host_is_ip_literal(const char *host)
Definition: http.c:933
static int http_shutdown(URLContext *h, int flags)
Definition: http.c:1871
int ff_http_do_new_request2(URLContext *h, const char *uri, AVDictionary **opts)
Send a new HTTP request, reusing the old connection.
Definition: http.c:424
static int parse_set_cookie(const char *set_cookie, AVDictionary **dict)
Definition: http.c:885
static int http_read(URLContext *h, uint8_t *buf, int size)
Definition: http.c:1828
static int http_getc(HTTPContext *s)
Definition: http.c:719
static const AVOption options[]
Definition: http.c:138
static int get_cookies(HTTPContext *s, char **cookies, const char *path)
Create a string containing cookie values for use as a HTTP cookie header field value for a particular...
Definition: http.c:1251
static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
Definition: http.c:196
static void parse_content_range(URLContext *h, const char *p)
Definition: http.c:789
int ff_http_get_shutdown_status(URLContext *h)
Get the HTTP shutdown response status, be used after http_shutdown.
Definition: http.c:404
static void update_metadata(URLContext *h, char *data)
Definition: http.c:1762
static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
Definition: http.c:1921
static int http_get_line(HTTPContext *s, char *line, int line_size)
Definition: http.c:736
int ff_http_averror(int status_code, int default_averror)
Definition: http.c:483
static int http_buf_read(URLContext *h, uint8_t *buf, int size)
Definition: http.c:1582
static int parse_icy(HTTPContext *s, const char *tag, const char *p)
Definition: http.c:838
static int has_header(const char *str, const char *header)
Definition: http.c:1343
static int http_listen(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: http.c:616
static int process_line(URLContext *h, char *line, int line_count, int *new_location)
Definition: http.c:1093
#define BUFFER_SIZE
Definition: http.c:49
static int check_http_code(URLContext *h, int http_code, const char *end)
Definition: http.c:760
static int host_in_cookie_domain(const char *host, const char *domain)
Definition: http.c:938
static int http_get_short_seek(URLContext *h)
Definition: http.c:1990
static int http_get_file_handle(URLContext *h)
Definition: http.c:1984
void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
Initialize the authentication state based on another HTTP URLContext.
Definition: http.c:186
static int http_write_reply(URLContext *h, int status_code)
Definition: http.c:500
#define HTTP_SINGLE
Definition: http.c:51
static int http_should_reconnect(HTTPContext *s, int err)
Definition: http.c:296
static void handle_http_errors(URLContext *h, int error)
Definition: http.c:574
static int http_read_header(URLContext *h, int *new_location)
Definition: http.c:1351
#define DEFAULT_USER_AGENT
Definition: http.c:136
#define HTTP_CLASS(flavor)
Definition: http.c:1996
static int http_connect(URLContext *h, const char *path, const char *local_path, const char *hoststr, const char *auth, const char *proxyauth, int *new_location)
Definition: http.c:1414
static int parse_set_cookie_expiry_time(const char *exp_str, struct tm *buf)
Definition: http.c:860
static int http_write(URLContext *h, const uint8_t *buf, int size)
Definition: http.c:1845
static void bprint_escaped_path(AVBPrint *bp, const char *path)
Escape unsafe characters in path in order to pass them safely to the HTTP request.
Definition: http.c:1390
static const char * cookie_domain(const AVDictionary *cookie_params)
Definition: http.c:925
#define MAX_REDIRECTS
Definition: http.c:50
static int64_t http_seek(URLContext *h, int64_t off, int whence)
Definition: http.c:1979
#define OFFSET(x)
Definition: http.c:133
static int bind_cookies_to_host(HTTPContext *s)
Definition: http.c:1028
static int http_open_cnx(URLContext *h, AVDictionary **options)
Definition: http.c:330
#define WHITESPACES
Definition: http.c:54
static int http_close(URLContext *h)
Definition: http.c:1900
int ff_http_do_new_request(URLContext *h, const char *uri)
Send a new HTTP request, reusing the old connection.
Definition: http.c:420
#define D
Definition: http.c:134
static int http_accept(URLContext *s, URLContext **c)
Definition: http.c:695
static int http_read_stream(URLContext *h, uint8_t *buf, int size)
Definition: http.c:1694
static int parse_cookie(HTTPContext *s, const char *p, const char *host, AVDictionary **cookies)
Definition: http.c:946
static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
Definition: http.c:1750
static int parse_location(HTTPContext *s, const char *p)
Definition: http.c:775
char * ff_http_auth_create_response(HTTPAuthState *state, const char *auth, const char *path, const char *method)
Definition: httpauth.c:245
void ff_http_auth_handle_header(HTTPAuthState *state, const char *key, const char *value)
Definition: httpauth.c:90
HTTPAuthType
Authentication types, ordered from weakest to strongest.
Definition: httpauth.h:28
@ HTTP_AUTH_NONE
No authentication specified.
Definition: httpauth.h:29
@ HTTP_AUTH_BASIC
HTTP 1.0 Basic auth from RFC 1945 (also in RFC 2617)
Definition: httpauth.h:30
const char * key
int i
Definition: input.c:407
#define SPACE_CHARS
Definition: internal.h:499
#define MAX_URL_SIZE
Definition: internal.h:30
common internal API header
#define SIZE_SPECIFIER
Definition: internal.h:193
version
Definition: libkvazaar.c:326
static void body(uint32_t ABCD[4], const uint8_t *src, int nblocks)
Definition: md5.c:101
uint32_t tag
Definition: movenc.c:1611
const char data[16]
Definition: mxf.c:142
int ff_network_sleep_interruptible(int64_t timeout, AVIOInterruptCB *int_cb)
Waits for up to 'timeout' microseconds.
Definition: network.c:98
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:551
AVOptions.
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition: opt.h:291
#define AV_OPT_FLAG_DEPRECATED
set if option is deprecated, users should refer to AVOption.help text for more information
Definition: opt.h:295
#define AV_OPT_FLAG_EXPORT
The option is intended for exporting values to the caller.
Definition: opt.h:286
miscellaneous OS support macros and functions.
char * av_small_strptime(const char *p, const char *fmt, struct tm *dt)
Simplified version of strptime.
Definition: parseutils.c:489
time_t av_timegm(struct tm *tm)
Convert the decomposed UTC time in tm to a time_t value.
Definition: parseutils.c:568
misc parsing utilities
const URLProtocol ff_https_protocol
const URLProtocol ff_http_protocol
const URLProtocol ff_httpproxy_protocol
const char * name
Definition: qsvenc.c:46
static const uint8_t header[24]
Definition: sdr2.c:67
#define snprintf
Definition: snprintf.h:34
unsigned int pos
Definition: spdifenc.c:412
Describe the class of an AVClass context structure.
Definition: log.h:67
char * key
Definition: dict.h:82
char * value
Definition: dict.h:83
AVOption.
Definition: opt.h:248
HTTP Authentication state structure.
Definition: httpauth.h:55
HandshakeState handshake_step
Definition: http.c:128
int is_akamai
Definition: http.c:95
uint64_t chunksize
Definition: http.c:69
URLContext * hd
Definition: http.c:64
char * resource
Definition: http.c:125
char * method
Definition: http.c:117
unsigned char * buf_end
Definition: http.c:65
int end_chunked_post
Definition: http.c:88
int max_redirects
Definition: http.c:130
uint64_t off
Definition: http.c:71
AVDictionary * chained_options
Definition: http.c:114
char * location
Definition: http.c:72
uint64_t icy_metaint
Definition: http.c:105
int reply_code
Definition: http.c:126
int listen
Definition: http.c:124
uint64_t icy_data_read
Definition: http.c:103
char * icy_metadata_packet
Definition: http.c:107
char * user_agent
Definition: http.c:79
char * reconnect_on_http_error
Definition: http.c:123
int seekable
Control seekability, 0 = disable, 1 = enable, -1 = probe.
Definition: http.c:85
char * headers
Definition: http.c:76
int http_code
Definition: http.c:67
int end_header
Definition: http.c:90
char * referer
Definition: http.c:80
char * host
Definition: http.c:100
int is_multi_client
Definition: http.c:127
AVDictionary * metadata
Definition: http.c:108
uint8_t * post_data
Definition: http.c:93
unsigned char buffer[BUFFER_SIZE]
Definition: http.c:65
char * content_type
Definition: http.c:81
int reconnect_on_network_error
Definition: http.c:120
char * http_version
Definition: http.c:78
int send_expect_100
Definition: http.c:116
uint64_t filesize
Definition: http.c:71
char * icy_metadata_headers
Definition: http.c:106
int willclose
Definition: http.c:84
char * mime_type
Definition: http.c:77
int is_mediagateway
Definition: http.c:96
int multiple_requests
Definition: http.c:92
char * http_proxy
Definition: http.c:75
int is_connected_server
Definition: http.c:129
HTTPAuthState proxy_auth_state
Definition: http.c:74
int reconnect_streamed
Definition: http.c:121
int icy
Definition: http.c:101
int reconnect
Definition: http.c:118
int chunked_post
Definition: http.c:86
char * cookies
holds newline ( ) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
Definition: http.c:97
uint64_t end_off
Definition: http.c:71
int post_datalen
Definition: http.c:94
HTTPAuthState auth_state
Definition: http.c:73
unsigned char * buf_ptr
Definition: http.c:65
AVDictionary * cookie_dict
Definition: http.c:99
int reconnect_at_eof
Definition: http.c:119
int reconnect_delay_max
Definition: http.c:122
int line_count
Definition: http.c:66
int chunkend
Definition: http.c:70
Definition: url.h:38
void * priv_data
Definition: url.h:41
AVIOInterruptCB interrupt_callback
Definition: url.h:47
const char * name
Definition: url.h:55
Definition: graph2dot.c:48
#define av_free(p)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
#define src
Definition: vp8dsp.c:255
AVDictionary * opts
Definition: movenc.c:50
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
int size
int ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Convert a relative url into an absolute url, given a base url.
Definition: url.c:319
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition: url.c:38
unbuffered private I/O API
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:34
else temp
Definition: vf_mcdeint.c:259
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:199
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len
static double c[64]