Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming response bodies #28

Merged
merged 5 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ namespace crow

private:

void prepare_buffers(){
void prepare_buffers()
{
//auto self = this->shared_from_this();
res.complete_request_handler_ = nullptr;

Expand Down Expand Up @@ -465,10 +466,11 @@ namespace crow

}
#if !defined(_WIN32)
void do_write_static(){
void do_write_static()
{
is_writing = true;
boost::asio::write(adaptor_.socket(), buffers_);
res.do_write_sendfile(&adaptor_);
res.do_stream_file(adaptor_);

res.end();
res.clear();
Expand All @@ -483,17 +485,31 @@ namespace crow
buffers_.clear();
}
#endif
void do_write_general(){
res_body_copy_.swap(res.body);
buffers_.emplace_back(res_body_copy_.data(), res_body_copy_.size());
void do_write_general()
{
if (res.body.length() < megabyte)
{
res_body_copy_.swap(res.body);
buffers_.emplace_back(res_body_copy_.data(), res_body_copy_.size());

do_write();
do_write();

if (need_to_start_read_after_complete_)
if (need_to_start_read_after_complete_)
{
need_to_start_read_after_complete_ = false;
start_deadline();
do_read();
}
}
else
{
need_to_start_read_after_complete_ = false;
start_deadline();
do_read();
is_writing = true;
boost::asio::write(adaptor_.socket(), buffers_);
res.do_stream_body(adaptor_);

res.end();
res.clear();
buffers_.clear();
}
}

Expand Down Expand Up @@ -608,6 +624,8 @@ namespace crow

boost::array<char, 4096> buffer_;

const uint megabyte = 1048576;
The-EDev marked this conversation as resolved.
Show resolved Hide resolved

HTTPParser<Connection> parser_;
request req_;
response res;
Expand Down
55 changes: 36 additions & 19 deletions include/crow/http_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <unordered_map>
#include <ios>
#include <fstream>
#include <sstream>

#include "crow/json.h"
#include "crow/http_request.h"
Expand Down Expand Up @@ -141,6 +142,7 @@ namespace crow
};
static_file_info file_info;

///Return a static file as the response body
void set_static_file_info(std::string path){
file_info.path = path;
file_info.statResult = stat(file_info.path.c_str(), &file_info.statbuf);
Expand All @@ -151,7 +153,7 @@ namespace crow
std::string mimeType = "";
code = 200;
this->add_header("Content-length", std::to_string(file_info.statbuf.st_size));

if (extension != ""){
mimeType = mime_types[extension];
if (mimeType != "")
Expand All @@ -168,45 +170,60 @@ namespace crow
}

template<typename Adaptor>
void do_write_sendfile(Adaptor adaptor) {

void do_stream_file(Adaptor& adaptor)
{
if (file_info.statResult == 0)
{

std::ifstream is(file_info.path.c_str(), std::ios::in | std::ios::binary);
write_streamed(is, adaptor);
}
}

template<typename Adaptor>
void do_stream_body(Adaptor& adaptor)
{
if (body.length() > 0)
{
std::istringstream is(body);
write_streamed(is, adaptor);
}
}
#endif
/* static file support end */
private:
bool completed_{};
std::function<void()> complete_request_handler_;
std::function<bool()> is_alive_helper_;

//In case of a JSON object, set the Content-Type header
void json_mode()
{
set_header("Content-Type", "application/json");
}

template<typename Stream, typename Adaptor>
void write_streamed(Stream& is, Adaptor& adaptor)
{
char buf[16384];
while (is.read(buf, sizeof(buf)).gcount() > 0)
{
std::vector<asio::const_buffer> buffers;
buffers.push_back(boost::asio::buffer(buf));
boost::asio::write(adaptor->socket(), buffers, [this](std::error_code ec, std::size_t)
boost::asio::write(adaptor.socket(), buffers, [this](std::error_code ec, std::size_t)
{
if (!ec)
{
//CROW_LOG_DEBUG << "sending file, no error";
return false;
}
else
{
CROW_LOG_ERROR << ec << " - happened while sending file";
CROW_LOG_ERROR << ec << " - happened while sending body";
this->end();
return true;
}
});
}
}
}
#endif
/* static file support end */
private:
bool completed_{};
std::function<void()> complete_request_handler_;
std::function<bool()> is_alive_helper_;

//In case of a JSON object, set the Content-Type header
void json_mode()
{
set_header("Content-Type", "application/json");
}
};
}
36 changes: 36 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1277,3 +1277,39 @@ TEST_CASE("multipart")
REQUIRE(test_string == res.body);
}
}

TEST_CASE("stream_response")
{

SimpleApp app;

CROW_ROUTE(app, "/test")
([](const crow::request&, crow::response& res)
{
std::string keyword_ = "hello";
std::string key_response;
for (unsigned int i = 0; i<1000000; i++)
key_response += keyword_;
res.body = key_response;
res.end();
});

app.validate();

{
std::string keyword_ = "hello";
std::string key_response;
for (unsigned int i = 0; i<1000000; i++)
key_response += keyword_;

request req;
response res;

req.url = "/test";

app.handle(req, res);

REQUIRE(key_response == res.body);
}

}