Change wxWebRequest API to use STATE event
This commit is contained in:
@@ -32,9 +32,18 @@ public:
|
|||||||
enum State
|
enum State
|
||||||
{
|
{
|
||||||
State_Idle,
|
State_Idle,
|
||||||
|
State_Unauthorized,
|
||||||
|
State_UnauthorizedProxy,
|
||||||
State_Active,
|
State_Active,
|
||||||
State_Ready,
|
State_Completed,
|
||||||
State_Failed
|
State_Failed,
|
||||||
|
State_Cancelled
|
||||||
|
};
|
||||||
|
|
||||||
|
enum CredentialTarget
|
||||||
|
{
|
||||||
|
CredentialTarget_Server,
|
||||||
|
CredentialTarget_Proxy
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual ~wxWebRequest() { }
|
virtual ~wxWebRequest() { }
|
||||||
@@ -48,6 +57,9 @@ public:
|
|||||||
|
|
||||||
void SetData(wxSharedPtr<wxInputStream> dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset);
|
void SetData(wxSharedPtr<wxInputStream> dataStream, const wxString& contentType, wxFileOffset dataSize = wxInvalidOffset);
|
||||||
|
|
||||||
|
void SetCredentials(const wxString& user, const wxString& password,
|
||||||
|
CredentialTarget target);
|
||||||
|
|
||||||
void SetIgnoreServerErrorStatus(bool ignore) { m_ignoreServerErrorStatus = ignore; }
|
void SetIgnoreServerErrorStatus(bool ignore) { m_ignoreServerErrorStatus = ignore; }
|
||||||
|
|
||||||
virtual void Start() = 0;
|
virtual void Start() = 0;
|
||||||
@@ -79,13 +91,10 @@ protected:
|
|||||||
private:
|
private:
|
||||||
int m_id;
|
int m_id;
|
||||||
State m_state;
|
State m_state;
|
||||||
wxString m_failMessage;
|
|
||||||
bool m_ignoreServerErrorStatus;
|
bool m_ignoreServerErrorStatus;
|
||||||
wxCharBuffer m_dataText;
|
wxCharBuffer m_dataText;
|
||||||
|
|
||||||
void ProcessReadyEvent();
|
void ProcessStateEvent(State state, const wxString& failMsg);
|
||||||
|
|
||||||
void ProcessFailedEvent();
|
|
||||||
|
|
||||||
wxDECLARE_NO_COPY_CLASS(wxWebRequest);
|
wxDECLARE_NO_COPY_CLASS(wxWebRequest);
|
||||||
};
|
};
|
||||||
@@ -167,12 +176,14 @@ class WXDLLIMPEXP_NET wxWebRequestEvent : public wxEvent
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wxWebRequestEvent() {}
|
wxWebRequestEvent() {}
|
||||||
wxWebRequestEvent(wxEventType type, int id, wxWebResponse* response = NULL,
|
wxWebRequestEvent(wxEventType type, int id, wxWebRequest::State state,
|
||||||
const wxString& errorDesc = "")
|
wxWebResponse* response = NULL, const wxString& errorDesc = "")
|
||||||
: wxEvent(id, type),
|
: wxEvent(id, type),
|
||||||
m_response(response), m_errorDescription(errorDesc)
|
m_state(state), m_response(response), m_errorDescription(errorDesc)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
wxWebRequest::State GetState() const { return m_state; }
|
||||||
|
|
||||||
wxWebResponse* GetResponse() const { return m_response; }
|
wxWebResponse* GetResponse() const { return m_response; }
|
||||||
|
|
||||||
const wxString& GetErrorDescription() const { return m_errorDescription; }
|
const wxString& GetErrorDescription() const { return m_errorDescription; }
|
||||||
@@ -180,14 +191,15 @@ public:
|
|||||||
wxEvent* Clone() const wxOVERRIDE { return new wxWebRequestEvent(*this); }
|
wxEvent* Clone() const wxOVERRIDE { return new wxWebRequestEvent(*this); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
wxWebRequest::State m_state;
|
||||||
wxWebResponse* m_response;
|
wxWebResponse* m_response;
|
||||||
wxString m_errorDescription;
|
wxString m_errorDescription;
|
||||||
};
|
};
|
||||||
|
|
||||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_READY, wxWebRequestEvent);
|
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_STATE, wxWebRequestEvent);
|
||||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_FAILED, wxWebRequestEvent);
|
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_DATA, wxWebRequestEvent);
|
||||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_AUTH_REQUIRED, wxWebRequestEvent);
|
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_DOWNLOAD_PROGRESS, wxWebRequestEvent);
|
||||||
|
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_UPLOAD_PROGRESS, wxWebRequestEvent);
|
||||||
|
|
||||||
#endif // wxUSE_WEBREQUEST
|
#endif // wxUSE_WEBREQUEST
|
||||||
|
|
||||||
|
@@ -136,8 +136,8 @@ public:
|
|||||||
wxObjectDataPtr<wxWebRequest> request(wxWebSession::GetDefault().CreateRequest(
|
wxObjectDataPtr<wxWebRequest> request(wxWebSession::GetDefault().CreateRequest(
|
||||||
m_urlTextCtrl->GetValue()));
|
m_urlTextCtrl->GetValue()));
|
||||||
|
|
||||||
// Bind event for failure
|
// Bind event for state change
|
||||||
request->Bind(wxEVT_WEBREQUEST_FAILED, &WebRequestFrame::OnWebRequestFailed, this);
|
request->Bind(wxEVT_WEBREQUEST_STATE, &WebRequestFrame::OnWebRequestState, this);
|
||||||
|
|
||||||
// Prepare request based on selected action
|
// Prepare request based on selected action
|
||||||
switch (m_notebook->GetSelection())
|
switch (m_notebook->GetSelection())
|
||||||
@@ -145,8 +145,6 @@ public:
|
|||||||
case Page_Image:
|
case Page_Image:
|
||||||
// Reset static bitmap image
|
// Reset static bitmap image
|
||||||
m_imageStaticBitmap->SetBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
|
m_imageStaticBitmap->SetBitmap(wxArtProvider::GetBitmap(wxART_MISSING_IMAGE));
|
||||||
// Bind completion event to response as image
|
|
||||||
request->Bind(wxEVT_WEBREQUEST_READY, &WebRequestFrame::OnImageRequestReady, this);
|
|
||||||
break;
|
break;
|
||||||
case Page_Text:
|
case Page_Text:
|
||||||
// Reset response text control
|
// Reset response text control
|
||||||
@@ -158,8 +156,6 @@ public:
|
|||||||
request->SetData(m_postRequestTextCtrl->GetValue(),
|
request->SetData(m_postRequestTextCtrl->GetValue(),
|
||||||
m_postContentTypeTextCtrl->GetValue());
|
m_postContentTypeTextCtrl->GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
request->Bind(wxEVT_WEBREQUEST_READY, &WebRequestFrame::OnTextRequestReady, this);
|
|
||||||
break;
|
break;
|
||||||
case Page_Download:
|
case Page_Download:
|
||||||
// TODO: implement
|
// TODO: implement
|
||||||
@@ -175,30 +171,37 @@ public:
|
|||||||
request->Start();
|
request->Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnImageRequestReady(wxWebRequestEvent& evt)
|
void OnWebRequestState(wxWebRequestEvent& evt)
|
||||||
{
|
{
|
||||||
wxImage img(*evt.GetResponse()->GetStream());
|
m_startButton->Enable(evt.GetState() != wxWebRequest::State_Active);
|
||||||
m_imageStaticBitmap->SetBitmap(img);
|
|
||||||
m_notebook->GetPage(Page_Image)->Layout();
|
|
||||||
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes image data", evt.GetResponse()->GetContentLength()));
|
|
||||||
m_startButton->Enable();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OnTextRequestReady(wxWebRequestEvent& evt)
|
switch (evt.GetState())
|
||||||
{
|
{
|
||||||
m_textResponseTextCtrl->SetValue(evt.GetResponse()->AsString());
|
case wxWebRequest::State_Completed:
|
||||||
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes text data (Status: %d %s)",
|
switch (m_notebook->GetSelection())
|
||||||
evt.GetResponse()->GetContentLength(),
|
{
|
||||||
evt.GetResponse()->GetStatus(),
|
case Page_Image:
|
||||||
evt.GetResponse()->GetStatusText()));
|
{
|
||||||
m_startButton->Enable();
|
wxImage img(*evt.GetResponse()->GetStream());
|
||||||
}
|
m_imageStaticBitmap->SetBitmap(img);
|
||||||
|
m_notebook->GetPage(Page_Image)->Layout();
|
||||||
|
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes image data", evt.GetResponse()->GetContentLength()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Page_Text:
|
||||||
|
m_textResponseTextCtrl->SetValue(evt.GetResponse()->AsString());
|
||||||
|
GetStatusBar()->SetStatusText(wxString::Format("Loaded %lld bytes text data (Status: %d %s)",
|
||||||
|
evt.GetResponse()->GetContentLength(),
|
||||||
|
evt.GetResponse()->GetStatus(),
|
||||||
|
evt.GetResponse()->GetStatusText()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
void OnWebRequestFailed(wxWebRequestEvent& evt)
|
break;
|
||||||
{
|
case wxWebRequest::State_Failed:
|
||||||
wxLogError("Web Request failed: %s", evt.GetErrorDescription());
|
wxLogError("Web Request failed: %s", evt.GetErrorDescription());
|
||||||
GetStatusBar()->SetStatusText("");
|
GetStatusBar()->SetStatusText("");
|
||||||
m_startButton->Enable();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnPostCheckBox(wxCommandEvent& WXUNUSED(evt))
|
void OnPostCheckBox(wxCommandEvent& WXUNUSED(evt))
|
||||||
|
@@ -36,9 +36,10 @@ extern WXDLLIMPEXP_DATA_NET(const char) wxWebSessionBackendCURL[] = "wxWebSessio
|
|||||||
extern WXDLLIMPEXP_DATA_NET(const char) wxWebSessionBackendDefault[] = "wxWebSessionBackendWinHTTP";
|
extern WXDLLIMPEXP_DATA_NET(const char) wxWebSessionBackendDefault[] = "wxWebSessionBackendWinHTTP";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_READY, wxWebRequestEvent);
|
wxDEFINE_EVENT(wxEVT_WEBREQUEST_STATE, wxWebRequestEvent);
|
||||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_FAILED, wxWebRequestEvent);
|
wxDEFINE_EVENT(wxEVT_WEBREQUEST_DATA, wxWebRequestEvent);
|
||||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_AUTH_REQUIRED, wxWebRequestEvent);
|
wxDEFINE_EVENT(wxEVT_WEBREQUEST_DOWNLOAD_PROGRESS, wxWebRequestEvent);
|
||||||
|
wxDEFINE_EVENT(wxEVT_WEBREQUEST_UPLOAD_PROGRESS, wxWebRequestEvent);
|
||||||
|
|
||||||
//
|
//
|
||||||
// wxWebRequest
|
// wxWebRequest
|
||||||
@@ -82,49 +83,31 @@ void wxWebRequest::SetData(wxSharedPtr<wxInputStream> dataStream, const wxString
|
|||||||
SetHeader("Content-Type", contentType);
|
SetHeader("Content-Type", contentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wxWebRequest::SetCredentials(const wxString & user, const wxString & password, CredentialTarget target)
|
||||||
|
{
|
||||||
|
wxFAIL_MSG("not implemented");
|
||||||
|
}
|
||||||
|
|
||||||
void wxWebRequest::SetState(State state, const wxString & failMsg)
|
void wxWebRequest::SetState(State state, const wxString & failMsg)
|
||||||
{
|
{
|
||||||
switch (state)
|
// Add a reference while the request is active
|
||||||
{
|
if (state == State_Active && m_state != State_Active)
|
||||||
case State_Active:
|
IncRef();
|
||||||
// Add a reference while the request is active
|
|
||||||
if ( m_state != State_Active )
|
// Trigger the event in the main thread
|
||||||
{
|
CallAfter(&wxWebRequest::ProcessStateEvent, state, failMsg);
|
||||||
IncRef();
|
|
||||||
m_state = state;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case State_Ready:
|
|
||||||
// Trigger the ready event in main thread
|
|
||||||
CallAfter(&wxWebRequest::ProcessReadyEvent);
|
|
||||||
break;
|
|
||||||
case State_Failed:
|
|
||||||
m_failMessage = failMsg;
|
|
||||||
// Trigger the failed event in main thread
|
|
||||||
CallAfter(&wxWebRequest::ProcessFailedEvent);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxWebRequest::ProcessReadyEvent()
|
void wxWebRequest::ProcessStateEvent(State state, const wxString& failMsg)
|
||||||
{
|
{
|
||||||
wxWebRequestEvent evt(wxEVT_WEBREQUEST_READY, GetId(), GetResponse());
|
wxWebRequestEvent evt(wxEVT_WEBREQUEST_STATE, GetId(), state,
|
||||||
|
GetResponse(), failMsg);
|
||||||
ProcessEvent(evt);
|
ProcessEvent(evt);
|
||||||
// Remove reference after the request is no longer active
|
// Remove reference after the request is no longer active
|
||||||
if ( m_state == State_Active )
|
if (state == State_Completed || state == State_Failed ||
|
||||||
|
state == State_Cancelled)
|
||||||
DecRef();
|
DecRef();
|
||||||
m_state = State_Ready;
|
m_state = state;
|
||||||
}
|
|
||||||
|
|
||||||
void wxWebRequest::ProcessFailedEvent()
|
|
||||||
{
|
|
||||||
wxWebRequestEvent evt(wxEVT_WEBREQUEST_FAILED, GetId(), NULL,
|
|
||||||
m_failMessage);
|
|
||||||
ProcessEvent(evt);
|
|
||||||
// Remove reference after the request is no longer active
|
|
||||||
if ( m_state == State_Active )
|
|
||||||
DecRef();
|
|
||||||
m_state = State_Failed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@@ -180,7 +180,7 @@ void wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus,
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_response->ReportDataComplete();
|
m_response->ReportDataComplete();
|
||||||
SetState(State_Ready);
|
SetState(State_Completed);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
|
case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
|
||||||
@@ -319,7 +319,7 @@ void wxWebRequestWinHTTP::Start()
|
|||||||
|
|
||||||
void wxWebRequestWinHTTP::Cancel()
|
void wxWebRequestWinHTTP::Cancel()
|
||||||
{
|
{
|
||||||
// TODO: implement
|
wxFAIL_MSG("not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
wxWebResponse* wxWebRequestWinHTTP::GetResponse()
|
wxWebResponse* wxWebRequestWinHTTP::GetResponse()
|
||||||
|
Reference in New Issue
Block a user