This is a result of the review.
-// 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
#ifndef HTTP_TYPES_H
#define HTTP_TYPES_H
-#include <utility>
-
/// @brief HTTP protocol version.
-///
-/// First value is a major version, second value is a minor version.
-typedef std::pair<unsigned, unsigned> 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
-// 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
}
// 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_ << "."
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
-// 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
#define HTTP_REQUEST_H
#include <exceptions/exceptions.h>
+#include <http/http_types.h>
#include <http/request_context.h>
#include <boost/shared_ptr.hpp>
#include <map>
class HttpRequest {
public:
- /// @brief Type of HTTP version, including major and minor version number.
- typedef std::pair<unsigned int, unsigned int> HttpVersion;
-
/// @brief HTTP methods.
enum class Method {
HTTP_GET,
-// 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
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<uint16_t>(status_code_);
s << " " << statusCodeToString(status_code_) << crlf;
-// 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
#include <config.h>
#include <cc/data.h>
+#include <http/http_types.h>
#include <http/post_request_json.h>
#include <http/tests/request_test.h>
#include <gtest/gtest.h>
// 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");
// 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");
// 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");
}
// 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();
// 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.
// 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");
// 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();
-// 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
#include <config.h>
#include <cc/data.h>
+#include <http/http_types.h>
#include <http/request_parser.h>
#include <http/post_request_json.h>
#include <gtest/gtest.h>
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;
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
// 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
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
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
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
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
-// 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
#ifndef HTTP_REQUEST_TEST_H
#define HTTP_REQUEST_TEST_H
+#include <http/http_types.h>
#include <http/request.h>
#include <boost/lexical_cast.hpp>
#include <gtest/gtest.h>
/// @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<unsigned int, unsigned int>& 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.
-// 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
#include <config.h>
#include <http/request.h>
+#include <http/http_types.h>
#include <http/tests/request_test.h>
#include <boost/lexical_cast.hpp>
#include <gtest/gtest.h>
typedef HttpRequestTestBase<HttpRequest> 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"));
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());
}
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);
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);
}
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");
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;
-// 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 <config.h>
+#include <http/http_types.h>
#include <http/request.h>
#include <http/response.h>
#include <http/response_creator.h>
-// 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
#include <config.h>
#include <cc/data.h>
+#include <http/http_types.h>
#include <http/response_json.h>
#include <http/tests/response_test.h>
#include <gtest/gtest.h>
-// 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
#ifndef HTTP_RESPONSE_TEST_H
#define HTTP_RESPONSE_TEST_H
+#include <http/http_types.h>
+#include <http/response.h>
+
namespace isc {
namespace http {
namespace test {