Fix url parsing in test code

Bug: 300696974
Change-Id: Ic9a158ed0c2e7434d3a4b669a7d301999f29449e
This commit is contained in:
Fred Gylys-Colwell
2023-09-20 15:11:14 -07:00
committed by Robert Shih
parent 386ca20974
commit 1548fe5c98
2 changed files with 32 additions and 14 deletions

View File

@@ -165,12 +165,25 @@ bool HttpSocket::ParseUrl(const std::string& url, std::string* scheme,
return false;
}
// Strip off the domain name and port. In the url it will be terminated by
// either a splash or a question mark:
// like this example.com?key=value
// or this example.com/path/to/resource
if (!Tokenize(url, "/", offset, domain_name, &offset)) {
// The rest of the URL belongs to the domain name.
domain_name->assign(url, offset, std::string::npos);
// No explicit path after the domain name.
path->assign("/");
if (Tokenize(url, "?", offset, domain_name, &offset)) {
// url had no '/', but it did have '?'. Use the default path but
// keep the extra parameters. i.e. turn '?extra' into '/?extra'.
path->assign("/");
path->append(url, offset - 1, std::string::npos);
} else {
// url had no '/' or '?'.
// The rest of the URL belongs to the domain name.
domain_name->assign(url, offset, std::string::npos);
// Use the default path.
path->assign("/");
}
} else {
// url had a '/'.
// The rest of the URL, including the preceding slash, belongs to the path.
path->assign(url, offset - 1, std::string::npos);
}
@@ -192,7 +205,7 @@ bool HttpSocket::ParseUrl(const std::string& url, std::string* scheme,
}
HttpSocket::HttpSocket(const std::string& url)
: socket_fd_(-1), ssl_(nullptr), ssl_ctx_(nullptr) {
: url_(url), socket_fd_(-1), ssl_(nullptr), ssl_ctx_(nullptr) {
valid_url_ = ParseUrl(url, &scheme_, &secure_connect_, &domain_name_, &port_,
&resource_path_);
create_time_ =
@@ -280,10 +293,12 @@ bool HttpSocket::Connect(int timeout_in_ms) {
if (ret == EAI_SYSTEM) {
// EAI_SYSTEM implies an underlying system issue. Error is
// specified by |errno|.
LOGE("getaddrinfo failed due to system error: errno = %d", GetError());
LOGE("getaddrinfo %s (port %s) failed due to system error: errno = %d",
domain_name_.c_str(), port_.c_str(), GetError());
} else {
// Error is specified by return value.
LOGE("getaddrinfo failed: ret = %d", ret);
LOGE("getaddrinfo %s (port %s) failed: ret = %d", domain_name_.c_str(),
port_.c_str(), ret);
}
return false;
}
@@ -292,7 +307,8 @@ bool HttpSocket::Connect(int timeout_in_ms) {
socket_fd_ = socket(addr_info->ai_family, addr_info->ai_socktype,
addr_info->ai_protocol);
if (socket_fd_ < 0) {
LOGE("Cannot open socket: errno = %d", GetError());
LOGE("Cannot open socket %s (port %s): errno = %d", domain_name_.c_str(),
port_.c_str(), GetError());
return false;
}
@@ -300,19 +316,22 @@ bool HttpSocket::Connect(int timeout_in_ms) {
#ifdef _WIN32
u_long mode = 1; // Non-blocking mode.
if (ioctlsocket(socket_fd_, FIONBIO, &mode) != 0) {
LOGE("ioctlsocket error, wsa error = %d", WSAGetLastError());
LOGE("ioctlsocket error %s (port %s), wsa error = %d", domain_name_.c_str(),
port_.c_str(), WSAGetLastError());
CloseSocket();
return false;
}
#else
const int original_flags = fcntl(socket_fd_, F_GETFL, 0);
if (original_flags == -1) {
LOGE("fcntl error, errno = %d", errno);
LOGE("fcntl error %s (port %s), errno = %d", domain_name_.c_str(),
port_.c_str(), errno);
CloseSocket();
return false;
}
if (fcntl(socket_fd_, F_SETFL, original_flags | O_NONBLOCK) == -1) {
LOGE("fcntl error, errno = %d", errno);
LOGE("fcntl error %s (port %s), errno = %d", domain_name_.c_str(),
port_.c_str(), errno);
CloseSocket();
return false;
}

View File

@@ -32,9 +32,7 @@ class HttpSocket {
const std::string& domain_name() const { return domain_name_; }
int port() const { return atoi(port_.c_str()); }
const std::string& resource_path() const { return resource_path_; }
std::string url() const {
return scheme_ + "://" + domain_name_ + ":" + port_ + resource_path_;
}
const std::string& url() const { return url_; }
int ReadAndLogErrors(char* data, int len, int timeout_in_ms);
int WriteAndLogErrors(const char* data, int len, int timeout_in_ms);
@@ -57,6 +55,7 @@ class HttpSocket {
bool Wait(bool for_read, int timeout_in_ms);
FRIEND_TEST(HttpSocketTest, ParseUrlTest);
std::string url_;
std::string scheme_;
bool secure_connect_;
std::string domain_name_;