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