Skip to content

Add few more utility functions for address and http proto's.#2504

Merged
htuch merged 8 commits intoenvoyproxy:masterfrom
colabsaumoh:ext-auth-review-generics
Feb 2, 2018
Merged

Add few more utility functions for address and http proto's.#2504
htuch merged 8 commits intoenvoyproxy:masterfrom
colabsaumoh:ext-auth-review-generics

Conversation

@saumoh
Copy link
Copy Markdown
Contributor

@saumoh saumoh commented Feb 1, 2018

Title
Add a few utility functions to the

  1. http/protocol and http_header_map.h files
  2. Another utility function to copy Address::Instance into it's protobuf representation

Testing
I have tested and used these new functions in the PR #2415
added a ut for the http changes.

Risk
Low: like really low.

Signed-off-by: Saurabh Mohan <saurabh+github@tigera.io>
Copy link
Copy Markdown
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

few quick comments, thanks

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.

/**
* @return a std::string.
*/
std::string getString() const {
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.

Is this really necessary? Seems pretty superfluous to me, but I don't feel that strongly about it. If it is, I suspect you don't need the if statement and the constructor will do the right thing if you pass a length of 0, but I'm not sure.

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.

the need for it came up in the feedback for pr #2415
it is kind of useful to have it here and not have the user have to convert a c_str(). thx

enum class Protocol { Http10, Http11, Http2 };
const size_t NumProtocols = 3;

inline std::string getProtocolString(const Protocol& p) {
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/access_log_formatter.cc#L33

Please consolidate (and use the string in access log formatter to be consistent).

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.

consolidated. Thanks.

Saurabh Mohan added 3 commits January 31, 2018 22:28
Signed-off-by: Saurabh Mohan <saurabh+github@tigera.io>
Signed-off-by: Saurabh Mohan <saurabh+github@tigera.io>
Signed-off-by: Saurabh Mohan <saurabh+github@tigera.io>
Copy link
Copy Markdown
Contributor

@alyssawilk alyssawilk left a comment

Choose a reason for hiding this comment

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

nice change! 2 nits from my end.

return Http11String;
case Protocol::Http2:
return Http2String;
default:
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.

Mild preference for no default - I'd rather have a compile fail if we add a new Protocol and forget to update this function.

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.

+1 just return directly and avoid default. Add NOT_REACHED at end of function if necessary.

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?

@alyssawilk alyssawilk self-assigned this Feb 1, 2018
const size_t NumProtocols = 3;

static const std::string DefaultString = "";
static const std::string Http10String = "HTTP/1.0";
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.

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 const char DefaultString = "" etc.

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.

We could also put these in a struct in headers.h
I was wavering on asking that anyway - they're not so much headers as "part of the firstline" but it's where I'd look for predefined header-type strings.

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.

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.

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.

@mattklein123 i think you are referring to https://github.com/envoyproxy/envoy/blob/master/source/common/http/headers.h
if so then i can certainly adopt that patter here as well.
@alyssawilk is it ok if i keep the new constant class here?

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.

Either way is fine with me

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.

@mattklein123 @alyssawilk Last push addresses this. Adding NOT_REACHED in this file causes a circular dependency so i added a abort(). If that is not what we want then I can move both constants and the getProtocolString to the common/http/headers.h file.
please let me know if that would be preferable.

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.

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

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.

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) {
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.

Protocol should be passed by value: Protocol p

const size_t NumProtocols = 3;

static const std::string DefaultString = "";
static const std::string Http10String = "HTTP/1.0";
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.

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.

return Http11String;
case Protocol::Http2:
return Http2String;
default:
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.

+1 just return directly and avoid default. Add NOT_REACHED at end of function if necessary.

Saurabh Mohan added 2 commits February 1, 2018 15:30
Signed-off-by: Saurabh Mohan <saurabh+github@tigera.io>
Signed-off-by: Saurabh Mohan <saurabh+github@tigera.io>
Copy link
Copy Markdown
Contributor

@alyssawilk alyssawilk left a comment

Choose a reason for hiding this comment

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

Looking good - just two nits from me otherwise LGTM

@@ -1,4 +1,5 @@
#pragma once
#include <string>
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.

Can this include be removed?

* @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.

@alyssawilk
Copy link
Copy Markdown
Contributor

Also I'm not quite sure what's with the macos build - I don't recall seeing this error mode on other PRs. Could you try merging with master and take a look if that doesn't fix it?

Saurabh Mohan added 2 commits February 1, 2018 18:47
Signed-off-by: Saurabh Mohan <saurabh+github@tigera.io>
…enerics

Signed-off-by: Saurabh Mohan <saurabh+github@tigera.io>
Copy link
Copy Markdown
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@htuch htuch merged commit 5f8b841 into envoyproxy:master Feb 2, 2018
@saumoh saumoh deleted the ext-auth-review-generics branch May 4, 2018 17:28
Shikugawa pushed a commit to Shikugawa/envoy that referenced this pull request Mar 28, 2020
)

Signed-off-by: Piotr Sikora <piotrsikora@google.com>
jpsim pushed a commit that referenced this pull request Nov 28, 2022
These changes are prerequisite for
envoyproxy/envoy-mobile#1549

Signed-off-by: Chidera Olibie <colibie@google.com>
Signed-off-by: JP Simard <jp@jpsim.com>
jpsim pushed a commit that referenced this pull request Nov 29, 2022
These changes are prerequisite for
envoyproxy/envoy-mobile#1549

Signed-off-by: Chidera Olibie <colibie@google.com>
Signed-off-by: JP Simard <jp@jpsim.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants