Skip to content

Commit

Permalink
Fixed seeking not working on HTTP handles
Browse files Browse the repository at this point in the history
This is only activated in standards mode due to using more memory.
  • Loading branch information
MCJack123 committed Mar 4, 2024
1 parent 39f4e60 commit a0a56ba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/apis/handles/http_handle.cpp
Expand Up @@ -28,6 +28,7 @@ int http_handle_free(lua_State *L) {
lastCFunction = __func__;
http_handle_t** handle = (http_handle_t**)lua_touserdata(L, 1);
if (*handle != NULL) {
if (config.standardsMode) delete (std::stringstream*)(*handle)->stream;
delete (*handle)->handle;
delete (*handle)->session;
delete *handle;
Expand All @@ -40,6 +41,7 @@ int http_handle_close(lua_State *L) {
lastCFunction = __func__;
http_handle_t** handle = (http_handle_t**)lua_touserdata(L, lua_upvalueindex(1));
if (*handle == NULL) return 0;
if (config.standardsMode) delete (std::stringstream*)(*handle)->stream;
delete (*handle)->handle;
delete (*handle)->session;
delete *handle;
Expand Down
15 changes: 14 additions & 1 deletion src/apis/http.cpp
Expand Up @@ -324,7 +324,17 @@ static void downloadThread(void* arg) {
}
http_handle_t * handle;
try {
handle = new http_handle_t(&session->receiveResponse(*response));
if (config.standardsMode) {
// Fix seeking by reading the entire data into a stringstream
std::istream& instream = session->receiveResponse(*response);
std::stringstream * ss = new std::stringstream;
while (!instream.eof()) {
char buf[4096];
instream.read(buf, 4096);
ss->write(buf, instream.gcount());
}
handle = new http_handle_t(ss);
} else handle = new http_handle_t(&session->receiveResponse(*response));
} catch (Poco::TimeoutException &e) {
http_handle_t * err = new http_handle_t(NULL);
err->url = param->url;
Expand All @@ -348,6 +358,8 @@ static void downloadThread(void* arg) {
err->url = param->url;
err->failureReason = "Response is too large";
queueEvent(param->comp, http_failure, err);
if (config.standardsMode) delete (std::stringstream*)handle->stream;
delete handle;
delete response;
delete session;
goto downloadThread_finish;
Expand All @@ -361,6 +373,7 @@ static void downloadThread(void* arg) {
if (location[0] == '/') location = uri.getScheme() + "://" + uri.getHost() + location;
else location = uri.getScheme() + "://" + uri.getHost() + path.substr(0, path.find('?')) + "/" + location;
}
if (config.standardsMode) delete (std::stringstream*)handle->stream;
delete handle->handle;
delete handle->session;
delete handle;
Expand Down

0 comments on commit a0a56ba

Please sign in to comment.