Skip to content

Commit b787df4

Browse files
committed
[NOT FOR MERGE] Add trivial program to debug certs issues
1 parent 788dc4e commit b787df4

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ install(
6060
EXPORT "${DUCKDB_EXPORT_SET}"
6161
LIBRARY DESTINATION "${INSTALL_LIB_DIR}"
6262
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}")
63+
64+
add_subdirectory(test-ssl)

test-ssl/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 3.5...3.31.5)
2+
3+
find_package(OpenSSL REQUIRED)
4+
5+
project("Test ssl")
6+
set(CMAKE_CXX_STANDARD 11)
7+
8+
add_definitions(-DNO_DUCKDB_RE2 -DCMAKE_BUILD_TYPE=Debug)
9+
10+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0")
11+
12+
include_directories(
13+
${PROJECT_SOURCE_DIR}/../third_party/httplib
14+
${PROJECT_SOURCE_DIR}/../duckdb/src/include)
15+
16+
add_executable("test_ssl" "test_ssl.cc")
17+
18+
target_link_libraries("test_ssl" OpenSSL::SSL OpenSSL::Crypto)
19+
20+
install(TARGETS "test_ssl")
21+
22+
# cmake -S . -G Ninja -B build && cmake --build build

test-ssl/test_ssl.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define CPPHTTPLIB_OPENSSL_SUPPORT
2+
#include "httplib.hpp"
3+
4+
using namespace duckdb_httplib_openssl;
5+
6+
int main() {
7+
Client client("https://ui.duckdb.org");
8+
auto res = client.Get("/");
9+
if (res) {
10+
std::cout << "Status: " << res->status << std::endl;
11+
std::cout << "Body: " << res->body.substr(0, 42) << "... (" << res->body.size() << ")" << std::endl;
12+
} else {
13+
std::cout << "Error: " << res.error() << std::endl;
14+
}
15+
return 0;
16+
}

third_party/httplib/httplib.hpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,78 @@ using socket_t = int;
248248
#include <thread>
249249
#include <unordered_map>
250250
#include <unordered_set>
251+
252+
#ifdef NO_DUCKDB_RE2
253+
namespace duckdb_re2 {
254+
enum class RegexOptions : uint8_t { NONE, CASE_INSENSITIVE };
255+
class Regex {
256+
public:
257+
explicit Regex(const std::string &pattern, RegexOptions options = RegexOptions::NONE): re(pattern) {}
258+
explicit Regex(const char *pattern, RegexOptions options = RegexOptions::NONE) : Regex(std::string(pattern)) {
259+
}
260+
// const duckdb_re2::RE2 &GetRegex() const {
261+
// return *regex;
262+
// }
263+
std::regex re;
264+
};
265+
struct GroupMatch {
266+
std::string text;
267+
uint32_t position;
268+
269+
const std::string &str() const { // NOLINT
270+
return text;
271+
}
272+
operator std::string() const { // NOLINT: allow implicit cast
273+
return text;
274+
}
275+
};
276+
277+
struct Match {
278+
279+
GroupMatch GetGroup(uint64_t index) {
280+
return {str(index), static_cast<uint32_t>(position(index))};
281+
}
282+
283+
std::string str(uint64_t index) { // NOLINT
284+
return m.str(index);
285+
}
286+
287+
uint64_t position(uint64_t index) { // NOLINT
288+
return m.position(index);
289+
}
290+
291+
uint64_t length(uint64_t index) { // NOLINT
292+
throw std::runtime_error("uint64_t length(uint64_t index) - NA");
293+
}
294+
295+
GroupMatch operator[](uint64_t i) {
296+
return GetGroup(i);
297+
}
298+
std::cmatch m;
299+
};
300+
301+
bool RegexSearch(const std::string &input, Match &match, const Regex &regex) {
302+
throw std::runtime_error("bool RegexSearch(const std::string &input, Match &match, const Regex &regex) - NA");
303+
}
304+
305+
bool RegexMatch(const std::string &input, Match &match, const Regex &regex) {
306+
return std::regex_match(input.c_str(), match.m, regex.re);
307+
}
308+
309+
bool RegexMatch(const char *start, const char *end, Match &match, const Regex &regex) {
310+
throw std::runtime_error("bool RegexMatch(const char *start, const char *end, Match &match, const Regex &regex) - NA");
311+
}
312+
313+
bool RegexMatch(const std::string &input, const Regex &regex) {
314+
std::cmatch m;
315+
return std::regex_match(input.c_str(), m, regex.re);
316+
}
317+
}
318+
319+
#else
251320
#include "duckdb/common/re2_regex.hpp"
252321
#include "duckdb/common/random_engine.hpp"
322+
#endif
253323

254324
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
255325
#ifdef _WIN32
@@ -4643,6 +4713,9 @@ inline std::string to_lower(const char *beg, const char *end) {
46434713
}
46444714

46454715
inline std::string make_multipart_data_boundary() {
4716+
#ifdef NO_DUCKDB_RE2
4717+
throw std::runtime_error("NA");
4718+
#else
46464719
static const char data[] =
46474720
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
46484721

@@ -4653,6 +4726,7 @@ inline std::string make_multipart_data_boundary() {
46534726
}
46544727

46554728
return result;
4729+
#endif
46564730
}
46574731

46584732
inline bool is_multipart_boundary_chars_valid(const std::string &boundary) {
@@ -5109,6 +5183,7 @@ inline bool parse_www_authenticate(const Response &res,
51095183
if (type == "Basic") {
51105184
return false;
51115185
} else if (type == "Digest") {
5186+
#ifndef NO_DUCKDB_RE2
51125187
s = s.substr(pos + 1);
51135188
auto matches = duckdb_re2::RegexFindAll(s, re);
51145189
for (auto &m : matches) {
@@ -5122,6 +5197,9 @@ inline bool parse_www_authenticate(const Response &res,
51225197
auth[key] = val;
51235198
}
51245199
return true;
5200+
#else
5201+
throw std::runtime_error("parse_www_authenticate- NA");
5202+
#endif
51255203
}
51265204
}
51275205
}
@@ -8166,6 +8244,10 @@ inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex,
81668244
}
81678245

81688246
if (ssl) {
8247+
#ifdef NO_DUCKDB_RE2
8248+
SSL_set_msg_callback(ssl, SSL_trace);
8249+
SSL_set_msg_callback_arg(ssl, BIO_new_fp(stdout, 0));
8250+
#endif
81698251
set_nonblocking(sock, true);
81708252
auto bio = BIO_new_socket(static_cast<int>(sock), BIO_NOCLOSE);
81718253
BIO_set_nbio(bio, 1);

0 commit comments

Comments
 (0)