Move next_request to run_cb_and_clear

nfs-proxy-old
Vitaliy Filippov 2022-02-19 16:47:47 +03:00
parent 390239c51b
commit 88402e6eb6
1 changed files with 12 additions and 22 deletions

View File

@ -129,6 +129,7 @@ void http_co_t::run_cb_and_clear()
// Call callback after clearing it because otherwise we may hit reenterability problems // Call callback after clearing it because otherwise we may hit reenterability problems
if (cb != NULL) if (cb != NULL)
cb(&parsed); cb(&parsed);
next_request();
} }
void http_co_t::send_request(const std::string & host, const std::string & request, void http_co_t::send_request(const std::string & host, const std::string & request,
@ -178,7 +179,6 @@ void http_co_t::send_request(const std::string & host, const std::string & reque
close_connection(); close_connection();
parsed = { .error = "HTTP request timed out" }; parsed = { .error = "HTTP request timed out" };
run_cb_and_clear(); run_cb_and_clear();
next_request();
} }
stackout(); stackout();
}); });
@ -309,7 +309,6 @@ void http_co_t::start_connection()
close_connection(); close_connection();
parsed = { .error = std::string("connect: ")+strerror(errno) }; parsed = { .error = std::string("connect: ")+strerror(errno) };
run_cb_and_clear(); run_cb_and_clear();
next_request();
stackout(); stackout();
return; return;
} }
@ -343,7 +342,6 @@ void http_co_t::handle_events()
{ {
close_connection(); close_connection();
run_cb_and_clear(); run_cb_and_clear();
next_request();
break; break;
} }
} }
@ -365,7 +363,6 @@ void http_co_t::handle_connect_result()
close_connection(); close_connection();
parsed = { .error = std::string("connect: ")+strerror(result) }; parsed = { .error = std::string("connect: ")+strerror(result) };
run_cb_and_clear(); run_cb_and_clear();
next_request();
stackout(); stackout();
return; return;
} }
@ -405,7 +402,6 @@ again:
close_connection(); close_connection();
parsed = { .error = std::string("sendmsg: ")+strerror(errno) }; parsed = { .error = std::string("sendmsg: ")+strerror(errno) };
run_cb_and_clear(); run_cb_and_clear();
next_request();
stackout(); stackout();
return; return;
} }
@ -456,7 +452,6 @@ again:
close_connection(); close_connection();
parsed = { .error = "HTTP request timed out" }; parsed = { .error = "HTTP request timed out" };
run_cb_and_clear(); run_cb_and_clear();
next_request();
} }
} }
else else
@ -472,7 +467,6 @@ again:
if (res < 0) if (res < 0)
parsed = { .error = std::string("recvmsg: ")+strerror(-res) }; parsed = { .error = std::string("recvmsg: ")+strerror(-res) };
run_cb_and_clear(); run_cb_and_clear();
next_request();
} }
else else
{ {
@ -523,7 +517,6 @@ bool http_co_t::handle_read()
close_connection(); close_connection();
parsed = { .error = "Response has neither Connection: close, nor Transfer-Encoding: chunked nor Content-Length headers" }; parsed = { .error = "Response has neither Connection: close, nor Transfer-Encoding: chunked nor Content-Length headers" };
run_cb_and_clear(); run_cb_and_clear();
next_request();
stackout(); stackout();
return false; return false;
} }
@ -537,8 +530,11 @@ bool http_co_t::handle_read()
if (state == HTTP_CO_HEADERS_RECEIVED && target_response_size > 0 && response.size() >= target_response_size) if (state == HTTP_CO_HEADERS_RECEIVED && target_response_size > 0 && response.size() >= target_response_size)
{ {
std::swap(parsed.body, response); std::swap(parsed.body, response);
response_callback(&parsed); if (!keepalive)
parsed.eof = true; close_connection();
else
state = HTTP_CO_KEEPALIVE;
run_cb_and_clear();
} }
else if (state == HTTP_CO_CHUNKED && response.size() > 0) else if (state == HTTP_CO_CHUNKED && response.size() > 0)
{ {
@ -569,10 +565,14 @@ bool http_co_t::handle_read()
response_callback(&parsed); response_callback(&parsed);
parsed.body = ""; parsed.body = "";
} }
if (parsed.eof && !want_streaming) else if (parsed.eof)
{ {
// Normal response // Normal response
response_callback(&parsed); if (!keepalive)
close_connection();
else
state = HTTP_CO_KEEPALIVE;
run_cb_and_clear();
} }
} }
else if (state == HTTP_CO_WEBSOCKET && response.size() > 0) else if (state == HTTP_CO_WEBSOCKET && response.size() > 0)
@ -583,16 +583,6 @@ bool http_co_t::handle_read()
parsed.body = ""; parsed.body = "";
} }
} }
if (parsed.eof)
{
response_callback = NULL;
parsed = {};
if (!keepalive)
close_connection();
else
state = HTTP_CO_KEEPALIVE;
next_request();
}
stackout(); stackout();
return true; return true;
} }