-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Set hostname rather than ip when channel Init by hostname but not host in http_requst().uri() #1529
Changes from all commits
dab62e4
95606cc
486de0d
5085c5e
a5c72a0
3cd4840
b1ebfe5
93b4efe
1d04a27
a3c02fa
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 |
---|---|---|
|
@@ -256,7 +256,7 @@ int Channel::Init(const char* server_addr, int port, | |
return -1; | ||
} | ||
} | ||
return InitSingle(point, server_addr, options); | ||
return InitSingle(point, server_addr, options, port); | ||
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. channel.Init("www.aa.com", 8899,...) 这个Init函数会调用这个带port参数的InitSingle |
||
} | ||
|
||
static int CreateSocketSSLContext(const ChannelOptions& options, | ||
|
@@ -283,16 +283,21 @@ int Channel::Init(butil::EndPoint server_addr_and_port, | |
|
||
int Channel::InitSingle(const butil::EndPoint& server_addr_and_port, | ||
const char* raw_server_address, | ||
const ChannelOptions* options) { | ||
const ChannelOptions* options, | ||
int raw_port) { | ||
GlobalInitializeOrDie(); | ||
if (InitChannelOptions(options) != 0) { | ||
return -1; | ||
} | ||
if (_options.protocol == brpc::PROTOCOL_HTTP && | ||
::strncmp(raw_server_address, "https://", 8) == 0) { | ||
std::string scheme; | ||
int* port_out = raw_port == -1 ? &raw_port: NULL; | ||
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. 如果raw_port 是默认值-1,那么就取它的地址,否则取NULL(表示raw_port已经有值了,不需要再从raw_server_address中解析) |
||
ParseURL(raw_server_address, &scheme, &_service_name, port_out); | ||
if (raw_port != -1) { | ||
_service_name.append(":").append(std::to_string(raw_port)); | ||
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. hostname:port 组合成 _service_name |
||
} | ||
if (_options.protocol == brpc::PROTOCOL_HTTP && scheme == "https://") { | ||
if (_options.mutable_ssl_options()->sni_name.empty()) { | ||
ParseURL(raw_server_address, | ||
NULL, &_options.mutable_ssl_options()->sni_name, NULL); | ||
_options.mutable_ssl_options()->sni_name = _service_name; | ||
} | ||
} | ||
const int port = server_addr_and_port.port; | ||
|
@@ -325,11 +330,15 @@ int Channel::Init(const char* ns_url, | |
if (InitChannelOptions(options) != 0) { | ||
return -1; | ||
} | ||
if (_options.protocol == brpc::PROTOCOL_HTTP && | ||
::strncmp(ns_url, "https://", 8) == 0) { | ||
std::string scheme; | ||
int raw_port = -1; | ||
ParseURL(ns_url, &scheme, &_service_name, &raw_port); | ||
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. 如果是name_service的Init,那么直接取尝试从ns_url中解析port |
||
if (raw_port != -1) { | ||
_service_name.append(":").append(std::to_string(raw_port)); | ||
} | ||
if (_options.protocol == brpc::PROTOCOL_HTTP && scheme == "https://") { | ||
if (_options.mutable_ssl_options()->sni_name.empty()) { | ||
ParseURL(ns_url, | ||
NULL, &_options.mutable_ssl_options()->sni_name, NULL); | ||
_options.mutable_ssl_options()->sni_name = _service_name; | ||
} | ||
} | ||
LoadBalancerWithNaming* lb = new (std::nothrow) LoadBalancerWithNaming; | ||
|
@@ -386,6 +395,12 @@ void Channel::CallMethod(const google::protobuf::MethodDescriptor* method, | |
CHECK(cntl->protocol_param().empty()); | ||
cntl->protocol_param() = _options.protocol.param(); | ||
} | ||
if (_options.protocol == brpc::PROTOCOL_HTTP) { | ||
URI& uri = cntl->http_request().uri(); | ||
if (uri.host().empty() && !_service_name.empty()) { | ||
uri.SetHostAndPort(_service_name); | ||
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. 一次性给uri的Host,port赋值 |
||
} | ||
} | ||
cntl->_preferred_index = _preferred_index; | ||
cntl->_retry_policy = _options.retry_policy; | ||
if (_options.enable_circuit_breaker) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,8 +213,10 @@ friend class SelectiveChannel; | |
int InitChannelOptions(const ChannelOptions* options); | ||
int InitSingle(const butil::EndPoint& server_addr_and_port, | ||
const char* raw_server_address, | ||
const ChannelOptions* options); | ||
const ChannelOptions* options, | ||
int raw_port = -1); | ||
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. InitSingle增加了一个带默认值的参数,raw_port |
||
|
||
std::string _service_name; | ||
butil::EndPoint _server_address; | ||
SocketId _server_id; | ||
Protocol::SerializeRequest _serialize_request; | ||
|
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.
最后这个Host是不是应该是"Host: www.foo.com:8989"?
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.
不是。在http_message.cpp中:
URL不包含host,但是channel初始化的时候包含的情况,我给uri的host字段赋了值。会走到:
if (!uri.host().empty())
的if 逻辑中。但是下面的uri.port() 是-1,所以不会把:port
追加到host中。我前面有个commit是改了http_message.cpp,改成:
从remote_side中拿port拼接到Host字段。不过你说可能有兼容性问题,所以我又去掉了。
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.
Hi, 我之前说的谦容性问题是,实现将Host: www.aa.com 变成了 Host: www.aa.com:80。现在这样实现的话 www.aa.com和www.aa.com:8899对应到请求全部都是Host: www.aa.com,不太符合预期 https://datatracker.ietf.org/doc/html/rfc2616#section-14.23
符合谦容性的实现是:
要想个办法把ParseUrl出来的port,也在set_host的时候顺便set_port,这样http_message.cpp就不用改了。简单看了下,似乎把parse出来的port追加到_service_name后面,然后替换set_host为uri.SetHostAndPort(_service_name)就可以了。看看可行么?
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.
有一点问题,如果调用的是 channel.Init("www.aa.com", 8899),那么对方收到的Host还是不包含端口号的。因为InitSingle中解析的原始地址,是Init的第一个参数。
感觉不存在十全十美的解决方案,我感觉还是要在CallMethod的时候,当uri不包含host的时候,把host和port都拼上。不影响uri中包含host时的处理逻辑。
另外一个方案就是在两个Init重载中,记录一下(新增bool成员变量存储标记,或者直接存原始port),InitSingle中判断是否单独设置了port参数。