TSDuck v3.46-4806
MPEG Transport Stream Toolkit
Loading...
Searching...
No Matches
ts::WebRequest Class Reference

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

#include <tsWebRequest.h>

Inheritance diagram for ts::WebRequest:
Collaboration diagram for ts::WebRequest:

Public Member Functions

 WebRequest (Report *report)
 Constructor.
 
 WebRequest (ReporterBase *delegate)
 Constructor.
 
virtual ~WebRequest () override
 Destructor.
 
void abort ()
 Abort a transfer in progress.
 
WebRequestArgs & args ()
 Access the Web request arguments to get or set them.
 
const WebRequestArgs & args () const
 Access the Web request arguments to get them.
 
bool close ()
 Close the transfer.
 
bool downloadBinaryContent (const UString &url, ByteBlockPtr &data, size_t chunk_size=Device::DEFAULT_RECEIVE_BUFFER_SIZE)
 Download the content of the URL as binary data in one operation.
 
bool downloadFile (const UString &url, const fs::path &file_name, size_t chunk_size=Device::DEFAULT_RECEIVE_BUFFER_SIZE)
 Download the content of the URL in a file in one operation.
 
bool downloadTextContent (const UString &url, UString &text, size_t chunk_size=Device::DEFAULT_RECEIVE_BUFFER_SIZE)
 Download the content of the URL as text in one operation.
 
bool isOpen () const
 Check if a transfer is open.
 
bool muteReport (bool mute)
 Temporarily mute the associated report.
 
bool open (const UString &url, size_t buffer_size=Device::DEFAULT_RECEIVE_BUFFER_SIZE)
 Open an URL and start the transfer.
 
bool receive (void *buffer, size_t max_size, size_t &ret_size)
 Receive data.
 
virtual Report & report () const override
 Access the Report which is associated with this object.
 
Report * setReport (Report *report)
 Associate this object with another Report to log errors.
 
ReporterBase * setReport (ReporterBase *delegate)
 Associate this object with another ReporterBase to log errors.
 
const WebRequestStatus & status () const
 Access the Web request status.
 

Static Public Member Functions

static int SilentLevel (bool silent, int default_severity=Severity::Error)
 Compute a log severity level from a "silent" parameter.
 

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 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).

Constructor & Destructor Documentation

◆ WebRequest() [1/2]

ts::WebRequest::WebRequest ( Report *  report)
explicit

Constructor.

Parameters
[in]reportWhere to report errors. The report object must remain valid as long as this object exists or setReport() is used with another Report object. If report is null, log messages are discarded.

◆ WebRequest() [2/2]

ts::WebRequest::WebRequest ( ReporterBase *  delegate)
explicit

Constructor.

Parameters
[in]delegateUse the report of another ReporterBase. If delegate is null, log messages are discarded.

Member Function Documentation

◆ args() [1/2]

WebRequestArgs & ts::WebRequest::args ( )

Access the Web request arguments to get or set them.

Returns
A non-const reference to the Web request arguments.

◆ args() [2/2]

const WebRequestArgs & ts::WebRequest::args ( ) const

Access the Web request arguments to get them.

Returns
A const reference to the Web request arguments.

◆ status()

const WebRequestStatus & ts::WebRequest::status ( ) const

Access the Web request status.

Returns
A const reference to the Web request status.

◆ open()

bool ts::WebRequest::open ( const UString &  url,
size_t  buffer_size = Device::DEFAULT_RECEIVE_BUFFER_SIZE 
)

Open an URL and start the transfer.

For HTTP request, perform all redirections and get response headers.

Parameters
[in]urlThe complete URL to fetch.
[in]buffer_sizeSize of input buffers to receive data.
Returns
True on success, false on error.

◆ isOpen()

bool ts::WebRequest::isOpen ( ) const

Check if a transfer is open.

Returns
True if a transfer is open, false otherwise.

◆ receive()

bool ts::WebRequest::receive ( void *  buffer,
size_t  max_size,
size_t &  ret_size 
)

Receive data.

Parameters
[out]bufferAddress of the buffer for the received data.
[in]max_sizeSize in bytes of the reception buffer.
[out]ret_sizeSize 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.
Returns
True on success, false on error. A successful end of transfer is reported when ret_size is zero and the returned value is true.

◆ close()

bool ts::WebRequest::close ( )

Close the transfer.

Returns
True on success, false on error.

◆ abort()

void ts::WebRequest::abort ( )

Abort a transfer in progress.

Can be called from another thread.

◆ downloadBinaryContent()

bool ts::WebRequest::downloadBinaryContent ( const UString &  url,
ByteBlockPtr &  data,
size_t  chunk_size = Device::DEFAULT_RECEIVE_BUFFER_SIZE 
)

Download the content of the URL as binary data in one operation.

The open/read/close session is embedded in this method. The request must be in blocking mode (the default).

Parameters
[in]urlThe complete URL to fetch.
[out]dataThe content of the URL in a shred pointer. Null in case of error.
[in]chunk_sizeIndividual download chunk size.
Returns
True on success, false on error.

◆ downloadTextContent()

bool ts::WebRequest::downloadTextContent ( const UString &  url,
UString &  text,
size_t  chunk_size = Device::DEFAULT_RECEIVE_BUFFER_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. The request must be in blocking mode (the default).

Parameters
[in]urlThe complete URL to fetch.
[out]textThe content of the URL.
[in]chunk_sizeIndividual download chunk size.
Returns
True on success, false on error.

◆ downloadFile()

bool ts::WebRequest::downloadFile ( const UString &  url,
const fs::path &  file_name,
size_t  chunk_size = Device::DEFAULT_RECEIVE_BUFFER_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. The request must be in blocking mode (the default).

Parameters
[in]urlThe complete URL to fetch.
[in]file_nameName of the file to create.
[in]chunk_sizeIndividual download chunk size.
Returns
True on success, false on error.

◆ report()

virtual Report & ts::ReporterBase::report ( ) const
overridevirtualinherited

Access the Report which is associated with this object.

Can be called from another thread only if the Report object is thread-safe.

Returns
A reference to the associated report.

Implements ts::ReporterInterface.

◆ setReport() [1/2]

Report * ts::ReporterBase::setReport ( Report *  report)
inherited

Associate this object with another Report to log errors.

Parameters
[in]reportWhere to report errors. The report object must remain valid as long as this object exists or setReport() is used with another Report object. If report is null, log messages are discarded.
Returns
The address of the previous Report object or a null pointer if there was none.

◆ setReport() [2/2]

ReporterBase * ts::ReporterBase::setReport ( ReporterBase *  delegate)
inherited

Associate this object with another ReporterBase to log errors.

Parameters
[in]delegateUse the report of another ReporterBase. If delegate is null, the previous explicit Report is used..
Returns
The address of the previous ReporterBase object or a null pointer if there was none.

◆ muteReport()

bool ts::ReporterBase::muteReport ( bool  mute)
inherited

Temporarily mute the associated report.

Parameters
[in]muteIt true, report() will return a null report (log messages are discarded), until muteReport() is invoked again with mute set to false.
Returns
Previous state of the mute field.

◆ SilentLevel()

static int ts::ReporterBase::SilentLevel ( bool  silent,
int  default_severity = Severity::Error 
)
inlinestaticinherited

Compute a log severity level from a "silent" parameter.

Some subclass methods have a "silent" parameter to avoid reporting errors which may be insignificant, typically when closing a device after an error, in which case the close operation may produce other errors if the previous error left the device in an inconsistent state. While those errors should not be displayed as errors, we still display them at debug level.

Parameters
[in]silentIf true, do not report errors, report debug messages instead.
[in]default_severityDefault severity, in non-silent mode (error by default).
Returns
Error when silent is false, Debug otherwise.

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