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