Skip to content

Commit df7e461

Browse files
committed
Determine whether to send a Connection: Keep-alive response or a Connection: Close response and do the appropriate thing with the socket.
1 parent dcf0751 commit df7e461

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/httpserver.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ void build_HTTP_response_header(std::string &response_header,
5151
os<<buff;
5252
sprintf(buff, "%d", body.size());
5353
headers["Content-Length"] = buff;
54-
headers["Connection"] = "Keep-alive";
5554
for (headers_t::iterator i = headers.begin();
5655
i != headers.end(); ++i) {
5756
os<<i->first<<": "<<i->second<<"\r\n";
@@ -67,7 +66,14 @@ void write_response(client_t *client,
6766
std::string &body) {
6867
assert(client->resstrs.empty());
6968
std::string header_str;
70-
int http_major = 1, http_minor = 1;
69+
const int http_major = client->parser.http_major;
70+
const int http_minor = client->parser.http_minor;
71+
if (http_should_keep_alive(&client->parser)) {
72+
headers["Connection"] = "Keep-alive";
73+
} else {
74+
headers["Connection"] = "Close";
75+
}
76+
7177
build_HTTP_response_header(header_str, http_major, http_minor,
7278
status_code, status_str, headers, body);
7379

@@ -200,7 +206,7 @@ void after_write(uv_write_t* req, int status) {
200206
client_t *client = (client_t*)(req->handle->data);
201207
uv_stream_t *pstrm = (uv_stream_t*)(&client->handle);
202208

203-
if (status != 0) {
209+
if (status != 0 || !http_should_keep_alive(&client->parser)) {
204210
uv_err_t err = uv_last_error(uv_loop);
205211
UVERR(err, "write");
206212
close_connection(client);

0 commit comments

Comments
 (0)