indigophox commented on code in PR #34817: URL: https://github.com/apache/arrow/pull/34817#discussion_r1388723301
########## cpp/src/arrow/flight/sql/server_session_middleware.cc: ########## @@ -0,0 +1,179 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <shared_mutex> +#include "arrow/flight/sql/server_session_middleware.h" +#include <boost/lexical_cast.hpp> +#include <boost/uuid/uuid.hpp> +#include <boost/uuid/uuid_generators.hpp> +#include <boost/uuid/uuid_io.hpp> + +namespace arrow { +namespace flight { +namespace sql { + +/// \brief A factory for ServerSessionMiddleware, itself storing session data. +class ServerSessionMiddlewareFactory : public ServerMiddlewareFactory { + protected: + std::map<std::string, std::shared_ptr<FlightSqlSession>> session_store_; + std::shared_mutex session_store_lock_; + boost::uuids::random_generator uuid_generator_; + + std::vector<std::pair<std::string, std::string>> ParseCookieString( + const std::string_view& s) { + const std::string list_sep = "; "; + const std::string pair_sep = "="; + const size_t pair_sep_len = pair_sep.length(); + + std::vector<std::pair<std::string, std::string>> result; + + size_t cur = 0; + while (cur < s.length()) { + const size_t end = s.find(list_sep, cur); + size_t len; + if (end == std::string::npos) { + // No (further) list delimiters + len = std::string::npos; + cur = s.length(); + } else { + len = end - cur; + cur = end; + } + const std::string_view tok = s.substr(cur, len); + + const size_t val_pos = tok.find(pair_sep); + result.emplace_back( + tok.substr(0, val_pos), + tok.substr(val_pos + pair_sep_len, std::string::npos) + ); + } + + return result; + } + + public: + Status StartCall(const CallInfo &, const CallHeaders &incoming_headers, + std::shared_ptr<ServerMiddleware> *middleware) { + std::string session_id; + + const std::pair<CallHeaders::const_iterator, CallHeaders::const_iterator>& + headers_it_pr = incoming_headers.equal_range("cookie"); + for (auto itr = headers_it_pr.first; itr != headers_it_pr.second; ++itr) { + const std::string_view& cookie_header = itr->second; + const std::vector<std::pair<std::string, std::string>> cookies = + ParseCookieString(cookie_header); + for (const std::pair<std::string, std::string>& cookie : cookies) { + if (cookie.first == kSessionCookieName) { + session_id = cookie.second; Review Comment: The pairs in the vector as well as the internals of the pair members are already (internally) mutable so we can do this. Kind of breaks the "set once const" paradigm but it's a very small block of code so not too dangerous—also reminds me that I should be using const more than once for loop/argument refs ;) Moved it to the end of the block (clearer move context & saves the wasteful construction in the error case!), too. ########## cpp/src/arrow/flight/sql/server_session_middleware.cc: ########## @@ -0,0 +1,179 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <shared_mutex> +#include "arrow/flight/sql/server_session_middleware.h" +#include <boost/lexical_cast.hpp> +#include <boost/uuid/uuid.hpp> +#include <boost/uuid/uuid_generators.hpp> +#include <boost/uuid/uuid_io.hpp> + +namespace arrow { +namespace flight { +namespace sql { + +/// \brief A factory for ServerSessionMiddleware, itself storing session data. +class ServerSessionMiddlewareFactory : public ServerMiddlewareFactory { + protected: + std::map<std::string, std::shared_ptr<FlightSqlSession>> session_store_; + std::shared_mutex session_store_lock_; + boost::uuids::random_generator uuid_generator_; + + std::vector<std::pair<std::string, std::string>> ParseCookieString( + const std::string_view& s) { + const std::string list_sep = "; "; + const std::string pair_sep = "="; + const size_t pair_sep_len = pair_sep.length(); + + std::vector<std::pair<std::string, std::string>> result; + + size_t cur = 0; + while (cur < s.length()) { + const size_t end = s.find(list_sep, cur); + size_t len; + if (end == std::string::npos) { + // No (further) list delimiters + len = std::string::npos; + cur = s.length(); + } else { + len = end - cur; + cur = end; + } + const std::string_view tok = s.substr(cur, len); + + const size_t val_pos = tok.find(pair_sep); + result.emplace_back( + tok.substr(0, val_pos), + tok.substr(val_pos + pair_sep_len, std::string::npos) + ); + } + + return result; + } + + public: + Status StartCall(const CallInfo &, const CallHeaders &incoming_headers, + std::shared_ptr<ServerMiddleware> *middleware) { + std::string session_id; + + const std::pair<CallHeaders::const_iterator, CallHeaders::const_iterator>& + headers_it_pr = incoming_headers.equal_range("cookie"); + for (auto itr = headers_it_pr.first; itr != headers_it_pr.second; ++itr) { + const std::string_view& cookie_header = itr->second; + const std::vector<std::pair<std::string, std::string>> cookies = + ParseCookieString(cookie_header); + for (const std::pair<std::string, std::string>& cookie : cookies) { + if (cookie.first == kSessionCookieName) { + session_id = cookie.second; Review Comment: The pairs in the vector as well as the internals of the pair members are already (internally) mutable so we can do this. Kind of breaks the "set once const" paradigm but it's a very small block of code so not too dangerous—also reminds me that I should be using const more than once for loop/argument refs ;) Moved it to the end of the block (clearer move context & saves the wasteful construction in the error case!), too. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
