Skip to content

Commit

Permalink
RCPP-63 Switch to using Baasaas for sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire committed Apr 3, 2024
1 parent 24dd2fd commit 635046b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
16 changes: 6 additions & 10 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ jobs:
- build-linux-sync
- build-windows-sync
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
repository: 'https://github.com/10gen/baasaas.git'
ref: 'main'
- name: Execute CLI
- name: Stop containers
run: |
chmod +x ./cli.sh
./cli.sh mine | grep '"id":' | awk -F '"' '{print $4}' | while read id; do
./cli.sh stop "$id"
export JSON_HEADER="Content-Type: application/json"
export AUTH_HEADER="apiKey: $APIKEY"
export BASE_URL="https://us-east-1.aws.data.mongodb-api.com/app/baas-container-service-autzb/endpoint"
curl --silent -X GET -H "$JSON_HEADER" -H "$AUTH_HEADER" "$BASE_URL/listContainers?mine=true" | jq | grep '"id":' | awk -F '"' '{print $4}' | while read id; do
curl --silent -X POST -H "$JSON_HEADER" -H "$AUTH_HEADER" "$BASE_URL/stopContainer?id=$id"
done
build-macos-xcode13_1:
runs-on: macos-12
name: macOS ${{ matrix.configuration }} (preset ${{ matrix.preset }}, Xcode ${{ matrix.xcode }})
Expand Down
68 changes: 64 additions & 4 deletions src/cpprealm/internal/network/network_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,57 @@ namespace realm::internal {
return "";
}

inline bool is_valid_ipv4(const std::string& ip) {
std::stringstream ss(ip);
std::string segment;
std::vector<std::string> segments;

while (std::getline(ss, segment, '.')) {
segments.push_back(segment);
}
if (segments.size() != 4) return false;

for (const std::string& seg : segments) {
if (seg.empty() || seg.size() > 3) return false;

for (char c : seg) {
if (!isdigit(c)) return false;
}

int num = std::stoi(seg);
if (num < 0 || num > 255) return false;
}

return true;
}

int extract_port(const std::string& url) {
std::regex port_regex(R"(^https?:\/\/[^\/:]+:(\d+))");
std::smatch match;

if (std::regex_search(url.begin(), url.end(), match, port_regex) && match.size() > 1) {
return std::stoi(match.str(1));
} else {
return -1;
}
}

enum class URLScheme {
HTTP,
HTTPS,
Unknown
};

URLScheme get_url_scheme(const std::string& url) {
if (url.find("https://") == 0) {
return URLScheme::HTTPS;
} else if (url.find("http://") == 0) {
return URLScheme::HTTP;
} else {
return URLScheme::Unknown;
}
}

void DefaultTransport::send_request_to_server(const app::Request& request,
std::function<void(const app::Response&)>&& completion_block) {
std::string host = host_from_url(request.url);
Expand All @@ -109,6 +160,8 @@ namespace realm::internal {
DefaultSocket socket{service};
realm::sync::network::Endpoint ep;
const bool is_localhost = (host == "localhost");
const bool is_ipv4 = is_valid_ipv4(host);
const URLScheme url_scheme = get_url_scheme(request.url);

try {
auto resolver = realm::sync::network::Resolver{service};
Expand All @@ -130,8 +183,15 @@ namespace realm::internal {
ep = *resolved.begin();
}
} else {
auto resolved = resolver.resolve(sync::network::Resolver::Query(host, is_localhost ? "9090" : "443"));
ep = *resolved.begin();
if (is_ipv4) {
std::error_code e;
auto address = realm::sync::network::make_address(host, e);
int port = extract_port(request.url);
ep = realm::sync::network::Endpoint(address, port);
} else {
auto resolved = resolver.resolve(sync::network::Resolver::Query(host, is_localhost ? "9090" : "443"));
ep = *resolved.begin();
}
}
socket.connect(ep);
} catch (...) {
Expand Down Expand Up @@ -182,7 +242,7 @@ namespace realm::internal {
m_ssl_context.use_included_certificate_roots();
#endif

if (!is_localhost) {
if (url_scheme == URLScheme::HTTPS) {
socket.ssl_stream.emplace(socket, m_ssl_context, Stream::client);
socket.ssl_stream->set_host_name(host); // Throws

Expand Down Expand Up @@ -265,7 +325,7 @@ namespace realm::internal {
return;
}
};
if (is_localhost) {
if (url_scheme != URLScheme::HTTPS) {
handler(std::error_code());
} else {
socket.async_handshake(std::move(handler)); // Throws
Expand Down

0 comments on commit 635046b

Please sign in to comment.