Skip to content
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

Correctly initialize the curl::Session when a schema is given #476

Merged
merged 4 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/");
}