2018-10-25 07:08:51 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/webrequest_curl.h
|
|
|
|
// Purpose: wxWebRequest implementation using libcurl
|
|
|
|
// Author: Tobias Taschner
|
|
|
|
// Created: 2018-10-25
|
|
|
|
// Copyright: (c) 2018 wxWidgets development team
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_WEBREQUEST_CURL_H
|
|
|
|
#define _WX_WEBREQUEST_CURL_H
|
|
|
|
|
|
|
|
#if wxUSE_WEBREQUEST_CURL
|
|
|
|
|
2020-12-26 18:40:16 -05:00
|
|
|
#include "wx/private/webrequest.h"
|
|
|
|
|
2018-11-03 12:50:04 -04:00
|
|
|
#include "wx/thread.h"
|
2018-11-05 16:12:41 -05:00
|
|
|
#include "wx/vector.h"
|
2021-01-25 17:26:27 -05:00
|
|
|
#include "wx/timer.h"
|
2021-02-07 01:48:02 -05:00
|
|
|
#include "wx/hashmap.h"
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
#include "curl/curl.h"
|
|
|
|
|
|
|
|
class wxWebRequestCURL;
|
2020-12-29 19:10:02 -05:00
|
|
|
class wxWebResponseCURL;
|
|
|
|
class wxWebSessionCURL;
|
2021-01-25 17:26:27 -05:00
|
|
|
class SocketPoller;
|
2018-11-03 12:50:04 -04:00
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
class wxWebAuthChallengeCURL : public wxWebAuthChallengeImpl
|
2018-11-03 12:50:04 -04:00
|
|
|
{
|
|
|
|
public:
|
2020-12-29 19:10:02 -05:00
|
|
|
wxWebAuthChallengeCURL(wxWebAuthChallenge::Source source,
|
|
|
|
wxWebRequestCURL& request);
|
2018-11-03 12:50:04 -04:00
|
|
|
|
2021-01-09 19:22:40 -05:00
|
|
|
void SetCredentials(const wxWebCredentials& cred) wxOVERRIDE;
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
wxWebRequestCURL& m_request;
|
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(wxWebAuthChallengeCURL);
|
|
|
|
};
|
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
class wxWebRequestCURL : public wxWebRequestImpl
|
2018-11-03 12:50:04 -04:00
|
|
|
{
|
|
|
|
public:
|
2020-12-29 19:10:02 -05:00
|
|
|
wxWebRequestCURL(wxWebSession& session,
|
|
|
|
wxWebSessionCURL& sessionImpl,
|
|
|
|
wxEvtHandler* handler,
|
|
|
|
const wxString& url,
|
|
|
|
int id);
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
~wxWebRequestCURL();
|
|
|
|
|
|
|
|
void Start() wxOVERRIDE;
|
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
wxWebResponseImplPtr GetResponse() const wxOVERRIDE
|
|
|
|
{ return m_response; }
|
2018-11-03 12:50:04 -04:00
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
wxWebAuthChallengeImplPtr GetAuthChallenge() const wxOVERRIDE
|
|
|
|
{ return m_authChallenge; }
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
wxFileOffset GetBytesSent() const wxOVERRIDE;
|
|
|
|
|
|
|
|
wxFileOffset GetBytesExpectedToSend() const wxOVERRIDE;
|
|
|
|
|
|
|
|
CURL* GetHandle() const { return m_handle; }
|
|
|
|
|
2021-01-15 18:21:00 -05:00
|
|
|
wxWebRequestHandle GetNativeHandle() const wxOVERRIDE
|
|
|
|
{
|
|
|
|
return (wxWebRequestHandle)GetHandle();
|
|
|
|
}
|
|
|
|
|
2018-11-03 12:50:04 -04:00
|
|
|
bool StartRequest();
|
|
|
|
|
|
|
|
void HandleCompletion();
|
|
|
|
|
|
|
|
wxString GetError() const;
|
|
|
|
|
2021-01-11 20:37:55 -05:00
|
|
|
// Method called from libcurl callback
|
|
|
|
size_t CURLOnRead(char* buffer, size_t size);
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
private:
|
2021-01-16 07:49:22 -05:00
|
|
|
void DoCancel() wxOVERRIDE;
|
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
wxWebSessionCURL& m_sessionImpl;
|
|
|
|
|
2018-11-03 12:50:04 -04:00
|
|
|
CURL* m_handle;
|
|
|
|
char m_errorBuffer[CURL_ERROR_SIZE];
|
|
|
|
struct curl_slist *m_headerList;
|
2020-12-29 19:10:02 -05:00
|
|
|
wxObjectDataPtr<wxWebResponseCURL> m_response;
|
|
|
|
wxObjectDataPtr<wxWebAuthChallengeCURL> m_authChallenge;
|
2018-11-04 08:46:03 -05:00
|
|
|
wxFileOffset m_bytesSent;
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
void DestroyHeaderList();
|
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(wxWebRequestCURL);
|
|
|
|
};
|
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
class wxWebResponseCURL : public wxWebResponseImpl
|
2018-10-25 07:08:51 -04:00
|
|
|
{
|
|
|
|
public:
|
2020-12-29 19:10:02 -05:00
|
|
|
explicit wxWebResponseCURL(wxWebRequestCURL& request);
|
2018-11-03 12:50:04 -04:00
|
|
|
|
2021-01-11 21:25:16 -05:00
|
|
|
wxFileOffset GetContentLength() const wxOVERRIDE;
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
wxString GetURL() const wxOVERRIDE;
|
2018-10-25 07:08:51 -04:00
|
|
|
|
2018-11-03 12:50:04 -04:00
|
|
|
wxString GetHeader(const wxString& name) const wxOVERRIDE;
|
2018-10-25 07:08:51 -04:00
|
|
|
|
2018-11-03 12:50:04 -04:00
|
|
|
int GetStatus() const wxOVERRIDE;
|
|
|
|
|
|
|
|
wxString GetStatusText() const wxOVERRIDE { return m_statusText; }
|
|
|
|
|
|
|
|
|
2021-01-11 20:37:55 -05:00
|
|
|
// Methods called from libcurl callbacks
|
|
|
|
size_t CURLOnWrite(void *buffer, size_t size);
|
|
|
|
size_t CURLOnHeader(const char* buffer, size_t size);
|
2021-02-07 21:43:51 -05:00
|
|
|
int CURLOnProgress(curl_off_t);
|
2018-10-25 07:08:51 -04:00
|
|
|
|
|
|
|
private:
|
2018-11-03 12:50:04 -04:00
|
|
|
wxWebRequestHeaderMap m_headers;
|
|
|
|
wxString m_statusText;
|
2021-02-07 21:43:51 -05:00
|
|
|
wxFileOffset m_knownDownloadSize;
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
CURL* GetHandle() const
|
|
|
|
{ return static_cast<wxWebRequestCURL&>(m_request).GetHandle(); }
|
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(wxWebResponseCURL);
|
|
|
|
};
|
|
|
|
|
2021-01-25 17:26:27 -05:00
|
|
|
class wxWebSessionCURL : public wxWebSessionImpl, public wxEvtHandler
|
2018-11-03 12:50:04 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxWebSessionCURL();
|
|
|
|
|
|
|
|
~wxWebSessionCURL();
|
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
wxWebRequestImplPtr
|
|
|
|
CreateRequest(wxWebSession& session,
|
|
|
|
wxEvtHandler* handler,
|
|
|
|
const wxString& url,
|
|
|
|
int id = wxID_ANY) wxOVERRIDE;
|
2018-11-03 12:50:04 -04:00
|
|
|
|
2018-11-06 16:39:13 -05:00
|
|
|
wxVersionInfo GetLibraryVersionInfo() wxOVERRIDE;
|
|
|
|
|
2021-01-15 18:21:00 -05:00
|
|
|
wxWebSessionHandle GetNativeHandle() const wxOVERRIDE
|
|
|
|
{
|
|
|
|
return (wxWebSessionHandle)m_handle;
|
|
|
|
}
|
|
|
|
|
2018-11-03 12:50:04 -04:00
|
|
|
bool StartRequest(wxWebRequestCURL& request);
|
|
|
|
|
2018-11-05 16:12:41 -05:00
|
|
|
void CancelRequest(wxWebRequestCURL* request);
|
|
|
|
|
2021-02-07 01:48:02 -05:00
|
|
|
void RequestHasTerminated(wxWebRequestCURL* request);
|
|
|
|
|
2021-02-07 21:37:47 -05:00
|
|
|
static bool CurlRuntimeAtLeastVersion(unsigned int, unsigned int,
|
|
|
|
unsigned int);
|
|
|
|
|
2018-11-03 12:50:04 -04:00
|
|
|
private:
|
2021-01-25 17:26:27 -05:00
|
|
|
static int TimerCallback(CURLM*, long, void*);
|
|
|
|
static int SocketCallback(CURL*, curl_socket_t, int, void*, void*);
|
|
|
|
|
|
|
|
void ProcessTimerCallback(long);
|
|
|
|
void TimeoutNotification(wxTimerEvent&);
|
|
|
|
void ProcessTimeoutNotification();
|
2021-02-07 21:00:38 -05:00
|
|
|
void ProcessSocketCallback(CURL*, curl_socket_t, int);
|
2021-01-25 17:26:27 -05:00
|
|
|
void ProcessSocketPollerResult(wxThreadEvent&);
|
|
|
|
void CheckForCompletedTransfers();
|
2021-02-07 21:00:38 -05:00
|
|
|
void FailRequest(CURL*, const wxString&);
|
2021-02-08 13:32:32 -05:00
|
|
|
void StopActiveTransfer(CURL*);
|
|
|
|
void RemoveActiveSocket(CURL*);
|
2021-01-25 17:26:27 -05:00
|
|
|
|
2021-02-07 01:48:02 -05:00
|
|
|
WX_DECLARE_HASH_MAP(CURL*, wxWebRequestCURL*, wxPointerHash, \
|
|
|
|
wxPointerEqual, TransferSet);
|
|
|
|
|
2021-02-08 13:32:32 -05:00
|
|
|
WX_DECLARE_HASH_MAP(CURL*, curl_socket_t, wxPointerHash, \
|
|
|
|
wxPointerEqual, CurlSocketMap);
|
|
|
|
|
2021-02-07 01:48:02 -05:00
|
|
|
TransferSet m_activeTransfers;
|
2021-02-08 13:32:32 -05:00
|
|
|
CurlSocketMap m_activeSockets;
|
2021-02-07 01:48:02 -05:00
|
|
|
|
2021-01-25 17:26:27 -05:00
|
|
|
SocketPoller* m_socketPoller;
|
|
|
|
wxTimer m_timeoutTimer;
|
2018-11-03 12:50:04 -04:00
|
|
|
CURLM* m_handle;
|
2021-01-10 19:11:44 -05:00
|
|
|
|
2018-11-03 12:50:04 -04:00
|
|
|
static int ms_activeSessions;
|
2021-02-07 21:37:47 -05:00
|
|
|
static unsigned int ms_runtimeVersion;
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(wxWebSessionCURL);
|
2018-10-25 07:08:51 -04:00
|
|
|
};
|
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
class wxWebSessionFactoryCURL : public wxWebSessionFactory
|
2018-10-25 07:08:51 -04:00
|
|
|
{
|
|
|
|
public:
|
2020-12-29 19:10:02 -05:00
|
|
|
wxWebSessionImpl* Create() wxOVERRIDE
|
2018-11-03 12:50:04 -04:00
|
|
|
{ return new wxWebSessionCURL(); }
|
2018-10-25 07:08:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // wxUSE_WEBREQUEST_CURL
|
|
|
|
|
|
|
|
#endif
|