Skip to content

Commit 6e6bfb6

Browse files
authored
Merge pull request fhessel#78 from fhessel/fix/connection-close
Allow Overriding Default Keep-Alive Behavior
2 parents a10f4e8 + 50c0f59 commit 6e6bfb6

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/HTTPConnection.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,15 @@ void HTTPConnection::loop() {
529529
// Now we need to check if we can use keep-alive to reuse the SSL connection
530530
// However, if the client did not set content-size or defined connection: close,
531531
// we have no chance to do so.
532+
// Also, the programmer may have explicitly set Connection: close for the response.
533+
std::string hConnection = res.getHeader("Connection");
534+
if (hConnection == "close") {
535+
_isKeepAlive = false;
536+
}
532537
if (!_isKeepAlive) {
533538
// No KeepAlive -> We are done. Transition to next state.
534539
if (!isClosed()) {
540+
res.finalize();
535541
_connectionState = STATE_BODY_FINISHED;
536542
}
537543
} else {

src/HTTPResponse.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ void HTTPResponse::setHeader(std::string const &name, std::string const &value)
5252
_headers.set(new HTTPHeader(name, value));
5353
}
5454

55+
std::string HTTPResponse::getHeader(std::string const &name) {
56+
HTTPHeader * h = _headers.get(name);
57+
if (h != NULL) {
58+
return h->_value;
59+
} else {
60+
return std::string();
61+
}
62+
}
63+
5564
bool HTTPResponse::isHeaderWritten() {
5665
return _headerWritten;
5766
}

src/HTTPResponse.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class HTTPResponse : public Print {
3232
uint16_t getStatusCode();
3333
std::string getStatusText();
3434
void setHeader(std::string const &name, std::string const &value);
35+
std::string getHeader(std::string const &name);
3536
bool isHeaderWritten();
3637

3738
void printStd(std::string const &str);

0 commit comments

Comments
 (0)