1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
///////////////////////////////////////////////////////////////////////////////
// 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
#include "wx/private/webrequest.h"
#include "wx/thread.h"
#include "wx/vector.h"
#include "wx/timer.h"
#include "wx/hashmap.h"
#include "curl/curl.h"
class wxWebRequestCURL;
class wxWebResponseCURL;
class wxWebSessionCURL;
class SocketPoller;
class wxWebAuthChallengeCURL : public wxWebAuthChallengeImpl
{
public:
wxWebAuthChallengeCURL(wxWebAuthChallenge::Source source,
wxWebRequestCURL& request);
void SetCredentials(const wxWebCredentials& cred) wxOVERRIDE;
private:
wxWebRequestCURL& m_request;
wxDECLARE_NO_COPY_CLASS(wxWebAuthChallengeCURL);
};
class wxWebRequestCURL : public wxWebRequestImpl
{
public:
wxWebRequestCURL(wxWebSession& session,
wxWebSessionCURL& sessionImpl,
wxEvtHandler* handler,
const wxString& url,
int id);
~wxWebRequestCURL();
void Start() wxOVERRIDE;
wxWebResponseImplPtr GetResponse() const wxOVERRIDE
{ return m_response; }
wxWebAuthChallengeImplPtr GetAuthChallenge() const wxOVERRIDE
{ return m_authChallenge; }
wxFileOffset GetBytesSent() const wxOVERRIDE;
wxFileOffset GetBytesExpectedToSend() const wxOVERRIDE;
CURL* GetHandle() const { return m_handle; }
wxWebRequestHandle GetNativeHandle() const wxOVERRIDE
{
return (wxWebRequestHandle)GetHandle();
}
bool StartRequest();
void HandleCompletion();
wxString GetError() const;
// Method called from libcurl callback
size_t CURLOnRead(char* buffer, size_t size);
private:
void DoCancel() wxOVERRIDE;
wxWebSessionCURL& m_sessionImpl;
CURL* m_handle;
char m_errorBuffer[CURL_ERROR_SIZE];
struct curl_slist *m_headerList;
wxObjectDataPtr<wxWebResponseCURL> m_response;
wxObjectDataPtr<wxWebAuthChallengeCURL> m_authChallenge;
wxFileOffset m_bytesSent;
void DestroyHeaderList();
wxDECLARE_NO_COPY_CLASS(wxWebRequestCURL);
};
class wxWebResponseCURL : public wxWebResponseImpl
{
public:
explicit wxWebResponseCURL(wxWebRequestCURL& request);
wxFileOffset GetContentLength() const wxOVERRIDE;
wxString GetURL() const wxOVERRIDE;
wxString GetHeader(const wxString& name) const wxOVERRIDE;
int GetStatus() const wxOVERRIDE;
wxString GetStatusText() const wxOVERRIDE { return m_statusText; }
// Methods called from libcurl callbacks
size_t CURLOnWrite(void *buffer, size_t size);
size_t CURLOnHeader(const char* buffer, size_t size);
int CURLOnProgress(curl_off_t);
private:
wxWebRequestHeaderMap m_headers;
wxString m_statusText;
wxFileOffset m_knownDownloadSize;
CURL* GetHandle() const
{ return static_cast<wxWebRequestCURL&>(m_request).GetHandle(); }
wxDECLARE_NO_COPY_CLASS(wxWebResponseCURL);
};
class wxWebSessionCURL : public wxWebSessionImpl, public wxEvtHandler
{
public:
wxWebSessionCURL();
~wxWebSessionCURL();
wxWebRequestImplPtr
CreateRequest(wxWebSession& session,
wxEvtHandler* handler,
const wxString& url,
int id = wxID_ANY) wxOVERRIDE;
wxVersionInfo GetLibraryVersionInfo() wxOVERRIDE;
wxWebSessionHandle GetNativeHandle() const wxOVERRIDE
{
return (wxWebSessionHandle)m_handle;
}
bool StartRequest(wxWebRequestCURL& request);
void CancelRequest(wxWebRequestCURL* request);
void RequestHasTerminated(wxWebRequestCURL* request);
static bool CurlRuntimeAtLeastVersion(unsigned int, unsigned int,
unsigned int);
private:
static int TimerCallback(CURLM*, long, void*);
static int SocketCallback(CURL*, curl_socket_t, int, void*, void*);
void ProcessTimerCallback(long);
void TimeoutNotification(wxTimerEvent&);
void ProcessTimeoutNotification();
void ProcessSocketCallback(CURL*, curl_socket_t, int);
void ProcessSocketPollerResult(wxThreadEvent&);
void CheckForCompletedTransfers();
void FailRequest(CURL*, const wxString&);
void StopActiveTransfer(CURL*);
void RemoveActiveSocket(CURL*);
WX_DECLARE_HASH_MAP(CURL*, wxWebRequestCURL*, wxPointerHash, \
wxPointerEqual, TransferSet);
WX_DECLARE_HASH_MAP(CURL*, curl_socket_t, wxPointerHash, \
wxPointerEqual, CurlSocketMap);
TransferSet m_activeTransfers;
CurlSocketMap m_activeSockets;
SocketPoller* m_socketPoller;
wxTimer m_timeoutTimer;
CURLM* m_handle;
static int ms_activeSessions;
static unsigned int ms_runtimeVersion;
wxDECLARE_NO_COPY_CLASS(wxWebSessionCURL);
};
class wxWebSessionFactoryCURL : public wxWebSessionFactory
{
public:
wxWebSessionImpl* Create() wxOVERRIDE
{ return new wxWebSessionCURL(); }
};
#endif // wxUSE_WEBREQUEST_CURL
#endif
|