guodongxiaren commented on a change in pull request #1529:
URL: https://github.com/apache/incubator-brpc/pull/1529#discussion_r701114304
##########
File path: src/brpc/channel.cpp
##########
@@ -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);
Review comment:
channel.Init("www.aa.com", 8899,...) 这个Init函数会调用这个带port参数的InitSingle
##########
File path: src/brpc/channel.cpp
##########
@@ -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);
Review comment:
如果是name_service的Init,那么直接取尝试从ns_url中解析port
##########
File path: src/brpc/channel.cpp
##########
@@ -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);
Review comment:
一次性给uri的Host,port赋值
##########
File path: docs/cn/http_client.md
##########
@@ -114,7 +114,9 @@ URL的一般形式如下图:
若用户没有填且URL中包含host,比如http://www.foo.com/path,则http request中会包含"Host:
www.foo.com"。
-若用户没有填且URL不包含host,比如"/index.html?name=value",则框架会以目标server的ip和port为Host,地址为10.46.188.39:8989的http
server将会看到"Host: 10.46.188.39:8989"。
+若用户没有填且URL不包含host,比如"/index.html?name=value",但如果Channel初始化的地址包含域名,则框架会以域名作为Host,比如"http://www.foo.com",该http
server将会看到"Host: www.foo.com"。如果地址是"http://www.foo.com:8989",则该http
server将会看到"Host: www.foo.com"。
+
Review comment:
@zyearn
我改了一下InitSingle的函数参数,多了一个raw_port参数(默认值-1),让InitSingle可以感知是从哪个Init重载调用进来的。
如果raw_port不等于-1,那么就是用它拼接到hostname后面作为 _service_name。
如果raw_port为-1,就去解析
raw_server_address(其实Init的第一个参数)字符串,看能不能解析到port。能的话就追加到hostname后面作为
_service_name,否则,直接用 hostname作为 _service_name。
CallMethod中调用SetHostAndPort
##########
File path: src/brpc/channel.h
##########
@@ -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);
Review comment:
InitSingle增加了一个带默认值的参数,raw_port
##########
File path: src/brpc/channel.cpp
##########
@@ -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;
+ ParseURL(raw_server_address, &scheme, &_service_name, port_out);
+ if (raw_port != -1) {
+ _service_name.append(":").append(std::to_string(raw_port));
Review comment:
hostname:port 组合成 _service_name
##########
File path: src/brpc/channel.cpp
##########
@@ -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;
Review comment:
如果raw_port
是默认值-1,那么就取它的地址,否则取NULL(表示raw_port已经有值了,不需要再从raw_server_address中解析)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]