Skip to content

Commit

Permalink
Correctly initialize the curl::Session when a schema is given (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyohannes authored Dec 22, 2020
1 parent 1e7b9d8 commit 9039454
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@ class SessionManager;
class Session : public http_client::Session
{
public:
Session(SessionManager &session_manager, std::string host, uint16_t port = 80)
Session(SessionManager &session_manager, const std::string &host, uint16_t port = 80)
: session_manager_(session_manager), is_session_active_(false)
{
if (host.rfind("http://", 0) != 0 && host.rfind("https://", 0) != 0)
{
host_ = "http://" + host; // TODO - https support
}
else
{
host_ = host;
}
host_ += ":" + std::to_string(port) + "/";
}

Expand Down Expand Up @@ -169,6 +173,12 @@ class Session : public http_client::Session

void SetId(uint64_t session_id) { session_id_ = session_id; }

/**
* Returns the base URI.
* @return the base URI as a string consisting of scheme, host and port.
*/
const std::string &GetBaseUri() const { return host_; }

private:
std::shared_ptr<Request> http_request_;
std::string host_;
Expand All @@ -182,7 +192,7 @@ class SessionManager : public http_client::SessionManager
{
public:
// The call (curl_global_init) is not thread safe. Ensure this is called only once.
SessionManager() { curl_global_init(CURL_GLOBAL_ALL); }
SessionManager() : next_session_id_{0} { curl_global_init(CURL_GLOBAL_ALL); }

std::shared_ptr<http_client::Session> CreateSession(nostd::string_view host,
uint16_t port = 80) noexcept override
Expand Down
17 changes: 17 additions & 0 deletions ext/test/http/curl_http_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <chrono>
#include <condition_variable>
#include <fstream>
#include <memory>
#include <thread>
#include <vector>

Expand Down Expand Up @@ -261,3 +262,19 @@ TEST_F(BasicCurlHttpTests, CurlHttpOperations)
http_operations3.Send();
delete handler;
}

TEST_F(BasicCurlHttpTests, GetBaseUri)
{
curl::SessionManager session_manager;

auto session = session_manager.CreateSession("127.0.0.1", 80);
ASSERT_EQ(std::static_pointer_cast<curl::Session>(session)->GetBaseUri(), "http://127.0.0.1:80/");

session = session_manager.CreateSession("https://127.0.0.1", 443);
ASSERT_EQ(std::static_pointer_cast<curl::Session>(session)->GetBaseUri(),
"https://127.0.0.1:443/");

session = session_manager.CreateSession("http://127.0.0.1", 31339);
ASSERT_EQ(std::static_pointer_cast<curl::Session>(session)->GetBaseUri(),
"http://127.0.0.1:31339/");
}

0 comments on commit 9039454

Please sign in to comment.