diff --git a/analytical_engine/apps/flash/flash_utils.h b/analytical_engine/apps/flash/flash_utils.h index 9bc330cb34f0..9badccc79b83 100644 --- a/analytical_engine/apps/flash/flash_utils.h +++ b/analytical_engine/apps/flash/flash_utils.h @@ -182,32 +182,32 @@ void reduce_vec(std::vector& src, std::vector& rst, } } -#define ReduceVec3(src, rst, F) \ - reduce_vec( \ - src, rst, \ - [](void* la, void* lb, int* lens, MPI_Datatype* dtype) { \ - using T = decltype(src.data()); \ - int len; \ - memcpy(&len, la, sizeof(int)); \ - memcpy(lb, &len, sizeof(int)); \ - T src = (T)((reinterpret_cast(la)) + sizeof(int)); \ - T rst = (T)((reinterpret_cast(lb)) + sizeof(int)); \ - F; \ - }, \ +#define ReduceVec3(src, rst, F) \ + reduce_vec( \ + src, rst, \ + [](void* la, void* lb, int* lens, MPI_Datatype* dtype) { \ + using T = decltype(src.data()); \ + int len; \ + memcpy(&len, la, sizeof(int)); \ + memcpy(lb, &len, sizeof(int)); \ + T src = (T) ((reinterpret_cast(la)) + sizeof(int)); \ + T rst = (T) ((reinterpret_cast(lb)) + sizeof(int)); \ + F; \ + }, \ true); -#define ReduceVec4(src, rst, F, bcast) \ - reduce_vec( \ - src, rst, \ - [](void* la, void* lb, int* lens, MPI_Datatype* dtype) { \ - using T = decltype(src.data()); \ - int len; \ - memcpy(&len, la, sizeof(int)); \ - memcpy(lb, &len, sizeof(int)); \ - T src = (T)((reinterpret_cast(la)) + sizeof(int)); \ - T rst = (T)((reinterpret_cast(lb)) + sizeof(int)); \ - F; \ - }, \ +#define ReduceVec4(src, rst, F, bcast) \ + reduce_vec( \ + src, rst, \ + [](void* la, void* lb, int* lens, MPI_Datatype* dtype) { \ + using T = decltype(src.data()); \ + int len; \ + memcpy(&len, la, sizeof(int)); \ + memcpy(lb, &len, sizeof(int)); \ + T src = (T) ((reinterpret_cast(la)) + sizeof(int)); \ + T rst = (T) ((reinterpret_cast(lb)) + sizeof(int)); \ + F; \ + }, \ bcast); #define GetReduceVec(_1, _2, _3, _4, NAME, ...) NAME diff --git a/analytical_engine/apps/sampling_path/sampling_path.h b/analytical_engine/apps/sampling_path/sampling_path.h index 875f4b51ab80..c5d733a983b4 100644 --- a/analytical_engine/apps/sampling_path/sampling_path.h +++ b/analytical_engine/apps/sampling_path/sampling_path.h @@ -49,7 +49,7 @@ class SamplingPath auto level = pair.first; auto& path = pair.second; - if ((size_t)(level + 2) < ctx.path_pattern.size()) { + if ((size_t) (level + 2) < ctx.path_pattern.size()) { vertex_t u; auto curr_e_label = ctx.path_pattern[level + 1]; auto curr_v_label = ctx.path_pattern[level + 2]; diff --git a/analytical_engine/apps/sssp/sssp_average_length.h b/analytical_engine/apps/sssp/sssp_average_length.h index e064bc5f576b..9fb01ba57f8a 100644 --- a/analytical_engine/apps/sssp/sssp_average_length.h +++ b/analytical_engine/apps/sssp/sssp_average_length.h @@ -126,7 +126,7 @@ class SSSPAverageLength updated_map[src_vid].Insert(v); } } else { // sum msg - fid_t fid = (fid_t)(std::get<0>(msg.second)); + fid_t fid = (fid_t) (std::get<0>(msg.second)); ctx.all_sums[fid] = std::get<2>(msg.second); } msg_cnt++; @@ -185,7 +185,7 @@ class SSSPAverageLength else messages.SendToFragment( 0, pair_msg_t(false, - tuple_t((vid_t)(fid), (vid_t)(fid), ctx.inner_sum))); + tuple_t((vid_t) (fid), (vid_t) (fid), ctx.inner_sum))); } inline void syncUpdated(vid_t src_vid, const fragment_t& frag, context_t& ctx, diff --git a/flex/codegen/src/building_context.h b/flex/codegen/src/building_context.h index b4135943c86f..414b9add95e4 100644 --- a/flex/codegen/src/building_context.h +++ b/flex/codegen/src/building_context.h @@ -126,9 +126,7 @@ struct TagIndMapping { tag_id_2_tag_inds_[tag_id] != -1; } - int32_t GetMaxTagId() const { - return tag_id_2_tag_inds_.size() - 1; - } + int32_t GetMaxTagId() const { return tag_id_2_tag_inds_.size() - 1; } // convert tag_ind (us) to tag ids std::vector tag_ind_2_tag_ids_; diff --git a/flex/codegen/src/string_utils.h b/flex/codegen/src/string_utils.h index 0fd455e732fc..b4a0cfa74f12 100644 --- a/flex/codegen/src/string_utils.h +++ b/flex/codegen/src/string_utils.h @@ -82,7 +82,6 @@ std::string res_alias_to_append_opt(int res_alias, int in_alias) { } } - template std::string ensure_label_id(LabelIdT label_id) { return std::string(LABEL_ID_T_CASTER) + std::string(" ") + diff --git a/flex/engines/graph_db/runtime/common/operators/dedup.h b/flex/engines/graph_db/runtime/common/operators/dedup.h index 37810c701bfa..fbf192232bb5 100644 --- a/flex/engines/graph_db/runtime/common/operators/dedup.h +++ b/flex/engines/graph_db/runtime/common/operators/dedup.h @@ -26,7 +26,6 @@ namespace gs { namespace runtime { - class Dedup { public: static void dedup(const ReadTransaction& txn, Context& ctx, diff --git a/flex/engines/hqps_db/core/operator/sink.h b/flex/engines/hqps_db/core/operator/sink.h index 01ab8a782b6b..f2f2be7002ab 100644 --- a/flex/engines/hqps_db/core/operator/sink.h +++ b/flex/engines/hqps_db/core/operator/sink.h @@ -114,9 +114,8 @@ void template_set_value(common::Value* value, T v) { } template ) && - (!std::is_same_v)>::type* = nullptr> + typename std::enable_if<(std::is_same_v) &&( + !std::is_same_v)>::type* = nullptr> void template_set_value(common::Value* value, T v) { value->set_i64(v); } diff --git a/flex/engines/hqps_db/structures/collection.h b/flex/engines/hqps_db/structures/collection.h index 0c9addabc95e..3820d2845959 100644 --- a/flex/engines/hqps_db/structures/collection.h +++ b/flex/engines/hqps_db/structures/collection.h @@ -434,9 +434,7 @@ class CountBuilder { return true; } - Collection Build() { - return Collection(std::move(vec_)); - } + Collection Build() { return Collection(std::move(vec_)); } private: std::vector vec_; diff --git a/flex/engines/http_server/actor/admin_actor.act.h b/flex/engines/http_server/actor/admin_actor.act.h index 878988ea4304..7611b49ff638 100644 --- a/flex/engines/http_server/actor/admin_actor.act.h +++ b/flex/engines/http_server/actor/admin_actor.act.h @@ -16,8 +16,8 @@ #ifndef ENGINES_HTTP_SERVER_ACTOR_ADMIN_ACT_H_ #define ENGINES_HTTP_SERVER_ACTOR_ADMIN_ACT_H_ -#include "flex/engines/http_server/types.h" #include "flex/engines/graph_db/database/graph_db.h" +#include "flex/engines/http_server/types.h" #include "flex/storages/metadata/graph_meta_store.h" @@ -29,50 +29,90 @@ namespace server { -class ANNOTATION(actor:impl) admin_actor : public hiactor::actor { +class ANNOTATION(actor : impl) admin_actor : public hiactor::actor { public: admin_actor(hiactor::actor_base* exec_ctx, const hiactor::byte_t* addr); ~admin_actor() override; - seastar::future ANNOTATION(actor:method) run_create_graph(query_param&& param); + seastar::future ANNOTATION(actor + : method) + run_create_graph(query_param&& param); - seastar::future ANNOTATION(actor:method) run_get_graph_schema(query_param&& param); + seastar::future ANNOTATION(actor + : method) + run_get_graph_schema(query_param&& param); - seastar::future ANNOTATION(actor:method) run_get_graph_meta(query_param&& param); + seastar::future ANNOTATION(actor + : method) + run_get_graph_meta(query_param&& param); - seastar::future ANNOTATION(actor:method) run_list_graphs(query_param&& param); + seastar::future ANNOTATION(actor + : method) + run_list_graphs(query_param&& param); - seastar::future ANNOTATION(actor:method) run_delete_graph(query_param&& param); + seastar::future ANNOTATION(actor + : method) + run_delete_graph(query_param&& param); - seastar::future ANNOTATION(actor:method) run_graph_loading(graph_management_param&& param); + seastar::future ANNOTATION(actor + : method) + run_graph_loading(graph_management_param&& param); - seastar::future ANNOTATION(actor:method) start_service(query_param&& param); + seastar::future ANNOTATION(actor + : method) + start_service(query_param&& param); - seastar::future ANNOTATION(actor:method) stop_service(query_param&& param); + seastar::future ANNOTATION(actor + : method) + stop_service(query_param&& param); - seastar::future ANNOTATION(actor:method) service_status(query_param&& param); + seastar::future ANNOTATION(actor + : method) + service_status(query_param&& param); - seastar::future ANNOTATION(actor:method) get_procedure_by_procedure_name(procedure_query_param&& param); + seastar::future ANNOTATION(actor + : method) + get_procedure_by_procedure_name(procedure_query_param&& param); - seastar::future ANNOTATION(actor:method) get_procedures_by_graph_name(query_param&& param); + seastar::future ANNOTATION(actor + : method) + get_procedures_by_graph_name(query_param&& param); - seastar::future ANNOTATION(actor:method) create_procedure(create_procedure_query_param&& param); + seastar::future ANNOTATION(actor + : method) + create_procedure(create_procedure_query_param&& param); - seastar::future ANNOTATION(actor:method) delete_procedure(procedure_query_param&& param); + seastar::future ANNOTATION(actor + : method) + delete_procedure(procedure_query_param&& param); - seastar::future ANNOTATION(actor:method) update_procedure(update_procedure_query_param&& param); + seastar::future ANNOTATION(actor + : method) + update_procedure(update_procedure_query_param&& param); - seastar::future ANNOTATION(actor:method) node_status(query_param&& param); + seastar::future ANNOTATION(actor + : method) + node_status(query_param&& param); - seastar::future ANNOTATION(actor:method) get_job(query_param&& param); + seastar::future ANNOTATION(actor + : method) + get_job(query_param&& param); - seastar::future ANNOTATION(actor:method) list_jobs(query_param&& param); + seastar::future ANNOTATION(actor + : method) + list_jobs(query_param&& param); - seastar::future ANNOTATION(actor:method) cancel_job(query_param&& param); + seastar::future ANNOTATION(actor + : method) + cancel_job(query_param&& param); - seastar::future ANNOTATION(actor:method) run_get_graph_statistic(query_param&& param); + seastar::future ANNOTATION(actor + : method) + run_get_graph_statistic(query_param&& param); - seastar::future ANNOTATION(actor:method) upload_file(query_param&& param); + seastar::future ANNOTATION(actor + : method) + upload_file(query_param&& param); // DECLARE_RUN_QUERIES; /// Declare `do_work` func here, no need to implement. diff --git a/flex/engines/http_server/actor/codegen_actor.act.h b/flex/engines/http_server/actor/codegen_actor.act.h index 6141ead6b1a5..f358a954d3f1 100644 --- a/flex/engines/http_server/actor/codegen_actor.act.h +++ b/flex/engines/http_server/actor/codegen_actor.act.h @@ -23,12 +23,14 @@ namespace server { -class ANNOTATION(actor:impl) codegen_actor : public hiactor::actor { +class ANNOTATION(actor : impl) codegen_actor : public hiactor::actor { public: codegen_actor(hiactor::actor_base* exec_ctx, const hiactor::byte_t* addr); ~codegen_actor() override; - seastar::future ANNOTATION(actor:method) do_codegen(query_param&& param); + seastar::future ANNOTATION(actor + : method) + do_codegen(query_param&& param); // DECLARE_RUN_QUERIES; /// Declare `do_work` func here, no need to implement. diff --git a/flex/engines/http_server/actor/executor.act.h b/flex/engines/http_server/actor/executor.act.h index 197e4ca3423b..bdbfec26adf3 100644 --- a/flex/engines/http_server/actor/executor.act.h +++ b/flex/engines/http_server/actor/executor.act.h @@ -25,29 +25,46 @@ namespace server { -class ANNOTATION(actor:impl) executor : public hiactor::actor { +class ANNOTATION(actor : impl) executor : public hiactor::actor { public: executor(hiactor::actor_base* exec_ctx, const hiactor::byte_t* addr); ~executor() override; - seastar::future ANNOTATION(actor:method) run_graph_db_query(query_param&& param); - - seastar::future ANNOTATION(actor:method) create_vertex(graph_management_param&& param); + seastar::future ANNOTATION(actor + : method) + run_graph_db_query(query_param&& param); - seastar::future ANNOTATION(actor:method) create_edge(graph_management_param&& param); + seastar::future ANNOTATION(actor + : method) + create_vertex(graph_management_param&& param); - seastar::future ANNOTATION(actor:method) delete_vertex(graph_management_param&& param); + seastar::future ANNOTATION(actor + : method) + create_edge(graph_management_param&& param); - seastar::future ANNOTATION(actor:method) delete_edge(graph_management_param&& param); + seastar::future ANNOTATION(actor + : method) + delete_vertex(graph_management_param&& param); - seastar::future ANNOTATION(actor:method) update_vertex(graph_management_param&& param); + seastar::future ANNOTATION(actor + : method) + delete_edge(graph_management_param&& param); - seastar::future ANNOTATION(actor:method) update_edge(graph_management_param&& param); + seastar::future ANNOTATION(actor + : method) + update_vertex(graph_management_param&& param); - seastar::future ANNOTATION(actor:method) get_vertex(graph_management_query_param&& param); + seastar::future ANNOTATION(actor + : method) + update_edge(graph_management_param&& param); - seastar::future ANNOTATION(actor:method) get_edge(graph_management_query_param&& param); + seastar::future ANNOTATION(actor + : method) + get_vertex(graph_management_query_param&& param); + seastar::future ANNOTATION(actor + : method) + get_edge(graph_management_query_param&& param); // DECLARE_RUN_QUERIES; /// Declare `do_work` func here, no need to implement. diff --git a/flex/engines/http_server/executor_group.actg.h b/flex/engines/http_server/executor_group.actg.h index 17fd91ccd547..ab28be8f79e2 100644 --- a/flex/engines/http_server/executor_group.actg.h +++ b/flex/engines/http_server/executor_group.actg.h @@ -20,7 +20,9 @@ namespace server { -class ANNOTATION(actor:group) executor_group : public hiactor::schedulable_actor_group { +class ANNOTATION(actor + : group) executor_group + : public hiactor::schedulable_actor_group { public: executor_group(hiactor::actor_base* exec_ctx, const hiactor::byte_t* addr) : hiactor::schedulable_actor_group(exec_ctx, addr) {} diff --git a/flex/engines/http_server/types.h b/flex/engines/http_server/types.h index c05f8711dd0a..681cd9764360 100644 --- a/flex/engines/http_server/types.h +++ b/flex/engines/http_server/types.h @@ -60,7 +60,8 @@ using admin_query_result = payload>; using graph_management_param = payload>; using graph_management_query_param = - payload>>; + payload>>; using procedure_query_param = payload>; using create_procedure_query_param = diff --git a/flex/storages/metadata/metadata_store_factory.h b/flex/storages/metadata/metadata_store_factory.h index 181452c1c5f2..84428c0e8eeb 100644 --- a/flex/storages/metadata/metadata_store_factory.h +++ b/flex/storages/metadata/metadata_store_factory.h @@ -17,9 +17,9 @@ #define FLEX_STORAGES_METADATA_METADATA_STORE_FACTORY_H_ #include -#include "flex/storages/metadata/local_file_metadata_store.h" #include "flex/storages/metadata/default_graph_meta_store.h" #include "flex/storages/metadata/graph_meta_store.h" +#include "flex/storages/metadata/local_file_metadata_store.h" namespace gs { diff --git a/flex/third_party/httplib.h b/flex/third_party/httplib.h index 709ac4cbe5e0..4b7b0faa078c 100644 --- a/flex/third_party/httplib.h +++ b/flex/third_party/httplib.h @@ -95,9 +95,9 @@ #endif #ifndef CPPHTTPLIB_THREAD_POOL_COUNT -#define CPPHTTPLIB_THREAD_POOL_COUNT \ - ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ - ? std::thread::hardware_concurrency() - 1 \ +#define CPPHTTPLIB_THREAD_POOL_COUNT \ + ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ + ? std::thread::hardware_concurrency() - 1 \ : 0)) #endif @@ -120,11 +120,11 @@ #ifdef _WIN32 #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS -#endif //_CRT_SECURE_NO_WARNINGS +#endif //_CRT_SECURE_NO_WARNINGS #ifndef _CRT_NONSTDC_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE -#endif //_CRT_NONSTDC_NO_DEPRECATE +#endif //_CRT_NONSTDC_NO_DEPRECATE #if defined(_MSC_VER) #if _MSC_VER < 1900 @@ -138,19 +138,19 @@ using ssize_t = __int64; #else using ssize_t = long; #endif -#endif // _MSC_VER +#endif // _MSC_VER #ifndef S_ISREG -#define S_ISREG(m) (((m)&S_IFREG) == S_IFREG) -#endif // S_ISREG +#define S_ISREG(m) (((m) &S_IFREG) == S_IFREG) +#endif // S_ISREG #ifndef S_ISDIR -#define S_ISDIR(m) (((m)&S_IFDIR) == S_IFDIR) -#endif // S_ISDIR +#define S_ISDIR(m) (((m) &S_IFDIR) == S_IFDIR) +#endif // S_ISDIR #ifndef NOMINMAX #define NOMINMAX -#endif // NOMINMAX +#endif // NOMINMAX #include #include @@ -162,14 +162,14 @@ using ssize_t = long; #ifndef strcasecmp #define strcasecmp _stricmp -#endif // strcasecmp +#endif // strcasecmp using socket_t = SOCKET; #ifdef CPPHTTPLIB_USE_POLL #define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) #endif -#else // not _WIN32 +#else // not _WIN32 #include #ifndef _AIX @@ -185,19 +185,22 @@ using socket_t = SOCKET; #ifdef CPPHTTPLIB_USE_POLL #include #endif -#include #include #include #include #include #include +#include using socket_t = int; #ifndef INVALID_SOCKET #define INVALID_SOCKET (-1) #endif -#endif //_WIN32 +#endif //_WIN32 +#include +#include +#include #include #include #include @@ -206,8 +209,6 @@ using socket_t = int; #include #include #include -#include -#include #include #include #include @@ -221,7 +222,6 @@ using socket_t = int; #include #include #include -#include #include #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -239,7 +239,7 @@ using socket_t = int; #pragma comment(lib, "crypt32.lib") #pragma comment(lib, "cryptui.lib") #endif -#endif //_WIN32 +#endif //_WIN32 #include #include @@ -259,7 +259,7 @@ using socket_t = int; #if OPENSSL_VERSION_NUMBER < 0x10100000L #include -inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1) { +inline const unsigned char* ASN1_STRING_get0_data(const ASN1_STRING* asn1) { return M_ASN1_STRING_data(asn1); } #endif @@ -291,7 +291,7 @@ namespace detail { template typename std::enable_if::value, std::unique_ptr>::type -make_unique(Args &&...args) { +make_unique(Args&&... args) { return std::unique_ptr(new T(std::forward(args)...)); } @@ -303,7 +303,7 @@ make_unique(std::size_t n) { } struct ci { - bool operator()(const std::string &s1, const std::string &s2) const { + bool operator()(const std::string& s1, const std::string& s2) const { return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), [](unsigned char c1, unsigned char c2) { @@ -312,7 +312,7 @@ struct ci { } }; -} // namespace detail +} // namespace detail using Headers = std::multimap; @@ -322,7 +322,7 @@ using Match = std::smatch; using Progress = std::function; struct Response; -using ResponseHandler = std::function; +using ResponseHandler = std::function; struct MultipartFormData { std::string name; @@ -334,57 +334,57 @@ using MultipartFormDataItems = std::vector; using MultipartFormDataMap = std::multimap; class DataSink { -public: + public: DataSink() : os(&sb_), sb_(*this) {} - DataSink(const DataSink &) = delete; - DataSink &operator=(const DataSink &) = delete; - DataSink(DataSink &&) = delete; - DataSink &operator=(DataSink &&) = delete; + DataSink(const DataSink&) = delete; + DataSink& operator=(const DataSink&) = delete; + DataSink(DataSink&&) = delete; + DataSink& operator=(DataSink&&) = delete; - std::function write; + std::function write; std::function done; std::function is_writable; std::ostream os; -private: + private: class data_sink_streambuf : public std::streambuf { - public: - explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} + public: + explicit data_sink_streambuf(DataSink& sink) : sink_(sink) {} - protected: - std::streamsize xsputn(const char *s, std::streamsize n) { + protected: + std::streamsize xsputn(const char* s, std::streamsize n) { sink_.write(s, static_cast(n)); return n; } - private: - DataSink &sink_; + private: + DataSink& sink_; }; data_sink_streambuf sb_; }; using ContentProvider = - std::function; + std::function; using ContentProviderWithoutLength = - std::function; + std::function; using ContentProviderResourceReleaser = std::function; using ContentReceiverWithProgress = - std::function; using ContentReceiver = - std::function; + std::function; using MultipartContentHeader = - std::function; + std::function; class ContentReader { -public: + public: using Reader = std::function; using MultipartReader = std::function; @@ -431,24 +431,24 @@ struct Request { ContentReceiverWithProgress content_receiver; Progress progress; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - const SSL *ssl = nullptr; + const SSL* ssl = nullptr; #endif - bool has_header(const std::string &key) const; - std::string get_header_value(const std::string &key, size_t id = 0) const; + bool has_header(const std::string& key) const; + std::string get_header_value(const std::string& key, size_t id = 0) const; template - T get_header_value(const std::string &key, size_t id = 0) const; - size_t get_header_value_count(const std::string &key) const; - void set_header(const std::string &key, const std::string &val); + T get_header_value(const std::string& key, size_t id = 0) const; + size_t get_header_value_count(const std::string& key) const; + void set_header(const std::string& key, const std::string& val); - bool has_param(const std::string &key) const; - std::string get_param_value(const std::string &key, size_t id = 0) const; - size_t get_param_value_count(const std::string &key) const; + bool has_param(const std::string& key) const; + std::string get_param_value(const std::string& key, size_t id = 0) const; + size_t get_param_value_count(const std::string& key) const; bool is_multipart_form_data() const; - bool has_file(const std::string &key) const; - MultipartFormData get_file_value(const std::string &key) const; + bool has_file(const std::string& key) const; + MultipartFormData get_file_value(const std::string& key) const; // private members... size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT; @@ -464,36 +464,36 @@ struct Response { std::string reason; Headers headers; std::string body; - std::string location; // Redirect location + std::string location; // Redirect location - bool has_header(const std::string &key) const; - std::string get_header_value(const std::string &key, size_t id = 0) const; + bool has_header(const std::string& key) const; + std::string get_header_value(const std::string& key, size_t id = 0) const; template - T get_header_value(const std::string &key, size_t id = 0) const; - size_t get_header_value_count(const std::string &key) const; - void set_header(const std::string &key, const std::string &val); + T get_header_value(const std::string& key, size_t id = 0) const; + size_t get_header_value_count(const std::string& key) const; + void set_header(const std::string& key, const std::string& val); - void set_redirect(const std::string &url, int status = 302); - void set_content(const char *s, size_t n, const std::string &content_type); - void set_content(const std::string &s, const std::string &content_type); + void set_redirect(const std::string& url, int status = 302); + void set_content(const char* s, size_t n, const std::string& content_type); + void set_content(const std::string& s, const std::string& content_type); void set_content_provider( - size_t length, const std::string &content_type, ContentProvider provider, + size_t length, const std::string& content_type, ContentProvider provider, ContentProviderResourceReleaser resource_releaser = nullptr); void set_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, + const std::string& content_type, ContentProviderWithoutLength provider, ContentProviderResourceReleaser resource_releaser = nullptr); void set_chunked_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, + const std::string& content_type, ContentProviderWithoutLength provider, ContentProviderResourceReleaser resource_releaser = nullptr); Response() = default; - Response(const Response &) = default; - Response &operator=(const Response &) = default; - Response(Response &&) = default; - Response &operator=(Response &&) = default; + Response(const Response&) = default; + Response& operator=(const Response&) = default; + Response(Response&&) = default; + Response& operator=(Response&&) = default; ~Response() { if (content_provider_resource_releaser_) { content_provider_resource_releaser_(content_provider_success_); @@ -509,25 +509,25 @@ struct Response { }; class Stream { -public: + public: virtual ~Stream() = default; virtual bool is_readable() const = 0; virtual bool is_writable() const = 0; - virtual ssize_t read(char *ptr, size_t size) = 0; - virtual ssize_t write(const char *ptr, size_t size) = 0; - virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0; + virtual ssize_t read(char* ptr, size_t size) = 0; + virtual ssize_t write(const char* ptr, size_t size) = 0; + virtual void get_remote_ip_and_port(std::string& ip, int& port) const = 0; virtual socket_t socket() const = 0; template - ssize_t write_format(const char *fmt, const Args &...args); - ssize_t write(const char *ptr); - ssize_t write(const std::string &s); + ssize_t write_format(const char* fmt, const Args&... args); + ssize_t write(const char* ptr); + ssize_t write(const std::string& s); }; class TaskQueue { -public: + public: TaskQueue() = default; virtual ~TaskQueue() = default; @@ -538,7 +538,7 @@ class TaskQueue { }; class ThreadPool : public TaskQueue { -public: + public: explicit ThreadPool(size_t n) : shutdown_(false) { while (n) { threads_.emplace_back(worker(*this)); @@ -546,7 +546,7 @@ class ThreadPool : public TaskQueue { } } - ThreadPool(const ThreadPool &) = delete; + ThreadPool(const ThreadPool&) = delete; ~ThreadPool() override = default; void enqueue(std::function fn) override { @@ -565,14 +565,14 @@ class ThreadPool : public TaskQueue { cond_.notify_all(); // Join... - for (auto &t : threads_) { + for (auto& t : threads_) { t.join(); } } -private: + private: struct worker { - explicit worker(ThreadPool &pool) : pool_(pool) {} + explicit worker(ThreadPool& pool) : pool_(pool) {} void operator()() { for (;;) { @@ -583,7 +583,9 @@ class ThreadPool : public TaskQueue { pool_.cond_.wait( lock, [&] { return !pool_.jobs_.empty() || pool_.shutdown_; }); - if (pool_.shutdown_ && pool_.jobs_.empty()) { break; } + if (pool_.shutdown_ && pool_.jobs_.empty()) { + break; + } fn = pool_.jobs_.front(); pool_.jobs_.pop_front(); @@ -594,7 +596,7 @@ class ThreadPool : public TaskQueue { } } - ThreadPool &pool_; + ThreadPool& pool_; }; friend struct worker; @@ -607,31 +609,31 @@ class ThreadPool : public TaskQueue { std::mutex mutex_; }; -using Logger = std::function; +using Logger = std::function; using SocketOptions = std::function; void default_socket_options(socket_t sock); class Server { -public: - using Handler = std::function; + public: + using Handler = std::function; using ExceptionHandler = - std::function; + std::function; enum class HandlerResponse { Handled, Unhandled, }; using HandlerWithResponse = - std::function; + std::function; using HandlerWithContentReader = std::function; + const Request&, Response&, const ContentReader& content_reader)>; using Expect100ContinueHandler = - std::function; + std::function; Server(); @@ -639,73 +641,73 @@ class Server { virtual bool is_valid() const; - Server &Get(const std::string &pattern, Handler handler); - Server &Post(const std::string &pattern, Handler handler); - Server &Post(const std::string &pattern, HandlerWithContentReader handler); - Server &Put(const std::string &pattern, Handler handler); - Server &Put(const std::string &pattern, HandlerWithContentReader handler); - Server &Patch(const std::string &pattern, Handler handler); - Server &Patch(const std::string &pattern, HandlerWithContentReader handler); - Server &Delete(const std::string &pattern, Handler handler); - Server &Delete(const std::string &pattern, HandlerWithContentReader handler); - Server &Options(const std::string &pattern, Handler handler); - - bool set_base_dir(const std::string &dir, - const std::string &mount_point = std::string()); - bool set_mount_point(const std::string &mount_point, const std::string &dir, + Server& Get(const std::string& pattern, Handler handler); + Server& Post(const std::string& pattern, Handler handler); + Server& Post(const std::string& pattern, HandlerWithContentReader handler); + Server& Put(const std::string& pattern, Handler handler); + Server& Put(const std::string& pattern, HandlerWithContentReader handler); + Server& Patch(const std::string& pattern, Handler handler); + Server& Patch(const std::string& pattern, HandlerWithContentReader handler); + Server& Delete(const std::string& pattern, Handler handler); + Server& Delete(const std::string& pattern, HandlerWithContentReader handler); + Server& Options(const std::string& pattern, Handler handler); + + bool set_base_dir(const std::string& dir, + const std::string& mount_point = std::string()); + bool set_mount_point(const std::string& mount_point, const std::string& dir, Headers headers = Headers()); - bool remove_mount_point(const std::string &mount_point); - Server &set_file_extension_and_mimetype_mapping(const std::string &ext, - const std::string &mime); - Server &set_file_request_handler(Handler handler); + bool remove_mount_point(const std::string& mount_point); + Server& set_file_extension_and_mimetype_mapping(const std::string& ext, + const std::string& mime); + Server& set_file_request_handler(Handler handler); - Server &set_error_handler(HandlerWithResponse handler); - Server &set_error_handler(Handler handler); - Server &set_exception_handler(ExceptionHandler handler); - Server &set_pre_routing_handler(HandlerWithResponse handler); - Server &set_post_routing_handler(Handler handler); + Server& set_error_handler(HandlerWithResponse handler); + Server& set_error_handler(Handler handler); + Server& set_exception_handler(ExceptionHandler handler); + Server& set_pre_routing_handler(HandlerWithResponse handler); + Server& set_post_routing_handler(Handler handler); - Server &set_expect_100_continue_handler(Expect100ContinueHandler handler); - Server &set_logger(Logger logger); + Server& set_expect_100_continue_handler(Expect100ContinueHandler handler); + Server& set_logger(Logger logger); - Server &set_address_family(int family); - Server &set_tcp_nodelay(bool on); - Server &set_socket_options(SocketOptions socket_options); + Server& set_address_family(int family); + Server& set_tcp_nodelay(bool on); + Server& set_socket_options(SocketOptions socket_options); - Server &set_default_headers(Headers headers); + Server& set_default_headers(Headers headers); - Server &set_keep_alive_max_count(size_t count); - Server &set_keep_alive_timeout(time_t sec); + Server& set_keep_alive_max_count(size_t count); + Server& set_keep_alive_timeout(time_t sec); - Server &set_read_timeout(time_t sec, time_t usec = 0); + Server& set_read_timeout(time_t sec, time_t usec = 0); template - Server &set_read_timeout(const std::chrono::duration &duration); + Server& set_read_timeout(const std::chrono::duration& duration); - Server &set_write_timeout(time_t sec, time_t usec = 0); + Server& set_write_timeout(time_t sec, time_t usec = 0); template - Server &set_write_timeout(const std::chrono::duration &duration); + Server& set_write_timeout(const std::chrono::duration& duration); - Server &set_idle_interval(time_t sec, time_t usec = 0); + Server& set_idle_interval(time_t sec, time_t usec = 0); template - Server &set_idle_interval(const std::chrono::duration &duration); + Server& set_idle_interval(const std::chrono::duration& duration); - Server &set_payload_max_length(size_t length); + Server& set_payload_max_length(size_t length); - bool bind_to_port(const std::string &host, int port, int socket_flags = 0); - int bind_to_any_port(const std::string &host, int socket_flags = 0); + bool bind_to_port(const std::string& host, int port, int socket_flags = 0); + int bind_to_any_port(const std::string& host, int socket_flags = 0); bool listen_after_bind(); - bool listen(const std::string &host, int port, int socket_flags = 0); + bool listen(const std::string& host, int port, int socket_flags = 0); bool is_running() const; void stop(); - std::function new_task_queue; + std::function new_task_queue; -protected: - bool process_request(Stream &strm, bool close_connection, - bool &connection_closed, - const std::function &setup_request); + protected: + bool process_request(Stream& strm, bool close_connection, + bool& connection_closed, + const std::function& setup_request); std::atomic svr_sock_; size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; @@ -718,46 +720,44 @@ class Server { time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; -private: + private: using Handlers = std::vector>; using HandlersForContentReader = std::vector>; - socket_t create_server_socket(const std::string &host, int port, + socket_t create_server_socket(const std::string& host, int port, int socket_flags, SocketOptions socket_options) const; - int bind_internal(const std::string &host, int port, int socket_flags); + int bind_internal(const std::string& host, int port, int socket_flags); bool listen_internal(); - bool routing(Request &req, Response &res, Stream &strm); - bool handle_file_request(const Request &req, Response &res, + bool routing(Request& req, Response& res, Stream& strm); + bool handle_file_request(const Request& req, Response& res, bool head = false); - bool dispatch_request(Request &req, Response &res, const Handlers &handlers); - bool - dispatch_request_for_content_reader(Request &req, Response &res, - ContentReader content_reader, - const HandlersForContentReader &handlers); - - bool parse_request_line(const char *s, Request &req); - void apply_ranges(const Request &req, Response &res, - std::string &content_type, std::string &boundary); - bool write_response(Stream &strm, bool close_connection, const Request &req, - Response &res); - bool write_response_with_content(Stream &strm, bool close_connection, - const Request &req, Response &res); - bool write_response_core(Stream &strm, bool close_connection, - const Request &req, Response &res, + bool dispatch_request(Request& req, Response& res, const Handlers& handlers); + bool dispatch_request_for_content_reader( + Request& req, Response& res, ContentReader content_reader, + const HandlersForContentReader& handlers); + + bool parse_request_line(const char* s, Request& req); + void apply_ranges(const Request& req, Response& res, + std::string& content_type, std::string& boundary); + bool write_response(Stream& strm, bool close_connection, const Request& req, + Response& res); + bool write_response_with_content(Stream& strm, bool close_connection, + const Request& req, Response& res); + bool write_response_core(Stream& strm, bool close_connection, + const Request& req, Response& res, bool need_apply_ranges); - bool write_content_with_provider(Stream &strm, const Request &req, - Response &res, const std::string &boundary, - const std::string &content_type); - bool read_content(Stream &strm, Request &req, Response &res); - bool - read_content_with_content_receiver(Stream &strm, Request &req, Response &res, - ContentReceiver receiver, - MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver); - bool read_content_core(Stream &strm, Request &req, Response &res, + bool write_content_with_provider(Stream& strm, const Request& req, + Response& res, const std::string& boundary, + const std::string& content_type); + bool read_content(Stream& strm, Request& req, Response& res); + bool read_content_with_content_receiver( + Stream& strm, Request& req, Response& res, ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver); + bool read_content_core(Stream& strm, Request& req, Response& res, ContentReceiver receiver, MultipartContentHeader mulitpart_header, ContentReceiver multipart_receiver); @@ -817,190 +817,191 @@ enum class Error { std::string to_string(const Error error); -std::ostream &operator<<(std::ostream &os, const Error &obj); +std::ostream& operator<<(std::ostream& os, const Error& obj); class Result { -public: - Result(std::unique_ptr &&res, Error err, - Headers &&request_headers = Headers{}) - : res_(std::move(res)), err_(err), + public: + Result(std::unique_ptr&& res, Error err, + Headers&& request_headers = Headers{}) + : res_(std::move(res)), + err_(err), request_headers_(std::move(request_headers)) {} // Response operator bool() const { return res_ != nullptr; } bool operator==(std::nullptr_t) const { return res_ == nullptr; } bool operator!=(std::nullptr_t) const { return res_ != nullptr; } - const Response &value() const { return *res_; } - Response &value() { return *res_; } - const Response &operator*() const { return *res_; } - Response &operator*() { return *res_; } - const Response *operator->() const { return res_.get(); } - Response *operator->() { return res_.get(); } + const Response& value() const { return *res_; } + Response& value() { return *res_; } + const Response& operator*() const { return *res_; } + Response& operator*() { return *res_; } + const Response* operator->() const { return res_.get(); } + Response* operator->() { return res_.get(); } // Error Error error() const { return err_; } // Request Headers - bool has_request_header(const std::string &key) const; - std::string get_request_header_value(const std::string &key, + bool has_request_header(const std::string& key) const; + std::string get_request_header_value(const std::string& key, size_t id = 0) const; template - T get_request_header_value(const std::string &key, size_t id = 0) const; - size_t get_request_header_value_count(const std::string &key) const; + T get_request_header_value(const std::string& key, size_t id = 0) const; + size_t get_request_header_value_count(const std::string& key) const; -private: + private: std::unique_ptr res_; Error err_; Headers request_headers_; }; class ClientImpl { -public: - explicit ClientImpl(const std::string &host); + public: + explicit ClientImpl(const std::string& host); - explicit ClientImpl(const std::string &host, int port); + explicit ClientImpl(const std::string& host, int port); - explicit ClientImpl(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path); + explicit ClientImpl(const std::string& host, int port, + const std::string& client_cert_path, + const std::string& client_key_path); virtual ~ClientImpl(); virtual bool is_valid() const; - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path); + Result Get(const std::string& path, const Headers& headers); + Result Get(const std::string& path, Progress progress); + Result Get(const std::string& path, const Headers& headers, Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path, ContentReceiver content_receiver); + Result Get(const std::string& path, const Headers& headers, ContentReceiver content_receiver); - Result Get(const std::string &path, ContentReceiver content_receiver, + Result Get(const std::string& path, ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path, const Headers& headers, ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, + Result Get(const std::string& path, ResponseHandler response_handler, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path, const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver); - Result Get(const std::string &path, ResponseHandler response_handler, + Result Get(const std::string& path, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path, const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ContentReceiver content_receiver, + Result Get(const std::string& path, const Params& params, + const Headers& headers, Progress progress = nullptr); + Result Get(const std::string& path, const Params& params, + const Headers& headers, ContentReceiver content_receiver, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ResponseHandler response_handler, + Result Get(const std::string& path, const Params& params, + const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress = nullptr); - Result Head(const std::string &path); - Result Head(const std::string &path, const Headers &headers); - - Result Post(const std::string &path); - Result Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Post(const std::string &path, size_t content_length, + Result Head(const std::string& path); + Result Head(const std::string& path, const Headers& headers); + + Result Post(const std::string& path); + Result Post(const std::string& path, const char* body, size_t content_length, + const std::string& content_type); + Result Post(const std::string& path, const Headers& headers, const char* body, + size_t content_length, const std::string& content_type); + Result Post(const std::string& path, const std::string& body, + const std::string& content_type); + Result Post(const std::string& path, const Headers& headers, + const std::string& body, const std::string& content_type); + Result Post(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, + const std::string& content_type); + Result Post(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Post(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Post(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - - Result Put(const std::string &path); - Result Put(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Put(const std::string &path, size_t content_length, - ContentProvider content_provider, const std::string &content_type); - Result Put(const std::string &path, + const std::string& content_type); + Result Post(const std::string& path, const Params& params); + Result Post(const std::string& path, const Headers& headers, + const Params& params); + Result Post(const std::string& path, const MultipartFormDataItems& items); + Result Post(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items); + Result Post(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items, const std::string& boundary); + + Result Put(const std::string& path); + Result Put(const std::string& path, const char* body, size_t content_length, + const std::string& content_type); + Result Put(const std::string& path, const Headers& headers, const char* body, + size_t content_length, const std::string& content_type); + Result Put(const std::string& path, const std::string& body, + const std::string& content_type); + Result Put(const std::string& path, const Headers& headers, + const std::string& body, const std::string& content_type); + Result Put(const std::string& path, size_t content_length, + ContentProvider content_provider, const std::string& content_type); + Result Put(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Put(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Put(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - - Result Patch(const std::string &path); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Patch(const std::string &path, size_t content_length, + const std::string& content_type); + Result Put(const std::string& path, const Params& params); + Result Put(const std::string& path, const Headers& headers, + const Params& params); + Result Put(const std::string& path, const MultipartFormDataItems& items); + Result Put(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items); + Result Put(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items, const std::string& boundary); + + Result Patch(const std::string& path); + Result Patch(const std::string& path, const char* body, size_t content_length, + const std::string& content_type); + Result Patch(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type); + Result Patch(const std::string& path, const std::string& body, + const std::string& content_type); + Result Patch(const std::string& path, const Headers& headers, + const std::string& body, const std::string& content_type); + Result Patch(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, + const std::string& content_type); + Result Patch(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Patch(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Patch(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type); - - Result Delete(const std::string &path); - Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - - Result Options(const std::string &path); - Result Options(const std::string &path, const Headers &headers); - - bool send(Request &req, Response &res, Error &error); - Result send(const Request &req); + const std::string& content_type); + + Result Delete(const std::string& path); + Result Delete(const std::string& path, const Headers& headers); + Result Delete(const std::string& path, const char* body, + size_t content_length, const std::string& content_type); + Result Delete(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type); + Result Delete(const std::string& path, const std::string& body, + const std::string& content_type); + Result Delete(const std::string& path, const Headers& headers, + const std::string& body, const std::string& content_type); + + Result Options(const std::string& path); + Result Options(const std::string& path, const Headers& headers); + + bool send(Request& req, Response& res, Error& error); + Result send(const Request& req); size_t is_socket_open() const; @@ -1018,22 +1019,22 @@ class ClientImpl { void set_connection_timeout(time_t sec, time_t usec = 0); template - void - set_connection_timeout(const std::chrono::duration &duration); + void set_connection_timeout( + const std::chrono::duration& duration); void set_read_timeout(time_t sec, time_t usec = 0); template - void set_read_timeout(const std::chrono::duration &duration); + void set_read_timeout(const std::chrono::duration& duration); void set_write_timeout(time_t sec, time_t usec = 0); template - void set_write_timeout(const std::chrono::duration &duration); + void set_write_timeout(const std::chrono::duration& duration); - void set_basic_auth(const std::string &username, const std::string &password); - void set_bearer_token_auth(const std::string &token); + void set_basic_auth(const std::string& username, const std::string& password); + void set_bearer_token_auth(const std::string& token); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_digest_auth(const std::string &username, - const std::string &password); + void set_digest_auth(const std::string& username, + const std::string& password); #endif void set_keep_alive(bool on); @@ -1045,21 +1046,21 @@ class ClientImpl { void set_decompress(bool on); - void set_interface(const std::string &intf); + void set_interface(const std::string& intf); - void set_proxy(const std::string &host, int port); - void set_proxy_basic_auth(const std::string &username, - const std::string &password); - void set_proxy_bearer_token_auth(const std::string &token); + void set_proxy(const std::string& host, int port); + void set_proxy_basic_auth(const std::string& username, + const std::string& password); + void set_proxy_bearer_token_auth(const std::string& token); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_proxy_digest_auth(const std::string &username, - const std::string &password); + void set_proxy_digest_auth(const std::string& username, + const std::string& password); #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path = std::string()); - void set_ca_cert_store(X509_STORE *ca_cert_store); + void set_ca_cert_path(const std::string& ca_cert_file_path, + const std::string& ca_cert_dir_path = std::string()); + void set_ca_cert_store(X509_STORE* ca_cert_store); #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1068,19 +1069,19 @@ class ClientImpl { void set_logger(Logger logger); -protected: + protected: struct Socket { socket_t sock = INVALID_SOCKET; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSL *ssl = nullptr; + SSL* ssl = nullptr; #endif bool is_open() const { return sock != INVALID_SOCKET; } }; - Result send_(Request &&req); + Result send_(Request&& req); - virtual bool create_and_connect_socket(Socket &socket, Error &error); + virtual bool create_and_connect_socket(Socket& socket, Error& error); // All of: // shutdown_ssl @@ -1089,17 +1090,17 @@ class ClientImpl { // should ONLY be called when socket_mutex_ is locked. // Also, shutdown_ssl and close_socket should also NOT be called concurrently // with a DIFFERENT thread sending requests using that socket. - virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully); - void shutdown_socket(Socket &socket); - void close_socket(Socket &socket); + virtual void shutdown_ssl(Socket& socket, bool shutdown_gracefully); + void shutdown_socket(Socket& socket); + void close_socket(Socket& socket); - bool process_request(Stream &strm, Request &req, Response &res, - bool close_connection, Error &error); + bool process_request(Stream& strm, Request& req, Response& res, + bool close_connection, Error& error); - bool write_content_with_provider(Stream &strm, const Request &req, - Error &error); + bool write_content_with_provider(Stream& strm, const Request& req, + Error& error); - void copy_settings(const ClientImpl &rhs); + void copy_settings(const ClientImpl& rhs); // Socket endpoint information const std::string host_; @@ -1170,7 +1171,7 @@ class ClientImpl { std::string ca_cert_file_path_; std::string ca_cert_dir_path_; - X509_STORE *ca_cert_store_ = nullptr; + X509_STORE* ca_cert_store_ = nullptr; #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1179,187 +1180,187 @@ class ClientImpl { Logger logger_; -private: - socket_t create_client_socket(Error &error) const; - bool read_response_line(Stream &strm, const Request &req, Response &res); - bool write_request(Stream &strm, Request &req, bool close_connection, - Error &error); - bool redirect(Request &req, Response &res, Error &error); - bool handle_request(Stream &strm, Request &req, Response &res, - bool close_connection, Error &error); + private: + socket_t create_client_socket(Error& error) const; + bool read_response_line(Stream& strm, const Request& req, Response& res); + bool write_request(Stream& strm, Request& req, bool close_connection, + Error& error); + bool redirect(Request& req, Response& res, Error& error); + bool handle_request(Stream& strm, Request& req, Response& res, + bool close_connection, Error& error); std::unique_ptr send_with_content_provider( - Request &req, const char *body, size_t content_length, + Request& req, const char* body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Error &error); + const std::string& content_type, Error& error); Result send_with_content_provider( - const std::string &method, const std::string &path, - const Headers &headers, const char *body, size_t content_length, + const std::string& method, const std::string& path, + const Headers& headers, const char* body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type); + const std::string& content_type); - std::string adjust_host_string(const std::string &host) const; + std::string adjust_host_string(const std::string& host) const; - virtual bool process_socket(const Socket &socket, - std::function callback); + virtual bool process_socket(const Socket& socket, + std::function callback); virtual bool is_ssl() const; }; class Client { -public: + public: // Universal interface - explicit Client(const std::string &scheme_host_port); + explicit Client(const std::string& scheme_host_port); - explicit Client(const std::string &scheme_host_port, - const std::string &client_cert_path, - const std::string &client_key_path); + explicit Client(const std::string& scheme_host_port, + const std::string& client_cert_path, + const std::string& client_key_path); // HTTP only interface - explicit Client(const std::string &host, int port); + explicit Client(const std::string& host, int port); - explicit Client(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path); + explicit Client(const std::string& host, int port, + const std::string& client_cert_path, + const std::string& client_key_path); - Client(Client &&) = default; + Client(Client&&) = default; ~Client(); bool is_valid() const; - Result Get(const std::string &path); - Result Get(const std::string &path, const Headers &headers); - Result Get(const std::string &path, Progress progress); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path); + Result Get(const std::string& path, const Headers& headers); + Result Get(const std::string& path, Progress progress); + Result Get(const std::string& path, const Headers& headers, Progress progress); - Result Get(const std::string &path, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path, ContentReceiver content_receiver); + Result Get(const std::string& path, const Headers& headers, ContentReceiver content_receiver); - Result Get(const std::string &path, ContentReceiver content_receiver, + Result Get(const std::string& path, ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path, const Headers& headers, ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, + Result Get(const std::string& path, ResponseHandler response_handler, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path, const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver); - Result Get(const std::string &path, const Headers &headers, + Result Get(const std::string& path, const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, ResponseHandler response_handler, + Result Get(const std::string& path, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ContentReceiver content_receiver, + Result Get(const std::string& path, const Params& params, + const Headers& headers, Progress progress = nullptr); + Result Get(const std::string& path, const Params& params, + const Headers& headers, ContentReceiver content_receiver, Progress progress = nullptr); - Result Get(const std::string &path, const Params ¶ms, - const Headers &headers, ResponseHandler response_handler, + Result Get(const std::string& path, const Params& params, + const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress = nullptr); - Result Head(const std::string &path); - Result Head(const std::string &path, const Headers &headers); - - Result Post(const std::string &path); - Result Post(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Post(const std::string &path, const std::string &body, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Post(const std::string &path, size_t content_length, + Result Head(const std::string& path); + Result Head(const std::string& path, const Headers& headers); + + Result Post(const std::string& path); + Result Post(const std::string& path, const char* body, size_t content_length, + const std::string& content_type); + Result Post(const std::string& path, const Headers& headers, const char* body, + size_t content_length, const std::string& content_type); + Result Post(const std::string& path, const std::string& body, + const std::string& content_type); + Result Post(const std::string& path, const Headers& headers, + const std::string& body, const std::string& content_type); + Result Post(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, + const std::string& content_type); + Result Post(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Post(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Post(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Post(const std::string &path, const Params ¶ms); - Result Post(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Post(const std::string &path, const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Put(const std::string &path); - Result Put(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, const char *body, - size_t content_length, const std::string &content_type); - Result Put(const std::string &path, const std::string &body, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Put(const std::string &path, size_t content_length, - ContentProvider content_provider, const std::string &content_type); - Result Put(const std::string &path, + const std::string& content_type); + Result Post(const std::string& path, const Params& params); + Result Post(const std::string& path, const Headers& headers, + const Params& params); + Result Post(const std::string& path, const MultipartFormDataItems& items); + Result Post(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items); + Result Post(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items, const std::string& boundary); + Result Put(const std::string& path); + Result Put(const std::string& path, const char* body, size_t content_length, + const std::string& content_type); + Result Put(const std::string& path, const Headers& headers, const char* body, + size_t content_length, const std::string& content_type); + Result Put(const std::string& path, const std::string& body, + const std::string& content_type); + Result Put(const std::string& path, const Headers& headers, + const std::string& body, const std::string& content_type); + Result Put(const std::string& path, size_t content_length, + ContentProvider content_provider, const std::string& content_type); + Result Put(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Put(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Put(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Put(const std::string &path, const Params ¶ms); - Result Put(const std::string &path, const Headers &headers, - const Params ¶ms); - Result Put(const std::string &path, const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items); - Result Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, const std::string &boundary); - Result Patch(const std::string &path); - Result Patch(const std::string &path, const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Patch(const std::string &path, const std::string &body, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - Result Patch(const std::string &path, size_t content_length, + const std::string& content_type); + Result Put(const std::string& path, const Params& params); + Result Put(const std::string& path, const Headers& headers, + const Params& params); + Result Put(const std::string& path, const MultipartFormDataItems& items); + Result Put(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items); + Result Put(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items, const std::string& boundary); + Result Patch(const std::string& path); + Result Patch(const std::string& path, const char* body, size_t content_length, + const std::string& content_type); + Result Patch(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type); + Result Patch(const std::string& path, const std::string& body, + const std::string& content_type); + Result Patch(const std::string& path, const Headers& headers, + const std::string& body, const std::string& content_type); + Result Patch(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, + const std::string& content_type); + Result Patch(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Patch(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type); - Result Patch(const std::string &path, const Headers &headers, + const std::string& content_type); + Result Patch(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type); - - Result Delete(const std::string &path); - Result Delete(const std::string &path, const Headers &headers); - Result Delete(const std::string &path, const char *body, - size_t content_length, const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type); - Result Delete(const std::string &path, const std::string &body, - const std::string &content_type); - Result Delete(const std::string &path, const Headers &headers, - const std::string &body, const std::string &content_type); - - Result Options(const std::string &path); - Result Options(const std::string &path, const Headers &headers); - - bool send(Request &req, Response &res, Error &error); - Result send(const Request &req); + const std::string& content_type); + + Result Delete(const std::string& path); + Result Delete(const std::string& path, const Headers& headers); + Result Delete(const std::string& path, const char* body, + size_t content_length, const std::string& content_type); + Result Delete(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type); + Result Delete(const std::string& path, const std::string& body, + const std::string& content_type); + Result Delete(const std::string& path, const Headers& headers, + const std::string& body, const std::string& content_type); + + Result Options(const std::string& path); + Result Options(const std::string& path, const Headers& headers); + + bool send(Request& req, Response& res, Error& error); + Result send(const Request& req); size_t is_socket_open() const; @@ -1377,22 +1378,22 @@ class Client { void set_connection_timeout(time_t sec, time_t usec = 0); template - void - set_connection_timeout(const std::chrono::duration &duration); + void set_connection_timeout( + const std::chrono::duration& duration); void set_read_timeout(time_t sec, time_t usec = 0); template - void set_read_timeout(const std::chrono::duration &duration); + void set_read_timeout(const std::chrono::duration& duration); void set_write_timeout(time_t sec, time_t usec = 0); template - void set_write_timeout(const std::chrono::duration &duration); + void set_write_timeout(const std::chrono::duration& duration); - void set_basic_auth(const std::string &username, const std::string &password); - void set_bearer_token_auth(const std::string &token); + void set_basic_auth(const std::string& username, const std::string& password); + void set_bearer_token_auth(const std::string& token); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_digest_auth(const std::string &username, - const std::string &password); + void set_digest_auth(const std::string& username, + const std::string& password); #endif void set_keep_alive(bool on); @@ -1404,15 +1405,15 @@ class Client { void set_decompress(bool on); - void set_interface(const std::string &intf); + void set_interface(const std::string& intf); - void set_proxy(const std::string &host, int port); - void set_proxy_basic_auth(const std::string &username, - const std::string &password); - void set_proxy_bearer_token_auth(const std::string &token); + void set_proxy(const std::string& host, int port); + void set_proxy_basic_auth(const std::string& username, + const std::string& password); + void set_proxy_bearer_token_auth(const std::string& token); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_proxy_digest_auth(const std::string &username, - const std::string &password); + void set_proxy_digest_auth(const std::string& username, + const std::string& password); #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1423,17 +1424,17 @@ class Client { // SSL #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - void set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path = std::string()); + void set_ca_cert_path(const std::string& ca_cert_file_path, + const std::string& ca_cert_dir_path = std::string()); - void set_ca_cert_store(X509_STORE *ca_cert_store); + void set_ca_cert_store(X509_STORE* ca_cert_store); long get_openssl_verify_result() const; - SSL_CTX *ssl_context() const; + SSL_CTX* ssl_context() const; #endif -private: + private: std::unique_ptr cli_; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1443,75 +1444,75 @@ class Client { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT class SSLServer : public Server { -public: - SSLServer(const char *cert_path, const char *private_key_path, - const char *client_ca_cert_file_path = nullptr, - const char *client_ca_cert_dir_path = nullptr, - const char *private_key_password = nullptr); + public: + SSLServer(const char* cert_path, const char* private_key_path, + const char* client_ca_cert_file_path = nullptr, + const char* client_ca_cert_dir_path = nullptr, + const char* private_key_password = nullptr); - SSLServer(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store = nullptr); + SSLServer(X509* cert, EVP_PKEY* private_key, + X509_STORE* client_ca_cert_store = nullptr); SSLServer( - const std::function &setup_ssl_ctx_callback); + const std::function& setup_ssl_ctx_callback); ~SSLServer() override; bool is_valid() const override; - SSL_CTX *ssl_context() const; + SSL_CTX* ssl_context() const; -private: + private: bool process_and_close_socket(socket_t sock) override; - SSL_CTX *ctx_; + SSL_CTX* ctx_; std::mutex ctx_mutex_; }; class SSLClient : public ClientImpl { -public: - explicit SSLClient(const std::string &host); + public: + explicit SSLClient(const std::string& host); - explicit SSLClient(const std::string &host, int port); + explicit SSLClient(const std::string& host, int port); - explicit SSLClient(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path); + explicit SSLClient(const std::string& host, int port, + const std::string& client_cert_path, + const std::string& client_key_path); - explicit SSLClient(const std::string &host, int port, X509 *client_cert, - EVP_PKEY *client_key); + explicit SSLClient(const std::string& host, int port, X509* client_cert, + EVP_PKEY* client_key); ~SSLClient() override; bool is_valid() const override; - void set_ca_cert_store(X509_STORE *ca_cert_store); + void set_ca_cert_store(X509_STORE* ca_cert_store); long get_openssl_verify_result() const; - SSL_CTX *ssl_context() const; + SSL_CTX* ssl_context() const; -private: - bool create_and_connect_socket(Socket &socket, Error &error) override; - void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override; - void shutdown_ssl_impl(Socket &socket, bool shutdown_socket); + private: + bool create_and_connect_socket(Socket& socket, Error& error) override; + void shutdown_ssl(Socket& socket, bool shutdown_gracefully) override; + void shutdown_ssl_impl(Socket& socket, bool shutdown_socket); - bool process_socket(const Socket &socket, - std::function callback) override; + bool process_socket(const Socket& socket, + std::function callback) override; bool is_ssl() const override; - bool connect_with_proxy(Socket &sock, Response &res, bool &success, - Error &error); - bool initialize_ssl(Socket &socket, Error &error); + bool connect_with_proxy(Socket& sock, Response& res, bool& success, + Error& error); + bool initialize_ssl(Socket& socket, Error& error); bool load_certs(); - bool verify_host(X509 *server_cert) const; - bool verify_host_with_subject_alt_name(X509 *server_cert) const; - bool verify_host_with_common_name(X509 *server_cert) const; - bool check_host_name(const char *pattern, size_t pattern_len) const; + bool verify_host(X509* server_cert) const; + bool verify_host_with_subject_alt_name(X509* server_cert) const; + bool verify_host_with_common_name(X509* server_cert) const; + bool check_host_name(const char* pattern, size_t pattern_len) const; - SSL_CTX *ctx_; + SSL_CTX* ctx_; std::mutex ctx_mutex_; std::once_flag initialize_cert_; @@ -1530,7 +1531,7 @@ class SSLClient : public ClientImpl { namespace detail { template -inline void duration_to_sec_and_usec(const T &duration, U callback) { +inline void duration_to_sec_and_usec(const T& duration, U callback) { auto sec = std::chrono::duration_cast(duration).count(); auto usec = std::chrono::duration_cast( duration - std::chrono::seconds(sec)) @@ -1539,13 +1540,13 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) { } template -inline T get_header_value(const Headers & /*headers*/, - const std::string & /*key*/, size_t /*id*/ = 0, +inline T get_header_value(const Headers& /*headers*/, + const std::string& /*key*/, size_t /*id*/ = 0, uint64_t /*def*/ = 0) {} template <> -inline uint64_t get_header_value(const Headers &headers, - const std::string &key, size_t id, +inline uint64_t get_header_value(const Headers& headers, + const std::string& key, size_t id, uint64_t def) { auto rng = headers.equal_range(key); auto it = rng.first; @@ -1556,25 +1557,27 @@ inline uint64_t get_header_value(const Headers &headers, return def; } -} // namespace detail +} // namespace detail template -inline T Request::get_header_value(const std::string &key, size_t id) const { +inline T Request::get_header_value(const std::string& key, size_t id) const { return detail::get_header_value(headers, key, id, 0); } template -inline T Response::get_header_value(const std::string &key, size_t id) const { +inline T Response::get_header_value(const std::string& key, size_t id) const { return detail::get_header_value(headers, key, id, 0); } template -inline ssize_t Stream::write_format(const char *fmt, const Args &...args) { +inline ssize_t Stream::write_format(const char* fmt, const Args&... args) { const auto bufsiz = 2048; std::array buf{}; auto sn = snprintf(buf.data(), buf.size() - 1, fmt, args...); - if (sn <= 0) { return sn; } + if (sn <= 0) { + return sn; + } auto n = static_cast(sn); @@ -1595,40 +1598,40 @@ inline ssize_t Stream::write_format(const char *fmt, const Args &...args) { inline void default_socket_options(socket_t sock) { int yes = 1; #ifdef _WIN32 - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), sizeof(yes)); setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, - reinterpret_cast(&yes), sizeof(yes)); + reinterpret_cast(&yes), sizeof(yes)); #else #ifdef SO_REUSEPORT - setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), sizeof(yes)); #else - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), sizeof(yes)); #endif #endif } template -inline Server & -Server::set_read_timeout(const std::chrono::duration &duration) { +inline Server& Server::set_read_timeout( + const std::chrono::duration& duration) { detail::duration_to_sec_and_usec( duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); return *this; } template -inline Server & -Server::set_write_timeout(const std::chrono::duration &duration) { +inline Server& Server::set_write_timeout( + const std::chrono::duration& duration) { detail::duration_to_sec_and_usec( duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); return *this; } template -inline Server & -Server::set_idle_interval(const std::chrono::duration &duration) { +inline Server& Server::set_idle_interval( + const std::chrono::duration& duration) { detail::duration_to_sec_and_usec( duration, [&](time_t sec, time_t usec) { set_idle_interval(sec, usec); }); return *this; @@ -1636,42 +1639,56 @@ Server::set_idle_interval(const std::chrono::duration &duration) { inline std::string to_string(const Error error) { switch (error) { - case Error::Success: return "Success"; - case Error::Connection: return "Connection"; - case Error::BindIPAddress: return "BindIPAddress"; - case Error::Read: return "Read"; - case Error::Write: return "Write"; - case Error::ExceedRedirectCount: return "ExceedRedirectCount"; - case Error::Canceled: return "Canceled"; - case Error::SSLConnection: return "SSLConnection"; - case Error::SSLLoadingCerts: return "SSLLoadingCerts"; - case Error::SSLServerVerification: return "SSLServerVerification"; + case Error::Success: + return "Success"; + case Error::Connection: + return "Connection"; + case Error::BindIPAddress: + return "BindIPAddress"; + case Error::Read: + return "Read"; + case Error::Write: + return "Write"; + case Error::ExceedRedirectCount: + return "ExceedRedirectCount"; + case Error::Canceled: + return "Canceled"; + case Error::SSLConnection: + return "SSLConnection"; + case Error::SSLLoadingCerts: + return "SSLLoadingCerts"; + case Error::SSLServerVerification: + return "SSLServerVerification"; case Error::UnsupportedMultipartBoundaryChars: return "UnsupportedMultipartBoundaryChars"; - case Error::Compression: return "Compression"; - case Error::ConnectionTimeout: return "ConnectionTimeout"; - case Error::Unknown: return "Unknown"; - default: break; + case Error::Compression: + return "Compression"; + case Error::ConnectionTimeout: + return "ConnectionTimeout"; + case Error::Unknown: + return "Unknown"; + default: + break; } return "Invalid"; } -inline std::ostream &operator<<(std::ostream &os, const Error &obj) { +inline std::ostream& operator<<(std::ostream& os, const Error& obj) { os << to_string(obj); os << " (" << static_cast::type>(obj) << ')'; return os; } template -inline T Result::get_request_header_value(const std::string &key, +inline T Result::get_request_header_value(const std::string& key, size_t id) const { return detail::get_header_value(request_headers_, key, id, 0); } template inline void ClientImpl::set_connection_timeout( - const std::chrono::duration &duration) { + const std::chrono::duration& duration) { detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) { set_connection_timeout(sec, usec); }); @@ -1679,33 +1696,33 @@ inline void ClientImpl::set_connection_timeout( template inline void ClientImpl::set_read_timeout( - const std::chrono::duration &duration) { + const std::chrono::duration& duration) { detail::duration_to_sec_and_usec( duration, [&](time_t sec, time_t usec) { set_read_timeout(sec, usec); }); } template inline void ClientImpl::set_write_timeout( - const std::chrono::duration &duration) { + const std::chrono::duration& duration) { detail::duration_to_sec_and_usec( duration, [&](time_t sec, time_t usec) { set_write_timeout(sec, usec); }); } template inline void Client::set_connection_timeout( - const std::chrono::duration &duration) { + const std::chrono::duration& duration) { cli_->set_connection_timeout(duration); } template -inline void -Client::set_read_timeout(const std::chrono::duration &duration) { +inline void Client::set_read_timeout( + const std::chrono::duration& duration) { cli_->set_read_timeout(duration); } template -inline void -Client::set_write_timeout(const std::chrono::duration &duration) { +inline void Client::set_write_timeout( + const std::chrono::duration& duration) { cli_->set_write_timeout(duration); } @@ -1714,135 +1731,134 @@ Client::set_write_timeout(const std::chrono::duration &duration) { * .h + .cc. */ -std::string hosted_at(const std::string &hostname); +std::string hosted_at(const std::string& hostname); -void hosted_at(const std::string &hostname, std::vector &addrs); +void hosted_at(const std::string& hostname, std::vector& addrs); -std::string append_query_params(const std::string &path, const Params ¶ms); +std::string append_query_params(const std::string& path, const Params& params); std::pair make_range_header(Ranges ranges); -std::pair -make_basic_authentication_header(const std::string &username, - const std::string &password, - bool is_proxy = false); +std::pair make_basic_authentication_header( + const std::string& username, const std::string& password, + bool is_proxy = false); namespace detail { -std::string encode_query_param(const std::string &value); +std::string encode_query_param(const std::string& value); -std::string decode_url(const std::string &s, bool convert_plus_to_space); +std::string decode_url(const std::string& s, bool convert_plus_to_space); -void read_file(const std::string &path, std::string &out); +void read_file(const std::string& path, std::string& out); -std::string trim_copy(const std::string &s); +std::string trim_copy(const std::string& s); -void split(const char *b, const char *e, char d, - std::function fn); +void split(const char* b, const char* e, char d, + std::function fn); bool process_client_socket(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, - std::function callback); + std::function callback); socket_t create_client_socket( - const std::string &host, const std::string &ip, int port, + const std::string& host, const std::string& ip, int port, int address_family, bool tcp_nodelay, SocketOptions socket_options, time_t connection_timeout_sec, time_t connection_timeout_usec, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, const std::string &intf, Error &error); + time_t write_timeout_usec, const std::string& intf, Error& error); -const char *get_header_value(const Headers &headers, const std::string &key, - size_t id = 0, const char *def = nullptr); +const char* get_header_value(const Headers& headers, const std::string& key, + size_t id = 0, const char* def = nullptr); -std::string params_to_query_str(const Params ¶ms); +std::string params_to_query_str(const Params& params); -void parse_query_text(const std::string &s, Params ¶ms); +void parse_query_text(const std::string& s, Params& params); -bool parse_range_header(const std::string &s, Ranges &ranges); +bool parse_range_header(const std::string& s, Ranges& ranges); int close_socket(socket_t sock); -ssize_t send_socket(socket_t sock, const void *ptr, size_t size, int flags); +ssize_t send_socket(socket_t sock, const void* ptr, size_t size, int flags); -ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags); +ssize_t read_socket(socket_t sock, void* ptr, size_t size, int flags); enum class EncodingType { None = 0, Gzip, Brotli }; -EncodingType encoding_type(const Request &req, const Response &res); +EncodingType encoding_type(const Request& req, const Response& res); class BufferStream : public Stream { -public: + public: BufferStream() = default; ~BufferStream() override = default; bool is_readable() const override; bool is_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; + ssize_t read(char* ptr, size_t size) override; + ssize_t write(const char* ptr, size_t size) override; + void get_remote_ip_and_port(std::string& ip, int& port) const override; socket_t socket() const override; - const std::string &get_buffer() const; + const std::string& get_buffer() const; -private: + private: std::string buffer; size_t position = 0; }; class compressor { -public: + public: virtual ~compressor() = default; - typedef std::function Callback; - virtual bool compress(const char *data, size_t data_length, bool last, + typedef std::function Callback; + virtual bool compress(const char* data, size_t data_length, bool last, Callback callback) = 0; }; class decompressor { -public: + public: virtual ~decompressor() = default; virtual bool is_valid() const = 0; - typedef std::function Callback; - virtual bool decompress(const char *data, size_t data_length, + typedef std::function Callback; + virtual bool decompress(const char* data, size_t data_length, Callback callback) = 0; }; class nocompressor : public compressor { -public: + public: virtual ~nocompressor() = default; - bool compress(const char *data, size_t data_length, bool /*last*/, + bool compress(const char* data, size_t data_length, bool /*last*/, Callback callback) override; }; #ifdef CPPHTTPLIB_ZLIB_SUPPORT class gzip_compressor : public compressor { -public: + public: gzip_compressor(); ~gzip_compressor(); - bool compress(const char *data, size_t data_length, bool last, + bool compress(const char* data, size_t data_length, bool last, Callback callback) override; -private: + private: bool is_valid_ = false; z_stream strm_; }; class gzip_decompressor : public decompressor { -public: + public: gzip_decompressor(); ~gzip_decompressor(); bool is_valid() const override; - bool decompress(const char *data, size_t data_length, + bool decompress(const char* data, size_t data_length, Callback callback) override; -private: + private: bool is_valid_ = false; z_stream strm_; }; @@ -1850,55 +1866,55 @@ class gzip_decompressor : public decompressor { #ifdef CPPHTTPLIB_BROTLI_SUPPORT class brotli_compressor : public compressor { -public: + public: brotli_compressor(); ~brotli_compressor(); - bool compress(const char *data, size_t data_length, bool last, + bool compress(const char* data, size_t data_length, bool last, Callback callback) override; -private: - BrotliEncoderState *state_ = nullptr; + private: + BrotliEncoderState* state_ = nullptr; }; class brotli_decompressor : public decompressor { -public: + public: brotli_decompressor(); ~brotli_decompressor(); bool is_valid() const override; - bool decompress(const char *data, size_t data_length, + bool decompress(const char* data, size_t data_length, Callback callback) override; -private: + private: BrotliDecoderResult decoder_r; - BrotliDecoderState *decoder_s = nullptr; + BrotliDecoderState* decoder_s = nullptr; }; #endif // NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` // to store data. The call can set memory on stack for performance. class stream_line_reader { -public: - stream_line_reader(Stream &strm, char *fixed_buffer, + public: + stream_line_reader(Stream& strm, char* fixed_buffer, size_t fixed_buffer_size); - const char *ptr() const; + const char* ptr() const; size_t size() const; bool end_with_crlf() const; bool getline(); -private: + private: void append(char c); - Stream &strm_; - char *fixed_buffer_; + Stream& strm_; + char* fixed_buffer_; const size_t fixed_buffer_size_; size_t fixed_buffer_used_size_ = 0; std::string glowable_buffer_; }; -} // namespace detail +} // namespace detail // ---------------------------------------------------------------------------- @@ -1908,7 +1924,7 @@ class stream_line_reader { namespace detail { -inline bool is_hex(char c, int &v) { +inline bool is_hex(char c, int& v) { if (0x20 <= c && isdigit(c)) { v = c - '0'; return true; @@ -1922,13 +1938,17 @@ inline bool is_hex(char c, int &v) { return false; } -inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, - int &val) { - if (i >= s.size()) { return false; } +inline bool from_hex_to_i(const std::string& s, size_t i, size_t cnt, + int& val) { + if (i >= s.size()) { + return false; + } val = 0; for (; cnt; i++, cnt--) { - if (!s[i]) { return false; } + if (!s[i]) { + return false; + } int v = 0; if (is_hex(s[i], v)) { val = val * 16 + v; @@ -1940,7 +1960,7 @@ inline bool from_hex_to_i(const std::string &s, size_t i, size_t cnt, } inline std::string from_i_to_hex(size_t n) { - const char *charset = "0123456789abcdef"; + const char* charset = "0123456789abcdef"; std::string ret; do { ret = charset[n & 15] + ret; @@ -1949,7 +1969,7 @@ inline std::string from_i_to_hex(size_t n) { return ret; } -inline size_t to_utf8(int code, char *buff) { +inline size_t to_utf8(int code, char* buff) { if (code < 0x0080) { buff[0] = (code & 0x7F); return 1; @@ -1962,7 +1982,7 @@ inline size_t to_utf8(int code, char *buff) { buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); buff[2] = static_cast(0x80 | (code & 0x3F)); return 3; - } else if (code < 0xE000) { // D800 - DFFF is invalid... + } else if (code < 0xE000) { // D800 - DFFF is invalid... return 0; } else if (code < 0x10000) { buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); @@ -1983,7 +2003,7 @@ inline size_t to_utf8(int code, char *buff) { // NOTE: This code came up with the following stackoverflow post: // https://stackoverflow.com/questions/180947/base64-decode-snippet-in-c -inline std::string base64_encode(const std::string &in) { +inline std::string base64_encode(const std::string& in) { static const auto lookup = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -2002,7 +2022,9 @@ inline std::string base64_encode(const std::string &in) { } } - if (valb > -6) { out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); } + if (valb > -6) { + out.push_back(lookup[((val << 8) >> (valb + 8)) & 0x3F]); + } while (out.size() % 4) { out.push_back('='); @@ -2011,7 +2033,7 @@ inline std::string base64_encode(const std::string &in) { return out; } -inline bool is_file(const std::string &path) { +inline bool is_file(const std::string& path) { #ifdef _WIN32 return _access_s(path.c_str(), 0) == 0; #else @@ -2020,12 +2042,12 @@ inline bool is_file(const std::string &path) { #endif } -inline bool is_dir(const std::string &path) { +inline bool is_dir(const std::string& path) { struct stat st; return stat(path.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); } -inline bool is_valid_path(const std::string &path) { +inline bool is_valid_path(const std::string& path) { size_t level = 0; size_t i = 0; @@ -2047,7 +2069,9 @@ inline bool is_valid_path(const std::string &path) { if (!path.compare(beg, len, ".")) { ; } else if (!path.compare(beg, len, "..")) { - if (level == 0) { return false; } + if (level == 0) { + return false; + } level--; } else { level++; @@ -2062,7 +2086,7 @@ inline bool is_valid_path(const std::string &path) { return true; } -inline std::string encode_query_param(const std::string &value) { +inline std::string encode_query_param(const std::string& value) { std::ostringstream escaped; escaped.fill('0'); escaped << std::hex; @@ -2083,20 +2107,34 @@ inline std::string encode_query_param(const std::string &value) { return escaped.str(); } -inline std::string encode_url(const std::string &s) { +inline std::string encode_url(const std::string& s) { std::string result; result.reserve(s.size()); for (size_t i = 0; s[i]; i++) { switch (s[i]) { - case ' ': result += "%20"; break; - case '+': result += "%2B"; break; - case '\r': result += "%0D"; break; - case '\n': result += "%0A"; break; - case '\'': result += "%27"; break; - case ',': result += "%2C"; break; + case ' ': + result += "%20"; + break; + case '+': + result += "%2B"; + break; + case '\r': + result += "%0D"; + break; + case '\n': + result += "%0A"; + break; + case '\'': + result += "%27"; + break; + case ',': + result += "%2C"; + break; // case ':': result += "%3A"; break; // ok? probably... - case ';': result += "%3B"; break; + case ';': + result += "%3B"; + break; default: auto c = static_cast(s[i]); if (c >= 0x80) { @@ -2115,7 +2153,7 @@ inline std::string encode_url(const std::string &s) { return result; } -inline std::string decode_url(const std::string &s, +inline std::string decode_url(const std::string& s, bool convert_plus_to_space) { std::string result; @@ -2127,8 +2165,10 @@ inline std::string decode_url(const std::string &s, // 4 digits Unicode codes char buff[4]; size_t len = to_utf8(val, buff); - if (len > 0) { result.append(buff, len); } - i += 5; // 'u0000' + if (len > 0) { + result.append(buff, len); + } + i += 5; // 'u0000' } else { result += s[i]; } @@ -2137,7 +2177,7 @@ inline std::string decode_url(const std::string &s, if (from_hex_to_i(s, i + 1, 2, val)) { // 2 digits hex codes result += static_cast(val); - i += 2; // '00' + i += 2; // '00' } else { result += s[i]; } @@ -2152,7 +2192,7 @@ inline std::string decode_url(const std::string &s, return result; } -inline void read_file(const std::string &path, std::string &out) { +inline void read_file(const std::string& path, std::string& out) { std::ifstream fs(path, std::ios_base::binary); fs.seekg(0, std::ios_base::end); auto size = fs.tellg(); @@ -2161,16 +2201,18 @@ inline void read_file(const std::string &path, std::string &out) { fs.read(&out[0], static_cast(size)); } -inline std::string file_extension(const std::string &path) { +inline std::string file_extension(const std::string& path) { std::smatch m; static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); - if (std::regex_search(path, m, re)) { return m[1].str(); } + if (std::regex_search(path, m, re)) { + return m[1].str(); + } return std::string(); } inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } -inline std::pair trim(const char *b, const char *e, size_t left, +inline std::pair trim(const char* b, const char* e, size_t left, size_t right) { while (b + left < e && is_space_or_tab(b[left])) { left++; @@ -2181,20 +2223,22 @@ inline std::pair trim(const char *b, const char *e, size_t left, return std::make_pair(left, right); } -inline std::string trim_copy(const std::string &s) { +inline std::string trim_copy(const std::string& s) { auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); return s.substr(r.first, r.second - r.first); } -inline void split(const char *b, const char *e, char d, - std::function fn) { +inline void split(const char* b, const char* e, char d, + std::function fn) { size_t i = 0; size_t beg = 0; while (e ? (b + i < e) : (b[i] != '\0')) { if (b[i] == d) { auto r = trim(b, e, beg, i); - if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + if (r.first < r.second) { + fn(&b[r.first], &b[r.second]); + } beg = i + 1; } i++; @@ -2202,16 +2246,19 @@ inline void split(const char *b, const char *e, char d, if (i) { auto r = trim(b, e, beg, i); - if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + if (r.first < r.second) { + fn(&b[r.first], &b[r.second]); + } } } -inline stream_line_reader::stream_line_reader(Stream &strm, char *fixed_buffer, +inline stream_line_reader::stream_line_reader(Stream& strm, char* fixed_buffer, size_t fixed_buffer_size) - : strm_(strm), fixed_buffer_(fixed_buffer), + : strm_(strm), + fixed_buffer_(fixed_buffer), fixed_buffer_size_(fixed_buffer_size) {} -inline const char *stream_line_reader::ptr() const { +inline const char* stream_line_reader::ptr() const { if (glowable_buffer_.empty()) { return fixed_buffer_; } else { @@ -2252,7 +2299,9 @@ inline bool stream_line_reader::getline() { append(byte); - if (byte == '\n') { break; } + if (byte == '\n') { + break; + } } return true; @@ -2279,21 +2328,24 @@ inline int close_socket(socket_t sock) { #endif } -template inline ssize_t handle_EINTR(T fn) { +template +inline ssize_t handle_EINTR(T fn) { ssize_t res = false; while (true) { res = fn(); - if (res < 0 && errno == EINTR) { continue; } + if (res < 0 && errno == EINTR) { + continue; + } break; } return res; } -inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { +inline ssize_t read_socket(socket_t sock, void* ptr, size_t size, int flags) { return handle_EINTR([&]() { return recv(sock, #ifdef _WIN32 - static_cast(ptr), static_cast(size), + static_cast(ptr), static_cast(size), #else ptr, size, #endif @@ -2301,12 +2353,12 @@ inline ssize_t read_socket(socket_t sock, void *ptr, size_t size, int flags) { }); } -inline ssize_t send_socket(socket_t sock, const void *ptr, size_t size, +inline ssize_t send_socket(socket_t sock, const void* ptr, size_t size, int flags) { return handle_EINTR([&]() { return send(sock, #ifdef _WIN32 - static_cast(ptr), static_cast(size), + static_cast(ptr), static_cast(size), #else ptr, size, #endif @@ -2325,7 +2377,9 @@ inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); #else #ifndef _WIN32 - if (sock >= FD_SETSIZE) { return 1; } + if (sock >= FD_SETSIZE) { + return 1; + } #endif fd_set fds; @@ -2353,7 +2407,9 @@ inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); #else #ifndef _WIN32 - if (sock >= FD_SETSIZE) { return 1; } + if (sock >= FD_SETSIZE) { + return 1; + } #endif fd_set fds; @@ -2381,13 +2437,15 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); - if (poll_res == 0) { return Error::ConnectionTimeout; } + if (poll_res == 0) { + return Error::ConnectionTimeout; + } if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { int error = 0; socklen_t len = sizeof(error); auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); + reinterpret_cast(&error), &len); auto successful = res >= 0 && !error; return successful ? Error::Success : Error::Connection; } @@ -2395,7 +2453,9 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, return Error::Connection; #else #ifndef _WIN32 - if (sock >= FD_SETSIZE) { return Error::Connection; } + if (sock >= FD_SETSIZE) { + return Error::Connection; + } #endif fd_set fdsr; @@ -2413,13 +2473,15 @@ inline Error wait_until_socket_is_ready(socket_t sock, time_t sec, return select(static_cast(sock + 1), &fdsr, &fdsw, &fdse, &tv); }); - if (ret == 0) { return Error::ConnectionTimeout; } + if (ret == 0) { + return Error::ConnectionTimeout; + } if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { int error = 0; socklen_t len = sizeof(error); auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len); + reinterpret_cast(&error), &len); auto successful = res >= 0 && !error; return successful ? Error::Success : Error::Connection; } @@ -2439,19 +2501,19 @@ inline bool is_socket_alive(socket_t sock) { } class SocketStream : public Stream { -public: + public: SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec); ~SocketStream() override; bool is_readable() const override; bool is_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; + ssize_t read(char* ptr, size_t size) override; + ssize_t write(const char* ptr, size_t size) override; + void get_remote_ip_and_port(std::string& ip, int& port) const override; socket_t socket() const override; -private: + private: socket_t sock_; time_t read_timeout_sec_; time_t read_timeout_usec_; @@ -2467,22 +2529,22 @@ class SocketStream : public Stream { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT class SSLSocketStream : public Stream { -public: - SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec, + public: + SSLSocketStream(socket_t sock, SSL* ssl, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec); ~SSLSocketStream() override; bool is_readable() const override; bool is_writable() const override; - ssize_t read(char *ptr, size_t size) override; - ssize_t write(const char *ptr, size_t size) override; - void get_remote_ip_and_port(std::string &ip, int &port) const override; + ssize_t read(char* ptr, size_t size) override; + ssize_t write(const char* ptr, size_t size) override; + void get_remote_ip_and_port(std::string& ip, int& port) const override; socket_t socket() const override; -private: + private: socket_t sock_; - SSL *ssl_; + SSL* ssl_; time_t read_timeout_sec_; time_t read_timeout_usec_; time_t write_timeout_sec_; @@ -2501,7 +2563,9 @@ inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { auto current = steady_clock::now(); auto duration = duration_cast(current - start); auto timeout = keep_alive_timeout_sec * 1000; - if (duration.count() > timeout) { return false; } + if (duration.count() > timeout) { + return false; + } std::this_thread::sleep_for(std::chrono::milliseconds(1)); } else { return true; @@ -2510,10 +2574,11 @@ inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { } template -inline bool -process_server_socket_core(const std::atomic &svr_sock, socket_t sock, - size_t keep_alive_max_count, - time_t keep_alive_timeout_sec, T callback) { +inline bool process_server_socket_core(const std::atomic& svr_sock, + socket_t sock, + size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, + T callback) { assert(keep_alive_max_count > 0); auto ret = false; auto count = keep_alive_max_count; @@ -2522,22 +2587,25 @@ process_server_socket_core(const std::atomic &svr_sock, socket_t sock, auto close_connection = count == 1; auto connection_closed = false; ret = callback(close_connection, connection_closed); - if (!ret || connection_closed) { break; } + if (!ret || connection_closed) { + break; + } count--; } return ret; } template -inline bool -process_server_socket(const std::atomic &svr_sock, socket_t sock, - size_t keep_alive_max_count, - time_t keep_alive_timeout_sec, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { +inline bool process_server_socket(const std::atomic& svr_sock, + socket_t sock, size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, + time_t read_timeout_sec, + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { return process_server_socket_core( svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, - [&](bool close_connection, bool &connection_closed) { + [&](bool close_connection, bool& connection_closed) { SocketStream strm(sock, read_timeout_sec, read_timeout_usec, write_timeout_sec, write_timeout_usec); return callback(strm, close_connection, connection_closed); @@ -2548,7 +2616,7 @@ inline bool process_client_socket(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, - std::function callback) { + std::function callback) { SocketStream strm(sock, read_timeout_sec, read_timeout_usec, write_timeout_sec, write_timeout_usec); return callback(strm); @@ -2563,14 +2631,14 @@ inline int shutdown_socket(socket_t sock) { } template -socket_t create_socket(const std::string &host, const std::string &ip, int port, +socket_t create_socket(const std::string& host, const std::string& ip, int port, int address_family, int socket_flags, bool tcp_nodelay, SocketOptions socket_options, BindOrConnect bind_or_connect) { // Get address info - const char *node = nullptr; + const char* node = nullptr; struct addrinfo hints; - struct addrinfo *result; + struct addrinfo* result; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_socktype = SOCK_STREAM; @@ -2582,7 +2650,9 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, hints.ai_family = AF_UNSPEC; hints.ai_flags = AI_NUMERICHOST; } else { - if (!host.empty()) { node = host.c_str(); } + if (!host.empty()) { + node = host.c_str(); + } hints.ai_family = address_family; hints.ai_flags = socket_flags; } @@ -2590,7 +2660,8 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #ifndef _WIN32 if (hints.ai_family == AF_UNIX) { const auto addrlen = host.length(); - if (addrlen > sizeof(sockaddr_un::sun_path)) return INVALID_SOCKET; + if (addrlen > sizeof(sockaddr_un::sun_path)) + return INVALID_SOCKET; auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); if (sock != INVALID_SOCKET) { @@ -2598,7 +2669,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, addr.sun_family = AF_UNIX; std::copy(host.begin(), host.end(), addr.sun_path); - hints.ai_addr = reinterpret_cast(&addr); + hints.ai_addr = reinterpret_cast(&addr); hints.ai_addrlen = static_cast( sizeof(addr) - sizeof(addr.sun_path) + addrlen); @@ -2646,23 +2717,29 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port, #else auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); #endif - if (sock == INVALID_SOCKET) { continue; } + if (sock == INVALID_SOCKET) { + continue; + } #ifndef _WIN32 - if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { continue; } + if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { + continue; + } #endif if (tcp_nodelay) { int yes = 1; - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&yes), + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&yes), sizeof(yes)); } - if (socket_options) { socket_options(sock); } + if (socket_options) { + socket_options(sock); + } if (rp->ai_family == AF_INET6) { int no = 0; - setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&no), + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&no), sizeof(no)); } @@ -2698,20 +2775,22 @@ inline bool is_connection_error() { #endif } -inline bool bind_ip_address(socket_t sock, const std::string &host) { +inline bool bind_ip_address(socket_t sock, const std::string& host) { struct addrinfo hints; - struct addrinfo *result; + struct addrinfo* result; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = 0; - if (getaddrinfo(host.c_str(), "0", &hints, &result)) { return false; } + if (getaddrinfo(host.c_str(), "0", &hints, &result)) { + return false; + } auto ret = false; for (auto rp = result; rp; rp = rp->ai_next) { - const auto &ai = *rp; + const auto& ai = *rp; if (!::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { ret = true; break; @@ -2727,8 +2806,8 @@ inline bool bind_ip_address(socket_t sock, const std::string &host) { #endif #ifdef USE_IF2IP -inline std::string if2ip(int address_family, const std::string &ifn) { - struct ifaddrs *ifap; +inline std::string if2ip(int address_family, const std::string& ifn) { + struct ifaddrs* ifap; getifaddrs(&ifap); std::string addr_candidate; for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { @@ -2736,14 +2815,14 @@ inline std::string if2ip(int address_family, const std::string &ifn) { (AF_UNSPEC == address_family || ifa->ifa_addr->sa_family == address_family)) { if (ifa->ifa_addr->sa_family == AF_INET) { - auto sa = reinterpret_cast(ifa->ifa_addr); + auto sa = reinterpret_cast(ifa->ifa_addr); char buf[INET_ADDRSTRLEN]; if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) { freeifaddrs(ifap); return std::string(buf, INET_ADDRSTRLEN); } } else if (ifa->ifa_addr->sa_family == AF_INET6) { - auto sa = reinterpret_cast(ifa->ifa_addr); + auto sa = reinterpret_cast(ifa->ifa_addr); if (!IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { char buf[INET6_ADDRSTRLEN] = {}; if (inet_ntop(AF_INET6, &sa->sin6_addr, buf, INET6_ADDRSTRLEN)) { @@ -2766,18 +2845,20 @@ inline std::string if2ip(int address_family, const std::string &ifn) { #endif inline socket_t create_client_socket( - const std::string &host, const std::string &ip, int port, + const std::string& host, const std::string& ip, int port, int address_family, bool tcp_nodelay, SocketOptions socket_options, time_t connection_timeout_sec, time_t connection_timeout_usec, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, const std::string &intf, Error &error) { + time_t write_timeout_usec, const std::string& intf, Error& error) { auto sock = create_socket( host, ip, port, address_family, 0, tcp_nodelay, std::move(socket_options), - [&](socket_t sock2, struct addrinfo &ai) -> bool { + [&](socket_t sock2, struct addrinfo& ai) -> bool { if (!intf.empty()) { #ifdef USE_IF2IP auto ip_from_if = if2ip(address_family, intf); - if (ip_from_if.empty()) { ip_from_if = intf; } + if (ip_from_if.empty()) { + ip_from_if = intf; + } if (!bind_ip_address(sock2, ip_from_if.c_str())) { error = Error::BindIPAddress; return false; @@ -2797,7 +2878,9 @@ inline socket_t create_client_socket( } error = wait_until_socket_is_ready(sock2, connection_timeout_sec, connection_timeout_usec); - if (error != Error::Success) { return false; } + if (error != Error::Success) { + return false; + } } set_nonblocking(sock2, false); @@ -2806,13 +2889,13 @@ inline socket_t create_client_socket( #ifdef _WIN32 auto timeout = static_cast(read_timeout_sec * 1000 + read_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char*) &timeout, sizeof(timeout)); #else timeval tv; tv.tv_sec = static_cast(read_timeout_sec); tv.tv_usec = static_cast(read_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, (char*) &tv, sizeof(tv)); #endif } { @@ -2820,13 +2903,13 @@ inline socket_t create_client_socket( #ifdef _WIN32 auto timeout = static_cast(write_timeout_sec * 1000 + write_timeout_usec / 1000); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char*) &timeout, sizeof(timeout)); #else timeval tv; tv.tv_sec = static_cast(write_timeout_sec); tv.tv_usec = static_cast(write_timeout_usec); - setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, (char*) &tv, sizeof(tv)); #endif } @@ -2837,26 +2920,28 @@ inline socket_t create_client_socket( if (sock != INVALID_SOCKET) { error = Error::Success; } else { - if (error == Error::Success) { error = Error::Connection; } + if (error == Error::Success) { + error = Error::Connection; + } } return sock; } -inline bool get_remote_ip_and_port(const struct sockaddr_storage &addr, - socklen_t addr_len, std::string &ip, - int &port) { +inline bool get_remote_ip_and_port(const struct sockaddr_storage& addr, + socklen_t addr_len, std::string& ip, + int& port) { if (addr.ss_family == AF_INET) { - port = ntohs(reinterpret_cast(&addr)->sin_port); + port = ntohs(reinterpret_cast(&addr)->sin_port); } else if (addr.ss_family == AF_INET6) { port = - ntohs(reinterpret_cast(&addr)->sin6_port); + ntohs(reinterpret_cast(&addr)->sin6_port); } else { return false; } std::array ipstr{}; - if (getnameinfo(reinterpret_cast(&addr), addr_len, + if (getnameinfo(reinterpret_cast(&addr), addr_len, ipstr.data(), static_cast(ipstr.size()), nullptr, 0, NI_NUMERICHOST)) { return false; @@ -2866,170 +2951,276 @@ inline bool get_remote_ip_and_port(const struct sockaddr_storage &addr, return true; } -inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { +inline void get_remote_ip_and_port(socket_t sock, std::string& ip, int& port) { struct sockaddr_storage addr; socklen_t addr_len = sizeof(addr); - if (!getpeername(sock, reinterpret_cast(&addr), + if (!getpeername(sock, reinterpret_cast(&addr), &addr_len)) { get_remote_ip_and_port(addr, addr_len, ip, port); } } -inline constexpr unsigned int str2tag_core(const char *s, size_t l, +inline constexpr unsigned int str2tag_core(const char* s, size_t l, unsigned int h) { return (l == 0) ? h : str2tag_core(s + 1, l - 1, (h * 33) ^ static_cast(*s)); } -inline unsigned int str2tag(const std::string &s) { +inline unsigned int str2tag(const std::string& s) { return str2tag_core(s.data(), s.size(), 0); } namespace udl { -inline constexpr unsigned int operator"" _t(const char *s, size_t l) { +inline constexpr unsigned int operator"" _t(const char* s, size_t l) { return str2tag_core(s, l, 0); } -} // namespace udl +} // namespace udl -inline const char * -find_content_type(const std::string &path, - const std::map &user_data) { +inline const char* find_content_type( + const std::string& path, + const std::map& user_data) { auto ext = file_extension(path); auto it = user_data.find(ext); - if (it != user_data.end()) { return it->second.c_str(); } + if (it != user_data.end()) { + return it->second.c_str(); + } using udl::operator""_t; switch (str2tag(ext)) { - default: return nullptr; - case "css"_t: return "text/css"; - case "csv"_t: return "text/csv"; + default: + return nullptr; + case "css"_t: + return "text/css"; + case "csv"_t: + return "text/csv"; case "htm"_t: - case "html"_t: return "text/html"; + case "html"_t: + return "text/html"; case "js"_t: - case "mjs"_t: return "text/javascript"; - case "txt"_t: return "text/plain"; - case "vtt"_t: return "text/vtt"; - - case "apng"_t: return "image/apng"; - case "avif"_t: return "image/avif"; - case "bmp"_t: return "image/bmp"; - case "gif"_t: return "image/gif"; - case "png"_t: return "image/png"; - case "svg"_t: return "image/svg+xml"; - case "webp"_t: return "image/webp"; - case "ico"_t: return "image/x-icon"; - case "tif"_t: return "image/tiff"; - case "tiff"_t: return "image/tiff"; + case "mjs"_t: + return "text/javascript"; + case "txt"_t: + return "text/plain"; + case "vtt"_t: + return "text/vtt"; + + case "apng"_t: + return "image/apng"; + case "avif"_t: + return "image/avif"; + case "bmp"_t: + return "image/bmp"; + case "gif"_t: + return "image/gif"; + case "png"_t: + return "image/png"; + case "svg"_t: + return "image/svg+xml"; + case "webp"_t: + return "image/webp"; + case "ico"_t: + return "image/x-icon"; + case "tif"_t: + return "image/tiff"; + case "tiff"_t: + return "image/tiff"; case "jpg"_t: - case "jpeg"_t: return "image/jpeg"; - - case "mp4"_t: return "video/mp4"; - case "mpeg"_t: return "video/mpeg"; - case "webm"_t: return "video/webm"; - - case "mp3"_t: return "audio/mp3"; - case "mpga"_t: return "audio/mpeg"; - case "weba"_t: return "audio/webm"; - case "wav"_t: return "audio/wave"; - - case "otf"_t: return "font/otf"; - case "ttf"_t: return "font/ttf"; - case "woff"_t: return "font/woff"; - case "woff2"_t: return "font/woff2"; - - case "7z"_t: return "application/x-7z-compressed"; - case "atom"_t: return "application/atom+xml"; - case "pdf"_t: return "application/pdf"; - case "json"_t: return "application/json"; - case "rss"_t: return "application/rss+xml"; - case "tar"_t: return "application/x-tar"; + case "jpeg"_t: + return "image/jpeg"; + + case "mp4"_t: + return "video/mp4"; + case "mpeg"_t: + return "video/mpeg"; + case "webm"_t: + return "video/webm"; + + case "mp3"_t: + return "audio/mp3"; + case "mpga"_t: + return "audio/mpeg"; + case "weba"_t: + return "audio/webm"; + case "wav"_t: + return "audio/wave"; + + case "otf"_t: + return "font/otf"; + case "ttf"_t: + return "font/ttf"; + case "woff"_t: + return "font/woff"; + case "woff2"_t: + return "font/woff2"; + + case "7z"_t: + return "application/x-7z-compressed"; + case "atom"_t: + return "application/atom+xml"; + case "pdf"_t: + return "application/pdf"; + case "json"_t: + return "application/json"; + case "rss"_t: + return "application/rss+xml"; + case "tar"_t: + return "application/x-tar"; case "xht"_t: - case "xhtml"_t: return "application/xhtml+xml"; - case "xslt"_t: return "application/xslt+xml"; - case "xml"_t: return "application/xml"; - case "gz"_t: return "application/gzip"; - case "zip"_t: return "application/zip"; - case "wasm"_t: return "application/wasm"; - } -} - -inline const char *status_message(int status) { + case "xhtml"_t: + return "application/xhtml+xml"; + case "xslt"_t: + return "application/xslt+xml"; + case "xml"_t: + return "application/xml"; + case "gz"_t: + return "application/gzip"; + case "zip"_t: + return "application/zip"; + case "wasm"_t: + return "application/wasm"; + } +} + +inline const char* status_message(int status) { switch (status) { - case 100: return "Continue"; - case 101: return "Switching Protocol"; - case 102: return "Processing"; - case 103: return "Early Hints"; - case 200: return "OK"; - case 201: return "Created"; - case 202: return "Accepted"; - case 203: return "Non-Authoritative Information"; - case 204: return "No Content"; - case 205: return "Reset Content"; - case 206: return "Partial Content"; - case 207: return "Multi-Status"; - case 208: return "Already Reported"; - case 226: return "IM Used"; - case 300: return "Multiple Choice"; - case 301: return "Moved Permanently"; - case 302: return "Found"; - case 303: return "See Other"; - case 304: return "Not Modified"; - case 305: return "Use Proxy"; - case 306: return "unused"; - case 307: return "Temporary Redirect"; - case 308: return "Permanent Redirect"; - case 400: return "Bad Request"; - case 401: return "Unauthorized"; - case 402: return "Payment Required"; - case 403: return "Forbidden"; - case 404: return "Not Found"; - case 405: return "Method Not Allowed"; - case 406: return "Not Acceptable"; - case 407: return "Proxy Authentication Required"; - case 408: return "Request Timeout"; - case 409: return "Conflict"; - case 410: return "Gone"; - case 411: return "Length Required"; - case 412: return "Precondition Failed"; - case 413: return "Payload Too Large"; - case 414: return "URI Too Long"; - case 415: return "Unsupported Media Type"; - case 416: return "Range Not Satisfiable"; - case 417: return "Expectation Failed"; - case 418: return "I'm a teapot"; - case 421: return "Misdirected Request"; - case 422: return "Unprocessable Entity"; - case 423: return "Locked"; - case 424: return "Failed Dependency"; - case 425: return "Too Early"; - case 426: return "Upgrade Required"; - case 428: return "Precondition Required"; - case 429: return "Too Many Requests"; - case 431: return "Request Header Fields Too Large"; - case 451: return "Unavailable For Legal Reasons"; - case 501: return "Not Implemented"; - case 502: return "Bad Gateway"; - case 503: return "Service Unavailable"; - case 504: return "Gateway Timeout"; - case 505: return "HTTP Version Not Supported"; - case 506: return "Variant Also Negotiates"; - case 507: return "Insufficient Storage"; - case 508: return "Loop Detected"; - case 510: return "Not Extended"; - case 511: return "Network Authentication Required"; + case 100: + return "Continue"; + case 101: + return "Switching Protocol"; + case 102: + return "Processing"; + case 103: + return "Early Hints"; + case 200: + return "OK"; + case 201: + return "Created"; + case 202: + return "Accepted"; + case 203: + return "Non-Authoritative Information"; + case 204: + return "No Content"; + case 205: + return "Reset Content"; + case 206: + return "Partial Content"; + case 207: + return "Multi-Status"; + case 208: + return "Already Reported"; + case 226: + return "IM Used"; + case 300: + return "Multiple Choice"; + case 301: + return "Moved Permanently"; + case 302: + return "Found"; + case 303: + return "See Other"; + case 304: + return "Not Modified"; + case 305: + return "Use Proxy"; + case 306: + return "unused"; + case 307: + return "Temporary Redirect"; + case 308: + return "Permanent Redirect"; + case 400: + return "Bad Request"; + case 401: + return "Unauthorized"; + case 402: + return "Payment Required"; + case 403: + return "Forbidden"; + case 404: + return "Not Found"; + case 405: + return "Method Not Allowed"; + case 406: + return "Not Acceptable"; + case 407: + return "Proxy Authentication Required"; + case 408: + return "Request Timeout"; + case 409: + return "Conflict"; + case 410: + return "Gone"; + case 411: + return "Length Required"; + case 412: + return "Precondition Failed"; + case 413: + return "Payload Too Large"; + case 414: + return "URI Too Long"; + case 415: + return "Unsupported Media Type"; + case 416: + return "Range Not Satisfiable"; + case 417: + return "Expectation Failed"; + case 418: + return "I'm a teapot"; + case 421: + return "Misdirected Request"; + case 422: + return "Unprocessable Entity"; + case 423: + return "Locked"; + case 424: + return "Failed Dependency"; + case 425: + return "Too Early"; + case 426: + return "Upgrade Required"; + case 428: + return "Precondition Required"; + case 429: + return "Too Many Requests"; + case 431: + return "Request Header Fields Too Large"; + case 451: + return "Unavailable For Legal Reasons"; + case 501: + return "Not Implemented"; + case 502: + return "Bad Gateway"; + case 503: + return "Service Unavailable"; + case 504: + return "Gateway Timeout"; + case 505: + return "HTTP Version Not Supported"; + case 506: + return "Variant Also Negotiates"; + case 507: + return "Insufficient Storage"; + case 508: + return "Loop Detected"; + case 510: + return "Not Extended"; + case 511: + return "Network Authentication Required"; default: - case 500: return "Internal Server Error"; + case 500: + return "Internal Server Error"; } } -inline bool can_compress_content_type(const std::string &content_type) { +inline bool can_compress_content_type(const std::string& content_type) { using udl::operator""_t; auto tag = str2tag(content_type); @@ -3040,39 +3231,48 @@ inline bool can_compress_content_type(const std::string &content_type) { case "application/json"_t: case "application/xml"_t: case "application/protobuf"_t: - case "application/xhtml+xml"_t: return true; + case "application/xhtml+xml"_t: + return true; default: return !content_type.rfind("text/", 0) && tag != "text/event-stream"_t; } } -inline EncodingType encoding_type(const Request &req, const Response &res) { +inline EncodingType encoding_type(const Request& req, const Response& res) { auto ret = detail::can_compress_content_type(res.get_header_value("Content-Type")); - if (!ret) { return EncodingType::None; } + if (!ret) { + return EncodingType::None; + } - const auto &s = req.get_header_value("Accept-Encoding"); - (void)(s); + const auto& s = req.get_header_value("Accept-Encoding"); + (void) (s); #ifdef CPPHTTPLIB_BROTLI_SUPPORT // TODO: 'Accept-Encoding' has br, not br;q=0 ret = s.find("br") != std::string::npos; - if (ret) { return EncodingType::Brotli; } + if (ret) { + return EncodingType::Brotli; + } #endif #ifdef CPPHTTPLIB_ZLIB_SUPPORT // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 ret = s.find("gzip") != std::string::npos; - if (ret) { return EncodingType::Gzip; } + if (ret) { + return EncodingType::Gzip; + } #endif return EncodingType::None; } -inline bool nocompressor::compress(const char *data, size_t data_length, +inline bool nocompressor::compress(const char* data, size_t data_length, bool /*last*/, Callback callback) { - if (!data_length) { return true; } + if (!data_length) { + return true; + } return callback(data, data_length); } @@ -3089,7 +3289,7 @@ inline gzip_compressor::gzip_compressor() { inline gzip_compressor::~gzip_compressor() { deflateEnd(&strm_); } -inline bool gzip_compressor::compress(const char *data, size_t data_length, +inline bool gzip_compressor::compress(const char* data, size_t data_length, bool last, Callback callback) { assert(is_valid_); @@ -3099,7 +3299,7 @@ inline bool gzip_compressor::compress(const char *data, size_t data_length, strm_.avail_in = static_cast( (std::min)(data_length, max_avail_in)); - strm_.next_in = const_cast(reinterpret_cast(data)); + strm_.next_in = const_cast(reinterpret_cast(data)); data_length -= strm_.avail_in; data += strm_.avail_in; @@ -3110,10 +3310,12 @@ inline bool gzip_compressor::compress(const char *data, size_t data_length, std::array buff{}; do { strm_.avail_out = static_cast(buff.size()); - strm_.next_out = reinterpret_cast(buff.data()); + strm_.next_out = reinterpret_cast(buff.data()); ret = deflate(&strm_, flush); - if (ret == Z_STREAM_ERROR) { return false; } + if (ret == Z_STREAM_ERROR) { + return false; + } if (!callback(buff.data(), buff.size() - strm_.avail_out)) { return false; @@ -3145,7 +3347,7 @@ inline gzip_decompressor::~gzip_decompressor() { inflateEnd(&strm_); } inline bool gzip_decompressor::is_valid() const { return is_valid_; } -inline bool gzip_decompressor::decompress(const char *data, size_t data_length, +inline bool gzip_decompressor::decompress(const char* data, size_t data_length, Callback callback) { assert(is_valid_); @@ -3157,7 +3359,7 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length, strm_.avail_in = static_cast( (std::min)(data_length, max_avail_in)); - strm_.next_in = const_cast(reinterpret_cast(data)); + strm_.next_in = const_cast(reinterpret_cast(data)); data_length -= strm_.avail_in; data += strm_.avail_in; @@ -3165,19 +3367,23 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length, std::array buff{}; while (strm_.avail_in > 0) { strm_.avail_out = static_cast(buff.size()); - strm_.next_out = reinterpret_cast(buff.data()); + strm_.next_out = reinterpret_cast(buff.data()); auto prev_avail_in = strm_.avail_in; ret = inflate(&strm_, Z_NO_FLUSH); - if (prev_avail_in - strm_.avail_in == 0) { return false; } + if (prev_avail_in - strm_.avail_in == 0) { + return false; + } assert(ret != Z_STREAM_ERROR); switch (ret) { case Z_NEED_DICT: case Z_DATA_ERROR: - case Z_MEM_ERROR: inflateEnd(&strm_); return false; + case Z_MEM_ERROR: + inflateEnd(&strm_); + return false; } if (!callback(buff.data(), buff.size() - strm_.avail_out)) { @@ -3185,7 +3391,8 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length, } } - if (ret != Z_OK && ret != Z_STREAM_END) return false; + if (ret != Z_OK && ret != Z_STREAM_END) + return false; } while (data_length > 0); @@ -3202,19 +3409,23 @@ inline brotli_compressor::~brotli_compressor() { BrotliEncoderDestroyInstance(state_); } -inline bool brotli_compressor::compress(const char *data, size_t data_length, +inline bool brotli_compressor::compress(const char* data, size_t data_length, bool last, Callback callback) { std::array buff{}; auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS; auto available_in = data_length; - auto next_in = reinterpret_cast(data); + auto next_in = reinterpret_cast(data); for (;;) { if (last) { - if (BrotliEncoderIsFinished(state_)) { break; } + if (BrotliEncoderIsFinished(state_)) { + break; + } } else { - if (!available_in) { break; } + if (!available_in) { + break; + } } auto available_out = buff.size(); @@ -3227,7 +3438,7 @@ inline bool brotli_compressor::compress(const char *data, size_t data_length, auto output_bytes = buff.size() - available_out; if (output_bytes) { - callback(reinterpret_cast(buff.data()), output_bytes); + callback(reinterpret_cast(buff.data()), output_bytes); } } @@ -3241,12 +3452,14 @@ inline brotli_decompressor::brotli_decompressor() { } inline brotli_decompressor::~brotli_decompressor() { - if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); } + if (decoder_s) { + BrotliDecoderDestroyInstance(decoder_s); + } } inline bool brotli_decompressor::is_valid() const { return decoder_s; } -inline bool brotli_decompressor::decompress(const char *data, +inline bool brotli_decompressor::decompress(const char* data, size_t data_length, Callback callback) { if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS || @@ -3254,7 +3467,7 @@ inline bool brotli_decompressor::decompress(const char *data, return 0; } - const uint8_t *next_in = (const uint8_t *)data; + const uint8_t* next_in = (const uint8_t*) data; size_t avail_in = data_length; size_t total_out; @@ -3262,16 +3475,20 @@ inline bool brotli_decompressor::decompress(const char *data, std::array buff{}; while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { - char *next_out = buff.data(); + char* next_out = buff.data(); size_t avail_out = buff.size(); decoder_r = BrotliDecoderDecompressStream( decoder_s, &avail_in, &next_in, &avail_out, - reinterpret_cast(&next_out), &total_out); + reinterpret_cast(&next_out), &total_out); - if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; } + if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { + return false; + } - if (!callback(buff.data(), buff.size() - avail_out)) { return false; } + if (!callback(buff.data(), buff.size() - avail_out)) { + return false; + } } return decoder_r == BROTLI_DECODER_RESULT_SUCCESS || @@ -3279,22 +3496,24 @@ inline bool brotli_decompressor::decompress(const char *data, } #endif -inline bool has_header(const Headers &headers, const std::string &key) { +inline bool has_header(const Headers& headers, const std::string& key) { return headers.find(key) != headers.end(); } -inline const char *get_header_value(const Headers &headers, - const std::string &key, size_t id, - const char *def) { +inline const char* get_header_value(const Headers& headers, + const std::string& key, size_t id, + const char* def) { auto rng = headers.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); - if (it != rng.second) { return it->second.c_str(); } + if (it != rng.second) { + return it->second.c_str(); + } return def; } template -inline bool parse_header(const char *beg, const char *end, T fn) { +inline bool parse_header(const char* beg, const char* end, T fn) { // Skip trailing spaces and tabs. while (beg < end && is_space_or_tab(end[-1])) { end--; @@ -3305,11 +3524,15 @@ inline bool parse_header(const char *beg, const char *end, T fn) { p++; } - if (p == end) { return false; } + if (p == end) { + return false; + } auto key_end = p; - if (*p++ != ':') { return false; } + if (*p++ != ':') { + return false; + } while (p < end && is_space_or_tab(*p)) { p++; @@ -3323,38 +3546,46 @@ inline bool parse_header(const char *beg, const char *end, T fn) { return false; } -inline bool read_headers(Stream &strm, Headers &headers) { +inline bool read_headers(Stream& strm, Headers& headers) { const auto bufsiz = 2048; char buf[bufsiz]; stream_line_reader line_reader(strm, buf, bufsiz); for (;;) { - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { + return false; + } // Check if the line ends with CRLF. auto line_terminator_len = 2; if (line_reader.end_with_crlf()) { // Blank line indicates end of headers. - if (line_reader.size() == 2) { break; } + if (line_reader.size() == 2) { + break; + } #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR } else { // Blank line indicates end of headers. - if (line_reader.size() == 1) { break; } + if (line_reader.size() == 1) { + break; + } line_terminator_len = 1; } #else } else { - continue; // Skip invalid line. + continue; // Skip invalid line. } #endif - if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + if (line_reader.size() > CPPHTTPLIB_HEADER_MAX_LENGTH) { + return false; + } // Exclude line terminator auto end = line_reader.ptr() + line_reader.size() - line_terminator_len; parse_header(line_reader.ptr(), end, - [&](std::string &&key, std::string &&val) { + [&](std::string&& key, std::string&& val) { headers.emplace(std::move(key), std::move(val)); }); } @@ -3362,7 +3593,7 @@ inline bool read_headers(Stream &strm, Headers &headers) { return true; } -inline bool read_content_with_length(Stream &strm, uint64_t len, +inline bool read_content_with_length(Stream& strm, uint64_t len, Progress progress, ContentReceiverWithProgress out) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; @@ -3371,31 +3602,39 @@ inline bool read_content_with_length(Stream &strm, uint64_t len, while (r < len) { auto read_len = static_cast(len - r); auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); - if (n <= 0) { return false; } + if (n <= 0) { + return false; + } - if (!out(buf, static_cast(n), r, len)) { return false; } + if (!out(buf, static_cast(n), r, len)) { + return false; + } r += static_cast(n); if (progress) { - if (!progress(r, len)) { return false; } + if (!progress(r, len)) { + return false; + } } } return true; } -inline void skip_content_with_length(Stream &strm, uint64_t len) { +inline void skip_content_with_length(Stream& strm, uint64_t len) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; uint64_t r = 0; while (r < len) { auto read_len = static_cast(len - r); auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); - if (n <= 0) { return; } + if (n <= 0) { + return; + } r += static_cast(n); } } -inline bool read_content_without_length(Stream &strm, +inline bool read_content_without_length(Stream& strm, ContentReceiverWithProgress out) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; uint64_t r = 0; @@ -3407,42 +3646,58 @@ inline bool read_content_without_length(Stream &strm, return true; } - if (!out(buf, static_cast(n), r, 0)) { return false; } + if (!out(buf, static_cast(n), r, 0)) { + return false; + } r += static_cast(n); } return true; } -inline bool read_content_chunked(Stream &strm, +inline bool read_content_chunked(Stream& strm, ContentReceiverWithProgress out) { const auto bufsiz = 16; char buf[bufsiz]; stream_line_reader line_reader(strm, buf, bufsiz); - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { + return false; + } unsigned long chunk_len; while (true) { - char *end_ptr; + char* end_ptr; chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); - if (end_ptr == line_reader.ptr()) { return false; } - if (chunk_len == ULONG_MAX) { return false; } + if (end_ptr == line_reader.ptr()) { + return false; + } + if (chunk_len == ULONG_MAX) { + return false; + } - if (chunk_len == 0) { break; } + if (chunk_len == 0) { + break; + } if (!read_content_with_length(strm, chunk_len, nullptr, out)) { return false; } - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { + return false; + } - if (strcmp(line_reader.ptr(), "\r\n")) { break; } + if (strcmp(line_reader.ptr(), "\r\n")) { + break; + } - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { + return false; + } } if (chunk_len == 0) { @@ -3454,13 +3709,13 @@ inline bool read_content_chunked(Stream &strm, return true; } -inline bool is_chunked_transfer_encoding(const Headers &headers) { +inline bool is_chunked_transfer_encoding(const Headers& headers) { return !strcasecmp(get_header_value(headers, "Transfer-Encoding", 0, ""), "chunked"); } template -bool prepare_content_receiver(T &x, int &status, +bool prepare_content_receiver(T& x, int& status, ContentReceiverWithProgress receiver, bool decompress, U callback) { if (decompress) { @@ -3485,10 +3740,10 @@ bool prepare_content_receiver(T &x, int &status, if (decompressor) { if (decompressor->is_valid()) { - ContentReceiverWithProgress out = [&](const char *buf, size_t n, + ContentReceiverWithProgress out = [&](const char* buf, size_t n, uint64_t off, uint64_t len) { return decompressor->decompress(buf, n, - [&](const char *buf2, size_t n2) { + [&](const char* buf2, size_t n2) { return receiver(buf2, n2, off, len); }); }; @@ -3500,7 +3755,7 @@ bool prepare_content_receiver(T &x, int &status, } } - ContentReceiverWithProgress out = [&](const char *buf, size_t n, uint64_t off, + ContentReceiverWithProgress out = [&](const char* buf, size_t n, uint64_t off, uint64_t len) { return receiver(buf, n, off, len); }; @@ -3508,12 +3763,12 @@ bool prepare_content_receiver(T &x, int &status, } template -bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, +bool read_content(Stream& strm, T& x, size_t payload_max_length, int& status, Progress progress, ContentReceiverWithProgress receiver, bool decompress) { return prepare_content_receiver( x, status, std::move(receiver), decompress, - [&](const ContentReceiverWithProgress &out) { + [&](const ContentReceiverWithProgress& out) { auto ret = true; auto exceed_payload_max_length = false; @@ -3532,44 +3787,52 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } } - if (!ret) { status = exceed_payload_max_length ? 413 : 400; } + if (!ret) { + status = exceed_payload_max_length ? 413 : 400; + } return ret; }); -} // namespace detail +} // namespace detail -inline ssize_t write_headers(Stream &strm, const Headers &headers) { +inline ssize_t write_headers(Stream& strm, const Headers& headers) { ssize_t write_len = 0; - for (const auto &x : headers) { + for (const auto& x : headers) { auto len = strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); - if (len < 0) { return len; } + if (len < 0) { + return len; + } write_len += len; } auto len = strm.write("\r\n"); - if (len < 0) { return len; } + if (len < 0) { + return len; + } write_len += len; return write_len; } -inline bool write_data(Stream &strm, const char *d, size_t l) { +inline bool write_data(Stream& strm, const char* d, size_t l) { size_t offset = 0; while (offset < l) { auto length = strm.write(d + offset, l - offset); - if (length < 0) { return false; } + if (length < 0) { + return false; + } offset += static_cast(length); } return true; } template -inline bool write_content(Stream &strm, const ContentProvider &content_provider, +inline bool write_content(Stream& strm, const ContentProvider& content_provider, size_t offset, size_t length, T is_shutting_down, - Error &error) { + Error& error) { size_t end_offset = offset + length; auto ok = true; DataSink data_sink; - data_sink.write = [&](const char *d, size_t l) -> bool { + data_sink.write = [&](const char* d, size_t l) -> bool { if (ok) { if (write_data(strm, d, l)) { offset += l; @@ -3598,28 +3861,29 @@ inline bool write_content(Stream &strm, const ContentProvider &content_provider, } template -inline bool write_content(Stream &strm, const ContentProvider &content_provider, +inline bool write_content(Stream& strm, const ContentProvider& content_provider, size_t offset, size_t length, - const T &is_shutting_down) { + const T& is_shutting_down) { auto error = Error::Success; return write_content(strm, content_provider, offset, length, is_shutting_down, error); } template -inline bool -write_content_without_length(Stream &strm, - const ContentProvider &content_provider, - const T &is_shutting_down) { +inline bool write_content_without_length( + Stream& strm, const ContentProvider& content_provider, + const T& is_shutting_down) { size_t offset = 0; auto data_available = true; auto ok = true; DataSink data_sink; - data_sink.write = [&](const char *d, size_t l) -> bool { + data_sink.write = [&](const char* d, size_t l) -> bool { if (ok) { offset += l; - if (!write_data(strm, d, l)) { ok = false; } + if (!write_data(strm, d, l)) { + ok = false; + } } return ok; }; @@ -3629,29 +3893,34 @@ write_content_without_length(Stream &strm, data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; while (data_available && !is_shutting_down()) { - if (!content_provider(offset, 0, data_sink)) { return false; } - if (!ok) { return false; } + if (!content_provider(offset, 0, data_sink)) { + return false; + } + if (!ok) { + return false; + } } return true; } template -inline bool -write_content_chunked(Stream &strm, const ContentProvider &content_provider, - const T &is_shutting_down, U &compressor, Error &error) { +inline bool write_content_chunked(Stream& strm, + const ContentProvider& content_provider, + const T& is_shutting_down, U& compressor, + Error& error) { size_t offset = 0; auto data_available = true; auto ok = true; DataSink data_sink; - data_sink.write = [&](const char *d, size_t l) -> bool { + data_sink.write = [&](const char* d, size_t l) -> bool { if (ok) { data_available = l > 0; offset += l; std::string payload; if (compressor.compress(d, l, false, - [&](const char *data, size_t data_len) { + [&](const char* data, size_t data_len) { payload.append(data, data_len); return true; })) { @@ -3659,7 +3928,9 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, // Emit chunked response header and footer for each chunk auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; - if (!write_data(strm, chunk.data(), chunk.size())) { ok = false; } + if (!write_data(strm, chunk.data(), chunk.size())) { + ok = false; + } } } else { ok = false; @@ -3669,13 +3940,15 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, }; data_sink.done = [&](void) { - if (!ok) { return; } + if (!ok) { + return; + } data_available = false; std::string payload; if (!compressor.compress(nullptr, 0, true, - [&](const char *data, size_t data_len) { + [&](const char* data, size_t data_len) { payload.append(data, data_len); return true; })) { @@ -3716,18 +3989,18 @@ write_content_chunked(Stream &strm, const ContentProvider &content_provider, } template -inline bool write_content_chunked(Stream &strm, - const ContentProvider &content_provider, - const T &is_shutting_down, U &compressor) { +inline bool write_content_chunked(Stream& strm, + const ContentProvider& content_provider, + const T& is_shutting_down, U& compressor) { auto error = Error::Success; return write_content_chunked(strm, content_provider, is_shutting_down, compressor, error); } template -inline bool redirect(T &cli, Request &req, Response &res, - const std::string &path, const std::string &location, - Error &error) { +inline bool redirect(T& cli, Request& req, Response& res, + const std::string& path, const std::string& location, + Error& error) { Request new_req = req; new_req.path = path; new_req.redirect_count_ -= 1; @@ -3749,11 +4022,13 @@ inline bool redirect(T &cli, Request &req, Response &res, return ret; } -inline std::string params_to_query_str(const Params ¶ms) { +inline std::string params_to_query_str(const Params& params) { std::string query; for (auto it = params.begin(); it != params.end(); ++it) { - if (it != params.begin()) { query += "&"; } + if (it != params.begin()) { + query += "&"; + } query += it->first; query += "="; query += encode_query_param(it->second); @@ -3761,16 +4036,18 @@ inline std::string params_to_query_str(const Params ¶ms) { return query; } -inline void parse_query_text(const std::string &s, Params ¶ms) { +inline void parse_query_text(const std::string& s, Params& params) { std::set cache; - split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) { + split(s.data(), s.data() + s.size(), '&', [&](const char* b, const char* e) { std::string kv(b, e); - if (cache.find(kv) != cache.end()) { return; } + if (cache.find(kv) != cache.end()) { + return; + } cache.insert(kv); std::string key; std::string val; - split(b, e, '=', [&](const char *b2, const char *e2) { + split(b, e, '=', [&](const char* b2, const char* e2) { if (key.empty()) { key.assign(b2, e2); } else { @@ -3784,10 +4061,12 @@ inline void parse_query_text(const std::string &s, Params ¶ms) { }); } -inline bool parse_multipart_boundary(const std::string &content_type, - std::string &boundary) { +inline bool parse_multipart_boundary(const std::string& content_type, + std::string& boundary) { auto pos = content_type.find("boundary="); - if (pos == std::string::npos) { return false; } + if (pos == std::string::npos) { + return false; + } boundary = content_type.substr(pos + 9); if (boundary.length() >= 2 && boundary.front() == '"' && boundary.back() == '"') { @@ -3797,9 +4076,9 @@ inline bool parse_multipart_boundary(const std::string &content_type, } #ifdef CPPHTTPLIB_NO_EXCEPTIONS -inline bool parse_range_header(const std::string &s, Ranges &ranges) { +inline bool parse_range_header(const std::string& s, Ranges& ranges) { #else -inline bool parse_range_header(const std::string &s, Ranges &ranges) try { +inline bool parse_range_header(const std::string& s, Ranges& ranges) try { #endif static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))"); std::smatch m; @@ -3807,8 +4086,9 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try { auto pos = static_cast(m.position(1)); auto len = static_cast(m.length(1)); bool all_valid_ranges = true; - split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { - if (!all_valid_ranges) return; + split(&s[pos], &s[pos + len], ',', [&](const char* b, const char* e) { + if (!all_valid_ranges) + return; static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))"); std::cmatch cm; if (std::regex_match(b, e, cm, re_another_range)) { @@ -3839,10 +4119,10 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try { #endif class MultipartFormDataParser { -public: + public: MultipartFormDataParser() = default; - void set_boundary(std::string &&boundary) { + void set_boundary(std::string&& boundary) { boundary_ = boundary; dash_boundary_crlf_ = dash_ + boundary_ + crlf_; crlf_dash_boundary_ = crlf_ + dash_ + boundary_; @@ -3850,9 +4130,8 @@ class MultipartFormDataParser { bool is_valid() const { return is_valid_; } - bool parse(const char *buf, size_t n, const ContentReceiver &content_callback, - const MultipartContentHeader &header_callback) { - + bool parse(const char* buf, size_t n, const ContentReceiver& content_callback, + const MultipartContentHeader& header_callback) { // TODO: support 'filename*' static const std::regex re_content_disposition( R"~(^Content-Disposition:\s*form-data;\s*name="(.*?)"(?:;\s*filename="(.*?)")?(?:;\s*filename\*=\S+)?\s*$)~", @@ -3862,22 +4141,28 @@ class MultipartFormDataParser { while (buf_size() > 0) { switch (state_) { - case 0: { // Initial boundary + case 0: { // Initial boundary buf_erase(buf_find(dash_boundary_crlf_)); - if (dash_boundary_crlf_.size() > buf_size()) { return true; } - if (!buf_start_with(dash_boundary_crlf_)) { return false; } + if (dash_boundary_crlf_.size() > buf_size()) { + return true; + } + if (!buf_start_with(dash_boundary_crlf_)) { + return false; + } buf_erase(dash_boundary_crlf_.size()); state_ = 1; break; } - case 1: { // New entry + case 1: { // New entry clear_file_info(); state_ = 2; break; } - case 2: { // Headers + case 2: { // Headers auto pos = buf_find(crlf_); - if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { return false; } + if (pos > CPPHTTPLIB_HEADER_MAX_LENGTH) { + return false; + } while (pos < buf_size()) { // Empty line if (pos == 0) { @@ -3904,11 +4189,15 @@ class MultipartFormDataParser { buf_erase(pos + crlf_.size()); pos = buf_find(crlf_); } - if (state_ != 3) { return true; } + if (state_ != 3) { + return true; + } break; } - case 3: { // Body - if (crlf_dash_boundary_.size() > buf_size()) { return true; } + case 3: { // Body + if (crlf_dash_boundary_.size() > buf_size()) { + return true; + } auto pos = buf_find(crlf_dash_boundary_); if (pos < buf_size()) { if (!content_callback(buf_data(), pos)) { @@ -3930,17 +4219,21 @@ class MultipartFormDataParser { } break; } - case 4: { // Boundary - if (crlf_.size() > buf_size()) { return true; } + case 4: { // Boundary + if (crlf_.size() > buf_size()) { + return true; + } if (buf_start_with(crlf_)) { buf_erase(crlf_.size()); state_ = 1; } else { - if (dash_crlf_.size() > buf_size()) { return true; } + if (dash_crlf_.size() > buf_size()) { + return true; + } if (buf_start_with(dash_crlf_)) { buf_erase(dash_crlf_.size()); is_valid_ = true; - buf_erase(buf_size()); // Remove epilogue + buf_erase(buf_size()); // Remove epilogue } else { return true; } @@ -3953,18 +4246,22 @@ class MultipartFormDataParser { return true; } -private: + private: void clear_file_info() { file_.name.clear(); file_.filename.clear(); file_.content_type.clear(); } - bool start_with_case_ignore(const std::string &a, - const std::string &b) const { - if (a.size() < b.size()) { return false; } + bool start_with_case_ignore(const std::string& a, + const std::string& b) const { + if (a.size() < b.size()) { + return false; + } for (size_t i = 0; i < b.size(); i++) { - if (::tolower(a[i]) != ::tolower(b[i])) { return false; } + if (::tolower(a[i]) != ::tolower(b[i])) { + return false; + } } return true; } @@ -3981,41 +4278,53 @@ class MultipartFormDataParser { MultipartFormData file_; // Buffer - bool start_with(const std::string &a, size_t spos, size_t epos, - const std::string &b) const { - if (epos - spos < b.size()) { return false; } + bool start_with(const std::string& a, size_t spos, size_t epos, + const std::string& b) const { + if (epos - spos < b.size()) { + return false; + } for (size_t i = 0; i < b.size(); i++) { - if (a[i + spos] != b[i]) { return false; } + if (a[i + spos] != b[i]) { + return false; + } } return true; } size_t buf_size() const { return buf_epos_ - buf_spos_; } - const char *buf_data() const { return &buf_[buf_spos_]; } + const char* buf_data() const { return &buf_[buf_spos_]; } std::string buf_head(size_t l) const { return buf_.substr(buf_spos_, l); } - bool buf_start_with(const std::string &s) const { + bool buf_start_with(const std::string& s) const { return start_with(buf_, buf_spos_, buf_epos_, s); } - size_t buf_find(const std::string &s) const { + size_t buf_find(const std::string& s) const { auto c = s.front(); size_t off = buf_spos_; while (off < buf_epos_) { auto pos = off; while (true) { - if (pos == buf_epos_) { return buf_size(); } - if (buf_[pos] == c) { break; } + if (pos == buf_epos_) { + return buf_size(); + } + if (buf_[pos] == c) { + break; + } pos++; } auto remaining_size = buf_epos_ - pos; - if (s.size() > remaining_size) { return buf_size(); } + if (s.size() > remaining_size) { + return buf_size(); + } - if (start_with(buf_, pos, buf_epos_, s)) { return pos - buf_spos_; } + if (start_with(buf_, pos, buf_epos_, s)) { + return pos - buf_spos_; + } off = pos + 1; } @@ -4023,7 +4332,7 @@ class MultipartFormDataParser { return buf_size(); } - void buf_append(const char *data, size_t n) { + void buf_append(const char* data, size_t n) { auto remaining_size = buf_size(); if (remaining_size > 0 && buf_spos_ > 0) { for (size_t i = 0; i < remaining_size; i++) { @@ -4033,7 +4342,9 @@ class MultipartFormDataParser { buf_spos_ = 0; buf_epos_ = remaining_size; - if (remaining_size + n > buf_.size()) { buf_.resize(remaining_size + n); } + if (remaining_size + n > buf_.size()) { + buf_.resize(remaining_size + n); + } for (size_t i = 0; i < n; i++) { buf_[buf_epos_ + i] = data[i]; @@ -4048,7 +4359,7 @@ class MultipartFormDataParser { size_t buf_epos_ = 0; }; -inline std::string to_lower(const char *beg, const char *end) { +inline std::string to_lower(const char* beg, const char* end) { std::string out; auto it = beg; while (it != end) { @@ -4080,7 +4391,7 @@ inline std::string make_multipart_data_boundary() { return result; } -inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { +inline bool is_multipart_boundary_chars_valid(const std::string& boundary) { auto valid = true; for (size_t i = 0; i < boundary.size(); i++) { auto c = boundary[i]; @@ -4092,13 +4403,12 @@ inline bool is_multipart_boundary_chars_valid(const std::string &boundary) { return valid; } -inline std::string -serialize_multipart_formdata(const MultipartFormDataItems &items, - const std::string &boundary, - std::string &content_type) { +inline std::string serialize_multipart_formdata( + const MultipartFormDataItems& items, const std::string& boundary, + std::string& content_type) { std::string body; - for (const auto &item : items) { + for (const auto& item : items) { body += "--" + boundary + "\r\n"; body += "Content-Disposition: form-data; name=\"" + item.name + "\""; if (!item.filename.empty()) { @@ -4118,9 +4428,8 @@ serialize_multipart_formdata(const MultipartFormDataItems &items, return body; } -inline std::pair -get_range_offset_and_length(const Request &req, size_t content_length, - size_t index) { +inline std::pair get_range_offset_and_length( + const Request& req, size_t content_length, size_t index) { auto r = req.ranges[index]; if (r.first == -1 && r.second == -1) { @@ -4134,7 +4443,9 @@ get_range_offset_and_length(const Request &req, size_t content_length, r.second = slen - 1; } - if (r.second == -1) { r.second = slen - 1; } + if (r.second == -1) { + r.second = slen - 1; + } return std::make_pair(r.first, static_cast(r.second - r.first) + 1); } @@ -4150,9 +4461,9 @@ inline std::string make_content_range_header_field(size_t offset, size_t length, } template -bool process_multipart_ranges_data(const Request &req, Response &res, - const std::string &boundary, - const std::string &content_type, +bool process_multipart_ranges_data(const Request& req, Response& res, + const std::string& boundary, + const std::string& content_type, SToken stoken, CToken ctoken, Content content) { for (size_t i = 0; i < req.ranges.size(); i++) { @@ -4173,7 +4484,9 @@ bool process_multipart_ranges_data(const Request &req, Response &res, stoken(make_content_range_header_field(offset, length, res.body.size())); ctoken("\r\n"); ctoken("\r\n"); - if (!content(offset, length)) { return false; } + if (!content(offset, length)) { + return false; + } ctoken("\r\n"); } @@ -4184,14 +4497,14 @@ bool process_multipart_ranges_data(const Request &req, Response &res, return true; } -inline bool make_multipart_ranges_data(const Request &req, Response &res, - const std::string &boundary, - const std::string &content_type, - std::string &data) { +inline bool make_multipart_ranges_data(const Request& req, Response& res, + const std::string& boundary, + const std::string& content_type, + std::string& data) { return process_multipart_ranges_data( req, res, boundary, content_type, - [&](const std::string &token) { data += token; }, - [&](const std::string &token) { data += token; }, + [&](const std::string& token) { data += token; }, + [&](const std::string& token) { data += token; }, [&](size_t offset, size_t length) { if (offset < res.body.size()) { data += res.body.substr(offset, length); @@ -4201,16 +4514,15 @@ inline bool make_multipart_ranges_data(const Request &req, Response &res, }); } -inline size_t -get_multipart_ranges_data_length(const Request &req, Response &res, - const std::string &boundary, - const std::string &content_type) { +inline size_t get_multipart_ranges_data_length( + const Request& req, Response& res, const std::string& boundary, + const std::string& content_type) { size_t data_length = 0; process_multipart_ranges_data( req, res, boundary, content_type, - [&](const std::string &token) { data_length += token.size(); }, - [&](const std::string &token) { data_length += token.size(); }, + [&](const std::string& token) { data_length += token.size(); }, + [&](const std::string& token) { data_length += token.size(); }, [&](size_t /*offset*/, size_t length) { data_length += length; return true; @@ -4220,24 +4532,23 @@ get_multipart_ranges_data_length(const Request &req, Response &res, } template -inline bool write_multipart_ranges_data(Stream &strm, const Request &req, - Response &res, - const std::string &boundary, - const std::string &content_type, - const T &is_shutting_down) { +inline bool write_multipart_ranges_data(Stream& strm, const Request& req, + Response& res, + const std::string& boundary, + const std::string& content_type, + const T& is_shutting_down) { return process_multipart_ranges_data( req, res, boundary, content_type, - [&](const std::string &token) { strm.write(token); }, - [&](const std::string &token) { strm.write(token); }, + [&](const std::string& token) { strm.write(token); }, + [&](const std::string& token) { strm.write(token); }, [&](size_t offset, size_t length) { return write_content(strm, res.content_provider_, offset, length, is_shutting_down); }); } -inline std::pair -get_range_offset_and_length(const Request &req, const Response &res, - size_t index) { +inline std::pair get_range_offset_and_length( + const Request& req, const Response& res, size_t index) { auto r = req.ranges[index]; if (r.second == -1) { @@ -4247,7 +4558,7 @@ get_range_offset_and_length(const Request &req, const Response &res, return std::make_pair(r.first, r.second - r.first + 1); } -inline bool expect_content(const Request &req) { +inline bool expect_content(const Request& req) { if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || req.method == "PRI" || req.method == "DELETE") { return true; @@ -4256,17 +4567,19 @@ inline bool expect_content(const Request &req) { return false; } -inline bool has_crlf(const std::string &s) { +inline bool has_crlf(const std::string& s) { auto p = s.c_str(); while (*p) { - if (*p == '\r' || *p == '\n') { return true; } + if (*p == '\r' || *p == '\n') { + return true; + } p++; } return false; } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline std::string message_digest(const std::string &s, const EVP_MD *algo) { +inline std::string message_digest(const std::string& s, const EVP_MD* algo) { auto context = std::unique_ptr( EVP_MD_CTX_new(), EVP_MD_CTX_free); @@ -4280,21 +4593,21 @@ inline std::string message_digest(const std::string &s, const EVP_MD *algo) { std::stringstream ss; for (auto i = 0u; i < hash_length; ++i) { ss << std::hex << std::setw(2) << std::setfill('0') - << (unsigned int)hash[i]; + << (unsigned int) hash[i]; } return ss.str(); } -inline std::string MD5(const std::string &s) { +inline std::string MD5(const std::string& s) { return message_digest(s, EVP_md5()); } -inline std::string SHA_256(const std::string &s) { +inline std::string SHA_256(const std::string& s) { return message_digest(s, EVP_sha256()); } -inline std::string SHA_512(const std::string &s) { +inline std::string SHA_512(const std::string& s) { return message_digest(s, EVP_sha512()); } #endif @@ -4303,16 +4616,18 @@ inline std::string SHA_512(const std::string &s) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT // NOTE: This code came up with the following stackoverflow post: // https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store -inline bool load_system_certs_on_windows(X509_STORE *store) { - auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); +inline bool load_system_certs_on_windows(X509_STORE* store) { + auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY) NULL, L"ROOT"); - if (!hStore) { return false; } + if (!hStore) { + return false; + } PCCERT_CONTEXT pContext = NULL; while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != nullptr) { auto encoded_cert = - static_cast(pContext->pbCertEncoded); + static_cast(pContext->pbCertEncoded); auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded); if (x509) { @@ -4329,14 +4644,16 @@ inline bool load_system_certs_on_windows(X509_STORE *store) { #endif class WSInit { -public: + public: WSInit() { WSADATA wsaData; - if (WSAStartup(0x0002, &wsaData) == 0) is_valid_ = true; + if (WSAStartup(0x0002, &wsaData) == 0) + is_valid_ = true; } ~WSInit() { - if (is_valid_) WSACleanup(); + if (is_valid_) + WSACleanup(); } bool is_valid_ = false; @@ -4347,9 +4664,9 @@ static WSInit wsinit_; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT inline std::pair make_digest_authentication_header( - const Request &req, const std::map &auth, - size_t cnonce_count, const std::string &cnonce, const std::string &username, - const std::string &password, bool is_proxy = false) { + const Request& req, const std::map& auth, + size_t cnonce_count, const std::string& cnonce, const std::string& username, + const std::string& password, bool is_proxy = false) { std::string nc; { std::stringstream ss; @@ -4370,7 +4687,9 @@ inline std::pair make_digest_authentication_header( } std::string algo = "MD5"; - if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); } + if (auth.find("algorithm") != auth.end()) { + algo = auth.at("algorithm"); + } std::string response; { @@ -4381,7 +4700,9 @@ inline std::pair make_digest_authentication_header( auto A1 = username + ":" + auth.at("realm") + ":" + password; auto A2 = req.method + ":" + req.path; - if (qop == "auth-int") { A2 += ":" + H(req.body); } + if (qop == "auth-int") { + A2 += ":" + H(req.body); + } if (qop.empty()) { response = H(H(A1) + ":" + auth.at("nonce") + ":" + H(A2)); @@ -4407,8 +4728,8 @@ inline std::pair make_digest_authentication_header( } #endif -inline bool parse_www_authenticate(const Response &res, - std::map &auth, +inline bool parse_www_authenticate(const Response& res, + std::map& auth, bool is_proxy) { auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; if (res.has_header(auth_key)) { @@ -4443,9 +4764,10 @@ inline bool parse_www_authenticate(const Response &res, // https://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c/440240#answer-440240 inline std::string random_string(size_t length) { auto randchar = []() -> char { - const char charset[] = "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; + const char charset[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; const size_t max_index = (sizeof(charset) - 1); return charset[static_cast(std::rand()) % max_index]; }; @@ -4455,32 +4777,34 @@ inline std::string random_string(size_t length) { } class ContentProviderAdapter { -public: + public: explicit ContentProviderAdapter( - ContentProviderWithoutLength &&content_provider) + ContentProviderWithoutLength&& content_provider) : content_provider_(content_provider) {} - bool operator()(size_t offset, size_t, DataSink &sink) { + bool operator()(size_t offset, size_t, DataSink& sink) { return content_provider_(offset, sink); } -private: + private: ContentProviderWithoutLength content_provider_; }; -} // namespace detail +} // namespace detail -inline std::string hosted_at(const std::string &hostname) { +inline std::string hosted_at(const std::string& hostname) { std::vector addrs; hosted_at(hostname, addrs); - if (addrs.empty()) { return std::string(); } + if (addrs.empty()) { + return std::string(); + } return addrs[0]; } -inline void hosted_at(const std::string &hostname, - std::vector &addrs) { +inline void hosted_at(const std::string& hostname, + std::vector& addrs) { struct addrinfo hints; - struct addrinfo *result; + struct addrinfo* result; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; @@ -4495,8 +4819,7 @@ inline void hosted_at(const std::string &hostname, } for (auto rp = result; rp; rp = rp->ai_next) { - const auto &addr = - *reinterpret_cast(rp->ai_addr); + const auto& addr = *reinterpret_cast(rp->ai_addr); std::string ip; int dummy = -1; if (detail::get_remote_ip_and_port(addr, sizeof(struct sockaddr_storage), @@ -4508,8 +4831,8 @@ inline void hosted_at(const std::string &hostname, freeaddrinfo(result); } -inline std::string append_query_params(const std::string &path, - const Params ¶ms) { +inline std::string append_query_params(const std::string& path, + const Params& params) { std::string path_with_query = path; const static std::regex re("[^?]+\\?.*"); auto delm = std::regex_match(path, re) ? '&' : '?'; @@ -4522,25 +4845,30 @@ inline std::pair make_range_header(Ranges ranges) { std::string field = "bytes="; auto i = 0; for (auto r : ranges) { - if (i != 0) { field += ", "; } - if (r.first != -1) { field += std::to_string(r.first); } + if (i != 0) { + field += ", "; + } + if (r.first != -1) { + field += std::to_string(r.first); + } field += '-'; - if (r.second != -1) { field += std::to_string(r.second); } + if (r.second != -1) { + field += std::to_string(r.second); + } i++; } return std::make_pair("Range", std::move(field)); } -inline std::pair -make_basic_authentication_header(const std::string &username, - const std::string &password, bool is_proxy) { +inline std::pair make_basic_authentication_header( + const std::string& username, const std::string& password, bool is_proxy) { auto field = "Basic " + detail::base64_encode(username + ":" + password); auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; return std::make_pair(key, std::move(field)); } inline std::pair -make_bearer_token_authentication_header(const std::string &token, +make_bearer_token_authentication_header(const std::string& token, bool is_proxy = false) { auto field = "Bearer " + token; auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; @@ -4548,83 +4876,87 @@ make_bearer_token_authentication_header(const std::string &token, } // Request implementation -inline bool Request::has_header(const std::string &key) const { +inline bool Request::has_header(const std::string& key) const { return detail::has_header(headers, key); } -inline std::string Request::get_header_value(const std::string &key, +inline std::string Request::get_header_value(const std::string& key, size_t id) const { return detail::get_header_value(headers, key, id, ""); } -inline size_t Request::get_header_value_count(const std::string &key) const { +inline size_t Request::get_header_value_count(const std::string& key) const { auto r = headers.equal_range(key); return static_cast(std::distance(r.first, r.second)); } -inline void Request::set_header(const std::string &key, - const std::string &val) { +inline void Request::set_header(const std::string& key, + const std::string& val) { if (!detail::has_crlf(key) && !detail::has_crlf(val)) { headers.emplace(key, val); } } -inline bool Request::has_param(const std::string &key) const { +inline bool Request::has_param(const std::string& key) const { return params.find(key) != params.end(); } -inline std::string Request::get_param_value(const std::string &key, +inline std::string Request::get_param_value(const std::string& key, size_t id) const { auto rng = params.equal_range(key); auto it = rng.first; std::advance(it, static_cast(id)); - if (it != rng.second) { return it->second; } + if (it != rng.second) { + return it->second; + } return std::string(); } -inline size_t Request::get_param_value_count(const std::string &key) const { +inline size_t Request::get_param_value_count(const std::string& key) const { auto r = params.equal_range(key); return static_cast(std::distance(r.first, r.second)); } inline bool Request::is_multipart_form_data() const { - const auto &content_type = get_header_value("Content-Type"); + const auto& content_type = get_header_value("Content-Type"); return !content_type.rfind("multipart/form-data", 0); } -inline bool Request::has_file(const std::string &key) const { +inline bool Request::has_file(const std::string& key) const { return files.find(key) != files.end(); } -inline MultipartFormData Request::get_file_value(const std::string &key) const { +inline MultipartFormData Request::get_file_value(const std::string& key) const { auto it = files.find(key); - if (it != files.end()) { return it->second; } + if (it != files.end()) { + return it->second; + } return MultipartFormData(); } // Response implementation -inline bool Response::has_header(const std::string &key) const { +inline bool Response::has_header(const std::string& key) const { return headers.find(key) != headers.end(); } -inline std::string Response::get_header_value(const std::string &key, +inline std::string Response::get_header_value(const std::string& key, size_t id) const { return detail::get_header_value(headers, key, id, ""); } -inline size_t Response::get_header_value_count(const std::string &key) const { +inline size_t Response::get_header_value_count(const std::string& key) const { auto r = headers.equal_range(key); return static_cast(std::distance(r.first, r.second)); } -inline void Response::set_header(const std::string &key, - const std::string &val) { +inline void Response::set_header(const std::string& key, + const std::string& val) { if (!detail::has_crlf(key) && !detail::has_crlf(val)) { headers.emplace(key, val); } } -inline void Response::set_redirect(const std::string &url, int stat) { +inline void Response::set_redirect(const std::string& url, int stat) { if (!detail::has_crlf(url)) { set_header("Location", url); if (300 <= stat && stat < 400) { @@ -4635,8 +4967,8 @@ inline void Response::set_redirect(const std::string &url, int stat) { } } -inline void Response::set_content(const char *s, size_t n, - const std::string &content_type) { +inline void Response::set_content(const char* s, size_t n, + const std::string& content_type) { body.assign(s, n); auto rng = headers.equal_range("Content-Type"); @@ -4644,13 +4976,13 @@ inline void Response::set_content(const char *s, size_t n, set_header("Content-Type", content_type); } -inline void Response::set_content(const std::string &s, - const std::string &content_type) { +inline void Response::set_content(const std::string& s, + const std::string& content_type) { set_content(s.data(), s.size(), content_type); } inline void Response::set_content_provider( - size_t in_length, const std::string &content_type, ContentProvider provider, + size_t in_length, const std::string& content_type, ContentProvider provider, ContentProviderResourceReleaser resource_releaser) { assert(in_length > 0); set_header("Content-Type", content_type); @@ -4661,7 +4993,7 @@ inline void Response::set_content_provider( } inline void Response::set_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, + const std::string& content_type, ContentProviderWithoutLength provider, ContentProviderResourceReleaser resource_releaser) { set_header("Content-Type", content_type); content_length_ = 0; @@ -4671,7 +5003,7 @@ inline void Response::set_content_provider( } inline void Response::set_chunked_content_provider( - const std::string &content_type, ContentProviderWithoutLength provider, + const std::string& content_type, ContentProviderWithoutLength provider, ContentProviderResourceReleaser resource_releaser) { set_header("Content-Type", content_type); content_length_ = 0; @@ -4681,27 +5013,27 @@ inline void Response::set_chunked_content_provider( } // Result implementation -inline bool Result::has_request_header(const std::string &key) const { +inline bool Result::has_request_header(const std::string& key) const { return request_headers_.find(key) != request_headers_.end(); } -inline std::string Result::get_request_header_value(const std::string &key, +inline std::string Result::get_request_header_value(const std::string& key, size_t id) const { return detail::get_header_value(request_headers_, key, id, ""); } -inline size_t -Result::get_request_header_value_count(const std::string &key) const { +inline size_t Result::get_request_header_value_count( + const std::string& key) const { auto r = request_headers_.equal_range(key); return static_cast(std::distance(r.first, r.second)); } // Stream implementation -inline ssize_t Stream::write(const char *ptr) { +inline ssize_t Stream::write(const char* ptr) { return write(ptr, strlen(ptr)); } -inline ssize_t Stream::write(const std::string &s) { +inline ssize_t Stream::write(const std::string& s) { return write(s.data(), s.size()); } @@ -4712,10 +5044,12 @@ inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec) - : sock_(sock), read_timeout_sec_(read_timeout_sec), + : sock_(sock), + read_timeout_sec_(read_timeout_sec), read_timeout_usec_(read_timeout_usec), write_timeout_sec_(write_timeout_sec), - write_timeout_usec_(write_timeout_usec), read_buff_(read_buff_size_, 0) {} + write_timeout_usec_(write_timeout_usec), + read_buff_(read_buff_size_, 0) {} inline SocketStream::~SocketStream() {} @@ -4725,10 +5059,10 @@ inline bool SocketStream::is_readable() const { inline bool SocketStream::is_writable() const { return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_); + is_socket_alive(sock_); } -inline ssize_t SocketStream::read(char *ptr, size_t size) { +inline ssize_t SocketStream::read(char* ptr, size_t size) { #ifdef _WIN32 size = (std::min)(size, static_cast((std::numeric_limits::max)())); @@ -4750,7 +5084,9 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) { } } - if (!is_readable()) { return -1; } + if (!is_readable()) { + return -1; + } read_buff_off_ = 0; read_buff_content_size_ = 0; @@ -4774,8 +5110,10 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) { } } -inline ssize_t SocketStream::write(const char *ptr, size_t size) { - if (!is_writable()) { return -1; } +inline ssize_t SocketStream::write(const char* ptr, size_t size) { + if (!is_writable()) { + return -1; + } #if defined(_WIN32) && !defined(_WIN64) size = @@ -4785,8 +5123,8 @@ inline ssize_t SocketStream::write(const char *ptr, size_t size) { return send_socket(sock_, ptr, size, CPPHTTPLIB_SEND_FLAGS); } -inline void SocketStream::get_remote_ip_and_port(std::string &ip, - int &port) const { +inline void SocketStream::get_remote_ip_and_port(std::string& ip, + int& port) const { return detail::get_remote_ip_and_port(sock_, ip, port); } @@ -4797,7 +5135,7 @@ inline bool BufferStream::is_readable() const { return true; } inline bool BufferStream::is_writable() const { return true; } -inline ssize_t BufferStream::read(char *ptr, size_t size) { +inline ssize_t BufferStream::read(char* ptr, size_t size) { #if defined(_MSC_VER) && _MSC_VER < 1910 auto len_read = buffer._Copy_s(ptr, size, size, position); #else @@ -4807,25 +5145,26 @@ inline ssize_t BufferStream::read(char *ptr, size_t size) { return static_cast(len_read); } -inline ssize_t BufferStream::write(const char *ptr, size_t size) { +inline ssize_t BufferStream::write(const char* ptr, size_t size) { buffer.append(ptr, size); return static_cast(size); } -inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/, - int & /*port*/) const {} +inline void BufferStream::get_remote_ip_and_port(std::string& /*ip*/, + int& /*port*/) const {} inline socket_t BufferStream::socket() const { return 0; } -inline const std::string &BufferStream::get_buffer() const { return buffer; } +inline const std::string& BufferStream::get_buffer() const { return buffer; } -} // namespace detail +} // namespace detail // HTTP server implementation inline Server::Server() : new_task_queue( [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }), - svr_sock_(INVALID_SOCKET), is_running_(false) { + svr_sock_(INVALID_SOCKET), + is_running_(false) { #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); #endif @@ -4833,77 +5172,77 @@ inline Server::Server() inline Server::~Server() {} -inline Server &Server::Get(const std::string &pattern, Handler handler) { +inline Server& Server::Get(const std::string& pattern, Handler handler) { get_handlers_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline Server &Server::Post(const std::string &pattern, Handler handler) { +inline Server& Server::Post(const std::string& pattern, Handler handler) { post_handlers_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline Server &Server::Post(const std::string &pattern, +inline Server& Server::Post(const std::string& pattern, HandlerWithContentReader handler) { post_handlers_for_content_reader_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline Server &Server::Put(const std::string &pattern, Handler handler) { +inline Server& Server::Put(const std::string& pattern, Handler handler) { put_handlers_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline Server &Server::Put(const std::string &pattern, +inline Server& Server::Put(const std::string& pattern, HandlerWithContentReader handler) { put_handlers_for_content_reader_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline Server &Server::Patch(const std::string &pattern, Handler handler) { +inline Server& Server::Patch(const std::string& pattern, Handler handler) { patch_handlers_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline Server &Server::Patch(const std::string &pattern, +inline Server& Server::Patch(const std::string& pattern, HandlerWithContentReader handler) { patch_handlers_for_content_reader_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline Server &Server::Delete(const std::string &pattern, Handler handler) { +inline Server& Server::Delete(const std::string& pattern, Handler handler) { delete_handlers_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline Server &Server::Delete(const std::string &pattern, +inline Server& Server::Delete(const std::string& pattern, HandlerWithContentReader handler) { delete_handlers_for_content_reader_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline Server &Server::Options(const std::string &pattern, Handler handler) { +inline Server& Server::Options(const std::string& pattern, Handler handler) { options_handlers_.push_back( std::make_pair(std::regex(pattern), std::move(handler))); return *this; } -inline bool Server::set_base_dir(const std::string &dir, - const std::string &mount_point) { +inline bool Server::set_base_dir(const std::string& dir, + const std::string& mount_point) { return set_mount_point(mount_point, dir); } -inline bool Server::set_mount_point(const std::string &mount_point, - const std::string &dir, Headers headers) { +inline bool Server::set_mount_point(const std::string& mount_point, + const std::string& dir, Headers headers) { if (detail::is_dir(dir)) { std::string mnt = !mount_point.empty() ? mount_point : "/"; if (!mnt.empty() && mnt[0] == '/') { @@ -4914,7 +5253,7 @@ inline bool Server::set_mount_point(const std::string &mount_point, return false; } -inline bool Server::remove_mount_point(const std::string &mount_point) { +inline bool Server::remove_mount_point(const std::string& mount_point) { for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) { if (it->mount_point == mount_point) { base_dirs_.erase(it); @@ -4924,123 +5263,123 @@ inline bool Server::remove_mount_point(const std::string &mount_point) { return false; } -inline Server & -Server::set_file_extension_and_mimetype_mapping(const std::string &ext, - const std::string &mime) { +inline Server& Server::set_file_extension_and_mimetype_mapping( + const std::string& ext, const std::string& mime) { file_extension_and_mimetype_map_[ext] = mime; return *this; } -inline Server &Server::set_file_request_handler(Handler handler) { +inline Server& Server::set_file_request_handler(Handler handler) { file_request_handler_ = std::move(handler); return *this; } -inline Server &Server::set_error_handler(HandlerWithResponse handler) { +inline Server& Server::set_error_handler(HandlerWithResponse handler) { error_handler_ = std::move(handler); return *this; } -inline Server &Server::set_error_handler(Handler handler) { - error_handler_ = [handler](const Request &req, Response &res) { +inline Server& Server::set_error_handler(Handler handler) { + error_handler_ = [handler](const Request& req, Response& res) { handler(req, res); return HandlerResponse::Handled; }; return *this; } -inline Server &Server::set_exception_handler(ExceptionHandler handler) { +inline Server& Server::set_exception_handler(ExceptionHandler handler) { exception_handler_ = std::move(handler); return *this; } -inline Server &Server::set_pre_routing_handler(HandlerWithResponse handler) { +inline Server& Server::set_pre_routing_handler(HandlerWithResponse handler) { pre_routing_handler_ = std::move(handler); return *this; } -inline Server &Server::set_post_routing_handler(Handler handler) { +inline Server& Server::set_post_routing_handler(Handler handler) { post_routing_handler_ = std::move(handler); return *this; } -inline Server &Server::set_logger(Logger logger) { +inline Server& Server::set_logger(Logger logger) { logger_ = std::move(logger); return *this; } -inline Server & -Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) { +inline Server& Server::set_expect_100_continue_handler( + Expect100ContinueHandler handler) { expect_100_continue_handler_ = std::move(handler); return *this; } -inline Server &Server::set_address_family(int family) { +inline Server& Server::set_address_family(int family) { address_family_ = family; return *this; } -inline Server &Server::set_tcp_nodelay(bool on) { +inline Server& Server::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; return *this; } -inline Server &Server::set_socket_options(SocketOptions socket_options) { +inline Server& Server::set_socket_options(SocketOptions socket_options) { socket_options_ = std::move(socket_options); return *this; } -inline Server &Server::set_default_headers(Headers headers) { +inline Server& Server::set_default_headers(Headers headers) { default_headers_ = std::move(headers); return *this; } -inline Server &Server::set_keep_alive_max_count(size_t count) { +inline Server& Server::set_keep_alive_max_count(size_t count) { keep_alive_max_count_ = count; return *this; } -inline Server &Server::set_keep_alive_timeout(time_t sec) { +inline Server& Server::set_keep_alive_timeout(time_t sec) { keep_alive_timeout_sec_ = sec; return *this; } -inline Server &Server::set_read_timeout(time_t sec, time_t usec) { +inline Server& Server::set_read_timeout(time_t sec, time_t usec) { read_timeout_sec_ = sec; read_timeout_usec_ = usec; return *this; } -inline Server &Server::set_write_timeout(time_t sec, time_t usec) { +inline Server& Server::set_write_timeout(time_t sec, time_t usec) { write_timeout_sec_ = sec; write_timeout_usec_ = usec; return *this; } -inline Server &Server::set_idle_interval(time_t sec, time_t usec) { +inline Server& Server::set_idle_interval(time_t sec, time_t usec) { idle_interval_sec_ = sec; idle_interval_usec_ = usec; return *this; } -inline Server &Server::set_payload_max_length(size_t length) { +inline Server& Server::set_payload_max_length(size_t length) { payload_max_length_ = length; return *this; } -inline bool Server::bind_to_port(const std::string &host, int port, +inline bool Server::bind_to_port(const std::string& host, int port, int socket_flags) { - if (bind_internal(host, port, socket_flags) < 0) return false; + if (bind_internal(host, port, socket_flags) < 0) + return false; return true; } -inline int Server::bind_to_any_port(const std::string &host, int socket_flags) { +inline int Server::bind_to_any_port(const std::string& host, int socket_flags) { return bind_internal(host, 0, socket_flags); } inline bool Server::listen_after_bind() { return listen_internal(); } -inline bool Server::listen(const std::string &host, int port, +inline bool Server::listen(const std::string& host, int port, int socket_flags) { return bind_to_port(host, port, socket_flags) && listen_internal(); } @@ -5056,34 +5395,49 @@ inline void Server::stop() { } } -inline bool Server::parse_request_line(const char *s, Request &req) { +inline bool Server::parse_request_line(const char* s, Request& req) { auto len = strlen(s); - if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { return false; } + if (len < 2 || s[len - 2] != '\r' || s[len - 1] != '\n') { + return false; + } len -= 2; { size_t count = 0; - detail::split(s, s + len, ' ', [&](const char *b, const char *e) { + detail::split(s, s + len, ' ', [&](const char* b, const char* e) { switch (count) { - case 0: req.method = std::string(b, e); break; - case 1: req.target = std::string(b, e); break; - case 2: req.version = std::string(b, e); break; - default: break; + case 0: + req.method = std::string(b, e); + break; + case 1: + req.target = std::string(b, e); + break; + case 2: + req.version = std::string(b, e); + break; + default: + break; } count++; }); - if (count != 3) { return false; } + if (count != 3) { + return false; + } } static const std::set methods{ "GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", "PATCH", "PRI"}; - if (methods.find(req.method) == methods.end()) { return false; } + if (methods.find(req.method) == methods.end()) { + return false; + } - if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { return false; } + if (req.version != "HTTP/1.1" && req.version != "HTTP/1.0") { + return false; + } { // Skip URL fragment @@ -5097,7 +5451,7 @@ inline bool Server::parse_request_line(const char *s, Request &req) { size_t count = 0; detail::split(req.target.data(), req.target.data() + req.target.size(), '?', - [&](const char *b, const char *e) { + [&](const char* b, const char* e) { switch (count) { case 0: req.path = detail::decode_url(std::string(b, e), false); @@ -5108,31 +5462,34 @@ inline bool Server::parse_request_line(const char *s, Request &req) { } break; } - default: break; + default: + break; } count++; }); - if (count > 2) { return false; } + if (count > 2) { + return false; + } } return true; } -inline bool Server::write_response(Stream &strm, bool close_connection, - const Request &req, Response &res) { +inline bool Server::write_response(Stream& strm, bool close_connection, + const Request& req, Response& res) { return write_response_core(strm, close_connection, req, res, false); } -inline bool Server::write_response_with_content(Stream &strm, +inline bool Server::write_response_with_content(Stream& strm, bool close_connection, - const Request &req, - Response &res) { + const Request& req, + Response& res) { return write_response_core(strm, close_connection, req, res, true); } -inline bool Server::write_response_core(Stream &strm, bool close_connection, - const Request &req, Response &res, +inline bool Server::write_response_core(Stream& strm, bool close_connection, + const Request& req, Response& res, bool need_apply_ranges) { assert(res.status != -1); @@ -5143,7 +5500,9 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, std::string content_type; std::string boundary; - if (need_apply_ranges) { apply_ranges(req, res, content_type, boundary); } + if (need_apply_ranges) { + apply_ranges(req, res, content_type, boundary); + } // Prepare additional headers if (close_connection || req.get_header_value("Connection") == "close") { @@ -5169,7 +5528,9 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, res.set_header("Accept-Ranges", "bytes"); } - if (post_routing_handler_) { post_routing_handler_(req, res); } + if (post_routing_handler_) { + post_routing_handler_(req, res); + } // Response line and headers { @@ -5180,10 +5541,12 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, return false; } - if (!detail::write_headers(bstrm, res.headers)) { return false; } + if (!detail::write_headers(bstrm, res.headers)) { + return false; + } // Flush buffer - auto &data = bstrm.get_buffer(); + auto& data = bstrm.get_buffer(); detail::write_data(strm, data.data(), data.size()); } @@ -5205,15 +5568,16 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection, } // Log - if (logger_) { logger_(req, res); } + if (logger_) { + logger_(req, res); + } return ret; } -inline bool -Server::write_content_with_provider(Stream &strm, const Request &req, - Response &res, const std::string &boundary, - const std::string &content_type) { +inline bool Server::write_content_with_provider( + Stream& strm, const Request& req, Response& res, + const std::string& boundary, const std::string& content_type) { auto is_shutting_down = [this]() { return this->svr_sock_ == INVALID_SOCKET; }; @@ -5260,35 +5624,39 @@ Server::write_content_with_provider(Stream &strm, const Request &req, } } -inline bool Server::read_content(Stream &strm, Request &req, Response &res) { +inline bool Server::read_content(Stream& strm, Request& req, Response& res) { MultipartFormDataMap::iterator cur; auto file_count = 0; if (read_content_core( strm, req, res, // Regular - [&](const char *buf, size_t n) { - if (req.body.size() + n > req.body.max_size()) { return false; } + [&](const char* buf, size_t n) { + if (req.body.size() + n > req.body.max_size()) { + return false; + } req.body.append(buf, n); return true; }, // Multipart - [&](const MultipartFormData &file) { + [&](const MultipartFormData& file) { if (file_count++ == CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT) { return false; } cur = req.files.emplace(file.name, file); return true; }, - [&](const char *buf, size_t n) { - auto &content = cur->second.content; - if (content.size() + n > content.max_size()) { return false; } + [&](const char* buf, size_t n) { + auto& content = cur->second.content; + if (content.size() + n > content.max_size()) { + return false; + } content.append(buf, n); return true; })) { - const auto &content_type = req.get_header_value("Content-Type"); + const auto& content_type = req.get_header_value("Content-Type"); if (!content_type.find("application/x-www-form-urlencoded")) { if (req.body.size() > CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH) { - res.status = 413; // NOTE: should be 414? + res.status = 413; // NOTE: should be 414? return false; } detail::parse_query_text(req.body, req.params); @@ -5299,7 +5667,7 @@ inline bool Server::read_content(Stream &strm, Request &req, Response &res) { } inline bool Server::read_content_with_content_receiver( - Stream &strm, Request &req, Response &res, ContentReceiver receiver, + Stream& strm, Request& req, Response& res, ContentReceiver receiver, MultipartContentHeader multipart_header, ContentReceiver multipart_receiver) { return read_content_core(strm, req, res, std::move(receiver), @@ -5307,7 +5675,7 @@ inline bool Server::read_content_with_content_receiver( std::move(multipart_receiver)); } -inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, +inline bool Server::read_content_core(Stream& strm, Request& req, Response& res, ContentReceiver receiver, MultipartContentHeader mulitpart_header, ContentReceiver multipart_receiver) { @@ -5315,7 +5683,7 @@ inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, ContentReceiverWithProgress out; if (req.is_multipart_form_data()) { - const auto &content_type = req.get_header_value("Content-Type"); + const auto& content_type = req.get_header_value("Content-Type"); std::string boundary; if (!detail::parse_multipart_boundary(content_type, boundary)) { res.status = 400; @@ -5323,7 +5691,7 @@ inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, } multipart_form_data_parser.set_boundary(std::move(boundary)); - out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { + out = [&](const char* buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { /* For debug size_t pos = 0; while (pos < n) { @@ -5339,7 +5707,7 @@ inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, mulitpart_header); }; } else { - out = [receiver](const char *buf, size_t n, uint64_t /*off*/, + out = [receiver](const char* buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { return receiver(buf, n); }; } @@ -5362,22 +5730,26 @@ inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, return true; } -inline bool Server::handle_file_request(const Request &req, Response &res, +inline bool Server::handle_file_request(const Request& req, Response& res, bool head) { - for (const auto &entry : base_dirs_) { + for (const auto& entry : base_dirs_) { // Prefix match if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { std::string sub_path = "/" + req.path.substr(entry.mount_point.size()); if (detail::is_valid_path(sub_path)) { auto path = entry.base_dir + sub_path; - if (path.back() == '/') { path += "index.html"; } + if (path.back() == '/') { + path += "index.html"; + } if (detail::is_file(path)) { detail::read_file(path, res.body); auto type = detail::find_content_type(path, file_extension_and_mimetype_map_); - if (type) { res.set_header("Content-Type", type); } - for (const auto &kv : entry.headers) { + if (type) { + res.set_header("Content-Type", type); + } + for (const auto& kv : entry.headers) { res.set_header(kv.first.c_str(), kv.second); } res.status = req.has_header("Range") ? 206 : 200; @@ -5392,40 +5764,45 @@ inline bool Server::handle_file_request(const Request &req, Response &res, return false; } -inline socket_t -Server::create_server_socket(const std::string &host, int port, - int socket_flags, - SocketOptions socket_options) const { +inline socket_t Server::create_server_socket( + const std::string& host, int port, int socket_flags, + SocketOptions socket_options) const { return detail::create_socket( host, std::string(), port, address_family_, socket_flags, tcp_nodelay_, std::move(socket_options), - [](socket_t sock, struct addrinfo &ai) -> bool { + [](socket_t sock, struct addrinfo& ai) -> bool { if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { return false; } - if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { return false; } + if (::listen(sock, CPPHTTPLIB_LISTEN_BACKLOG)) { + return false; + } return true; }); } -inline int Server::bind_internal(const std::string &host, int port, +inline int Server::bind_internal(const std::string& host, int port, int socket_flags) { - if (!is_valid()) { return -1; } + if (!is_valid()) { + return -1; + } svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_); - if (svr_sock_ == INVALID_SOCKET) { return -1; } + if (svr_sock_ == INVALID_SOCKET) { + return -1; + } if (port == 0) { struct sockaddr_storage addr; socklen_t addr_len = sizeof(addr); - if (getsockname(svr_sock_, reinterpret_cast(&addr), + if (getsockname(svr_sock_, reinterpret_cast(&addr), &addr_len) == -1) { return -1; } if (addr.ss_family == AF_INET) { - return ntohs(reinterpret_cast(&addr)->sin_port); + return ntohs(reinterpret_cast(&addr)->sin_port); } else if (addr.ss_family == AF_INET6) { - return ntohs(reinterpret_cast(&addr)->sin6_port); + return ntohs(reinterpret_cast(&addr)->sin6_port); } else { return -1; } @@ -5447,7 +5824,7 @@ inline bool Server::listen_internal() { #endif auto val = detail::select_read(svr_sock_, idle_interval_sec_, idle_interval_usec_); - if (val == 0) { // Timeout + if (val == 0) { // Timeout task_queue->on_idle(); continue; } @@ -5467,7 +5844,7 @@ inline bool Server::listen_internal() { detail::close_socket(svr_sock_); ret = false; } else { - ; // The server socket was closed by user. + ; // The server socket was closed by user. } break; } @@ -5476,27 +5853,26 @@ inline bool Server::listen_internal() { #ifdef _WIN32 auto timeout = static_cast(read_timeout_sec_ * 1000 + read_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*) &timeout, sizeof(timeout)); #else timeval tv; tv.tv_sec = static_cast(read_timeout_sec_); tv.tv_usec = static_cast(read_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*) &tv, sizeof(tv)); #endif } { - #ifdef _WIN32 auto timeout = static_cast(write_timeout_sec_ * 1000 + write_timeout_usec_ / 1000); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*) &timeout, sizeof(timeout)); #else timeval tv; tv.tv_sec = static_cast(write_timeout_sec_); tv.tv_usec = static_cast(write_timeout_usec_); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv)); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*) &tv, sizeof(tv)); #endif } @@ -5514,7 +5890,7 @@ inline bool Server::listen_internal() { return ret; } -inline bool Server::routing(Request &req, Response &res, Stream &strm) { +inline bool Server::routing(Request& req, Response& res, Stream& strm) { if (pre_routing_handler_ && pre_routing_handler_(req, res) == HandlerResponse::Handled) { return true; @@ -5569,7 +5945,9 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { } // Read content into `req.body` - if (!read_content(strm, req, res)) { return false; } + if (!read_content(strm, req, res)) { + return false; + } } // Regular handler @@ -5591,11 +5969,11 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) { return false; } -inline bool Server::dispatch_request(Request &req, Response &res, - const Handlers &handlers) { - for (const auto &x : handlers) { - const auto &pattern = x.first; - const auto &handler = x.second; +inline bool Server::dispatch_request(Request& req, Response& res, + const Handlers& handlers) { + for (const auto& x : handlers) { + const auto& pattern = x.first; + const auto& handler = x.second; if (std::regex_match(req.path, req.matches, pattern)) { handler(req, res); @@ -5605,9 +5983,9 @@ inline bool Server::dispatch_request(Request &req, Response &res, return false; } -inline void Server::apply_ranges(const Request &req, Response &res, - std::string &content_type, - std::string &boundary) { +inline void Server::apply_ranges(const Request& req, Response& res, + std::string& content_type, + std::string& boundary) { if (req.ranges.size() > 1) { boundary = detail::make_multipart_data_boundary(); @@ -5700,7 +6078,7 @@ inline void Server::apply_ranges(const Request &req, Response &res, if (compressor) { std::string compressed; if (compressor->compress(res.body.data(), res.body.size(), true, - [&](const char *data, size_t data_len) { + [&](const char* data, size_t data_len) { compressed.append(data, data_len); return true; })) { @@ -5716,11 +6094,11 @@ inline void Server::apply_ranges(const Request &req, Response &res, } inline bool Server::dispatch_request_for_content_reader( - Request &req, Response &res, ContentReader content_reader, - const HandlersForContentReader &handlers) { - for (const auto &x : handlers) { - const auto &pattern = x.first; - const auto &handler = x.second; + Request& req, Response& res, ContentReader content_reader, + const HandlersForContentReader& handlers) { + for (const auto& x : handlers) { + const auto& pattern = x.first; + const auto& handler = x.second; if (std::regex_match(req.path, req.matches, pattern)) { handler(req, res, content_reader); @@ -5730,23 +6108,24 @@ inline bool Server::dispatch_request_for_content_reader( return false; } -inline bool -Server::process_request(Stream &strm, bool close_connection, - bool &connection_closed, - const std::function &setup_request) { +inline bool Server::process_request( + Stream& strm, bool close_connection, bool& connection_closed, + const std::function& setup_request) { std::array buf{}; detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); // Connection has been closed on client - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { + return false; + } Request req; Response res; res.version = "HTTP/1.1"; - for (const auto &header : default_headers_) { + for (const auto& header : default_headers_) { if (res.headers.find(header.first) == res.headers.end()) { res.headers.insert(header); } @@ -5795,14 +6174,16 @@ Server::process_request(Stream &strm, bool close_connection, req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); if (req.has_header("Range")) { - const auto &range_header_value = req.get_header_value("Range"); + const auto& range_header_value = req.get_header_value("Range"); if (!detail::parse_range_header(range_header_value, req.ranges)) { res.status = 416; return write_response(strm, close_connection, req, res); } } - if (setup_request) { setup_request(req); } + if (setup_request) { + setup_request(req); + } if (req.get_header_value("Expect") == "100-continue") { auto status = 100; @@ -5815,7 +6196,8 @@ Server::process_request(Stream &strm, bool close_connection, strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, detail::status_message(status)); break; - default: return write_response(strm, close_connection, req, res); + default: + return write_response(strm, close_connection, req, res); } } @@ -5826,7 +6208,7 @@ Server::process_request(Stream &strm, bool close_connection, #else try { routed = routing(req, res, strm); - } catch (std::exception &e) { + } catch (std::exception& e) { if (exception_handler_) { auto ep = std::current_exception(); exception_handler_(req, res, ep); @@ -5848,10 +6230,14 @@ Server::process_request(Stream &strm, bool close_connection, #endif if (routed) { - if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; } + if (res.status == -1) { + res.status = req.ranges.empty() ? 200 : 206; + } return write_response_with_content(strm, close_connection, req, res); } else { - if (res.status == -1) { res.status = 404; } + if (res.status == -1) { + res.status = 404; + } return write_response(strm, close_connection, req, res); } } @@ -5863,7 +6249,7 @@ inline bool Server::process_and_close_socket(socket_t sock) { svr_sock_, sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, - [this](Stream &strm, bool close_connection, bool &connection_closed) { + [this](Stream& strm, bool close_connection, bool& connection_closed) { return process_request(strm, close_connection, connection_closed, nullptr); }); @@ -5874,18 +6260,20 @@ inline bool Server::process_and_close_socket(socket_t sock) { } // HTTP client implementation -inline ClientImpl::ClientImpl(const std::string &host) +inline ClientImpl::ClientImpl(const std::string& host) : ClientImpl(host, 80, std::string(), std::string()) {} -inline ClientImpl::ClientImpl(const std::string &host, int port) +inline ClientImpl::ClientImpl(const std::string& host, int port) : ClientImpl(host, port, std::string(), std::string()) {} -inline ClientImpl::ClientImpl(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path) - : host_(host), port_(port), +inline ClientImpl::ClientImpl(const std::string& host, int port, + const std::string& client_cert_path, + const std::string& client_key_path) + : host_(host), + port_(port), host_and_port_(adjust_host_string(host) + ":" + std::to_string(port)), - client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} + client_cert_path_(client_cert_path), + client_key_path_(client_key_path) {} inline ClientImpl::~ClientImpl() { std::lock_guard guard(socket_mutex_); @@ -5895,7 +6283,7 @@ inline ClientImpl::~ClientImpl() { inline bool ClientImpl::is_valid() const { return true; } -inline void ClientImpl::copy_settings(const ClientImpl &rhs) { +inline void ClientImpl::copy_settings(const ClientImpl& rhs) { client_cert_path_ = rhs.client_cert_path_; client_key_path_ = rhs.client_key_path_; connection_timeout_sec_ = rhs.connection_timeout_sec_; @@ -5939,7 +6327,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) { logger_ = rhs.logger_; } -inline socket_t ClientImpl::create_client_socket(Error &error) const { +inline socket_t ClientImpl::create_client_socket(Error& error) const { if (!proxy_host_.empty() && proxy_port_ != -1) { return detail::create_client_socket( proxy_host_, std::string(), proxy_port_, address_family_, tcp_nodelay_, @@ -5951,7 +6339,8 @@ inline socket_t ClientImpl::create_client_socket(Error &error) const { // Check is custom IP specified for host_ std::string ip; auto it = addr_map_.find(host_); - if (it != addr_map_.end()) ip = it->second; + if (it != addr_map_.end()) + ip = it->second; return detail::create_client_socket( host_, ip, port_, address_family_, tcp_nodelay_, socket_options_, @@ -5960,15 +6349,17 @@ inline socket_t ClientImpl::create_client_socket(Error &error) const { error); } -inline bool ClientImpl::create_and_connect_socket(Socket &socket, - Error &error) { +inline bool ClientImpl::create_and_connect_socket(Socket& socket, + Error& error) { auto sock = create_client_socket(error); - if (sock == INVALID_SOCKET) { return false; } + if (sock == INVALID_SOCKET) { + return false; + } socket.sock = sock; return true; } -inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, +inline void ClientImpl::shutdown_ssl(Socket& /*socket*/, bool /*shutdown_gracefully*/) { // If there are any requests in flight from threads other than us, then it's // a thread-unsafe race because individual ssl* objects are not thread-safe. @@ -5976,12 +6367,14 @@ inline void ClientImpl::shutdown_ssl(Socket & /*socket*/, socket_requests_are_from_thread_ == std::this_thread::get_id()); } -inline void ClientImpl::shutdown_socket(Socket &socket) { - if (socket.sock == INVALID_SOCKET) { return; } +inline void ClientImpl::shutdown_socket(Socket& socket) { + if (socket.sock == INVALID_SOCKET) { + return; + } detail::shutdown_socket(socket.sock); } -inline void ClientImpl::close_socket(Socket &socket) { +inline void ClientImpl::close_socket(Socket& socket) { // If there are requests in flight in another thread, usually closing // the socket will be fine and they will simply receive an error when // using the closed socket, but it is still a bug since rarely the OS @@ -5995,18 +6388,22 @@ inline void ClientImpl::close_socket(Socket &socket) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT assert(socket.ssl == nullptr); #endif - if (socket.sock == INVALID_SOCKET) { return; } + if (socket.sock == INVALID_SOCKET) { + return; + } detail::close_socket(socket.sock); socket.sock = INVALID_SOCKET; } -inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, - Response &res) { +inline bool ClientImpl::read_response_line(Stream& strm, const Request& req, + Response& res) { std::array buf{}; detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); - if (!line_reader.getline()) { return false; } + if (!line_reader.getline()) { + return false; + } #ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n"); @@ -6024,10 +6421,16 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, // Ignore '100 Continue' while (res.status == 100) { - if (!line_reader.getline()) { return false; } // CRLF - if (!line_reader.getline()) { return false; } // next response line + if (!line_reader.getline()) { + return false; + } // CRLF + if (!line_reader.getline()) { + return false; + } // next response line - if (!std::regex_match(line_reader.ptr(), m, re)) { return false; } + if (!std::regex_match(line_reader.ptr(), m, re)) { + return false; + } res.version = std::string(m[1]); res.status = std::stoi(std::string(m[2])); res.reason = std::string(m[3]); @@ -6036,7 +6439,7 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req, return true; } -inline bool ClientImpl::send(Request &req, Response &res, Error &error) { +inline bool ClientImpl::send(Request& req, Response& res, Error& error) { std::lock_guard request_mutex_guard(request_mutex_); { @@ -6062,12 +6465,14 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { } if (!is_alive) { - if (!create_and_connect_socket(socket_, error)) { return false; } + if (!create_and_connect_socket(socket_, error)) { + return false; + } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT // TODO: refactoring if (is_ssl()) { - auto &scli = static_cast(*this); + auto& scli = static_cast(*this); if (!proxy_host_.empty() && proxy_port_ != -1) { bool success = false; if (!scli.connect_with_proxy(socket_, res, success, error)) { @@ -6075,7 +6480,9 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { } } - if (!scli.initialize_ssl(socket_, error)) { return false; } + if (!scli.initialize_ssl(socket_, error)) { + return false; + } } #endif } @@ -6090,14 +6497,14 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { socket_requests_are_from_thread_ = std::this_thread::get_id(); } - for (const auto &header : default_headers_) { + for (const auto& header : default_headers_) { if (req.headers.find(header.first) == req.headers.end()) { req.headers.insert(header); } } auto close_connection = !keep_alive_; - auto ret = process_socket(socket_, [&](Stream &strm) { + auto ret = process_socket(socket_, [&](Stream& strm) { return handle_request(strm, req, res, close_connection, error); }); @@ -6119,27 +6526,29 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) { } if (!ret) { - if (error == Error::Success) { error = Error::Unknown; } + if (error == Error::Success) { + error = Error::Unknown; + } } return ret; } -inline Result ClientImpl::send(const Request &req) { +inline Result ClientImpl::send(const Request& req) { auto req2 = req; return send_(std::move(req2)); } -inline Result ClientImpl::send_(Request &&req) { +inline Result ClientImpl::send_(Request&& req) { auto res = detail::make_unique(); auto error = Error::Success; auto ret = send(req, *res, error); return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)}; } -inline bool ClientImpl::handle_request(Stream &strm, Request &req, - Response &res, bool close_connection, - Error &error) { +inline bool ClientImpl::handle_request(Stream& strm, Request& req, + Response& res, bool close_connection, + Error& error) { if (req.path.empty()) { error = Error::Connection; return false; @@ -6159,7 +6568,9 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, ret = process_request(strm, req, res, close_connection, error); } - if (!ret) { return false; } + if (!ret) { + return false; + } if (300 < res.status && res.status < 400 && follow_location_) { req = req_save; @@ -6170,9 +6581,9 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, if ((res.status == 401 || res.status == 407) && req.authorization_count_ < 5) { auto is_proxy = res.status == 407; - const auto &username = + const auto& username = is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; - const auto &password = + const auto& password = is_proxy ? proxy_digest_auth_password_ : digest_auth_password_; if (!username.empty() && !password.empty()) { @@ -6189,7 +6600,9 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, Response new_res; ret = send(new_req, new_res, error); - if (ret) { res = new_res; } + if (ret) { + res = new_res; + } } } } @@ -6198,26 +6611,32 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req, return ret; } -inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { +inline bool ClientImpl::redirect(Request& req, Response& res, Error& error) { if (req.redirect_count_ == 0) { error = Error::ExceedRedirectCount; return false; } auto location = detail::decode_url(res.get_header_value("location"), true); - if (location.empty()) { return false; } + if (location.empty()) { + return false; + } const static std::regex re( R"((?:(https?):)?(?://(?:\[([\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*(?:\?[^#]*)?)(?:#.*)?)"); std::smatch m; - if (!std::regex_match(location, m, re)) { return false; } + if (!std::regex_match(location, m, re)) { + return false; + } auto scheme = is_ssl() ? "https" : "http"; auto next_scheme = m[1].str(); auto next_host = m[2].str(); - if (next_host.empty()) { next_host = m[3].str(); } + if (next_host.empty()) { + next_host = m[3].str(); + } auto port_str = m[4].str(); auto next_path = m[5].str(); @@ -6228,9 +6647,15 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { next_port = next_scheme == "https" ? 443 : 80; } - if (next_scheme.empty()) { next_scheme = scheme; } - if (next_host.empty()) { next_host = host_; } - if (next_path.empty()) { next_path = "/"; } + if (next_scheme.empty()) { + next_scheme = scheme; + } + if (next_host.empty()) { + next_host = host_; + } + if (next_path.empty()) { + next_path = "/"; + } if (next_scheme == scheme && next_host == host_ && next_port == port_) { return detail::redirect(*this, req, res, next_path, location, error); @@ -6239,7 +6664,9 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT SSLClient cli(next_host.c_str(), next_port); cli.copy_settings(*this); - if (ca_cert_store_) { cli.set_ca_cert_store(ca_cert_store_); } + if (ca_cert_store_) { + cli.set_ca_cert_store(ca_cert_store_); + } return detail::redirect(cli, req, res, next_path, location, error); #else return false; @@ -6252,9 +6679,9 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) { } } -inline bool ClientImpl::write_content_with_provider(Stream &strm, - const Request &req, - Error &error) { +inline bool ClientImpl::write_content_with_provider(Stream& strm, + const Request& req, + Error& error) { auto is_shutting_down = []() { return false; }; if (req.is_chunked_content_provider_) { @@ -6275,10 +6702,10 @@ inline bool ClientImpl::write_content_with_provider(Stream &strm, return detail::write_content(strm, req.content_provider_, 0, req.content_length_, is_shutting_down, error); } -} // namespace httplib +} // namespace httplib -inline bool ClientImpl::write_request(Stream &strm, Request &req, - bool close_connection, Error &error) { +inline bool ClientImpl::write_request(Stream& strm, Request& req, + bool close_connection, Error& error) { // Prepare additional headers if (close_connection) { if (!req.has_header("Connection")) { @@ -6302,7 +6729,9 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, } } - if (!req.has_header("Accept")) { req.headers.emplace("Accept", "*/*"); } + if (!req.has_header("Accept")) { + req.headers.emplace("Accept", "*/*"); + } #ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT if (!req.has_header("User-Agent")) { @@ -6369,13 +6798,13 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, { detail::BufferStream bstrm; - const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path; + const auto& path = url_encode_ ? detail::encode_url(req.path) : req.path; bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str()); detail::write_headers(bstrm, req.headers); // Flush buffer - auto &data = bstrm.get_buffer(); + auto& data = bstrm.get_buffer(); if (!detail::write_data(strm, data.data(), data.size())) { error = Error::Write; return false; @@ -6396,16 +6825,18 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req, } inline std::unique_ptr ClientImpl::send_with_content_provider( - Request &req, const char *body, size_t content_length, + Request& req, const char* body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type, Error &error) { + const std::string& content_type, Error& error) { if (!content_type.empty()) { req.headers.emplace("Content-Type", content_type); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT - if (compress_) { req.headers.emplace("Content-Encoding", "gzip"); } + if (compress_) { + req.headers.emplace("Content-Encoding", "gzip"); + } #endif #ifdef CPPHTTPLIB_ZLIB_SUPPORT @@ -6418,13 +6849,13 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( size_t offset = 0; DataSink data_sink; - data_sink.write = [&](const char *data, size_t data_len) -> bool { + data_sink.write = [&](const char* data, size_t data_len) -> bool { if (ok) { auto last = offset + data_len == content_length; auto ret = compressor.compress( data, data_len, last, - [&](const char *compressed_data, size_t compressed_data_len) { + [&](const char* compressed_data, size_t compressed_data_len) { req.body.append(compressed_data, compressed_data_len); return true; }); @@ -6448,7 +6879,7 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( } } else { if (!compressor.compress(body, content_length, true, - [&](const char *data, size_t data_len) { + [&](const char* data, size_t data_len) { req.body.append(data, data_len); return true; })) { @@ -6480,10 +6911,10 @@ inline std::unique_ptr ClientImpl::send_with_content_provider( } inline Result ClientImpl::send_with_content_provider( - const std::string &method, const std::string &path, const Headers &headers, - const char *body, size_t content_length, ContentProvider content_provider, + const std::string& method, const std::string& path, const Headers& headers, + const char* body, size_t content_length, ContentProvider content_provider, ContentProviderWithoutLength content_provider_without_length, - const std::string &content_type) { + const std::string& content_type) { Request req; req.method = method; req.headers = headers; @@ -6498,17 +6929,21 @@ inline Result ClientImpl::send_with_content_provider( return Result{std::move(res), error, std::move(req.headers)}; } -inline std::string -ClientImpl::adjust_host_string(const std::string &host) const { - if (host.find(':') != std::string::npos) { return "[" + host + "]"; } +inline std::string ClientImpl::adjust_host_string( + const std::string& host) const { + if (host.find(':') != std::string::npos) { + return "[" + host + "]"; + } return host; } -inline bool ClientImpl::process_request(Stream &strm, Request &req, - Response &res, bool close_connection, - Error &error) { +inline bool ClientImpl::process_request(Stream& strm, Request& req, + Response& res, bool close_connection, + Error& error) { // Send request - if (!write_request(strm, req, close_connection, error)) { return false; } + if (!write_request(strm, req, close_connection, error)) { + return false; + } // Receive response and headers if (!read_response_line(strm, req, res) || @@ -6531,14 +6966,18 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, auto out = req.content_receiver ? static_cast( - [&](const char *buf, size_t n, uint64_t off, uint64_t len) { - if (redirect) { return true; } + [&](const char* buf, size_t n, uint64_t off, uint64_t len) { + if (redirect) { + return true; + } auto ret = req.content_receiver(buf, n, off, len); - if (!ret) { error = Error::Canceled; } + if (!ret) { + error = Error::Canceled; + } return ret; }) : static_cast( - [&](const char *buf, size_t n, uint64_t /*off*/, + [&](const char* buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) { if (res.body.size() + n > res.body.max_size()) { return false; @@ -6548,9 +6987,13 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, }); auto progress = [&](uint64_t current, uint64_t total) { - if (!req.progress || redirect) { return true; } + if (!req.progress || redirect) { + return true; + } auto ret = req.progress(current, total); - if (!ret) { error = Error::Canceled; } + if (!ret) { + error = Error::Canceled; + } return ret; }; @@ -6558,7 +7001,9 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, if (!detail::read_content(strm, res, (std::numeric_limits::max)(), dummy_status, std::move(progress), std::move(out), decompress_)) { - if (error != Error::Canceled) { error = Error::Read; } + if (error != Error::Canceled) { + error = Error::Read; + } return false; } } @@ -6582,14 +7027,15 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req, } // Log - if (logger_) { logger_(req, res); } + if (logger_) { + logger_(req, res); + } return true; } -inline bool -ClientImpl::process_socket(const Socket &socket, - std::function callback) { +inline bool ClientImpl::process_socket( + const Socket& socket, std::function callback) { return detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, std::move(callback)); @@ -6597,19 +7043,19 @@ ClientImpl::process_socket(const Socket &socket, inline bool ClientImpl::is_ssl() const { return false; } -inline Result ClientImpl::Get(const std::string &path) { +inline Result ClientImpl::Get(const std::string& path) { return Get(path, Headers(), Progress()); } -inline Result ClientImpl::Get(const std::string &path, Progress progress) { +inline Result ClientImpl::Get(const std::string& path, Progress progress) { return Get(path, Headers(), std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers) { +inline Result ClientImpl::Get(const std::string& path, const Headers& headers) { return Get(path, headers, Progress()); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, +inline Result ClientImpl::Get(const std::string& path, const Headers& headers, Progress progress) { Request req; req.method = "GET"; @@ -6620,45 +7066,45 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, return send_(std::move(req)); } -inline Result ClientImpl::Get(const std::string &path, +inline Result ClientImpl::Get(const std::string& path, ContentReceiver content_receiver) { return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr); } -inline Result ClientImpl::Get(const std::string &path, +inline Result ClientImpl::Get(const std::string& path, ContentReceiver content_receiver, Progress progress) { return Get(path, Headers(), nullptr, std::move(content_receiver), std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, +inline Result ClientImpl::Get(const std::string& path, const Headers& headers, ContentReceiver content_receiver) { return Get(path, headers, nullptr, std::move(content_receiver), nullptr); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, +inline Result ClientImpl::Get(const std::string& path, const Headers& headers, ContentReceiver content_receiver, Progress progress) { return Get(path, headers, nullptr, std::move(content_receiver), std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, +inline Result ClientImpl::Get(const std::string& path, ResponseHandler response_handler, ContentReceiver content_receiver) { return Get(path, Headers(), std::move(response_handler), std::move(content_receiver), nullptr); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, +inline Result ClientImpl::Get(const std::string& path, const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver) { return Get(path, headers, std::move(response_handler), std::move(content_receiver), nullptr); } -inline Result ClientImpl::Get(const std::string &path, +inline Result ClientImpl::Get(const std::string& path, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress) { @@ -6666,7 +7112,7 @@ inline Result ClientImpl::Get(const std::string &path, std::move(content_receiver), std::move(progress)); } -inline Result ClientImpl::Get(const std::string &path, const Headers &headers, +inline Result ClientImpl::Get(const std::string& path, const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress) { @@ -6675,33 +7121,35 @@ inline Result ClientImpl::Get(const std::string &path, const Headers &headers, req.path = path; req.headers = headers; req.response_handler = std::move(response_handler); - req.content_receiver = - [content_receiver](const char *data, size_t data_length, - uint64_t /*offset*/, uint64_t /*total_length*/) { - return content_receiver(data, data_length); - }; + req.content_receiver = [content_receiver]( + const char* data, size_t data_length, + uint64_t /*offset*/, uint64_t /*total_length*/) { + return content_receiver(data, data_length); + }; req.progress = std::move(progress); return send_(std::move(req)); } -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress) { - if (params.empty()) { return Get(path, headers); } +inline Result ClientImpl::Get(const std::string& path, const Params& params, + const Headers& headers, Progress progress) { + if (params.empty()) { + return Get(path, headers); + } std::string path_with_query = append_query_params(path, params); return Get(path_with_query.c_str(), headers, progress); } -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, +inline Result ClientImpl::Get(const std::string& path, const Params& params, + const Headers& headers, ContentReceiver content_receiver, Progress progress) { return Get(path, params, headers, nullptr, content_receiver, progress); } -inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, - const Headers &headers, +inline Result ClientImpl::Get(const std::string& path, const Params& params, + const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress) { @@ -6714,12 +7162,12 @@ inline Result ClientImpl::Get(const std::string &path, const Params ¶ms, content_receiver, progress); } -inline Result ClientImpl::Head(const std::string &path) { +inline Result ClientImpl::Head(const std::string& path) { return Head(path, Headers()); } -inline Result ClientImpl::Head(const std::string &path, - const Headers &headers) { +inline Result ClientImpl::Head(const std::string& path, + const Headers& headers) { Request req; req.method = "HEAD"; req.headers = headers; @@ -6728,276 +7176,276 @@ inline Result ClientImpl::Head(const std::string &path, return send_(std::move(req)); } -inline Result ClientImpl::Post(const std::string &path) { +inline Result ClientImpl::Post(const std::string& path) { return Post(path, std::string(), std::string()); } -inline Result ClientImpl::Post(const std::string &path, const char *body, +inline Result ClientImpl::Post(const std::string& path, const char* body, size_t content_length, - const std::string &content_type) { + const std::string& content_type) { return Post(path, Headers(), body, content_length, content_type); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { +inline Result ClientImpl::Post(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type) { return send_with_content_provider("POST", path, headers, body, content_length, nullptr, nullptr, content_type); } -inline Result ClientImpl::Post(const std::string &path, const std::string &body, - const std::string &content_type) { +inline Result ClientImpl::Post(const std::string& path, const std::string& body, + const std::string& content_type) { return Post(path, Headers(), body, content_type); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { +inline Result ClientImpl::Post(const std::string& path, const Headers& headers, + const std::string& body, + const std::string& content_type) { return send_with_content_provider("POST", path, headers, body.data(), body.size(), nullptr, nullptr, content_type); } -inline Result ClientImpl::Post(const std::string &path, const Params ¶ms) { +inline Result ClientImpl::Post(const std::string& path, const Params& params) { return Post(path, Headers(), params); } -inline Result ClientImpl::Post(const std::string &path, size_t content_length, +inline Result ClientImpl::Post(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return Post(path, Headers(), content_length, std::move(content_provider), content_type); } -inline Result ClientImpl::Post(const std::string &path, +inline Result ClientImpl::Post(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return Post(path, Headers(), std::move(content_provider), content_type); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, +inline Result ClientImpl::Post(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return send_with_content_provider("POST", path, headers, nullptr, content_length, std::move(content_provider), nullptr, content_type); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, +inline Result ClientImpl::Post(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return send_with_content_provider("POST", path, headers, nullptr, 0, nullptr, std::move(content_provider), content_type); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const Params ¶ms) { +inline Result ClientImpl::Post(const std::string& path, const Headers& headers, + const Params& params) { auto query = detail::params_to_query_str(params); return Post(path, headers, query, "application/x-www-form-urlencoded"); } -inline Result ClientImpl::Post(const std::string &path, - const MultipartFormDataItems &items) { +inline Result ClientImpl::Post(const std::string& path, + const MultipartFormDataItems& items) { return Post(path, Headers(), items); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { +inline Result ClientImpl::Post(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items) { std::string content_type; - const auto &body = detail::serialize_multipart_formdata( + const auto& body = detail::serialize_multipart_formdata( items, detail::make_multipart_data_boundary(), content_type); return Post(path, headers, body, content_type.c_str()); } -inline Result ClientImpl::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { +inline Result ClientImpl::Post(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items, + const std::string& boundary) { if (!detail::is_multipart_boundary_chars_valid(boundary)) { return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; } std::string content_type; - const auto &body = + const auto& body = detail::serialize_multipart_formdata(items, boundary, content_type); return Post(path, headers, body, content_type.c_str()); } -inline Result ClientImpl::Put(const std::string &path) { +inline Result ClientImpl::Put(const std::string& path) { return Put(path, std::string(), std::string()); } -inline Result ClientImpl::Put(const std::string &path, const char *body, +inline Result ClientImpl::Put(const std::string& path, const char* body, size_t content_length, - const std::string &content_type) { + const std::string& content_type) { return Put(path, Headers(), body, content_length, content_type); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { +inline Result ClientImpl::Put(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type) { return send_with_content_provider("PUT", path, headers, body, content_length, nullptr, nullptr, content_type); } -inline Result ClientImpl::Put(const std::string &path, const std::string &body, - const std::string &content_type) { +inline Result ClientImpl::Put(const std::string& path, const std::string& body, + const std::string& content_type) { return Put(path, Headers(), body, content_type); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { +inline Result ClientImpl::Put(const std::string& path, const Headers& headers, + const std::string& body, + const std::string& content_type) { return send_with_content_provider("PUT", path, headers, body.data(), body.size(), nullptr, nullptr, content_type); } -inline Result ClientImpl::Put(const std::string &path, size_t content_length, +inline Result ClientImpl::Put(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return Put(path, Headers(), content_length, std::move(content_provider), content_type); } -inline Result ClientImpl::Put(const std::string &path, +inline Result ClientImpl::Put(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return Put(path, Headers(), std::move(content_provider), content_type); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, +inline Result ClientImpl::Put(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return send_with_content_provider("PUT", path, headers, nullptr, content_length, std::move(content_provider), nullptr, content_type); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, +inline Result ClientImpl::Put(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return send_with_content_provider("PUT", path, headers, nullptr, 0, nullptr, std::move(content_provider), content_type); } -inline Result ClientImpl::Put(const std::string &path, const Params ¶ms) { +inline Result ClientImpl::Put(const std::string& path, const Params& params) { return Put(path, Headers(), params); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const Params ¶ms) { +inline Result ClientImpl::Put(const std::string& path, const Headers& headers, + const Params& params) { auto query = detail::params_to_query_str(params); return Put(path, headers, query, "application/x-www-form-urlencoded"); } -inline Result ClientImpl::Put(const std::string &path, - const MultipartFormDataItems &items) { +inline Result ClientImpl::Put(const std::string& path, + const MultipartFormDataItems& items) { return Put(path, Headers(), items); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { +inline Result ClientImpl::Put(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items) { std::string content_type; - const auto &body = detail::serialize_multipart_formdata( + const auto& body = detail::serialize_multipart_formdata( items, detail::make_multipart_data_boundary(), content_type); return Put(path, headers, body, content_type); } -inline Result ClientImpl::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { +inline Result ClientImpl::Put(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items, + const std::string& boundary) { if (!detail::is_multipart_boundary_chars_valid(boundary)) { return Result{nullptr, Error::UnsupportedMultipartBoundaryChars}; } std::string content_type; - const auto &body = + const auto& body = detail::serialize_multipart_formdata(items, boundary, content_type); return Put(path, headers, body, content_type); } -inline Result ClientImpl::Patch(const std::string &path) { +inline Result ClientImpl::Patch(const std::string& path) { return Patch(path, std::string(), std::string()); } -inline Result ClientImpl::Patch(const std::string &path, const char *body, +inline Result ClientImpl::Patch(const std::string& path, const char* body, size_t content_length, - const std::string &content_type) { + const std::string& content_type) { return Patch(path, Headers(), body, content_length, content_type); } -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { +inline Result ClientImpl::Patch(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type) { return send_with_content_provider("PATCH", path, headers, body, content_length, nullptr, nullptr, content_type); } -inline Result ClientImpl::Patch(const std::string &path, - const std::string &body, - const std::string &content_type) { +inline Result ClientImpl::Patch(const std::string& path, + const std::string& body, + const std::string& content_type) { return Patch(path, Headers(), body, content_type); } -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { +inline Result ClientImpl::Patch(const std::string& path, const Headers& headers, + const std::string& body, + const std::string& content_type) { return send_with_content_provider("PATCH", path, headers, body.data(), body.size(), nullptr, nullptr, content_type); } -inline Result ClientImpl::Patch(const std::string &path, size_t content_length, +inline Result ClientImpl::Patch(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return Patch(path, Headers(), content_length, std::move(content_provider), content_type); } -inline Result ClientImpl::Patch(const std::string &path, +inline Result ClientImpl::Patch(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return Patch(path, Headers(), std::move(content_provider), content_type); } -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, +inline Result ClientImpl::Patch(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return send_with_content_provider("PATCH", path, headers, nullptr, content_length, std::move(content_provider), nullptr, content_type); } -inline Result ClientImpl::Patch(const std::string &path, const Headers &headers, +inline Result ClientImpl::Patch(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return send_with_content_provider("PATCH", path, headers, nullptr, 0, nullptr, std::move(content_provider), content_type); } -inline Result ClientImpl::Delete(const std::string &path) { +inline Result ClientImpl::Delete(const std::string& path) { return Delete(path, Headers(), std::string(), std::string()); } -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers) { +inline Result ClientImpl::Delete(const std::string& path, + const Headers& headers) { return Delete(path, headers, std::string(), std::string()); } -inline Result ClientImpl::Delete(const std::string &path, const char *body, +inline Result ClientImpl::Delete(const std::string& path, const char* body, size_t content_length, - const std::string &content_type) { + const std::string& content_type) { return Delete(path, Headers(), body, content_length, content_type); } -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, const char *body, +inline Result ClientImpl::Delete(const std::string& path, + const Headers& headers, const char* body, size_t content_length, - const std::string &content_type) { + const std::string& content_type) { Request req; req.method = "DELETE"; req.headers = headers; @@ -7011,25 +7459,25 @@ inline Result ClientImpl::Delete(const std::string &path, return send_(std::move(req)); } -inline Result ClientImpl::Delete(const std::string &path, - const std::string &body, - const std::string &content_type) { +inline Result ClientImpl::Delete(const std::string& path, + const std::string& body, + const std::string& content_type) { return Delete(path, Headers(), body.data(), body.size(), content_type); } -inline Result ClientImpl::Delete(const std::string &path, - const Headers &headers, - const std::string &body, - const std::string &content_type) { +inline Result ClientImpl::Delete(const std::string& path, + const Headers& headers, + const std::string& body, + const std::string& content_type) { return Delete(path, headers, body.data(), body.size(), content_type); } -inline Result ClientImpl::Options(const std::string &path) { +inline Result ClientImpl::Options(const std::string& path) { return Options(path, Headers()); } -inline Result ClientImpl::Options(const std::string &path, - const Headers &headers) { +inline Result ClientImpl::Options(const std::string& path, + const Headers& headers) { Request req; req.method = "OPTIONS"; req.headers = headers; @@ -7083,19 +7531,19 @@ inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { write_timeout_usec_ = usec; } -inline void ClientImpl::set_basic_auth(const std::string &username, - const std::string &password) { +inline void ClientImpl::set_basic_auth(const std::string& username, + const std::string& password) { basic_auth_username_ = username; basic_auth_password_ = password; } -inline void ClientImpl::set_bearer_token_auth(const std::string &token) { +inline void ClientImpl::set_bearer_token_auth(const std::string& token) { bearer_token_auth_token_ = token; } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void ClientImpl::set_digest_auth(const std::string &username, - const std::string &password) { +inline void ClientImpl::set_digest_auth(const std::string& username, + const std::string& password) { digest_auth_username_ = username; digest_auth_password_ = password; } @@ -7107,8 +7555,8 @@ inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } inline void ClientImpl::set_url_encode(bool on) { url_encode_ = on; } -inline void -ClientImpl::set_hostname_addr_map(std::map addr_map) { +inline void ClientImpl::set_hostname_addr_map( + std::map addr_map) { addr_map_ = std::move(addr_map); } @@ -7130,41 +7578,41 @@ inline void ClientImpl::set_compress(bool on) { compress_ = on; } inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } -inline void ClientImpl::set_interface(const std::string &intf) { +inline void ClientImpl::set_interface(const std::string& intf) { interface_ = intf; } -inline void ClientImpl::set_proxy(const std::string &host, int port) { +inline void ClientImpl::set_proxy(const std::string& host, int port) { proxy_host_ = host; proxy_port_ = port; } -inline void ClientImpl::set_proxy_basic_auth(const std::string &username, - const std::string &password) { +inline void ClientImpl::set_proxy_basic_auth(const std::string& username, + const std::string& password) { proxy_basic_auth_username_ = username; proxy_basic_auth_password_ = password; } -inline void ClientImpl::set_proxy_bearer_token_auth(const std::string &token) { +inline void ClientImpl::set_proxy_bearer_token_auth(const std::string& token) { proxy_bearer_token_auth_token_ = token; } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void ClientImpl::set_proxy_digest_auth(const std::string &username, - const std::string &password) { +inline void ClientImpl::set_proxy_digest_auth(const std::string& username, + const std::string& password) { proxy_digest_auth_username_ = username; proxy_digest_auth_password_ = password; } #endif #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void ClientImpl::set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path) { +inline void ClientImpl::set_ca_cert_path(const std::string& ca_cert_file_path, + const std::string& ca_cert_dir_path) { ca_cert_file_path_ = ca_cert_file_path; ca_cert_dir_path_ = ca_cert_dir_path; } -inline void ClientImpl::set_ca_cert_store(X509_STORE *ca_cert_store) { +inline void ClientImpl::set_ca_cert_store(X509_STORE* ca_cert_store) { if (ca_cert_store && ca_cert_store != ca_cert_store_) { ca_cert_store_ = ca_cert_store; } @@ -7188,9 +7636,9 @@ inline void ClientImpl::set_logger(Logger logger) { namespace detail { template -inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, +inline SSL* ssl_new(socket_t sock, SSL_CTX* ctx, std::mutex& ctx_mutex, U SSL_connect_or_accept, V setup) { - SSL *ssl = nullptr; + SSL* ssl = nullptr; { std::lock_guard guard(ctx_mutex); ssl = SSL_new(ctx); @@ -7218,20 +7666,22 @@ inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, return ssl; } -inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, +inline void ssl_delete(std::mutex& ctx_mutex, SSL* ssl, bool shutdown_gracefully) { // sometimes we may want to skip this to try to avoid SIGPIPE if we know // the remote has closed the network connection // Note that it is not always possible to avoid SIGPIPE, this is merely a // best-efforts. - if (shutdown_gracefully) { SSL_shutdown(ssl); } + if (shutdown_gracefully) { + SSL_shutdown(ssl); + } std::lock_guard guard(ctx_mutex); SSL_free(ssl); } template -bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, +bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL* ssl, U ssl_connect_or_accept, time_t timeout_sec, time_t timeout_usec) { @@ -7240,12 +7690,17 @@ bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, auto err = SSL_get_error(ssl, res); switch (err) { case SSL_ERROR_WANT_READ: - if (select_read(sock, timeout_sec, timeout_usec) > 0) { continue; } + if (select_read(sock, timeout_sec, timeout_usec) > 0) { + continue; + } break; case SSL_ERROR_WANT_WRITE: - if (select_write(sock, timeout_sec, timeout_usec) > 0) { continue; } + if (select_write(sock, timeout_sec, timeout_usec) > 0) { + continue; + } + break; + default: break; - default: break; } return false; } @@ -7254,13 +7709,13 @@ bool ssl_connect_or_accept_nonblocking(socket_t sock, SSL *ssl, template inline bool process_server_socket_ssl( - const std::atomic &svr_sock, SSL *ssl, socket_t sock, + const std::atomic& svr_sock, SSL* ssl, socket_t sock, size_t keep_alive_max_count, time_t keep_alive_timeout_sec, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, T callback) { return process_server_socket_core( svr_sock, sock, keep_alive_max_count, keep_alive_timeout_sec, - [&](bool close_connection, bool &connection_closed) { + [&](bool close_connection, bool& connection_closed) { SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, write_timeout_sec, write_timeout_usec); return callback(strm, close_connection, connection_closed); @@ -7268,10 +7723,11 @@ inline bool process_server_socket_ssl( } template -inline bool -process_client_socket_ssl(SSL *ssl, socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec, time_t write_timeout_sec, - time_t write_timeout_usec, T callback) { +inline bool process_client_socket_ssl(SSL* ssl, socket_t sock, + time_t read_timeout_sec, + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, write_timeout_sec, write_timeout_usec); return callback(strm); @@ -7281,7 +7737,7 @@ process_client_socket_ssl(SSL *ssl, socket_t sock, time_t read_timeout_sec, static std::shared_ptr> openSSL_locks_; class SSLThreadLocks { -public: + public: SSLThreadLocks() { openSSL_locks_ = std::make_shared>(CRYPTO_num_locks()); @@ -7290,10 +7746,10 @@ class SSLThreadLocks { ~SSLThreadLocks() { CRYPTO_set_locking_callback(nullptr); } -private: - static void locking_callback(int mode, int type, const char * /*file*/, + private: + static void locking_callback(int mode, int type, const char* /*file*/, int /*line*/) { - auto &lk = (*openSSL_locks_)[static_cast(type)]; + auto& lk = (*openSSL_locks_)[static_cast(type)]; if (mode & CRYPTO_LOCK) { lk.lock(); } else { @@ -7305,7 +7761,7 @@ class SSLThreadLocks { #endif class SSLInit { -public: + public: SSLInit() { #if OPENSSL_VERSION_NUMBER < 0x1010001fL SSL_load_error_strings(); @@ -7322,19 +7778,21 @@ class SSLInit { #endif } -private: + private: #if OPENSSL_VERSION_NUMBER < 0x10100000L SSLThreadLocks thread_init_; #endif }; // SSL socket stream implementation -inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl, +inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL* ssl, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec) - : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), + : sock_(sock), + ssl_(ssl), + read_timeout_sec_(read_timeout_sec), read_timeout_usec_(read_timeout_usec), write_timeout_sec_(write_timeout_sec), write_timeout_usec_(write_timeout_usec) { @@ -7349,10 +7807,10 @@ inline bool SSLSocketStream::is_readable() const { inline bool SSLSocketStream::is_writable() const { return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0 && - is_socket_alive(sock_); + is_socket_alive(sock_); } -inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { +inline ssize_t SSLSocketStream::read(char* ptr, size_t size) { if (SSL_pending(ssl_) > 0) { return SSL_read(ssl_, ptr, static_cast(size)); } else if (is_readable()) { @@ -7372,7 +7830,9 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { } else if (is_readable()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); ret = SSL_read(ssl_, ptr, static_cast(size)); - if (ret >= 0) { return ret; } + if (ret >= 0) { + return ret; + } err = SSL_get_error(ssl_, ret); } else { return -1; @@ -7384,7 +7844,7 @@ inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { return -1; } -inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { +inline ssize_t SSLSocketStream::write(const char* ptr, size_t size) { if (is_writable()) { auto handle_size = static_cast( std::min(size, (std::numeric_limits::max)())); @@ -7403,7 +7863,9 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { if (is_writable()) { std::this_thread::sleep_for(std::chrono::milliseconds(1)); ret = SSL_write(ssl_, ptr, static_cast(handle_size)); - if (ret >= 0) { return ret; } + if (ret >= 0) { + return ret; + } err = SSL_get_error(ssl_, ret); } else { return -1; @@ -7415,8 +7877,8 @@ inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { return -1; } -inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, - int &port) const { +inline void SSLSocketStream::get_remote_ip_and_port(std::string& ip, + int& port) const { detail::get_remote_ip_and_port(sock_, ip, port); } @@ -7424,26 +7886,26 @@ inline socket_t SSLSocketStream::socket() const { return sock_; } static SSLInit sslinit_; -} // namespace detail +} // namespace detail // SSL HTTP server implementation -inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, - const char *client_ca_cert_file_path, - const char *client_ca_cert_dir_path, - const char *private_key_password) { +inline SSLServer::SSLServer(const char* cert_path, const char* private_key_path, + const char* client_ca_cert_file_path, + const char* client_ca_cert_dir_path, + const char* private_key_password) { ctx_ = SSL_CTX_new(TLS_server_method()); if (ctx_) { - SSL_CTX_set_options(ctx_, - SSL_OP_NO_COMPRESSION | - SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + SSL_CTX_set_options( + ctx_, + SSL_OP_NO_COMPRESSION | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); SSL_CTX_set_min_proto_version(ctx_, TLS1_1_VERSION); // add default password callback before opening encrypted private key if (private_key_password != nullptr && (private_key_password[0] != '\0')) { SSL_CTX_set_default_passwd_cb_userdata(ctx_, - (char *)private_key_password); + (char*) private_key_password); } if (SSL_CTX_use_certificate_chain_file(ctx_, cert_path) != 1 || @@ -7461,14 +7923,14 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, } } -inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, - X509_STORE *client_ca_cert_store) { +inline SSLServer::SSLServer(X509* cert, EVP_PKEY* private_key, + X509_STORE* client_ca_cert_store) { ctx_ = SSL_CTX_new(TLS_server_method()); if (ctx_) { - SSL_CTX_set_options(ctx_, - SSL_OP_NO_COMPRESSION | - SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + SSL_CTX_set_options( + ctx_, + SSL_OP_NO_COMPRESSION | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); SSL_CTX_set_min_proto_version(ctx_, TLS1_1_VERSION); @@ -7486,7 +7948,7 @@ inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, } inline SSLServer::SSLServer( - const std::function &setup_ssl_ctx_callback) { + const std::function& setup_ssl_ctx_callback) { ctx_ = SSL_CTX_new(TLS_method()); if (ctx_) { if (!setup_ssl_ctx_callback(*ctx_)) { @@ -7497,21 +7959,23 @@ inline SSLServer::SSLServer( } inline SSLServer::~SSLServer() { - if (ctx_) { SSL_CTX_free(ctx_); } + if (ctx_) { + SSL_CTX_free(ctx_); + } } inline bool SSLServer::is_valid() const { return ctx_; } -inline SSL_CTX *SSLServer::ssl_context() const { return ctx_; } +inline SSL_CTX* SSLServer::ssl_context() const { return ctx_; } inline bool SSLServer::process_and_close_socket(socket_t sock) { auto ssl = detail::ssl_new( sock, ctx_, ctx_mutex_, - [&](SSL *ssl2) { + [&](SSL* ssl2) { return detail::ssl_connect_or_accept_nonblocking( sock, ssl2, SSL_accept, read_timeout_sec_, read_timeout_usec_); }, - [](SSL * /*ssl2*/) { return true; }); + [](SSL* /*ssl2*/) { return true; }); bool ret = false; if (ssl) { @@ -7519,10 +7983,10 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) { svr_sock_, ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, - [this, ssl](Stream &strm, bool close_connection, - bool &connection_closed) { + [this, ssl](Stream& strm, bool close_connection, + bool& connection_closed) { return process_request(strm, close_connection, connection_closed, - [&](Request &req) { req.ssl = ssl; }); + [&](Request& req) { req.ssl = ssl; }); }); // Shutdown gracefully if the result seemed successful, non-gracefully if @@ -7537,20 +8001,20 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) { } // SSL HTTP client implementation -inline SSLClient::SSLClient(const std::string &host) +inline SSLClient::SSLClient(const std::string& host) : SSLClient(host, 443, std::string(), std::string()) {} -inline SSLClient::SSLClient(const std::string &host, int port) +inline SSLClient::SSLClient(const std::string& host, int port) : SSLClient(host, port, std::string(), std::string()) {} -inline SSLClient::SSLClient(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path) +inline SSLClient::SSLClient(const std::string& host, int port, + const std::string& client_cert_path, + const std::string& client_key_path) : ClientImpl(host, port, client_cert_path, client_key_path) { ctx_ = SSL_CTX_new(TLS_client_method()); detail::split(&host_[0], &host_[host_.size()], '.', - [&](const char *b, const char *e) { + [&](const char* b, const char* e) { host_components_.emplace_back(std::string(b, e)); }); @@ -7565,13 +8029,13 @@ inline SSLClient::SSLClient(const std::string &host, int port, } } -inline SSLClient::SSLClient(const std::string &host, int port, - X509 *client_cert, EVP_PKEY *client_key) +inline SSLClient::SSLClient(const std::string& host, int port, + X509* client_cert, EVP_PKEY* client_key) : ClientImpl(host, port) { ctx_ = SSL_CTX_new(TLS_client_method()); detail::split(&host_[0], &host_[host_.size()], '.', - [&](const char *b, const char *e) { + [&](const char* b, const char* e) { host_components_.emplace_back(std::string(b, e)); }); @@ -7585,7 +8049,9 @@ inline SSLClient::SSLClient(const std::string &host, int port, } inline SSLClient::~SSLClient() { - if (ctx_) { SSL_CTX_free(ctx_); } + if (ctx_) { + SSL_CTX_free(ctx_); + } // Make sure to shut down SSL since shutdown_ssl will resolve to the // base function rather than the derived function once we get to the // base class destructor, and won't free the SSL (causing a leak). @@ -7594,7 +8060,7 @@ inline SSLClient::~SSLClient() { inline bool SSLClient::is_valid() const { return ctx_; } -inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { +inline void SSLClient::set_ca_cert_store(X509_STORE* ca_cert_store) { if (ca_cert_store) { if (ctx_) { if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { @@ -7611,20 +8077,20 @@ inline long SSLClient::get_openssl_verify_result() const { return verify_result_; } -inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } +inline SSL_CTX* SSLClient::ssl_context() const { return ctx_; } -inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) { +inline bool SSLClient::create_and_connect_socket(Socket& socket, Error& error) { return is_valid() && ClientImpl::create_and_connect_socket(socket, error); } // Assumes that socket_mutex_ is locked and that there are no requests in flight -inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, - bool &success, Error &error) { +inline bool SSLClient::connect_with_proxy(Socket& socket, Response& res, + bool& success, Error& error) { success = true; Response res2; if (!detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + write_timeout_sec_, write_timeout_usec_, [&](Stream& strm) { Request req2; req2.method = "CONNECT"; req2.path = host_and_port_; @@ -7647,7 +8113,7 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, Response res3; if (!detail::process_client_socket( socket.sock, read_timeout_sec_, read_timeout_usec_, - write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + write_timeout_sec_, write_timeout_usec_, [&](Stream& strm) { Request req3; req3.method = "CONNECT"; req3.path = host_and_port_; @@ -7702,10 +8168,10 @@ inline bool SSLClient::load_certs() { return ret; } -inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { +inline bool SSLClient::initialize_ssl(Socket& socket, Error& error) { auto ssl = detail::ssl_new( socket.sock, ctx_, ctx_mutex_, - [&](SSL *ssl2) { + [&](SSL* ssl2) { if (server_certificate_verification_) { if (!load_certs()) { error = Error::SSLLoadingCerts; @@ -7746,7 +8212,7 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return true; }, - [&](SSL *ssl2) { + [&](SSL* ssl2) { SSL_set_tlsext_host_name(ssl2, host_.c_str()); return true; }); @@ -7761,11 +8227,11 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) { return false; } -inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) { +inline void SSLClient::shutdown_ssl(Socket& socket, bool shutdown_gracefully) { shutdown_ssl_impl(socket, shutdown_gracefully); } -inline void SSLClient::shutdown_ssl_impl(Socket &socket, +inline void SSLClient::shutdown_ssl_impl(Socket& socket, bool shutdown_gracefully) { if (socket.sock == INVALID_SOCKET) { assert(socket.ssl == nullptr); @@ -7778,9 +8244,8 @@ inline void SSLClient::shutdown_ssl_impl(Socket &socket, assert(socket.ssl == nullptr); } -inline bool -SSLClient::process_socket(const Socket &socket, - std::function callback) { +inline bool SSLClient::process_socket( + const Socket& socket, std::function callback) { assert(socket.ssl); return detail::process_client_socket_ssl( socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, @@ -7789,7 +8254,7 @@ SSLClient::process_socket(const Socket &socket, inline bool SSLClient::is_ssl() const { return true; } -inline bool SSLClient::verify_host(X509 *server_cert) const { +inline bool SSLClient::verify_host(X509* server_cert) const { /* Quote from RFC2818 section 3.1 "Server Identity" If a subjectAltName extension of type dNSName is present, that MUST @@ -7815,8 +8280,8 @@ inline bool SSLClient::verify_host(X509 *server_cert) const { verify_host_with_common_name(server_cert); } -inline bool -SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { +inline bool SSLClient::verify_host_with_subject_alt_name( + X509* server_cert) const { auto ret = false; auto type = GEN_DNS; @@ -7835,7 +8300,7 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { } #endif - auto alt_names = static_cast( + auto alt_names = static_cast( X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr)); if (alt_names) { @@ -7847,11 +8312,13 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { for (decltype(count) i = 0; i < count && !dsn_matched; i++) { auto val = sk_GENERAL_NAME_value(alt_names, i); if (val->type == type) { - auto name = (const char *)ASN1_STRING_get0_data(val->d.ia5); - auto name_len = (size_t)ASN1_STRING_length(val->d.ia5); + auto name = (const char*) ASN1_STRING_get0_data(val->d.ia5); + auto name_len = (size_t) ASN1_STRING_length(val->d.ia5); switch (type) { - case GEN_DNS: dsn_matched = check_host_name(name, name_len); break; + case GEN_DNS: + dsn_matched = check_host_name(name, name_len); + break; case GEN_IPADD: if (!memcmp(&addr6, name, addr_len) || @@ -7863,14 +8330,16 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { } } - if (dsn_matched || ip_mached) { ret = true; } + if (dsn_matched || ip_mached) { + ret = true; + } } - GENERAL_NAMES_free((STACK_OF(GENERAL_NAME) *)alt_names); + GENERAL_NAMES_free((STACK_OF(GENERAL_NAME)*) alt_names); return ret; } -inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { +inline bool SSLClient::verify_host_with_common_name(X509* server_cert) const { const auto subject_name = X509_get_subject_name(server_cert); if (subject_name != nullptr) { @@ -7886,27 +8355,33 @@ inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { return false; } -inline bool SSLClient::check_host_name(const char *pattern, +inline bool SSLClient::check_host_name(const char* pattern, size_t pattern_len) const { - if (host_.size() == pattern_len && host_ == pattern) { return true; } + if (host_.size() == pattern_len && host_ == pattern) { + return true; + } // Wildcard match // https://bugs.launchpad.net/ubuntu/+source/firefox-3.0/+bug/376484 std::vector pattern_components; detail::split(&pattern[0], &pattern[pattern_len], '.', - [&](const char *b, const char *e) { + [&](const char* b, const char* e) { pattern_components.emplace_back(std::string(b, e)); }); - if (host_components_.size() != pattern_components.size()) { return false; } + if (host_components_.size() != pattern_components.size()) { + return false; + } auto itr = pattern_components.begin(); - for (const auto &h : host_components_) { - auto &p = *itr; + for (const auto& h : host_components_) { + auto& p = *itr; if (p != h && p != "*") { auto partial_match = (p.size() > 0 && p[p.size() - 1] == '*' && !p.compare(0, p.size() - 1, h)); - if (!partial_match) { return false; } + if (!partial_match) { + return false; + } } ++itr; } @@ -7916,12 +8391,12 @@ inline bool SSLClient::check_host_name(const char *pattern, #endif // Universal client implementation -inline Client::Client(const std::string &scheme_host_port) +inline Client::Client(const std::string& scheme_host_port) : Client(scheme_host_port, std::string(), std::string()) {} -inline Client::Client(const std::string &scheme_host_port, - const std::string &client_cert_path, - const std::string &client_key_path) { +inline Client::Client(const std::string& scheme_host_port, + const std::string& client_cert_path, + const std::string& client_key_path) { const static std::regex re( R"((?:([a-z]+):\/\/)?(?:\[([\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); @@ -7944,7 +8419,9 @@ inline Client::Client(const std::string &scheme_host_port, auto is_ssl = scheme == "https"; auto host = m[2].str(); - if (host.empty()) { host = m[3].str(); } + if (host.empty()) { + host = m[3].str(); + } auto port_str = m[4].str(); auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); @@ -7965,12 +8442,12 @@ inline Client::Client(const std::string &scheme_host_port, } } -inline Client::Client(const std::string &host, int port) +inline Client::Client(const std::string& host, int port) : cli_(detail::make_unique(host, port)) {} -inline Client::Client(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path) +inline Client::Client(const std::string& host, int port, + const std::string& client_cert_path, + const std::string& client_key_path) : cli_(detail::make_unique(host, port, client_cert_path, client_key_path)) {} @@ -7980,288 +8457,288 @@ inline bool Client::is_valid() const { return cli_ != nullptr && cli_->is_valid(); } -inline Result Client::Get(const std::string &path) { return cli_->Get(path); } -inline Result Client::Get(const std::string &path, const Headers &headers) { +inline Result Client::Get(const std::string& path) { return cli_->Get(path); } +inline Result Client::Get(const std::string& path, const Headers& headers) { return cli_->Get(path, headers); } -inline Result Client::Get(const std::string &path, Progress progress) { +inline Result Client::Get(const std::string& path, Progress progress) { return cli_->Get(path, std::move(progress)); } -inline Result Client::Get(const std::string &path, const Headers &headers, +inline Result Client::Get(const std::string& path, const Headers& headers, Progress progress) { return cli_->Get(path, headers, std::move(progress)); } -inline Result Client::Get(const std::string &path, +inline Result Client::Get(const std::string& path, ContentReceiver content_receiver) { return cli_->Get(path, std::move(content_receiver)); } -inline Result Client::Get(const std::string &path, const Headers &headers, +inline Result Client::Get(const std::string& path, const Headers& headers, ContentReceiver content_receiver) { return cli_->Get(path, headers, std::move(content_receiver)); } -inline Result Client::Get(const std::string &path, +inline Result Client::Get(const std::string& path, ContentReceiver content_receiver, Progress progress) { return cli_->Get(path, std::move(content_receiver), std::move(progress)); } -inline Result Client::Get(const std::string &path, const Headers &headers, +inline Result Client::Get(const std::string& path, const Headers& headers, ContentReceiver content_receiver, Progress progress) { return cli_->Get(path, headers, std::move(content_receiver), std::move(progress)); } -inline Result Client::Get(const std::string &path, +inline Result Client::Get(const std::string& path, ResponseHandler response_handler, ContentReceiver content_receiver) { return cli_->Get(path, std::move(response_handler), std::move(content_receiver)); } -inline Result Client::Get(const std::string &path, const Headers &headers, +inline Result Client::Get(const std::string& path, const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver) { return cli_->Get(path, headers, std::move(response_handler), std::move(content_receiver)); } -inline Result Client::Get(const std::string &path, +inline Result Client::Get(const std::string& path, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress) { return cli_->Get(path, std::move(response_handler), std::move(content_receiver), std::move(progress)); } -inline Result Client::Get(const std::string &path, const Headers &headers, +inline Result Client::Get(const std::string& path, const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress) { return cli_->Get(path, headers, std::move(response_handler), std::move(content_receiver), std::move(progress)); } -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, Progress progress) { +inline Result Client::Get(const std::string& path, const Params& params, + const Headers& headers, Progress progress) { return cli_->Get(path, params, headers, progress); } -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, +inline Result Client::Get(const std::string& path, const Params& params, + const Headers& headers, ContentReceiver content_receiver, Progress progress) { return cli_->Get(path, params, headers, content_receiver, progress); } -inline Result Client::Get(const std::string &path, const Params ¶ms, - const Headers &headers, +inline Result Client::Get(const std::string& path, const Params& params, + const Headers& headers, ResponseHandler response_handler, ContentReceiver content_receiver, Progress progress) { return cli_->Get(path, params, headers, response_handler, content_receiver, progress); } -inline Result Client::Head(const std::string &path) { return cli_->Head(path); } -inline Result Client::Head(const std::string &path, const Headers &headers) { +inline Result Client::Head(const std::string& path) { return cli_->Head(path); } +inline Result Client::Head(const std::string& path, const Headers& headers) { return cli_->Head(path, headers); } -inline Result Client::Post(const std::string &path) { return cli_->Post(path); } -inline Result Client::Post(const std::string &path, const char *body, +inline Result Client::Post(const std::string& path) { return cli_->Post(path); } +inline Result Client::Post(const std::string& path, const char* body, size_t content_length, - const std::string &content_type) { + const std::string& content_type) { return cli_->Post(path, body, content_length, content_type); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { +inline Result Client::Post(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type) { return cli_->Post(path, headers, body, content_length, content_type); } -inline Result Client::Post(const std::string &path, const std::string &body, - const std::string &content_type) { +inline Result Client::Post(const std::string& path, const std::string& body, + const std::string& content_type) { return cli_->Post(path, body, content_type); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { +inline Result Client::Post(const std::string& path, const Headers& headers, + const std::string& body, + const std::string& content_type) { return cli_->Post(path, headers, body, content_type); } -inline Result Client::Post(const std::string &path, size_t content_length, +inline Result Client::Post(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Post(path, content_length, std::move(content_provider), content_type); } -inline Result Client::Post(const std::string &path, +inline Result Client::Post(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Post(path, std::move(content_provider), content_type); } -inline Result Client::Post(const std::string &path, const Headers &headers, +inline Result Client::Post(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Post(path, headers, content_length, std::move(content_provider), content_type); } -inline Result Client::Post(const std::string &path, const Headers &headers, +inline Result Client::Post(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Post(path, headers, std::move(content_provider), content_type); } -inline Result Client::Post(const std::string &path, const Params ¶ms) { +inline Result Client::Post(const std::string& path, const Params& params) { return cli_->Post(path, params); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const Params ¶ms) { +inline Result Client::Post(const std::string& path, const Headers& headers, + const Params& params) { return cli_->Post(path, headers, params); } -inline Result Client::Post(const std::string &path, - const MultipartFormDataItems &items) { +inline Result Client::Post(const std::string& path, + const MultipartFormDataItems& items) { return cli_->Post(path, items); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { +inline Result Client::Post(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items) { return cli_->Post(path, headers, items); } -inline Result Client::Post(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { +inline Result Client::Post(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items, + const std::string& boundary) { return cli_->Post(path, headers, items, boundary); } -inline Result Client::Put(const std::string &path) { return cli_->Put(path); } -inline Result Client::Put(const std::string &path, const char *body, +inline Result Client::Put(const std::string& path) { return cli_->Put(path); } +inline Result Client::Put(const std::string& path, const char* body, size_t content_length, - const std::string &content_type) { + const std::string& content_type) { return cli_->Put(path, body, content_length, content_type); } -inline Result Client::Put(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { +inline Result Client::Put(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type) { return cli_->Put(path, headers, body, content_length, content_type); } -inline Result Client::Put(const std::string &path, const std::string &body, - const std::string &content_type) { +inline Result Client::Put(const std::string& path, const std::string& body, + const std::string& content_type) { return cli_->Put(path, body, content_type); } -inline Result Client::Put(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { +inline Result Client::Put(const std::string& path, const Headers& headers, + const std::string& body, + const std::string& content_type) { return cli_->Put(path, headers, body, content_type); } -inline Result Client::Put(const std::string &path, size_t content_length, +inline Result Client::Put(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Put(path, content_length, std::move(content_provider), content_type); } -inline Result Client::Put(const std::string &path, +inline Result Client::Put(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Put(path, std::move(content_provider), content_type); } -inline Result Client::Put(const std::string &path, const Headers &headers, +inline Result Client::Put(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Put(path, headers, content_length, std::move(content_provider), content_type); } -inline Result Client::Put(const std::string &path, const Headers &headers, +inline Result Client::Put(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Put(path, headers, std::move(content_provider), content_type); } -inline Result Client::Put(const std::string &path, const Params ¶ms) { +inline Result Client::Put(const std::string& path, const Params& params) { return cli_->Put(path, params); } -inline Result Client::Put(const std::string &path, const Headers &headers, - const Params ¶ms) { +inline Result Client::Put(const std::string& path, const Headers& headers, + const Params& params) { return cli_->Put(path, headers, params); } -inline Result Client::Put(const std::string &path, - const MultipartFormDataItems &items) { +inline Result Client::Put(const std::string& path, + const MultipartFormDataItems& items) { return cli_->Put(path, items); } -inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items) { +inline Result Client::Put(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items) { return cli_->Put(path, headers, items); } -inline Result Client::Put(const std::string &path, const Headers &headers, - const MultipartFormDataItems &items, - const std::string &boundary) { +inline Result Client::Put(const std::string& path, const Headers& headers, + const MultipartFormDataItems& items, + const std::string& boundary) { return cli_->Put(path, headers, items, boundary); } -inline Result Client::Patch(const std::string &path) { +inline Result Client::Patch(const std::string& path) { return cli_->Patch(path); } -inline Result Client::Patch(const std::string &path, const char *body, +inline Result Client::Patch(const std::string& path, const char* body, size_t content_length, - const std::string &content_type) { + const std::string& content_type) { return cli_->Patch(path, body, content_length, content_type); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { +inline Result Client::Patch(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type) { return cli_->Patch(path, headers, body, content_length, content_type); } -inline Result Client::Patch(const std::string &path, const std::string &body, - const std::string &content_type) { +inline Result Client::Patch(const std::string& path, const std::string& body, + const std::string& content_type) { return cli_->Patch(path, body, content_type); } -inline Result Client::Patch(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { +inline Result Client::Patch(const std::string& path, const Headers& headers, + const std::string& body, + const std::string& content_type) { return cli_->Patch(path, headers, body, content_type); } -inline Result Client::Patch(const std::string &path, size_t content_length, +inline Result Client::Patch(const std::string& path, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Patch(path, content_length, std::move(content_provider), content_type); } -inline Result Client::Patch(const std::string &path, +inline Result Client::Patch(const std::string& path, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Patch(path, std::move(content_provider), content_type); } -inline Result Client::Patch(const std::string &path, const Headers &headers, +inline Result Client::Patch(const std::string& path, const Headers& headers, size_t content_length, ContentProvider content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Patch(path, headers, content_length, std::move(content_provider), content_type); } -inline Result Client::Patch(const std::string &path, const Headers &headers, +inline Result Client::Patch(const std::string& path, const Headers& headers, ContentProviderWithoutLength content_provider, - const std::string &content_type) { + const std::string& content_type) { return cli_->Patch(path, headers, std::move(content_provider), content_type); } -inline Result Client::Delete(const std::string &path) { +inline Result Client::Delete(const std::string& path) { return cli_->Delete(path); } -inline Result Client::Delete(const std::string &path, const Headers &headers) { +inline Result Client::Delete(const std::string& path, const Headers& headers) { return cli_->Delete(path, headers); } -inline Result Client::Delete(const std::string &path, const char *body, +inline Result Client::Delete(const std::string& path, const char* body, size_t content_length, - const std::string &content_type) { + const std::string& content_type) { return cli_->Delete(path, body, content_length, content_type); } -inline Result Client::Delete(const std::string &path, const Headers &headers, - const char *body, size_t content_length, - const std::string &content_type) { +inline Result Client::Delete(const std::string& path, const Headers& headers, + const char* body, size_t content_length, + const std::string& content_type) { return cli_->Delete(path, headers, body, content_length, content_type); } -inline Result Client::Delete(const std::string &path, const std::string &body, - const std::string &content_type) { +inline Result Client::Delete(const std::string& path, const std::string& body, + const std::string& content_type) { return cli_->Delete(path, body, content_type); } -inline Result Client::Delete(const std::string &path, const Headers &headers, - const std::string &body, - const std::string &content_type) { +inline Result Client::Delete(const std::string& path, const Headers& headers, + const std::string& body, + const std::string& content_type) { return cli_->Delete(path, headers, body, content_type); } -inline Result Client::Options(const std::string &path) { +inline Result Client::Options(const std::string& path) { return cli_->Options(path); } -inline Result Client::Options(const std::string &path, const Headers &headers) { +inline Result Client::Options(const std::string& path, const Headers& headers) { return cli_->Options(path, headers); } -inline bool Client::send(Request &req, Response &res, Error &error) { +inline bool Client::send(Request& req, Response& res, Error& error) { return cli_->send(req, res, error); } -inline Result Client::send(const Request &req) { return cli_->send(req); } +inline Result Client::send(const Request& req) { return cli_->send(req); } inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } @@ -8269,8 +8746,8 @@ inline socket_t Client::socket() const { return cli_->socket(); } inline void Client::stop() { cli_->stop(); } -inline void -Client::set_hostname_addr_map(std::map addr_map) { +inline void Client::set_hostname_addr_map( + std::map addr_map) { cli_->set_hostname_addr_map(std::move(addr_map)); } @@ -8300,16 +8777,16 @@ inline void Client::set_write_timeout(time_t sec, time_t usec) { cli_->set_write_timeout(sec, usec); } -inline void Client::set_basic_auth(const std::string &username, - const std::string &password) { +inline void Client::set_basic_auth(const std::string& username, + const std::string& password) { cli_->set_basic_auth(username, password); } -inline void Client::set_bearer_token_auth(const std::string &token) { +inline void Client::set_bearer_token_auth(const std::string& token) { cli_->set_bearer_token_auth(token); } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_digest_auth(const std::string &username, - const std::string &password) { +inline void Client::set_digest_auth(const std::string& username, + const std::string& password) { cli_->set_digest_auth(username, password); } #endif @@ -8325,23 +8802,23 @@ inline void Client::set_compress(bool on) { cli_->set_compress(on); } inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } -inline void Client::set_interface(const std::string &intf) { +inline void Client::set_interface(const std::string& intf) { cli_->set_interface(intf); } -inline void Client::set_proxy(const std::string &host, int port) { +inline void Client::set_proxy(const std::string& host, int port) { cli_->set_proxy(host, port); } -inline void Client::set_proxy_basic_auth(const std::string &username, - const std::string &password) { +inline void Client::set_proxy_basic_auth(const std::string& username, + const std::string& password) { cli_->set_proxy_basic_auth(username, password); } -inline void Client::set_proxy_bearer_token_auth(const std::string &token) { +inline void Client::set_proxy_bearer_token_auth(const std::string& token) { cli_->set_proxy_bearer_token_auth(token); } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_proxy_digest_auth(const std::string &username, - const std::string &password) { +inline void Client::set_proxy_digest_auth(const std::string& username, + const std::string& password) { cli_->set_proxy_digest_auth(username, password); } #endif @@ -8355,14 +8832,14 @@ inline void Client::enable_server_certificate_verification(bool enabled) { inline void Client::set_logger(Logger logger) { cli_->set_logger(logger); } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_ca_cert_path(const std::string &ca_cert_file_path, - const std::string &ca_cert_dir_path) { +inline void Client::set_ca_cert_path(const std::string& ca_cert_file_path, + const std::string& ca_cert_dir_path) { cli_->set_ca_cert_path(ca_cert_file_path, ca_cert_dir_path); } -inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { +inline void Client::set_ca_cert_store(X509_STORE* ca_cert_store) { if (is_ssl_) { - static_cast(*cli_).set_ca_cert_store(ca_cert_store); + static_cast(*cli_).set_ca_cert_store(ca_cert_store); } else { cli_->set_ca_cert_store(ca_cert_store); } @@ -8370,19 +8847,21 @@ inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { inline long Client::get_openssl_verify_result() const { if (is_ssl_) { - return static_cast(*cli_).get_openssl_verify_result(); + return static_cast(*cli_).get_openssl_verify_result(); } - return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? + return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? } -inline SSL_CTX *Client::ssl_context() const { - if (is_ssl_) { return static_cast(*cli_).ssl_context(); } +inline SSL_CTX* Client::ssl_context() const { + if (is_ssl_) { + return static_cast(*cli_).ssl_context(); + } return nullptr; } #endif // ---------------------------------------------------------------------------- -} // namespace httplib +} // namespace httplib -#endif // CPPHTTPLIB_HTTPLIB_H +#endif // CPPHTTPLIB_HTTPLIB_H diff --git a/flex/third_party/odps/include/common/base64.h b/flex/third_party/odps/include/common/base64.h index 75d2e78c3fb9..235c0cdec8fc 100644 --- a/flex/third_party/odps/include/common/base64.h +++ b/flex/third_party/odps/include/common/base64.h @@ -283,7 +283,7 @@ inline size_t ExpectedEncodeLength(size_t len) { } inline size_t ExpectedDecodeLength(size_t len) { - return (size_t)((len + 3) / 4 * 3); + return (size_t) ((len + 3) / 4 * 3); } } // anonymous namespace diff --git a/flex/third_party/odps/include/common/md5.h b/flex/third_party/odps/include/common/md5.h index 80b52b57945a..d90fd5d9e17e 100644 --- a/flex/third_party/odps/include/common/md5.h +++ b/flex/third_party/odps/include/common/md5.h @@ -253,7 +253,7 @@ inline void MD5::update(const byte* input, size_t length) { _finished = false; /* Compute number of bytes mod 64 */ - index = (uint32)((_count[0] >> 3) & 0x3f); + index = (uint32) ((_count[0] >> 3) & 0x3f); /* update number of bits */ if ((_count[0] += ((uint32) length << 3)) < ((uint32) length << 3)) { @@ -298,7 +298,7 @@ inline void MD5::final() { encode(_count, bits, 8); /* Pad out to 56 mod 64. */ - index = (uint32)((_count[0] >> 3) & 0x3f); + index = (uint32) ((_count[0] >> 3) & 0x3f); padLen = (index < 56) ? (56 - index) : (120 - index); update(PADDING, padLen); @@ -402,10 +402,10 @@ a multiple of 4. */ inline void MD5::encode(const uint32* input, byte* output, size_t length) { for (size_t i = 0, j = 0; j < length; ++i, j += 4) { - output[j] = (byte)(input[i] & 0xff); - output[j + 1] = (byte)((input[i] >> 8) & 0xff); - output[j + 2] = (byte)((input[i] >> 16) & 0xff); - output[j + 3] = (byte)((input[i] >> 24) & 0xff); + output[j] = (byte) (input[i] & 0xff); + output[j + 1] = (byte) ((input[i] >> 8) & 0xff); + output[j + 2] = (byte) ((input[i] >> 16) & 0xff); + output[j + 3] = (byte) ((input[i] >> 24) & 0xff); } } diff --git a/flex/third_party/odps/include/common/sha1.h b/flex/third_party/odps/include/common/sha1.h index dd6f7fcf4ce8..3cd9422f9394 100644 --- a/flex/third_party/odps/include/common/sha1.h +++ b/flex/third_party/odps/include/common/sha1.h @@ -164,7 +164,7 @@ inline void SHA1::transform() { inline void SHA1::add(const uint8_t* data, size_t data_len) { unsigned mlen = (unsigned) ((bits >> 3) % SHA1_INPUT_BYTES); bits += (uint64_t) data_len << 3; - unsigned use = (unsigned) min((size_t)(SHA1_INPUT_BYTES - mlen), data_len); + unsigned use = (unsigned) min((size_t) (SHA1_INPUT_BYTES - mlen), data_len); memcpy(M + mlen, data, use); mlen += use; diff --git a/flex/third_party/odps/include/common/string_piece.h b/flex/third_party/odps/include/common/string_piece.h index c4a83b30adcd..de5fdf99a2bd 100644 --- a/flex/third_party/odps/include/common/string_piece.h +++ b/flex/third_party/odps/include/common/string_piece.h @@ -38,7 +38,7 @@ class StringPieceDetail { StringPieceDetail(const typename string_type::const_iterator& begin, const typename string_type::const_iterator& end) : mStr((end > begin) ? &(*begin) : NULL), - mLength((end > begin) ? (size_type)(end - begin) : 0) {} + mLength((end > begin) ? (size_type) (end - begin) : 0) {} bool operator==(const StringPieceDetail& s) const { return this == &s || this->compare(s) == 0; diff --git a/flex/utils/mmap_vector.h b/flex/utils/mmap_vector.h index 410a899940f8..7bf907b426b8 100644 --- a/flex/utils/mmap_vector.h +++ b/flex/utils/mmap_vector.h @@ -78,5 +78,5 @@ class mmap_vector { mmap_array array_; size_t size_; }; -}; // namespace gs +}; // namespace gs #endif // GRAPHSCOPE_UTILS_MMAP_VECTOR_H_ diff --git a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/global_store_ffi.h b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/global_store_ffi.h index b9db1fd0aeb3..f643c4d8184f 100644 --- a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/global_store_ffi.h +++ b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/global_store_ffi.h @@ -1,12 +1,12 @@ /** * Copyright 2020 Alibaba Group Holding Limited. - * + * * Licensed 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. @@ -28,14 +28,14 @@ typedef int64_t EdgeId; typedef int32_t LabelId; typedef int32_t PartitionId; typedef int32_t PropertyId; -typedef void* GraphHandle; -typedef void* OutEdgeIterator; -typedef void* InEdgeIterator; -typedef void* GetVertexIterator; -typedef void* GetAllVerticesIterator; -typedef void* GetAllEdgesIterator; -typedef void* PropertiesIterator; -typedef void* Schema; +typedef void *GraphHandle; +typedef void *OutEdgeIterator; +typedef void *InEdgeIterator; +typedef void *GetVertexIterator; +typedef void *GetAllVerticesIterator; +typedef void *GetAllEdgesIterator; +typedef void *PropertiesIterator; +typedef void *Schema; typedef int64_t Vertex; struct Edge { @@ -82,7 +82,7 @@ enum PropertyType { struct Property { int id; enum PropertyType type; - void* data; + void *data; int64_t len; }; @@ -103,13 +103,13 @@ void v6d_free_graph_handle(GraphHandle handle); // limit表示返回的最大结果数 // 返回值是一个迭代器 GetVertexIterator v6d_get_vertices(GraphHandle graph, PartitionId partition_id, - LabelId* labels, VertexId* ids, int count); + LabelId *labels, VertexId *ids, int count); // 释放由get_vertices返回的迭代器(也可以什么都不做,取决于具体实现) void v6d_free_get_vertex_iterator(GetVertexIterator iter); // 从迭代器取出下一个元素,返回值是一个Vertex -int v6d_get_vertices_next(GetVertexIterator iter, Vertex* v_out); +int v6d_get_vertices_next(GetVertexIterator iter, Vertex *v_out); // 查询某个partition内部的所有相关label的点 // labels是待查询label的列表 @@ -118,15 +118,15 @@ int v6d_get_vertices_next(GetVertexIterator iter, Vertex* v_out); // 返回值是一个迭代器 // 注意:如果label_count为0或者labels为null,则查询所有label GetAllVerticesIterator v6d_get_all_vertices(GraphHandle graph, - PartitionId partition_id, - LabelId* labels, int labels_count, - int64_t limit); + PartitionId partition_id, + LabelId *labels, int labels_count, + int64_t limit); // 释放由get_all_vertices返回的迭代器(也可以什么都不做,取决于具体实现) void v6d_free_get_all_vertices_iterator(GetAllVerticesIterator iter); // 从迭代器取出下一个元素,返回值是一个Vertex -int v6d_get_all_vertices_next(GetAllVerticesIterator iter, Vertex* v_out); +int v6d_get_all_vertices_next(GetAllVerticesIterator iter, Vertex *v_out); // 获取点id VertexId v6d_get_vertex_id(GraphHandle graph, Vertex v); @@ -134,7 +134,7 @@ VertexId v6d_get_vertex_id(GraphHandle graph, Vertex v); OuterId v6d_get_outer_id(GraphHandle graph, Vertex v); int v6d_get_vertex_by_outer_id(GraphHandle graph, LabelId label_id, - OuterId outer_id, Vertex* v); + OuterId outer_id, Vertex *v); OuterId v6d_get_outer_id_by_vertex_id(GraphHandle graph, VertexId v); @@ -143,7 +143,7 @@ LabelId v6d_get_vertex_label(GraphHandle graph, Vertex v); // 获取点的某个属性 int v6d_get_vertex_property(GraphHandle graph, Vertex v, PropertyId id, - struct Property* p_out); + struct Property *p_out); // 获取点的属性列表,返回一个迭代器 PropertiesIterator v6d_get_vertex_properties(GraphHandle graph, Vertex v); @@ -156,14 +156,14 @@ PropertiesIterator v6d_get_vertex_properties(GraphHandle graph, Vertex v); // src_ids_count和labels_count分别表示src_id和label列表的长度,limit表示返回的最大结果数,返回值是一个迭代器 // 注意:如果label_count为0或者labels为null,则查询所有label OutEdgeIterator v6d_get_out_edges(GraphHandle graph, PartitionId partition_id, - VertexId src_id, LabelId* labels, - int labels_count, int64_t limit); + VertexId src_id, LabelId *labels, + int labels_count, int64_t limit); // 释放迭代器 void v6d_free_out_edge_iterator(OutEdgeIterator iter); // 从迭代器取出下一个元素,返回值是一个Edge -int v6d_out_edge_next(OutEdgeIterator iter, struct Edge* e_out); +int v6d_out_edge_next(OutEdgeIterator iter, struct Edge *e_out); // 查询某个partition内的点的入边 // src_ids是待查询的点id列表 @@ -172,14 +172,14 @@ int v6d_out_edge_next(OutEdgeIterator iter, struct Edge* e_out); // limit表示返回的最大结果数,返回值是一个迭代器 // 注意:如果label_count为0或者labels为null,则查询所有label InEdgeIterator v6d_get_in_edges(GraphHandle graph, PartitionId partition_id, - VertexId dst_id, LabelId* labels, int labels_count, - int64_t limit); + VertexId dst_id, LabelId *labels, + int labels_count, int64_t limit); // 释放迭代器 void v6d_free_in_edge_iterator(InEdgeIterator iter); // 从迭代器取出下一个元素,返回值是一个Edge -int v6d_in_edge_next(InEdgeIterator iter, struct Edge* e_out); +int v6d_in_edge_next(InEdgeIterator iter, struct Edge *e_out); // 查询某个partition内某些label的边数据 // labels是待查询的label列表 @@ -187,43 +187,43 @@ int v6d_in_edge_next(InEdgeIterator iter, struct Edge* e_out); // limit表示返回的最大结果数 // 返回值是一个迭代器。 // 注意:如果label_count为0或者labels为null,则查询所有label -GetAllEdgesIterator v6d_get_all_edges(GraphHandle graph, PartitionId partition_id, - LabelId* labels, int labels_count, - int64_t limit); +GetAllEdgesIterator v6d_get_all_edges(GraphHandle graph, + PartitionId partition_id, LabelId *labels, + int labels_count, int64_t limit); // 释放迭代器 void v6d_free_get_all_edges_iterator(GetAllEdgesIterator iter); // 从迭代器取出下一个元素,返回值是一个Edge -int v6d_get_all_edges_next(GetAllEdgesIterator iter, struct Edge* e_out); +int v6d_get_all_edges_next(GetAllEdgesIterator iter, struct Edge *e_out); // 从edge对象获取起点id -VertexId v6d_get_edge_src_id(GraphHandle graph, struct Edge* e); +VertexId v6d_get_edge_src_id(GraphHandle graph, struct Edge *e); // 从edge对象获取终点id -VertexId v6d_get_edge_dst_id(GraphHandle graph, struct Edge* e); +VertexId v6d_get_edge_dst_id(GraphHandle graph, struct Edge *e); // 从edge对象获取边id -EdgeId v6d_get_edge_id(GraphHandle graph, struct Edge* e); +EdgeId v6d_get_edge_id(GraphHandle graph, struct Edge *e); // 从edge对象获取起点label -LabelId v6d_get_edge_src_label(GraphHandle graph, struct Edge* e); +LabelId v6d_get_edge_src_label(GraphHandle graph, struct Edge *e); // 从edge对象获取终点label -LabelId v6d_get_edge_dst_label(GraphHandle graph, struct Edge* e); +LabelId v6d_get_edge_dst_label(GraphHandle graph, struct Edge *e); // 从edge对象获取边label -LabelId v6d_get_edge_label(GraphHandle graph, struct Edge* e); +LabelId v6d_get_edge_label(GraphHandle graph, struct Edge *e); // 获取边的某个属性 -int v6d_get_edge_property(GraphHandle graph, struct Edge*, PropertyId id, - struct Property* p_out); +int v6d_get_edge_property(GraphHandle graph, struct Edge *, PropertyId id, + struct Property *p_out); // 获取边的属性列表,返回一个迭代器 -PropertiesIterator v6d_get_edge_properties(GraphHandle graph, struct Edge*); +PropertiesIterator v6d_get_edge_properties(GraphHandle graph, struct Edge *); // 从属性列表迭代器中取出一个属性,如果没有新的元素,则将Property对象内部的data置为nullptr -int v6d_properties_next(PropertiesIterator iter, struct Property* p_out); +int v6d_properties_next(PropertiesIterator iter, struct Property *p_out); // 释放迭代器 void v6d_free_properties_iterator(PropertiesIterator iter); @@ -232,53 +232,58 @@ void v6d_free_properties_iterator(PropertiesIterator iter); // 获取属性值,这里需要在c++里判断类型是否正确,比如:对string属性调用get_property_as_int就应该报错,返回-1表示错误,返回0表示正确。 // 如果类型是正确的,则把值填进out指针 -int v6d_get_property_as_bool(struct Property* property, bool* out); -int v6d_get_property_as_char(struct Property* property, char* out); -int v6d_get_property_as_short(struct Property* property, int16_t* out); -int v6d_get_property_as_int(struct Property* property, int* out); -int v6d_get_property_as_long(struct Property* property, int64_t* out); -int v6d_get_property_as_float(struct Property* property, float* out); -int v6d_get_property_as_double(struct Property* property, double* out); - -int v6d_get_property_as_string(struct Property* property, const char** out, - int* out_len); -int v6d_get_property_as_bytes(struct Property* property, const char** out, - int* out_len); -int v6d_get_property_as_int_list(struct Property* property, const int** out, - int* out_len); -int v6d_get_property_as_long_list(struct Property* property, const int64_t** out, - int* out_len); -int v6d_get_property_as_float_list(struct Property* property, const float** out, - int* out_len); -int v6d_get_property_as_double_list(struct Property* property, const double** out, - int* out_len); -int v6d_get_property_as_string_list(struct Property* property, const char*** out, - const int** out_len, int* out_num); -int v6d_get_property_as_date32(struct Property* property, int32_t *out); -int v6d_get_property_as_date64(struct Property* property, int64_t *out); -int v6d_get_property_as_time32_s(struct Property* property, int32_t *out); -int v6d_get_property_as_time32_ms(struct Property* property, int32_t *out); -int v6d_get_property_as_time32_us(struct Property* property, int32_t *out); -int v6d_get_property_as_time32_ns(struct Property* property, int32_t *out); -int v6d_get_property_as_time64_s(struct Property* property, int64_t *out); -int v6d_get_property_as_time64_ms(struct Property* property, int64_t *out); -int v6d_get_property_as_time64_us(struct Property* property, int64_t *out); -int v6d_get_property_as_time64_ns(struct Property* property, int64_t *out); -int v6d_get_property_as_timestamp_s(struct Property* property, int64_t *out, - const char **out_timezone, int *out_timezone_len); -int v6d_get_property_as_timestamp_ms(struct Property* property, int64_t *out, - const char **out_timezone, int *out_timezone_len); -int v6d_get_property_as_timestamp_us(struct Property* property, int64_t *out, - const char **out_timezone, int *out_timezone_len); -int v6d_get_property_as_timestamp_ns(struct Property* property, int64_t *out, - const char **out_timezone, int *out_timezone_len); +int v6d_get_property_as_bool(struct Property *property, bool *out); +int v6d_get_property_as_char(struct Property *property, char *out); +int v6d_get_property_as_short(struct Property *property, int16_t *out); +int v6d_get_property_as_int(struct Property *property, int *out); +int v6d_get_property_as_long(struct Property *property, int64_t *out); +int v6d_get_property_as_float(struct Property *property, float *out); +int v6d_get_property_as_double(struct Property *property, double *out); + +int v6d_get_property_as_string(struct Property *property, const char **out, + int *out_len); +int v6d_get_property_as_bytes(struct Property *property, const char **out, + int *out_len); +int v6d_get_property_as_int_list(struct Property *property, const int **out, + int *out_len); +int v6d_get_property_as_long_list(struct Property *property, + const int64_t **out, int *out_len); +int v6d_get_property_as_float_list(struct Property *property, const float **out, + int *out_len); +int v6d_get_property_as_double_list(struct Property *property, + const double **out, int *out_len); +int v6d_get_property_as_string_list(struct Property *property, + const char ***out, const int **out_len, + int *out_num); +int v6d_get_property_as_date32(struct Property *property, int32_t *out); +int v6d_get_property_as_date64(struct Property *property, int64_t *out); +int v6d_get_property_as_time32_s(struct Property *property, int32_t *out); +int v6d_get_property_as_time32_ms(struct Property *property, int32_t *out); +int v6d_get_property_as_time32_us(struct Property *property, int32_t *out); +int v6d_get_property_as_time32_ns(struct Property *property, int32_t *out); +int v6d_get_property_as_time64_s(struct Property *property, int64_t *out); +int v6d_get_property_as_time64_ms(struct Property *property, int64_t *out); +int v6d_get_property_as_time64_us(struct Property *property, int64_t *out); +int v6d_get_property_as_time64_ns(struct Property *property, int64_t *out); +int v6d_get_property_as_timestamp_s(struct Property *property, int64_t *out, + const char **out_timezone, + int *out_timezone_len); +int v6d_get_property_as_timestamp_ms(struct Property *property, int64_t *out, + const char **out_timezone, + int *out_timezone_len); +int v6d_get_property_as_timestamp_us(struct Property *property, int64_t *out, + const char **out_timezone, + int *out_timezone_len); +int v6d_get_property_as_timestamp_ns(struct Property *property, int64_t *out, + const char **out_timezone, + int *out_timezone_len); // *out_num为string的个数 // (*out_len)[i]为第i个string的长度 // (*out)[i]为第i个string的其实地址 // 释放属性对象 -void v6d_free_property(struct Property* property); +void v6d_free_property(struct Property *property); // ------------------ get schema ------------- // @@ -294,8 +299,8 @@ PartitionId v6d_get_partition_id(GraphHandle graph, VertexId v); // key是\0结束的字符串,如果 key 不存在,返回 -1 // 否则返回0,结果存在 internal_id 和 partition_id 中 int v6d_get_vertex_id_from_primary_key(GraphHandle graph, LabelId label_id, - const char* key, VertexId* internal_id, - PartitionId* partition_id); + const char *key, VertexId *internal_id, + PartitionId *partition_id); // 返回本地的partition列表。 // @@ -306,14 +311,14 @@ int v6d_get_vertex_id_from_primary_key(GraphHandle graph, LabelId label_id, // partition_id_size: partition_ids的长度 // void v6d_get_process_partition_list(GraphHandle graph, - PartitionId** partition_ids, - int* partition_id_size); + PartitionId **partition_ids, + int *partition_id_size); // 释放partition_ids对应的内存 -void v6d_free_partition_list(PartitionId* partition_ids); +void v6d_free_partition_list(PartitionId *partition_ids); #ifdef __cplusplus } #endif -#endif // ANALYTICAL_ENGINE_HTAP_GLOBAL_STORE_FFI_H_ +#endif // ANALYTICAL_ENGINE_HTAP_GLOBAL_STORE_FFI_H_ diff --git a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/graph_builder_ffi.h b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/graph_builder_ffi.h index 3fcd9c93c645..650eae3714bd 100644 --- a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/graph_builder_ffi.h +++ b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/graph_builder_ffi.h @@ -1,12 +1,12 @@ /** * Copyright 2020 Alibaba Group Holding Limited. - * + * * Licensed 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. @@ -28,17 +28,17 @@ extern "C" { // c.f.: https://yuque.antfin-inc.com/7br/graphscope/xwxtm3#KFLSY -typedef void* GraphBuilder; +typedef void *GraphBuilder; typedef int64_t ObjectID; typedef uint64_t InstanceId; typedef int64_t VertexId; typedef int64_t EdgeId; typedef int LabelId; -typedef void* Schema; +typedef void *Schema; typedef int PropertyId; -typedef void* GraphHandle; -typedef void* VertexTypeBuilder; -typedef void* EdgeTypeBuilder; +typedef void *GraphHandle; +typedef void *VertexTypeBuilder; +typedef void *EdgeTypeBuilder; /** * step 1: 创建Local的GraphBuilder @@ -49,8 +49,8 @@ typedef void* EdgeTypeBuilder; * * 📌 vineyard client library会从环境变量中得到vineyard_ipc_socket。 */ -GraphBuilder v6d_create_graph_builder(const char* graph_name, Schema schema, - const int index); +GraphBuilder v6d_create_graph_builder(const char *graph_name, Schema schema, + const int index); /** * 获取当前builder的id,以及所在的instance id,用于step 2的同步。 @@ -58,8 +58,8 @@ GraphBuilder v6d_create_graph_builder(const char* graph_name, Schema schema, * builder_id: 用于接收返回值 * instance_id: 用于接收返回值 */ -void v6d_get_builder_id(GraphBuilder builder, ObjectId* object_id, - InstanceId* instance_id); +void v6d_get_builder_id(GraphBuilder builder, ObjectId *object_id, + InstanceId *instance_id); /** * 创建流程step 2中的global object。 @@ -72,9 +72,9 @@ void v6d_get_builder_id(GraphBuilder builder, ObjectId* object_id, * * 📌 vineyard client library会从环境变量中得到vineyard_ipc_socket */ -ObjectId v6d_build_global_graph_stream(const char* graph_name, size_t size, - ObjectId* object_ids, - InstanceId* instance_ids); +ObjectId v6d_build_global_graph_stream(const char *graph_name, size_t size, + ObjectId *object_ids, + InstanceId *instance_ids); /** * 用于step 3中的根据graph name和index获取对应的graph builder @@ -84,7 +84,7 @@ ObjectId v6d_build_global_graph_stream(const char* graph_name, size_t size, * * 对于invalid的输入,例如graph_name错误或者index错误,返回空指针。 */ -GraphBuilder v6d_get_graph_builder(const char* graph_name, const int index); +GraphBuilder v6d_get_graph_builder(const char *graph_name, const int index); /** * Initialize the builder using schema. @@ -97,31 +97,31 @@ int v6d_initialize_graph_builder(GraphBuilder builder, Schema schema); * 多个property用array的方式给出,property_size指定property array的size。 */ int v6d_add_vertex(GraphBuilder builder, VertexId id, LabelId labelid, - size_t property_size, Property* properties); + size_t property_size, Property *properties); /** * (label, src_label, dst_label) 对应于protobuf定义中的repeated EdgeType。 * * 多个property用array的方式给出,property_size指定property array的size。 */ -int v6d_add_edge(GraphBuilder builder, VertexId src_id, - VertexId dst_id, LabelId label, LabelId src_label, - LabelId dst_label, size_t property_size, Property* properties); +int v6d_add_edge(GraphBuilder builder, VertexId src_id, VertexId dst_id, + LabelId label, LabelId src_label, LabelId dst_label, + size_t property_size, Property *properties); /** * 参数含义与add_vertex一致,都变为array形式,vertex_size给出当前batch的size。 */ -int v6d_add_vertices(GraphBuilder builder, size_t vertex_size, VertexId* ids, - LabelId* labelids, size_t* property_sizes, - Property* properties); +int v6d_add_vertices(GraphBuilder builder, size_t vertex_size, VertexId *ids, + LabelId *labelids, size_t *property_sizes, + Property *properties); /** * 参数含义与add_edge一致,都变为array形式,edge_size给出当前batch的size。 */ -int v6d_add_edges(GraphBuilder builder, size_t edge_size, - VertexId* src_id, VertexId* dst_id, LabelId* labels, - LabelId* src_labels, LabelId* dst_labels, size_t* property_sizes, - Property* properties); +int v6d_add_edges(GraphBuilder builder, size_t edge_size, VertexId *src_id, + VertexId *dst_id, LabelId *labels, LabelId *src_labels, + LabelId *dst_labels, size_t *property_sizes, + Property *properties); /** * 结束local GraphBuilder的build,点、边写完之后分别调用 @@ -143,28 +143,28 @@ void v6d_destroy(GraphBuilder builder); Schema v6d_get_schema(GraphHandle graph); // 释放schema对象 -//TODO: rename to destroy_schema(Schema schema) to be more consistent +// TODO: rename to destroy_schema(Schema schema) to be more consistent void v6d_free_schema(Schema schema); // 根据property name获取property // id,如果找到id,则赋值给out,并且返回0,否则返回-1 -int v6d_get_property_id(Schema schema, const char* name, PropertyId* out); +int v6d_get_property_id(Schema schema, const char *name, PropertyId *out); // 获取属性的类型,如果属性存在,则赋值给out,并且返回0,否则返回-1 int v6d_get_property_type(Schema schema, LabelId label, PropertyId id, - PropertyType* out); + PropertyType *out); // 根据属性id获取属性名,如果属性存在,则赋值给out,并且返回0,否则返回-1 -int v6d_get_property_name(Schema schema, PropertyId id, const char** out); +int v6d_get_property_name(Schema schema, PropertyId id, const char **out); // 根据label名称获取label id,如果label存在则赋值给out,并且返回0,否则返回-1 -int v6d_get_label_id(Schema schema, const char* name, LabelId* out); +int v6d_get_label_id(Schema schema, const char *name, LabelId *out); // 根据label id获取label名称,如果label存在,则赋值给out,并且返回0,否则返回-1 -int v6d_get_label_name(Schema schema, LabelId label, const char** out); +int v6d_get_label_name(Schema schema, LabelId label, const char **out); // 释放从上述接口中获取的字符串 -void v6d_free_string(char* s); +void v6d_free_string(char *s); /********************** 创建Schema相关API **********************/ // 创建Schema builder @@ -172,26 +172,27 @@ Schema v6d_create_schema_builder(); // 根据点类型的id、name创建vertex type builder VertexTypeBuilder v6d_build_vertex_type(Schema schema, LabelId label, - const char* name); + const char *name); // 根据边类型的id、name创建edge type builder -EdgeTypeBuilder v6d_build_edge_type(Schema schema, LabelId label, const char* name); +EdgeTypeBuilder v6d_build_edge_type(Schema schema, LabelId label, + const char *name); // 根据属性id、name、属性type在点类型中增加属性 int v6d_build_vertex_property(VertexTypeBuilder vertex, PropertyId id, - const char* name, PropertyType prop_type); + const char *name, PropertyType prop_type); // 根据属性id、name、属性type在边类型中增加属性 -int v6d_build_edge_property(EdgeTypeBuilder edge, PropertyId id, const char* name, - PropertyType prop_type); +int v6d_build_edge_property(EdgeTypeBuilder edge, PropertyId id, + const char *name, PropertyType prop_type); // 设置点类型的主键列表 int v6d_build_vertex_primary_keys(VertexTypeBuilder vertex, size_t key_count, - const char** key_name_list); + const char **key_name_list); // 在边类型中增加一条 <起点类型-->终点类型> 的关系,一个边类型可以增加多条关系 -int v6d_build_edge_relation(EdgeTypeBuilder edge, const char* src, - const char* dst); +int v6d_build_edge_relation(EdgeTypeBuilder edge, const char *src, + const char *dst); // 完成创建指定的点类型并释放空间 int v6d_finish_build_vertex(VertexTypeBuilder vertex); @@ -206,4 +207,4 @@ Schema v6d_finish_build_schema(Schema schema); } #endif -#endif // SRC_CLIENT_DS_STREAM_PROPERTY_GRAPH_HTAP_H_ +#endif // SRC_CLIENT_DS_STREAM_PROPERTY_GRAPH_HTAP_H_ diff --git a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/graph_schema.h b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/graph_schema.h index e469527bebd3..8a62ab90ea46 100644 --- a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/graph_schema.h +++ b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/graph_schema.h @@ -31,8 +31,8 @@ #include "boost/leaf.hpp" #include "vineyard/common/util/json.h" -#include "vineyard/graph/fragment/property_graph_types.h" #include "vineyard/graph/fragment/graph_schema.h" +#include "vineyard/graph/fragment/property_graph_types.h" namespace vineyard { @@ -46,24 +46,24 @@ class MGPropertyGraphSchema { kAnalytical, }; - public: +public: using LabelId = int; using PropertyId = int; using Entry = vineyard::Entry; - PropertyId GetPropertyId(const std::string& name); + PropertyId GetPropertyId(const std::string &name); PropertyType GetPropertyType(LabelId label_id, PropertyId prop_id); std::string GetPropertyName(PropertyId prop_id); - LabelId GetLabelId(const std::string& name); + LabelId GetLabelId(const std::string &name); std::string GetLabelName(LabelId label_id); void set_fnum(size_t fnum) { fnum_ = fnum; } - Entry* CreateEntry(const std::string& type, LabelId label_id, - const std::string& name); + Entry *CreateEntry(const std::string &type, LabelId label_id, + const std::string &name); - void AddEntry(const Entry& entry) { + void AddEntry(const Entry &entry) { if (entry.type == "VERTEX") { vertex_entries_.push_back(entry); } else { @@ -71,30 +71,30 @@ class MGPropertyGraphSchema { } } - void ToJSON(vineyard::json& root) const; - void FromJSON(vineyard::json const& root); + void ToJSON(vineyard::json &root) const; + void FromJSON(vineyard::json const &root); std::string ToJSONString() const; - void FromJSONString(std::string const& schema); + void FromJSONString(std::string const &schema); size_t fnum() const { return fnum_; } - void DumpToFile(std::string const& path); + void DumpToFile(std::string const &path); MGPropertyGraphSchema TransformToInteractive(); - const std::vector& VertexEntries() const { return vertex_entries_; } + const std::vector &VertexEntries() const { return vertex_entries_; } - const std::vector& EdgeEntries() const { return edge_entries_; } + const std::vector &EdgeEntries() const { return edge_entries_; } - void set_unique_property_names( - const std::vector& property_names) { + void + set_unique_property_names(const std::vector &property_names) { unique_property_names_ = property_names; } void set_schema_type(SchemaType type) { schema_type_ = type; } - private: +private: SchemaType schema_type_ = SchemaType::kAnalytical; size_t fnum_; std::vector vertex_entries_; @@ -102,14 +102,14 @@ class MGPropertyGraphSchema { // In Analytical engine, assume label ids of vertex entries are continuous // from zero, and property ids of each label is also continuous from zero. - // When transform schema to Interactive Engine style, we gather all property names and - // unique them, assign each name a id (index of the vector), then preserve a - // vector for each label, stores mappings from original id to transformed - // id. + // When transform schema to Interactive Engine style, we gather all property + // names and unique them, assign each name a id (index of the vector), then + // preserve a vector for each label, stores mappings from original id to + // transformed id. std::vector unique_property_names_; }; -} // namespace htap -} // namespace vineyard +} // namespace htap +} // namespace vineyard -#endif // MODULES_GRAPH_FRAGMENT_HTAP_GRAPH_SCHEMA_H_ +#endif // MODULES_GRAPH_FRAGMENT_HTAP_GRAPH_SCHEMA_H_ diff --git a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/htap_ds_impl.h b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/htap_ds_impl.h index a516c3ab22e1..f9a27640dde5 100644 --- a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/htap_ds_impl.h +++ b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/htap_ds_impl.h @@ -1,12 +1,12 @@ /** * Copyright 2020 Alibaba Group Holding Limited. - * + * * Licensed 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. @@ -35,11 +35,15 @@ using STRING_OID_TYPE = std::string; using FRAGMENT_TYPE = ::vineyard::ArrowFragment; using INT32_FRAGMENT_TYPE = ::vineyard::ArrowFragment; -using STRING_FRAGMENT_TYPE = ::vineyard::ArrowFragment; +using STRING_FRAGMENT_TYPE = + ::vineyard::ArrowFragment; -using VERTEX_MAP_TYPE = ::vineyard::ArrowVertexMap; -using INT32_VERTEX_MAP_TYPE = ::vineyard::ArrowVertexMap; -using STRING_VERTEX_MAP_TYPE = ::vineyard::ArrowVertexMap; +using VERTEX_MAP_TYPE = + ::vineyard::ArrowVertexMap; +using INT32_VERTEX_MAP_TYPE = + ::vineyard::ArrowVertexMap; +using STRING_VERTEX_MAP_TYPE = + ::vineyard::ArrowVertexMap; using FRAG_ID_TYPE = ::vineyard::fid_t; using EID_TYPE = typename FRAGMENT_TYPE::eid_t; @@ -47,58 +51,58 @@ using VERTEX_RANGE_TYPE = std::pair; using VERTEX_TYPE = typename FRAGMENT_TYPE::vertex_t; struct GraphHandleImpl { - vineyard::Client* client = nullptr; + vineyard::Client *client = nullptr; bool use_int64_oid = true; bool use_int32_oid = true; bool use_string_oid = false; - FRAGMENT_TYPE* fragments = nullptr; - INT32_FRAGMENT_TYPE* int32_fragments = nullptr; - STRING_FRAGMENT_TYPE* string_fragments = nullptr; + FRAGMENT_TYPE *fragments = nullptr; + INT32_FRAGMENT_TYPE *int32_fragments = nullptr; + STRING_FRAGMENT_TYPE *string_fragments = nullptr; - VERTEX_MAP_TYPE* vertex_map = nullptr; - INT32_VERTEX_MAP_TYPE* int32_vertex_map = nullptr; - STRING_VERTEX_MAP_TYPE* string_vertex_map = nullptr; + VERTEX_MAP_TYPE *vertex_map = nullptr; + INT32_VERTEX_MAP_TYPE *int32_vertex_map = nullptr; + STRING_VERTEX_MAP_TYPE *string_vertex_map = nullptr; FRAG_ID_TYPE fnum; vineyard::IdParser vid_parser; vineyard::IdParser eid_parser; - vineyard::htap::MGPropertyGraphSchema* schema = nullptr; + vineyard::htap::MGPropertyGraphSchema *schema = nullptr; FRAG_ID_TYPE local_fnum; - FRAG_ID_TYPE* local_fragments = nullptr; + FRAG_ID_TYPE *local_fragments = nullptr; int vertex_label_num; int edge_label_num; PartitionId channel_num; - VID_TYPE** vertex_chunk_sizes = nullptr; + VID_TYPE **vertex_chunk_sizes = nullptr; }; -inline int get_edge_partition_id(EID_TYPE id, GraphHandleImpl* handle) { +inline int get_edge_partition_id(EID_TYPE id, GraphHandleImpl *handle) { return handle->eid_parser.GetFid(id); } void get_graph_handle(ObjectId id, PartitionId channel_num, - GraphHandleImpl* handle); + GraphHandleImpl *handle); -void free_graph_handle(GraphHandleImpl* handle); +void free_graph_handle(GraphHandleImpl *handle); struct GetVertexIteratorImpl { - VID_TYPE* ids; + VID_TYPE *ids; int count; int index; }; template -void get_vertices(FRAGMENT_TYPE* frag, LabelId* label, VertexId* ids, int count, - GetVertexIteratorImpl* out); +void get_vertices(FRAGMENT_TYPE *frag, LabelId *label, VertexId *ids, int count, + GetVertexIteratorImpl *out); -void free_get_vertex_iterator(GetVertexIteratorImpl* iter); +void free_get_vertex_iterator(GetVertexIteratorImpl *iter); -int get_vertices_next(GetVertexIteratorImpl* iter, Vertex* v_out); +int get_vertices_next(GetVertexIteratorImpl *iter, Vertex *v_out); struct GetAllVerticesIteratorImpl { - VERTEX_RANGE_TYPE* ranges; + VERTEX_RANGE_TYPE *ranges; int range_num; int range_id; @@ -106,19 +110,19 @@ struct GetAllVerticesIteratorImpl { }; template -void get_all_vertices(FRAGMENT_TYPE* frag, PartitionId channel_id, - const VID_TYPE* chunk_sizes, LabelId* labels, +void get_all_vertices(FRAGMENT_TYPE *frag, PartitionId channel_id, + const VID_TYPE *chunk_sizes, LabelId *labels, int labels_count, int64_t limit, - GetAllVerticesIteratorImpl* out); + GetAllVerticesIteratorImpl *out); -void free_get_all_vertices_iterator(GetAllVerticesIteratorImpl* iter); +void free_get_all_vertices_iterator(GetAllVerticesIteratorImpl *iter); -int get_all_vertices_next(GetAllVerticesIteratorImpl* iter, Vertex* v_out); +int get_all_vertices_next(GetAllVerticesIteratorImpl *iter, Vertex *v_out); struct PropertiesIteratorImpl { - GraphHandleImpl* handle; - arrow::Table* table; - bool vertex_or_edge; // true: vertex, false: edge + GraphHandleImpl *handle; + arrow::Table *table; + bool vertex_or_edge; // true: vertex, false: edge LabelId label_id; int64_t row_id; PropertyId col_num; @@ -126,62 +130,62 @@ struct PropertiesIteratorImpl { }; template -typename FRAGMENT_TYPE::oid_t get_outer_id(FRAGMENT_TYPE* frag, Vertex v); +typename FRAGMENT_TYPE::oid_t get_outer_id(FRAGMENT_TYPE *frag, Vertex v); template -int get_vertex_property(FRAGMENT_TYPE* frag, Vertex v, PropertyId id, - Property* p_out); +int get_vertex_property(FRAGMENT_TYPE *frag, Vertex v, PropertyId id, + Property *p_out); template -void get_vertex_properties(FRAGMENT_TYPE* frag, Vertex v, - PropertiesIteratorImpl* iter); +void get_vertex_properties(FRAGMENT_TYPE *frag, Vertex v, + PropertiesIteratorImpl *iter); using NBR_TYPE = typename FRAGMENT_TYPE::nbr_unit_t; // using ADJ_LIST_TYPE = std::pair; struct AdjListUnit { - const NBR_TYPE* begin; - const NBR_TYPE* end; + const NBR_TYPE *begin; + const NBR_TYPE *end; LabelId label; } __attribute__((packed)); struct EdgeIteratorImpl { // FRAG_ID_TYPE fid; - FRAGMENT_TYPE* fragment = nullptr; - INT32_FRAGMENT_TYPE* int32_fragment = nullptr; - STRING_FRAGMENT_TYPE* string_fragment = nullptr; - vineyard::IdParser* eid_parser; + FRAGMENT_TYPE *fragment = nullptr; + INT32_FRAGMENT_TYPE *int32_fragment = nullptr; + STRING_FRAGMENT_TYPE *string_fragment = nullptr; + vineyard::IdParser *eid_parser; int64_t src; - AdjListUnit* lists; + AdjListUnit *lists; int list_num; int list_id; - const NBR_TYPE* cur_edge; + const NBR_TYPE *cur_edge; }; -void empty_edge_iterator(EdgeIteratorImpl* iter); +void empty_edge_iterator(EdgeIteratorImpl *iter); template -void get_out_edges(FRAGMENT_TYPE* frag, - vineyard::IdParser* eid_parser, VertexId src_id, - LabelId* labels, int labels_count, int64_t limit, - EdgeIteratorImpl* iter); +void get_out_edges(FRAGMENT_TYPE *frag, + vineyard::IdParser *eid_parser, VertexId src_id, + LabelId *labels, int labels_count, int64_t limit, + EdgeIteratorImpl *iter); -int out_edge_next(EdgeIteratorImpl* iter, Edge* e_out); +int out_edge_next(EdgeIteratorImpl *iter, Edge *e_out); template -void get_in_edges(FRAGMENT_TYPE* frag, vineyard::IdParser* eid_parser, - VertexId dst_id, LabelId* labels, int labels_count, - int64_t limit, EdgeIteratorImpl* iter); +void get_in_edges(FRAGMENT_TYPE *frag, vineyard::IdParser *eid_parser, + VertexId dst_id, LabelId *labels, int labels_count, + int64_t limit, EdgeIteratorImpl *iter); -int in_edge_next(EdgeIteratorImpl* iter, Edge* e_out); +int in_edge_next(EdgeIteratorImpl *iter, Edge *e_out); struct GetAllEdgesIteratorImpl { - FRAGMENT_TYPE* fragment = nullptr; - INT32_FRAGMENT_TYPE* int32_fragment = nullptr; - STRING_FRAGMENT_TYPE* string_fragment = nullptr; - LabelId* e_labels; - vineyard::IdParser* eid_parser; + FRAGMENT_TYPE *fragment = nullptr; + INT32_FRAGMENT_TYPE *int32_fragment = nullptr; + STRING_FRAGMENT_TYPE *string_fragment = nullptr; + LabelId *e_labels; + vineyard::IdParser *eid_parser; int e_labels_count; int cur_v_label; @@ -189,7 +193,7 @@ struct GetAllEdgesIteratorImpl { EdgeIteratorImpl ei; - const VID_TYPE* chunk_sizes; + const VID_TYPE *chunk_sizes; PartitionId channel_id; int64_t index; @@ -197,32 +201,32 @@ struct GetAllEdgesIteratorImpl { }; template -void get_all_edges(FRAGMENT_TYPE* frag, PartitionId channel_id, - const VID_TYPE* chunk_sizes, - vineyard::IdParser* eid_parser, LabelId* labels, +void get_all_edges(FRAGMENT_TYPE *frag, PartitionId channel_id, + const VID_TYPE *chunk_sizes, + vineyard::IdParser *eid_parser, LabelId *labels, int labels_count, int64_t limit, - GetAllEdgesIteratorImpl* iter); + GetAllEdgesIteratorImpl *iter); -int get_all_edges_next(GetAllEdgesIteratorImpl* iter, Edge* e_out); +int get_all_edges_next(GetAllEdgesIteratorImpl *iter, Edge *e_out); -void free_edge_iterator(EdgeIteratorImpl* iter); +void free_edge_iterator(EdgeIteratorImpl *iter); -void free_get_all_edges_iterator(GetAllEdgesIteratorImpl* iter); +void free_get_all_edges_iterator(GetAllEdgesIteratorImpl *iter); template -EdgeId get_edge_id(FRAGMENT_TYPE* frag, LabelId label, int64_t offset); +EdgeId get_edge_id(FRAGMENT_TYPE *frag, LabelId label, int64_t offset); template -int get_edge_property(FRAGMENT_TYPE* frag, LabelId label, int64_t offset, - PropertyId id, Property* p_out); +int get_edge_property(FRAGMENT_TYPE *frag, LabelId label, int64_t offset, + PropertyId id, Property *p_out); template -void get_edge_properties(FRAGMENT_TYPE* frag, LabelId label, int64_t offset, - PropertiesIteratorImpl* iter); +void get_edge_properties(FRAGMENT_TYPE *frag, LabelId label, int64_t offset, + PropertiesIteratorImpl *iter); -int properties_next(PropertiesIteratorImpl* iter, Property* p_out); +int properties_next(PropertiesIteratorImpl *iter, Property *p_out); -void free_properties_iterator(PropertiesIteratorImpl* iter); +void free_properties_iterator(PropertiesIteratorImpl *iter); union PodProperties { bool bool_value; @@ -234,46 +238,50 @@ union PodProperties { double double_value; }; -int get_property_as_bool(Property* property, bool* out); -int get_property_as_char(Property* property, char* out); -int get_property_as_short(Property* property, int16_t* out); -int get_property_as_int(Property* property, int* out); -int get_property_as_long(Property* property, int64_t* out); -int get_property_as_float(Property* property, float* out); -int get_property_as_double(Property* property, double* out); -int get_property_as_string(Property* property, const char** out, int* out_len); -int get_property_as_bytes(Property* property, const char** out, int* out_len); -int get_property_as_int_list(Property* property, const int** out, int* out_len); -int get_property_as_long_list(Property* property, const int64_t** out, - int* out_len); -int get_property_as_float_list(Property* property, const float** out, - int* out_len); -int get_property_as_double_list(Property* property, const double** out, - int* out_len); -int get_property_as_string_list(Property* property, const char*** out, - const int** out_len, int* out_num); -int get_property_as_date32(struct Property* property, int32_t *out); -int get_property_as_date64(struct Property* property, int64_t *out); -int get_property_as_time32_s(struct Property* property, int32_t *out); -int get_property_as_time32_ms(struct Property* property, int32_t *out); -int get_property_as_time32_us(struct Property* property, int32_t *out); -int get_property_as_time32_ns(struct Property* property, int32_t *out); -int get_property_as_time64_s(struct Property* property, int64_t *out); -int get_property_as_time64_ms(struct Property* property, int64_t *out); -int get_property_as_time64_us(struct Property* property, int64_t *out); -int get_property_as_time64_ns(struct Property* property, int64_t *out); -int get_property_as_timestamp_s(struct Property* property, int64_t *out, - const char **out_timezone, int *out_timezone_len); -int get_property_as_timestamp_ms(struct Property* property, int64_t *out, - const char **out_timezone, int *out_timezone_len); -int get_property_as_timestamp_us(struct Property* property, int64_t *out, - const char **out_timezone, int *out_timezone_len); -int get_property_as_timestamp_ns(struct Property* property, int64_t *out, - const char **out_timezone, int *out_timezone_len); - -void free_property(Property* property); - -} // namespace htap_impl -} // namespace vineyard - -#endif // ANALYTICAL_ENGINE_HTAP_HTAP_DS_IMPL_H_ +int get_property_as_bool(Property *property, bool *out); +int get_property_as_char(Property *property, char *out); +int get_property_as_short(Property *property, int16_t *out); +int get_property_as_int(Property *property, int *out); +int get_property_as_long(Property *property, int64_t *out); +int get_property_as_float(Property *property, float *out); +int get_property_as_double(Property *property, double *out); +int get_property_as_string(Property *property, const char **out, int *out_len); +int get_property_as_bytes(Property *property, const char **out, int *out_len); +int get_property_as_int_list(Property *property, const int **out, int *out_len); +int get_property_as_long_list(Property *property, const int64_t **out, + int *out_len); +int get_property_as_float_list(Property *property, const float **out, + int *out_len); +int get_property_as_double_list(Property *property, const double **out, + int *out_len); +int get_property_as_string_list(Property *property, const char ***out, + const int **out_len, int *out_num); +int get_property_as_date32(struct Property *property, int32_t *out); +int get_property_as_date64(struct Property *property, int64_t *out); +int get_property_as_time32_s(struct Property *property, int32_t *out); +int get_property_as_time32_ms(struct Property *property, int32_t *out); +int get_property_as_time32_us(struct Property *property, int32_t *out); +int get_property_as_time32_ns(struct Property *property, int32_t *out); +int get_property_as_time64_s(struct Property *property, int64_t *out); +int get_property_as_time64_ms(struct Property *property, int64_t *out); +int get_property_as_time64_us(struct Property *property, int64_t *out); +int get_property_as_time64_ns(struct Property *property, int64_t *out); +int get_property_as_timestamp_s(struct Property *property, int64_t *out, + const char **out_timezone, + int *out_timezone_len); +int get_property_as_timestamp_ms(struct Property *property, int64_t *out, + const char **out_timezone, + int *out_timezone_len); +int get_property_as_timestamp_us(struct Property *property, int64_t *out, + const char **out_timezone, + int *out_timezone_len); +int get_property_as_timestamp_ns(struct Property *property, int64_t *out, + const char **out_timezone, + int *out_timezone_len); + +void free_property(Property *property); + +} // namespace htap_impl +} // namespace vineyard + +#endif // ANALYTICAL_ENGINE_HTAP_HTAP_DS_IMPL_H_ diff --git a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/htap_types.h b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/htap_types.h index f9d5ecb57c5b..c5eb09786ea9 100644 --- a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/htap_types.h +++ b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/htap_types.h @@ -1,12 +1,12 @@ /** * Copyright 2020 Alibaba Group Holding Limited. - * + * * Licensed 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. @@ -35,7 +35,7 @@ using ORIGIN_EID_TYPE = int64_t; using PROP_ID_TYPE = int; using LABEL_ID_TYPE = int; -using ADJ_LIST_TYPE = std::pair; +using ADJ_LIST_TYPE = std::pair; using VERTEX_RANGE_TYPE = std::pair; using VID_ARRAY = typename ConvertToArrowType::ArrayType; @@ -65,10 +65,10 @@ union PodProperties { double double_value; }; -} // namespace htap_types +} // namespace htap_types -} // namespace htap +} // namespace htap -} // namespace vineyard +} // namespace vineyard -#endif // SRC_CLIENT_DS_GRAPH_FRAGMENT_HTAP_TYPES_H_ +#endif // SRC_CLIENT_DS_GRAPH_FRAGMENT_HTAP_TYPES_H_ diff --git a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/property_graph_stream.h b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/property_graph_stream.h index c97ee6c4fba1..f962e3e80736 100644 --- a/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/property_graph_stream.h +++ b/interactive_engine/executor/store/global_query/src/store_impl/v6d/native/property_graph_stream.h @@ -1,12 +1,12 @@ /** * Copyright 2020 Alibaba Group Holding Limited. - * + * * Licensed 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. @@ -29,8 +29,8 @@ #include "arrow/record_batch.h" #include "arrow/type.h" #include "arrow/util/config.h" -#include "arrow/util/logging.h" #include "arrow/util/key_value_metadata.h" +#include "arrow/util/logging.h" #include "grape/worker/comm_spec.h" @@ -44,9 +44,9 @@ #include "vineyard/common/util/json.h" #include "vineyard/common/util/status.h" +#include "graph_builder_ffi.h" #include "graph_schema.h" #include "htap_types.h" -#include "graph_builder_ffi.h" namespace vineyard { namespace htap { @@ -54,195 +54,187 @@ namespace htap { namespace detail { std::shared_ptr PropertyTypeToDataType(::PropertyType type); -::PropertyType PropertyTypeFromDataType(std::shared_ptr const &type); +::PropertyType +PropertyTypeFromDataType(std::shared_ptr const &type); -template -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { LOG(FATAL) << "Unimplemented..."; } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; CHECK_ARROW_ERROR( - dynamic_cast(builder)->Append(pp.bool_value)); + dynamic_cast(builder)->Append(pp.bool_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; CHECK_ARROW_ERROR( - dynamic_cast(builder)->Append(pp.char_value)); + dynamic_cast(builder)->Append(pp.char_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; CHECK_ARROW_ERROR( - dynamic_cast(builder)->Append(pp.int16_value)); + dynamic_cast(builder)->Append(pp.int16_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; CHECK_ARROW_ERROR( - dynamic_cast(builder)->Append(pp.int_value)); + dynamic_cast(builder)->Append(pp.int_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { CHECK_ARROW_ERROR( - dynamic_cast(builder)->Append(prop->len)); + dynamic_cast(builder)->Append(prop->len)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; CHECK_ARROW_ERROR( - dynamic_cast(builder)->Append(pp.float_value)); + dynamic_cast(builder)->Append(pp.float_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; CHECK_ARROW_ERROR( - dynamic_cast(builder)->Append(pp.double_value)); + dynamic_cast(builder)->Append(pp.double_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { - CHECK_ARROW_ERROR(dynamic_cast(builder)->Append( - static_cast(prop->data), prop->len)); +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { + CHECK_ARROW_ERROR( + dynamic_cast(builder)->Append( + static_cast(prop->data), prop->len)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; - CHECK_ARROW_ERROR(dynamic_cast(builder)->Append(pp.int_value)); + CHECK_ARROW_ERROR( + dynamic_cast(builder)->Append(pp.int_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; - CHECK_ARROW_ERROR(dynamic_cast(builder)->Append(pp.long_value)); + CHECK_ARROW_ERROR( + dynamic_cast(builder)->Append(pp.long_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; - CHECK_ARROW_ERROR(dynamic_cast(builder)->Append(pp.int_value)); + CHECK_ARROW_ERROR( + dynamic_cast(builder)->Append(pp.int_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; - CHECK_ARROW_ERROR(dynamic_cast(builder)->Append(pp.long_value)); + CHECK_ARROW_ERROR( + dynamic_cast(builder)->Append(pp.long_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { vineyard::htap::htap_types::PodProperties pp; pp.long_value = prop->len; - CHECK_ARROW_ERROR(dynamic_cast(builder)->Append(pp.long_value)); + CHECK_ARROW_ERROR(dynamic_cast(builder)->Append( + pp.long_value)); } }; -template <> -struct AppendProperty { - static void append(arrow::ArrayBuilder* builder, Property const* prop) { +template <> struct AppendProperty { + static void append(arrow::ArrayBuilder *builder, Property const *prop) { CHECK_ARROW_ERROR( - dynamic_cast(builder)->Append(nullptr)); + dynamic_cast(builder)->Append(nullptr)); } }; template -void generic_appender(arrow::ArrayBuilder* builder, T const& value) { +void generic_appender(arrow::ArrayBuilder *builder, T const &value) { CHECK_ARROW_ERROR( - dynamic_cast::BuilderType*>(builder) + dynamic_cast::BuilderType *>(builder) ->Append(value)); } -using property_appender_func = void (*)(arrow::ArrayBuilder*, - Property const* prop); +using property_appender_func = void (*)(arrow::ArrayBuilder *, + Property const *prop); class PropertyTableAppender { - public: +public: explicit PropertyTableAppender(std::shared_ptr schema); // apply for vertex values and properties. - void Apply(std::unique_ptr& builder, VertexId id, - size_t property_size, Property* properties, - std::map const& property_id_mapping, - std::shared_ptr& batch_out); + void Apply(std::unique_ptr &builder, VertexId id, + size_t property_size, Property *properties, + std::map const &property_id_mapping, + std::shared_ptr &batch_out); // apply for edge values and properties. - void Apply(std::unique_ptr& builder, - VertexId src_id, VertexId dst_id, - LabelId src_label, LabelId dst_label, size_t property_size, - Property* properties, - std::map const& property_id_mapping, - std::shared_ptr& batch_out); - - void Flush(std::unique_ptr& builder, - std::shared_ptr& batches_out, + void Apply(std::unique_ptr &builder, + VertexId src_id, VertexId dst_id, LabelId src_label, + LabelId dst_label, size_t property_size, Property *properties, + std::map const &property_id_mapping, + std::shared_ptr &batch_out); + + void Flush(std::unique_ptr &builder, + std::shared_ptr &batches_out, const bool allow_empty = false); - private: +private: std::vector funcs_; size_t col_num_; }; -} // namespace detail +} // namespace detail class PropertyGraphInStream; class PropertyGraphOutStream : public Registered { - public: +public: const uint64_t instance_id() const { return meta_.GetClient()->instance_id(); } - static std::unique_ptr Create( - Client& client, const char* graph_name, MGPropertyGraphSchema* schema, - const int index) { - auto s = std::unique_ptr(new PropertyGraphOutStream()); + static std::unique_ptr + Create(Client &client, const char *graph_name, MGPropertyGraphSchema *schema, + const int index) { + auto s = + std::unique_ptr(new PropertyGraphOutStream()); // take ownership of the `MGPropertyGraphSchema` object. s->graph_schema_ = std::shared_ptr(schema); @@ -250,25 +242,25 @@ class PropertyGraphOutStream : public Registered { // create two streams for vertices and edges { std::unordered_map params{ - {"kind", "vertex"}, {"graph_name", graph_name} - }; + {"kind", "vertex"}, {"graph_name", graph_name}}; auto stream_id = StreamBuilder::Make(client, params); s->vertex_stream_ = client.GetObject(stream_id); VINEYARD_DISCARD(client.Persist(s->vertex_stream_->id())); // Don't "OpenWriter" when creating, it will be "Get and Construct" again - // VINEYARD_CHECK_OK(s->vertex_stream_->OpenWriter(client, s->vertex_writer_)); + // VINEYARD_CHECK_OK(s->vertex_stream_->OpenWriter(client, + // s->vertex_writer_)); } { std::unordered_map params{ - {"kind", "edge"}, {"graph_name", graph_name} - }; + {"kind", "edge"}, {"graph_name", graph_name}}; auto stream_id = StreamBuilder::Make(client, params); s->edge_stream_ = client.GetObject(stream_id); VINEYARD_DISCARD(client.Persist(s->edge_stream_->id())); // Don't "OpenWriter" when creating, it will be "Get and Construct" again - // VINEYARD_CHECK_OK(s->edge_stream_->OpenWriter(client, s->edge_writer_)); + // VINEYARD_CHECK_OK(s->edge_stream_->OpenWriter(client, + // s->edge_writer_)); } s->stream_index_ = index; @@ -290,17 +282,19 @@ class PropertyGraphOutStream : public Registered { std::unique_ptr(new PropertyGraphOutStream())); } - void Construct(const ObjectMeta& meta) override { + void Construct(const ObjectMeta &meta) override { meta_ = meta; this->id_ = ObjectIDFromString(meta.GetKeyValue("id")); this->stream_index_ = meta.GetKeyValue("stream_index"); this->vertex_stream_ = - std::dynamic_pointer_cast(meta.GetMember("vertex_stream")); - this->edge_stream_ = - std::dynamic_pointer_cast(meta.GetMember("edge_stream")); + std::dynamic_pointer_cast( + meta.GetMember("vertex_stream")); + this->edge_stream_ = std::dynamic_pointer_cast( + meta.GetMember("edge_stream")); this->graph_schema_ = std::make_shared(); - auto graph_schema_json = vineyard::json::parse(meta.GetKeyValue("graph_schema")); + auto graph_schema_json = + vineyard::json::parse(meta.GetKeyValue("graph_schema")); if (graph_schema_json.contains("types")) { graph_schema_->FromJSON(graph_schema_json); this->initialTables(); @@ -319,19 +313,18 @@ class PropertyGraphOutStream : public Registered { int Initialize(Schema schema); int AddVertex(VertexId id, LabelId labelid, size_t property_size, - Property* properties); + Property *properties); int AddEdge(VertexId src_id, VertexId dst_id, LabelId label, - LabelId src_label, LabelId dst_label, size_t property_size, - Property* properties); + LabelId src_label, LabelId dst_label, size_t property_size, + Property *properties); - int AddVertices(size_t vertex_size, VertexId* ids, LabelId* labelids, - size_t* property_sizes, Property* properties); + int AddVertices(size_t vertex_size, VertexId *ids, LabelId *labelids, + size_t *property_sizes, Property *properties); - int AddEdges(size_t edge_size, VertexId* src_ids, - VertexId* dst_ids, LabelId* labels, LabelId* src_labels, - LabelId* dst_labels, size_t* property_sizes, - Property* properties); + int AddEdges(size_t edge_size, VertexId *src_ids, VertexId *dst_ids, + LabelId *labels, LabelId *src_labels, LabelId *dst_labels, + size_t *property_sizes, Property *properties); Status Abort(); @@ -341,12 +334,13 @@ class PropertyGraphOutStream : public Registered { int stream_index() const { return stream_index_; } - private: +private: void initialTables(); - void buildTableChunk(std::shared_ptr batch, - std::shared_ptr &output_stream, - int const property_offset, - std::map const& property_id_mapping); + void + buildTableChunk(std::shared_ptr batch, + std::shared_ptr &output_stream, + int const property_offset, + std::map const &property_id_mapping); std::shared_ptr graph_schema_; @@ -356,8 +350,8 @@ class PropertyGraphOutStream : public Registered { std::map> vertex_builders_; - // vertex label id to its primary key column (assuming only single column key) ordinal mapping - // -1 means no primary key column + // vertex label id to its primary key column (assuming only single column key) + // ordinal mapping -1 means no primary key column static constexpr size_t kNoPrimaryKeyColumn = static_cast(-1); std::map vertex_primary_key_column_; std::map> @@ -381,11 +375,11 @@ class PropertyGraphOutStream : public Registered { }; class PropertyGraphInStream { - public: - explicit PropertyGraphInStream(vineyard::Client &client, PropertyGraphOutStream& stream, bool vertex) +public: + explicit PropertyGraphInStream(vineyard::Client &client, + PropertyGraphOutStream &stream, bool vertex) : vertex_stream_(stream.vertex_stream_), - edge_stream_(stream.edge_stream_), - graph_schema_(stream.graph_schema_) { + edge_stream_(stream.edge_stream_), graph_schema_(stream.graph_schema_) { if (vertex) { VINEYARD_CHECK_OK(vertex_stream_->OpenReader(&client)); } else { @@ -393,13 +387,13 @@ class PropertyGraphInStream { } } - Status GetNextVertices(Client& client, - std::shared_ptr& vertices) { + Status GetNextVertices(Client &client, + std::shared_ptr &vertices) { return vertex_stream_->ReadBatch(vertices, true); } - Status GetNextEdges(Client& client, - std::shared_ptr& edges) { + Status GetNextEdges(Client &client, + std::shared_ptr &edges) { return edge_stream_->ReadBatch(edges, true); } @@ -407,7 +401,7 @@ class PropertyGraphInStream { return graph_schema_; } - private: +private: std::shared_ptr vertex_stream_; std::shared_ptr edge_stream_; std::shared_ptr graph_schema_; @@ -416,7 +410,7 @@ class PropertyGraphInStream { class GlobalPGStreamBuilder; class GlobalPGStream : public Registered, GlobalObject { - public: +public: static std::unique_ptr Create() __attribute__((used)) { return std::static_pointer_cast( std::unique_ptr(new GlobalPGStream())); @@ -426,14 +420,14 @@ class GlobalPGStream : public Registered, GlobalObject { return local_streams_[index % local_stream_chunks_]; } - const std::vector>& AvailableStreams( - Client& client) const { + const std::vector> & + AvailableStreams(Client &client) const { return local_streams_; } - void Construct(const ObjectMeta& meta) override; + void Construct(const ObjectMeta &meta) override; - private: +private: size_t local_stream_chunks_; size_t total_stream_chunks_; std::vector> local_streams_; @@ -443,9 +437,8 @@ class GlobalPGStream : public Registered, GlobalObject { }; class GlobalPGStreamBuilder : public ObjectBuilder { - public: - explicit GlobalPGStreamBuilder(Client& client) - : total_stream_chunks_(0) {} +public: + explicit GlobalPGStreamBuilder(Client &client) : total_stream_chunks_(0) {} ~GlobalPGStreamBuilder() = default; void AddStream(int const /* index */, ObjectID const stream_id, @@ -454,18 +447,16 @@ class GlobalPGStreamBuilder : public ObjectBuilder { stream_chunks_.push_back(stream_id); } - Status Build(Client& client) { - return Status::OK(); - } + Status Build(Client &client) { return Status::OK(); } - Status _Seal(Client& client, std::shared_ptr&object); + Status _Seal(Client &client, std::shared_ptr &object); - private: +private: std::vector stream_chunks_; size_t total_stream_chunks_; }; -} // namespace htap -} // namespace vineyard +} // namespace htap +} // namespace vineyard -#endif // SRC_CLIENT_DS_STREAM_PROPERTY_GRAPH_H_ +#endif // SRC_CLIENT_DS_STREAM_PROPERTY_GRAPH_H_ diff --git a/python/graphscope/learning/gl_torch_examples/store_learning.py b/python/graphscope/learning/gl_torch_examples/store_learning.py index ff0b6d99b9d6..c578908f456d 100644 --- a/python/graphscope/learning/gl_torch_examples/store_learning.py +++ b/python/graphscope/learning/gl_torch_examples/store_learning.py @@ -1,182 +1,199 @@ -import base64 -import json -from multiprocessing.reduction import ForkingPickler -from typing import Dict -from typing import List -from typing import Optional -from typing import Tuple - -from torch_geometric.data.graph_store import EdgeAttr -from torch_geometric.data.graph_store import GraphStore -from torch_geometric.typing import EdgeTensorType - -from graphscope.learning.graphlearn_torch.distributed.dist_client import request_server -from graphscope.learning.graphlearn_torch.distributed.dist_server import DistServer - - -class GsGraphStore(GraphStore): - def __init__(self, endpoints, handle=None, config=None, graph=None) -> None: - super().__init__() - self.handle = handle - self.config = config - self.edge_attrs: Dict[Tuple[Tuple[str, str, str], str, bool], EdgeAttr] = {} - - if config is not None: - config = json.loads( - base64.b64decode(config.encode("utf-8", errors="ignore")).decode( - "utf-8", errors="ignore" - ) - ) - self.edges = config["edges"] - self.edge_weights = config["edge_weights"] - self.edge_dir = config["edge_dir"] - self.random_node_split = config["random_node_split"] - if self.edges is not None: - for edge in self.edges: - edge = tuple(edge) - if self.edge_dir is not None: - layout = "csr" if self.edge_dir == "out" else "csc" - is_sorted = False if layout == "csr" else True - self.edge_attrs[(edge, layout, is_sorted)] = EdgeAttr( - edge, layout, is_sorted - ) - else: - layout = "coo" - self.edge_attrs[(edge, layout, False)] = EdgeAttr( - edge, layout, is_sorted - ) - - assert len(endpoints) == 4 - self.endpoints = endpoints - self._master_addr, self._server_client_master_port = endpoints[0].split(":") - self._train_master_addr, self._train_loader_master_port = endpoints[1].split( - ":" +import time + +import torch +import torch.nn.functional as F +from ogb.nodeproppred import Evaluator +from torch_geometric.data import EdgeAttr +from torch_geometric.nn import GraphSAGE + +import graphscope as gs +import graphscope.learning.graphlearn_torch as glt +from graphscope.dataset import load_ogbn_arxiv +from graphscope.learning.gs_feature_store import GsTensorAttr + +NUM_EPOCHS = 10 +BATCH_SIZE = 4 +NUM_SERVERS = 1 +NUM_NEIGHBORS = [2, 2, 2] + +print("Batch size:", BATCH_SIZE) +print("Number of epochs:", NUM_EPOCHS) +print("Number of neighbors:", NUM_NEIGHBORS) + +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +print("Using device:", device) + +gs.set_option(log_level="DEBUG") +gs.set_option(show_log=True) + +# load the ogbn_arxiv graph. +sess = gs.session(cluster_type="hosts", num_workers=NUM_SERVERS) +g = load_ogbn_arxiv(sess=sess) + +print("-- Initializing store ...") +feature_store, graph_store = gs.pyg_remote_backend( + g, + edges=[ + ("paper", "citation", "paper"), + ], + node_features={ + "paper": [f"feat_{i}" for i in range(128)], + }, + node_labels={ + "paper": "label", + }, + edge_dir="out", + random_node_split={ + "num_val": 0.1, + "num_test": 0.1, + }, +) + +print("-- Initializing client ...") +glt.distributed.init_client( + num_servers=1, + num_clients=1, + client_rank=0, + master_addr=graph_store.master_addr, + master_port=graph_store.server_client_master_port, + num_rpc_threads=4, + is_dynamic=True, +) + +model = GraphSAGE( + in_channels=128, + hidden_channels=256, + num_layers=3, + out_channels=47, +).to(device) +optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4) + + +graph_topo = graph_store.get_edge_index(EdgeAttr(("paper", "citation", "paper"), "csr")) +DATA_LENGTH = graph_topo[0].size(0) - 1 +TRAIN_DATA_LENGTH = 200 +print("Number of nodes:", DATA_LENGTH) + + +def get_neighbors(node_id, neighbor_num): + indices_start = graph_topo[0][node_id] + indices_end = graph_topo[0][node_id + 1] + neighbors = graph_topo[1][indices_start:indices_end] + random = torch.randperm(neighbors.size(0)) + neighbors = neighbors[random[:neighbor_num]] + return neighbors + + +def get_node_feature(node_id): + node_feature = feature_store.get_tensor( + GsTensorAttr(group_name="paper", index=node_id) + ) + return node_feature + + +def get_node_label(node_id): + node_feature = feature_store.get_tensor( + GsTensorAttr( + group_name="paper", attr_name="label", index=node_id, is_label=True ) - self._val_master_addr, self._val_loader_master_port = endpoints[2].split(":") - self._test_master_addr, self._test_loader_master_port = endpoints[3].split(":") - assert ( - self._master_addr - == self._train_master_addr - == self._val_master_addr - == self._test_master_addr - ) - - @property - def master_addr(self): - return self._master_addr - - @property - def train_master_addr(self): - return self._train_master_addr - - @property - def val_master_addr(self): - return self._val_master_addr - - @property - def test_master_addr(self): - return self._test_master_addr - - @property - def server_client_master_port(self): - return self._server_client_master_port - - @property - def train_loader_master_port(self): - return self._train_loader_master_port - - @property - def val_loader_master_port(self): - return self._val_loader_master_port - - @property - def test_loader_master_port(self): - return self._test_loader_master_port - - def get_handle(self): - return self.handle - - def get_config(self): - return self.config - - def get_endpoints(self): - return self.endpoints - - @staticmethod - def key(attr: EdgeAttr) -> Tuple: - return (attr.edge_type, attr.layout.value, attr.is_sorted, attr.size) - - def _put_edge_index( - self, - edge_index: EdgeTensorType, - edge_attr: EdgeAttr, - ) -> bool: - r"""To be implemented by :class:`GsFeatureStore`.""" - raise NotImplementedError - - def _get_edge_index(self, edge_attr: EdgeAttr) -> Optional[EdgeTensorType]: - r"""Obtains a :class:`EdgeTensorType` from the remote server with :class:`EdgeAttr`. - - Args: - edge_attr(`EdgeAttr`): Uniquely corresponds to a topology of subgraph . - - Returns: - edge_index(`EdgeTensorType`): The edge index tensor, which is a :class:`tuple` of\ - (row indice tensor, column indice tensor)(COO)\ - (row ptr tensor, column indice tensor)(CSR)\ - (column ptr tensor, row indice tensor)(CSC). - """ - group_name, layout, is_sorted, _ = self.key(edge_attr) - edge_index = None - edge_index, size = request_server( - 0, DistServer.get_edge_index, group_name, layout - ) - if edge_index is not None: - new_edge_attr = EdgeAttr(group_name, layout, is_sorted, size) - self.edge_attrs[(group_name, layout, is_sorted)] = new_edge_attr - return edge_index - - def _remove_edge_index(self, edge_attr: EdgeAttr) -> bool: - r"""To be implemented by :class:`GsFeatureStore`.""" - raise NotImplementedError - - def get_all_edge_attrs(self) -> List[EdgeAttr]: - r"""Obtains all the subgraph type stored in remote server. - - Returns: - edge_attrs(`List[EdgeAttr]`): All the subgraph type stored in the remote server. - """ - result = [] - for attr in self.edge_attrs.values(): - if attr.size is None: - self._get_edge_index(attr) - result.append( - self.edge_attrs[(attr.edge_type, attr.layout.value, attr.is_sorted)] - ) - else: - result.append(attr) - return result - - @classmethod - def from_ipc_handle(cls, ipc_handle): - return cls(*ipc_handle) - - def share_ipc(self): - ipc_hanlde = (list(self.endpoints), self.handle, self.config) - return ipc_hanlde - - -# Pickling Registration - - -def rebuild_graphstore(ipc_handle): - gs = GsGraphStore.from_ipc_handle(ipc_handle) - return gs - - -def reduce_graphstore(GraphStore: GsGraphStore): - ipc_handle = GraphStore.share_ipc() - return (rebuild_graphstore, (ipc_handle,)) - - -ForkingPickler.register(GsGraphStore, reduce_graphstore) \ No newline at end of file + ) + return node_feature + + +def get_nodes_features(nodes): + nodes_features = [] + for node_id in nodes: + node_feature = get_node_feature(node_id) + nodes_features.append(node_feature) + node_features = torch.stack(nodes_features) + return node_features + + +def get_nodes_labels(nodes): + nodes_labels = [] + for node_id in nodes: + node_feature = get_node_label(node_id) + nodes_labels.append(node_feature) + nodes_labels = torch.cat(nodes_labels) + return nodes_labels + + +def sample_batch(batch, num_neighbors): + nodes = {element: index for index, element in enumerate(batch)} + row = [] + col = [] + current_sample_layer = batch + for num in num_neighbors: + # print(f"current_sample_layer({num}):{current_sample_layer}") + next_sample_layer = [] + for node_id in current_sample_layer: + neighbors = get_neighbors(node_id, num) + for neighbor in neighbors.tolist(): + if neighbor not in nodes: + nodes[neighbor] = len(nodes) + next_sample_layer.append(neighbor) + row.append(nodes[node_id]) + col.append(nodes[neighbor]) + # print(f"next_sample_layer:{next_sample_layer}") + current_sample_layer = next_sample_layer + edge_index = torch.tensor([col, row]) + x = get_nodes_features(nodes).to(dtype=torch.float32) + y = get_nodes_labels(nodes) + batch_size = len(batch) + return x, edge_index, y, batch_size + + +def test(model, shuffle_node_id): + evaluator = Evaluator(name="ogbn-arxiv") + model.eval() + xs = [] + y_true = [] + test_data_length = TRAIN_DATA_LENGTH // 2 + l = DATA_LENGTH - test_data_length - 1 + for i in range(DATA_LENGTH - 1, l, -BATCH_SIZE): + end = i + start = max(i - BATCH_SIZE, 0) + batch = shuffle_node_id[start:end] + x, edge_index, y, batch_size = sample_batch(batch, NUM_NEIGHBORS) + out = model(x, edge_index)[:batch_size] + xs.append(out.cpu()) + y_true.append(y[:batch_size].cpu()) + + y_pred = torch.cat(xs, dim=0) + y_pred = y_pred.argmax(dim=-1, keepdim=True) + y_true = torch.cat(y_true, dim=0).unsqueeze(-1) + test_acc = evaluator.eval( + { + "y_true": y_true, + "y_pred": y_pred, + } + )["acc"] + return test_acc + + +for epoch in range(NUM_EPOCHS): + shuffle_node_id = torch.randperm(DATA_LENGTH).tolist() + model.train() + start = time.time() + for i in range(0, TRAIN_DATA_LENGTH, BATCH_SIZE): + optimizer.zero_grad() + start = i + end = min(i + BATCH_SIZE, TRAIN_DATA_LENGTH) + batch = shuffle_node_id[start:end] + x, edge_index, y, batch_size = sample_batch(batch, NUM_NEIGHBORS) + out = model(x, edge_index)[:batch_size].log_softmax(dim=-1) + loss = F.nll_loss(out, y[:batch_size]) + loss.backward() + optimizer.step() + + end = time.time() + print(f"-- Epoch: {epoch:03d}, Loss: {loss:.4f}, Epoch Time: {end - start}") + # Test accuracy. + if epoch == 0 or epoch > (NUM_EPOCHS // 2): + test_acc = test(model, shuffle_node_id) + print(f"-- Test Accuracy: {test_acc:.4f}") + +print("-- Shutdowning ...") +glt.distributed.shutdown_client() + +print("-- Exited ...") diff --git a/python/graphscope/learning/gs_feature_store.py b/python/graphscope/learning/gs_feature_store.py index 83cb05dea200..7d8d0d6ecf42 100644 --- a/python/graphscope/learning/gs_feature_store.py +++ b/python/graphscope/learning/gs_feature_store.py @@ -32,7 +32,7 @@ class GsTensorAttr(TensorAttr): is_label: Optional[bool] = False def is_fully_specified(self) -> bool: - r"""Whether the :obj:`TensorAttr` has no unset fields.""" + r"""Whether the :obj:`TensorAttr` has no unset fields(except "attr_name").""" result = True for key in self.__dataclass_fields__: if not self.is_set(key) and key != "attr_name": @@ -110,6 +110,18 @@ def _put_tensor(self, tensor: FeatureTensorType, attr: GsTensorAttr) -> bool: raise NotImplementedError def _get_tensor(self, attr: GsTensorAttr) -> Optional[Tensor]: + r"""Obtains a :class:`torch.Tensor` from the remote server. + + Args: + attr(`GsTensorAttr`): Uniquely corresponds to a node/edge feature tensor . + + Raises: + ValueError: If the attr can not be found in the attrlists of feature store. + + Returns: + feature(`torch.Tensor`): The node/edge feature tensor. + """ + group_name, attr_name, index, is_label = self.key(attr) if not self._check_attr(attr): raise ValueError( @@ -137,6 +149,14 @@ def _get_tensor(self, attr: GsTensorAttr) -> Optional[Tensor]: return result[0] def _get_partition_id(self, attr: GsTensorAttr) -> Optional[int]: + r"""Obtains the id of the partition where the tensor is stored from remote server. + + Args: + attr(`GsTensorAttr`): Uniquely corresponds to a node/edge feature tensor . + + Returns: + partition_id(int): The corresponding partition id. + """ result = None group_name, _, gid, _ = self.key(attr) gid = self.index_to_tensor(gid) @@ -155,21 +175,51 @@ def _remove_tensor(self, attr: GsTensorAttr) -> bool: raise NotImplementedError def _check_attr(self, attr: GsTensorAttr) -> bool: + r"""Check the given :class:`GsTensorAttr` is stored in remote server or not. + + Args: + attr(`GsTensorAttr`): Uniquely corresponds to a node/edge feature tensor . + + Returns: + flag(bool): True: :class:`GsTensorAttr` is stored in remote server. \ + False: :class:`GsTensorAttr` is not stored in remote server + """ group_name, attr_name, _, _ = self.key(attr) if not attr.is_set("attr_name"): return any(group_name in key for key in self.tensor_attrs.keys()) return (group_name, attr_name) in self.tensor_attrs def _get_tensor_size(self, attr: GsTensorAttr) -> Optional[torch.Size]: + r"""Obtains the dimension of feature tensor from remote server. + + Args: + attr(`GsTensorAttr`): Uniquely corresponds to a node/edge feature tensor type . + + Returns: + tensor_size(`torch.Size`): The dimension of corresponding tensor type. + """ group_name, attr_name, _, _ = self.key(attr) attr = GsTensorAttr(group_name, attr_name, 0) tensor = self._get_tensor(attr) return tensor.shape def get_all_tensor_attrs(self) -> List[GsTensorAttr]: + r"""Obtains all the tensor type stored in remote server. + + Returns: + tensor_attrs(`List[GsTensorAttr]`): All the tensor type stored in the remote server. + """ return [attr for attr in self.tensor_attrs.values()] def index_to_tensor(self, index: IndexType) -> torch.Tensor: + r"""Convert the Index to type :class:`torch.Tensor`. + + Args: + index(`IndexType`): The index that needs to be converted. + + Returns: + index(`torch.Tensor`): index of type :class:`torch.Tensor`. + """ if isinstance(index, torch.Tensor): return index elif isinstance(index, np.ndarray): @@ -206,4 +256,4 @@ def reduce_featurestore(FeatureStore: GsFeatureStore): return (rebuild_featurestore, (ipc_handle,)) -ForkingPickler.register(GsFeatureStore, reduce_featurestore) +ForkingPickler.register(GsFeatureStore, reduce_featurestore) \ No newline at end of file diff --git a/python/graphscope/learning/gs_graph_store.py b/python/graphscope/learning/gs_graph_store.py index d637ede19afc..ff0b6d99b9d6 100644 --- a/python/graphscope/learning/gs_graph_store.py +++ b/python/graphscope/learning/gs_graph_store.py @@ -115,12 +115,15 @@ def _put_edge_index( raise NotImplementedError def _get_edge_index(self, edge_attr: EdgeAttr) -> Optional[EdgeTensorType]: - r"""Get edge index from remote server with edge_attr. + r"""Obtains a :class:`EdgeTensorType` from the remote server with :class:`EdgeAttr`. - Returns: + Args: + edge_attr(`EdgeAttr`): Uniquely corresponds to a topology of subgraph . - (row indice tensor, column indice tensor)(COO) | - (row ptr tensor, column indice tensor)(CSR) | + Returns: + edge_index(`EdgeTensorType`): The edge index tensor, which is a :class:`tuple` of\ + (row indice tensor, column indice tensor)(COO)\ + (row ptr tensor, column indice tensor)(CSR)\ (column ptr tensor, row indice tensor)(CSC). """ group_name, layout, is_sorted, _ = self.key(edge_attr) @@ -138,6 +141,11 @@ def _remove_edge_index(self, edge_attr: EdgeAttr) -> bool: raise NotImplementedError def get_all_edge_attrs(self) -> List[EdgeAttr]: + r"""Obtains all the subgraph type stored in remote server. + + Returns: + edge_attrs(`List[EdgeAttr]`): All the subgraph type stored in the remote server. + """ result = [] for attr in self.edge_attrs.values(): if attr.size is None: @@ -171,4 +179,4 @@ def reduce_graphstore(GraphStore: GsGraphStore): return (rebuild_graphstore, (ipc_handle,)) -ForkingPickler.register(GsGraphStore, reduce_graphstore) +ForkingPickler.register(GsGraphStore, reduce_graphstore) \ No newline at end of file