FFmpeg
Data Structures | Macros | Enumerations | Functions | Variables
http.c File Reference
#include "config.h"
#include "config_components.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/bprint.h"
#include "libavutil/getenv_utf8.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include "libavutil/parseutils.h"
#include "avformat.h"
#include "http.h"
#include "httpauth.h"
#include "internal.h"
#include "network.h"
#include "os_support.h"
#include "url.h"
#include "version.h"

Go to the source code of this file.

Data Structures

struct  HTTPContext
 

Macros

#define BUFFER_SIZE   (MAX_URL_SIZE + HTTP_HEADERS_SIZE)
 
#define MAX_REDIRECTS   8
 
#define MAX_CACHED_REDIRECTS   32
 
#define HTTP_SINGLE   1
 
#define HTTP_MUTLI   2
 
#define MAX_EXPIRY   19
 
#define WHITESPACES   " \n\t\r"
 
#define OFFSET(x)   offsetof(HTTPContext, x)
 
#define D   AV_OPT_FLAG_DECODING_PARAM
 
#define E   AV_OPT_FLAG_ENCODING_PARAM
 
#define DEFAULT_USER_AGENT   "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)
 
#define NEEDS_ESCAPE(ch)
 
#define HTTP_CLASS(flavor)
 

Enumerations

enum  HandshakeState { LOWER_PROTO, READ_HEADERS, WRITE_REPLY_HEADERS, FINISH }
 

Functions

static int http_connect (URLContext *h, const char *path, const char *local_path, const char *hoststr, const char *auth, const char *proxyauth)
 
static int http_read_header (URLContext *h)
 
static int http_shutdown (URLContext *h, int flags)
 
void ff_http_init_auth_state (URLContext *dest, const URLContext *src)
 Initialize the authentication state based on another HTTP URLContext. More...
 
static int http_open_cnx_internal (URLContext *h, AVDictionary **options)
 
static int http_should_reconnect (HTTPContext *s, int err)
 
static char * redirect_cache_get (HTTPContext *s)
 
static int redirect_cache_set (HTTPContext *s, const char *source, const char *dest, int64_t expiry)
 
static int http_open_cnx (URLContext *h, AVDictionary **options)
 
int ff_http_do_new_request (URLContext *h, const char *uri)
 Send a new HTTP request, reusing the old connection. More...
 
int ff_http_do_new_request2 (URLContext *h, const char *uri, AVDictionary **opts)
 Send a new HTTP request, reusing the old connection. More...
 
int ff_http_averror (int status_code, int default_averror)
 
static int http_write_reply (URLContext *h, int status_code)
 
static void handle_http_errors (URLContext *h, int error)
 
static int http_handshake (URLContext *c)
 
static int http_listen (URLContext *h, const char *uri, int flags, AVDictionary **options)
 
static int http_open (URLContext *h, const char *uri, int flags, AVDictionary **options)
 
static int http_accept (URLContext *s, URLContext **c)
 
static int http_getc (HTTPContext *s)
 
static int http_get_line (HTTPContext *s, char *line, int line_size)
 
static int check_http_code (URLContext *h, int http_code, const char *end)
 
static int parse_location (HTTPContext *s, const char *p)
 
static void parse_content_range (URLContext *h, const char *p)
 
static int parse_content_encoding (URLContext *h, const char *p)
 
static int parse_icy (HTTPContext *s, const char *tag, const char *p)
 
static int parse_set_cookie_expiry_time (const char *exp_str, struct tm *buf)
 
static int parse_set_cookie (const char *set_cookie, AVDictionary **dict)
 
static int parse_cookie (HTTPContext *s, const char *p, AVDictionary **cookies)
 
static int cookie_string (AVDictionary *dict, char **cookies)
 
static void parse_expires (HTTPContext *s, const char *p)
 
static void parse_cache_control (HTTPContext *s, const char *p)
 
static int process_line (URLContext *h, char *line, int line_count)
 
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 path and domain from the cookie values stored in the HTTP protocol context. More...
 
static int has_header (const char *str, const char *header)
 
static void bprint_escaped_path (AVBPrint *bp, const char *path)
 Escape unsafe characters in path in order to pass them safely to the HTTP request. More...
 
static int http_buf_read (URLContext *h, uint8_t *buf, int size)
 
static int64_t http_seek_internal (URLContext *h, int64_t off, int whence, int force_reconnect)
 
static int http_read_stream (URLContext *h, uint8_t *buf, int size)
 
static int http_read_stream_all (URLContext *h, uint8_t *buf, int size)
 
static void update_metadata (URLContext *h, char *data)
 
static int store_icy (URLContext *h, int size)
 
static int http_read (URLContext *h, uint8_t *buf, int size)
 
static int http_write (URLContext *h, const uint8_t *buf, int size)
 
static int http_close (URLContext *h)
 
static int64_t http_seek (URLContext *h, int64_t off, int whence)
 
static int http_get_file_handle (URLContext *h)
 
static int http_get_short_seek (URLContext *h)
 

Variables

static const AVOption options []
 

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   (MAX_URL_SIZE + HTTP_HEADERS_SIZE)

Definition at line 52 of file http.c.

◆ MAX_REDIRECTS

#define MAX_REDIRECTS   8

Definition at line 53 of file http.c.

◆ MAX_CACHED_REDIRECTS

#define MAX_CACHED_REDIRECTS   32

Definition at line 54 of file http.c.

◆ HTTP_SINGLE

#define HTTP_SINGLE   1

Definition at line 55 of file http.c.

◆ HTTP_MUTLI

#define HTTP_MUTLI   2

Definition at line 56 of file http.c.

◆ MAX_EXPIRY

#define MAX_EXPIRY   19

Definition at line 57 of file http.c.

◆ WHITESPACES

#define WHITESPACES   " \n\t\r"

Definition at line 58 of file http.c.

◆ OFFSET

#define OFFSET (   x)    offsetof(HTTPContext, x)

Definition at line 141 of file http.c.

◆ D

Definition at line 142 of file http.c.

◆ E

Definition at line 143 of file http.c.

◆ DEFAULT_USER_AGENT

#define DEFAULT_USER_AGENT   "Lavf/" AV_STRINGIFY(LIBAVFORMAT_VERSION)

Definition at line 144 of file http.c.

◆ NEEDS_ESCAPE

#define NEEDS_ESCAPE (   ch)
Value:
((ch) <= ' ' || (ch) >= '\x7f' || \
(ch) == '"' || (ch) == '%' || (ch) == '<' || (ch) == '>' || (ch) == '\\' || \
(ch) == '^' || (ch) == '`' || (ch) == '{' || (ch) == '}' || (ch) == '|')

◆ HTTP_CLASS

#define HTTP_CLASS (   flavor)
Value:
static const AVClass flavor ## _context_class = { \
.class_name = # flavor, \
.item_name = av_default_item_name, \
.option = options, \
.version = LIBAVUTIL_VERSION_INT, \
}

Definition at line 1985 of file http.c.

Enumeration Type Documentation

◆ HandshakeState

Enumerator
LOWER_PROTO 
READ_HEADERS 
WRITE_REPLY_HEADERS 
FINISH 

Definition at line 59 of file http.c.

Function Documentation

◆ http_connect()

static int http_connect ( URLContext h,
const char *  path,
const char *  local_path,
const char *  hoststr,
const char *  auth,
const char *  proxyauth 
)
static

Definition at line 1399 of file http.c.

Referenced by http_open_cnx_internal().

◆ http_read_header()

static int http_read_header ( URLContext h)
static

Definition at line 1329 of file http.c.

Referenced by http_connect(), http_handshake(), and http_read_stream().

◆ http_shutdown()

static int http_shutdown ( URLContext h,
int  flags 
)
static

Definition at line 1845 of file http.c.

Referenced by ff_http_do_new_request2(), and http_close().

◆ ff_http_init_auth_state()

void ff_http_init_auth_state ( URLContext dest,
const URLContext src 
)

Initialize the authentication state based on another HTTP URLContext.

This can be used to pre-initialize the authentication parameters if they are known beforehand, to avoid having to do an initial failing request just to get the parameters.

Parameters
destURL context whose authentication state gets updated
srcURL context whose authentication state gets copied

Definition at line 190 of file http.c.

◆ http_open_cnx_internal()

static int http_open_cnx_internal ( URLContext h,
AVDictionary **  options 
)
static

Definition at line 200 of file http.c.

Referenced by http_open_cnx().

◆ http_should_reconnect()

static int http_should_reconnect ( HTTPContext s,
int  err 
)
static

Definition at line 277 of file http.c.

Referenced by http_open_cnx().

◆ redirect_cache_get()

static char* redirect_cache_get ( HTTPContext s)
static

Definition at line 310 of file http.c.

Referenced by http_open_cnx().

◆ redirect_cache_set()

static int redirect_cache_set ( HTTPContext s,
const char *  source,
const char *  dest,
int64_t  expiry 
)
static

Definition at line 334 of file http.c.

Referenced by http_open_cnx().

◆ http_open_cnx()

static int http_open_cnx ( URLContext h,
AVDictionary **  options 
)
static

Definition at line 352 of file http.c.

Referenced by ff_http_do_new_request2(), http_open(), and http_seek_internal().

◆ ff_http_do_new_request()

int ff_http_do_new_request ( URLContext h,
const char *  uri 
)

Send a new HTTP request, reusing the old connection.

Parameters
hpointer to the resource
uriuri used to perform the request
Returns
a negative value if an error condition occurred, 0 otherwise

Definition at line 452 of file http.c.

Referenced by dashenc_io_open(), hlsenc_io_open(), and rtmp_http_send_cmd().

◆ ff_http_do_new_request2()

int ff_http_do_new_request2 ( URLContext h,
const char *  uri,
AVDictionary **  options 
)

Send a new HTTP request, reusing the old connection.

Parameters
hpointer to the resource
uriuri used to perform the request
optionsA dictionary filled with HTTP options. On return this parameter will be destroyed and replaced with a dict containing options that were not found. May be NULL.
Returns
a negative value if an error condition occurred, 0 otherwise

Definition at line 456 of file http.c.

Referenced by ff_http_do_new_request(), and open_url_keepalive().

◆ ff_http_averror()

int ff_http_averror ( int  status_code,
int  default_averror 
)

Definition at line 516 of file http.c.

Referenced by check_http_code(), ff_rtsp_averror(), http_open_cnx(), and process_line().

◆ http_write_reply()

static int http_write_reply ( URLContext h,
int  status_code 
)
static

Definition at line 533 of file http.c.

Referenced by handle_http_errors(), and http_handshake().

◆ handle_http_errors()

static void handle_http_errors ( URLContext h,
int  error 
)
static

Definition at line 607 of file http.c.

Referenced by http_handshake().

◆ http_handshake()

static int http_handshake ( URLContext c)
static

Definition at line 613 of file http.c.

Referenced by http_listen().

◆ http_listen()

static int http_listen ( URLContext h,
const char *  uri,
int  flags,
AVDictionary **  options 
)
static

Definition at line 649 of file http.c.

Referenced by http_open().

◆ http_open()

static int http_open ( URLContext h,
const char *  uri,
int  flags,
AVDictionary **  options 
)
static

Definition at line 681 of file http.c.

◆ http_accept()

static int http_accept ( URLContext s,
URLContext **  c 
)
static

Definition at line 734 of file http.c.

◆ http_getc()

static int http_getc ( HTTPContext s)
static

Definition at line 758 of file http.c.

Referenced by http_get_line().

◆ http_get_line()

static int http_get_line ( HTTPContext s,
char *  line,
int  line_size 
)
static

Definition at line 775 of file http.c.

Referenced by http_buf_read(), and http_read_header().

◆ check_http_code()

static int check_http_code ( URLContext h,
int  http_code,
const char *  end 
)
static

Definition at line 799 of file http.c.

Referenced by process_line().

◆ parse_location()

static int parse_location ( HTTPContext s,
const char *  p 
)
static

Definition at line 814 of file http.c.

Referenced by process_line().

◆ parse_content_range()

static void parse_content_range ( URLContext h,
const char *  p 
)
static

Definition at line 827 of file http.c.

Referenced by process_line().

◆ parse_content_encoding()

static int parse_content_encoding ( URLContext h,
const char *  p 
)
static

Definition at line 842 of file http.c.

Referenced by process_line().

◆ parse_icy()

static int parse_icy ( HTTPContext s,
const char *  tag,
const char *  p 
)
static

Definition at line 876 of file http.c.

Referenced by process_line().

◆ parse_set_cookie_expiry_time()

static int parse_set_cookie_expiry_time ( const char *  exp_str,
struct tm *  buf 
)
static

Definition at line 898 of file http.c.

Referenced by get_cookies(), parse_cookie(), and parse_expires().

◆ parse_set_cookie()

static int parse_set_cookie ( const char *  set_cookie,
AVDictionary **  dict 
)
static

Definition at line 923 of file http.c.

Referenced by get_cookies(), and parse_cookie().

◆ parse_cookie()

static int parse_cookie ( HTTPContext s,
const char *  p,
AVDictionary **  cookies 
)
static

Definition at line 960 of file http.c.

Referenced by get_cookies(), and process_line().

◆ cookie_string()

static int cookie_string ( AVDictionary dict,
char **  cookies 
)
static

Definition at line 1022 of file http.c.

Referenced by http_read_header().

◆ parse_expires()

static void parse_expires ( HTTPContext s,
const char *  p 
)
static

Definition at line 1045 of file http.c.

Referenced by process_line().

◆ parse_cache_control()

static void parse_cache_control ( HTTPContext s,
const char *  p 
)
static

Definition at line 1054 of file http.c.

Referenced by process_line().

◆ process_line()

static int process_line ( URLContext h,
char *  line,
int  line_count 
)
static

Definition at line 1081 of file http.c.

Referenced by http_read_header().

◆ get_cookies()

static int get_cookies ( HTTPContext s,
char **  cookies,
const char *  path,
const char *  domain 
)
static

Create a string containing cookie values for use as a HTTP cookie header field value for a particular path and domain from the cookie values stored in the HTTP protocol context.

The cookie string is stored in *cookies, and may be NULL if there are no valid cookies.

Returns
a negative value if an error condition occurred, 0 otherwise

Definition at line 1237 of file http.c.

Referenced by http_connect().

◆ has_header()

static int has_header ( const char *  str,
const char *  header 
)
inlinestatic

Definition at line 1321 of file http.c.

Referenced by http_connect().

◆ bprint_escaped_path()

static void bprint_escaped_path ( AVBPrint *  bp,
const char *  path 
)
static

Escape unsafe characters in path in order to pass them safely to the HTTP request.

Insipred by the algorithm in GNU wget:

  • escape "%" characters not followed by two hex digits
  • escape all "unsafe" characters except which are also "reserved"
  • pass through everything else

Definition at line 1375 of file http.c.

Referenced by http_connect().

◆ http_buf_read()

static int http_buf_read ( URLContext h,
uint8_t *  buf,
int  size 
)
static

Definition at line 1559 of file http.c.

Referenced by http_read_stream().

◆ http_seek_internal()

static int64_t http_seek_internal ( URLContext h,
int64_t  off,
int  whence,
int  force_reconnect 
)
static

Definition at line 1898 of file http.c.

Referenced by http_read_stream(), and http_seek().

◆ http_read_stream()

static int http_read_stream ( URLContext h,
uint8_t *  buf,
int  size 
)
static

Definition at line 1668 of file http.c.

Referenced by http_read(), and http_read_stream_all().

◆ http_read_stream_all()

static int http_read_stream_all ( URLContext h,
uint8_t *  buf,
int  size 
)
static

Definition at line 1724 of file http.c.

Referenced by store_icy().

◆ update_metadata()

static void update_metadata ( URLContext h,
char *  data 
)
static

Definition at line 1736 of file http.c.

Referenced by store_icy().

◆ store_icy()

static int store_icy ( URLContext h,
int  size 
)
static

Definition at line 1764 of file http.c.

Referenced by http_read().

◆ http_read()

static int http_read ( URLContext h,
uint8_t *  buf,
int  size 
)
static

Definition at line 1802 of file http.c.

◆ http_write()

static int http_write ( URLContext h,
const uint8_t *  buf,
int  size 
)
static

Definition at line 1819 of file http.c.

◆ http_close()

static int http_close ( URLContext h)
static

Definition at line 1874 of file http.c.

◆ http_seek()

static int64_t http_seek ( URLContext h,
int64_t  off,
int  whence 
)
static

Definition at line 1966 of file http.c.

◆ http_get_file_handle()

static int http_get_file_handle ( URLContext h)
static

Definition at line 1971 of file http.c.

◆ http_get_short_seek()

static int http_get_short_seek ( URLContext h)
static

Definition at line 1977 of file http.c.

Variable Documentation

◆ options

const AVOption options[]
static
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
options
static const AVOption options[]
Definition: http.c:146
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71