wxWidgets/interface/wx/webrequest.h

346 lines
9.6 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
communicate the request status.
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
// Bind events
request->Bind(wxEVT_WEBREQUEST_READY, [](wxWebRequestEvent& evt) {
wxImage logoImage(evt->GetResponse()->GetStream());
if (logoImage.IsOK())
wxLogInfo("Image loaded");
});
request->Bind(wxEVT_WEBREQUEST_FAILED, [](wxWebRequestEvent& evt) {
wxLogError("Could not load logo: %s", evt.GetErrorDescription());
});
// 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}
@event{wxEVT_WEBREQUEST_READY(id, func)}
The response data is ready to be used.
@event{wxEVT_WEBREQUEST_FAILED(id, func)}
A network error has occurred. This could be client side or server side.
2018-10-14 16:20:54 -04:00
Use wxWebRequestEvent::GetErrorDescription() to get more details.
@event{wxEVT_WEBREQUEST_AUTH_REQUIRED(id, func)}
The request needs additional authentication to continue.
@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:
/**
Sets a request header which will be send to the server by this request.
2018-10-14 16:20:54 -04:00
The header will be added if it hasen'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".
*/
void SetData(const wxString& text, const wxString& contentType);
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-14 16:20:54 -04:00
*/
void SetData(const wxInputStream& dataStream, const wxString& contentType);
/**
Instructs the request to ignore server error status codes.
Per default, server side errors (status code 400-599) will send
2018-10-14 16:20:54 -04:00
a wxEVT_WEBREQUEST_FAILED event just like network errors, but
if the response is still required in this 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-14 16:20:54 -04:00
from the wxEVT_WEBREQUEST_READY event handler.
*/
void SetIgnoreServerErrorStatus(bool ignore);
/**
Send the request to the server asynchronously.
Events will be triggered on success or failure.
2018-10-14 16:20:54 -04:00
@see Cancel()
*/
void Start();
/**
Cancel an active request.
2018-10-14 16:20:54 -04:00
*/
void Cancel();
/**
Returns a response object after a successful request.
Before sending a request or after a failed request this will return
@c NULL.
2018-10-14 16:20:54 -04:00
*/
const wxWebResponse* GetResponse() const;
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;
/**
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.
*/
wxInputStream& GetStream() const;
/**
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);
/**
Returns the default session
*/
static wxWebSession& GetDefault();
2018-10-14 16:20:54 -04:00
/**
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-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();
wxWebRequestEvent(wxEventType type, int id);
2018-10-14 16:20:54 -04:00
/**
The response for a wxEVT_WEBREQUEST_READY event or @c NULL for other
events.
2018-10-14 16:20:54 -04:00
*/
const wxWebResponse* GetResponse() const;
2018-10-14 16:20:54 -04:00
/**
A textual error description for a client side error
in case of wxEVT_WEBREQUEST_FAILED
2018-10-14 16:20:54 -04:00
*/
const wxString& GetErrorDescription() const;
};
wxEventType wxEVT_WEBREQUEST_READY;
wxEventType wxEVT_WEBREQUEST_FAILED;
wxEventType wxEVT_WEBREQUEST_AUTH_REQUIRED;