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