Change wxWebRequest API to use STATE event
This commit is contained in:
parent
d756159f5d
commit
701f697fa4
@ -32,9 +32,18 @@ public:
|
||||
enum State
|
||||
{
|
||||
State_Idle,
|
||||
State_Unauthorized,
|
||||
State_UnauthorizedProxy,
|
||||
State_Active,
|
||||
State_Ready,
|
||||
State_Failed
|
||||
State_Completed,
|
||||
State_Failed,
|
||||
State_Cancelled
|
||||
};
|
||||
|
||||
enum CredentialTarget
|
||||
{
|
||||
CredentialTarget_Server,
|
||||
CredentialTarget_Proxy
|
||||
};
|
||||
|
||||
virtual ~wxWebRequest() { }
|
||||
@ -48,6 +57,9 @@ public:
|
||||
|
||||
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; }
|
||||
|
||||
virtual void Start() = 0;
|
||||
@ -79,13 +91,10 @@ protected:
|
||||
private:
|
||||
int m_id;
|
||||
State m_state;
|
||||
wxString m_failMessage;
|
||||
bool m_ignoreServerErrorStatus;
|
||||
wxCharBuffer m_dataText;
|
||||
|
||||
void ProcessReadyEvent();
|
||||
|
||||
void ProcessFailedEvent();
|
||||
void ProcessStateEvent(State state, const wxString& failMsg);
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebRequest);
|
||||
};
|
||||
@ -167,12 +176,14 @@ class WXDLLIMPEXP_NET wxWebRequestEvent : public wxEvent
|
||||
{
|
||||
public:
|
||||
wxWebRequestEvent() {}
|
||||
wxWebRequestEvent(wxEventType type, int id, wxWebResponse* response = NULL,
|
||||
const wxString& errorDesc = "")
|
||||
wxWebRequestEvent(wxEventType type, int id, wxWebRequest::State state,
|
||||
wxWebResponse* response = NULL, const wxString& errorDesc = "")
|
||||
: 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; }
|
||||
|
||||
const wxString& GetErrorDescription() const { return m_errorDescription; }
|
||||
@ -180,14 +191,15 @@ public:
|
||||
wxEvent* Clone() const wxOVERRIDE { return new wxWebRequestEvent(*this); }
|
||||
|
||||
private:
|
||||
wxWebRequest::State m_state;
|
||||
wxWebResponse* m_response;
|
||||
wxString m_errorDescription;
|
||||
};
|
||||
|
||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_READY, wxWebRequestEvent);
|
||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_FAILED, wxWebRequestEvent);
|
||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_AUTH_REQUIRED, wxWebRequestEvent);
|
||||
|
||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_STATE, wxWebRequestEvent);
|
||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_DATA, wxWebRequestEvent);
|
||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_DOWNLOAD_PROGRESS, wxWebRequestEvent);
|
||||
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_NET, wxEVT_WEBREQUEST_UPLOAD_PROGRESS, wxWebRequestEvent);
|
||||
|
||||
#endif // wxUSE_WEBREQUEST
|
||||
|
||||
|
@ -136,8 +136,8 @@ public:
|
||||
wxObjectDataPtr<wxWebRequest> request(wxWebSession::GetDefault().CreateRequest(
|
||||
m_urlTextCtrl->GetValue()));
|
||||
|
||||
// Bind event for failure
|
||||
request->Bind(wxEVT_WEBREQUEST_FAILED, &WebRequestFrame::OnWebRequestFailed, this);
|
||||
// Bind event for state change
|
||||
request->Bind(wxEVT_WEBREQUEST_STATE, &WebRequestFrame::OnWebRequestState, this);
|
||||
|
||||
// Prepare request based on selected action
|
||||
switch (m_notebook->GetSelection())
|
||||
@ -145,8 +145,6 @@ public:
|
||||
case Page_Image:
|
||||
// Reset static bitmap 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;
|
||||
case Page_Text:
|
||||
// Reset response text control
|
||||
@ -158,8 +156,6 @@ public:
|
||||
request->SetData(m_postRequestTextCtrl->GetValue(),
|
||||
m_postContentTypeTextCtrl->GetValue());
|
||||
}
|
||||
|
||||
request->Bind(wxEVT_WEBREQUEST_READY, &WebRequestFrame::OnTextRequestReady, this);
|
||||
break;
|
||||
case Page_Download:
|
||||
// TODO: implement
|
||||
@ -175,30 +171,37 @@ public:
|
||||
request->Start();
|
||||
}
|
||||
|
||||
void OnImageRequestReady(wxWebRequestEvent& evt)
|
||||
void OnWebRequestState(wxWebRequestEvent& evt)
|
||||
{
|
||||
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()));
|
||||
m_startButton->Enable();
|
||||
}
|
||||
m_startButton->Enable(evt.GetState() != wxWebRequest::State_Active);
|
||||
|
||||
void OnTextRequestReady(wxWebRequestEvent& evt)
|
||||
{
|
||||
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()));
|
||||
m_startButton->Enable();
|
||||
}
|
||||
switch (evt.GetState())
|
||||
{
|
||||
case wxWebRequest::State_Completed:
|
||||
switch (m_notebook->GetSelection())
|
||||
{
|
||||
case Page_Image:
|
||||
{
|
||||
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)
|
||||
{
|
||||
wxLogError("Web Request failed: %s", evt.GetErrorDescription());
|
||||
GetStatusBar()->SetStatusText("");
|
||||
m_startButton->Enable();
|
||||
break;
|
||||
case wxWebRequest::State_Failed:
|
||||
wxLogError("Web Request failed: %s", evt.GetErrorDescription());
|
||||
GetStatusBar()->SetStatusText("");
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
#endif
|
||||
|
||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_READY, wxWebRequestEvent);
|
||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_FAILED, wxWebRequestEvent);
|
||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_AUTH_REQUIRED, wxWebRequestEvent);
|
||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_STATE, wxWebRequestEvent);
|
||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_DATA, wxWebRequestEvent);
|
||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_DOWNLOAD_PROGRESS, wxWebRequestEvent);
|
||||
wxDEFINE_EVENT(wxEVT_WEBREQUEST_UPLOAD_PROGRESS, wxWebRequestEvent);
|
||||
|
||||
//
|
||||
// wxWebRequest
|
||||
@ -56,13 +57,13 @@ bool wxWebRequest::CheckServerStatus()
|
||||
return true;
|
||||
}
|
||||
|
||||
void wxWebRequest::SetData(const wxString& text, const wxString& contentType, const wxMBConv& conv)
|
||||
void wxWebRequest::SetData(const wxString& text, const wxString& contentType, const wxMBConv& conv)
|
||||
{
|
||||
m_dataText = text.mb_str(conv);
|
||||
SetData(wxSharedPtr<wxInputStream>(new wxMemoryInputStream(m_dataText, m_dataText.length())), contentType);
|
||||
}
|
||||
|
||||
void wxWebRequest::SetData(wxSharedPtr<wxInputStream> dataStream, const wxString& contentType, wxFileOffset dataSize)
|
||||
}
|
||||
|
||||
void wxWebRequest::SetData(wxSharedPtr<wxInputStream> dataStream, const wxString& contentType, wxFileOffset dataSize)
|
||||
{
|
||||
m_dataStream = dataStream;
|
||||
if ( m_dataStream.get() )
|
||||
@ -80,51 +81,33 @@ void wxWebRequest::SetData(wxSharedPtr<wxInputStream> dataStream, const wxString
|
||||
m_dataSize = 0;
|
||||
|
||||
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)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case State_Active:
|
||||
// Add a reference while the request is active
|
||||
if ( m_state != State_Active )
|
||||
{
|
||||
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;
|
||||
}
|
||||
// Add a reference while the request is active
|
||||
if (state == State_Active && m_state != State_Active)
|
||||
IncRef();
|
||||
|
||||
// Trigger the event in the main thread
|
||||
CallAfter(&wxWebRequest::ProcessStateEvent, state, failMsg);
|
||||
}
|
||||
|
||||
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);
|
||||
// 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();
|
||||
m_state = State_Ready;
|
||||
}
|
||||
|
||||
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;
|
||||
m_state = state;
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -180,7 +180,7 @@ void wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus,
|
||||
else
|
||||
{
|
||||
m_response->ReportDataComplete();
|
||||
SetState(State_Ready);
|
||||
SetState(State_Completed);
|
||||
}
|
||||
break;
|
||||
case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
|
||||
@ -195,7 +195,7 @@ void wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus,
|
||||
}
|
||||
}
|
||||
|
||||
void wxWebRequestWinHTTP::WriteData()
|
||||
void wxWebRequestWinHTTP::WriteData()
|
||||
{
|
||||
int dataWriteSize = 8 * 1024;
|
||||
if ( m_dataWritten + dataWriteSize > m_dataSize )
|
||||
@ -211,10 +211,10 @@ void wxWebRequestWinHTTP::WriteData()
|
||||
m_dataWritten += dataWriteSize;
|
||||
}
|
||||
else
|
||||
CreateResponse();
|
||||
}
|
||||
|
||||
void wxWebRequestWinHTTP::CreateResponse()
|
||||
CreateResponse();
|
||||
}
|
||||
|
||||
void wxWebRequestWinHTTP::CreateResponse()
|
||||
{
|
||||
if (::WinHttpReceiveResponse(m_request, NULL))
|
||||
{
|
||||
@ -228,8 +228,8 @@ void wxWebRequestWinHTTP::CreateResponse()
|
||||
}
|
||||
else
|
||||
SetFailedWithLastError();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void wxWebRequestWinHTTP::SetFailedWithLastError()
|
||||
{
|
||||
wxString failMessage = wxWinHTTPErrorToString(::GetLastError());
|
||||
@ -319,7 +319,7 @@ void wxWebRequestWinHTTP::Start()
|
||||
|
||||
void wxWebRequestWinHTTP::Cancel()
|
||||
{
|
||||
// TODO: implement
|
||||
wxFAIL_MSG("not implemented");
|
||||
}
|
||||
|
||||
wxWebResponse* wxWebRequestWinHTTP::GetResponse()
|
||||
|
Loading…
Reference in New Issue
Block a user