TSDuck v3.40-4025
MPEG Transport Stream Toolkit
|
Perform a simple Web request (HTTP, HTTPS, FTP). More...
#include <tsWebRequest.h>
Public Types | |
using | HeadersMap = std::multimap< UString, UString > |
Representation of request or reponse headers. | |
Public Member Functions | |
WebRequest (Report &report) | |
Constructor. | |
virtual | ~WebRequest () |
Destructor. | |
void | abort () |
Abort a transfer in progress. | |
size_t | announdedContentSize () const |
Get the announced content size in bytes. | |
void | clearRequestHeaders () |
Clear all headers which will be sent with the request. | |
bool | close () |
Close the transfer. | |
size_t | contentSize () const |
Get the size in bytes of the downloaded content. | |
bool | deleteCookiesFile () const |
Delete the cookies file, if one was defined. | |
void | disableCookies () |
Disable the use of cookies for all requests. | |
bool | downloadBinaryContent (const UString &url, ByteBlock &data, size_t chunkSize=DEFAULT_CHUNK_SIZE) |
Download the content of the URL as binary data in one operation. | |
bool | downloadFile (const UString &url, const fs::path &fileName, size_t chunkSize=DEFAULT_CHUNK_SIZE) |
Download the content of the URL in a file in one operation. | |
bool | downloadTextContent (const UString &url, UString &text, size_t chunkSize=DEFAULT_CHUNK_SIZE) |
Download the content of the URL as text in one operation. | |
void | enableCompression (bool on=true) |
Enable compression. | |
void | enableCookies (const fs::path &fileName=fs::path()) |
Enable the use of cookies for all requests using this instance. | |
UString | finalURL () const |
Get the final URL of the actual download operation. | |
fs::path | getCookiesFileName () const |
Get the file name to use for cookies for all requests using this instance. | |
void | getResponseHeaders (HeadersMap &headers) const |
Get all response headers. | |
int | httpStatus () const |
Get the HTTP status code (200, 404, etc). | |
bool | isOpen () const |
Check if a transfer is open. | |
UString | mimeType (bool simple=true, bool lowercase=true) const |
Get the MIME type in the response headers. | |
bool | open (const UString &url) |
Open an URL and start the transfer. | |
UString | originalURL () const |
Get the original URL, as set by setURL(). | |
const UString & | proxyHost () const |
Get the current actual proxy host. | |
const UString & | proxyPassword () const |
Get the current actual proxy user password. | |
uint16_t | proxyPort () const |
Get the current actual proxy port number. | |
const UString & | proxyUser () const |
Get the current actual proxy user name. | |
bool | receive (void *buffer, size_t maxSize, size_t &retSize) |
Receive data. | |
UString | reponseHeader (const UString &name) const |
Get the value of one header. | |
void | setArgs (const WebRequestArgs &args) |
Set various arguments from command line. | |
void | setAutoRedirect (bool on) |
Enable or disable the automatic redirection of HTTP requests. | |
void | setConnectionTimeout (cn::milliseconds timeout) |
Set the connection timeout for this request. | |
void | setProxyHost (const UString &host, uint16_t port) |
Set the optional proxy host and port for this request. | |
void | setProxyUser (const UString &user, const UString &password) |
Set the optional proxy authentication for this request. | |
void | setReceiveTimeout (cn::milliseconds timeout) |
Set the timeout for each receive operation. | |
void | setRequestHeader (const UString &name, const UString &value) |
Set a header which will be sent with the request. | |
void | setUserAgent (const UString &name=UString()) |
Set the user agent name to use in HTTP headers. | |
const UString & | userAgent () const |
Get the current user agent name to use in HTTP headers. | |
Static Public Member Functions | |
static UString | GetLibraryVersion () |
Get the version of the underlying HTTP library. | |
static void | SetDefaultProxyHost (const UString &host, uint16_t port) |
Set the default proxy host and port for all subsequent requests. | |
static void | SetDefaultProxyUser (const UString &user, const UString &password) |
Set the default proxy authentication for all subsequent requests. | |
Static Public Attributes | |
static constexpr size_t | DEFAULT_CHUNK_SIZE = 64 * 1024 |
Default download chunk size for bulk transfers. | |
static constexpr const UChar * | DEFAULT_USER_AGENT = u"tsduck" |
Default user agent string ("tsduck"). | |
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 proxy and transfer settings must be set before starting any download operation. The HTTP status and the response headers are available after a successful download start.
By default, no proxy is used. If no proxy is set, the default proxy is used (system configuration on Windows, http_proxy environment on Unix systems).
using ts::WebRequest::HeadersMap = std::multimap<UString,UString> |
Representation of request or reponse headers.
The keys of the map are the header names.
ts::WebRequest::WebRequest | ( | Report & | report | ) |
Constructor.
[in,out] | report | Where to report errors. |
|
inline |
Set the connection timeout for this request.
[in] | timeout | Connection timeout in milliseconds. |
|
inline |
Set the timeout for each receive operation.
[in] | timeout | Reception timeout in milliseconds. |
void ts::WebRequest::setProxyHost | ( | const UString & | host, |
uint16_t | port | ||
) |
Set the optional proxy host and port for this request.
[in] | host | Proxy host name or address. |
[in] | port | Proxy port number. |
Set the optional proxy authentication for this request.
[in] | user | Proxy user name. |
[in] | password | Proxy user's password. |
|
static |
Set the default proxy host and port for all subsequent requests.
[in] | host | Proxy host name or address. |
[in] | port | Proxy port number. |
|
static |
Set the default proxy authentication for all subsequent requests.
[in] | user | Proxy user name. |
[in] | password | Proxy user's password. |
const UString & ts::WebRequest::proxyHost | ( | ) | const |
Get the current actual proxy host.
uint16_t ts::WebRequest::proxyPort | ( | ) | const |
Get the current actual proxy port number.
const UString & ts::WebRequest::proxyUser | ( | ) | const |
Get the current actual proxy user name.
const UString & ts::WebRequest::proxyPassword | ( | ) | const |
Get the current actual proxy user password.
void ts::WebRequest::enableCookies | ( | const fs::path & | fileName = fs::path() | ) |
Enable the use of cookies for all requests using this instance.
[in] | fileName | The name of the file to use to load and store cookies. On Windows, there is an implicit per-user cookie repository and fileName is ignored. On Unix systems, this file is used to store and retrieve cookies in the libcurl format. When fileName is empty, use a temporary file name. |
void ts::WebRequest::disableCookies | ( | ) |
Disable the use of cookies for all requests.
Cookies are initially disabled by default.
fs::path ts::WebRequest::getCookiesFileName | ( | ) | const |
Get the file name to use for cookies for all requests using this instance.
bool ts::WebRequest::deleteCookiesFile | ( | ) | const |
Delete the cookies file, if one was defined.
Set the user agent name to use in HTTP headers.
[in] | name | The user agent name. If empty, DEFAULT_USER_AGENT is used. |
|
inline |
Enable compression.
Compression is disabled by default.
[in] | on | Boolean setting compression on or off. |
|
inline |
Get the current user agent name to use in HTTP headers.
|
inline |
Enable or disable the automatic redirection of HTTP requests.
This option is active by default.
[in] | on | If true, allow automatic redirection of HTTP requests. |
void ts::WebRequest::setArgs | ( | const WebRequestArgs & | args | ) |
Set various arguments from command line.
[in] | args | Command line arguments. |
Set a header which will be sent with the request.
[in] | name | The header name. |
[in] | value | The header value. |
bool ts::WebRequest::open | ( | const UString & | url | ) |
|
inline |
Check if a transfer is open.
|
inline |
Get the HTTP status code (200, 404, etc).
|
inline |
Get the announced content size in bytes.
This is the value which was sent in the content headers. This mat be zero, this may not be the actual size of the content to download.
void ts::WebRequest::getResponseHeaders | ( | HeadersMap & | headers | ) | const |
Get all response headers.
[out] | headers | A multimap of all response headers. |
Get the value of one header.
[in] | name | Header name, case sensitive. |
UString ts::WebRequest::mimeType | ( | bool | simple = true , |
bool | lowercase = true |
||
) | const |
Get the MIME type in the response headers.
[in] | simple | If true, simple type name. If false, return the full specification with options. |
[in] | lowercase | Force lowercase in the result. |
|
inline |
|
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.
bool ts::WebRequest::receive | ( | void * | buffer, |
size_t | maxSize, | ||
size_t & | retSize | ||
) |
Receive data.
[out] | buffer | Address of the buffer for the received data. |
[in] | maxSize | Size in bytes of the reception buffer. |
[out] | retSize | Size in bytes of the received data. Will never be larger than max_size. When ret_size is zero, this is the end of the transfer. |
bool ts::WebRequest::close | ( | ) |
Close the transfer.
void ts::WebRequest::abort | ( | ) |
Abort a transfer in progress.
Can be called from another thread.
|
inline |
Get the size in bytes of the downloaded content.
bool ts::WebRequest::downloadBinaryContent | ( | const UString & | url, |
ByteBlock & | data, | ||
size_t | chunkSize = DEFAULT_CHUNK_SIZE |
||
) |
bool ts::WebRequest::downloadTextContent | ( | const UString & | url, |
UString & | text, | ||
size_t | chunkSize = DEFAULT_CHUNK_SIZE |
||
) |
Download the content of the URL as text in one operation.
The open/read/close session is embedded in this method.. The downloaded text is converted from UTF-8. End of lines are normalized as LF.
[in] | url | The complete URL to fetch. |
[out] | text | The content of the URL. |
[in] | chunkSize | Individual download chunk size. |
bool ts::WebRequest::downloadFile | ( | const UString & | url, |
const fs::path & | fileName, | ||
size_t | chunkSize = DEFAULT_CHUNK_SIZE |
||
) |
Download the content of the URL in a file in one operation.
The open/read/close session is embedded in this method.. No transformation is applied to the data.
[in] | url | The complete URL to fetch. |
[in] | fileName | Name of the file to create. |
[in] | chunkSize | Individual download chunk size. |
|
static |
Get the version of the underlying HTTP library.