1
0
mirror of synced 2025-04-20 11:47:43 +03:00

Removed HTTP version. It's now always 'HTTP/1.1'.

This commit is contained in:
yhirose 2018-05-13 19:00:05 -04:00
parent 37130cd7f9
commit 312a8d7523
5 changed files with 42 additions and 71 deletions

View File

@ -68,12 +68,10 @@ std::string log(const Request& req, const Response& res)
int main(void) int main(void)
{ {
auto version = httplib::HttpVersion::v1_1;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, version); SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
#else #else
Server svr(version); Server svr;
#endif #endif
if (!svr.is_valid()) { if (!svr.is_valid()) {

View File

@ -99,12 +99,10 @@ int main(int argc, const char** argv)
return 1; return 1;
} }
auto version = httplib::HttpVersion::v1_1;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, version); SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
#else #else
Server svr(version); Server svr;
#endif #endif
svr.Post("/multipart", [](const auto& req, auto& res) { svr.Post("/multipart", [](const auto& req, auto& res) {

View File

@ -189,7 +189,7 @@ public:
typedef std::function<void (const Request&, Response&)> Handler; typedef std::function<void (const Request&, Response&)> Handler;
typedef std::function<void (const Request&, const Response&)> Logger; typedef std::function<void (const Request&, const Response&)> Logger;
Server(HttpVersion http_version = HttpVersion::v1_0); Server();
virtual ~Server(); virtual ~Server();
@ -218,8 +218,6 @@ public:
protected: protected:
bool process_request(Stream& strm, bool last_connection); bool process_request(Stream& strm, bool last_connection);
const HttpVersion http_version_;
private: private:
typedef std::vector<std::pair<std::regex, Handler>> Handlers; typedef std::vector<std::pair<std::regex, Handler>> Handlers;
@ -257,8 +255,7 @@ public:
Client( Client(
const char* host, const char* host,
int port = 80, int port = 80,
size_t timeout_sec = 300, size_t timeout_sec = 300);
HttpVersion http_version = HttpVersion::v1_0);
virtual ~Client(); virtual ~Client();
@ -293,7 +290,6 @@ protected:
const std::string host_; const std::string host_;
const int port_; const int port_;
size_t timeout_sec_; size_t timeout_sec_;
const HttpVersion http_version_;
const std::string host_and_port_; const std::string host_and_port_;
private: private:
@ -323,8 +319,7 @@ private:
class SSLServer : public Server { class SSLServer : public Server {
public: public:
SSLServer( SSLServer(
const char* cert_path, const char* private_key_path, const char* cert_path, const char* private_key_path);
HttpVersion http_version = HttpVersion::v1_0);
virtual ~SSLServer(); virtual ~SSLServer();
@ -342,8 +337,7 @@ public:
SSLClient( SSLClient(
const char* host, const char* host,
int port = 80, int port = 80,
size_t timeout_sec = 300, size_t timeout_sec = 300);
HttpVersion http_version = HttpVersion::v1_0);
virtual ~SSLClient(); virtual ~SSLClient();
@ -362,8 +356,6 @@ private:
*/ */
namespace detail { namespace detail {
static std::vector<const char*> http_version_strings = { "HTTP/1.0", "HTTP/1.1" };
template <class Fn> template <class Fn>
void split(const char* b, const char* e, char d, Fn fn) void split(const char* b, const char* e, char d, Fn fn)
{ {
@ -1413,9 +1405,8 @@ inline std::string SocketStream::get_remote_addr() {
} }
// HTTP server implementation // HTTP server implementation
inline Server::Server(HttpVersion http_version) inline Server::Server()
: http_version_(http_version) : is_running_(false)
, is_running_(false)
, svr_sock_(INVALID_SOCKET) , svr_sock_(INVALID_SOCKET)
, running_threads_(0) , running_threads_(0)
{ {
@ -1539,8 +1530,7 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req
} }
// Response line // Response line
strm.write_format("%s %d %s\r\n", strm.write_format("HTTP/1.1 %d %s\r\n",
detail::http_version_strings[static_cast<size_t>(http_version_)],
res.status, res.status,
detail::status_message(res.status)); detail::status_message(res.status));
@ -1551,7 +1541,7 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req
if (!res.body.empty()) { if (!res.body.empty()) {
#ifdef CPPHTTPLIB_ZLIB_SUPPORT #ifdef CPPHTTPLIB_ZLIB_SUPPORT
// TODO: Server version is HTTP/1.1 and 'Accpet-Encoding' has gzip, not gzip;q=0 // TODO: 'Accpet-Encoding' has gzip, not gzip;q=0
const auto& encodings = req.get_header_value("Accept-Encoding"); const auto& encodings = req.get_header_value("Accept-Encoding");
if (encodings.find("gzip") != std::string::npos && if (encodings.find("gzip") != std::string::npos &&
detail::can_compress(res.get_header_value("Content-Type"))) { detail::can_compress(res.get_header_value("Content-Type"))) {
@ -1749,7 +1739,7 @@ inline bool Server::process_request(Stream& strm, bool last_connection)
Request req; Request req;
Response res; Response res;
res.version = detail::http_version_strings[static_cast<size_t>(http_version_)]; res.version = "HTTP/1.1";
// Request line and headers // Request line and headers
if (!parse_request_line(reader.ptr(), req) || !detail::read_headers(strm, req.headers)) { if (!parse_request_line(reader.ptr(), req) || !detail::read_headers(strm, req.headers)) {
@ -1817,11 +1807,9 @@ inline bool Server::is_valid() const
inline bool Server::read_and_close_socket(socket_t sock) inline bool Server::read_and_close_socket(socket_t sock)
{ {
auto keep_alive = http_version_ == HttpVersion::v1_1;
return detail::read_and_close_socket( return detail::read_and_close_socket(
sock, sock,
keep_alive, true,
[this](Stream& strm, bool last_connection) { [this](Stream& strm, bool last_connection) {
return process_request(strm, last_connection); return process_request(strm, last_connection);
}); });
@ -1829,11 +1817,10 @@ inline bool Server::read_and_close_socket(socket_t sock)
// HTTP client implementation // HTTP client implementation
inline Client::Client( inline Client::Client(
const char* host, int port, size_t timeout_sec, HttpVersion http_version) const char* host, int port, size_t timeout_sec)
: host_(host) : host_(host)
, port_(port) , port_(port)
, timeout_sec_(timeout_sec) , timeout_sec_(timeout_sec)
, http_version_(http_version)
, host_and_port_(host_ + ":" + std::to_string(port_)) , host_and_port_(host_ + ":" + std::to_string(port_))
{ {
} }
@ -1878,11 +1865,12 @@ inline bool Client::read_response_line(Stream& strm, Response& res)
return false; return false;
} }
const static std::regex re("HTTP/1\\.[01] (\\d+?) .+\r\n"); const static std::regex re("(HTTP/1\\.[01]) (\\d+?) .+\r\n");
std::cmatch m; std::cmatch m;
if (std::regex_match(reader.ptr(), m, re)) { if (std::regex_match(reader.ptr(), m, re)) {
res.status = std::stoi(std::string(m[1])); res.version = std::string(m[1]);
res.status = std::stoi(std::string(m[2]));
} }
return true; return true;
@ -1907,10 +1895,9 @@ inline void Client::write_request(Stream& strm, Request& req)
auto path = detail::encode_url(req.path); auto path = detail::encode_url(req.path);
// Request line // Request line
strm.write_format("%s %s %s\r\n", strm.write_format("%s %s HTTP/1.1\r\n",
req.method.c_str(), req.method.c_str(),
path.c_str(), path.c_str());
detail::http_version_strings[static_cast<size_t>(http_version_)]);
// Headers // Headers
req.set_header("Host", host_and_port_.c_str()); req.set_header("Host", host_and_port_.c_str());
@ -1961,7 +1948,8 @@ inline bool Client::process_request(Stream& strm, Request& req, Response& res)
return false; return false;
} }
// TODO: Check if 'Connection' header is 'close' or HTTP version is 1.0, then close socket... // TODO: Check if 'Connection' header is 'close' or HTTP version is 1.0,
// then close socket...
// Body // Body
if (req.method != "HEAD") { if (req.method != "HEAD") {
@ -2227,8 +2215,7 @@ inline std::string SSLSocketStream::get_remote_addr() {
} }
// SSL HTTP server implementation // SSL HTTP server implementation
inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path, HttpVersion http_version) inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path)
: Server(http_version)
{ {
ctx_ = SSL_CTX_new(SSLv23_server_method()); ctx_ = SSL_CTX_new(SSLv23_server_method());
@ -2264,11 +2251,9 @@ inline bool SSLServer::is_valid() const
inline bool SSLServer::read_and_close_socket(socket_t sock) inline bool SSLServer::read_and_close_socket(socket_t sock)
{ {
auto keep_alive = http_version_ == HttpVersion::v1_1;
return detail::read_and_close_socket_ssl( return detail::read_and_close_socket_ssl(
sock, sock,
keep_alive, true,
ctx_, ctx_mutex_, ctx_, ctx_mutex_,
SSL_accept, SSL_accept,
[](SSL* /*ssl*/) {}, [](SSL* /*ssl*/) {},
@ -2278,9 +2263,8 @@ inline bool SSLServer::read_and_close_socket(socket_t sock)
} }
// SSL HTTP client implementation // SSL HTTP client implementation
inline SSLClient::SSLClient( inline SSLClient::SSLClient(const char* host, int port, size_t timeout_sec)
const char* host, int port, size_t timeout_sec, HttpVersion http_version) : Client(host, port, timeout_sec)
: Client(host, port, timeout_sec, http_version)
{ {
ctx_ = SSL_CTX_new(SSLv23_client_method()); ctx_ = SSL_CTX_new(SSLv23_client_method());
} }

View File

@ -1,8 +1,8 @@
CC = clang++ CC = clang++
CFLAGS = -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -lpthread CFLAGS = -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -lpthread
#OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto
#ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
all : test all : test
./test ./test

View File

@ -118,17 +118,17 @@ TEST(GetHeaderValueTest, Range)
} }
} }
void testChunkedEncoding(httplib::HttpVersion ver) TEST(ChunkedEncodingTest, FromHTTPWatch)
{ {
auto host = "www.httpwatch.com"; auto host = "www.httpwatch.com";
auto sec = 2; auto sec = 2;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443; auto port = 443;
httplib::SSLClient cli(host, port, sec, ver); httplib::SSLClient cli(host, port, sec);
#else #else
auto port = 80; auto port = 80;
httplib::Client cli(host, port, sec, ver); httplib::Client cli(host, port, sec);
#endif #endif
auto res = cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137"); auto res = cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
@ -141,24 +141,17 @@ void testChunkedEncoding(httplib::HttpVersion ver)
EXPECT_EQ(out, res->body); EXPECT_EQ(out, res->body);
} }
TEST(ChunkedEncodingTest, FromHTTPWatch)
{
testChunkedEncoding(httplib::HttpVersion::v1_0);
testChunkedEncoding(httplib::HttpVersion::v1_1);
}
TEST(RangeTest, FromHTTPBin) TEST(RangeTest, FromHTTPBin)
{ {
auto host = "httpbin.org"; auto host = "httpbin.org";
auto sec = 5; auto sec = 5;
auto ver = httplib::HttpVersion::v1_1;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443; auto port = 443;
httplib::SSLClient cli(host, port, sec, ver); httplib::SSLClient cli(host, port, sec);
#else #else
auto port = 80; auto port = 80;
httplib::Client cli(host, port, sec, ver); httplib::Client cli(host, port, sec);
#endif #endif
{ {
@ -190,14 +183,13 @@ TEST(ConnectionErrorTest, InvalidHost)
{ {
auto host = "abcde.com"; auto host = "abcde.com";
auto sec = 2; auto sec = 2;
auto ver = httplib::HttpVersion::v1_1;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443; auto port = 443;
httplib::SSLClient cli(host, port, sec, ver); httplib::SSLClient cli(host, port, sec);
#else #else
auto port = 80; auto port = 80;
httplib::Client cli(host, port, sec, ver); httplib::Client cli(host, port, sec);
#endif #endif
auto res = cli.Get("/"); auto res = cli.Get("/");
@ -208,14 +200,13 @@ TEST(ConnectionErrorTest, InvalidPort)
{ {
auto host = "localhost"; auto host = "localhost";
auto sec = 2; auto sec = 2;
auto ver = httplib::HttpVersion::v1_1;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 44380; auto port = 44380;
httplib::SSLClient cli(host, port, sec, ver); httplib::SSLClient cli(host, port, sec);
#else #else
auto port = 8080; auto port = 8080;
httplib::Client cli(host, port, sec, ver); httplib::Client cli(host, port, sec);
#endif #endif
auto res = cli.Get("/"); auto res = cli.Get("/");
@ -226,14 +217,13 @@ TEST(ConnectionErrorTest, Timeout)
{ {
auto host = "google.com"; auto host = "google.com";
auto sec = 2; auto sec = 2;
auto ver = httplib::HttpVersion::v1_1;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 44380; auto port = 44380;
httplib::SSLClient cli(host, port, sec, ver); httplib::SSLClient cli(host, port, sec);
#else #else
auto port = 8080; auto port = 8080;
httplib::Client cli(host, port, sec, ver); httplib::Client cli(host, port, sec);
#endif #endif
auto res = cli.Get("/"); auto res = cli.Get("/");
@ -241,7 +231,7 @@ TEST(ConnectionErrorTest, Timeout)
} }
TEST(Server, BindAndListenSeparately) { TEST(Server, BindAndListenSeparately) {
Server svr(httplib::HttpVersion::v1_1); Server svr;
int port = svr.bind_to_any_port("localhost"); int port = svr.bind_to_any_port("localhost");
ASSERT_TRUE(port > 0); ASSERT_TRUE(port > 0);
svr.stop(); svr.stop();
@ -400,6 +390,7 @@ TEST_F(ServerTest, GetMethod200)
{ {
auto res = cli_.Get("/hi"); auto res = cli_.Get("/hi");
ASSERT_TRUE(res != nullptr); ASSERT_TRUE(res != nullptr);
EXPECT_EQ("HTTP/1.1", res->version);
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
EXPECT_EQ("text/plain", res->get_header_value("Content-Type")); EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
EXPECT_EQ("Hello World!", res->body); EXPECT_EQ("Hello World!", res->body);