|
| 1 | +#define DUCKDB_EXTENSION_MAIN |
| 2 | +#include "http_client_extension.hpp" |
| 3 | +#include "duckdb.hpp" |
| 4 | +#include "duckdb/function/scalar_function.hpp" |
| 5 | +#include "duckdb/main/extension_util.hpp" |
| 6 | +#include "duckdb/common/atomic.hpp" |
| 7 | +#include "duckdb/common/exception/http_exception.hpp" |
| 8 | +#include <duckdb/parser/parsed_data/create_scalar_function_info.hpp> |
| 9 | + |
| 10 | +#define CPPHTTPLIB_OPENSSL_SUPPORT |
| 11 | +#include "httplib.hpp" |
| 12 | + |
| 13 | +#include <string> |
| 14 | +#include <sstream> |
| 15 | + |
| 16 | +namespace duckdb { |
| 17 | + |
| 18 | +static void HTTPGetRequestFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
| 19 | + D_ASSERT(args.data.size() == 1); |
| 20 | + |
| 21 | + UnaryExecutor::Execute<string_t, string_t>(args.data[0], result, args.size(), [&](string_t input) { |
| 22 | + std::string url = input.GetString(); |
| 23 | + duckdb_httplib_openssl::Client client(url); |
| 24 | + |
| 25 | + auto res = client.Get("/"); |
| 26 | + if (res) { |
| 27 | + if (res->status == 200) { |
| 28 | + return StringVector::AddString(result, res->body); |
| 29 | + } else { |
| 30 | + throw std::runtime_error("HTTP error: " + std::to_string(res->status)); |
| 31 | + } |
| 32 | + } else { |
| 33 | + auto err = res.error(); |
| 34 | + throw std::runtime_error("HTTP error: " + duckdb_httplib_openssl::to_string(err)); // Fully qualify to_string |
| 35 | + } |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +static void HTTPPostRequestFunction(DataChunk &args, ExpressionState &state, Vector &result) { |
| 40 | + D_ASSERT(args.data.size() == 3); |
| 41 | + |
| 42 | + auto &url_vector = args.data[0]; |
| 43 | + auto &headers_vector = args.data[1]; |
| 44 | + auto &body_vector = args.data[2]; |
| 45 | + |
| 46 | + TernaryExecutor::Execute<string_t, string_t, string_t, string_t>( |
| 47 | + url_vector, headers_vector, body_vector, result, args.size(), |
| 48 | + [&](string_t url, string_t headers, string_t body) { |
| 49 | + std::string url_str = url.GetString(); |
| 50 | + duckdb_httplib_openssl::Client client(url_str); |
| 51 | + |
| 52 | + // HeaderMap header_map = {}; |
| 53 | + |
| 54 | + duckdb_httplib_openssl::Headers header_map; // Fully qualified httplib::Headers |
| 55 | + std::istringstream header_stream(headers.GetString()); |
| 56 | + std::string header; |
| 57 | + while (std::getline(header_stream, header)) { |
| 58 | + size_t colon_pos = header.find(':'); |
| 59 | + if (colon_pos != std::string::npos) { |
| 60 | + std::string key = header.substr(0, colon_pos); |
| 61 | + std::string value = header.substr(colon_pos + 1); |
| 62 | + // Trim leading and trailing whitespace |
| 63 | + key.erase(0, key.find_first_not_of(" \t")); |
| 64 | + key.erase(key.find_last_not_of(" \t") + 1); |
| 65 | + value.erase(0, value.find_first_not_of(" \t")); |
| 66 | + value.erase(value.find_last_not_of(" \t") + 1); |
| 67 | + header_map.emplace(key, value); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + auto res = client.Post("/", header_map, body.GetString(), "application/json"); |
| 72 | + if (res) { |
| 73 | + if (res->status == 200) { |
| 74 | + return StringVector::AddString(result, res->body); |
| 75 | + } else { |
| 76 | + throw std::runtime_error("HTTP error: " + std::to_string(res->status)); |
| 77 | + } |
| 78 | + } else { |
| 79 | + auto err = res.error(); |
| 80 | + throw std::runtime_error("HTTP error: " + duckdb_httplib_openssl::to_string(err)); // Fully qualify to_string |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +static void LoadInternal(DatabaseInstance &instance) { |
| 86 | + ScalarFunctionSet http_get("http_get"); |
| 87 | + http_get.AddFunction(ScalarFunction({LogicalType::VARCHAR}, LogicalType::VARCHAR, HTTPGetRequestFunction)); |
| 88 | + ExtensionUtil::RegisterFunction(instance, http_get); |
| 89 | + |
| 90 | + ScalarFunctionSet http_post("http_post"); |
| 91 | + http_post.AddFunction(ScalarFunction( |
| 92 | + {LogicalType::VARCHAR, LogicalType::VARCHAR, LogicalType::VARCHAR}, |
| 93 | + LogicalType::VARCHAR, HTTPPostRequestFunction)); |
| 94 | + ExtensionUtil::RegisterFunction(instance, http_post); |
| 95 | +} |
| 96 | + |
| 97 | +void HttpClientExtension::Load(DuckDB &db) { |
| 98 | + LoadInternal(*db.instance); |
| 99 | +} |
| 100 | + |
| 101 | +std::string HttpClientExtension::Name() { |
| 102 | + return "http_client"; |
| 103 | +} |
| 104 | + |
| 105 | +std::string HttpClientExtension::Version() const { |
| 106 | +#ifdef EXT_VERSION_HTTPCLIENT |
| 107 | + return EXT_VERSION_HTTPCLIENT; |
| 108 | +#else |
| 109 | + return ""; |
| 110 | +#endif |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +} // namespace duckdb |
| 115 | + |
| 116 | +extern "C" { |
| 117 | +DUCKDB_EXTENSION_API void http_client_init(duckdb::DatabaseInstance &db) { |
| 118 | + duckdb::DuckDB db_wrapper(db); |
| 119 | + db_wrapper.LoadExtension<duckdb::HttpClientExtension>(); |
| 120 | +} |
| 121 | + |
| 122 | +DUCKDB_EXTENSION_API const char *http_client_version() { |
| 123 | + return duckdb::DuckDB::LibraryVersion(); |
| 124 | +} |
| 125 | +} |
| 126 | + |
| 127 | +#ifndef DUCKDB_EXTENSION_MAIN |
| 128 | +#error DUCKDB_EXTENSION_MAIN not defined |
| 129 | +#endif |
| 130 | + |
0 commit comments