Commit 3bcb33f2 authored by Marcin Siodelski's avatar Marcin Siodelski

[5088] HttpVersion is now a struct rather than a pair.

This is a result of the review.
parent 17a4fb25
// 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 <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
......@@ -66,7 +66,7 @@ HttpRequest::create() {
}
// Check if the HTTP version is allowed for this request.
if (!inRequiredSet(std::make_pair(context_->http_version_major_,
if (!inRequiredSet(HttpVersion(context_->http_version_major_,
context_->http_version_minor_),
required_versions_)) {
isc_throw(BadValue, "use of HTTP version "
......@@ -144,10 +144,10 @@ HttpRequest::getUri() const {
return (context_->uri_);
}
HttpRequest::HttpVersion
HttpVersion
HttpRequest::getHttpVersion() const {
checkCreated();
return (std::make_pair(context_->http_version_major_,
return (HttpVersion(context_->http_version_major_,
context_->http_version_minor_));
}
......
// 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 <exceptions/exceptions.h>
#include <http/http_types.h>
#include <http/request_context.h>
#include <boost/shared_ptr.hpp>
#include <map>
......@@ -60,9 +61,6 @@ typedef boost::shared_ptr<const HttpRequest> ConstHttpRequestPtr;
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
......@@ -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<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
......@@ -7,6 +7,7 @@
#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>
......@@ -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();
......
// 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 <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>
......@@ -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
......
// 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 <http/http_types.h>
#include <http/request.h>
#include <boost/lexical_cast.hpp>
#include <gtest/gtest.h>
......@@ -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<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
......@@ -7,6 +7,7 @@
#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>
......@@ -20,28 +21,28 @@ namespace {
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"));
......@@ -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;
......
// 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
......@@ -6,6 +6,7 @@
#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
......@@ -7,6 +7,9 @@
#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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment