From 4c821525b425664a2bf968597d243b069c088171 Mon Sep 17 00:00:00 2001 From: Josh Tynjala Date: Thu, 13 Oct 2022 11:56:49 -0700 Subject: [PATCH] CURLBindings: fix "Send failed since rewinding of the data stream failed" error with 302 redirect When CURLOPT_READDATA is specified, not only must we set CURLOPT_READFUNCTION, but we must also set CURLOPT_SEEKFUNCTION to ensure that the data can be resent if there's a redirect. --- project/src/net/curl/CURLBindings.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/project/src/net/curl/CURLBindings.cpp b/project/src/net/curl/CURLBindings.cpp index d7f28b18c..2bbb095ea 100644 --- a/project/src/net/curl/CURLBindings.cpp +++ b/project/src/net/curl/CURLBindings.cpp @@ -1287,6 +1287,13 @@ namespace lime { } + static int seek_callback (void *userp, curl_off_t offset, int origin) { + if (origin == SEEK_SET) { + readBytesPosition[userp] = offset; + return CURL_SEEKFUNC_OK; + } + return CURL_SEEKFUNC_CANTSEEK; + } static size_t read_callback (void *buffer, size_t size, size_t nmemb, void *userp) { @@ -1634,6 +1641,9 @@ namespace lime { readBytesPosition[handle] = 0; readBytesRoot[handle] = new ValuePointer (bytes); + // seek function is needed to support redirects + curl_easy_setopt (easy_handle, CURLOPT_SEEKFUNCTION, seek_callback); + curl_easy_setopt (easy_handle, CURLOPT_SEEKDATA, handle); code = curl_easy_setopt (easy_handle, CURLOPT_READFUNCTION, read_callback); curl_easy_setopt (easy_handle, CURLOPT_READDATA, handle); @@ -2061,6 +2071,8 @@ namespace lime { readBytesPosition[handle] = 0; readBytesRoot[handle] = new ValuePointer ((vobj*)bytes); + curl_easy_setopt (easy_handle, CURLOPT_SEEKFUNCTION, seek_callback); + curl_easy_setopt (easy_handle, CURLOPT_SEEKDATA, handle); code = curl_easy_setopt (easy_handle, CURLOPT_READFUNCTION, read_callback); curl_easy_setopt (easy_handle, CURLOPT_READDATA, handle);