Ensure correct HTTP request state when getting result

Add assertions to ensure that the HTTP request result data and SHA256 are available when getting them instead of returning `nullptr` and `SHA256_ZEROED` when they are not.

Rename `CHttpRequest::Sha256` function to `ResultSha256`.
This commit is contained in:
Robert Müller 2024-02-05 21:52:17 +01:00
parent c5c9e6c983
commit 78997e55d9
3 changed files with 10 additions and 13 deletions

View file

@ -2064,7 +2064,7 @@ void CClient::ResetDDNetInfoTask()
void CClient::FinishDDNetInfo() void CClient::FinishDDNetInfo()
{ {
if(m_ServerBrowser.DDNetInfoSha256() == m_pDDNetInfoTask->Sha256()) if(m_ServerBrowser.DDNetInfoSha256() == m_pDDNetInfoTask->ResultSha256())
{ {
log_debug("client/info", "DDNet info already up-to-date"); log_debug("client/info", "DDNet info already up-to-date");
return; return;
@ -2082,7 +2082,6 @@ void CClient::FinishDDNetInfo()
unsigned char *pResult; unsigned char *pResult;
size_t ResultLength; size_t ResultLength;
m_pDDNetInfoTask->Result(&pResult, &ResultLength); m_pDDNetInfoTask->Result(&pResult, &ResultLength);
dbg_assert(pResult != nullptr, "Invalid info task state");
bool Error = io_write(File, pResult, ResultLength) != ResultLength; bool Error = io_write(File, pResult, ResultLength) != ResultLength;
Error |= io_close(File) != 0; Error |= io_close(File) != 0;
if(Error) if(Error)

View file

@ -362,12 +362,8 @@ void CHttpRequest::Wait()
void CHttpRequest::Result(unsigned char **ppResult, size_t *pResultLength) const void CHttpRequest::Result(unsigned char **ppResult, size_t *pResultLength) const
{ {
if(m_WriteToFile || State() != EHttpState::DONE) dbg_assert(State() == EHttpState::DONE, "Request not done");
{ dbg_assert(!m_WriteToFile, "Result not usable together with WriteToFile");
*ppResult = nullptr;
*pResultLength = 0;
return;
}
*ppResult = m_pBuffer; *ppResult = m_pBuffer;
*pResultLength = m_ResponseLength; *pResultLength = m_ResponseLength;
} }
@ -377,13 +373,15 @@ json_value *CHttpRequest::ResultJson() const
unsigned char *pResult; unsigned char *pResult;
size_t ResultLength; size_t ResultLength;
Result(&pResult, &ResultLength); Result(&pResult, &ResultLength);
if(!pResult)
{
return nullptr;
}
return json_parse((char *)pResult, ResultLength); return json_parse((char *)pResult, ResultLength);
} }
const SHA256_DIGEST &CHttpRequest::ResultSha256() const
{
dbg_assert(State() == EHttpState::DONE, "Request not done");
return m_ActualSha256;
}
bool CHttp::Init(std::chrono::milliseconds ShutdownDelay) bool CHttp::Init(std::chrono::milliseconds ShutdownDelay)
{ {
m_ShutdownDelay = ShutdownDelay; m_ShutdownDelay = ShutdownDelay;

View file

@ -199,7 +199,7 @@ public:
void Result(unsigned char **ppResult, size_t *pResultLength) const; void Result(unsigned char **ppResult, size_t *pResultLength) const;
json_value *ResultJson() const; json_value *ResultJson() const;
const SHA256_DIGEST &Sha256() const { return m_ActualSha256; } const SHA256_DIGEST &ResultSha256() const;
}; };
inline std::unique_ptr<CHttpRequest> HttpHead(const char *pUrl) inline std::unique_ptr<CHttpRequest> HttpHead(const char *pUrl)