wxWidgets/interface/wx/webrequest.h

612 lines
17 KiB
C
Raw Normal View History

2018-10-14 16:20:54 -04:00
/////////////////////////////////////////////////////////////////////////////
// Name: webrequest.h
// Created: 2018-10-14
// Copyright: (c) 2018 wxWidgets development team
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
/**
@class wxWebRequest
This class allows for simple HTTP requests using the operating systems
components as implementation.
The latest features of the operating system will be used if available
(e.g. HTTP/2, TLS 1.3).
System-wide configuration like proxy and SSL certificates will be used
2018-10-14 16:20:54 -04:00
when possible.
Instances of wxWebRequest are created by using
wxWebSession::CreateRequest().
2018-10-14 16:20:54 -04:00
The requests are handled asynchronously and event handlers are used to
2018-10-22 16:00:25 -04:00
communicate the request status. The response data may be stored in
memory, to a file or processed directly, see SetStorage() for details.
2018-10-14 16:20:54 -04:00
Example usage:
@code
// Create the request object
wxObjectDataPtr<wxWebRequest> request(
wxWebSession::GetDefault().CreateRequest("https://www.wxwidgets.org/downloads/logos/blocks.png"));
2018-10-14 16:20:54 -04:00
2018-10-22 16:00:25 -04:00
// Bind state event
request->Bind(wxEVT_WEBREQUEST_STATE, [](wxWebRequestEvent& evt) {
switch (evt.GetState())
{
// Request completed
case wxWebRequest::State_Completed:
{
wxImage logoImage(*evt->GetResponse()->GetStream());
if (logoImage.IsOK())
wxLogInfo("Image loaded");
break;
}
// Request failed
case wxWebRequest::State_Failed:
wxLogError("Could not load logo: %s", evt.GetErrorDescription());
break;
}
2018-10-14 16:20:54 -04:00
});
// Start the request
request->Start();
@endcode
@section descriptions Implementation Descriptions
The following APIs are used per platform, additional documentation
about supported features may be found in their documentation
Available features by implementation and minimum version:
<table>
<tr><th>Operating System</th><th>API</th><th>HTTPS</th><th>HTTP/2</th></tr>
<tr>
<td>Windows</td>
<td>
<a target=_new href="https://docs.microsoft.com/en-us/windows/desktop/WinHttp/about-winhttp">WinHTTP</a>
</td>
<td>Yes</td>
<td>Windows 10 1607</td>
</tr>
<tr>
<td>macOS</td>
<td>
<a target=_new href="https://developer.apple.com/documentation/foundation/urlsession">NSURLSession</a>
</td>
<td>macOS 10.9</td>
<td>macOS 10.11</td>
</tr>
<tr>
<td>iOS</td>
<td>
<a target=_new href="https://developer.apple.com/documentation/foundation/urlsession">NSURLSession</a>
</td>
<td>iOS 7.0</td>
<td>iOS 9.0</td>
</tr>
<tr>
<td>Linux</td>
<td>
<a target=_new href="https://curl.haxx.se/libcurl/">libcurl</a>
</td>
<td>Yes</td>
<td>7.47.0</td>
</tr>
</table>
@beginEventEmissionTable{wxWebRequestEvent}
2018-10-22 16:00:25 -04:00
@event{wxEVT_WEBREQUEST_STATE(id, func)}
The request state changed.
@event{wxEVT_WEBREQUEST_DATA(id, func)}
A new block of data has been downloaded.
2018-10-14 16:20:54 -04:00
@endEventTable
@since 3.1.2
@library{wxnet}
@category{net}
@see wxWebResponse, wxWebSession
2018-10-14 16:20:54 -04:00
*/
class wxWebRequest: public wxEvtHandler, public wxRefCounter
{
public:
2018-10-22 16:00:25 -04:00
/**
Possible request states returned by GetState().
*/
enum State
{
/// The request has just been created and Start() has not been called
State_Idle,
/// The request has been started and is transferring data
State_Active,
/**
2018-10-25 17:02:23 -04:00
The request is currently unauthorized. Calling GetAuthChallenge()
returns a challenge object with further details.
2018-10-22 16:00:25 -04:00
*/
State_Unauthorized,
/// The request completed successfully and all data has been received.
State_Completed,
/// The request failed
State_Failed,
/// The request has been cancelled before completion by calling Cancel()
State_Cancelled
};
/**
Possible storage types. Set by SetStorage().
*/
enum Storage
{
/// All data is collected in memory until the request is complete
Storage_Memory,
/// The data is written to a file on disk
Storage_File,
/**
The data is not stored by the request and is only available
via events.
*/
Storage_None
};
2018-10-26 17:04:31 -04:00
/**
Send the request to the server asynchronously.
Events will be triggered on success or failure.
@see Cancel()
*/
void Start();
/**
Cancel an active request.
*/
void Cancel();
/**
Returns a response object after a successful request.
Before sending a request or after a failed request this will return
@c NULL.
*/
wxWebResponse* GetResponse();
/**
Returns the current authentication challenge object while the request
is in @c State_Unauthorized.
*/
wxWebAuthChallenge* GetAuthChallenge();
/**
Returns the id specified while creating this request.
*/
int GetId() const;
/** @name Request options
Methods that set options before starting the request
*/
///@{
2018-10-14 16:20:54 -04:00
/**
2018-10-18 03:46:48 -04:00
Sets a request header which will be sent to the server by this request.
2018-10-14 16:20:54 -04:00
2018-10-18 03:46:48 -04:00
The header will be added if it hasn't been set before or replaced
otherwise.
@param name
Name of the header
@param value
String value of the header. An empty string will remove the header.
2018-10-14 16:20:54 -04:00
*/
void SetHeader(const wxString& name, const wxString& value);
2018-10-14 16:20:54 -04:00
/**
Set <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">common</a>
or expanded HTTP method.
The default method is GET unless request data is provided in which
case POST is the default.
@param method
HTTP method name, e.g. "GET".
*/
void SetMethod(const wxString& method);
/**
Set the text to be posted to the server.
After a successful call to this method, the request will use HTTP @c
POST instead of the default @c GET when it's executed.
@param text
The text data to post.
2018-10-14 16:20:54 -04:00
@param contentType
The value of HTTP "Content-Type" header, e.g. "text/html;
charset=UTF-8".
2018-10-22 16:00:25 -04:00
@param conv
Conversion used when sending the text to the server
2018-10-14 16:20:54 -04:00
*/
2018-10-22 16:00:25 -04:00
void SetData(const wxString& text, const wxString& contentType,
const wxMBConv& conv = wxConvUTF8);
2018-10-14 16:20:54 -04:00
/**
Set the binary data to be posted to the server.
The next request will be a HTTP @c POST instead of the default HTTP
@c GET and the given @a dataStream will be posted as the body of
this request.
2018-10-14 16:20:54 -04:00
@param dataStream
The data in this stream will be posted as the request body
@param contentType
The value of HTTP "Content-Type" header, e.g.
"application/octet-stream".
2018-10-22 16:00:25 -04:00
@param dataSize
Amount of data which is sent to the server. If set to
@c wxInvalidOffset all stream data is sent.
2018-10-14 16:20:54 -04:00
*/
2018-10-22 16:00:25 -04:00
void SetData(wxSharedPtr<wxInputStream> dataStream,
const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset);
2018-10-14 16:20:54 -04:00
/**
Instructs the request to ignore server error status codes.
2018-10-22 16:00:25 -04:00
Per default, server side errors (status code 400-599) will enter
the State_Failed state just like network errors, but
if the response is still required in such cases (e.g. to get more
details from the response body), set this option to ignore all errors.
If ignored, wxWebResponse::GetStatus() has to be checked
2018-10-22 16:00:25 -04:00
from the State_Completed event handler.
2018-10-14 16:20:54 -04:00
*/
void SetIgnoreServerErrorStatus(bool ignore);
2018-10-22 16:00:25 -04:00
/**
Sets how response data will be stored.
The default storage method @c Storage_Memory collects all response data
in memory until the request is completed. This is fine for most usage
scenarios like API calls, loading images, etc. For larger downloads or
if the response data will be used permanently @c Storage_File instructs
the request to write the response to a temporary file. This temporary
file may then be read or moved after the request is complete. The file
will be downloaded to the system temp directory as returned by
wxStandardPaths::GetTempDir(). To specify a different directory use
wxWebSession::SetTempDir().
Sometimes response data needs to be processed while its downloaded from
the server. For example if the response is in a format that can be
parsed piece by piece like XML, JSON or an archive format like ZIP.
In these cases storing the data in memory or a file before being able
to process it might not be ideal and @c Storage_None should be set.
With this storage method the data is only available during the
@c wxEVT_WEBREQUEST_DATA event calls as soon as it's received from the
server.
*/
void SetStorage(Storage storage);
2018-10-26 17:04:31 -04:00
///@}
2018-10-22 16:00:25 -04:00
2018-10-26 17:04:31 -04:00
/** @name Progress methods
Methods that describe the requests progress
2018-10-14 16:20:54 -04:00
*/
2018-10-26 17:04:31 -04:00
///@{
2018-10-14 16:20:54 -04:00
/**
2018-10-26 17:04:31 -04:00
Returns the current state of the request.
2018-10-14 16:20:54 -04:00
*/
2018-10-26 17:04:31 -04:00
State GetState() const { return m_state; }
2018-10-14 16:20:54 -04:00
2018-10-26 17:04:31 -04:00
/// Returns the number of bytes sent to the server.
wxFileOffset GetBytesSent() const;
2018-10-26 17:04:31 -04:00
/// Returns the number of bytes expected to be send to the server.
wxFileOffset GetBytesExpectedToSend() const;
2018-10-26 17:04:31 -04:00
/// Returns the number of bytes received from the server.
wxFileOffset GetBytesReceived() const;
2018-10-22 16:00:25 -04:00
/**
2018-10-26 17:04:31 -04:00
Returns the number of bytes expected to be received from the server.
This value is based on the @c Content-Length header, if none is found
it will return -1.
2018-10-22 16:00:25 -04:00
*/
2018-10-26 17:04:31 -04:00
wxFileOffset GetBytesExpectedToReceive() const;
///@}
2018-10-14 16:20:54 -04:00
};
2018-10-25 17:02:23 -04:00
/**
Authentication challenge information available via
wxWebRequest::GetAuthChallenge().
Use SetCredentials() to provide user credentials.
*/
class wxWebAuthChallenge
{
public:
2018-10-26 17:04:31 -04:00
enum Source
{
/// The server requested authentication
Source_Server,
/// A proxy requested authentication
Source_Proxy
};
/**
Returns which source requested credentials with this challenge.
*/
Source GetSource() const { return m_source; }
2018-10-25 17:02:23 -04:00
/**
Used to provide user credentials to the authentication challenge.
@param user
User name.
@param password
The users password.
*/
void SetCredentials(const wxString& user, const wxString& password);
};
2018-10-14 16:20:54 -04:00
/**
A wxWebResponse allows access to the response sent by the server.
2018-10-14 16:20:54 -04:00
@since 3.1.2
@library{wxnet}
@category{net}
@see wxWebRequest
*/
class wxWebResponse
2018-10-14 16:20:54 -04:00
{
public:
/**
Returns the final URL.
This URL might be different than the request URL when a redirection
occurred.
2018-10-14 16:20:54 -04:00
*/
wxString GetURL() const;
/**
Returns a header from the response or an empty string if the header
2018-10-14 16:20:54 -04:00
could not be found.
@param name Name of the header field
*/
wxString GetHeader(const wxString& name) const;
2018-10-26 17:04:31 -04:00
/**
Returns the MIME type of the response (if available).
*/
wxString GetMimeType() const;
2018-10-14 16:20:54 -04:00
/**
Returns the status code returned by the server.
2018-10-14 16:20:54 -04:00
*/
int GetStatus() const;
/**
Returns the status text of the response.
2018-10-14 16:20:54 -04:00
*/
wxString GetStatusText() const;
/**
Returns a stream which represents the response data sent by the server.
*/
2018-10-22 16:00:25 -04:00
wxInputStream* GetStream();
2018-10-14 16:20:54 -04:00
2018-10-26 17:04:31 -04:00
/**
Returns a suggested filename for the response data.
*/
wxString GetSuggestedFileName() const;
2018-10-14 16:20:54 -04:00
/**
Returns all response data as a string.
@param conv wxMBConv used to convert the response to a string.
If @c NULL, the conversion will be determined by
response headers. The default is UTF-8.
2018-10-14 16:20:54 -04:00
*/
wxString AsString(wxMBConv* conv = NULL) const;
};
/**
@class wxWebSession
2018-10-14 16:20:54 -04:00
This class handles session-wide parameters and data used by wxWebRequest
2018-10-14 16:20:54 -04:00
instances.
Usually the default session available via wxWebSession::GetDefault()
2018-10-14 16:20:54 -04:00
should be used. Additional instances might be useful if session separation
is required. Instances <b>must not</b> be deleted before every active web
request has finished.
Every wxWebRequest sharing the same session object will use the same
cookies. Additionally, an underlying network connection might be kept
2018-10-14 16:20:54 -04:00
alive to achieve faster additional responses.
@since 3.1.2
@library{wxnet}
@category{net}
@see wxWebRequest
*/
class wxWebSession
2018-10-14 16:20:54 -04:00
{
public:
/**
Constructor for the session
All requests created by a call to CreateRequest() will use this session
for communication and to store cookies.
*/
wxWebSession();
2018-10-14 16:20:54 -04:00
/**
Create a new request for the specified URL
@param url
The URL of the HTTP resource for this request
@param id
Optional id sent with events
2018-10-14 16:20:54 -04:00
*/
wxWebRequest* CreateRequest(const wxString& url, int id = wxID_ANY);
/**
Sets a request header in every wxWebRequest created from this session
after is has been set.
2018-10-14 16:20:54 -04:00
A good example for a session-wide request header is the @c User-Agent
2018-10-14 16:20:54 -04:00
header.
@param name Name of the header
2018-10-14 16:20:54 -04:00
@param value String value of the header
*/
void SetHeader(const wxString& name, const wxString& value);
2018-10-22 16:00:25 -04:00
/**
Override the default temporary directory that may be used by the
session implemention, when required.
*/
void SetTempDir(const wxString& name);
/**
Returns the current temporary directory.
@see SetTempDir()
*/
2018-10-30 17:59:57 -04:00
const wxString& GetTempDir() const;
2018-10-22 16:00:25 -04:00
/**
Returns the default session
*/
static wxWebSession& GetDefault();
/**
Creates a new wxWebSession object.
@param backend
The backend web session implementation to use.
@return
The created wxWebSession
*/
static wxWebSession* New(const wxString& backend = wxWebSessionBackendDefault);
/**
Allows the registering of new backend for wxWebSession.
backend can be used as an argument to New().
@param backend The name for the new backend to be registered under
@param factory A shared pointer to the factory which creates the appropriate backend.
*/
static void RegisterFactory(const wxString& backend, wxSharedPtr<wxWebSessionFactory> factory);
2018-10-18 17:19:17 -04:00
/**
Allows to check if the specified backend is available at runtime.
Usually the default backend should always be available, but e.g. macOS
before 10.9 does not have the @c NSURLSession implementation available.
*/
static bool IsBackendAvailable(const wxString& backend);
};
/**
@class wxWebSessionFactory
An abstract factory class for creation wxWebSession backends.
Each implementation of wxWebSession should have its own factory.
@since 3.1.2
@library{wxnet}
@category{net}
@see wxWebSession
*/
class wxWebSessionFactory
{
public:
/**
Creates a new web session object
*/
virtual wxWebSession* Create() = 0;
2018-10-14 16:20:54 -04:00
};
/**
@class wxWebRequestEvent
A web request event sent during or after server communication.
2018-10-14 16:20:54 -04:00
@since 3.1.2
@library{wxnet}
@category{net}
@see wxWebRequest
*/
class wxWebRequestEvent : public wxEvent
{
public:
wxWebRequestEvent();
2018-10-22 16:00:25 -04:00
wxWebRequestEvent(wxEventType type, int id, wxWebRequest::State state,
wxWebResponse* response = NULL, const wxString& errorDesc = "");
/**
Return the current state of the request
*/
wxWebRequest::State GetState() const;
2018-10-14 16:20:54 -04:00
/**
2018-10-22 16:00:25 -04:00
The response with the state set to @c State_Complete or @c NULL for other
events.
2018-10-14 16:20:54 -04:00
*/
wxWebResponse* GetResponse() const;
2018-10-14 16:20:54 -04:00
/**
A textual error description for a client side error
2018-10-30 17:59:57 -04:00
in case of @c State_Failed
2018-10-14 16:20:54 -04:00
*/
const wxString& GetErrorDescription() const;
2018-10-30 17:59:57 -04:00
/**
Returns a file name to a temporary file containing the response data
when the state is @c State_Completed and storage is @Storage_File.
The file will be removed after the event handlers are called. You can
move the file to location of your choice if you want to process the
contents outside of the event handler.
*/
const wxString& GetResponseFileName() const;
void SetResponseFileName(const wxString& filename);
/**
Only for @c wxEVT_WEBREQUEST_DATA events. The buffer is only valid
in the event handler.
*/
///@{
const void* GetDataBuffer() const;
size_t GetDataSize() const;
///@}
void SetDataBuffer(const void* buffer, size_t size);
2018-10-14 16:20:54 -04:00
};
2018-10-22 16:00:25 -04:00
wxEventType wxEVT_WEBREQUEST_STATE;
wxEventType wxEVT_WEBREQUEST_DATA;