Skip to content

example: send response content-length as part of http header from server #808

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

Merged
merged 3 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion boost/network/protocol/http/message/async_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ struct async_message {
destination_;
mutable boost::shared_future<std::uint16_t> status_;
mutable boost::shared_future<headers_container_type> headers_;
mutable boost::optional<headers_container_type> retrieved_headers_;
mutable headers_container_type added_headers;
mutable std::set<string_type> removed_headers;
mutable boost::shared_future<string_type> body_;
mutable boost::optional<headers_container_type> retrieved_headers_;

friend struct boost::network::http::impl::ready_wrapper<Tag>;
};
Expand Down
20 changes: 13 additions & 7 deletions libs/network/example/http/async_server_file_upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ struct connection_handler {
/// @param [in] conn Connection object
///
void operator()(server::request const& req, const server::connection_ptr& conn) {
static std::map<std::string, std::string> headers = {
std::map<std::string, std::string> headers = {
{"Connection","close"},
{"Content-Length", "0"},
{"Content-Type", "text/plain"}
};

Expand All @@ -206,24 +207,29 @@ struct connection_handler {
// Wait until the data transfer is done by the IO threads
uploader->wait_for_completion();

// Respond to the client
conn->set_status(server::connection::ok);
conn->set_headers(headers);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> diff = end - start;
std::ostringstream stm;
stm << "Took " << diff.count() << " milliseconds for the transfer." << std::endl;
conn->write(stm.str());
auto body = stm.str();
// Respond to the client
headers["Content-Length"] = std::to_string(body.size());
conn->set_status(server::connection::ok);
conn->set_headers(headers);
conn->write(body);
} catch (const file_uploader_exception& e) {
const std::string err = e.what();
headers["Content-Length"] = std::to_string(err.size());
conn->set_status(server::connection::bad_request);
conn->set_headers(headers);
const std::string err = e.what();
conn->write(err);
}
} else {
static constexpr char body[] = "Only path allowed is /upload";
headers["Content-Length"] = std::to_string(sizeof(body));
conn->set_status(server::connection::bad_request);
conn->set_headers(headers);
conn->write("Only path allowed is /upload.");
conn->write(body);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ void process_request(work_queue& queue) {
std::this_thread::sleep_for(std::chrono::seconds(10));

std::map<std::string, std::string> headers = {
{"Content-Length", "0"},
{"Content-Type", "text/plain"},
};

std::string body("Hello, world!");
headers["Content-Length"] = std::to_string(body.size());
request->conn->set_status(server::connection::ok);
request->conn->set_headers(headers);
request->conn->write("Hello, world!");
request->conn->write(body);
}

std::this_thread::sleep_for(std::chrono::microseconds(1000));
Expand Down
6 changes: 5 additions & 1 deletion libs/network/example/http/hello_world_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ struct hello_world {
data << "Hello, " << ip << ':' << port << '!';

std::map<std::string, std::string> headers = {
{"Content-Length", "0"},
{"Content-Type", "text/plain"},
};

auto body = data.str();
headers["Content-Length"] = std::to_string(body.size());

connection->set_status(server::connection::ok);
connection->set_headers(headers);
connection->write(data.str());
connection->write(body);
}
};

Expand Down