-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add few more utility functions for address and http proto's. #2504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
da62afa
858d842
ab15cc3
a519fb3
57c6f26
e4156e8
49a3570
df0f0bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| #pragma once | ||
| #include <string> | ||
|
|
||
| namespace Envoy { | ||
| namespace Http { | ||
|
|
@@ -10,5 +11,24 @@ namespace Http { | |
| enum class Protocol { Http10, Http11, Http2 }; | ||
| const size_t NumProtocols = 3; | ||
|
|
||
| static const std::string DefaultString = ""; | ||
| static const std::string Http10String = "HTTP/1.0"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Static non-POD is not allowed in the Envoy style, see https://github.com/envoyproxy/envoy/blob/master/STYLE.md. This could probably just be
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also put these in a struct in headers.h
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO we should continue to return string references for perf reasons. I still think that the non-POD rule for std::string is a little silly, but, the rules are the rules. I would just use the constant class pattern we use elsewhere like in Http::Headers.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mattklein123 i think you are referring to https://github.com/envoyproxy/envoy/blob/master/source/common/http/headers.h
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either way is fine with me
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mattklein123 @alyssawilk Last push addresses this. Adding
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend moving into common and using NOT_REACHED. You can either put in headers.h or feel free to just put in Http::Utility
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool. moved. please review when u folks get a chance. thanks |
||
| static const std::string Http11String = "HTTP/1.1"; | ||
| static const std::string Http2String = "HTTP/2"; | ||
|
|
||
| inline const std::string& getProtocolString(const Protocol& p) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Protocol should be passed by value: |
||
| switch (p) { | ||
| case Protocol::Http10: | ||
| return Http10String; | ||
| case Protocol::Http11: | ||
| return Http11String; | ||
| case Protocol::Http2: | ||
| return Http2String; | ||
| default: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mild preference for no default - I'd rather have a compile fail if we add a new Protocol and forget to update this function.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 just return directly and avoid default. Add NOT_REACHED at end of function if necessary. |
||
| break; | ||
| } | ||
| return DefaultString; | ||
| } | ||
|
|
||
| } // namespace Http | ||
| } // namespace Envoy | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -371,5 +371,17 @@ absl::uint128 Utility::flipOrder(const absl::uint128& input) { | |
| return result; | ||
| } | ||
|
|
||
| void Utility::addressToProtobufAddress(const Address::Instance& address, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -198,6 +198,21 @@ 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()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| } | ||
| { | ||
| envoy::api::v2::Address proto_address; | ||
| Address::PipeInstance address("/hello"); | ||
| Utility::addressToProtobufAddress(address, proto_address); | ||
| EXPECT_EQ(false, proto_address.has_socket_address()); | ||
| } | ||
| } | ||
|
|
||
| TEST(PortRangeListTest, Errors) { | ||
| { | ||
| std::string port_range_str = "a1"; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this include be removed?