commit 88c116f222c4c9f362e02b6984f3f5e4cc2c7f7d
parent 02070e64b4dee1d399ffb4cd437a03ec78ce3b39
Author: cfillion <cfillion@users.noreply.github.com>
Date: Tue, 24 Nov 2015 01:05:18 -0500
ensure the download buffer is zero-terminated and fix recursive download restarts
Diffstat:
3 files changed, 9 insertions(+), 22 deletions(-)
diff --git a/src/download.cpp b/src/download.cpp
@@ -69,7 +69,6 @@ void Download::timerTick() // static
plugin_register("-timer", (void*)timerTick);
}
-
Download::StartCode Download::start()
{
if(m_threadHandle)
@@ -131,9 +130,13 @@ DWORD WINAPI Download::worker(void *ptr) // static
const int size = agent.bytes_available();
if(status == 200) {
- char *buffer = new char[size];
+ char *buffer = new char[size + 1];
agent.get_bytes(buffer, size);
+ // get_bytes doesn't zero-terminate the string
+ // without the next line strlen(buffer) cound be anything
+ buffer[size] = '\0';
+
download->finish(status, buffer);
}
else
@@ -166,21 +169,7 @@ void Download::execCallbacks()
}
for(DownloadCallback callback : m_callback)
- callback(status(), contents());
-}
-
-int Download::status()
-{
- WDL_MutexLock lock(&m_mutex);
-
- return m_status;
-}
-
-const char *Download::contents()
-{
- WDL_MutexLock lock(&m_mutex);
-
- return m_contents;
+ callback(m_status, m_contents);
}
bool Download::isFinished()
diff --git a/src/download.hpp b/src/download.hpp
@@ -26,8 +26,6 @@ public:
bool isFinished();
bool isAborted();
- int status();
- const char *contents();
void addCallback(const DownloadCallback &);
StartCode start();
diff --git a/src/main.cpp b/src/main.cpp
@@ -24,13 +24,13 @@ extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
printf("%s\n", GetResourcePath());
- Download *down = new Download("http://perdu.com/3");
+ Download *down = new Download("http://cfillion.tk/");
down->addCallback([=](const int status, const char *contents) {
char strStatus[3];
- sprintf(strStatus, "%d", status);
+ sprintf(strStatus, "%zu", strlen(contents));
ShowMessageBox(contents, strStatus, 0);
- delete down;
+ down->start();
});
down->start();