Libav
http.c
Go to the documentation of this file.
1 /*
2  * HTTP protocol for avconv client
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/avstring.h"
29 #include "libavutil/opt.h"
30 
31 #include "avformat.h"
32 #include "http.h"
33 #include "httpauth.h"
34 #include "internal.h"
35 #include "network.h"
36 #include "os_support.h"
37 #include "url.h"
38 
39 /* XXX: POST protocol is not completely implemented because avconv uses
40  * only a subset of it. */
41 
42 /* The IO buffer size is unrelated to the max URL size in itself, but needs
43  * to be large enough to fit the full request headers (including long
44  * path names). */
45 #define BUFFER_SIZE MAX_URL_SIZE
46 #define MAX_REDIRECTS 8
47 
48 typedef struct {
49  const AVClass *class;
51  unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
53  int http_code;
54  /* Used if "Transfer-Encoding: chunked" otherwise -1. */
55  uint64_t chunksize;
56  uint64_t off, end_off, filesize;
57  char *location;
60  char *headers;
61  char *mime_type;
62  char *user_agent;
63  char *content_type;
64  /* Set if the server correctly handles Connection: close and will close
65  * the connection after feeding us the content. */
66  int willclose;
68  /* A flag which indicates if the end of chunked encoding has been sent. */
70  /* A flag which indicates we have finished to read POST reply. */
72  /* A flag which indicates if we use persistent connections. */
76  int icy;
77  /* how much data was read since the last ICY metadata packet */
78  uint64_t icy_data_read;
79  /* after how many bytes of read data a new metadata packet will be found */
80  uint64_t icy_metaint;
84 #if CONFIG_ZLIB
85  int compressed;
86  z_stream inflate_stream;
87  uint8_t *inflate_buffer;
88 #endif /* CONFIG_ZLIB */
91  char *method;
92 } HTTPContext;
93 
94 #define OFFSET(x) offsetof(HTTPContext, x)
95 #define D AV_OPT_FLAG_DECODING_PARAM
96 #define E AV_OPT_FLAG_ENCODING_PARAM
97 #define DEFAULT_USER_AGENT "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
98 
99 static const AVOption options[] = {
100  { "chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E },
101  { "headers", "set custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
102  { "content_type", "set a specific content type for the POST messages", OFFSET(content_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
103  { "user_agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
104  { "user-agent", "override User-Agent header, for compatibility with ffmpeg", OFFSET(user_agent), AV_OPT_TYPE_STRING, { .str = DEFAULT_USER_AGENT }, 0, 0, D },
105  { "multiple_requests", "use persistent connections", OFFSET(multiple_requests), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D | E },
106  { "post_data", "set custom HTTP post data", OFFSET(post_data), AV_OPT_TYPE_BINARY, .flags = D | E },
107  { "mime_type", "export the MIME type", OFFSET(mime_type), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
108  { "icy", "request ICY metadata", OFFSET(icy), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, D },
109  { "icy_metadata_headers", "return ICY metadata headers", OFFSET(icy_metadata_headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT },
110  { "icy_metadata_packet", "return current ICY metadata packet", OFFSET(icy_metadata_packet), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_EXPORT },
111  { "metadata", "metadata read from the bitstream", OFFSET(metadata), AV_OPT_TYPE_DICT, {0}, 0, 0, AV_OPT_FLAG_EXPORT },
112  { "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"},
113  { "none", "No auth method set, autodetect", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_NONE }, 0, 0, D | E, "auth_type"},
114  { "basic", "HTTP basic authentication", 0, AV_OPT_TYPE_CONST, { .i64 = HTTP_AUTH_BASIC }, 0, 0, D | E, "auth_type"},
115  { "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 },
116  { "location", "The actual location of the data received", OFFSET(location), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D | E },
117  { "offset", "initial byte offset", OFFSET(off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
118  { "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 },
119  { "method", "Override the HTTP method", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
120  { NULL }
121 };
122 
123 static int http_connect(URLContext *h, const char *path, const char *local_path,
124  const char *hoststr, const char *auth,
125  const char *proxyauth, int *new_location);
126 
128 {
129  memcpy(&((HTTPContext *)dest->priv_data)->auth_state,
130  &((HTTPContext *)src->priv_data)->auth_state,
131  sizeof(HTTPAuthState));
132  memcpy(&((HTTPContext *)dest->priv_data)->proxy_auth_state,
133  &((HTTPContext *)src->priv_data)->proxy_auth_state,
134  sizeof(HTTPAuthState));
135 }
136 
138 {
139  const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
140  char hostname[1024], hoststr[1024], proto[10];
141  char auth[1024], proxyauth[1024] = "";
142  char path1[MAX_URL_SIZE];
143  char buf[1024], urlbuf[MAX_URL_SIZE];
144  int port, use_proxy, err, location_changed = 0;
145  HTTPContext *s = h->priv_data;
146 
147  av_url_split(proto, sizeof(proto), auth, sizeof(auth),
148  hostname, sizeof(hostname), &port,
149  path1, sizeof(path1), s->location);
150  ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
151 
152  proxy_path = getenv("http_proxy");
153  use_proxy = !ff_http_match_no_proxy(getenv("no_proxy"), hostname) &&
154  proxy_path && av_strstart(proxy_path, "http://", NULL);
155 
156  if (!strcmp(proto, "https")) {
157  lower_proto = "tls";
158  use_proxy = 0;
159  if (port < 0)
160  port = 443;
161  }
162  if (port < 0)
163  port = 80;
164 
165  if (path1[0] == '\0')
166  path = "/";
167  else
168  path = path1;
169  local_path = path;
170  if (use_proxy) {
171  /* Reassemble the request URL without auth string - we don't
172  * want to leak the auth to the proxy. */
173  ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
174  path1);
175  path = urlbuf;
176  av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
177  hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
178  }
179 
180  ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
181 
182  if (!s->hd) {
183  err = ffurl_open(&s->hd, buf, AVIO_FLAG_READ_WRITE,
184  &h->interrupt_callback, options);
185  if (err < 0)
186  return err;
187  }
188 
189  err = http_connect(h, path, local_path, hoststr,
190  auth, proxyauth, &location_changed);
191  if (err < 0)
192  return err;
193 
194  return location_changed;
195 }
196 
197 /* return non zero if error */
198 static int http_open_cnx(URLContext *h, AVDictionary **options)
199 {
200  HTTPAuthType cur_auth_type, cur_proxy_auth_type;
201  HTTPContext *s = h->priv_data;
202  int location_changed, attempts = 0, redirects = 0;
203 redo:
204  cur_auth_type = s->auth_state.auth_type;
205  cur_proxy_auth_type = s->auth_state.auth_type;
206 
207  location_changed = http_open_cnx_internal(h, options);
208  if (location_changed < 0)
209  goto fail;
210 
211  attempts++;
212  if (s->http_code == 401) {
213  if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
214  s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
215  ffurl_close(s->hd);
216  s->hd = NULL;
217  goto redo;
218  } else
219  goto fail;
220  }
221  if (s->http_code == 407) {
222  if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
223  s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
224  ffurl_close(s->hd);
225  s->hd = NULL;
226  goto redo;
227  } else
228  goto fail;
229  }
230  if ((s->http_code == 301 || s->http_code == 302 ||
231  s->http_code == 303 || s->http_code == 307) &&
232  location_changed == 1) {
233  /* url moved, get next */
234  ffurl_close(s->hd);
235  s->hd = NULL;
236  if (redirects++ >= MAX_REDIRECTS)
237  return AVERROR(EIO);
238  /* Restart the authentication process with the new target, which
239  * might use a different auth mechanism. */
240  memset(&s->auth_state, 0, sizeof(s->auth_state));
241  attempts = 0;
242  location_changed = 0;
243  goto redo;
244  }
245  return 0;
246 
247 fail:
248  if (s->hd)
249  ffurl_close(s->hd);
250  s->hd = NULL;
251  return AVERROR(EIO);
252 }
253 
254 int ff_http_do_new_request(URLContext *h, const char *uri)
255 {
256  HTTPContext *s = h->priv_data;
257  AVDictionary *options = NULL;
258  int ret;
259 
260  s->off = 0;
261  s->icy_data_read = 0;
262  av_free(s->location);
263  s->location = av_strdup(uri);
264  if (!s->location)
265  return AVERROR(ENOMEM);
266 
267  av_dict_copy(&options, s->chained_options, 0);
268  ret = http_open_cnx(h, &options);
269  av_dict_free(&options);
270  return ret;
271 }
272 
273 static int http_open(URLContext *h, const char *uri, int flags,
274  AVDictionary **options)
275 {
276  HTTPContext *s = h->priv_data;
277  int ret;
278 
279  h->is_streamed = 1;
280 
281  s->filesize = UINT64_MAX;
282  s->location = av_strdup(uri);
283  if (!s->location)
284  return AVERROR(ENOMEM);
285  if (options)
286  av_dict_copy(&s->chained_options, *options, 0);
287 
288  if (s->headers) {
289  int len = strlen(s->headers);
290  if (len < 2 || strcmp("\r\n", s->headers + len - 2))
292  "No trailing CRLF found in HTTP header.\n");
293  }
294 
295  ret = http_open_cnx(h, options);
296  if (ret < 0)
298  return ret;
299 }
300 
301 static int http_getc(HTTPContext *s)
302 {
303  int len;
304  if (s->buf_ptr >= s->buf_end) {
305  len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
306  if (len < 0) {
307  return len;
308  } else if (len == 0) {
309  return AVERROR_EOF;
310  } else {
311  s->buf_ptr = s->buffer;
312  s->buf_end = s->buffer + len;
313  }
314  }
315  return *s->buf_ptr++;
316 }
317 
318 static int http_get_line(HTTPContext *s, char *line, int line_size)
319 {
320  int ch;
321  char *q;
322 
323  q = line;
324  for (;;) {
325  ch = http_getc(s);
326  if (ch < 0)
327  return ch;
328  if (ch == '\n') {
329  /* process line */
330  if (q > line && q[-1] == '\r')
331  q--;
332  *q = '\0';
333 
334  return 0;
335  } else {
336  if ((q - line) < line_size - 1)
337  *q++ = ch;
338  }
339  }
340 }
341 
342 static int check_http_code(URLContext *h, int http_code, const char *end)
343 {
344  HTTPContext *s = h->priv_data;
345  /* error codes are 4xx and 5xx, but regard 401 as a success, so we
346  * don't abort until all headers have been parsed. */
347  if (http_code >= 400 && http_code < 600 &&
348  (http_code != 401 || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
349  (http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
350  end += strspn(end, SPACE_CHARS);
351  av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n", http_code, end);
352  return AVERROR(EIO);
353  }
354  return 0;
355 }
356 
357 static int parse_location(HTTPContext *s, const char *p)
358 {
359  char redirected_location[MAX_URL_SIZE], *new_loc;
360  ff_make_absolute_url(redirected_location, sizeof(redirected_location),
361  s->location, p);
362  new_loc = av_strdup(redirected_location);
363  if (!new_loc)
364  return AVERROR(ENOMEM);
365  av_free(s->location);
366  s->location = new_loc;
367  return 0;
368 }
369 
370 /* "bytes $from-$to/$document_size" */
371 static void parse_content_range(URLContext *h, const char *p)
372 {
373  HTTPContext *s = h->priv_data;
374  const char *slash;
375 
376  if (!strncmp(p, "bytes ", 6)) {
377  p += 6;
378  s->off = strtoull(p, NULL, 10);
379  if ((slash = strchr(p, '/')) && strlen(slash) > 0)
380  s->filesize = strtoull(slash + 1, NULL, 10);
381  }
382  h->is_streamed = 0; /* we _can_ in fact seek */
383 }
384 
385 static int parse_content_encoding(URLContext *h, const char *p)
386 {
387  if (!av_strncasecmp(p, "gzip", 4) ||
388  !av_strncasecmp(p, "deflate", 7)) {
389 #if CONFIG_ZLIB
390  HTTPContext *s = h->priv_data;
391 
392  s->compressed = 1;
393  inflateEnd(&s->inflate_stream);
394  if (inflateInit2(&s->inflate_stream, 32 + 15) != Z_OK) {
395  av_log(h, AV_LOG_WARNING, "Error during zlib initialisation: %s\n",
396  s->inflate_stream.msg);
397  return AVERROR(ENOSYS);
398  }
399  if (zlibCompileFlags() & (1 << 17)) {
401  "Your zlib was compiled without gzip support.\n");
402  return AVERROR(ENOSYS);
403  }
404 #else
406  "Compressed (%s) content, need zlib with gzip support\n", p);
407  return AVERROR(ENOSYS);
408 #endif /* CONFIG_ZLIB */
409  } else if (!av_strncasecmp(p, "identity", 8)) {
410  // The normal, no-encoding case (although servers shouldn't include
411  // the header at all if this is the case).
412  } else {
413  av_log(h, AV_LOG_WARNING, "Unknown content coding: %s\n", p);
414  return AVERROR(ENOSYS);
415  }
416  return 0;
417 }
418 
419 // Concat all Icy- header lines
420 static int parse_icy(HTTPContext *s, const char *tag, const char *p)
421 {
422  int len = 4 + strlen(p) + strlen(tag);
423  int is_first = !s->icy_metadata_headers;
424  int ret;
425 
426  av_dict_set(&s->metadata, tag, p, 0);
427 
428  if (s->icy_metadata_headers)
429  len += strlen(s->icy_metadata_headers);
430 
431  if ((ret = av_reallocp(&s->icy_metadata_headers, len)) < 0)
432  return ret;
433 
434  if (is_first)
435  *s->icy_metadata_headers = '\0';
436 
437  av_strlcatf(s->icy_metadata_headers, len, "%s: %s\n", tag, p);
438 
439  return 0;
440 }
441 
442 static int process_line(URLContext *h, char *line, int line_count,
443  int *new_location)
444 {
445  HTTPContext *s = h->priv_data;
446  char *tag, *p, *end;
447  int ret;
448 
449  /* end of header */
450  if (line[0] == '\0') {
451  s->end_header = 1;
452  return 0;
453  }
454 
455  p = line;
456  if (line_count == 0) {
457  while (!av_isspace(*p) && *p != '\0')
458  p++;
459  while (av_isspace(*p))
460  p++;
461  s->http_code = strtol(p, &end, 10);
462 
463  av_dlog(NULL, "http_code=%d\n", s->http_code);
464 
465  if ((ret = check_http_code(h, s->http_code, end)) < 0)
466  return ret;
467  } else {
468  while (*p != '\0' && *p != ':')
469  p++;
470  if (*p != ':')
471  return 1;
472 
473  *p = '\0';
474  tag = line;
475  p++;
476  while (av_isspace(*p))
477  p++;
478  if (!av_strcasecmp(tag, "Location")) {
479  if ((ret = parse_location(s, p)) < 0)
480  return ret;
481  *new_location = 1;
482  } else if (!av_strcasecmp(tag, "Content-Length") &&
483  s->filesize == UINT64_MAX) {
484  s->filesize = strtoull(p, NULL, 10);
485  } else if (!av_strcasecmp(tag, "Content-Range")) {
486  parse_content_range(h, p);
487  } else if (!av_strcasecmp(tag, "Accept-Ranges") &&
488  !strncmp(p, "bytes", 5)) {
489  h->is_streamed = 0;
490  } else if (!av_strcasecmp(tag, "Transfer-Encoding") &&
491  !av_strncasecmp(p, "chunked", 7)) {
492  s->filesize = UINT64_MAX;
493  s->chunksize = 0;
494  } else if (!av_strcasecmp(tag, "WWW-Authenticate")) {
496  } else if (!av_strcasecmp(tag, "Authentication-Info")) {
498  } else if (!av_strcasecmp(tag, "Proxy-Authenticate")) {
500  } else if (!av_strcasecmp(tag, "Connection")) {
501  if (!strcmp(p, "close"))
502  s->willclose = 1;
503  } else if (!av_strcasecmp(tag, "Content-Type")) {
504  av_free(s->mime_type);
505  s->mime_type = av_strdup(p);
506  } else if (!av_strcasecmp(tag, "Icy-MetaInt")) {
507  s->icy_metaint = strtoull(p, NULL, 10);
508  } else if (!av_strncasecmp(tag, "Icy-", 4)) {
509  if ((ret = parse_icy(s, tag, p)) < 0)
510  return ret;
511  } else if (!av_strcasecmp(tag, "Content-Encoding")) {
512  if ((ret = parse_content_encoding(h, p)) < 0)
513  return ret;
514  }
515  }
516  return 1;
517 }
518 
519 static inline int has_header(const char *str, const char *header)
520 {
521  /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
522  if (!str)
523  return 0;
524  return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
525 }
526 
527 static int http_read_header(URLContext *h, int *new_location)
528 {
529  HTTPContext *s = h->priv_data;
530  char line[MAX_URL_SIZE];
531  int err = 0;
532 
533  s->chunksize = UINT64_MAX;
534 
535  for (;;) {
536  if ((err = http_get_line(s, line, sizeof(line))) < 0)
537  return err;
538 
539  av_dlog(NULL, "header='%s'\n", line);
540 
541  err = process_line(h, line, s->line_count, new_location);
542  if (err < 0)
543  return err;
544  if (err == 0)
545  break;
546  s->line_count++;
547  }
548 
549  return err;
550 }
551 
552 static int http_connect(URLContext *h, const char *path, const char *local_path,
553  const char *hoststr, const char *auth,
554  const char *proxyauth, int *new_location)
555 {
556  HTTPContext *s = h->priv_data;
557  int post, err;
558  char headers[HTTP_HEADERS_SIZE] = "";
559  char *authstr = NULL, *proxyauthstr = NULL;
560  uint64_t off = s->off;
561  int len = 0;
562  const char *method;
563  int send_expect_100 = 0;
564 
565  /* send http header */
566  post = h->flags & AVIO_FLAG_WRITE;
567 
568  if (s->post_data) {
569  /* force POST method and disable chunked encoding when
570  * custom HTTP post data is set */
571  post = 1;
572  s->chunked_post = 0;
573  }
574 
575  if (s->method)
576  method = s->method;
577  else
578  method = post ? "POST" : "GET";
579 
580  authstr = ff_http_auth_create_response(&s->auth_state, auth,
581  local_path, method);
582  proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
583  local_path, method);
584  if (post && !s->post_data) {
585  send_expect_100 = s->send_expect_100;
586  /* The user has supplied authentication but we don't know the auth type,
587  * send Expect: 100-continue to get the 401 response including the
588  * WWW-Authenticate header, or an 100 continue if no auth actually
589  * is needed. */
590  if (auth && *auth &&
592  s->http_code != 401)
593  send_expect_100 = 1;
594  }
595 
596  /* set default headers if needed */
597  if (!has_header(s->headers, "\r\nUser-Agent: "))
598  len += av_strlcatf(headers + len, sizeof(headers) - len,
599  "User-Agent: %s\r\n", s->user_agent);
600  if (!has_header(s->headers, "\r\nAccept: "))
601  len += av_strlcpy(headers + len, "Accept: */*\r\n",
602  sizeof(headers) - len);
603  // Note: we send this on purpose even when s->off is 0 when we're probing,
604  // since it allows us to detect more reliably if a (non-conforming)
605  // server supports seeking by analysing the reply headers.
606  if (!has_header(s->headers, "\r\nRange: ") && !post) {
607  len += av_strlcatf(headers + len, sizeof(headers) - len,
608  "Range: bytes=%"PRIu64"-", s->off);
609  if (s->end_off)
610  len += av_strlcatf(headers + len, sizeof(headers) - len,
611  "%"PRId64, s->end_off - 1);
612  len += av_strlcpy(headers + len, "\r\n",
613  sizeof(headers) - len);
614  }
615  if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
616  len += av_strlcatf(headers + len, sizeof(headers) - len,
617  "Expect: 100-continue\r\n");
618 
619  if (!has_header(s->headers, "\r\nConnection: ")) {
620  if (s->multiple_requests)
621  len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
622  sizeof(headers) - len);
623  else
624  len += av_strlcpy(headers + len, "Connection: close\r\n",
625  sizeof(headers) - len);
626  }
627 
628  if (!has_header(s->headers, "\r\nHost: "))
629  len += av_strlcatf(headers + len, sizeof(headers) - len,
630  "Host: %s\r\n", hoststr);
631  if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
632  len += av_strlcatf(headers + len, sizeof(headers) - len,
633  "Content-Length: %d\r\n", s->post_datalen);
634 
635  if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
636  len += av_strlcatf(headers + len, sizeof(headers) - len,
637  "Content-Type: %s\r\n", s->content_type);
638  if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
639  len += av_strlcatf(headers + len, sizeof(headers) - len,
640  "Icy-MetaData: %d\r\n", 1);
641 
642  /* now add in custom headers */
643  if (s->headers)
644  av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
645 
646  snprintf(s->buffer, sizeof(s->buffer),
647  "%s %s HTTP/1.1\r\n"
648  "%s"
649  "%s"
650  "%s"
651  "%s%s"
652  "\r\n",
653  method,
654  path,
655  post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
656  headers,
657  authstr ? authstr : "",
658  proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
659 
660  av_freep(&authstr);
661  av_freep(&proxyauthstr);
662  if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
663  return err;
664 
665  if (s->post_data)
666  if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
667  return err;
668 
669  /* init input buffer */
670  s->buf_ptr = s->buffer;
671  s->buf_end = s->buffer;
672  s->line_count = 0;
673  s->off = 0;
674  s->icy_data_read = 0;
675  s->filesize = UINT64_MAX;
676  s->willclose = 0;
677  s->end_chunked_post = 0;
678  s->end_header = 0;
679  if (post && !s->post_data && !send_expect_100) {
680  /* Pretend that it did work. We didn't read any header yet, since
681  * we've still to send the POST data, but the code calling this
682  * function will check http_code after we return. */
683  s->http_code = 200;
684  return 0;
685  }
686 
687  /* wait for header */
688  err = http_read_header(h, new_location);
689  if (err < 0)
690  return err;
691 
692  return (off == s->off) ? 0 : UINT64_MAX;
693 }
694 
695 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
696 {
697  HTTPContext *s = h->priv_data;
698  int len;
699  /* read bytes from input buffer first */
700  len = s->buf_end - s->buf_ptr;
701  if (len > 0) {
702  if (len > size)
703  len = size;
704  memcpy(buf, s->buf_ptr, len);
705  s->buf_ptr += len;
706  } else {
707  uint64_t target_end = s->end_off ? s->end_off : s->filesize;
708  if ((!s->willclose || s->chunksize == UINT64_MAX) && s->off >= target_end)
709  return AVERROR_EOF;
710  len = ffurl_read(s->hd, buf, size);
711  if (!len && (!s->willclose || s->chunksize == UINT64_MAX) && s->off < target_end) {
712  av_log(h, AV_LOG_ERROR,
713  "Streams ends prematurly at %"PRIu64", should be %"PRIu64"\n",
714  s->off, target_end
715  );
716  return AVERROR(EIO);
717  }
718  }
719  if (len > 0) {
720  s->off += len;
721  if (s->chunksize > 0)
722  s->chunksize -= len;
723  }
724  return len;
725 }
726 
727 #if CONFIG_ZLIB
728 #define DECOMPRESS_BUF_SIZE (256 * 1024)
729 static int http_buf_read_compressed(URLContext *h, uint8_t *buf, int size)
730 {
731  HTTPContext *s = h->priv_data;
732  int ret;
733 
734  if (!s->inflate_buffer) {
735  s->inflate_buffer = av_malloc(DECOMPRESS_BUF_SIZE);
736  if (!s->inflate_buffer)
737  return AVERROR(ENOMEM);
738  }
739 
740  if (s->inflate_stream.avail_in == 0) {
741  int read = http_buf_read(h, s->inflate_buffer, DECOMPRESS_BUF_SIZE);
742  if (read <= 0)
743  return read;
744  s->inflate_stream.next_in = s->inflate_buffer;
745  s->inflate_stream.avail_in = read;
746  }
747 
748  s->inflate_stream.avail_out = size;
749  s->inflate_stream.next_out = buf;
750 
751  ret = inflate(&s->inflate_stream, Z_SYNC_FLUSH);
752  if (ret != Z_OK && ret != Z_STREAM_END)
753  av_log(h, AV_LOG_WARNING, "inflate return value: %d, %s\n",
754  ret, s->inflate_stream.msg);
755 
756  return size - s->inflate_stream.avail_out;
757 }
758 #endif /* CONFIG_ZLIB */
759 
760 static int http_read_stream(URLContext *h, uint8_t *buf, int size)
761 {
762  HTTPContext *s = h->priv_data;
763  int err, new_location;
764 
765  if (!s->hd)
766  return AVERROR_EOF;
767 
768  if (s->end_chunked_post && !s->end_header) {
769  err = http_read_header(h, &new_location);
770  if (err < 0)
771  return err;
772  }
773 
774  if (s->chunksize != UINT64_MAX) {
775  if (!s->chunksize) {
776  char line[32];
777 
778  for (;;) {
779  do {
780  if ((err = http_get_line(s, line, sizeof(line))) < 0)
781  return err;
782  } while (!*line); /* skip CR LF from last chunk */
783 
784  s->chunksize = strtoull(line, NULL, 16);
785 
786  av_log(h, AV_LOG_DEBUG,
787  "Chunked encoding data size: %"PRIu64"'\n",
788  s->chunksize);
789  if (!s->chunksize)
790  return 0;
791  else if (s->chunksize == UINT64_MAX) {
792  av_log(h, AV_LOG_ERROR, "Invalid chunk size %"PRIu64"\n",
793  s->chunksize);
794  return AVERROR_INVALIDDATA;
795  }
796  }
797  }
798  size = FFMIN(size, s->chunksize);
799  }
800 
801 #if CONFIG_ZLIB
802  if (s->compressed)
803  return http_buf_read_compressed(h, buf, size);
804 #endif /* CONFIG_ZLIB */
805  return http_buf_read(h, buf, size);
806 }
807 
808 static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
809 {
810  int pos = 0;
811  while (pos < size) {
812  int len = http_read_stream(h, buf + pos, size - pos);
813  if (len < 0)
814  return len;
815  pos += len;
816  }
817  return pos;
818 }
819 
820 static void update_metadata(HTTPContext *s, char *data)
821 {
822  char *key;
823  char *val;
824  char *end;
825  char *next = data;
826 
827  while (*next) {
828  key = next;
829  val = strstr(key, "='");
830  if (!val)
831  break;
832  end = strstr(val, "';");
833  if (!end)
834  break;
835 
836  *val = '\0';
837  *end = '\0';
838  val += 2;
839 
840  av_dict_set(&s->metadata, key, val, 0);
841 
842  next = end + 2;
843  }
844 }
845 
846 static int store_icy(URLContext *h, int size)
847 {
848  HTTPContext *s = h->priv_data;
849  /* until next metadata packet */
850  uint64_t remaining;
851 
852  if (s->icy_metaint < s->icy_data_read)
853  return AVERROR_INVALIDDATA;
854  remaining = s->icy_metaint - s->icy_data_read;
855 
856  if (!remaining) {
857  /* The metadata packet is variable sized. It has a 1 byte header
858  * which sets the length of the packet (divided by 16). If it's 0,
859  * the metadata doesn't change. After the packet, icy_metaint bytes
860  * of normal data follows. */
861  uint8_t ch;
862  int len = http_read_stream_all(h, &ch, 1);
863  if (len < 0)
864  return len;
865  if (ch > 0) {
866  char data[255 * 16 + 1];
867  int ret;
868  len = ch * 16;
869  ret = http_read_stream_all(h, data, len);
870  if (ret < 0)
871  return ret;
872  data[len + 1] = 0;
873  if ((ret = av_opt_set(s, "icy_metadata_packet", data, 0)) < 0)
874  return ret;
875  update_metadata(s, data);
876  }
877  s->icy_data_read = 0;
878  remaining = s->icy_metaint;
879  }
880 
881  return FFMIN(size, remaining);
882 }
883 
884 static int http_read(URLContext *h, uint8_t *buf, int size)
885 {
886  HTTPContext *s = h->priv_data;
887 
888  if (s->icy_metaint > 0) {
889  size = store_icy(h, size);
890  if (size < 0)
891  return size;
892  }
893 
894  size = http_read_stream(h, buf, size);
895  if (size > 0)
896  s->icy_data_read += size;
897  return size;
898 }
899 
900 /* used only when posting data */
901 static int http_write(URLContext *h, const uint8_t *buf, int size)
902 {
903  char temp[11] = ""; /* 32-bit hex + CRLF + nul */
904  int ret;
905  char crlf[] = "\r\n";
906  HTTPContext *s = h->priv_data;
907 
908  if (!s->chunked_post) {
909  /* non-chunked data is sent without any special encoding */
910  return ffurl_write(s->hd, buf, size);
911  }
912 
913  /* silently ignore zero-size data since chunk encoding that would
914  * signal EOF */
915  if (size > 0) {
916  /* upload data using chunked encoding */
917  snprintf(temp, sizeof(temp), "%x\r\n", size);
918 
919  if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
920  (ret = ffurl_write(s->hd, buf, size)) < 0 ||
921  (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
922  return ret;
923  }
924  return size;
925 }
926 
927 static int http_shutdown(URLContext *h, int flags)
928 {
929  int ret = 0;
930  char footer[] = "0\r\n\r\n";
931  HTTPContext *s = h->priv_data;
932 
933  /* signal end of chunked encoding if used */
934  if ((flags & AVIO_FLAG_WRITE) && s->chunked_post) {
935  ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
936  ret = ret > 0 ? 0 : ret;
937  s->end_chunked_post = 1;
938  }
939 
940  return ret;
941 }
942 
943 static int http_close(URLContext *h)
944 {
945  int ret = 0;
946  HTTPContext *s = h->priv_data;
947 
948 #if CONFIG_ZLIB
949  inflateEnd(&s->inflate_stream);
950  av_freep(&s->inflate_buffer);
951 #endif /* CONFIG_ZLIB */
952 
953  if (!s->end_chunked_post)
954  /* Close the write direction by sending the end of chunked encoding. */
955  ret = http_shutdown(h, h->flags);
956 
957  if (s->hd)
958  ffurl_close(s->hd);
960  return ret;
961 }
962 
963 static int64_t http_seek(URLContext *h, int64_t off, int whence)
964 {
965  HTTPContext *s = h->priv_data;
966  URLContext *old_hd = s->hd;
967  uint64_t old_off = s->off;
968  uint8_t old_buf[BUFFER_SIZE];
969  int old_buf_size, ret;
970  AVDictionary *options = NULL;
971 
972  if (whence == AVSEEK_SIZE)
973  return s->filesize;
974  else if ((whence == SEEK_CUR && off == 0) ||
975  (whence == SEEK_SET && off == s->off))
976  return s->off;
977  else if ((s->filesize == UINT64_MAX && whence == SEEK_END) || h->is_streamed)
978  return AVERROR(ENOSYS);
979 
980  /* we save the old context in case the seek fails */
981  old_buf_size = s->buf_end - s->buf_ptr;
982  memcpy(old_buf, s->buf_ptr, old_buf_size);
983  s->hd = NULL;
984  if (whence == SEEK_CUR)
985  off += s->off;
986  else if (whence == SEEK_END)
987  off += s->filesize;
988  s->off = off;
989 
990  /* if it fails, continue on old connection */
991  av_dict_copy(&options, s->chained_options, 0);
992  if ((ret = http_open_cnx(h, &options)) < 0) {
993  av_dict_free(&options);
994  memcpy(s->buffer, old_buf, old_buf_size);
995  s->buf_ptr = s->buffer;
996  s->buf_end = s->buffer + old_buf_size;
997  s->hd = old_hd;
998  s->off = old_off;
999  return ret;
1000  }
1001  av_dict_free(&options);
1002  ffurl_close(old_hd);
1003  return off;
1004 }
1005 
1007 {
1008  HTTPContext *s = h->priv_data;
1009  return ffurl_get_file_handle(s->hd);
1010 }
1011 
1012 #define HTTP_CLASS(flavor) \
1013 static const AVClass flavor ## _context_class = { \
1014  .class_name = # flavor, \
1015  .item_name = av_default_item_name, \
1016  .option = options, \
1017  .version = LIBAVUTIL_VERSION_INT, \
1018 }
1019 
1020 #if CONFIG_HTTP_PROTOCOL
1021 HTTP_CLASS(http);
1022 
1023 URLProtocol ff_http_protocol = {
1024  .name = "http",
1025  .url_open2 = http_open,
1026  .url_read = http_read,
1027  .url_write = http_write,
1028  .url_seek = http_seek,
1029  .url_close = http_close,
1030  .url_get_file_handle = http_get_file_handle,
1031  .url_shutdown = http_shutdown,
1032  .priv_data_size = sizeof(HTTPContext),
1033  .priv_data_class = &http_context_class,
1035 };
1036 #endif /* CONFIG_HTTP_PROTOCOL */
1037 
1038 #if CONFIG_HTTPS_PROTOCOL
1039 HTTP_CLASS(https);
1040 
1041 URLProtocol ff_https_protocol = {
1042  .name = "https",
1043  .url_open2 = http_open,
1044  .url_read = http_read,
1045  .url_write = http_write,
1046  .url_seek = http_seek,
1047  .url_close = http_close,
1048  .url_get_file_handle = http_get_file_handle,
1049  .url_shutdown = http_shutdown,
1050  .priv_data_size = sizeof(HTTPContext),
1051  .priv_data_class = &https_context_class,
1053 };
1054 #endif /* CONFIG_HTTPS_PROTOCOL */
1055 
1056 #if CONFIG_HTTPPROXY_PROTOCOL
1057 static int http_proxy_close(URLContext *h)
1058 {
1059  HTTPContext *s = h->priv_data;
1060  if (s->hd)
1061  ffurl_close(s->hd);
1062  return 0;
1063 }
1064 
1065 static int http_proxy_open(URLContext *h, const char *uri, int flags)
1066 {
1067  HTTPContext *s = h->priv_data;
1068  char hostname[1024], hoststr[1024];
1069  char auth[1024], pathbuf[1024], *path;
1070  char lower_url[100];
1071  int port, ret = 0, attempts = 0;
1072  HTTPAuthType cur_auth_type;
1073  char *authstr;
1074  int new_loc;
1075 
1076  h->is_streamed = 1;
1077 
1078  av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
1079  pathbuf, sizeof(pathbuf), uri);
1080  ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
1081  path = pathbuf;
1082  if (*path == '/')
1083  path++;
1084 
1085  ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
1086  NULL);
1087 redo:
1088  ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
1089  &h->interrupt_callback, NULL);
1090  if (ret < 0)
1091  return ret;
1092 
1093  authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
1094  path, "CONNECT");
1095  snprintf(s->buffer, sizeof(s->buffer),
1096  "CONNECT %s HTTP/1.1\r\n"
1097  "Host: %s\r\n"
1098  "Connection: close\r\n"
1099  "%s%s"
1100  "\r\n",
1101  path,
1102  hoststr,
1103  authstr ? "Proxy-" : "", authstr ? authstr : "");
1104  av_freep(&authstr);
1105 
1106  if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
1107  goto fail;
1108 
1109  s->buf_ptr = s->buffer;
1110  s->buf_end = s->buffer;
1111  s->line_count = 0;
1112  s->filesize = UINT64_MAX;
1113  cur_auth_type = s->proxy_auth_state.auth_type;
1114 
1115  /* Note: This uses buffering, potentially reading more than the
1116  * HTTP header. If tunneling a protocol where the server starts
1117  * the conversation, we might buffer part of that here, too.
1118  * Reading that requires using the proper ffurl_read() function
1119  * on this URLContext, not using the fd directly (as the tls
1120  * protocol does). This shouldn't be an issue for tls though,
1121  * since the client starts the conversation there, so there
1122  * is no extra data that we might buffer up here.
1123  */
1124  ret = http_read_header(h, &new_loc);
1125  if (ret < 0)
1126  goto fail;
1127 
1128  attempts++;
1129  if (s->http_code == 407 &&
1130  (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
1131  s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
1132  ffurl_close(s->hd);
1133  s->hd = NULL;
1134  goto redo;
1135  }
1136 
1137  if (s->http_code < 400)
1138  return 0;
1139  ret = AVERROR(EIO);
1140 
1141 fail:
1142  http_proxy_close(h);
1143  return ret;
1144 }
1145 
1146 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
1147 {
1148  HTTPContext *s = h->priv_data;
1149  return ffurl_write(s->hd, buf, size);
1150 }
1151 
1152 URLProtocol ff_httpproxy_protocol = {
1153  .name = "httpproxy",
1154  .url_open = http_proxy_open,
1155  .url_read = http_buf_read,
1156  .url_write = http_proxy_write,
1157  .url_close = http_proxy_close,
1158  .url_get_file_handle = http_get_file_handle,
1159  .priv_data_size = sizeof(HTTPContext),
1160  .flags = URL_PROTOCOL_FLAG_NETWORK,
1161 };
1162 #endif /* CONFIG_HTTPPROXY_PROTOCOL */
static int http_get_line(HTTPContext *s, char *line, int line_size)
Definition: http.c:318
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:2713
static void parse_content_range(URLContext *h, const char *p)
Definition: http.c:371
AVDictionary * metadata
Definition: http.c:83
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
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:552
void ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Definition: url.c:80
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
#define URL_PROTOCOL_FLAG_NETWORK
Definition: url.h:35
uint8_t * post_data
Definition: http.c:74
AVOption.
Definition: opt.h:234
#define AV_OPT_FLAG_EXPORT
The option is inteded for exporting values to the caller.
Definition: opt.h:275
HTTPAuthType
Authentication types, ordered from weakest to strongest.
Definition: httpauth.h:28
#define BUFFER_SIZE
Definition: http.c:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static int http_close(URLContext *h)
Definition: http.c:943
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:276
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:54
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:48
AVIOInterruptCB interrupt_callback
Definition: url.h:50
HTTPAuthState proxy_auth_state
Definition: http.c:59
char * icy_metadata_headers
Definition: http.c:81
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:295
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:166
uint64_t icy_metaint
Definition: http.c:80
int flags
Definition: url.h:46
char * content_type
Definition: http.c:63
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
uint64_t filesize
Definition: http.c:56
static int http_getc(HTTPContext *s)
Definition: http.c:301
HTTP Authentication state structure.
Definition: httpauth.h:55
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
#define MAX_URL_SIZE
Definition: internal.h:27
int end_chunked_post
Definition: http.c:69
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:43
static int http_get_file_handle(URLContext *h)
Definition: http.c:1006
static int http_buf_read(URLContext *h, uint8_t *buf, int size)
Definition: http.c:695
uint8_t
int post_datalen
Definition: http.c:75
AVOptions.
miscellaneous OS support macros and functions.
static int http_read_header(URLContext *h, int *new_location)
Definition: http.c:527
#define HTTP_HEADERS_SIZE
Definition: http.h:27
static int http_open_cnx(URLContext *h, AVDictionary **options)
Definition: http.c:198
const char data[16]
Definition: mxf.c:70
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
static int flags
Definition: log.c:44
uint32_t tag
Definition: movenc.c:844
#define DEFAULT_USER_AGENT
Definition: http.c:97
#define AVERROR_EOF
End of file.
Definition: error.h:51
void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
Initialize the authentication state based on another HTTP URLContext.
Definition: http.c:127
uint64_t icy_data_read
Definition: http.c:78
HTTP 1.0 Basic auth from RFC 1945 (also in RFC 2617)
Definition: httpauth.h:30
static int http_open_cnx_internal(URLContext *h, AVDictionary **options)
Definition: http.c:137
int line_count
Definition: http.c:52
void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:184
unsigned char * buf_end
Definition: http.c:51
static void update_metadata(HTTPContext *s, char *data)
Definition: http.c:820
int chunked_post
Definition: http.c:67
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define E
Definition: http.c:96
#define AVERROR(e)
Definition: error.h:43
static int http_write(URLContext *h, const uint8_t *buf, int size)
Definition: http.c:901
static const AVOption options[]
Definition: http.c:99
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:170
Definition: graph2dot.c:49
uint64_t off
Definition: http.c:56
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
#define OFFSET(x)
Definition: http.c:94
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.c:225
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:81
uint64_t chunksize
Definition: http.c:55
AVDictionary * chained_options
Definition: http.c:89
static int store_icy(URLContext *h, int size)
Definition: http.c:846
char * headers
Definition: http.c:60
char * user_agent
Definition: http.c:62
char * icy_metadata_packet
Definition: http.c:82
#define FFMIN(a, b)
Definition: common.h:57
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:156
int send_expect_100
Definition: http.c:90
int willclose
Definition: http.c:66
int ff_http_match_no_proxy(const char *no_proxy, const char *hostname)
Definition: network.c:329
static char buffer[20]
Definition: seek-test.c:31
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition: avio.c:352
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:226
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
NULL
Definition: eval.c:55
static int http_read(URLContext *h, uint8_t *buf, int size)
Definition: http.c:884
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:213
Definition: url.h:41
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition: avio.h:296
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:68
unsigned char buffer[BUFFER_SIZE]
Definition: http.c:51
Describe the class of an AVClass context structure.
Definition: log.h:33
#define SPACE_CHARS
Definition: internal.h:160
void * priv_data
Definition: url.h:44
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:99
int icy
Definition: http.c:76
char * ff_http_auth_create_response(HTTPAuthState *state, const char *auth, const char *path, const char *method)
Definition: httpauth.c:242
const char * name
Definition: url.h:54
URLContext * hd
Definition: http.c:50
static int has_header(const char *str, const char *header)
Definition: http.c:519
char * mime_type
Definition: http.c:61
int ffurl_close(URLContext *h)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition: avio.c:297
static int process_line(URLContext *h, char *line, int line_count, int *new_location)
Definition: http.c:442
int ff_http_do_new_request(URLContext *h, const char *uri)
Send a new HTTP request, reusing the old connection.
Definition: http.c:254
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:32
Main libavformat public API header.
#define MAX_REDIRECTS
Definition: http.c:46
static int parse_content_encoding(URLContext *h, const char *p)
Definition: http.c:385
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:211
int http_code
Definition: http.c:53
static int http_open(URLContext *h, const char *uri, int flags, AVDictionary **options)
Definition: http.c:273
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition: avio.h:190
void ff_http_auth_handle_header(HTTPAuthState *state, const char *key, const char *value)
Definition: httpauth.c:90
int multiple_requests
Definition: http.c:73
HTTPAuthState auth_state
Definition: http.c:58
int len
static int parse_location(HTTPContext *s, const char *p)
Definition: http.c:357
char * location
Definition: http.c:57
static int http_shutdown(URLContext *h, int flags)
Definition: http.c:927
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition: opt.h:280
int end_header
Definition: http.c:71
unsigned char * buf_ptr
Definition: http.c:51
#define D
Definition: http.c:95
char * method
Definition: http.c:91
static int parse_icy(HTTPContext *s, const char *tag, const char *p)
Definition: http.c:420
uint64_t end_off
Definition: http.c:56
HTTPAuthType auth_type
The currently chosen auth type.
Definition: httpauth.h:59
static int http_read_stream(URLContext *h, uint8_t *buf, int size)
Definition: http.c:760
static int http_read_stream_all(URLContext *h, uint8_t *buf, int size)
Definition: http.c:808
unbuffered private I/O API
static int64_t http_seek(URLContext *h, int64_t off, int whence)
Definition: http.c:963
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:212
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:262
#define HTTP_CLASS(flavor)
Definition: http.c:1012
No authentication specified.
Definition: httpauth.h:29
static int check_http_code(URLContext *h, int http_code, const char *end)
Definition: http.c:342