comparison Core/HttpServer/HttpOutput.cpp @ 1115:da56a7916e8a

Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Sep 2014 17:30:13 +0200
parents ba5c0908600c
children 1e1390665639
comparison
equal deleted inserted replaced
1114:adfd2c7a92f3 1115:da56a7916e8a
41 #include "../OrthancException.h" 41 #include "../OrthancException.h"
42 #include "../Toolbox.h" 42 #include "../Toolbox.h"
43 43
44 namespace Orthanc 44 namespace Orthanc
45 { 45 {
46 HttpOutput::StateMachine::StateMachine(IHttpOutputStream& stream) : 46 HttpOutput::StateMachine::StateMachine(IHttpOutputStream& stream,
47 bool isKeepAlive) :
47 stream_(stream), 48 stream_(stream),
48 state_(State_WritingHeader), 49 state_(State_WritingHeader),
49 status_(HttpStatus_200_Ok), 50 status_(HttpStatus_200_Ok),
50 hasContentLength_(false), 51 hasContentLength_(false),
51 contentPosition_(0) 52 contentPosition_(0),
53 keepAlive_(isKeepAlive)
52 { 54 {
53 } 55 }
54 56
55 HttpOutput::StateMachine::~StateMachine() 57 HttpOutput::StateMachine::~StateMachine()
56 { 58 {
158 std::string s = "HTTP/1.1 " + 160 std::string s = "HTTP/1.1 " +
159 boost::lexical_cast<std::string>(status_) + 161 boost::lexical_cast<std::string>(status_) +
160 " " + std::string(EnumerationToString(status_)) + 162 " " + std::string(EnumerationToString(status_)) +
161 "\r\n"; 163 "\r\n";
162 164
163 if (status_ != HttpStatus_200_Ok) 165 if (keepAlive_)
164 { 166 {
165 hasContentLength_ = false; 167 s += "Connection: keep-alive\r\n";
166 } 168 }
167 169
168 for (std::list<std::string>::const_iterator 170 for (std::list<std::string>::const_iterator
169 it = headers_.begin(); it != headers_.end(); ++it) 171 it = headers_.begin(); it != headers_.end(); ++it)
170 { 172 {
171 s += *it; 173 s += *it;
174 }
175
176 if (status_ != HttpStatus_200_Ok)
177 {
178 hasContentLength_ = false;
172 } 179 }
173 180
174 uint64_t contentLength = (hasContentLength_ ? contentLength_ : length); 181 uint64_t contentLength = (hasContentLength_ ? contentLength_ : length);
175 s += "Content-Length: " + boost::lexical_cast<std::string>(contentLength) + "\r\n\r\n"; 182 s += "Content-Length: " + boost::lexical_cast<std::string>(contentLength) + "\r\n\r\n";
176 183