TSDuck Version 3.16-1064 (TSDuck - The MPEG Transport Stream Toolkit)
ts::WebRequest Class Reference

Perform a simple Web request (HTTP, HTTPS, FTP). More...

Public Types

typedef std::multimap< UString, UStringHeadersMap
 Representation of request or reponse headers. More...
 

Public Member Functions

 WebRequest (Report &report)
 Constructor. More...
 
virtual ~WebRequest ()
 Destructor.
 
void clearRequestHeaders ()
 Clear all headers which will be sent with the request.
 
size_t contentSize () const
 Get the size in bytes of the downloaded content. More...
 
bool downloadBinaryContent (ByteBlock &data)
 Download the content of the URL as binary data. More...
 
bool downloadFile (const UString &fileName)
 Download the content of the URL in a file. More...
 
bool downloadTextContent (UString &text)
 Download the content of the URL as text. More...
 
bool downloadToApplication (WebRequestHandlerInterface *handler)
 Download the content of the URL and pass data to the application. More...
 
UString finalURL () const
 Get the final URL of the actual download operation. More...
 
void getResponseHeaders (HeadersMap &headers) const
 Get all response headers. More...
 
int httpStatus () const
 Get the HTTP status code (200, 404, etc). More...
 
UString mimeType (bool simple=true, bool lowercase=true) const
 Get the MIME type in the response headers. More...
 
UString originalURL () const
 Get the original URL, as set by setURL(). More...
 
UString reponseHeader (const UString &name) const
 Get the value of one header. More...
 
void setArgs (const WebRequestArgs &args)
 Set various arguments from command line. More...
 
void setAutoRedirect (bool on)
 Enable or disable the automatic redirection of HTTP requests. More...
 
void setConnectionTimeout (MilliSecond timeout)
 Set the connection timeout for this request. More...
 
void setProxyHost (const UString &host, uint16_t port)
 Set the optional proxy host and port for this request. More...
 
void setProxyUser (const UString &user, const UString &password)
 Set the optional proxy authentication for this request. More...
 
void setReceiveTimeout (MilliSecond timeout)
 Set the timeout for each receive operation. More...
 
void setRequestHeader (const UString &name, const UString &value)
 Set a header which will be sent with the request. More...
 
void setURL (const UString &url)
 Set the URL to get. More...
 
void setUserAgent (const UString &name)
 Set the user agent name to use in HTTP headers. More...
 

Static Public Member Functions

static UString GetLibraryVersion ()
 Get the version of the underlying HTTP library. More...
 
static void SetDefaultProxyHost (const UString &host, uint16_t port)
 Set the default proxy host and port for all subsequent requests. More...
 
static void SetDefaultProxyUser (const UString &user, const UString &password)
 Set the default proxy authentication for all subsequent requests. More...
 

Detailed Description

Perform a simple Web request (HTTP, HTTPS, FTP).

On UNIX systems, the implementation uses libcurl. On Windows systems, the implementation uses Microsoft Wininet. We could have used libcurl on Windows but building it was a pain...

The URL and optional proxy settings must be set before any download operation. By default, no proxy is used. On Windows, if no proxy is set, the default system proxy is used.

The response headers are available after a successful download operation.

Member Typedef Documentation

◆ HeadersMap

typedef std::multimap<UString,UString> ts::WebRequest::HeadersMap

Representation of request or reponse headers.

The keys of the map are the header names.

Constructor & Destructor Documentation

◆ WebRequest()

ts::WebRequest::WebRequest ( Report report)

Constructor.

Parameters
[in,out]reportWhere to report errors.

Member Function Documentation

◆ setURL()

void ts::WebRequest::setURL ( const UString url)

Set the URL to get.

Parameters
[in]urlThe complete URL to fetch.

◆ originalURL()

UString ts::WebRequest::originalURL ( ) const
inline

Get the original URL, as set by setURL().

Returns
The original URL.

◆ finalURL()

UString ts::WebRequest::finalURL ( ) const
inline

Get the final URL of the actual download operation.

It can be different from originalURL() if some HTTP redirections were performed. When called before a download operation, return originalURL().

If redirections are disabled using setAutoRedirect() and the site returned a redirection, finalURL() returns the redirected URL.

Returns
The final / redirected URL.

◆ setConnectionTimeout()

void ts::WebRequest::setConnectionTimeout ( MilliSecond  timeout)

Set the connection timeout for this request.

Parameters
[in]timeoutConnection timeout in milliseconds.

◆ setReceiveTimeout()

void ts::WebRequest::setReceiveTimeout ( MilliSecond  timeout)

Set the timeout for each receive operation.

Parameters
[in]timeoutReception timeout in milliseconds.

◆ setProxyHost()

void ts::WebRequest::setProxyHost ( const UString host,
uint16_t  port 
)

Set the optional proxy host and port for this request.

Parameters
[in]hostProxy host name or address.
[in]portProxy port number.

◆ setProxyUser()

void ts::WebRequest::setProxyUser ( const UString user,
const UString password 
)

Set the optional proxy authentication for this request.

Parameters
[in]userProxy user name.
[in]passwordProxy user's password.

◆ SetDefaultProxyHost()

static void ts::WebRequest::SetDefaultProxyHost ( const UString host,
uint16_t  port 
)
static

Set the default proxy host and port for all subsequent requests.

Parameters
[in]hostProxy host name or address.
[in]portProxy port number.

◆ SetDefaultProxyUser()

static void ts::WebRequest::SetDefaultProxyUser ( const UString user,
const UString password 
)
static

Set the default proxy authentication for all subsequent requests.

Parameters
[in]userProxy user name.
[in]passwordProxy user's password.

◆ setUserAgent()

void ts::WebRequest::setUserAgent ( const UString name)
inline

Set the user agent name to use in HTTP headers.

Parameters
[in]nameThe user agent name.

◆ setAutoRedirect()

void ts::WebRequest::setAutoRedirect ( bool  on)
inline

Enable or disable the automatic redirection of HTTP requests.

This option is active by default.

Parameters
[in]onIf true, allow automatic redirection of HTTP requests.

◆ setArgs()

void ts::WebRequest::setArgs ( const WebRequestArgs args)

Set various arguments from command line.

Parameters
[in]argsCommand line arguments.

◆ setRequestHeader()

void ts::WebRequest::setRequestHeader ( const UString name,
const UString value 
)

Set a header which will be sent with the request.

Parameters
[in]nameThe header name.
[in]valueThe header value.

◆ downloadBinaryContent()

bool ts::WebRequest::downloadBinaryContent ( ByteBlock data)

Download the content of the URL as binary data.

Parameters
[out]dataThe content of the URL.
Returns
True on success, false on error.

◆ downloadTextContent()

bool ts::WebRequest::downloadTextContent ( UString text)

Download the content of the URL as text.

The downloaded text is converted from UTF-8. End of lines are normalized as LF.

Parameters
[out]textThe content of the URL.
Returns
True on success, false on error.

◆ downloadFile()

bool ts::WebRequest::downloadFile ( const UString fileName)

Download the content of the URL in a file.

No transformation is applied to the data.

Parameters
[in]fileNameName of the file to create.
Returns
True on success, false on error.

◆ downloadToApplication()

bool ts::WebRequest::downloadToApplication ( WebRequestHandlerInterface handler)

Download the content of the URL and pass data to the application.

No transformation is applied to the data.

Parameters
[in]handlerApplication-defined handler to process received data.
Returns
True on success, false on error.

◆ getResponseHeaders()

void ts::WebRequest::getResponseHeaders ( HeadersMap headers) const

Get all response headers.

Parameters
[out]headersA multimap of all response headers.

◆ reponseHeader()

UString ts::WebRequest::reponseHeader ( const UString name) const

Get the value of one header.

Parameters
[in]nameHeader name, case sensitive.
Returns
Header value or an empty string when the header is not found. If the header is present more than once, the first value is returned.

◆ mimeType()

UString ts::WebRequest::mimeType ( bool  simple = true,
bool  lowercase = true 
) const

Get the MIME type in the response headers.

Parameters
[in]simpleIf true, simple type name. If false, return the full specification with options.
[in]lowercaseForce lowercase in the result.
Returns
The MIME type.

◆ contentSize()

size_t ts::WebRequest::contentSize ( ) const
inline

Get the size in bytes of the downloaded content.

Returns
Size in bytes of the downloaded content.

◆ httpStatus()

int ts::WebRequest::httpStatus ( ) const
inline

Get the HTTP status code (200, 404, etc).

Returns
The HTTP status code.

◆ GetLibraryVersion()

static UString ts::WebRequest::GetLibraryVersion ( )
static

Get the version of the underlying HTTP library.

Returns
The library version.

The documentation for this class was generated from the following file: