Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
deanberris merged 3 commits intocpp-netlib:0.13-releasefromcarun:fix-content-length
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionboost/network/protocol/http/message/async_message.hpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionslibs/network/example/http/async_server_file_upload.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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) {
staticstd::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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionlibs/network/example/http/hello_world_server.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp