|
| 1 | +#include "httpserver_extension/http_handler/authentication.hpp" |
| 2 | +#include "httpserver_extension/http_handler/bindings.hpp" |
| 3 | +#include "httpserver_extension/http_handler/common.hpp" |
| 4 | +#include "httpserver_extension/http_handler/handler.hpp" |
| 5 | +#include "httpserver_extension/http_handler/playground.hpp" |
| 6 | +#include "httpserver_extension/http_handler/response_serializer.hpp" |
| 7 | +#include "httpserver_extension/state.hpp" |
| 8 | +#include "duckdb.hpp" |
| 9 | + |
| 10 | +#include <string> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#define CPPHTTPLIB_OPENSSL_SUPPORT |
| 14 | +#include "httplib.hpp" |
| 15 | + |
| 16 | +using namespace duckdb; |
| 17 | + |
| 18 | +namespace duckdb_httpserver { |
| 19 | + |
| 20 | +// Handle both GET and POST requests |
| 21 | +void HttpHandler(const duckdb_httplib_openssl::Request& req, duckdb_httplib_openssl::Response& res) { |
| 22 | + try { |
| 23 | + CheckAuthentication(req); |
| 24 | + |
| 25 | + // CORS allow |
| 26 | + res.set_header("Access-Control-Allow-Origin", "*"); |
| 27 | + res.set_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT"); |
| 28 | + res.set_header("Access-Control-Allow-Headers", "*"); |
| 29 | + res.set_header("Access-Control-Allow-Credentials", "true"); |
| 30 | + res.set_header("Access-Control-Max-Age", "86400"); |
| 31 | + |
| 32 | + // Handle preflight OPTIONS request |
| 33 | + if (req.method == "OPTIONS") { |
| 34 | + res.status = 204; // No content |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + auto query = ExtractQuery(req); |
| 39 | + auto format = ExtractFormat(req); |
| 40 | + |
| 41 | + if (query == "") { |
| 42 | + res.status = 200; |
| 43 | + res.set_content(reinterpret_cast<char const*>(playgroundContent), sizeof(playgroundContent), "text/html"); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (!global_state.db_instance) { |
| 48 | + throw IOException("Database instance not initialized"); |
| 49 | + } |
| 50 | + |
| 51 | + auto start = std::chrono::system_clock::now(); |
| 52 | + auto result = ExecuteQuery(req, query); |
| 53 | + auto end = std::chrono::system_clock::now(); |
| 54 | + auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); |
| 55 | + |
| 56 | + QueryExecStats stats{ |
| 57 | + static_cast<float>(elapsed.count()) / 1000, |
| 58 | + 0, |
| 59 | + 0 |
| 60 | + }; |
| 61 | + |
| 62 | + // Format Options |
| 63 | + if (format == "JSONEachRow") { |
| 64 | + std::string json_output = ConvertResultToNDJSON(*result); |
| 65 | + res.set_content(json_output, "application/x-ndjson"); |
| 66 | + } else if (format == "JSONCompact") { |
| 67 | + std::string json_output = ConvertResultToJSON(*result, stats); |
| 68 | + res.set_content(json_output, "application/json"); |
| 69 | + } else { |
| 70 | + // Default to NDJSON for DuckDB's own queries |
| 71 | + std::string json_output = ConvertResultToNDJSON(*result); |
| 72 | + res.set_content(json_output, "application/x-ndjson"); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + catch (const HttpHandlerException& ex) { |
| 77 | + res.status = ex.status; |
| 78 | + res.set_content(ex.message, "text/plain"); |
| 79 | + } |
| 80 | + catch (const Exception& ex) { |
| 81 | + res.status = 500; |
| 82 | + std::string error_message = "Code: 59, e.displayText() = DB::Exception: " + std::string(ex.what()); |
| 83 | + res.set_content(error_message, "text/plain"); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Execute query (optionally using a prepared statement) |
| 88 | +std::unique_ptr<MaterializedQueryResult> ExecuteQuery( |
| 89 | + const duckdb_httplib_openssl::Request& req, |
| 90 | + const std::string& query |
| 91 | +) { |
| 92 | + Connection con(*global_state.db_instance); |
| 93 | + std::unique_ptr<MaterializedQueryResult> result; |
| 94 | + |
| 95 | + if (req.has_param("parameters")) { |
| 96 | + auto prepared_stmt = con.Prepare(query); |
| 97 | + if (prepared_stmt->HasError()) { |
| 98 | + throw HttpHandlerException(500, prepared_stmt->GetError()); |
| 99 | + } |
| 100 | + |
| 101 | + auto named_values = ExtractQueryParameters(req); |
| 102 | + |
| 103 | + auto prepared_stmt_result = prepared_stmt->Execute(named_values); |
| 104 | + D_ASSERT(prepared_stmt_result->type == QueryResultType::STREAM_RESULT); |
| 105 | + result = unique_ptr_cast<QueryResult, StreamQueryResult>(std::move(prepared_stmt_result))->Materialize(); |
| 106 | + } else { |
| 107 | + result = con.Query(query); |
| 108 | + } |
| 109 | + |
| 110 | + if (result->HasError()) { |
| 111 | + throw HttpHandlerException(500, result->GetError()); |
| 112 | + } |
| 113 | + |
| 114 | + return result; |
| 115 | +} |
| 116 | + |
| 117 | +std::string ExtractQuery(const duckdb_httplib_openssl::Request& req) { |
| 118 | + // Check if the query is in the URL parameters |
| 119 | + if (req.has_param("query")) { |
| 120 | + return req.get_param_value("query"); |
| 121 | + } |
| 122 | + else if (req.has_param("q")) { |
| 123 | + return req.get_param_value("q"); |
| 124 | + } |
| 125 | + |
| 126 | + // If not in URL, and it's a POST request, check the body |
| 127 | + else if (req.method == "POST" && !req.body.empty()) { |
| 128 | + return req.body; |
| 129 | + } |
| 130 | + |
| 131 | + // std::optional is not available for this project |
| 132 | + return ""; |
| 133 | +} |
| 134 | + |
| 135 | +std::string ExtractFormat(const duckdb_httplib_openssl::Request& req) { |
| 136 | + std::string format = "JSONEachRow"; |
| 137 | + |
| 138 | + // Check for format in URL parameter or header |
| 139 | + if (req.has_param("default_format")) { |
| 140 | + format = req.get_param_value("default_format"); |
| 141 | + } else if (req.has_header("X-ClickHouse-Format")) { |
| 142 | + format = req.get_header_value("X-ClickHouse-Format"); |
| 143 | + } else if (req.has_header("format")) { |
| 144 | + format = req.get_header_value("format"); |
| 145 | + } |
| 146 | + |
| 147 | + return format; |
| 148 | +} |
| 149 | + |
| 150 | +} // namespace duckdb_httpserver |
0 commit comments