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 
33 #include "avformat.h"
34 #include "http.h"
35 #include "httpauth.h"
36 #include "internal.h"
37 #include "network.h"
38 #include "os_support.h"
39 #include "url.h"
40 
41 /* XXX: POST protocol is not completely implemented because ffmpeg uses
42  * only a subset of it. */
43 
44 /* The IO buffer size is unrelated to the max URL size in itself, but needs
45  * to be large enough to fit the full request headers (including long
46  * path names). */
47 #define BUFFER_SIZE MAX_URL_SIZE
48 #define MAX_REDIRECTS 8
49 #define HTTP_SINGLE 1
50 #define HTTP_MUTLI 2
51 typedef enum {
57 
58 typedef struct HTTPContext {
59  const AVClass *class;
61  unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
63  int http_code;
64  /* Used if "Transfer-Encoding: chunked" otherwise -1. */
65  int64_t chunksize;
66  int64_t off, end_off, filesize;
67  char *location;
70  char *http_proxy;
71  char *headers;
72  char *mime_type;
73  char *user_agent;
74  char *content_type;
75  /* Set if the server correctly handles Connection: close and will close
76  * the connection after feeding us the content. */
77  int willclose;
78  int seekable; /**< Control seekability, 0 = disable, 1 = enable, -1 = probe. */
80  /* A flag which indicates if the end of chunked encoding has been sent. */
82  /* A flag which indicates we have finished to read POST reply. */
84  /* A flag which indicates if we use persistent connections. */
88  int is_akamai;
90  char *cookies; ///< holds newline (\n) delimited Set-Cookie header field values (without the "Set-Cookie: " field name)
91  /* A dictionary containing cookies keyed by cookie name */
93  int icy;
94  /* how much data was read since the last ICY metadata packet */
96  /* after how many bytes of read data a new metadata packet will be found */
101 #if CONFIG_ZLIB
102  int compressed;
103  z_stream inflate_stream;
104  uint8_t *inflate_buffer;
105 #endif /* CONFIG_ZLIB */
108  char *method;
114  int listen;
115  char *resource;
120 } HTTPContext;
121 
122 #define OFFSET(x) offsetof(HTTPContext, x)
123 #define D AV_OPT_FLAG_DECODING_PARAM
124 #define E AV_OPT_FLAG_ENCODING_PARAM
125 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
126 
127 static const AVOption options[] = {
128  { "seekable", "control seekability of connection", OFFSET(seekable), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D },
129  { "chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
130  { "http_proxy", "set HTTP proxy to tunnel through", OFFSET(http_proxy), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
131  { "headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
132  { "content_type", "set a specific content type for the POST messages", OFFSET(content_type), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
133  { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
134  { "user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
135  { "multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D | E },
136  { "post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D | E },
137  { "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 },
138  { "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 },
139  { "icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, D },
140  { "icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
141  { "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_EXPORT },
142  { "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
143  { "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"},
144  { "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, "auth_type"},
145  { "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 0, 0, D | E, "auth_type"},
146  { "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 },
147  { "location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
148  { "offset", "initial byte offset", OFFSET(off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
149  { "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 },
150  { "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 },
151  { "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
152  { "reconnect_at_eof", "auto reconnect at EOF", OFFSET(reconnect_at_eof), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
153  { "reconnect_streamed", "auto reconnect streamed / non seekable streams", OFFSET(reconnect_streamed), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
154  { "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 },
155  { "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
156  { "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
157  { "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
158  { NULL }
159 };
160 
161 static int http_connect(URLContext *h, const char *path, const char *local_path,
162  const char *hoststr, const char *auth,
163  const char *proxyauth, int *new_location);
164 static int http_read_header(URLContext *h, int *new_location);
165 
167 {
168  memcpy(&((HTTPContext *)dest->priv_data)->auth_state,
169  &((HTTPContext *)src->priv_data)->auth_state,
170  sizeof(HTTPAuthState));
171  memcpy(&((HTTPContext *)dest->priv_data)->proxy_auth_state,
172  &((HTTPContext *)src->priv_data)->proxy_auth_state,
173  sizeof(HTTPAuthState));
174 }
175 
177 {
178  const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
179  char hostname[1024], hoststr[1024], proto[10];
180  char auth[1024], proxyauth[1024] = "";
181  char path1[MAX_URL_SIZE];
182  char buf[1024], urlbuf[MAX_URL_SIZE];
183  int port, use_proxy, err, location_changed = 0;
184  HTTPContext *s = h->priv_data;
185 
186  av_url_split(proto, sizeof(proto), auth, sizeof(auth),
187  hostname, sizeof(hostname), &port,
188  path1, sizeof(path1), s->location);
189  ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
190 
191  proxy_path = s->http_proxy ? s->http_proxy : getenv("http_proxy");
192  use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
193  proxy_path && av_strstart(proxy_path, "http://", NULL);
194 
195  if (!strcmp(proto, "https")) {
196  lower_proto = "tls";
197  use_proxy = 0;
198  if (port < 0)
199  port = 443;
200  }
201  if (port < 0)
202  port = 80;
203 
204  if (path1[0] == '\0')
205  path = "/";
206  else
207  path = path1;
208  local_path = path;
209  if (use_proxy) {
210  /* Reassemble the request URL without auth string - we don't
211  * want to leak the auth to the proxy. */
212  ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
213  path1);
214  path = urlbuf;
215  av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
216  hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
217  }
218 
219  ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
220 
221  if (!s->hd) {
223  &h->interrupt_callback, options,
225  if (err < 0)
226  return err;
227  }
228 
229  err = http_connect(h, path, local_path, hoststr,
230  auth, proxyauth, &location_changed);
231  if (err < 0)
232  return err;
233 
234  return location_changed;
235 }
236 
237 /* return non zero if error */
238 static int http_open_cnx(URLContext *h, AVDictionary **options)
239 {
240  HTTPAuthType cur_auth_type, cur_proxy_auth_type;
241  HTTPContext *s = h->priv_data;
242  int location_changed, attempts = 0, redirects = 0;
243 redo:
244  av_dict_copy(options, s->chained_options, 0);
245 
246  cur_auth_type = s->auth_state.auth_type;
247  cur_proxy_auth_type = s->auth_state.auth_type;
248 
249  location_changed = http_open_cnx_internal(h, options);
250  if (location_changed < 0)
251  goto fail;
252 
253  attempts++;
254  if (s->http_code == 401) {
255  if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
256  s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
257  ffurl_closep(&s->hd);
258  goto redo;
259  } else
260  goto fail;
261  }
262  if (s->http_code == 407) {
263  if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
264  s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
265  ffurl_closep(&s->hd);
266  goto redo;
267  } else
268  goto fail;
269  }
270  if ((s->http_code == 301 || s->http_code == 302 ||
271  s->http_code == 303 || s->http_code == 307) &&
272  location_changed == 1) {
273  /* url moved, get next */
274  ffurl_closep(&s->hd);
275  if (redirects++ >= MAX_REDIRECTS)
276  return AVERROR(EIO);
277  /* Restart the authentication process with the new target, which
278  * might use a different auth mechanism. */
279  memset(&s->auth_state, 0, sizeof(s->auth_state));
280  attempts = 0;
281  location_changed = 0;
282  goto redo;
283  }
284  return 0;
285 
286 fail:
287  if (s->hd)
288  ffurl_closep(&s->hd);
289  if (location_changed < 0)
290  return location_changed;
291  return ff_http_averror(s->http_code, AVERROR(EIO));
292 }
293 
294 int ff_http_do_new_request(URLContext *h, const char *uri)
295 {
296  HTTPContext *s = h->priv_data;
297  AVDictionary *options = NULL;
298  int ret;
299 
300  s->off = 0;
301  s->icy_data_read = 0;
302  av_free(s->location);
303  s->location = av_strdup(uri);
304  if (!s->location)
305  return AVERROR(ENOMEM);
306 
307  ret = http_open_cnx(h, &options);
308  av_dict_free(&options);
309  return ret;
310 }
311 
312 int ff_http_averror(int status_code, int default_averror)
313 {
314  switch (status_code) {
315  case 400: return AVERROR_HTTP_BAD_REQUEST;
316  case 401: return AVERROR_HTTP_UNAUTHORIZED;
317  case 403: return AVERROR_HTTP_FORBIDDEN;
318  case 404: return AVERROR_HTTP_NOT_FOUND;
319  default: break;
320  }
321  if (status_code >= 400 && status_code <= 499)
322  return AVERROR_HTTP_OTHER_4XX;
323  else if (status_code >= 500)
325  else
326  return default_averror;
327 }
328 
329 static int http_write_reply(URLContext* h, int status_code)
330 {
331  int ret, body = 0, reply_code, message_len;
332  const char *reply_text, *content_type;
333  HTTPContext *s = h->priv_data;
334  char message[BUFFER_SIZE];
335  content_type = "text/plain";
336 
337  if (status_code < 0)
338  body = 1;
339  switch (status_code) {
341  case 400:
342  reply_code = 400;
343  reply_text = "Bad Request";
344  break;
346  case 403:
347  reply_code = 403;
348  reply_text = "Forbidden";
349  break;
351  case 404:
352  reply_code = 404;
353  reply_text = "Not Found";
354  break;
355  case 200:
356  reply_code = 200;
357  reply_text = "OK";
358  content_type = s->content_type ? s->content_type : "application/octet-stream";
359  break;
361  case 500:
362  reply_code = 500;
363  reply_text = "Internal server error";
364  break;
365  default:
366  return AVERROR(EINVAL);
367  }
368  if (body) {
369  s->chunked_post = 0;
370  message_len = snprintf(message, sizeof(message),
371  "HTTP/1.1 %03d %s\r\n"
372  "Content-Type: %s\r\n"
373  "Content-Length: %"SIZE_SPECIFIER"\r\n"
374  "%s"
375  "\r\n"
376  "%03d %s\r\n",
377  reply_code,
378  reply_text,
379  content_type,
380  strlen(reply_text) + 6, // 3 digit status code + space + \r\n
381  s->headers ? s->headers : "",
382  reply_code,
383  reply_text);
384  } else {
385  s->chunked_post = 1;
386  message_len = snprintf(message, sizeof(message),
387  "HTTP/1.1 %03d %s\r\n"
388  "Content-Type: %s\r\n"
389  "Transfer-Encoding: chunked\r\n"
390  "%s"
391  "\r\n",
392  reply_code,
393  reply_text,
394  content_type,
395  s->headers ? s->headers : "");
396  }
397  av_log(h, AV_LOG_TRACE, "HTTP reply header: \n%s----\n", message);
398  if ((ret = ffurl_write(s->hd, message, message_len)) < 0)
399  return ret;
400  return 0;
401 }
402 
403 static void handle_http_errors(URLContext *h, int error)
404 {
405  av_assert0(error < 0);
406  http_write_reply(h, error);
407 }
408 
410 {
411  int ret, err, new_location;
412  HTTPContext *ch = c->priv_data;
413  URLContext *cl = ch->hd;
414  switch (ch->handshake_step) {
415  case LOWER_PROTO:
416  av_log(c, AV_LOG_TRACE, "Lower protocol\n");
417  if ((ret = ffurl_handshake(cl)) > 0)
418  return 2 + ret;
419  if (ret < 0)
420  return ret;
422  ch->is_connected_server = 1;
423  return 2;
424  case READ_HEADERS:
425  av_log(c, AV_LOG_TRACE, "Read headers\n");
426  if ((err = http_read_header(c, &new_location)) < 0) {
427  handle_http_errors(c, err);
428  return err;
429  }
431  return 1;
432  case WRITE_REPLY_HEADERS:
433  av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
434  if ((err = http_write_reply(c, ch->reply_code)) < 0)
435  return err;
436  ch->handshake_step = FINISH;
437  return 1;
438  case FINISH:
439  return 0;
440  }
441  // this should never be reached.
442  return AVERROR(EINVAL);
443 }
444 
445 static int http_listen(URLContext *h, const char *uri, int flags,
446  AVDictionary **options) {
447  HTTPContext *s = h->priv_data;
448  int ret;
449  char hostname[1024], proto[10];
450  char lower_url[100];
451  const char *lower_proto = "tcp";
452  int port;
453  av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
454  NULL, 0, uri);
455  if (!strcmp(proto, "https"))
456  lower_proto = "tls";
457  ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
458  NULL);
459  if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
460  goto fail;
461  if ((ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
462  &h->interrupt_callback, options,
464  )) < 0)
465  goto fail;
467  if (s->listen == HTTP_SINGLE) { /* single client */
468  s->reply_code = 200;
469  while ((ret = http_handshake(h)) > 0);
470  }
471 fail:
473  return ret;
474 }
475 
476 static int http_open(URLContext *h, const char *uri, int flags,
477  AVDictionary **options)
478 {
479  HTTPContext *s = h->priv_data;
480  int ret;
481 
482  if( s->seekable == 1 )
483  h->is_streamed = 0;
484  else
485  h->is_streamed = 1;
486 
487  s->filesize = -1;
488  s->location = av_strdup(uri);
489  if (!s->location)
490  return AVERROR(ENOMEM);
491  if (options)
492  av_dict_copy(&s->chained_options, *options, 0);
493 
494  if (s->headers) {
495  int len = strlen(s->headers);
496  if (len < 2 || strcmp("\r\n", s->headers + len - 2)) {
498  "No trailing CRLF found in HTTP header.\n");
499  ret = av_reallocp(&s->headers, len + 3);
500  if (ret < 0)
501  return ret;
502  s->headers[len] = '\r';
503  s->headers[len + 1] = '\n';
504  s->headers[len + 2] = '\0';
505  }
506  }
507 
508  if (s->listen) {
509  return http_listen(h, uri, flags, options);
510  }
511  ret = http_open_cnx(h, options);
512  if (ret < 0)
514  return ret;
515 }
516 
518 {
519  int ret;
520  HTTPContext *sc = s->priv_data;
521  HTTPContext *cc;
522  URLContext *sl = sc->hd;
523  URLContext *cl = NULL;
524 
525  av_assert0(sc->listen);
526  if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
527  goto fail;
528  cc = (*c)->priv_data;
529  if ((ret = ffurl_accept(sl, &cl)) < 0)
530  goto fail;
531  cc->hd = cl;
532  cc->is_multi_client = 1;
533 fail:
534  return ret;
535 }
536 
537 static int http_getc(HTTPContext *s)
538 {
539  int len;
540  if (s->buf_ptr >= s->buf_end) {
541  len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
542  if (len < 0) {
543  return len;
544  } else if (len == 0) {
545  return AVERROR_EOF;
546  } else {
547  s->buf_ptr = s->buffer;
548  s->buf_end = s->buffer + len;
549  }
550  }
551  return *s->buf_ptr++;
552 }
553 
554 static int http_get_line(HTTPContext *s, char *line, int line_size)
555 {
556  int ch;
557  char *q;
558 
559  q = line;
560  for (;;) {
561  ch = http_getc(s);
562  if (ch < 0)
563  return ch;
564  if (ch == '\n') {
565  /* process line */
566  if (q > line && q[-1] == '\r')
567  q--;
568  *q = '\0';
569 
570  return 0;
571  } else {
572  if ((q - line) < line_size - 1)
573  *q++ = ch;
574  }
575  }
576 }
577 
578 static int check_http_code(URLContext *h, int http_code, const char *end)
579 {
580  HTTPContext *s = h->priv_data;
581  /* error codes are 4xx and 5xx, but regard 401 as a success, so we
582  * don't abort until all headers have been parsed. */
583  if (http_code >= 400 && http_code < 600 &&
584  (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
585  (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
586  end += strspn(end, SPACE_CHARS);
587  av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
588  return ff_http_averror(http_code, AVERROR(EIO));
589  }
590  return 0;
591 }
592 
593 static int parse_location(HTTPContext *s, const char *p)
594 {
595  char redirected_location[MAX_URL_SIZE], *new_loc;
596  ff_make_absolute_url(redirected_location, sizeof(redirected_location),
597  s->location, p);
598  new_loc = av_strdup(redirected_location);
599  if (!new_loc)
600  return AVERROR(ENOMEM);
601  av_free(s->location);
602  s->location = new_loc;
603  return 0;
604 }
605 
606 /* "bytes $from-$to/$document_size" */
607 static void parse_content_range(URLContext *h, const char *p)
608 {
609  HTTPContext *s = h->priv_data;
610  const char *slash;
611 
612  if (!strncmp(p, "bytes ", 6)) {
613  p += 6;
614  s->off = strtoll(p, NULL, 10);
615  if ((slash = strchr(p, '/')) && strlen(slash) > 0)
616  s->filesize = strtoll(slash + 1, NULL, 10);
617  }
618  if (s->seekable == -1 && (!s->is_akamai || s->filesize != 2147483647))
619  h->is_streamed = 0; /* we _can_ in fact seek */
620 }
621 
622 static int parse_content_encoding(URLContext *h, const char *p)
623 {
624  if (!av_strncasecmp(p, "gzip", 4) ||
625  !av_strncasecmp(p, "deflate", 7)) {
626 #if CONFIG_ZLIB
627  HTTPContext *s = h->priv_data;
628 
629  s->compressed = 1;
630  inflateEnd(&s->inflate_stream);
631  if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
632  av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
633  s->inflate_stream.msg);
634  return AVERROR(ENOSYS);
635  }
636  if (zlibCompileFlags() & (1 << 17)) {
638  "Your zlib was compiled without gzip support.\n");
639  return AVERROR(ENOSYS);
640  }
641 #else
643  "Compressed (%s) content, need zlib with gzip support\n", p);
644  return AVERROR(ENOSYS);
645 #endif /* CONFIG_ZLIB */
646  } else if (!av_strncasecmp(p, "identity", 8)) {
647  // The normal, no-encoding case (although servers shouldn't include
648  // the header at all if this is the case).
649  } else {
650  av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
651  }
652  return 0;
653 }
654 
655 // Concat all Icy- header lines
656 static int parse_icy(HTTPContext *s, const char *tag, const char *p)
657 {
658  int len = 4 + strlen(p) + strlen(tag);
659  int is_first = !s->icy_metadata_headers;
660  int ret;
661 
662  av_dict_set(&s->metadata, tag, p, 0);
663 
664  if (s->icy_metadata_headers)
665  len += strlen(s->icy_metadata_headers);
666 
667  if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
668  return ret;
669 
670  if (is_first)
671  *s->icy_metadata_headers = '\0';
672 
673  av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
674 
675  return 0;
676 }
677 
678 static int parse_cookie(HTTPContext *s, const char *p, AVDictionary **cookies)
679 {
680  char *eql, *name;
681 
682  // duplicate the cookie name (dict will dupe the value)
683  if (!(eql = strchr(p, '='))) return AVERROR(EINVAL);
684  if (!(name = av_strndup(p, eql - p))) return AVERROR(ENOMEM);
685 
686  // add the cookie to the dictionary
687  av_dict_set(cookies, name, eql, AV_DICT_DONT_STRDUP_KEY);
688 
689  return 0;
690 }
691 
692 static int cookie_string(AVDictionary *dict, char **cookies)
693 {
694  AVDictionaryEntry *e = NULL;
695  int len = 1;
696 
697  // determine how much memory is needed for the cookies string
698  while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
699  len += strlen(e->key) + strlen(e->value) + 1;
700 
701  // reallocate the cookies
702  e = NULL;
703  if (*cookies) av_free(*cookies);
704  *cookies = av_malloc(len);
705  if (!*cookies) return AVERROR(ENOMEM);
706  *cookies[0] = '\0';
707 
708  // write out the cookies
709  while (e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX))
710  av_strlcatf(*cookies, len, "%s%s\n", e->key, e->value);
711 
712  return 0;
713 }
714 
715 static int process_line(URLContext *h, char *line, int line_count,
716  int *new_location)
717 {
718  HTTPContext *s = h->priv_data;
719  const char *auto_method = h->flags & AVIO_FLAG_READ ? "POST" : "GET";
720  char *tag, *p, *end, *method, *resource, *version;
721  int ret;
722 
723  /* end of header */
724  if (line[0] == '\0') {
725  s->end_header = 1;
726  return 0;
727  }
728 
729  p = line;
730  if (line_count == 0) {
731  if (s->is_connected_server) {
732  // HTTP method
733  method = p;
734  while (*p && !av_isspace(*p))
735  p++;
736  *(p++) = '\0';
737  av_log(h, AV_LOG_TRACE, "Received method: %s\n", method);
738  if (s->method) {
739  if (av_strcasecmp(s->method, method)) {
740  av_log(h, AV_LOG_ERROR, "Received and expected HTTP method do not match. (%s expected, %s received)\n",
741  s->method, method);
742  return ff_http_averror(400, AVERROR(EIO));
743  }
744  } else {
745  // use autodetected HTTP method to expect
746  av_log(h, AV_LOG_TRACE, "Autodetected %s HTTP method\n", auto_method);
747  if (av_strcasecmp(auto_method, method)) {
748  av_log(h, AV_LOG_ERROR, "Received and autodetected HTTP method did not match "
749  "(%s autodetected %s received)\n", auto_method, method);
750  return ff_http_averror(400, AVERROR(EIO));
751  }
752  if (!(s->method = av_strdup(method)))
753  return AVERROR(ENOMEM);
754  }
755 
756  // HTTP resource
757  while (av_isspace(*p))
758  p++;
759  resource = p;
760  while (!av_isspace(*p))
761  p++;
762  *(p++) = '\0';
763  av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
764  if (!(s->resource = av_strdup(resource)))
765  return AVERROR(ENOMEM);
766 
767  // HTTP version
768  while (av_isspace(*p))
769  p++;
770  version = p;
771  while (*p && !av_isspace(*p))
772  p++;
773  *p = '\0';
774  if (av_strncasecmp(version, "HTTP/", 5)) {
775  av_log(h, AV_LOG_ERROR, "Malformed HTTP version string.\n");
776  return ff_http_averror(400, AVERROR(EIO));
777  }
778  av_log(h, AV_LOG_TRACE, "HTTP version string: %s\n", version);
779  } else {
780  while (!av_isspace(*p) && *p != '\0')
781  p++;
782  while (av_isspace(*p))
783  p++;
784  s->http_code = strtol(p, &end, 10);
785 
786  av_log(h, AV_LOG_TRACE, "http_code=%d\n", s->http_code);
787 
788  if ((ret = check_http_code(h, s->http_code, end)) < 0)
789  return ret;
790  }
791  } else {
792  while (*p != '\0' && *p != ':')
793  p++;
794  if (*p != ':')
795  return 1;
796 
797  *p = '\0';
798  tag = line;
799  p++;
800  while (av_isspace(*p))
801  p++;
802  if (!av_strcasecmp(tag, "Location")) {
803  if ((ret = parse_location(s, p)) < 0)
804  return ret;
805  *new_location = 1;
806  } else if (!av_strcasecmp(tag, "Content-Length") && s->filesize == -1) {
807  s->filesize = strtoll(p, NULL, 10);
808  } else if (!av_strcasecmp(tag, "Content-Range")) {
809  parse_content_range(h, p);
810  } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
811  !strncmp(p, "bytes", 5) &&
812  s->seekable == -1) {
813  h->is_streamed = 0;
814  } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
815  !av_strncasecmp(p, "chunked", 7)) {
816  s->filesize = -1;
817  s->chunksize = 0;
818  } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
820  } else if (!av_strcasecmp(tag, "Authentication-Info")) {
822  } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
824  } else if (!av_strcasecmp(tag, "Connection")) {
825  if (!strcmp(p, "close"))
826  s->willclose = 1;
827  } else if (!av_strcasecmp(tag, "Server")) {
828  if (!av_strcasecmp(p, "AkamaiGHost")) {
829  s->is_akamai = 1;
830  } else if (!av_strncasecmp(p, "MediaGateway", 12)) {
831  s->is_mediagateway = 1;
832  }
833  } else if (!av_strcasecmp(tag, "Content-Type")) {
834  av_free(s->mime_type);
835  s->mime_type = av_strdup(p);
836  } else if (!av_strcasecmp(tag, "Set-Cookie")) {
837  if (parse_cookie(s, p, &s->cookie_dict))
838  av_log(h, AV_LOG_WARNING, "Unable to parse '%s'\n", p);
839  } else if (!av_strcasecmp(tag, "Icy-MetaInt")) {
840  s->icy_metaint = strtoll(p, NULL, 10);
841  } else if (!av_strncasecmp(tag, "Icy-", 4)) {
842  if ((ret = parse_icy(s, tag, p)) < 0)
843  return ret;
844  } else if (!av_strcasecmp(tag, "Content-Encoding")) {
845  if ((ret = parse_content_encoding(h, p)) < 0)
846  return ret;
847  }
848  }
849  return 1;
850 }
851 
852 /**
853  * Create a string containing cookie values for use as a HTTP cookie header
854  * field value for a particular path and domain from the cookie values stored in
855  * the HTTP protocol context. The cookie string is stored in *cookies.
856  *
857  * @return a negative value if an error condition occurred, 0 otherwise
858  */
859 static int get_cookies(HTTPContext *s, char **cookies, const char *path,
860  const char *domain)
861 {
862  // cookie strings will look like Set-Cookie header field values. Multiple
863  // Set-Cookie fields will result in multiple values delimited by a newline
864  int ret = 0;
865  char *next, *cookie, *set_cookies = av_strdup(s->cookies), *cset_cookies = set_cookies;
866 
867  if (!set_cookies) return AVERROR(EINVAL);
868 
869  // destroy any cookies in the dictionary.
871 
872  *cookies = NULL;
873  while ((cookie = av_strtok(set_cookies, "\n", &next))) {
874  int domain_offset = 0;
875  char *param, *next_param, *cdomain = NULL, *cpath = NULL, *cvalue = NULL;
876  set_cookies = NULL;
877 
878  // store the cookie in a dict in case it is updated in the response
879  if (parse_cookie(s, cookie, &s->cookie_dict))
880  av_log(s, AV_LOG_WARNING, "Unable to parse '%s'\n", cookie);
881 
882  while ((param = av_strtok(cookie, "; ", &next_param))) {
883  if (cookie) {
884  // first key-value pair is the actual cookie value
885  cvalue = av_strdup(param);
886  cookie = NULL;
887  } else if (!av_strncasecmp("path=", param, 5)) {
888  av_free(cpath);
889  cpath = av_strdup(&param[5]);
890  } else if (!av_strncasecmp("domain=", param, 7)) {
891  // if the cookie specifies a sub-domain, skip the leading dot thereby
892  // supporting URLs that point to sub-domains and the master domain
893  int leading_dot = (param[7] == '.');
894  av_free(cdomain);
895  cdomain = av_strdup(&param[7+leading_dot]);
896  } else {
897  // ignore unknown attributes
898  }
899  }
900  if (!cdomain)
901  cdomain = av_strdup(domain);
902 
903  // ensure all of the necessary values are valid
904  if (!cdomain || !cpath || !cvalue) {
906  "Invalid cookie found, no value, path or domain specified\n");
907  goto done_cookie;
908  }
909 
910  // check if the request path matches the cookie path
911  if (av_strncasecmp(path, cpath, strlen(cpath)))
912  goto done_cookie;
913 
914  // the domain should be at least the size of our cookie domain
915  domain_offset = strlen(domain) - strlen(cdomain);
916  if (domain_offset < 0)
917  goto done_cookie;
918 
919  // match the cookie domain
920  if (av_strcasecmp(&domain[domain_offset], cdomain))
921  goto done_cookie;
922 
923  // cookie parameters match, so copy the value
924  if (!*cookies) {
925  if (!(*cookies = av_strdup(cvalue))) {
926  ret = AVERROR(ENOMEM);
927  goto done_cookie;
928  }
929  } else {
930  char *tmp = *cookies;
931  size_t str_size = strlen(cvalue) + strlen(*cookies) + 3;
932  if (!(*cookies = av_malloc(str_size))) {
933  ret = AVERROR(ENOMEM);
934  goto done_cookie;
935  }
936  snprintf(*cookies, str_size, "%s; %s", tmp, cvalue);
937  av_free(tmp);
938  }
939 
940  done_cookie:
941  av_freep(&cdomain);
942  av_freep(&cpath);
943  av_freep(&cvalue);
944  if (ret < 0) {
945  if (*cookies) av_freep(cookies);
946  av_free(cset_cookies);
947  return ret;
948  }
949  }
950 
951  av_free(cset_cookies);
952 
953  return 0;
954 }
955 
956 static inline int has_header(const char *str, const char *header)
957 {
958  /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
959  if (!str)
960  return 0;
961  return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
962 }
963 
964 static int http_read_header(URLContext *h, int *new_location)
965 {
966  HTTPContext *s = h->priv_data;
967  char line[MAX_URL_SIZE];
968  int err = 0;
969 
970  s->chunksize = -1;
971 
972  for (;;) {
973  if ((err = http_get_line(s, line, sizeof(line))) < 0)
974  return err;
975 
976  av_log(h, AV_LOG_TRACE, "header='%s'\n", line);
977 
978  err = process_line(h, line, s->line_count, new_location);
979  if (err < 0)
980  return err;
981  if (err == 0)
982  break;
983  s->line_count++;
984  }
985 
986  if (s->seekable == -1 && s->is_mediagateway && s->filesize == 2000000000)
987  h->is_streamed = 1; /* we can in fact _not_ seek */
988 
989  // add any new cookies into the existing cookie string
992 
993  return err;
994 }
995 
996 static int http_connect(URLContext *h, const char *path, const char *local_path,
997  const char *hoststr, const char *auth,
998  const char *proxyauth, int *new_location)
999 {
1000  HTTPContext *s = h->priv_data;
1001  int post, err;
1002  char headers[HTTP_HEADERS_SIZE] = "";
1003  char *authstr = NULL, *proxyauthstr = NULL;
1004  int64_t off = s->off;
1005  int len = 0;
1006  const char *method;
1007  int send_expect_100 = 0;
1008 
1009  /* send http header */
1010  post = h->flags & AVIO_FLAG_WRITE;
1011 
1012  if (s->post_data) {
1013  /* force POST method and disable chunked encoding when
1014  * custom HTTP post data is set */
1015  post = 1;
1016  s->chunked_post = 0;
1017  }
1018 
1019  if (s->method)
1020  method = s->method;
1021  else
1022  method = post ? "POST" : "GET";
1023 
1024  authstr = ff_http_auth_create_response(&s->auth_state, auth,
1025  local_path, method);
1026  proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
1027  local_path, method);
1028  if (post && !s->post_data) {
1029  send_expect_100 = s->send_expect_100;
1030  /* The user has supplied authentication but we don't know the auth type,
1031  * send Expect: 100-continue to get the 401 response including the
1032  * WWW-Authenticate header, or an 100 continue if no auth actually
1033  * is needed. */
1034  if (auth && *auth &&
1036  s->http_code != 401)
1037  send_expect_100 = 1;
1038  }
1039 
1040  /* set default headers if needed */
1041  if (!has_header(s->headers, "\r\nUser-Agent: "))
1042  len += av_strlcatf(headers + len, sizeof(headers) - len,
1043  "User-Agent: %s\r\n", s->user_agent);
1044  if (!has_header(s->headers, "\r\nAccept: "))
1045  len += av_strlcpy(headers + len, "Accept: */*\r\n",
1046  sizeof(headers) - len);
1047  // Note: we send this on purpose even when s->off is 0 when we're probing,
1048  // since it allows us to detect more reliably if a (non-conforming)
1049  // server supports seeking by analysing the reply headers.
1050  if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable == -1)) {
1051  len += av_strlcatf(headers + len, sizeof(headers) - len,
1052  "Range: bytes=%"PRId64"-", s->off);
1053  if (s->end_off)
1054  len += av_strlcatf(headers + len, sizeof(headers) - len,
1055  "%"PRId64, s->end_off - 1);
1056  len += av_strlcpy(headers + len, "\r\n",
1057  sizeof(headers) - len);
1058  }
1059  if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
1060  len += av_strlcatf(headers + len, sizeof(headers) - len,
1061  "Expect: 100-continue\r\n");
1062 
1063  if (!has_header(s->headers, "\r\nConnection: ")) {
1064  if (s->multiple_requests)
1065  len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
1066  sizeof(headers) - len);
1067  else
1068  len += av_strlcpy(headers + len, "Connection: close\r\n",
1069  sizeof(headers) - len);
1070  }
1071 
1072  if (!has_header(s->headers, "\r\nHost: "))
1073  len += av_strlcatf(headers + len, sizeof(headers) - len,
1074  "Host: %s\r\n", hoststr);
1075  if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
1076  len += av_strlcatf(headers + len, sizeof(headers) - len,
1077  "Content-Length: %d\r\n", s->post_datalen);
1078 
1079  if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
1080  len += av_strlcatf(headers + len, sizeof(headers) - len,
1081  "Content-Type: %s\r\n", s->content_type);
1082  if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
1083  char *cookies = NULL;
1084  if (!get_cookies(s, &cookies, path, hoststr) && cookies) {
1085  len += av_strlcatf(headers + len, sizeof(headers) - len,
1086  "Cookie: %s\r\n", cookies);
1087  av_free(cookies);
1088  }
1089  }
1090  if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
1091  len += av_strlcatf(headers + len, sizeof(headers) - len,
1092  "Icy-MetaData: %d\r\n", 1);
1093 
1094  /* now add in custom headers */
1095  if (s->headers)
1096  av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
1097 
1098  snprintf(s->buffer, sizeof(s->buffer),
1099  "%s %s HTTP/1.1\r\n"
1100  "%s"
1101  "%s"
1102  "%s"
1103  "%s%s"
1104  "\r\n",
1105  method,
1106  path,
1107  post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
1108  headers,
1109  authstr ? authstr : "",
1110  proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
1111 
1112  av_log(h, AV_LOG_DEBUG, "request: %s\n", s->buffer);
1113 
1114  if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1115  goto done;
1116 
1117  if (s->post_data)
1118  if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
1119  goto done;
1120 
1121  /* init input buffer */
1122  s->buf_ptr = s->buffer;
1123  s->buf_end = s->buffer;
1124  s->line_count = 0;
1125  s->off = 0;
1126  s->icy_data_read = 0;
1127  s->filesize = -1;
1128  s->willclose = 0;
1129  s->end_chunked_post = 0;
1130  s->end_header = 0;
1131  if (post && !s->post_data && !send_expect_100) {
1132  /* Pretend that it did work. We didn't read any header yet, since
1133  * we've still to send the POST data, but the code calling this
1134  * function will check http_code after we return. */
1135  s->http_code = 200;
1136  err = 0;
1137  goto done;
1138  }
1139 
1140  /* wait for header */
1141  err = http_read_header(h, new_location);
1142  if (err < 0)
1143  goto done;
1144 
1145  if (*new_location)
1146  s->off = off;
1147 
1148  err = (off == s->off) ? 0 : -1;
1149 done:
1150  av_freep(&authstr);
1151  av_freep(&proxyauthstr);
1152  return err;
1153 }
1154 
1156 {
1157  HTTPContext *s = h->priv_data;
1158  int len;
1159  /* read bytes from input buffer first */
1160  len = s->buf_end - s->buf_ptr;
1161  if (len > 0) {
1162  if (len > size)
1163  len = size;
1164  memcpy(buf, s->buf_ptr, len);
1165  s->buf_ptr += len;
1166  } else {
1167  int64_t target_end = s->end_off ? s->end_off : s->filesize;
1168  if ((!s->willclose || s->chunksize < 0) &&
1169  target_end >= 0 && s->off >= target_end)
1170  return AVERROR_EOF;
1171  len = ffurl_read(s->hd, buf, size);
1172  if (!len && (!s->willclose || s->chunksize < 0) &&
1173  target_end >= 0 && s->off < target_end) {
1174  av_log(h, AV_LOG_ERROR,
1175  "Stream ends prematurely at %"PRId64", should be %"PRId64"\n",
1176  s->off, target_end
1177  );
1178  return AVERROR(EIO);
1179  }
1180  }
1181  if (len > 0) {
1182  s->off += len;
1183  if (s->chunksize > 0)
1184  s->chunksize -= len;
1185  }
1186  return len;
1187 }
1188 
1189 #if CONFIG_ZLIB
1190 #define DECOMPRESS_BUF_SIZE (256 * 1024)
1191 static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
1192 {
1193  HTTPContext *s = h->priv_data;
1194  int ret;
1195 
1196  if (!s->inflate_buffer) {
1197  s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
1198  if (!s->inflate_buffer)
1199  return AVERROR(ENOMEM);
1200  }
1201 
1202  if (s->inflate_stream.avail_in == 0) {
1203  int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
1204  if (read <= 0)
1205  return read;
1206  s->inflate_stream.next_in = s->inflate_buffer;
1207  s->inflate_stream.avail_in = read;
1208  }
1209 
1210  s->inflate_stream.avail_out = size;
1211  s->inflate_stream.next_out = buf;
1212 
1213  ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
1214  if (ret != Z_OK && ret != Z_STREAM_END)
1215  av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n",
1216  ret, s->inflate_stream.msg);
1217 
1218  return size - s->inflate_stream.avail_out;
1219 }
1220 #endif /* CONFIG_ZLIB */
1221 
1222 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect);
1223 
1224 static int http_read_stream(URLContext *h, uint8_t *buf, int size)
1225 {
1226  HTTPContext *s = h->priv_data;
1227  int err, new_location, read_ret;
1228  int64_t seek_ret;
1229 
1230  if (!s->hd)
1231  return AVERROR_EOF;
1232 
1233  if (s->end_chunked_post && !s->end_header) {
1234  err = http_read_header(h, &new_location);
1235  if (err < 0)
1236  return err;
1237  }
1238 
1239  if (s->chunksize >= 0) {
1240  if (!s->chunksize) {
1241  char line[32];
1242 
1243  do {
1244  if ((err = http_get_line(s, line, sizeof(line))) < 0)
1245  return err;
1246  } while (!*line); /* skip CR LF from last chunk */
1247 
1248  s->chunksize = strtoll(line, NULL, 16);
1249 
1250  av_log(NULL, AV_LOG_TRACE, "Chunked encoding data size: %"PRId64"'\n",
1251  s->chunksize);
1252 
1253  if (!s->chunksize)
1254  return 0;
1255  }
1256  size = FFMIN(size, s->chunksize);
1257  }
1258 #if CONFIG_ZLIB
1259  if (s->compressed)
1260  return http_buf_read_compressed(h, buf, size);
1261 #endif /* CONFIG_ZLIB */
1262  read_ret = http_buf_read(h, buf, size);
1263  if ( (read_ret < 0 && s->reconnect && (!h->is_streamed || s->reconnect_streamed) && s->filesize > 0 && s->off < s->filesize)
1264  || (read_ret == 0 && s->reconnect_at_eof && (!h->is_streamed || s->reconnect_streamed))) {
1265  int64_t target = h->is_streamed ? 0 : s->off;
1266 
1268  return AVERROR(EIO);
1269 
1270  av_log(h, AV_LOG_INFO, "Will reconnect at %"PRId64" error=%s.\n", s->off, av_err2str(read_ret));
1271  av_usleep(1000U*1000*s->reconnect_delay);
1272  s->reconnect_delay = 1 + 2*s->reconnect_delay;
1273  seek_ret = http_seek_internal(h, target, SEEK_SET, 1);
1274  if (seek_ret != target) {
1275  av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRId64".\n", target);
1276  return read_ret;
1277  }
1278 
1279  read_ret = http_buf_read(h, buf, size);
1280  } else
1281  s->reconnect_delay = 0;
1282 
1283  return read_ret;
1284 }
1285 
1286 // Like http_read_stream(), but no short reads.
1287 // Assumes partial reads are an error.
1288 static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
1289 {
1290  int pos = 0;
1291  while (pos < size) {
1292  int len = http_read_stream(h, buf + pos, size - pos);
1293  if (len < 0)
1294  return len;
1295  pos += len;
1296  }
1297  return pos;
1298 }
1299 
1300 static void update_metadata(HTTPContext *s, char *data)
1301 {
1302  char *key;
1303  char *val;
1304  char *end;
1305  char *next = data;
1306 
1307  while (*next) {
1308  key = next;
1309  val = strstr(key, "='");
1310  if (!val)
1311  break;
1312  end = strstr(val, "';");
1313  if (!end)
1314  break;
1315 
1316  *val = '\0';
1317  *end = '\0';
1318  val += 2;
1319 
1320  av_dict_set(&s->metadata, key, val, 0);
1321 
1322  next = end + 2;
1323  }
1324 }
1325 
1326 static int store_icy(URLContext *h, int size)
1327 {
1328  HTTPContext *s = h->priv_data;
1329  /* until next metadata packet */
1330  int remaining = s->icy_metaint - s->icy_data_read;
1331 
1332  if (remaining < 0)
1333  return AVERROR_INVALIDDATA;
1334 
1335  if (!remaining) {
1336  /* The metadata packet is variable sized. It has a 1 byte header
1337  * which sets the length of the packet (divided by 16). If it's 0,
1338  * the metadata doesn't change. After the packet, icy_metaint bytes
1339  * of normal data follows. */
1340  uint8_t ch;
1341  int len = http_read_stream_all(h, &ch, 1);
1342  if (len < 0)
1343  return len;
1344  if (ch > 0) {
1345  char data[255 * 16 + 1];
1346  int ret;
1347  len = ch * 16;
1348  ret = http_read_stream_all(h, data, len);
1349  if (ret < 0)
1350  return ret;
1351  data[len + 1] = 0;
1352  if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
1353  return ret;
1354  update_metadata(s, data);
1355  }
1356  s->icy_data_read = 0;
1357  remaining = s->icy_metaint;
1358  }
1359 
1360  return FFMIN(size, remaining);
1361 }
1362 
1363 static int http_read(URLContext *h, uint8_t *buf, int size)
1364 {
1365  HTTPContext *s = h->priv_data;
1366 
1367  if (s->icy_metaint > 0) {
1368  size = store_icy(h, size);
1369  if (size < 0)
1370  return size;
1371  }
1372 
1373  size = http_read_stream(h, buf, size);
1374  if (size > 0)
1375  s->icy_data_read += size;
1376  return size;
1377 }
1378 
1379 /* used only when posting data */
1380 static int http_write(URLContext *h, const uint8_t *buf, int size)
1381 {
1382  char temp[11] = ""; /* 32-bit hex + CRLF + nul */
1383  int ret;
1384  char crlf[] = "\r\n";
1385  HTTPContext *s = h->priv_data;
1386 
1387  if (!s->chunked_post) {
1388  /* non-chunked data is sent without any special encoding */
1389  return ffurl_write(s->hd, buf, size);
1390  }
1391 
1392  /* silently ignore zero-size data since chunk encoding that would
1393  * signal EOF */
1394  if (size > 0) {
1395  /* upload data using chunked encoding */
1396  snprintf(temp, sizeof(temp), "%x\r\n", size);
1397 
1398  if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
1399  (ret = ffurl_write(s->hd, buf, size)) < 0 ||
1400  (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
1401  return ret;
1402  }
1403  return size;
1404 }
1405 
1406 static int http_shutdown(URLContext *h, int flags)
1407 {
1408  int ret = 0;
1409  char footer[] = "0\r\n\r\n";
1410  HTTPContext *s = h->priv_data;
1411 
1412  /* signal end of chunked encoding if used */
1413  if (((flags & AVIO_FLAG_WRITE) && s->chunked_post) ||
1414  ((flags & AVIO_FLAG_READ) && s->chunked_post && s->listen)) {
1415  ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
1416  ret = ret > 0 ? 0 : ret;
1417  s->end_chunked_post = 1;
1418  }
1419 
1420  return ret;
1421 }
1422 
1423 static int http_close(URLContext *h)
1424 {
1425  int ret = 0;
1426  HTTPContext *s = h->priv_data;
1427 
1428 #if CONFIG_ZLIB
1429  inflateEnd(&s->inflate_stream);
1430  av_freep(&s->inflate_buffer);
1431 #endif /* CONFIG_ZLIB */
1432 
1433  if (!s->end_chunked_post)
1434  /* Close the write direction by sending the end of chunked encoding. */
1435  ret = http_shutdown(h, h->flags);
1436 
1437  if (s->hd)
1438  ffurl_closep(&s->hd);
1440  return ret;
1441 }
1442 
1443 static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
1444 {
1445  HTTPContext *s = h->priv_data;
1446  URLContext *old_hd = s->hd;
1447  int64_t old_off = s->off;
1448  uint8_t old_buf[BUFFER_SIZE];
1449  int old_buf_size, ret;
1450  AVDictionary *options = NULL;
1451 
1452  if (whence == AVSEEK_SIZE)
1453  return s->filesize;
1454  else if (!force_reconnect &&
1455  ((whence == SEEK_CUR && off == 0) ||
1456  (whence == SEEK_SET && off == s->off)))
1457  return s->off;
1458  else if ((s->filesize == -1 && whence == SEEK_END))
1459  return AVERROR(ENOSYS);
1460 
1461  if (whence == SEEK_CUR)
1462  off += s->off;
1463  else if (whence == SEEK_END)
1464  off += s->filesize;
1465  else if (whence != SEEK_SET)
1466  return AVERROR(EINVAL);
1467  if (off < 0)
1468  return AVERROR(EINVAL);
1469  s->off = off;
1470 
1471  if (s->off && h->is_streamed)
1472  return AVERROR(ENOSYS);
1473 
1474  /* we save the old context in case the seek fails */
1475  old_buf_size = s->buf_end - s->buf_ptr;
1476  memcpy(old_buf, s->buf_ptr, old_buf_size);
1477  s->hd = NULL;
1478 
1479  /* if it fails, continue on old connection */
1480  if ((ret = http_open_cnx(h, &options)) < 0) {
1481  av_dict_free(&options);
1482  memcpy(s->buffer, old_buf, old_buf_size);
1483  s->buf_ptr = s->buffer;
1484  s->buf_end = s->buffer + old_buf_size;
1485  s->hd = old_hd;
1486  s->off = old_off;
1487  return ret;
1488  }
1489  av_dict_free(&options);
1490  ffurl_close(old_hd);
1491  return off;
1492 }
1493 
1494 static int64_t http_seek(URLContext *h, int64_t off, int whence)
1495 {
1496  return http_seek_internal(h, off, whence, 0);
1497 }
1498 
1500 {
1501  HTTPContext *s = h->priv_data;
1502  return ffurl_get_file_handle(s->hd);
1503 }
1504 
1505 #define HTTP_CLASS(flavor) \
1506 static const AVClass flavor ## _context_class = { \
1507  .class_name = # flavor, \
1508  .item_name = av_default_item_name, \
1509  .option = options, \
1510  .version = LIBAVUTIL_VERSION_INT, \
1511 }
1512 
1513 #if CONFIG_HTTP_PROTOCOL
1514 HTTP_CLASS(http);
1515 
1516 const URLProtocol ff_http_protocol = {
1517  .name = "http",
1518  .url_open2 = http_open,
1519  .url_accept = http_accept,
1520  .url_handshake = http_handshake,
1521  .url_read = http_read,
1522  .url_write = http_write,
1523  .url_seek = http_seek,
1524  .url_close = http_close,
1525  .url_get_file_handle = http_get_file_handle,
1526  .url_shutdown = http_shutdown,
1527  .priv_data_size = sizeof(HTTPContext),
1528  .priv_data_class = &http_context_class,
1530  .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy"
1531 };
1532 #endif /* CONFIG_HTTP_PROTOCOL */
1533 
1534 #if CONFIG_HTTPS_PROTOCOL
1535 HTTP_CLASS(https);
1536 
1538  .name = "https",
1539  .url_open2 = http_open,
1540  .url_read = http_read,
1541  .url_write = http_write,
1542  .url_seek = http_seek,
1543  .url_close = http_close,
1544  .url_get_file_handle = http_get_file_handle,
1545  .url_shutdown = http_shutdown,
1546  .priv_data_size = sizeof(HTTPContext),
1547  .priv_data_class = &https_context_class,
1549  .default_whitelist = "http,https,tls,rtp,tcp,udp,crypto,httpproxy"
1550 };
1551 #endif /* CONFIG_HTTPS_PROTOCOL */
1552 
1553 #if CONFIG_HTTPPROXY_PROTOCOL
1554 static int http_proxy_close(URLContext *h)
1555 {
1556  HTTPContext *s = h->priv_data;
1557  if (s->hd)
1558  ffurl_closep(&s->hd);
1559  return 0;
1560 }
1561 
1562 static int http_proxy_open(URLContext *h, const char *uri, int flags)
1563 {
1564  HTTPContext *s = h->priv_data;
1565  char hostname[1024], hoststr[1024];
1566  char auth[1024], pathbuf[1024], *path;
1567  char lower_url[100];
1568  int port, ret = 0, attempts = 0;
1569  HTTPAuthType cur_auth_type;
1570  char *authstr;
1571  int new_loc;
1572 
1573  if( s->seekable == 1 )
1574  h->is_streamed = 0;
1575  else
1576  h->is_streamed = 1;
1577 
1578  av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
1579  pathbuf, sizeof(pathbuf), uri);
1580  ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1581  path = pathbuf;
1582  if (*path == '/')
1583  path++;
1584 
1585  ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
1586  NULL);
1587 redo:
1588  ret = ffurl_open_whitelist(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
1589  &h->interrupt_callback, NULL,
1591  if (ret < 0)
1592  return ret;
1593 
1594  authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
1595  path, "CONNECT");
1596  snprintf(s->buffer, sizeof(s->buffer),
1597  "CONNECT %s HTTP/1.1\r\n"
1598  "Host: %s\r\n"
1599  "Connection: close\r\n"
1600  "%s%s"
1601  "\r\n",
1602  path,
1603  hoststr,
1604  authstr ? "Proxy-" : "", authstr ? authstr : "");
1605  av_freep(&authstr);
1606 
1607  if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1608  goto fail;
1609 
1610  s->buf_ptr = s->buffer;
1611  s->buf_end = s->buffer;
1612  s->line_count = 0;
1613  s->filesize = -1;
1614  cur_auth_type = s->proxy_auth_state.auth_type;
1615 
1616  /* Note: This uses buffering, potentially reading more than the
1617  * HTTP header. If tunneling a protocol where the server starts
1618  * the conversation, we might buffer part of that here, too.
1619  * Reading that requires using the proper ffurl_read() function
1620  * on this URLContext, not using the fd directly (as the tls
1621  * protocol does). This shouldn't be an issue for tls though,
1622  * since the client starts the conversation there, so there
1623  * is no extra data that we might buffer up here.
1624  */
1625  ret = http_read_header(h, &new_loc);
1626  if (ret < 0)
1627  goto fail;
1628 
1629  attempts++;
1630  if (s->http_code == 407 &&
1631  (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
1632  s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
1633  ffurl_closep(&s->hd);
1634  goto redo;
1635  }
1636 
1637  if (s->http_code < 400)
1638  return 0;
1639  ret = ff_http_averror(s->http_code, AVERROR(EIO));
1640 
1641 fail:
1642  http_proxy_close(h);
1643  return ret;
1644 }
1645 
1646 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
1647 {
1648  HTTPContext *s = h->priv_data;
1649  return ffurl_write(s->hd, buf, size);
1650 }
1651 
1653  .name = "httpproxy",
1654  .url_open = http_proxy_open,
1655  .url_read = http_buf_read,
1656  .url_write = http_proxy_write,
1657  .url_close = http_proxy_close,
1658  .url_get_file_handle = http_get_file_handle,
1659  .priv_data_size = sizeof(HTTPContext),
1660  .flags = URL_PROTOCOL_FLAG_NETWORK,
1661 };
1662 #endif /* CONFIG_HTTPPROXY_PROTOCOL */
static int http_get_line(HTTPContext *s, char *line, int line_size)
Definition: http.c:554
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:4387
static void parse_content_range(URLContext *h, const char *p)
Definition: http.c:607
AVDictionary * metadata
Definition: http.c:100
#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:996
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
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int icy_data_read
Definition: http.c:95
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
Definition: avio.c:309
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:34
uint8_t * post_data
Definition: http.c:86
int reconnect_delay
Definition: http.c:112
AVOption.
Definition: opt.h:245
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:286
HTTPAuthType
Authentication types, ordered from weakest to strongest.
Definition: httpauth.h:28
#define BUFFER_SIZE
Definition: http.c:47
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
static int http_close(URLContext *h)
Definition: http.c:1423
else temp
Definition: vf_mcdeint.c:259
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:69
Definition: http.c:55
#define AVIO_FLAG_READ
read-only
Definition: avio.h:606
char * icy_metadata_headers
Definition: http.c:98
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:607
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:223
#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:445
char * content_type
Definition: http.c:74
int version
Definition: avisynth_c.h:766
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:76
static int http_getc(HTTPContext *s)
Definition: http.c:537
HTTP Authentication state structure.
Definition: httpauth.h:55
int auth_type
The currently chosen auth type.
Definition: httpauth.h:59
int is_connected_server
Definition: http.c:119
#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:49
#define MAX_URL_SIZE
Definition: internal.h:30
static int parse_cookie(HTTPContext *s, const char *p, AVDictionary **cookies)
Definition: http.c:678
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int end_chunked_post
Definition: http.c:81
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:1499
static int http_buf_read(URLContext *h, uint8_t *buf, int size)
Definition: http.c:1155
uint8_t
#define av_malloc(s)
int post_datalen
Definition: http.c:87
AVOptions.
int ff_http_averror(int status_code, int default_averror)
Definition: http.c:312
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:964
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:88
static int http_open_cnx(URLContext *h, AVDictionary **options)
Definition: http.c:238
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:129
uint32_t tag
Definition: movenc.c:1376
#define DEFAULT_USER_AGENT
Definition: http.c:125
#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:166
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
static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
Definition: http.c:176
int line_count
Definition: http.c:62
#define av_log(a,...)
unsigned char * buf_end
Definition: http.c:61
static void update_metadata(HTTPContext *s, char *data)
Definition: http.c:1300
#define U(x)
Definition: vp56_arith.h:37
int is_multi_client
Definition: http.c:117
int chunked_post
Definition: http.c:79
static int64_t http_seek_internal(URLContext *h, int64_t off, int whence, int force_reconnect)
Definition: http.c:1443
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:292
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int reconnect_delay_max
Definition: http.c:113
#define E
Definition: http.c:124
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:1380
static const AVOption options[]
Definition: http.c:127
#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:409
Definition: graph2dot.c:48
simple assert() macros that are a bit more flexible than ISO C assert().
#define OFFSET(x)
Definition: http.c:122
static void body(uint32_t ABCD[4], uint32_t *src, int nblocks)
Definition: md5.c:101
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:82
AVDictionary * chained_options
Definition: http.c:106
#define AVERROR_HTTP_SERVER_ERROR
Definition: error.h:81
static int store_icy(URLContext *h, int size)
Definition: http.c:1326
char * headers
Definition: http.c:71
char * user_agent
Definition: http.c:73
#define AVERROR_HTTP_UNAUTHORIZED
Definition: error.h:77
char * icy_metadata_packet
Definition: http.c:99
char method[16]
Definition: ffserver.c:170
#define FFMIN(a, b)
Definition: common.h:96
const URLProtocol ff_httpproxy_protocol
int64_t off
Definition: http.c:66
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
int64_t end_off
Definition: http.c:66
int send_expect_100
Definition: http.c:107
#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 willclose
Definition: http.c:77
int is_mediagateway
Definition: http.c:89
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:109
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:307
#define src
Definition: vp9dsp.c:530
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
AVDictionary * cookie_dict
Definition: http.c:92
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
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:111
static int http_read(URLContext *h, uint8_t *buf, int size)
Definition: http.c:1363
#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:187
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:267
const char * protocol_blacklist
Definition: url.h:50
char * http_proxy
Definition: http.c:70
HandshakeState handshake_step
Definition: http.c:118
int seekable
Control seekability, 0 = disable, 1 = enable, -1 = probe.
Definition: http.c:78
void * buf
Definition: avisynth_c.h:690
Definition: url.h:38
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:608
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
Describe the class of an AVClass context structure.
Definition: log.h:67
#define SPACE_CHARS
Definition: internal.h:272
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:93
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:54
URLContext * hd
Definition: http.c:60
static int has_header(const char *str, const char *header)
Definition: http.c:956
char * mime_type
Definition: http.c:72
#define SIZE_SPECIFIER
Definition: internal.h:252
#define AVERROR_HTTP_FORBIDDEN
Definition: error.h:78
static int flags
Definition: cpu.c:47
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:715
int ff_http_do_new_request(URLContext *h, const char *uri)
Send a new HTTP request, reusing the old connection.
Definition: http.c:294
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:90
#define MAX_REDIRECTS
Definition: http.c:48
static int parse_content_encoding(URLContext *h, const char *p)
Definition: http.c:622
int reconnect_at_eof
Definition: http.c:110
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:51
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:859
int http_code
Definition: http.c:63
char * filename
specified URL
Definition: url.h:42
int listen
Definition: http.c:114
static int http_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: http.c:476
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:485
char * key
Definition: dict.h:86
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_YASM &&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
void ff_http_auth_handle_header(HTTPAuthState *state, const char *key, const char *value)
Definition: httpauth.c:90
int multiple_requests
Definition: http.c:85
HTTPAuthState auth_state
Definition: http.c:68
#define av_free(p)
static int cookie_string(AVDictionary *dict, char **cookies)
Definition: http.c:692
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:593
uint8_t * buffer
Definition: ffserver.c:173
int64_t filesize
Definition: http.c:66
char * resource
Definition: http.c:115
char * location
Definition: http.c:67
static uint8_t tmp[8]
Definition: des.c:38
static int http_shutdown(URLContext *h, int flags)
Definition: http.c:1406
int icy_metaint
Definition: http.c:97
static int http_write_reply(URLContext *h, int status_code)
Definition: http.c:329
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition: opt.h:291
int end_header
Definition: http.c:83
unsigned char * buf_ptr
Definition: http.c:61
#define D
Definition: http.c:123
#define AVERROR_HTTP_BAD_REQUEST
Definition: error.h:76
char * method
Definition: http.c:108
static int parse_icy(HTTPContext *s, const char *tag, const char *p)
Definition: http.c:656
static int http_read_stream(URLContext *h, uint8_t *buf, int size)
Definition: http.c:1224
#define av_freep(p)
static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
Definition: http.c:1288
#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:403
static int64_t http_seek(URLContext *h, int64_t off, int whence)
Definition: http.c:1494
int reply_code
Definition: http.c:116
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:431
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:279
static int http_accept(URLContext *s, URLContext **c)
Definition: http.c:517
int64_t chunksize
Definition: http.c:65
GLuint buffer
Definition: opengl_enc.c:102
#define HTTP_CLASS(flavor)
Definition: http.c:1505
No authentication specified.
Definition: httpauth.h:29
static int check_http_code(URLContext *h, int http_code, const char *end)
Definition: http.c:578
const char * name
Definition: opengl_enc.c:103