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