Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/envoy/http/header_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class HeaderString {
*/
const char* c_str() const { return buffer_.ref_; }

/**
* @return a std::string.
*/
std::string getString() const { return std::string(buffer_.ref_, string_length_); }

/**
* Return the string to a default state. Reference strings are not touched. Both inline/dynamic
* strings are reset to zero size.
Expand Down
2 changes: 2 additions & 0 deletions source/common/access_log/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ envoy_cc_library(
"//include/envoy/request_info:request_info_interface",
"//source/common/common:assert_lib",
"//source/common/common:utility_lib",
"//source/common/http:utility_lib",
"//source/common/request_info:utility_lib",
],
)
Expand Down Expand Up @@ -65,6 +66,7 @@ envoy_cc_library(
"//include/envoy/thread_local:thread_local_interface",
"//include/envoy/upstream:cluster_manager_interface",
"//source/common/grpc:async_client_lib",
"//source/common/network:utility_lib",
"@envoy_api//envoy/api/v2/filter/accesslog:accesslog_cc",
"@envoy_api//envoy/config/accesslog/v2:als_cc",
"@envoy_api//envoy/service/accesslog/v2:als_cc",
Expand Down
19 changes: 3 additions & 16 deletions source/common/access_log/access_log_formatter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "common/common/assert.h"
#include "common/common/fmt.h"
#include "common/common/utility.h"
#include "common/http/utility.h"
#include "common/request_info/utility.h"

namespace Envoy {
Expand All @@ -25,26 +26,12 @@ FormatterPtr AccessLogFormatUtils::defaultAccessLogFormatter() {
return FormatterPtr{new FormatterImpl(DEFAULT_FORMAT)};
}

static const std::string Http10String = "HTTP/1.0";
static const std::string Http11String = "HTTP/1.1";
static const std::string Http2String = "HTTP/2";

const std::string&
AccessLogFormatUtils::protocolToString(const Optional<Http::Protocol>& protocol) {
if (protocol.valid()) {
switch (protocol.value()) {
case Http::Protocol::Http10:
return Http10String;
case Http::Protocol::Http11:
return Http11String;
case Http::Protocol::Http2:
return Http2String;
}
} else {
return UnspecifiedValueString;
return Http::Utility::getProtocolString(protocol.value());
}

NOT_REACHED;
return UnspecifiedValueString;
}

FormatterImpl::FormatterImpl(const std::string& format) {
Expand Down
32 changes: 12 additions & 20 deletions source/common/access_log/grpc_access_log_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "common/common/assert.h"
#include "common/http/header_map_impl.h"
#include "common/network/utility.h"

namespace Envoy {
namespace AccessLog {
Expand Down Expand Up @@ -64,18 +65,6 @@ HttpGrpcAccessLog::HttpGrpcAccessLog(
: filter_(std::move(filter)), config_(config),
grpc_access_log_streamer_(grpc_access_log_streamer) {}

void HttpGrpcAccessLog::addressToAccessLogAddress(
envoy::api::v2::Address& proto_address, const Network::Address::Instance& network_address) {
if (network_address.type() == Network::Address::Type::Pipe) {
proto_address.mutable_pipe()->set_path(network_address.asString());
} else {
ASSERT(network_address.type() == Network::Address::Type::Ip);
auto* socket_address = proto_address.mutable_socket_address();
socket_address->set_address(network_address.ip()->addressAsString());
socket_address->set_port_value(network_address.ip()->port());
}
}

void HttpGrpcAccessLog::responseFlagsToAccessLogResponseFlags(
envoy::api::v2::filter::accesslog::AccessLogCommon& common_access_log,
const RequestInfo::RequestInfo& request_info) {
Expand Down Expand Up @@ -161,10 +150,12 @@ void HttpGrpcAccessLog::log(const Http::HeaderMap* request_headers,
// TODO(mattklein123): Populate time_to_first_downstream_tx_byte field.
// TODO(mattklein123): Populate metadata field and wire up to filters.
auto* common_properties = log_entry->mutable_common_properties();
addressToAccessLogAddress(*common_properties->mutable_downstream_remote_address(),
*request_info.downstreamRemoteAddress());
addressToAccessLogAddress(*common_properties->mutable_downstream_local_address(),
*request_info.downstreamLocalAddress());
Network::Utility::addressToProtobufAddress(
*request_info.downstreamRemoteAddress(),
*common_properties->mutable_downstream_remote_address());
Network::Utility::addressToProtobufAddress(
*request_info.downstreamLocalAddress(),
*common_properties->mutable_downstream_local_address());
common_properties->mutable_start_time()->MergeFrom(
Protobuf::util::TimeUtil::MicrosecondsToTimestamp(
std::chrono::duration_cast<std::chrono::microseconds>(
Expand All @@ -183,13 +174,14 @@ void HttpGrpcAccessLog::log(const Http::HeaderMap* request_headers,
common_properties->mutable_time_to_last_downstream_tx_byte()->MergeFrom(
Protobuf::util::TimeUtil::MicrosecondsToDuration(request_info.duration().count()));
if (request_info.upstreamHost() != nullptr) {
addressToAccessLogAddress(*common_properties->mutable_upstream_remote_address(),
*request_info.upstreamHost()->address());
Network::Utility::addressToProtobufAddress(
*request_info.upstreamHost()->address(),
*common_properties->mutable_upstream_remote_address());
common_properties->set_upstream_cluster(request_info.upstreamHost()->cluster().name());
}
if (request_info.upstreamLocalAddress() != nullptr) {
addressToAccessLogAddress(*common_properties->mutable_upstream_local_address(),
*request_info.upstreamLocalAddress());
Network::Utility::addressToProtobufAddress(
*request_info.upstreamLocalAddress(), *common_properties->mutable_upstream_local_address());
}
responseFlagsToAccessLogResponseFlags(*common_properties, request_info);

Expand Down
2 changes: 0 additions & 2 deletions source/common/access_log/grpc_access_log_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ class HttpGrpcAccessLog : public Instance {
const envoy::config::accesslog::v2::HttpGrpcAccessLogConfig& config,
GrpcAccessLogStreamerSharedPtr grpc_access_log_streamer);

static void addressToAccessLogAddress(envoy::api::v2::Address& proto_address,
const Network::Address::Instance& network_address);
static void responseFlagsToAccessLogResponseFlags(
envoy::api::v2::filter::accesslog::AccessLogCommon& common_access_log,
const RequestInfo::RequestInfo& request_info);
Expand Down
6 changes: 6 additions & 0 deletions source/common/http/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ class HeaderValues {
struct {
const std::string True{"true"};
} CORSValues;

struct {
const std::string Http10String{"HTTP/1.0"};
const std::string Http11String{"HTTP/1.1"};
const std::string Http2String{"HTTP/2"};
} ProtocolStrings;
};

typedef ConstSingleton<HeaderValues> Headers;
Expand Down
13 changes: 13 additions & 0 deletions source/common/http/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,18 @@ Utility::getLastAddressFromXFF(const Http::HeaderMap& request_headers) {
}
}

const std::string& Utility::getProtocolString(const Protocol protocol) {
switch (protocol) {
case Protocol::Http10:
return Headers::get().ProtocolStrings.Http10String;
case Protocol::Http11:
return Headers::get().ProtocolStrings.Http11String;
case Protocol::Http2:
return Headers::get().ProtocolStrings.Http2String;
}

NOT_REACHED;
}

} // namespace Http
} // namespace Envoy
7 changes: 7 additions & 0 deletions source/common/http/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ class Utility {
* @see GetLastAddressFromXffInfo for more information.
*/
static GetLastAddressFromXffInfo getLastAddressFromXFF(const Http::HeaderMap& request_headers);

/**
* Get the string for the given http protocol.
* @param protocol for which to return the string representation.
* @return string representation of the protocol.
*/
static const std::string& getProtocolString(const Protocol p);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By docs comments I think this should be named "protocol" rather than "p"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. fixed this and the other comment as well.
Merged master also, hopefully that will help with the macos build. fingers crossed.
Thanks for the quick review.

};

} // namespace Http
Expand Down
1 change: 1 addition & 0 deletions source/common/network/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ envoy_cc_library(
"//source/common/common:assert_lib",
"//source/common/common:utility_lib",
"//source/common/protobuf",
"@envoy_api//envoy/api/v2:address_cc",
"@envoy_api//envoy/api/v2:base_cc",
],
)
12 changes: 12 additions & 0 deletions source/common/network/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -371,5 +371,17 @@ absl::uint128 Utility::flipOrder(const absl::uint128& input) {
return result;
}

void Utility::addressToProtobufAddress(const Address::Instance& address,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already implemented here: https://github.com/envoyproxy/envoy/blob/master/source/common/access_log/grpc_access_log_impl.cc#L67 please consolidate

Also, a dedicated function like this needs dedicated tests.

Copy link
Copy Markdown
Contributor Author

@saumoh saumoh Feb 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added tests and consolidated. thanks.

envoy::api::v2::Address& proto_address) {
if (address.type() == Address::Type::Pipe) {
proto_address.mutable_pipe()->set_path(address.asString());
} else {
ASSERT(address.type() == Address::Type::Ip);
auto* socket_address = proto_address.mutable_socket_address();
socket_address->set_address(address.ip()->addressAsString());
socket_address->set_port_value(address.ip()->port());
}
}

} // namespace Network
} // namespace Envoy
8 changes: 8 additions & 0 deletions source/common/network/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ class Utility {
*/
static absl::uint128 Ip6htonl(const absl::uint128& address);

/**
* Copies the address instance into the protobuf representation of an address.
* @param address is the address to be copied into the protobuf representation of this address.
* @param proto_address is the protobuf address to which the address instance is copied into.
*/
static void addressToProtobufAddress(const Address::Instance& address,
envoy::api::v2::Address& proto_address);

private:
static void throwWithMalformedIp(const std::string& ip_address);

Expand Down
13 changes: 13 additions & 0 deletions test/common/http/header_map_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ TEST(HeaderStringTest, All) {
EXPECT_FALSE(string.caseInsensitiveContains("keep-alive"));
EXPECT_FALSE(string.caseInsensitiveContains(""));
}

// getString
{
std::string static_string("HELLO");
HeaderString headerString1(static_string);
std::string retString1 = headerString1.getString();
EXPECT_EQ("HELLO", retString1);
EXPECT_EQ(5U, retString1.size());

HeaderString headerString2;
std::string retString2 = headerString2.getString();
EXPECT_EQ(0U, retString2.size());
}
}

TEST(HeaderMapImplTest, InlineInsert) {
Expand Down
18 changes: 18 additions & 0 deletions test/common/network/utility_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,24 @@ TEST(NetworkUtility, AnyAddress) {
}
}

TEST(NetworkUtility, AddressToProtobufAddress) {
{
envoy::api::v2::Address proto_address;
Address::Ipv4Instance address("127.0.0.1");
Utility::addressToProtobufAddress(address, proto_address);
EXPECT_EQ(true, proto_address.has_socket_address());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, mind checking the value and port here, and adding a test case for pipe?

EXPECT_EQ("127.0.0.1", proto_address.socket_address().address());
EXPECT_EQ(0, proto_address.socket_address().port_value());
}
{
envoy::api::v2::Address proto_address;
Address::PipeInstance address("/hello");
Utility::addressToProtobufAddress(address, proto_address);
EXPECT_EQ(true, proto_address.has_pipe());
EXPECT_EQ("/hello", proto_address.pipe().path());
}
}

TEST(PortRangeListTest, Errors) {
{
std::string port_range_str = "a1";
Expand Down