2828#include < Poco/URI.h>
2929#include < Poco/JSON/Array.h>
3030#include < Poco/JSON/Parser.h>
31+ #include < Poco/Net/HTTPClientSession.h>
32+ #include < Poco/Net/HTTPResponse.h>
33+ #include < Poco/Net/HTTPSClientSession.h>
34+ #include < Poco/Net/SSLManager.h>
35+ #include < Poco/StreamCopier.h>
3136
3237
3338namespace DB ::ErrorCodes
@@ -203,12 +208,11 @@ std::string RestCatalog::retrieveAccessToken() const
203208 // / 1. support oauth2-server-uri
204209 // / https://github.com/apache/iceberg/blob/918f81f3c3f498f46afcea17c1ac9cdc6913cb5c/open-api/rest-catalog-open-api.yaml#L183C82-L183C99
205210
206- DB::HTTPHeaderEntries headers;
207- headers.emplace_back (" Content-Type" , " application/x-www-form-urlencoded" );
208- headers.emplace_back (" Accepts" , " application/json; charset=UTF-8" );
209-
210211 Poco::URI url;
211212 DB::ReadWriteBufferFromHTTP::OutStreamCallback out_stream_callback;
213+ size_t body_size = 0 ;
214+ String body;
215+
212216 if (oauth_server_uri.empty () && !oauth_server_use_request_body)
213217 {
214218 url = Poco::URI (base_url / oauth_tokens_endpoint);
@@ -223,11 +227,20 @@ std::string RestCatalog::retrieveAccessToken() const
223227 }
224228 else
225229 {
230+ String encoded_auth_scope;
231+ String encoded_client_id;
232+ String encoded_client_secret;
233+ Poco::URI::encode (auth_scope, auth_scope, encoded_auth_scope);
234+ Poco::URI::encode (client_id, client_id, encoded_client_id);
235+ Poco::URI::encode (client_secret, client_secret, encoded_client_secret);
236+
237+ body = fmt::format (
238+ " grant_type=client_credentials&scope={}&client_id={}&client_secret={}" ,
239+ encoded_auth_scope, encoded_client_id, encoded_client_secret);
240+ body_size = body.size ();
226241 out_stream_callback = [&](std::ostream & os)
227242 {
228- os << fmt::format (
229- " grant_type=client_credentials&scope={}&client_id={}&client_secret={}" ,
230- auth_scope, client_id, client_secret);
243+ os << body;
231244 };
232245
233246 if (oauth_server_uri.empty ())
@@ -237,19 +250,23 @@ std::string RestCatalog::retrieveAccessToken() const
237250 }
238251
239252 const auto & context = getContext ();
240- auto wb = DB::BuilderRWBufferFromHTTP (url)
241- .withConnectionGroup (DB::HTTPConnectionGroupType::HTTP)
242- .withMethod (Poco::Net::HTTPRequest::HTTP_POST)
243- .withSettings (context->getReadSettings ())
244- .withTimeouts (DB::ConnectionTimeouts::getHTTPTimeouts (context->getSettingsRef (), context->getServerSettings ()))
245- .withHostFilter (&context->getRemoteHostFilter ())
246- .withOutCallback (std::move (out_stream_callback))
247- .withSkipNotFound (false )
248- .withHeaders (headers)
249- .create (credentials);
253+ auto timeouts = DB::ConnectionTimeouts::getHTTPTimeouts (context->getSettingsRef (), context->getServerSettings ());
254+ auto session = makeHTTPSession (DB::HTTPConnectionGroupType::HTTP, url, timeouts, {});
255+
256+ Poco::Net::HTTPRequest request (Poco::Net::HTTPRequest::HTTP_POST, url.getPathAndQuery (),
257+ Poco::Net::HTTPMessage::HTTP_1_1);
258+ request.setContentType (" application/x-www-form-urlencoded" );
259+ request.setContentLength (body_size);
260+ request.set (" Accept" , " application/json" );
261+
262+ std::ostream & os = session->sendRequest (request);
263+ out_stream_callback (os);
264+
265+ Poco::Net::HTTPResponse response;
266+ std::istream & rs = session->receiveResponse (response);
250267
251268 std::string json_str;
252- readJSONObjectPossiblyInvalid (json_str, *wb );
269+ Poco::StreamCopier::copyToString (rs, json_str );
253270
254271 Poco::JSON::Parser parser;
255272 Poco::Dynamic::Var res_json = parser.parse (json_str);
0 commit comments