MCPcopy Create free account
hub / github.com/Tencent/mars / SendHttpQuery

Function SendHttpQuery

mars/sdt/src/checkimpl/httpquery.cc:79–203  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

77
78
79int SendHttpQuery(const std::string& _url, int& _status_code, std::string& _errmsg, int _timeout) {
80 xinfo2(TSF"httpQuery:_url=%_", _url);
81 if (!strutil::StartsWith(_url, "http://")) {
82 xerror2(TSF"url is not start with http://");
83 _errmsg.append("url is not start with http://");
84 return -2;
85 }
86 unsigned long long timebegin = gettickcount();
87
88 if (_timeout <= 0) {
89 _timeout = HTTP_DEFAULT_TIMEOUT;
90 }
91 HttpUrlParser http_url_parser(_url);
92 std::string host = http_url_parser.Host();
93 xdebug2(TSF"host=%0", host);
94
95 std::string str_req("");
96 http::RequestLine reqLine(http::RequestLine::kGet, http_url_parser.Path(), http::kVersion_1_1);
97 str_req.append(reqLine.ToString());
98
99 http::HeaderFields header;
100 header.HeaderFiled("Accept", "text/html, application/xhtml+xml, */*");
101 header.HeaderFiled("Accept-Language", "zh-CN");
102 header.HeaderFiled("User-Agent", USER_AGENT);
103 header.HeaderFiled("Accept-Encoding", "gzip, deflate");
104 header.HeaderFiled("Proxy-Connection", "Keep-Alive");
105
106 bool domain_is_ipaddr = socket_address(host.c_str(), 0).valid(); // 判断strHost是否是一个点分十进制IP
107
108 header.HeaderFiled("Host", host.c_str());
109 str_req.append(header.ToString());
110 str_req.append("\r\n\r\n"); // important
111
112 xdebug2(TSF"str_req=%_", str_req);
113
114 unsigned int port = http_url_parser.Port();
115
116 struct socket_ipinfo_t ipinfo;
117
118 char ip[20] = {0};
119
120 int ret = 0;
121
122 do {
123 if (!domain_is_ipaddr) {
124 unsigned long long timespan1 = gettickspan(timebegin);
125
126 if ((unsigned int)_timeout <= timespan1) {
127 xwarn2(TSF"check http timeout.");
128 _errmsg.append("check http timeout.");
129 ret = -1;
130 break;
131 }
132
133 if (0 == socket_gethostbyname(host.c_str(), &ipinfo, (int)(_timeout - timespan1), NULL)) {
134 strncpy(ip, socket_address(ipinfo.ip[0]).ip(), sizeof(ip));
135 } else {
136 xerror2(TSF"check http get DNS error.");

Callers 1

__DoCheckMethod · 0.85

Calls 15

gettickcountFunction · 0.85
gettickspanFunction · 0.85
socket_gethostbynameFunction · 0.85
SplitHttpHeadAndBodyFunction · 0.85
HostMethod · 0.80
PathMethod · 0.80
HeaderFiledMethod · 0.80
c_strMethod · 0.80
ipMethod · 0.80
tcp_sendMethod · 0.80
tcp_receiveMethod · 0.80
StatusCodeMethod · 0.80

Tested by

no test coverage detected