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