Move response data handling to base class
This commit is contained in:
parent
5660565081
commit
6530e3c08e
@ -32,22 +32,13 @@ public:
|
||||
|
||||
wxString GetStatusText() const wxOVERRIDE;
|
||||
|
||||
wxInputStream* GetStream() const wxOVERRIDE;
|
||||
|
||||
wxString AsString(wxMBConv* conv = NULL) const wxOVERRIDE;
|
||||
|
||||
bool ReadData();
|
||||
|
||||
bool ReportAvailableData(DWORD dataLen);
|
||||
|
||||
void ReportDataComplete();
|
||||
|
||||
private:
|
||||
wxWebRequestWinHTTP& m_request;
|
||||
HINTERNET m_requestHandle;
|
||||
wxInt64 m_contentLength;
|
||||
long m_readSize;
|
||||
wxMemoryBuffer m_readBuffer;
|
||||
wxScopedPtr<wxInputStream> m_stream;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebResponseWinHTTP);
|
||||
};
|
||||
|
@ -40,12 +40,12 @@ public:
|
||||
State_Cancelled
|
||||
};
|
||||
|
||||
enum Storage
|
||||
{
|
||||
Storage_Memory,
|
||||
Storage_File,
|
||||
Storage_None
|
||||
};
|
||||
enum Storage
|
||||
{
|
||||
Storage_Memory,
|
||||
Storage_File,
|
||||
Storage_None
|
||||
};
|
||||
|
||||
virtual ~wxWebRequest() { }
|
||||
|
||||
@ -60,8 +60,10 @@ public:
|
||||
|
||||
void SetIgnoreServerErrorStatus(bool ignore) { m_ignoreServerErrorStatus = ignore; }
|
||||
|
||||
virtual void SetStorage(Storage storage);
|
||||
|
||||
virtual void SetStorage(Storage storage) { m_storage = storage; }
|
||||
|
||||
Storage GetStorage() const { return m_storage; }
|
||||
|
||||
virtual void Start() = 0;
|
||||
|
||||
virtual void Cancel() = 0;
|
||||
@ -128,16 +130,27 @@ public:
|
||||
|
||||
virtual wxString GetStatusText() const = 0;
|
||||
|
||||
virtual wxInputStream* GetStream() const = 0;
|
||||
virtual wxInputStream* GetStream() const;
|
||||
|
||||
virtual wxString GetSuggestedFileName() const;
|
||||
|
||||
virtual wxString AsString(wxMBConv* conv = NULL) const = 0;
|
||||
wxString AsString(wxMBConv* conv = NULL) const;
|
||||
|
||||
protected:
|
||||
wxWebResponse() { }
|
||||
wxWebRequest& m_request;
|
||||
size_t m_readSize;
|
||||
|
||||
wxWebResponse(wxWebRequest& request):
|
||||
m_request(request), m_readSize(8 * 1024) { }
|
||||
|
||||
void* GetDataBuffer(size_t sizeNeeded);
|
||||
|
||||
void ReportDataReceived(size_t sizeReceived);
|
||||
|
||||
private:
|
||||
wxMemoryBuffer m_readBuffer;
|
||||
mutable wxScopedPtr<wxInputStream> m_stream;
|
||||
|
||||
wxDECLARE_NO_COPY_CLASS(wxWebResponse);
|
||||
};
|
||||
|
||||
|
@ -123,6 +123,17 @@ wxString wxWebResponse::GetMimeType() const
|
||||
return GetHeader("Mime-Type");
|
||||
}
|
||||
|
||||
wxInputStream * wxWebResponse::GetStream() const
|
||||
{
|
||||
if ( !m_stream.get() )
|
||||
{
|
||||
// Create stream
|
||||
m_stream.reset(new wxMemoryInputStream(m_readBuffer.GetData(), m_readBuffer.GetDataLen()));
|
||||
}
|
||||
|
||||
return m_stream.get();
|
||||
}
|
||||
|
||||
wxString wxWebResponse::GetSuggestedFileName() const
|
||||
{
|
||||
wxString suggestedFilename;
|
||||
@ -141,6 +152,31 @@ wxString wxWebResponse::GetSuggestedFileName() const
|
||||
return suggestedFilename;
|
||||
}
|
||||
|
||||
wxString wxWebResponse::AsString(wxMBConv * conv) const
|
||||
{
|
||||
// TODO: try to determine encoding type from content-type header
|
||||
if (!conv)
|
||||
conv = &wxConvUTF8;
|
||||
|
||||
if (m_request.GetStorage() == wxWebRequest::Storage_Memory)
|
||||
{
|
||||
size_t outLen = 0;
|
||||
return conv->cMB2WC((const char*)m_readBuffer.GetData(), m_readBuffer.GetDataLen(), &outLen);
|
||||
}
|
||||
else
|
||||
return wxString();
|
||||
}
|
||||
|
||||
void* wxWebResponse::GetDataBuffer(size_t sizeNeeded)
|
||||
{
|
||||
return m_readBuffer.GetAppendBuf(sizeNeeded);
|
||||
}
|
||||
|
||||
void wxWebResponse::ReportDataReceived(size_t sizeReceived)
|
||||
{
|
||||
m_readBuffer.UngetAppendBuf(sizeReceived);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// wxWebSession
|
||||
|
@ -181,10 +181,7 @@ void wxWebRequestWinHTTP::HandleCallback(DWORD dwInternetStatus,
|
||||
SetFailedWithLastError();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_response->ReportDataComplete();
|
||||
SetState(State_Completed);
|
||||
}
|
||||
break;
|
||||
case WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE:
|
||||
WriteData();
|
||||
@ -351,10 +348,10 @@ wxWebResponse* wxWebRequestWinHTTP::GetResponse()
|
||||
//
|
||||
|
||||
wxWebResponseWinHTTP::wxWebResponseWinHTTP(wxWebRequestWinHTTP& request):
|
||||
m_request(request),
|
||||
m_readSize(8 * 1024)
|
||||
wxWebResponse(request),
|
||||
m_requestHandle(request.GetHandle())
|
||||
{
|
||||
wxString contentLengthStr = wxWinHTTPQueryHeaderString(m_request.GetHandle(),
|
||||
wxString contentLengthStr = wxWinHTTPQueryHeaderString(m_requestHandle,
|
||||
WINHTTP_QUERY_CONTENT_LENGTH);
|
||||
if ( contentLengthStr.empty() ||
|
||||
!contentLengthStr.ToLongLong(&m_contentLength) )
|
||||
@ -363,12 +360,12 @@ wxWebResponseWinHTTP::wxWebResponseWinHTTP(wxWebRequestWinHTTP& request):
|
||||
|
||||
wxString wxWebResponseWinHTTP::GetURL() const
|
||||
{
|
||||
return wxWinHTTPQueryOptionString(m_request.GetHandle(), WINHTTP_OPTION_URL);
|
||||
return wxWinHTTPQueryOptionString(m_requestHandle, WINHTTP_OPTION_URL);
|
||||
}
|
||||
|
||||
wxString wxWebResponseWinHTTP::GetHeader(const wxString& name) const
|
||||
{
|
||||
return wxWinHTTPQueryHeaderString(m_request.GetHandle(),
|
||||
return wxWinHTTPQueryHeaderString(m_requestHandle,
|
||||
WINHTTP_QUERY_CUSTOM, name.wc_str());
|
||||
}
|
||||
|
||||
@ -376,7 +373,7 @@ int wxWebResponseWinHTTP::GetStatus() const
|
||||
{
|
||||
DWORD status = 0;
|
||||
DWORD statusSize = sizeof(status);
|
||||
if ( !::WinHttpQueryHeaders(m_request.GetHandle(),
|
||||
if ( !::WinHttpQueryHeaders(m_requestHandle,
|
||||
WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
|
||||
WINHTTP_HEADER_NAME_BY_INDEX, &status, &statusSize, 0) )
|
||||
{
|
||||
@ -389,28 +386,13 @@ int wxWebResponseWinHTTP::GetStatus() const
|
||||
|
||||
wxString wxWebResponseWinHTTP::GetStatusText() const
|
||||
{
|
||||
return wxWinHTTPQueryHeaderString(m_request.GetHandle(), WINHTTP_QUERY_STATUS_TEXT);
|
||||
}
|
||||
|
||||
wxInputStream* wxWebResponseWinHTTP::GetStream() const
|
||||
{
|
||||
return m_stream.get();
|
||||
}
|
||||
|
||||
wxString wxWebResponseWinHTTP::AsString(wxMBConv* conv) const
|
||||
{
|
||||
// TODO: try to determine encoding type from content-type header
|
||||
if ( !conv )
|
||||
conv = &wxConvUTF8;
|
||||
|
||||
size_t outLen = 0;
|
||||
return conv->cMB2WC((const char*) m_readBuffer.GetData(), m_readBuffer.GetDataLen(), &outLen);
|
||||
return wxWinHTTPQueryHeaderString(m_requestHandle, WINHTTP_QUERY_STATUS_TEXT);
|
||||
}
|
||||
|
||||
bool wxWebResponseWinHTTP::ReadData()
|
||||
{
|
||||
if ( ::WinHttpReadData(m_request.GetHandle(),
|
||||
m_readBuffer.GetAppendBuf(m_readSize), m_readSize, NULL) )
|
||||
if ( ::WinHttpReadData(m_requestHandle,
|
||||
GetDataBuffer(m_readSize), m_readSize, NULL) )
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
@ -418,16 +400,11 @@ bool wxWebResponseWinHTTP::ReadData()
|
||||
|
||||
bool wxWebResponseWinHTTP::ReportAvailableData(DWORD dataLen)
|
||||
{
|
||||
m_readBuffer.UngetAppendBuf(dataLen);
|
||||
m_request.m_bytesReceived += dataLen;
|
||||
ReportDataReceived(dataLen);
|
||||
static_cast<wxWebRequestWinHTTP&>(m_request).m_bytesReceived += dataLen;
|
||||
return ReadData();
|
||||
}
|
||||
|
||||
void wxWebResponseWinHTTP::ReportDataComplete()
|
||||
{
|
||||
m_stream.reset(new wxMemoryInputStream(m_readBuffer.GetData(), m_readBuffer.GetDataLen()));
|
||||
}
|
||||
|
||||
//
|
||||
// wxWebAuthChallengeWinHTTP
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user