@@ -189,7 +189,7 @@ class Server {
189189 typedef std::function<void (const Request&, Response&)> Handler;
190190 typedef std::function<void (const Request&, const Response&)> Logger;
191191
192- Server (HttpVersion http_version = HttpVersion::v1_0 );
192+ Server ();
193193
194194 virtual ~Server ();
195195
@@ -218,8 +218,6 @@ class Server {
218218protected:
219219 bool process_request (Stream& strm, bool last_connection);
220220
221- const HttpVersion http_version_;
222-
223221private:
224222 typedef std::vector<std::pair<std::regex, Handler>> Handlers;
225223
@@ -257,8 +255,7 @@ class Client {
257255 Client (
258256 const char * host,
259257 int port = 80 ,
260- size_t timeout_sec = 300 ,
261- HttpVersion http_version = HttpVersion::v1_0);
258+ size_t timeout_sec = 300 );
262259
263260 virtual ~Client ();
264261
@@ -293,7 +290,6 @@ class Client {
293290 const std::string host_;
294291 const int port_;
295292 size_t timeout_sec_;
296- const HttpVersion http_version_;
297293 const std::string host_and_port_;
298294
299295private:
@@ -323,8 +319,7 @@ class SSLSocketStream : public Stream {
323319class SSLServer : public Server {
324320public:
325321 SSLServer (
326- const char * cert_path, const char * private_key_path,
327- HttpVersion http_version = HttpVersion::v1_0);
322+ const char * cert_path, const char * private_key_path);
328323
329324 virtual ~SSLServer ();
330325
@@ -342,8 +337,7 @@ class SSLClient : public Client {
342337 SSLClient (
343338 const char * host,
344339 int port = 80 ,
345- size_t timeout_sec = 300 ,
346- HttpVersion http_version = HttpVersion::v1_0);
340+ size_t timeout_sec = 300 );
347341
348342 virtual ~SSLClient ();
349343
@@ -362,8 +356,6 @@ class SSLClient : public Client {
362356 */
363357namespace detail {
364358
365- static std::vector<const char *> http_version_strings = { " HTTP/1.0" , " HTTP/1.1" };
366-
367359template <class Fn >
368360void split (const char * b, const char * e, char d, Fn fn)
369361{
@@ -1413,9 +1405,8 @@ inline std::string SocketStream::get_remote_addr() {
14131405}
14141406
14151407// HTTP server implementation
1416- inline Server::Server (HttpVersion http_version)
1417- : http_version_(http_version)
1418- , is_running_(false )
1408+ inline Server::Server ()
1409+ : is_running_(false )
14191410 , svr_sock_(INVALID_SOCKET)
14201411 , running_threads_(0 )
14211412{
@@ -1539,8 +1530,7 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req
15391530 }
15401531
15411532 // Response line
1542- strm.write_format (" %s %d %s\r\n " ,
1543- detail::http_version_strings[static_cast <size_t >(http_version_)],
1533+ strm.write_format (" HTTP/1.1 %d %s\r\n " ,
15441534 res.status ,
15451535 detail::status_message (res.status ));
15461536
@@ -1551,7 +1541,7 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req
15511541
15521542 if (!res.body .empty ()) {
15531543#ifdef CPPHTTPLIB_ZLIB_SUPPORT
1554- // TODO: Server version is HTTP/1.1 and 'Accpet-Encoding' has gzip, not gzip;q=0
1544+ // TODO: 'Accpet-Encoding' has gzip, not gzip;q=0
15551545 const auto & encodings = req.get_header_value (" Accept-Encoding" );
15561546 if (encodings.find (" gzip" ) != std::string::npos &&
15571547 detail::can_compress (res.get_header_value (" Content-Type" ))) {
@@ -1749,7 +1739,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection)
17491739 Request req;
17501740 Response res;
17511741
1752- res.version = detail::http_version_strings[ static_cast < size_t >(http_version_)] ;
1742+ res.version = " HTTP/1.1 " ;
17531743
17541744 // Request line and headers
17551745 if (!parse_request_line (reader.ptr (), req) || !detail::read_headers (strm, req.headers )) {
@@ -1817,23 +1807,20 @@ inline bool Server::is_valid() const
18171807
18181808inline bool Server::read_and_close_socket (socket_t sock)
18191809{
1820- auto keep_alive = http_version_ == HttpVersion::v1_1;
1821-
18221810 return detail::read_and_close_socket (
18231811 sock,
1824- keep_alive ,
1812+ true ,
18251813 [this ](Stream& strm, bool last_connection) {
18261814 return process_request (strm, last_connection);
18271815 });
18281816}
18291817
18301818// HTTP client implementation
18311819inline Client::Client (
1832- const char * host, int port, size_t timeout_sec, HttpVersion http_version )
1820+ const char * host, int port, size_t timeout_sec)
18331821 : host_(host)
18341822 , port_(port)
18351823 , timeout_sec_(timeout_sec)
1836- , http_version_(http_version)
18371824 , host_and_port_(host_ + " :" + std::to_string(port_))
18381825{
18391826}
@@ -1878,11 +1865,12 @@ inline bool Client::read_response_line(Stream& strm, Response& res)
18781865 return false ;
18791866 }
18801867
1881- const static std::regex re (" HTTP/1\\ .[01] (\\ d+?) .+\r\n " );
1868+ const static std::regex re (" ( HTTP/1\\ .[01]) (\\ d+?) .+\r\n " );
18821869
18831870 std::cmatch m;
18841871 if (std::regex_match (reader.ptr (), m, re)) {
1885- res.status = std::stoi (std::string (m[1 ]));
1872+ res.version = std::string (m[1 ]);
1873+ res.status = std::stoi (std::string (m[2 ]));
18861874 }
18871875
18881876 return true ;
@@ -1907,10 +1895,9 @@ inline void Client::write_request(Stream& strm, Request& req)
19071895 auto path = detail::encode_url (req.path );
19081896
19091897 // Request line
1910- strm.write_format (" %s %s %s \r\n " ,
1898+ strm.write_format (" %s %s HTTP/1.1 \r\n " ,
19111899 req.method .c_str (),
1912- path.c_str (),
1913- detail::http_version_strings[static_cast <size_t >(http_version_)]);
1900+ path.c_str ());
19141901
19151902 // Headers
19161903 req.set_header (" Host" , host_and_port_.c_str ());
@@ -1961,7 +1948,8 @@ inline bool Client::process_request(Stream& strm, Request& req, Response& res)
19611948 return false ;
19621949 }
19631950
1964- // TODO: Check if 'Connection' header is 'close' or HTTP version is 1.0, then close socket...
1951+ // TODO: Check if 'Connection' header is 'close' or HTTP version is 1.0,
1952+ // then close socket...
19651953
19661954 // Body
19671955 if (req.method != " HEAD" ) {
@@ -2227,8 +2215,7 @@ inline std::string SSLSocketStream::get_remote_addr() {
22272215}
22282216
22292217// SSL HTTP server implementation
2230- inline SSLServer::SSLServer (const char * cert_path, const char * private_key_path, HttpVersion http_version)
2231- : Server(http_version)
2218+ inline SSLServer::SSLServer (const char * cert_path, const char * private_key_path)
22322219{
22332220 ctx_ = SSL_CTX_new (SSLv23_server_method ());
22342221
@@ -2264,11 +2251,9 @@ inline bool SSLServer::is_valid() const
22642251
22652252inline bool SSLServer::read_and_close_socket (socket_t sock)
22662253{
2267- auto keep_alive = http_version_ == HttpVersion::v1_1;
2268-
22692254 return detail::read_and_close_socket_ssl (
22702255 sock,
2271- keep_alive ,
2256+ true ,
22722257 ctx_, ctx_mutex_,
22732258 SSL_accept,
22742259 [](SSL* /* ssl*/ ) {},
@@ -2278,9 +2263,8 @@ inline bool SSLServer::read_and_close_socket(socket_t sock)
22782263}
22792264
22802265// SSL HTTP client implementation
2281- inline SSLClient::SSLClient (
2282- const char * host, int port, size_t timeout_sec, HttpVersion http_version)
2283- : Client(host, port, timeout_sec, http_version)
2266+ inline SSLClient::SSLClient (const char * host, int port, size_t timeout_sec)
2267+ : Client(host, port, timeout_sec)
22842268{
22852269 ctx_ = SSL_CTX_new (SSLv23_client_method ());
22862270}
0 commit comments