Skip to content

Commit 312a8d7

Browse files
committed
Removed HTTP version. It's now always 'HTTP/1.1'.
1 parent 37130cd commit 312a8d7

File tree

5 files changed

+42
-71
lines changed

5 files changed

+42
-71
lines changed

example/server.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ std::string log(const Request& req, const Response& res)
6868

6969
int main(void)
7070
{
71-
auto version = httplib::HttpVersion::v1_1;
72-
7371
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
74-
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, version);
72+
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
7573
#else
76-
Server svr(version);
74+
Server svr;
7775
#endif
7876

7977
if (!svr.is_valid()) {

example/simplesvr.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,10 @@ int main(int argc, const char** argv)
9999
return 1;
100100
}
101101

102-
auto version = httplib::HttpVersion::v1_1;
103-
104102
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
105-
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, version);
103+
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
106104
#else
107-
Server svr(version);
105+
Server svr;
108106
#endif
109107

110108
svr.Post("/multipart", [](const auto& req, auto& res) {

httplib.h

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
218218
protected:
219219
bool process_request(Stream& strm, bool last_connection);
220220

221-
const HttpVersion http_version_;
222-
223221
private:
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

299295
private:
@@ -323,8 +319,7 @@ class SSLSocketStream : public Stream {
323319
class SSLServer : public Server {
324320
public:
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
*/
363357
namespace detail {
364358

365-
static std::vector<const char*> http_version_strings = { "HTTP/1.0", "HTTP/1.1" };
366-
367359
template <class Fn>
368360
void 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

18181808
inline 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
18311819
inline 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

22652252
inline 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
}

test/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
CC = clang++
3-
CFLAGS = -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -lpthread
4-
#OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto
5-
#ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
3+
CFLAGS = -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -lpthread
4+
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto
5+
ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
66

77
all : test
88
./test

test/test.cc

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,17 @@ TEST(GetHeaderValueTest, Range)
118118
}
119119
}
120120

121-
void testChunkedEncoding(httplib::HttpVersion ver)
121+
TEST(ChunkedEncodingTest, FromHTTPWatch)
122122
{
123123
auto host = "www.httpwatch.com";
124124
auto sec = 2;
125125

126126
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
127127
auto port = 443;
128-
httplib::SSLClient cli(host, port, sec, ver);
128+
httplib::SSLClient cli(host, port, sec);
129129
#else
130130
auto port = 80;
131-
httplib::Client cli(host, port, sec, ver);
131+
httplib::Client cli(host, port, sec);
132132
#endif
133133

134134
auto res = cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
@@ -141,24 +141,17 @@ void testChunkedEncoding(httplib::HttpVersion ver)
141141
EXPECT_EQ(out, res->body);
142142
}
143143

144-
TEST(ChunkedEncodingTest, FromHTTPWatch)
145-
{
146-
testChunkedEncoding(httplib::HttpVersion::v1_0);
147-
testChunkedEncoding(httplib::HttpVersion::v1_1);
148-
}
149-
150144
TEST(RangeTest, FromHTTPBin)
151145
{
152146
auto host = "httpbin.org";
153147
auto sec = 5;
154-
auto ver = httplib::HttpVersion::v1_1;
155148

156149
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
157150
auto port = 443;
158-
httplib::SSLClient cli(host, port, sec, ver);
151+
httplib::SSLClient cli(host, port, sec);
159152
#else
160153
auto port = 80;
161-
httplib::Client cli(host, port, sec, ver);
154+
httplib::Client cli(host, port, sec);
162155
#endif
163156

164157
{
@@ -190,14 +183,13 @@ TEST(ConnectionErrorTest, InvalidHost)
190183
{
191184
auto host = "abcde.com";
192185
auto sec = 2;
193-
auto ver = httplib::HttpVersion::v1_1;
194186

195187
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
196188
auto port = 443;
197-
httplib::SSLClient cli(host, port, sec, ver);
189+
httplib::SSLClient cli(host, port, sec);
198190
#else
199191
auto port = 80;
200-
httplib::Client cli(host, port, sec, ver);
192+
httplib::Client cli(host, port, sec);
201193
#endif
202194

203195
auto res = cli.Get("/");
@@ -208,14 +200,13 @@ TEST(ConnectionErrorTest, InvalidPort)
208200
{
209201
auto host = "localhost";
210202
auto sec = 2;
211-
auto ver = httplib::HttpVersion::v1_1;
212203

213204
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
214205
auto port = 44380;
215-
httplib::SSLClient cli(host, port, sec, ver);
206+
httplib::SSLClient cli(host, port, sec);
216207
#else
217208
auto port = 8080;
218-
httplib::Client cli(host, port, sec, ver);
209+
httplib::Client cli(host, port, sec);
219210
#endif
220211

221212
auto res = cli.Get("/");
@@ -226,22 +217,21 @@ TEST(ConnectionErrorTest, Timeout)
226217
{
227218
auto host = "google.com";
228219
auto sec = 2;
229-
auto ver = httplib::HttpVersion::v1_1;
230220

231221
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
232222
auto port = 44380;
233-
httplib::SSLClient cli(host, port, sec, ver);
223+
httplib::SSLClient cli(host, port, sec);
234224
#else
235225
auto port = 8080;
236-
httplib::Client cli(host, port, sec, ver);
226+
httplib::Client cli(host, port, sec);
237227
#endif
238228

239229
auto res = cli.Get("/");
240230
ASSERT_TRUE(res == nullptr);
241231
}
242232

243233
TEST(Server, BindAndListenSeparately) {
244-
Server svr(httplib::HttpVersion::v1_1);
234+
Server svr;
245235
int port = svr.bind_to_any_port("localhost");
246236
ASSERT_TRUE(port > 0);
247237
svr.stop();
@@ -400,6 +390,7 @@ TEST_F(ServerTest, GetMethod200)
400390
{
401391
auto res = cli_.Get("/hi");
402392
ASSERT_TRUE(res != nullptr);
393+
EXPECT_EQ("HTTP/1.1", res->version);
403394
EXPECT_EQ(200, res->status);
404395
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
405396
EXPECT_EQ("Hello World!", res->body);

0 commit comments

Comments
 (0)