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

Commite653e47

Browse files
carundeanberris
authored andcommitted
example: send response content-length as part of http header from server to client (issue#681)
1 parenta809089 commite653e47

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

‎libs/network/example/http/async_server_file_upload.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ struct connection_handler {
191191
voidoperator()(server::requestconst& req,const server::connection_ptr& conn) {
192192
static std::map<std::string, std::string> headers = {
193193
{"Connection","close"},
194+
{"Content-Length","0"},
194195
{"Content-Type","text/plain"}
195196
};
196197

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

209-
// Respond to the client
210-
conn->set_status(server::connection::ok);
211-
conn->set_headers(headers);
212210
auto end =std::chrono::high_resolution_clock::now();
213211
std::chrono::duration<double, std::milli> diff = end - start;
214212
std::ostringstream stm;
215213
stm <<"Took" << diff.count() <<" milliseconds for the transfer." << std::endl;
216-
conn->write(stm.str());
214+
auto body = stm.str();
215+
// Respond to the client
216+
headers["Content-Length"] =std::to_string(body.size());
217+
conn->set_status(server::connection::ok);
218+
conn->set_headers(headers);
219+
conn->write(body);
217220
}catch (const file_uploader_exception& e) {
221+
const std::string err = e.what();
222+
headers["Content-Length"] =std::to_string(err.size());
218223
conn->set_status(server::connection::bad_request);
219224
conn->set_headers(headers);
220-
const std::string err = e.what();
221225
conn->write(err);
222226
}
223227
}else {
228+
static std::stringbody("Only path allowed is /upload");
229+
headers["Content-Length"] =std::to_string(body.size());
224230
conn->set_status(server::connection::bad_request);
225231
conn->set_headers(headers);
226-
conn->write("Only path allowed is /upload.");
232+
conn->write(body);
227233
}
228234
}
229235
};

‎libs/network/example/http/hello_world_async_server_with_work_queue.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,16 @@ void process_request(work_queue& queue) {
125125
// some heavy work!
126126
std::this_thread::sleep_for(std::chrono::seconds(10));
127127

128-
std::map<std::string, std::string> headers = {
128+
static std::map<std::string, std::string> headers = {
129+
{"Content-Length","0"},
129130
{"Content-Type","text/plain"},
130131
};
131132

133+
std::stringbody("Hello, world!");
134+
headers["Content-Length"] =std::to_string(body.size());
132135
request->conn->set_status(server::connection::ok);
133136
request->conn->set_headers(headers);
134-
request->conn->write("Hello, world!");
137+
request->conn->write(body);
135138
}
136139

137140
std::this_thread::sleep_for(std::chrono::microseconds(1000));

‎libs/network/example/http/hello_world_server.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ struct hello_world {
3030
std::ostringstream data;
3131
data <<"Hello," << ip <<':' << port <<'!';
3232

33-
std::map<std::string, std::string> headers = {
33+
static std::map<std::string, std::string> headers = {
34+
{"Content-Length","0"},
3435
{"Content-Type","text/plain"},
3536
};
3637

38+
auto body = data.str();
39+
headers["Content-Length"] =std::to_string(body.size());
40+
3741
connection->set_status(server::connection::ok);
3842
connection->set_headers(headers);
39-
connection->write(data.str());
43+
connection->write(body);
4044
}
4145
};
4246

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp