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"
|
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;
|
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;
|
|
|
|
|
|
|
|
void Cancel() 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:
|
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);
|
2018-10-25 07:08:51 -04:00
|
|
|
|
|
|
|
private:
|
2018-11-03 12:50:04 -04:00
|
|
|
wxWebRequestHeaderMap m_headers;
|
|
|
|
wxString m_statusText;
|
|
|
|
|
|
|
|
CURL* GetHandle() const
|
|
|
|
{ return static_cast<wxWebRequestCURL&>(m_request).GetHandle(); }
|
|
|
|
|
|
|
|
wxDECLARE_NO_COPY_CLASS(wxWebResponseCURL);
|
|
|
|
};
|
|
|
|
|
2020-12-29 19:10:02 -05:00
|
|
|
class wxWebSessionCURL : public wxWebSessionImpl, private wxThreadHelper
|
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);
|
|
|
|
|
2018-11-03 12:50:04 -04:00
|
|
|
protected:
|
|
|
|
wxThread::ExitCode Entry() wxOVERRIDE;
|
|
|
|
|
|
|
|
private:
|
|
|
|
CURLM* m_handle;
|
2021-01-10 19:11:44 -05:00
|
|
|
|
|
|
|
// Mutex and condition are used together to signal to the worker thread to
|
|
|
|
// wake up and mutex is also used to protected m_shuttingDown field.
|
2018-11-03 12:50:04 -04:00
|
|
|
wxMutex m_mutex;
|
2018-11-05 17:08:18 -05:00
|
|
|
wxCondition m_condition;
|
2018-11-03 12:50:04 -04:00
|
|
|
bool m_shuttingDown;
|
2021-01-10 14:42:38 -05:00
|
|
|
|
|
|
|
// MT-safe vector of requests for which Cancel() was called.
|
|
|
|
struct CancelledData
|
|
|
|
{
|
|
|
|
wxCriticalSection cs;
|
|
|
|
wxVector< wxObjectDataPtr<wxWebRequestCURL> > requests;
|
|
|
|
} m_cancelled;
|
2018-11-03 12:50:04 -04:00
|
|
|
|
|
|
|
static int ms_activeSessions;
|
|
|
|
|
|
|
|
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
|