From: Marcin Siodelski Date: Fri, 6 Jan 2017 23:09:32 +0000 (+0100) Subject: [5088] HttpVersion is now a struct rather than a pair. X-Git-Tag: trac5090_base~2^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3bcb33f2176def03acd0e420f18c299e2b2773ea;p=thirdparty%2Fkea.git [5088] HttpVersion is now a struct rather than a pair. This is a result of the review. --- diff --git a/src/lib/http/http_types.h b/src/lib/http/http_types.h index 8658d612ba..b01f73189c 100644 --- a/src/lib/http/http_types.h +++ b/src/lib/http/http_types.h @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -7,11 +7,40 @@ #ifndef HTTP_TYPES_H #define HTTP_TYPES_H -#include - /// @brief HTTP protocol version. -/// -/// First value is a major version, second value is a minor version. -typedef std::pair HttpVersion; +struct HttpVersion { + unsigned major_; ///< Major HTTP version. + unsigned minor_; ///< Minor HTTP version. + + /// @brief Constructor. + /// + /// @param major Major HTTP version. + /// @param minor Minor HTTP version. + explicit HttpVersion(const unsigned major, const unsigned minor) + : major_(major), minor_(minor) { + } + + /// @brief Operator less. + /// + /// @param rhs Version to compare to. + bool operator<(const HttpVersion& rhs) const { + return ((major_ < rhs.major_) || + ((major_ == rhs.major_) && (minor_ < rhs.minor_))); + } + + /// @brief Operator equal. + /// + /// @param rhs Version to compare to. + bool operator==(const HttpVersion& rhs) const { + return ((major_ == rhs.major_) && (minor_ == rhs.minor_)); + } + + /// @brief Operator not equal. + /// + /// @param rhs Version to compare to. + bool operator!=(const HttpVersion& rhs) const { + return (!operator==(rhs)); + } +}; #endif diff --git a/src/lib/http/request.cc b/src/lib/http/request.cc index 3c7405688e..206ab332fe 100644 --- a/src/lib/http/request.cc +++ b/src/lib/http/request.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -66,8 +66,8 @@ HttpRequest::create() { } // Check if the HTTP version is allowed for this request. - if (!inRequiredSet(std::make_pair(context_->http_version_major_, - context_->http_version_minor_), + if (!inRequiredSet(HttpVersion(context_->http_version_major_, + context_->http_version_minor_), required_versions_)) { isc_throw(BadValue, "use of HTTP version " << context_->http_version_major_ << "." @@ -144,11 +144,11 @@ HttpRequest::getUri() const { return (context_->uri_); } -HttpRequest::HttpVersion +HttpVersion HttpRequest::getHttpVersion() const { checkCreated(); - return (std::make_pair(context_->http_version_major_, - context_->http_version_minor_)); + return (HttpVersion(context_->http_version_major_, + context_->http_version_minor_)); } std::string diff --git a/src/lib/http/request.h b/src/lib/http/request.h index e821685ba6..290e598413 100644 --- a/src/lib/http/request.h +++ b/src/lib/http/request.h @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -8,6 +8,7 @@ #define HTTP_REQUEST_H #include +#include #include #include #include @@ -60,9 +61,6 @@ typedef boost::shared_ptr ConstHttpRequestPtr; class HttpRequest { public: - /// @brief Type of HTTP version, including major and minor version number. - typedef std::pair HttpVersion; - /// @brief HTTP methods. enum class Method { HTTP_GET, diff --git a/src/lib/http/response.cc b/src/lib/http/response.cc index 705b5fe79d..cf8393c5e8 100644 --- a/src/lib/http/response.cc +++ b/src/lib/http/response.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -101,7 +101,7 @@ std::string HttpResponse::toString() const { std::ostringstream s; // HTTP version number and status code. - s << "HTTP/" << http_version_.first << "." << http_version_.second; + s << "HTTP/" << http_version_.major_ << "." << http_version_.minor_; s << " " << static_cast(status_code_); s << " " << statusCodeToString(status_code_) << crlf; diff --git a/src/lib/http/tests/post_request_json_unittests.cc b/src/lib/http/tests/post_request_json_unittests.cc index 89668d536d..f335521765 100644 --- a/src/lib/http/tests/post_request_json_unittests.cc +++ b/src/lib/http/tests/post_request_json_unittests.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -49,14 +50,14 @@ public: // POST messages. TEST_F(PostHttpRequestJsonTest, requiredPost) { // Use a GET method that is not supported. - setContextBasics("GET", "/isc/org", std::make_pair(1, 0)); + setContextBasics("GET", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Type", "application/json"); ASSERT_THROW(request_.create(), HttpRequestError); // Now use POST. It should be accepted. - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Type", "application/json"); @@ -67,14 +68,14 @@ TEST_F(PostHttpRequestJsonTest, requiredPost) { // header equal to "application/json". TEST_F(PostHttpRequestJsonTest, requireContentTypeJson) { // Specify "Content-Type" other than "application/json". - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Type", "text/html"); ASSERT_THROW(request_.create(), HttpRequestError); // This time specify correct "Content-Type". It should pass. - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Type", "application/json"); @@ -85,13 +86,13 @@ TEST_F(PostHttpRequestJsonTest, requireContentTypeJson) { // header. TEST_F(PostHttpRequestJsonTest, requireContentLength) { // "Content-Length" is not specified initially. It should fail. - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Type", "text/html"); ASSERT_THROW(request_.create(), HttpRequestError); // Specify "Content-Length". It should pass. - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Type", "application/json"); } @@ -100,7 +101,7 @@ TEST_F(PostHttpRequestJsonTest, requireContentLength) { // HTTP request. TEST_F(PostHttpRequestJsonTest, getBodyAsJson) { // Create HTTP POST request with JSON body. - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Type", "application/json"); setBody(); @@ -129,7 +130,7 @@ TEST_F(PostHttpRequestJsonTest, getBodyAsJson) { // This test verifies that an attempt to parse/retrieve malformed // JSON structure will cause an exception. TEST_F(PostHttpRequestJsonTest, getBodyAsJsonMalformed) { - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Type", "application/json"); // No colon before 123. @@ -141,7 +142,7 @@ TEST_F(PostHttpRequestJsonTest, getBodyAsJsonMalformed) { // This test verifies that NULL pointer is returned when trying to // retrieve root element of the empty JSON structure. TEST_F(PostHttpRequestJsonTest, getEmptyJsonBody) { - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Type", "application/json"); @@ -153,7 +154,7 @@ TEST_F(PostHttpRequestJsonTest, getEmptyJsonBody) { // This test verifies that the specific JSON element can be retrieved. TEST_F(PostHttpRequestJsonTest, getJsonElement) { - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Type", "application/json"); setBody(); diff --git a/src/lib/http/tests/request_parser_unittests.cc b/src/lib/http/tests/request_parser_unittests.cc index 224d8a2d96..5760db19aa 100644 --- a/src/lib/http/tests/request_parser_unittests.cc +++ b/src/lib/http/tests/request_parser_unittests.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -118,8 +119,8 @@ TEST_F(HttpRequestParserTest, postHttpRequestWithJson) { EXPECT_EQ("/foo/bar", request.getUri()); EXPECT_EQ("application/json", request.getHeaderValue("Content-Type")); EXPECT_EQ(json.length(), request.getHeaderValueAsUint64("Content-Length")); - EXPECT_EQ(1, request.getHttpVersion().first); - EXPECT_EQ(0, request.getHttpVersion().second); + EXPECT_EQ(1, request.getHttpVersion().major_); + EXPECT_EQ(0, request.getHttpVersion().minor_); // Try to retrieve values carried in JSON payload. ConstElementPtr json_element; @@ -190,8 +191,8 @@ TEST_F(HttpRequestParserTest, getLWS) { EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); EXPECT_EQ("Kea/1.2 Command Control Client", request_.getHeaderValue("User-Agent")); - EXPECT_EQ(1, request_.getHttpVersion().first); - EXPECT_EQ(1, request_.getHttpVersion().second); + EXPECT_EQ(1, request_.getHttpVersion().major_); + EXPECT_EQ(1, request_.getHttpVersion().minor_); } // This test verifies that the HTTP request with no headers is @@ -204,8 +205,8 @@ TEST_F(HttpRequestParserTest, noHeaders) { // Verify the values. EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ("/foo/bar", request_.getUri()); - EXPECT_EQ(1, request_.getHttpVersion().first); - EXPECT_EQ(1, request_.getHttpVersion().second); + EXPECT_EQ(1, request_.getHttpVersion().major_); + EXPECT_EQ(1, request_.getHttpVersion().minor_); } // This test verifies that the HTTP method can be specified in lower @@ -219,8 +220,8 @@ TEST_F(HttpRequestParserTest, getLowerCase) { EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ("/foo/bar", request_.getUri()); EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); - EXPECT_EQ(1, request_.getHttpVersion().first); - EXPECT_EQ(1, request_.getHttpVersion().second); + EXPECT_EQ(1, request_.getHttpVersion().major_); + EXPECT_EQ(1, request_.getHttpVersion().minor_); } // This test verifies that other value of the HTTP version can be @@ -234,8 +235,8 @@ TEST_F(HttpRequestParserTest, http20) { EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ("/foo/bar", request_.getUri()); EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); - EXPECT_EQ(2, request_.getHttpVersion().first); - EXPECT_EQ(0, request_.getHttpVersion().second); + EXPECT_EQ(2, request_.getHttpVersion().major_); + EXPECT_EQ(0, request_.getHttpVersion().minor_); } // This test verifies that the header with no whitespace between the @@ -249,8 +250,8 @@ TEST_F(HttpRequestParserTest, noHeaderWhitespace) { EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ("/foo/bar", request_.getUri()); EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); - EXPECT_EQ(1, request_.getHttpVersion().first); - EXPECT_EQ(0, request_.getHttpVersion().second); + EXPECT_EQ(1, request_.getHttpVersion().major_); + EXPECT_EQ(0, request_.getHttpVersion().minor_); } // This test verifies that the header value preceded with multiple @@ -264,8 +265,8 @@ TEST_F(HttpRequestParserTest, multipleLeadingHeaderWhitespaces) { EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ("/foo/bar", request_.getUri()); EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); - EXPECT_EQ(1, request_.getHttpVersion().first); - EXPECT_EQ(0, request_.getHttpVersion().second); + EXPECT_EQ(1, request_.getHttpVersion().major_); + EXPECT_EQ(0, request_.getHttpVersion().minor_); } // This test verifies that error is reported when unsupported HTTP diff --git a/src/lib/http/tests/request_test.h b/src/lib/http/tests/request_test.h index 017de3c1ec..f2755ec8d2 100644 --- a/src/lib/http/tests/request_test.h +++ b/src/lib/http/tests/request_test.h @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -7,6 +7,7 @@ #ifndef HTTP_REQUEST_TEST_H #define HTTP_REQUEST_TEST_H +#include #include #include #include @@ -50,11 +51,11 @@ public: /// @param version A pair of values of which the first is the major HTTP /// version and the second is the minor HTTP version. void setContextBasics(const std::string& method, const std::string& uri, - const std::pair& version) { + const HttpVersion& version) { request_.context()->method_ = method; request_.context()->uri_ = uri; - request_.context()->http_version_major_ = version.first; - request_.context()->http_version_minor_ = version.second; + request_.context()->http_version_major_ = version.major_; + request_.context()->http_version_minor_ = version.minor_; } /// @brief Adds HTTP header to the context. diff --git a/src/lib/http/tests/request_unittests.cc b/src/lib/http/tests/request_unittests.cc index a668b8efc9..ae6bbf20f9 100644 --- a/src/lib/http/tests/request_unittests.cc +++ b/src/lib/http/tests/request_unittests.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -20,28 +21,28 @@ namespace { typedef HttpRequestTestBase HttpRequestTest; TEST_F(HttpRequestTest, minimal) { - setContextBasics("GET", "/isc/org", std::make_pair(1, 1)); + setContextBasics("GET", "/isc/org", HttpVersion(1, 1)); ASSERT_NO_THROW(request_.create()); EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ("/isc/org", request_.getUri()); - EXPECT_EQ(1, request_.getHttpVersion().first); - EXPECT_EQ(1, request_.getHttpVersion().second); + EXPECT_EQ(1, request_.getHttpVersion().major_); + EXPECT_EQ(1, request_.getHttpVersion().minor_); EXPECT_THROW(request_.getHeaderValue("Content-Length"), HttpRequestNonExistingHeader); } TEST_F(HttpRequestTest, includeHeaders) { - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", "1024"); addHeaderToContext("Content-Type", "application/json"); ASSERT_NO_THROW(request_.create()); EXPECT_EQ(HttpRequest::Method::HTTP_POST, request_.getMethod()); EXPECT_EQ("/isc/org", request_.getUri()); - EXPECT_EQ(1, request_.getHttpVersion().first); - EXPECT_EQ(0, request_.getHttpVersion().second); + EXPECT_EQ(1, request_.getHttpVersion().major_); + EXPECT_EQ(0, request_.getHttpVersion().minor_); std::string content_type; ASSERT_NO_THROW(content_type = request_.getHeaderValue("Content-Type")); @@ -58,7 +59,7 @@ TEST_F(HttpRequestTest, requiredMethods) { request_.requireHttpMethod(HttpRequest::Method::HTTP_GET); request_.requireHttpMethod(HttpRequest::Method::HTTP_POST); - setContextBasics("GET", "/isc/org", std::make_pair(1, 1)); + setContextBasics("GET", "/isc/org", HttpVersion(1, 1)); ASSERT_NO_THROW(request_.create()); @@ -70,22 +71,22 @@ TEST_F(HttpRequestTest, requiredMethods) { } TEST_F(HttpRequestTest, requiredHttpVersion) { - request_.requireHttpVersion(std::make_pair(1, 0)); - request_.requireHttpVersion(std::make_pair(1, 1)); + request_.requireHttpVersion(HttpVersion(1, 0)); + request_.requireHttpVersion(HttpVersion(1, 1)); - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); EXPECT_NO_THROW(request_.create()); - setContextBasics("POST", "/isc/org", std::make_pair(1, 1)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 1)); EXPECT_NO_THROW(request_.create()); - setContextBasics("POST", "/isc/org", std::make_pair(2, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(2, 0)); EXPECT_THROW(request_.create(), HttpRequestError); } TEST_F(HttpRequestTest, requiredHeader) { request_.requireHeader("Content-Length"); - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); ASSERT_THROW(request_.create(), HttpRequestError); @@ -98,7 +99,7 @@ TEST_F(HttpRequestTest, requiredHeader) { TEST_F(HttpRequestTest, requiredHeaderValue) { request_.requireHeaderValue("Content-Type", "application/json"); - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Type", "text/html"); ASSERT_THROW(request_.create(), HttpRequestError); @@ -109,7 +110,7 @@ TEST_F(HttpRequestTest, requiredHeaderValue) { } TEST_F(HttpRequestTest, notCreated) { - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Type", "text/html"); addHeaderToContext("Content-Length", "1024"); @@ -138,7 +139,7 @@ TEST_F(HttpRequestTest, notCreated) { TEST_F(HttpRequestTest, getBody) { std::string json_body = "{ \"param1\": \"foo\" }"; - setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); addHeaderToContext("Content-Length", json_body.length()); request_.context()->body_ = json_body; diff --git a/src/lib/http/tests/response_creator_unittests.cc b/src/lib/http/tests/response_creator_unittests.cc index bf26047bba..8f79aa3f48 100644 --- a/src/lib/http/tests/response_creator_unittests.cc +++ b/src/lib/http/tests/response_creator_unittests.cc @@ -1,10 +1,11 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include +#include #include #include #include diff --git a/src/lib/http/tests/response_json_unittests.cc b/src/lib/http/tests/response_json_unittests.cc index 589702fa91..898662a116 100644 --- a/src/lib/http/tests/response_json_unittests.cc +++ b/src/lib/http/tests/response_json_unittests.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -6,6 +6,7 @@ #include #include +#include #include #include #include diff --git a/src/lib/http/tests/response_test.h b/src/lib/http/tests/response_test.h index fa92368547..8d4e24e1c6 100644 --- a/src/lib/http/tests/response_test.h +++ b/src/lib/http/tests/response_test.h @@ -1,4 +1,4 @@ -// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -7,6 +7,9 @@ #ifndef HTTP_RESPONSE_TEST_H #define HTTP_RESPONSE_TEST_H +#include +#include + namespace isc { namespace http { namespace test {