Change wxWebRequest and related objects to hide ref counting
Don't force the application code to deal with wxObjectDataPtr<> or, worse, calling {Inc,Dec}Ref() manually by hiding it inside the wx objects themselves and giving the value-like semantics to them. There should be no real changes in the behaviour, but the API does change significantly. Notably, wxWebRequest is not a wxEvtHandler itself any longer, as this would be incompatible with the value semantics, and an event handler needs to be specified when creating it, so that it could be notified about the request state changes.
This commit is contained in:
@@ -10,6 +10,170 @@
|
||||
#ifndef _WX_PRIVATE_WEBREQUEST_H_
|
||||
#define _WX_PRIVATE_WEBREQUEST_H_
|
||||
|
||||
#include "wx/ffile.h"
|
||||
#include "wx/hashmap.h"
|
||||
#include "wx/scopedptr.h"
|
||||
|
||||
WX_DECLARE_STRING_HASH_MAP(wxString, wxWebRequestHeaderMap);
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebAuthChallengeImpl
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class wxWebAuthChallengeImpl : public wxRefCounter
|
||||
{
|
||||
public:
|
||||
virtual ~wxWebAuthChallengeImpl() { }
|
||||
|
||||
wxWebAuthChallenge::Source GetSource() const { return m_source; }
|
||||
|
||||
virtual void
|
||||
SetCredentials(const wxString& user, const wxString& password) = 0;
|
||||
|
||||
protected:
|
||||
explicit wxWebAuthChallengeImpl(wxWebAuthChallenge::Source source)
|
||||
: m_source(source) { }
|
||||
|
||||
private:
|
||||
const wxWebAuthChallenge::Source m_source;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebAuthChallengeImpl);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebRequestImpl
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class wxWebRequestImpl : public wxRefCounter
|
||||
{
|
||||
public:
|
||||
virtual ~wxWebRequestImpl() { }
|
||||
|
||||
void SetHeader(const wxString& name, const wxString& value)
|
||||
{ m_headers[name] = value; }
|
||||
|
||||
void SetMethod(const wxString& method) { m_method = method; }
|
||||
|
||||
void SetData(const wxString& text, const wxString& contentType, const wxMBConv& conv = wxConvUTF8);
|
||||
|
||||
bool SetData(const wxSharedPtr<wxInputStream>& dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset);
|
||||
|
||||
void SetIgnoreServerErrorStatus(bool ignore) { m_ignoreServerErrorStatus = ignore; }
|
||||
|
||||
void SetStorage(wxWebRequest::Storage storage) { m_storage = storage; }
|
||||
|
||||
wxWebRequest::Storage GetStorage() const { return m_storage; }
|
||||
|
||||
virtual void Start() = 0;
|
||||
|
||||
virtual void Cancel() = 0;
|
||||
|
||||
virtual wxWebResponseImplPtr GetResponse() const = 0;
|
||||
|
||||
virtual wxWebAuthChallengeImplPtr GetAuthChallenge() const = 0;
|
||||
|
||||
int GetId() const { return m_id; }
|
||||
|
||||
wxWebSession& GetSession() const { return m_session; }
|
||||
|
||||
wxWebRequest::State GetState() const { return m_state; }
|
||||
|
||||
virtual wxFileOffset GetBytesSent() const = 0;
|
||||
|
||||
virtual wxFileOffset GetBytesExpectedToSend() const = 0;
|
||||
|
||||
virtual wxFileOffset GetBytesReceived() const;
|
||||
|
||||
virtual wxFileOffset GetBytesExpectedToReceive() const;
|
||||
|
||||
void SetState(wxWebRequest::State state, const wxString& failMsg = wxString());
|
||||
|
||||
void ReportDataReceived(size_t sizeReceived);
|
||||
|
||||
wxEvtHandler* GetHandler() const { return m_handler; }
|
||||
|
||||
void ProcessStateEvent(wxWebRequest::State state, const wxString& failMsg);
|
||||
|
||||
protected:
|
||||
wxString m_method;
|
||||
wxWebRequest::Storage m_storage;
|
||||
wxWebRequestHeaderMap m_headers;
|
||||
wxFileOffset m_dataSize;
|
||||
wxSharedPtr<wxInputStream> m_dataStream;
|
||||
|
||||
wxWebRequestImpl(wxWebSession& session, wxEvtHandler* handler, int id);
|
||||
|
||||
bool CheckServerStatus();
|
||||
|
||||
static bool IsActiveState(wxWebRequest::State state);
|
||||
|
||||
private:
|
||||
wxWebSession& m_session;
|
||||
wxEvtHandler* const m_handler;
|
||||
const int m_id;
|
||||
wxWebRequest::State m_state;
|
||||
bool m_ignoreServerErrorStatus;
|
||||
wxFileOffset m_bytesReceived;
|
||||
wxCharBuffer m_dataText;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebRequestImpl);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebResponseImpl
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class wxWebResponseImpl : public wxRefCounter
|
||||
{
|
||||
public:
|
||||
virtual ~wxWebResponseImpl();
|
||||
|
||||
virtual wxInt64 GetContentLength() const = 0;
|
||||
|
||||
virtual wxString GetURL() const = 0;
|
||||
|
||||
virtual wxString GetHeader(const wxString& name) const = 0;
|
||||
|
||||
virtual wxString GetMimeType() const;
|
||||
|
||||
virtual int GetStatus() const = 0;
|
||||
|
||||
virtual wxString GetStatusText() const = 0;
|
||||
|
||||
virtual wxInputStream* GetStream() const;
|
||||
|
||||
virtual wxString GetSuggestedFileName() const;
|
||||
|
||||
wxString AsString() const;
|
||||
|
||||
virtual wxString GetFileName() const;
|
||||
|
||||
protected:
|
||||
wxWebRequestImpl& m_request;
|
||||
size_t m_readSize;
|
||||
|
||||
explicit wxWebResponseImpl(wxWebRequestImpl& request);
|
||||
|
||||
// Called from derived class ctor to finish initialization which can't be
|
||||
// performed in ctor itself as it needs to use pure virtual method.
|
||||
void Init();
|
||||
|
||||
void* GetDataBuffer(size_t sizeNeeded);
|
||||
|
||||
void ReportDataReceived(size_t sizeReceived);
|
||||
|
||||
private:
|
||||
// Called by wxWebRequestImpl only.
|
||||
friend class wxWebRequestImpl;
|
||||
void Finalize();
|
||||
|
||||
wxMemoryBuffer m_readBuffer;
|
||||
mutable wxFFile m_file;
|
||||
mutable wxScopedPtr<wxInputStream> m_stream;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebResponseImpl);
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebSessionFactory
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -17,9 +181,48 @@
|
||||
class wxWebSessionFactory
|
||||
{
|
||||
public:
|
||||
virtual wxWebSession* Create() = 0;
|
||||
virtual wxWebSessionImpl* Create() = 0;
|
||||
|
||||
virtual ~wxWebSessionFactory() { }
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// wxWebSessionImpl
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
class wxWebSessionImpl : public wxRefCounter
|
||||
{
|
||||
public:
|
||||
virtual ~wxWebSessionImpl() { }
|
||||
|
||||
virtual wxWebRequestImplPtr
|
||||
CreateRequest(wxWebSession& session,
|
||||
wxEvtHandler* handler,
|
||||
const wxString& url,
|
||||
int id) = 0;
|
||||
|
||||
virtual wxVersionInfo GetLibraryVersionInfo() = 0;
|
||||
|
||||
void SetHeader(const wxString& name, const wxString& value)
|
||||
{ m_headers[name] = value; }
|
||||
|
||||
void SetTempDir(const wxString& dir) { m_tempDir = dir; }
|
||||
|
||||
wxString GetTempDir() const;
|
||||
|
||||
const wxWebRequestHeaderMap& GetHeaders() const { return m_headers; }
|
||||
|
||||
protected:
|
||||
wxWebSessionImpl();
|
||||
|
||||
private:
|
||||
// Make it a friend to allow accessing our m_headers.
|
||||
friend class wxWebRequest;
|
||||
|
||||
wxWebRequestHeaderMap m_headers;
|
||||
wxString m_tempDir;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebSessionImpl);
|
||||
};
|
||||
|
||||
#endif // _WX_PRIVATE_WEBREQUEST_H_
|
||||
|
@@ -19,13 +19,15 @@
|
||||
|
||||
#include "curl/curl.h"
|
||||
|
||||
class wxWebResponseCURL;
|
||||
class wxWebRequestCURL;
|
||||
class wxWebResponseCURL;
|
||||
class wxWebSessionCURL;
|
||||
|
||||
class WXDLLIMPEXP_NET wxWebAuthChallengeCURL : public wxWebAuthChallenge
|
||||
class wxWebAuthChallengeCURL : public wxWebAuthChallengeImpl
|
||||
{
|
||||
public:
|
||||
wxWebAuthChallengeCURL(Source source, wxWebRequestCURL& request);
|
||||
wxWebAuthChallengeCURL(wxWebAuthChallenge::Source source,
|
||||
wxWebRequestCURL& request);
|
||||
|
||||
bool Init();
|
||||
|
||||
@@ -37,10 +39,14 @@ private:
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebAuthChallengeCURL);
|
||||
};
|
||||
|
||||
class WXDLLIMPEXP_NET wxWebRequestCURL : public wxWebRequest
|
||||
class wxWebRequestCURL : public wxWebRequestImpl
|
||||
{
|
||||
public:
|
||||
wxWebRequestCURL(wxWebSession& session, int id, const wxString& url);
|
||||
wxWebRequestCURL(wxWebSession& session,
|
||||
wxWebSessionCURL& sessionImpl,
|
||||
wxEvtHandler* handler,
|
||||
const wxString& url,
|
||||
int id);
|
||||
|
||||
~wxWebRequestCURL();
|
||||
|
||||
@@ -48,10 +54,11 @@ public:
|
||||
|
||||
void Cancel() wxOVERRIDE;
|
||||
|
||||
wxWebResponse* GetResponse() const wxOVERRIDE;
|
||||
wxWebResponseImplPtr GetResponse() const wxOVERRIDE
|
||||
{ return m_response; }
|
||||
|
||||
wxWebAuthChallenge* GetAuthChallenge() const wxOVERRIDE
|
||||
{ return m_authChallenge.get(); }
|
||||
wxWebAuthChallengeImplPtr GetAuthChallenge() const wxOVERRIDE
|
||||
{ return m_authChallenge; }
|
||||
|
||||
wxFileOffset GetBytesSent() const wxOVERRIDE;
|
||||
|
||||
@@ -68,11 +75,13 @@ public:
|
||||
size_t ReadData(char* buffer, size_t size);
|
||||
|
||||
private:
|
||||
wxWebSessionCURL& m_sessionImpl;
|
||||
|
||||
CURL* m_handle;
|
||||
char m_errorBuffer[CURL_ERROR_SIZE];
|
||||
struct curl_slist *m_headerList;
|
||||
wxScopedPtr<wxWebResponseCURL> m_response;
|
||||
wxScopedPtr<wxWebAuthChallengeCURL> m_authChallenge;
|
||||
wxObjectDataPtr<wxWebResponseCURL> m_response;
|
||||
wxObjectDataPtr<wxWebAuthChallengeCURL> m_authChallenge;
|
||||
wxFileOffset m_bytesSent;
|
||||
|
||||
void DestroyHeaderList();
|
||||
@@ -80,10 +89,10 @@ private:
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebRequestCURL);
|
||||
};
|
||||
|
||||
class WXDLLIMPEXP_NET wxWebResponseCURL : public wxWebResponse
|
||||
class wxWebResponseCURL : public wxWebResponseImpl
|
||||
{
|
||||
public:
|
||||
wxWebResponseCURL(wxWebRequest& request);
|
||||
explicit wxWebResponseCURL(wxWebRequestCURL& request);
|
||||
|
||||
wxInt64 GetContentLength() const wxOVERRIDE;
|
||||
|
||||
@@ -109,14 +118,18 @@ private:
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebResponseCURL);
|
||||
};
|
||||
|
||||
class WXDLLIMPEXP_NET wxWebSessionCURL : public wxWebSession, private wxThreadHelper
|
||||
class wxWebSessionCURL : public wxWebSessionImpl, private wxThreadHelper
|
||||
{
|
||||
public:
|
||||
wxWebSessionCURL();
|
||||
|
||||
~wxWebSessionCURL();
|
||||
|
||||
wxWebRequest* CreateRequest(const wxString& url, int id = wxID_ANY) wxOVERRIDE;
|
||||
wxWebRequestImplPtr
|
||||
CreateRequest(wxWebSession& session,
|
||||
wxEvtHandler* handler,
|
||||
const wxString& url,
|
||||
int id = wxID_ANY) wxOVERRIDE;
|
||||
|
||||
wxVersionInfo GetLibraryVersionInfo() wxOVERRIDE;
|
||||
|
||||
@@ -140,10 +153,10 @@ private:
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebSessionCURL);
|
||||
};
|
||||
|
||||
class WXDLLIMPEXP_NET wxWebSessionFactoryCURL : public wxWebSessionFactory
|
||||
class wxWebSessionFactoryCURL : public wxWebSessionFactory
|
||||
{
|
||||
public:
|
||||
wxWebSession* Create() wxOVERRIDE
|
||||
wxWebSessionImpl* Create() wxOVERRIDE
|
||||
{ return new wxWebSessionCURL(); }
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user