commit f6db1a27e4b42923a61b5460dfc37578d45c151f Author: haowei.yao Date: Wed Dec 27 10:03:32 2017 +0800 Init commit Signed-off-by: haowei.yao diff --git a/3rdparty/CMakeLists.txt b/3rdparty/CMakeLists.txt new file mode 100644 index 000000000..10cc7018e --- /dev/null +++ b/3rdparty/CMakeLists.txt @@ -0,0 +1,50 @@ +# +# Copyright 2009-2017 Alibaba Cloud All rights reserved. +# +# 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. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +if(CMAKE_HOST_WIN32) + ExternalProject_Add(jsoncpp + URL https://github.com/open-source-parsers/jsoncpp/archive/0.10.5.tar.gz + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX= + -DJSONCPP_WITH_TESTS=OFF + -DJSONCPP_WITH_POST_BUILD_UNITTEST=OFF + -DJSONCPP_WITH_WARNING_AS_ERROR=OFF + -DJSONCPP_WITH_PKGCONFIG_SUPPORT=OFF + -DJSONCPP_WITH_CMAKE_PACKAGE=OFF + -DBUILD_SHARED_LIBS=OFF + -DBUILD_STATIC_LIBS=ON + ) + +ExternalProject_Add(curl + URL https://github.com/curl/curl/releases/download/curl-7_55_1/curl-7.55.1.tar.gz + CMAKE_ARGS -DBUILD_CURL_EXE=OFF + -DBUILD_TESTING=OFF + -DCMAKE_INSTALL_PREFIX= + -DCMAKE_USE_WINSSL=ON + -DCURL_STATICLIB=ON + -DENABLE_MANUAL=OFF + -DHTTP_ONLY=ON + ) +endif() + +ExternalProject_Add(catch + URL https://github.com/catchorg/Catch2/releases/download/v2.0.1/catch.hpp + TIMEOUT 10 + DOWNLOAD_NO_EXTRACT 1 + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND + ${CMAKE_COMMAND} -E copy_if_different + ) diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 000000000..625e09f01 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,52 @@ +# +# Copyright 2009-2017 Alibaba Cloud All rights reserved. +# +# 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. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +cmake_minimum_required(VERSION 3.1) + +cmake_policy(SET CMP0048 NEW) + +file(STRINGS "VERSION" version) + +project(alibabacloud-sdk VERSION ${version}) + +message(STATUS "Project version: ${PROJECT_VERSION}") + +set(TARGET_OUTPUT_NAME_PREFIX "alibabacloud-sdk-" CACHE STRING "The target's output name prefix") +option(BUILD_SHARED_LIBS "Enable shared library" ON) +option(BUILD_TESTS "Enable tests" ON) + +set(LIB_TYPE STATIC) +if(BUILD_SHARED_LIBS) + set(LIB_TYPE SHARED) + add_definitions(-DALIBABACLOUD_SHARED) +endif() + +set_property(GLOBAL + PROPERTY + USE_FOLDERS ON) + +set(CMAKE_CXX_STANDARD 11) + +include(ExternalProject) +include(GNUInstallDirs) + +add_subdirectory(3rdparty) +add_subdirectory(core) + +if(BUILD_TESTS) + enable_testing() + add_subdirectory(core-tests) +endif() diff --git a/Doxyfile b/Doxyfile new file mode 100644 index 000000000..24546fa44 --- /dev/null +++ b/Doxyfile @@ -0,0 +1,336 @@ +# Doxyfile 1.8.13 + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- +DOXYFILE_ENCODING = UTF-8 +PROJECT_NAME = "alibabacloud-cpp-sdk" +PROJECT_NUMBER = 0.0.1 +PROJECT_BRIEF = "Alibaba Cloud SDK for C++" +PROJECT_LOGO = +OUTPUT_DIRECTORY = +CREATE_SUBDIRS = NO +ALLOW_UNICODE_NAMES = NO +OUTPUT_LANGUAGE = English +BRIEF_MEMBER_DESC = YES +REPEAT_BRIEF = YES +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the +ALWAYS_DETAILED_SEC = NO +INLINE_INHERITED_MEMB = NO +FULL_PATH_NAMES = YES +STRIP_FROM_PATH = +STRIP_FROM_INC_PATH = +SHORT_NAMES = NO +JAVADOC_AUTOBRIEF = NO +QT_AUTOBRIEF = NO +MULTILINE_CPP_IS_BRIEF = NO +INHERIT_DOCS = YES +SEPARATE_MEMBER_PAGES = NO +TAB_SIZE = 4 +ALIASES = +TCL_SUBST = +OPTIMIZE_OUTPUT_FOR_C = NO +OPTIMIZE_OUTPUT_JAVA = NO +OPTIMIZE_FOR_FORTRAN = NO +OPTIMIZE_OUTPUT_VHDL = NO +EXTENSION_MAPPING = +MARKDOWN_SUPPORT = YES +TOC_INCLUDE_HEADINGS = 0 +AUTOLINK_SUPPORT = YES +BUILTIN_STL_SUPPORT = YES +CPP_CLI_SUPPORT = NO +SIP_SUPPORT = NO +IDL_PROPERTY_SUPPORT = YES +DISTRIBUTE_GROUP_DOC = NO +GROUP_NESTED_COMPOUNDS = NO +SUBGROUPING = YES +INLINE_GROUPED_CLASSES = NO +INLINE_SIMPLE_STRUCTS = NO +TYPEDEF_HIDES_STRUCT = NO +LOOKUP_CACHE_SIZE = 0 +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- +EXTRACT_ALL = NO +EXTRACT_PRIVATE = NO +EXTRACT_PACKAGE = NO +EXTRACT_STATIC = NO +EXTRACT_LOCAL_CLASSES = YES +EXTRACT_LOCAL_METHODS = NO +EXTRACT_ANON_NSPACES = NO +HIDE_UNDOC_MEMBERS = NO +HIDE_UNDOC_CLASSES = NO +HIDE_FRIEND_COMPOUNDS = NO +HIDE_IN_BODY_DOCS = NO +INTERNAL_DOCS = NO +CASE_SENSE_NAMES = NO +HIDE_SCOPE_NAMES = NO +HIDE_COMPOUND_REFERENCE= NO +SHOW_INCLUDE_FILES = YES +SHOW_GROUPED_MEMB_INC = NO +FORCE_LOCAL_INCLUDES = NO +INLINE_INFO = YES +SORT_MEMBER_DOCS = YES +SORT_BRIEF_DOCS = NO +SORT_MEMBERS_CTORS_1ST = NO +SORT_GROUP_NAMES = NO +SORT_BY_SCOPE_NAME = NO +STRICT_PROTO_MATCHING = NO +GENERATE_TODOLIST = YES +GENERATE_TESTLIST = YES +GENERATE_BUGLIST = YES +GENERATE_DEPRECATEDLIST= YES +ENABLED_SECTIONS = +MAX_INITIALIZER_LINES = 30 +SHOW_USED_FILES = NO +SHOW_FILES = NO +SHOW_NAMESPACES = YES +FILE_VERSION_FILTER = +LAYOUT_FILE = +CITE_BIB_FILES = +#--------------------------------------------------------------------------- +# Configuration options related to warning and progress messages +#--------------------------------------------------------------------------- +QUIET = NO +WARNINGS = YES +WARN_IF_UNDOCUMENTED = YES +WARN_IF_DOC_ERROR = YES +WARN_NO_PARAMDOC = NO +WARN_AS_ERROR = NO +WARN_FORMAT = "$file:$line: $text" +WARN_LOGFILE = +#--------------------------------------------------------------------------- +# Configuration options related to the input files +#--------------------------------------------------------------------------- +INPUT = ./ +INPUT_ENCODING = UTF-8 +FILE_PATTERNS = *.cc \ + *.h \ + *.md +RECURSIVE = YES +EXCLUDE = +EXCLUDE_SYMLINKS = NO +EXCLUDE_PATTERNS = */3rdparty/* \ + */CMakeFiles/* \ + */src/*.h \ + */tests/* +EXCLUDE_SYMBOLS = +EXAMPLE_PATH = +EXAMPLE_PATTERNS = * +EXAMPLE_RECURSIVE = NO +IMAGE_PATH = +INPUT_FILTER = +FILTER_PATTERNS = +FILTER_SOURCE_FILES = NO +FILTER_SOURCE_PATTERNS = +USE_MDFILE_AS_MAINPAGE = ./README.md +#--------------------------------------------------------------------------- +# Configuration options related to source browsing +#--------------------------------------------------------------------------- +SOURCE_BROWSER = YES +INLINE_SOURCES = NO +STRIP_CODE_COMMENTS = YES +REFERENCED_BY_RELATION = NO +REFERENCES_RELATION = NO +REFERENCES_LINK_SOURCE = YES +SOURCE_TOOLTIPS = YES +USE_HTAGS = NO +VERBATIM_HEADERS = YES +CLANG_ASSISTED_PARSING = NO +CLANG_OPTIONS = +#--------------------------------------------------------------------------- +# Configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- +ALPHABETICAL_INDEX = YES +COLS_IN_ALPHA_INDEX = 5 +IGNORE_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the HTML output +#--------------------------------------------------------------------------- +GENERATE_HTML = YES +HTML_OUTPUT = html +HTML_FILE_EXTENSION = .html +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = +HTML_EXTRA_STYLESHEET = +HTML_EXTRA_FILES = +HTML_COLORSTYLE_HUE = 220 +HTML_COLORSTYLE_SAT = 100 +HTML_COLORSTYLE_GAMMA = 80 +HTML_TIMESTAMP = NO +HTML_DYNAMIC_SECTIONS = NO +HTML_INDEX_NUM_ENTRIES = 100 +GENERATE_DOCSET = NO +DOCSET_FEEDNAME = "Doxygen generated docs" +DOCSET_BUNDLE_ID = org.doxygen.Project +DOCSET_PUBLISHER_ID = org.doxygen.Publisher +DOCSET_PUBLISHER_NAME = Publisher +GENERATE_HTMLHELP = NO +CHM_FILE = +HHC_LOCATION = +GENERATE_CHI = NO +CHM_INDEX_ENCODING = +BINARY_TOC = NO +TOC_EXPAND = NO +GENERATE_QHP = NO +QCH_FILE = +QHP_NAMESPACE = org.doxygen.Project +QHP_VIRTUAL_FOLDER = doc +QHP_CUST_FILTER_NAME = +QHP_CUST_FILTER_ATTRS = +QHP_SECT_FILTER_ATTRS = +QHG_LOCATION = +GENERATE_ECLIPSEHELP = NO +ECLIPSE_DOC_ID = org.doxygen.Project +DISABLE_INDEX = NO +GENERATE_TREEVIEW = NO +ENUM_VALUES_PER_LINE = 4 +TREEVIEW_WIDTH = 250 +EXT_LINKS_IN_WINDOW = NO +FORMULA_FONTSIZE = 10 +FORMULA_TRANSPARENT = YES +USE_MATHJAX = NO +MATHJAX_FORMAT = HTML-CSS +MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest +MATHJAX_EXTENSIONS = +MATHJAX_CODEFILE = +SEARCHENGINE = YES +SERVER_BASED_SEARCH = NO +EXTERNAL_SEARCH = NO +SEARCHENGINE_URL = +SEARCHDATA_FILE = searchdata.xml +EXTERNAL_SEARCH_ID = +EXTRA_SEARCH_MAPPINGS = +#--------------------------------------------------------------------------- +# Configuration options related to the LaTeX output +#--------------------------------------------------------------------------- +GENERATE_LATEX = YES +LATEX_OUTPUT = latex +LATEX_CMD_NAME = latex +MAKEINDEX_CMD_NAME = makeindex +COMPACT_LATEX = NO +PAPER_TYPE = a4 +EXTRA_PACKAGES = +LATEX_HEADER = +LATEX_FOOTER = +LATEX_EXTRA_STYLESHEET = +LATEX_EXTRA_FILES = +PDF_HYPERLINKS = YES +USE_PDFLATEX = YES +LATEX_BATCHMODE = NO +LATEX_HIDE_INDICES = NO +LATEX_SOURCE_CODE = NO +LATEX_BIB_STYLE = plain +LATEX_TIMESTAMP = NO +#--------------------------------------------------------------------------- +# Configuration options related to the RTF output +#--------------------------------------------------------------------------- +GENERATE_RTF = NO +RTF_OUTPUT = rtf +COMPACT_RTF = NO +RTF_HYPERLINKS = NO +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = +RTF_SOURCE_CODE = NO +#--------------------------------------------------------------------------- +# Configuration options related to the man page output +#--------------------------------------------------------------------------- +GENERATE_MAN = NO +MAN_OUTPUT = man +MAN_EXTENSION = .3 +MAN_SUBDIR = +MAN_LINKS = NO +#--------------------------------------------------------------------------- +# Configuration options related to the XML output +#--------------------------------------------------------------------------- +GENERATE_XML = NO +XML_OUTPUT = xml +XML_PROGRAMLISTING = YES +#--------------------------------------------------------------------------- +# Configuration options related to the DOCBOOK output +#--------------------------------------------------------------------------- +GENERATE_DOCBOOK = NO +DOCBOOK_OUTPUT = docbook +DOCBOOK_PROGRAMLISTING = NO +#--------------------------------------------------------------------------- +# Configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- +GENERATE_AUTOGEN_DEF = NO +#--------------------------------------------------------------------------- +# Configuration options related to the Perl module output +#--------------------------------------------------------------------------- +GENERATE_PERLMOD = NO +PERLMOD_LATEX = NO +PERLMOD_PRETTY = YES +PERLMOD_MAKEVAR_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- +ENABLE_PREPROCESSING = YES +MACRO_EXPANSION = NO +EXPAND_ONLY_PREDEF = NO +SEARCH_INCLUDES = YES +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = +EXPAND_AS_DEFINED = +SKIP_FUNCTION_MACROS = YES +#--------------------------------------------------------------------------- +# Configuration options related to external references +#--------------------------------------------------------------------------- +TAGFILES = +GENERATE_TAGFILE = +ALLEXTERNALS = NO +EXTERNAL_GROUPS = YES +EXTERNAL_PAGES = YES +PERL_PATH = /usr/bin/perl +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- +CLASS_DIAGRAMS = YES +MSCGEN_PATH = +DIA_PATH = +HIDE_UNDOC_RELATIONS = YES +HAVE_DOT = NO +DOT_NUM_THREADS = 0 +DOT_FONTNAME = Helvetica +DOT_FONTSIZE = 10 +DOT_FONTPATH = +CLASS_GRAPH = YES +COLLABORATION_GRAPH = YES +GROUP_GRAPHS = YES +UML_LOOK = NO +UML_LIMIT_NUM_FIELDS = 10 +TEMPLATE_RELATIONS = NO +INCLUDE_GRAPH = YES +INCLUDED_BY_GRAPH = YES +CALL_GRAPH = NO +CALLER_GRAPH = NO +GRAPHICAL_HIERARCHY = YES +DIRECTORY_GRAPH = YES +DOT_IMAGE_FORMAT = png +INTERACTIVE_SVG = NO +DOT_PATH = +DOTFILE_DIRS = +MSCFILE_DIRS = +DIAFILE_DIRS = +PLANTUML_JAR_PATH = +PLANTUML_CFG_FILE = +PLANTUML_INCLUDE_PATH = +DOT_GRAPH_MAX_NODES = 50 +MAX_DOT_GRAPH_DEPTH = 0 +DOT_TRANSPARENT = NO +DOT_MULTI_TARGETS = NO +GENERATE_LEGEND = YES +DOT_CLEANUP = YES diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..75a5e98b9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2009-2017 Alibaba Cloud All rights reserved. + +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. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 000000000..475f812cd --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Alibaba Cloud SDK for C++ \ No newline at end of file diff --git a/VERSION b/VERSION new file mode 100644 index 000000000..8acdd82b7 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.0.1 diff --git a/core-tests/CMakeLists.txt b/core-tests/CMakeLists.txt new file mode 100644 index 000000000..fb89aeffd --- /dev/null +++ b/core-tests/CMakeLists.txt @@ -0,0 +1,25 @@ +add_executable(core-tests + main.cc + httpmessage.cc + signer.cc + url.cc ) + +set_target_properties(core-tests + PROPERTIES + OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}core-tests + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +target_link_libraries(core-tests + core + ) + +ExternalProject_Get_Property(catch INSTALL_DIR) +set(catch_install_dir ${INSTALL_DIR}) +add_dependencies(core-tests catch) + +target_include_directories(core-tests + PRIVATE ${catch_install_dir} + ${CMAKE_SOURCE_DIR}/core/include + ) + +add_test(NAME core-tests COMMAND core-tests) diff --git a/core-tests/httpmessage.cc b/core-tests/httpmessage.cc new file mode 100644 index 000000000..e81867584 --- /dev/null +++ b/core-tests/httpmessage.cc @@ -0,0 +1,33 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include + +using namespace AlibabaCloud; + +TEST_CASE("testHttpMessage") +{ + const std::string data = "Thu, 19 Oct 2017 04:38:27 GMT"; + HttpRequest r; + r.setHeader("accept", "application/xml"); + r.setHeader("Accept", "application/json"); + r.setHeader("Date", data); + + REQUIRE(r.headers().size() == 2); + REQUIRE(r.header("accept") == "application/json"); + REQUIRE(r.header(HttpMessage::KnownHeader::Date) == data); +} \ No newline at end of file diff --git a/core-tests/main.cc b/core-tests/main.cc new file mode 100644 index 000000000..bf7bb139c --- /dev/null +++ b/core-tests/main.cc @@ -0,0 +1,18 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#define CATCH_CONFIG_MAIN +#include diff --git a/core-tests/signer.cc b/core-tests/signer.cc new file mode 100644 index 000000000..500cf312d --- /dev/null +++ b/core-tests/signer.cc @@ -0,0 +1,31 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include + +using namespace AlibabaCloud; + +TEST_CASE("testHmacSha1Signer") +{ + HmacSha1Signer sig; + std::string sign = sig.generate("GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeRegions" + "%26Format%3DXML%26RegionId%3Dregion1%26SignatureMethod%3DHMAC-SHA1" + "%26SignatureNonce%3DNwDAxvLU6tFE0DVb%26SignatureVersion%3D1.0" + "%26TimeStamp%3D2012-12-26T10%253A33%253A56Z%26Version%3D2013-01-10", + "testsecret&"); + REQUIRE("axE3FUHgDyfm9/+Iep0HpZXrRwE=" == sign); +} \ No newline at end of file diff --git a/core-tests/url.cc b/core-tests/url.cc new file mode 100644 index 000000000..a35b190bf --- /dev/null +++ b/core-tests/url.cc @@ -0,0 +1,63 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include + +using namespace AlibabaCloud; + +TEST_CASE("testUrl") +{ + const std::string src = "abc://username:password@example.com:123/path/data?key=value&key2=value2#fragid1"; + Url url; + url.setScheme("abc"); + url.setUserName("username"); + url.setPassword("password"); + url.setHost("example.com"); + url.setPath("/path/data"); + url.setPort(123); + url.setQuery("key=value&key2=value2"); + url.setFragment("fragid1"); + REQUIRE(url.toString() == src); + + url.clear(); + url.fromString(src); + REQUIRE(url.scheme() == "abc"); + REQUIRE(url.userName() == "username"); + REQUIRE(url.password() == "password"); + REQUIRE(url.host() == "example.com"); + REQUIRE(url.path() == "/path/data"); + REQUIRE(url.port() == 123); + REQUIRE(url.query() == "key=value&key2=value2"); + REQUIRE(url.fragment() == "fragid1"); + + Url newurl = url; + REQUIRE(newurl == url); +} + +TEST_CASE("testUrl2") +{ + const std::string src = "http://oss.example.com"; + Url url(src); + REQUIRE(url.scheme() == "http"); + REQUIRE(url.userName() == ""); + REQUIRE(url.password() == ""); + REQUIRE(url.host() == "oss.example.com"); + REQUIRE(url.path() == "/"); + REQUIRE(url.port() == -1); + REQUIRE(url.query() == ""); + REQUIRE(url.fragment() == ""); +} \ No newline at end of file diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt new file mode 100755 index 000000000..c9d992849 --- /dev/null +++ b/core/CMakeLists.txt @@ -0,0 +1,181 @@ +# +# Copyright 2009-2017 Alibaba Cloud All rights reserved. +# +# 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. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +configure_file(src/Config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/include/alibabacloud/core/Config.h @ONLY) + +set(core_public_header + include/alibabacloud/core/AlibabaCloud.h + include/alibabacloud/core/AsyncCallerContext.h + include/alibabacloud/core/ClientConfiguration.h + include/alibabacloud/core/CommonClient.h + include/alibabacloud/core/CommonRequest.h + include/alibabacloud/core/CommonResponse.h + include/alibabacloud/core/Config.h + include/alibabacloud/core/CoreClient.h + include/alibabacloud/core/CoreExport.h + include/alibabacloud/core/Credentials.h + include/alibabacloud/core/CredentialsProvider.h + include/alibabacloud/core/EcsInstanceProfileConfigLoader.h + include/alibabacloud/core/EndpointProvider.h + include/alibabacloud/core/Error.h + include/alibabacloud/core/Global.h + include/alibabacloud/core/HmacSha1Signer.h + include/alibabacloud/core/HttpClient.h + include/alibabacloud/core/HttpMessage.h + include/alibabacloud/core/HttpResponse.h + include/alibabacloud/core/HttpRequest.h + include/alibabacloud/core/InstanceProfileConfigLoader.h + include/alibabacloud/core/InstanceProfileCredentialsProvider.h + include/alibabacloud/core/NetworkProxy.h + include/alibabacloud/core/Outcome.h + include/alibabacloud/core/Profile.h + include/alibabacloud/core/Runnable.h + include/alibabacloud/core/RoaServiceClient.h + include/alibabacloud/core/RoaServiceRequest.h + include/alibabacloud/core/RpcServiceClient.h + include/alibabacloud/core/RpcServiceRequest.h + include/alibabacloud/core/ServiceRequest.h + include/alibabacloud/core/ServiceResult.h + include/alibabacloud/core/Signer.h + include/alibabacloud/core/SimpleCredentialsProvider.h + include/alibabacloud/core/StsAssumeRoleCredentialsProvider.h + include/alibabacloud/core/Url.h + include/alibabacloud/core/location/LocationClient.h + include/alibabacloud/core/location/LocationRequest.h + include/alibabacloud/core/location/model/DescribeEndpointsRequest.h + include/alibabacloud/core/location/model/DescribeEndpointsResult.h + include/alibabacloud/core/sts/StsClient.h + include/alibabacloud/core/sts/StsRequest.h + include/alibabacloud/core/sts/model/AssumeRoleRequest.h + include/alibabacloud/core/sts/model/AssumeRoleResult.h + include/alibabacloud/core/sts/model/GetCallerIdentityRequest.h + include/alibabacloud/core/sts/model/GetCallerIdentityResult.h + ) + +set(core_src + src/AlibabaCloud.cc + src/AsyncCallerContext.cc + src/ClientConfiguration.cc + src/CommonClient.cc + src/CommonRequest.cc + src/CommonResponse.cc + src/CoreClient.cc + src/Credentials.cc + src/CredentialsProvider.cc + src/CurlHttpClient.h + src/CurlHttpClient.cc + src/EcsInstanceProfileConfigLoader.cc + src/EcsMetadataClient.h + src/EcsMetadataClient.cc + src/EndpointProvider.cc + src/Error.cc + src/Executor.cc + src/Executor.h + src/HmacSha1Signer.cc + src/HttpClient.cc + src/HttpMessage.cc + src/HttpResponse.cc + src/HttpRequest.cc + src/InstanceProfileConfigLoader.cc + src/InstanceProfileCredentialsProvider.cc + src/NetworkProxy.cc + src/Outcome.cc + src/Profile.cc + src/Runnable.cc + src/RoaServiceClient.cc + src/RoaServiceRequest.cc + src/RpcServiceClient.cc + src/RpcServiceRequest.cc + src/ServiceRequest.cc + src/ServiceResult.cc + src/Signer.cc + src/SimpleCredentialsProvider.cc + src/StsAssumeRoleCredentialsProvider.cc + src/Url.cc + src/Utils.h + src/Utils.cc + src/location/LocationClient.cc + src/location/LocationRequest.cc + src/location/model/DescribeEndpointsRequest.cc + src/location/model/DescribeEndpointsResult.cc + src/sts/StsClient.cc + src/sts/StsRequest.cc + src/sts/model/AssumeRoleRequest.cc + src/sts/model/AssumeRoleResult.cc + src/sts/model/GetCallerIdentityRequest.cc + src/sts/model/GetCallerIdentityResult.cc + ) + +add_library(core ${LIB_TYPE} ${core_public_header} ${core_src}) + +set_target_properties(core + PROPERTIES + LINKER_LANGUAGE CXX + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin + OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}core + PUBLIC_HEADER "${core_public_header}" + ) + +if(${LIB_TYPE} STREQUAL "SHARED") + set_target_properties(core + PROPERTIES + DEFINE_SYMBOL ALIBABACLOUD_CORE_LIBRARY) +endif() + +target_include_directories(core + PRIVATE include ) + +if(CMAKE_HOST_WIN32) + ExternalProject_Get_Property(curl INSTALL_DIR) + set(curl_install_dir ${INSTALL_DIR}) + ExternalProject_Get_Property(jsoncpp INSTALL_DIR) + set(jsoncpp_install_dir ${INSTALL_DIR}) + add_dependencies(core + curl + jsoncpp ) + target_include_directories(core + PRIVATE ${curl_install_dir}/include + ${jsoncpp_install_dir}/include + ${tinyxml2_install_dir}/include) + target_link_libraries(core + Crypt32 + Rpcrt4 + Ws2_32 + ${jsoncpp_install_dir}/lib/jsoncpp.lib + debug ${curl_install_dir}/lib/libcurl-d.lib + optimized ${curl_install_dir}/lib/libcurl.lib + ) + target_compile_definitions(core + PRIVATE CURL_STATICLIB + _CRT_SECURE_NO_WARNINGS) +else() + target_include_directories(core + PRIVATE /usr/include/jsoncpp) + target_link_libraries(core + crypto + curl + jsoncpp + uuid ) +endif() + +install(TARGETS core + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/alibabacloud/core + ) diff --git a/core/include/alibabacloud/core/.gitignore b/core/include/alibabacloud/core/.gitignore new file mode 100644 index 000000000..12bfd6165 --- /dev/null +++ b/core/include/alibabacloud/core/.gitignore @@ -0,0 +1 @@ +Config.h diff --git a/core/include/alibabacloud/core/AlibabaCloud.h b/core/include/alibabacloud/core/AlibabaCloud.h new file mode 100644 index 000000000..17e8625ec --- /dev/null +++ b/core/include/alibabacloud/core/AlibabaCloud.h @@ -0,0 +1,29 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_ALIBABACLOUD_H_ +#define ALIBABACLOUD_CORE_ALIBABACLOUD_H_ + +#include "CoreExport.h" + +namespace AlibabaCloud +{ + ALIBABACLOUD_CORE_EXPORT void InitializeSdk(); + ALIBABACLOUD_CORE_EXPORT bool IsSdkInitialized(); + ALIBABACLOUD_CORE_EXPORT void ShutdownSdk(); +} + +#endif \ No newline at end of file diff --git a/core/include/alibabacloud/core/AsyncCallerContext.h b/core/include/alibabacloud/core/AsyncCallerContext.h new file mode 100644 index 000000000..19243e39d --- /dev/null +++ b/core/include/alibabacloud/core/AsyncCallerContext.h @@ -0,0 +1,38 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_ASYNCCALLERCONTEXT_H_ +#define ALIBABACLOUD_CORE_ASYNCCALLERCONTEXT_H_ + +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT AsyncCallerContext + { + public: + AsyncCallerContext(); + explicit AsyncCallerContext(const std::string &uuid); + virtual ~AsyncCallerContext(); + + std::string uuid()const; + void setUuid(const std::string &uuid); + private: + std::string uuid_; + }; +} +#endif diff --git a/core/include/alibabacloud/core/ClientConfiguration.h b/core/include/alibabacloud/core/ClientConfiguration.h new file mode 100644 index 000000000..6ff3cfa7f --- /dev/null +++ b/core/include/alibabacloud/core/ClientConfiguration.h @@ -0,0 +1,49 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_CLIENTCONFIGURATION_H_ +#define ALIBABACLOUD_CORE_CLIENTCONFIGURATION_H_ + +#include +#include +#include "CredentialsProvider.h" +#include "CoreExport.h" +#include "NetworkProxy.h" +#include "Signer.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT ClientConfiguration + { + public: + explicit ClientConfiguration(const std::string ®ionId = "cn-hangzhou", + const NetworkProxy &proxy = NetworkProxy()); + ~ClientConfiguration(); + + std::string endpoint()const; + NetworkProxy proxy()const; + std::string regionId()const; + void setEndpoint(const std::string &endpoint); + void setProxy(const NetworkProxy &proxy); + void setRegionId(const std::string ®ionId); + private: + std::string endpoint_; + NetworkProxy proxy_; + std::string regionId_; + }; +} + +#endif // !ALIBABACLOUD_CORE_CLIENTCONFIGURATION_H_ \ No newline at end of file diff --git a/core/include/alibabacloud/core/CommonClient.h b/core/include/alibabacloud/core/CommonClient.h new file mode 100644 index 000000000..d1eb79560 --- /dev/null +++ b/core/include/alibabacloud/core/CommonClient.h @@ -0,0 +1,65 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_COMMONCLIENT_H_ +#define ALIBABACLOUD_CORE_COMMONCLIENT_H_ + +#include +#include "ClientConfiguration.h" +#include "CoreExport.h" +#include "CoreClient.h" +#include "CredentialsProvider.h" +#include "EndpointProvider.h" +#include "CommonRequest.h" +#include "CommonResponse.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT CommonClient : public CoreClient + { + public: + typedef Outcome CommonResponseOutcome; + typedef std::future CommonResponseOutcomeCallable; + typedef std::function&)> CommonResponseAsyncHandler; + typedef Outcome JsonOutcome; + + CommonClient(const Credentials &credentials, const ClientConfiguration &configuration); + CommonClient(const std::shared_ptr &credentialsProvider, const ClientConfiguration &configuration); + CommonClient(const std::string &accessKeyId, const std::string &accessKeySecret, const ClientConfiguration &configuration); + ~CommonClient(); + + CommonResponseOutcome commonResponse(const CommonRequest &request)const; + void commonResponseAsync(const CommonRequest& request, const CommonResponseAsyncHandler& handler, const std::shared_ptr& context = nullptr) const; + CommonResponseOutcomeCallable commonResponseCallable(const CommonRequest& request) const; + + protected: + virtual HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest & msg, HttpRequest::Method method) const override; + HttpRequest buildHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const; + HttpRequest buildRoaHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const; + HttpRequest buildRpcHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const; + virtual EndpointOutcome endpoint() const override; + JsonOutcome makeRequest(const std::string &endpoint, const CommonRequest &msg, HttpRequest::Method method = HttpRequest::Method::Get)const; + + private: + std::string canonicalizedQuery(const std::map ¶ms)const; + std::string canonicalizedHeaders(const HttpMessage::HeaderCollection &headers)const; + + std::shared_ptr credentialsProvider_; + std::shared_ptr endpointProvider_; + std::shared_ptr signer_; + }; +} +#endif // !ALIBABACLOUD_CORE_COMMONCLIENT_H_ diff --git a/core/include/alibabacloud/core/CommonRequest.h b/core/include/alibabacloud/core/CommonRequest.h new file mode 100644 index 000000000..aeba8851e --- /dev/null +++ b/core/include/alibabacloud/core/CommonRequest.h @@ -0,0 +1,59 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_COMMONREQUEST_H_ +#define ALIBABACLOUD_CORE_COMMONREQUEST_H_ + +#include +#include "CoreExport.h" +#include "ServiceRequest.h" +#include "HttpRequest.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT CommonRequest : public ServiceRequest + { + public: + CommonRequest(); + ~CommonRequest(); + + std::string domain()const; + ParameterValueType headerParameter(const ParameterNameType &name)const; + ParameterCollection headerParameters()const; + HttpRequest::Method httpMethod()const; + ParameterValueType queryParameter(const ParameterNameType &name)const; + ParameterCollection queryParameters()const; + using ServiceRequest::setContent; + void setDomain(const std::string &domain); + void setHeaderParameter(const ParameterNameType &name, const ParameterValueType &value); + void setHttpMethod(HttpRequest::Method method); + void setQueryParameter(const ParameterNameType &name, const ParameterValueType &value); + using ServiceRequest::setResourcePath; + void setUriPattern(const std::string &uriPattern); + using ServiceRequest::setVersion; + std::string uriPattern()const; + protected: + using ServiceRequest::product; + + private: + std::string domain_; + std::string uriPattern_; + ParameterCollection queryParams_; + ParameterCollection headerParams_; + HttpRequest::Method httpMethod_; + }; +} +#endif // !ALIBABACLOUD_CORE_COMMONREQUEST_H_ \ No newline at end of file diff --git a/core/include/alibabacloud/core/CommonResponse.h b/core/include/alibabacloud/core/CommonResponse.h new file mode 100644 index 000000000..6ec8a2194 --- /dev/null +++ b/core/include/alibabacloud/core/CommonResponse.h @@ -0,0 +1,38 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_COMMONRESPONSE_H_ +#define ALIBABACLOUD_CORE_COMMONRESPONSE_H_ + +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT CommonResponse + { + public: + CommonResponse(); + explicit CommonResponse(const std::string &payload); + ~CommonResponse(); + + std::string payload()const; + private: + std::string payload_; + + }; +} +#endif // !ALIBABACLOUD_CORE_COMMONRESPONSE_H_ diff --git a/core/include/alibabacloud/core/CoreClient.h b/core/include/alibabacloud/core/CoreClient.h new file mode 100644 index 000000000..c259bfb8c --- /dev/null +++ b/core/include/alibabacloud/core/CoreClient.h @@ -0,0 +1,55 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_CORECLIENT_H_ +#define ALIBABACLOUD_CORE_CORECLIENT_H_ + +#include +#include +#include "ClientConfiguration.h" +#include "CoreExport.h" +#include "HttpClient.h" +#include "HttpResponse.h" +#include "HttpRequest.h" +#include "Outcome.h" +#include "ServiceRequest.h" +#include "Runnable.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT CoreClient + { + public: + CoreClient(const ClientConfiguration &configuration); + virtual ~CoreClient(); + + ClientConfiguration configuration()const; + protected: + typedef Outcome EndpointOutcome; + + HttpClient::HttpResponseOutcome AttemptRequest(const std::string & endpoint, const ServiceRequest &request, HttpRequest::Method method)const; + Error buildCoreError(const HttpResponse &response)const; + bool hasResponseError(const HttpResponse &response)const; + virtual HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const = 0; + void asyncExecute(Runnable * r)const; + virtual EndpointOutcome endpoint()const = 0; + private: + ClientConfiguration configuration_; + std::shared_ptr credentialsProvider_; + HttpClient *httpClient_; + }; +} +#endif diff --git a/core/include/alibabacloud/core/CoreExport.h b/core/include/alibabacloud/core/CoreExport.h new file mode 100644 index 000000000..0c8b5a6b7 --- /dev/null +++ b/core/include/alibabacloud/core/CoreExport.h @@ -0,0 +1,32 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_COREEXPORT_H_ +#define ALIBABACLOUD_CORE_COREEXPORT_H_ + +#include "Global.h" + +#if defined(ALIBABACLOUD_SHARED) +# if defined(ALIBABACLOUD_CORE_LIBRARY) +# define ALIBABACLOUD_CORE_EXPORT ALIBABACLOUD_DECL_EXPORT +# else +# define ALIBABACLOUD_CORE_EXPORT ALIBABACLOUD_DECL_IMPORT +# endif +#else +# define ALIBABACLOUD_CORE_EXPORT +#endif + +#endif // !ALIBABACLOUD_CORE_COREEXPORT_H_ diff --git a/core/include/alibabacloud/core/Credentials.h b/core/include/alibabacloud/core/Credentials.h new file mode 100644 index 000000000..ac4dd81cb --- /dev/null +++ b/core/include/alibabacloud/core/Credentials.h @@ -0,0 +1,47 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_CREDENTIAL_H_ +#define ALIBABACLOUD_CORE_CREDENTIAL_H_ + +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT Credentials + { + public: + Credentials(const std::string &accessKeyId, + const std::string &accessKeySecret, + const std::string &sessionToken=""); + ~Credentials(); + + std::string accessKeyId () const; + std::string accessKeySecret () const; + void setAccessKeyId(const std::string &accessKeyId); + void setAccessKeySecret(const std::string &accessKeySecret); + void setSessionToken (const std::string &sessionToken); + std::string sessionToken () const; + + private: + std::string accessKeyId_; + std::string accessKeySecret_; + std::string sessionToken_; + }; +} + +#endif // !ALIBABACLOUD_CORE_CREDENTIAL_H_ diff --git a/core/include/alibabacloud/core/CredentialsProvider.h b/core/include/alibabacloud/core/CredentialsProvider.h new file mode 100644 index 000000000..e206683aa --- /dev/null +++ b/core/include/alibabacloud/core/CredentialsProvider.h @@ -0,0 +1,36 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_CREDENTIALSPROVIDER_H_ +#define ALIBABACLOUD_CORE_CREDENTIALSPROVIDER_H_ + +#include "CoreExport.h" +#include "Credentials.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT CredentialsProvider + { + public: + CredentialsProvider() = default; + virtual ~CredentialsProvider() = default; + + virtual Credentials getCredentials() = 0; + private: + + }; +} +#endif \ No newline at end of file diff --git a/core/include/alibabacloud/core/EcsInstanceProfileConfigLoader.h b/core/include/alibabacloud/core/EcsInstanceProfileConfigLoader.h new file mode 100644 index 000000000..6739901da --- /dev/null +++ b/core/include/alibabacloud/core/EcsInstanceProfileConfigLoader.h @@ -0,0 +1,40 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_ECSINSTANCEPROFILECONFIGLOADER_H_ +#define ALIBABACLOUD_CORE_ECSINSTANCEPROFILECONFIGLOADER_H_ + +#include +#include "InstanceProfileConfigLoader.h" + +namespace AlibabaCloud +{ + class EcsMetadataClient; + class EcsInstanceProfileConfigLoader : public InstanceProfileConfigLoader + { + public: + EcsInstanceProfileConfigLoader(); + EcsInstanceProfileConfigLoader(const std::shared_ptr& metadataClient); + ~EcsInstanceProfileConfigLoader(); + + protected: + virtual bool loadInternal() override; + + private: + std::shared_ptr metadataClient_; + }; +} +#endif // !ALIBABACLOUD_CORE_ECSINSTANCEPROFILECONFIGLOADER_H_ diff --git a/core/include/alibabacloud/core/EndpointProvider.h b/core/include/alibabacloud/core/EndpointProvider.h new file mode 100644 index 000000000..a84722342 --- /dev/null +++ b/core/include/alibabacloud/core/EndpointProvider.h @@ -0,0 +1,51 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_ENDPOINTPROVIDER_H_ +#define ALIBABACLOUD_CORE_ENDPOINTPROVIDER_H_ + +#include +#include +#include +#include + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT EndpointProvider + { + public: + EndpointProvider(const std::shared_ptr& locationClient, + const std::string regionId, + const std::string serviceCode, + int durationSeconds = 3600); + ~EndpointProvider(); + + std::string getEndpoint(); + + private: + void loadEndpoint(); + bool checkExpiry()const; + + std::mutex cachedMutex_; + std::string cachedEndpoint_; + std::shared_ptr locationClient_; + int durationSeconds_; + std::chrono::system_clock::time_point expiry_; + std::string regionId_; + std::string serviceCode_; + }; +} +#endif \ No newline at end of file diff --git a/core/include/alibabacloud/core/Error.h b/core/include/alibabacloud/core/Error.h new file mode 100644 index 000000000..107ddc166 --- /dev/null +++ b/core/include/alibabacloud/core/Error.h @@ -0,0 +1,48 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_ERROR_H_ +#define ALIBABACLOUD_CORE_ERROR_H_ + +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT Error + { + public: + Error() = default; + Error(std::string code, const std::string message); + ~Error() = default; + + std::string errorCode()const; + std::string errorMessage() const; + std::string host() const; + std::string requestId() const; + void setErrorCode(const std::string &code); + void setErrorMessage(const std::string& message); + void setHost(const std::string& host); + void setRequestId(const std::string& request); + private: + std::string errorCode_; + std::string message_; + std::string host_; + std::string requestId_; + }; +} + +#endif // !ALIBABACLOUD_CORE_ERROR_H_ diff --git a/core/include/alibabacloud/core/Global.h b/core/include/alibabacloud/core/Global.h new file mode 100644 index 000000000..74699fd99 --- /dev/null +++ b/core/include/alibabacloud/core/Global.h @@ -0,0 +1,41 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_GLOBAL_H_ +#define ALIBABACLOUD_CORE_GLOBAL_H_ + +#include "Config.h" + +#if defined(_WIN32) +# ifdef _MSC_VER +# pragma warning(disable : 4251) +# endif // _MSC_VER +# define ALIBABACLOUD_DECL_EXPORT __declspec(dllexport) +# define ALIBABACLOUD_DECL_IMPORT __declspec(dllimport) +#elif defined(__linux__) +# define ALIBABACLOUD_DECL_EXPORT __attribute__((visibility("default"))) +# define ALIBABACLOUD_DECL_IMPORT __attribute__((visibility("default"))) +#endif + +#if !defined(ALIBABACLOUD_DECL_EXPORT) +# define ALIBABACLOUD_DECL_EXPORT +#endif + +#if !defined(ALIBABACLOUD_DECL_IMPORT) +# define ALIBABACLOUD_DECL_IMPORT +#endif + +#endif // !ALIBABACLOUD_CORE_GLOBAL_H_ diff --git a/core/include/alibabacloud/core/HmacSha1Signer.h b/core/include/alibabacloud/core/HmacSha1Signer.h new file mode 100644 index 000000000..f8675cbbe --- /dev/null +++ b/core/include/alibabacloud/core/HmacSha1Signer.h @@ -0,0 +1,34 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_HMACSHA1SIGNER_H_ +#define ALIBABACLOUD_CORE_HMACSHA1SIGNER_H_ + +#include "Signer.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT HmacSha1Signer : public Signer + { + public: + HmacSha1Signer(); + ~HmacSha1Signer(); + + virtual std::string generate(const std::string &src, const std::string &secret)const override; + }; +} + +#endif // !ALIBABACLOUD_CORE_HMACSHA1SIGNER_H_ diff --git a/core/include/alibabacloud/core/HttpClient.h b/core/include/alibabacloud/core/HttpClient.h new file mode 100644 index 000000000..365386d39 --- /dev/null +++ b/core/include/alibabacloud/core/HttpClient.h @@ -0,0 +1,43 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_HTTPCLIENT_H_ +#define ALIBABACLOUD_CORE_HTTPCLIENT_H_ + +#include +#include +#include +#include +#include + +namespace AlibabaCloud +{ + class HttpClient + { + public: + typedef Outcome HttpResponseOutcome; + + HttpClient(); + virtual ~HttpClient(); + + virtual HttpResponseOutcome makeRequest(const HttpRequest &request) = 0; + NetworkProxy proxy()const; + void setProxy(const NetworkProxy &proxy); + private: + NetworkProxy proxy_; + }; +} +#endif \ No newline at end of file diff --git a/core/include/alibabacloud/core/HttpMessage.h b/core/include/alibabacloud/core/HttpMessage.h new file mode 100644 index 000000000..e9a579b36 --- /dev/null +++ b/core/include/alibabacloud/core/HttpMessage.h @@ -0,0 +1,82 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_HTTPMESSAGE_H_ +#define ALIBABACLOUD_CORE_HTTPMESSAGE_H_ + +#include +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT HttpMessage + { + private: + struct ALIBABACLOUD_CORE_EXPORT nocaseLess + { + bool operator() (const std::string & s1, const std::string & s2) const; + }; + public: + enum KnownHeader + { + Accept, + AcceptCharset, + AcceptEncoding, + AcceptLanguage, + Authorization, + Connection, + ContentLength, + ContentMD5, + ContentType, + Date, + Host, + Server, + UserAgent + }; + + typedef std::string HeaderNameType; + typedef std::string HeaderValueType; + typedef std::map HeaderCollection; + + HttpMessage(const HttpMessage &other); + HttpMessage(HttpMessage &&other); + HttpMessage& operator=(const HttpMessage &other); + HttpMessage& operator=(HttpMessage &&other); + virtual ~HttpMessage(); + + void addHeader(const HeaderNameType &name, const HeaderValueType &value); + void addHeader(KnownHeader header, const HeaderValueType &value); + const char* body()const; + size_t bodySize()const; + bool hasBody()const; + HeaderValueType header(const HeaderNameType &name)const; + HeaderValueType header(KnownHeader header)const; + HeaderCollection headers()const; + void removeHeader(const HeaderNameType &name); + void removeHeader(KnownHeader header); + void setBody(const char *data, size_t size); + void setHeader(const HeaderNameType &name, const HeaderValueType &value); + void setHeader(KnownHeader header, const std::string &value); + protected: + HttpMessage(); + private: + char *body_; + size_t bodySize_; + HeaderCollection headers_; + }; +} +#endif // !ALIBABACLOUD_CORE_HTTPMESSAGE_H_ diff --git a/core/include/alibabacloud/core/HttpRequest.h b/core/include/alibabacloud/core/HttpRequest.h new file mode 100644 index 000000000..95c5e97e3 --- /dev/null +++ b/core/include/alibabacloud/core/HttpRequest.h @@ -0,0 +1,53 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_HTTPREQUEST_H_ +#define ALIBABACLOUD_CORE_HTTPREQUEST_H_ + +#include +#include "HttpMessage.h" +#include "Url.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT HttpRequest : public HttpMessage + { + public: + enum Method + { + Get, + Head, + Post, + Put, + Delete, + Connect, + Options, + Patch, + Trace + }; + explicit HttpRequest(const Url &url = Url(), Method method = Get); + ~HttpRequest(); + + Method method()const; + void setMethod(Method method); + void setUrl(const Url &url); + Url url()const; + private: + Method method_; + Url url_; + }; +} +#endif diff --git a/core/include/alibabacloud/core/HttpResponse.h b/core/include/alibabacloud/core/HttpResponse.h new file mode 100644 index 000000000..77a004fd0 --- /dev/null +++ b/core/include/alibabacloud/core/HttpResponse.h @@ -0,0 +1,43 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_HTTPRESPONSE_H_ +#define ALIBABACLOUD_CORE_HTTPRESPONSE_H_ + +#include +#include "CoreExport.h" +#include "HttpMessage.h" +#include "HttpRequest.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT HttpResponse : public HttpMessage + { + public: + HttpResponse(); + explicit HttpResponse(const HttpRequest & request); + ~HttpResponse(); + + HttpRequest request()const; + void setStatusCode(int code); + int statusCode()const; + private: + int statusCode_; + HttpRequest request_; + }; +} + +#endif // !ALIBABACLOUD_CORE_HTTPRESPONSE_H_ diff --git a/core/include/alibabacloud/core/InstanceProfileConfigLoader.h b/core/include/alibabacloud/core/InstanceProfileConfigLoader.h new file mode 100644 index 000000000..257cde546 --- /dev/null +++ b/core/include/alibabacloud/core/InstanceProfileConfigLoader.h @@ -0,0 +1,47 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_INSTANCEPROFILECONFIGLOADER_H_ +#define ALIBABACLOUD_CORE_INSTANCEPROFILECONFIGLOADER_H_ + +#include +#include +#include +#include "Profile.h" + +namespace AlibabaCloud +{ + class InstanceProfileConfigLoader + { + public: + InstanceProfileConfigLoader(); + virtual ~InstanceProfileConfigLoader(); + + bool load(); + bool persistProfiles(const std::map& profiles); + + std::map allProfiles() const; + std::chrono::system_clock::time_point lastLoadTime() const; + + protected: + virtual bool loadInternal() = 0; + virtual bool persistInternal(const std::map&); + + std::map profiles_; + std::chrono::system_clock::time_point lastLoadTime_; + }; +} +#endif // !ALIBABACLOUD_CORE_INSTANCEPROFILECONFIGLOADER_H_ diff --git a/core/include/alibabacloud/core/InstanceProfileCredentials.h b/core/include/alibabacloud/core/InstanceProfileCredentials.h new file mode 100644 index 000000000..8f23addfe --- /dev/null +++ b/core/include/alibabacloud/core/InstanceProfileCredentials.h @@ -0,0 +1,34 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_INSTANCEPROFILECREDENTIALS_H_ +#define ALIBABACLOUD_CORE_INSTANCEPROFILECREDENTIALS_H_ + +#include "BasicSessionCredentials.h" + +namespace AlibabaCloud +{ + class InstanceProfileCredentials : public BasicSessionCredentials + { + public: + InstanceProfileCredentials(); + ~InstanceProfileCredentials(); + + private: + + }; +} +#endif // !ALIBABACLOUD_CORE_INSTANCEPROFILECREDENTIALS_H_ diff --git a/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h b/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h new file mode 100644 index 000000000..ad6ae81ff --- /dev/null +++ b/core/include/alibabacloud/core/InstanceProfileCredentialsProvider.h @@ -0,0 +1,47 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_INSTANCEPROFILECREDENTIALSPROVIDER_H_ +#define ALIBABACLOUD_CORE_INSTANCEPROFILECREDENTIALSPROVIDER_H_ + +#include +#include +#include +#include "CredentialsProvider.h" +#include "Credentials.h" + +namespace AlibabaCloud +{ + class EcsInstanceProfileConfigLoader; + class InstanceProfileConfigLoader; + class ALIBABACLOUD_CORE_EXPORT InstanceProfileCredentialsProvider : public CredentialsProvider + { + public: + InstanceProfileCredentialsProvider(size_t refreshRateMs = (1000 * 60 * 10)); + InstanceProfileCredentialsProvider(const std::shared_ptr&, size_t refreshRateMs = (1000 * 60 * 10)); + + virtual Credentials getCredentials() override; + private: + void refreshIfExpired(); + virtual bool isTimeToRefresh(long reloadFrequency); + + std::chrono::system_clock::time_point lastLoaded_; + std::shared_ptr metadataConfigLoader_; + size_t loadFrequencyMs_; + mutable std::mutex m_reloadMutex; + }; +} +#endif \ No newline at end of file diff --git a/core/include/alibabacloud/core/NetworkProxy.h b/core/include/alibabacloud/core/NetworkProxy.h new file mode 100644 index 000000000..1a2a557d4 --- /dev/null +++ b/core/include/alibabacloud/core/NetworkProxy.h @@ -0,0 +1,59 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_NETWORKPROXY_H_ +#define ALIBABACLOUD_CORE_NETWORKPROXY_H_ + +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT NetworkProxy + { + public: + enum Type + { + None = 0, + Http, + Socks5 + }; + NetworkProxy(Type type = None, + const std::string &hostName = "", + uint16_t port = 0, + const std::string &user = "", + const std::string &password = ""); + ~NetworkProxy(); + + std::string hostName() const; + std::string password() const; + uint16_t port() const; + void setHostName(const std::string &hostName); + void setPassword(const std::string &password); + void setPort(uint16_t port); + void setType(Type type); + void setUser(const std::string &user); + Type type() const; + std::string user() const; + private: + std::string hostName_; + std::string password_; + uint16_t port_; + Type type_; + std::string user_; + }; +} +#endif // !ALIBABACLOUD_CORE_NETWORKPROXY_H_ diff --git a/core/include/alibabacloud/core/Outcome.h b/core/include/alibabacloud/core/Outcome.h new file mode 100644 index 000000000..e643715a2 --- /dev/null +++ b/core/include/alibabacloud/core/Outcome.h @@ -0,0 +1,72 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_OUTCOME_H_ +#define ALIBABACLOUD_CORE_OUTCOME_H_ + +#include + +namespace AlibabaCloud +{ + template + class Outcome + { + public: + Outcome() : + success_(true), e_(), r_() { } + explicit Outcome(const E &e) : + e_(e), success_(false), r_() { } + explicit Outcome(const R &r) : + r_(r), success_(true), e_() { } + Outcome(const Outcome &other) : + success_(other.success_), + e_(other.e_), + r_(other.r_) + { } + Outcome(Outcome &&other) + { + *this = std::move(other); + } + Outcome & operator=(const Outcome &other) + { + if (this != &other) { + success_ = other.success_; + e_ = other.e_; + r_ = other.r_; + } + return *this; + } + Outcome & operator=(Outcome &&other) + { + if (this != &other) + { + success_ = other.success_; + r_ = other.r_; + e_ = other.e_; + } + return *this; + } + + bool isSuccess()const { return success_; } + E error()const { return e_; } + R result()const { return r_; } + private: + bool success_; + E e_; + R r_; + }; +} +#endif // !ALIBABACLOUD_CORE_OUTCOME_H_ diff --git a/core/include/alibabacloud/core/Profile.h b/core/include/alibabacloud/core/Profile.h new file mode 100644 index 000000000..2e59866a4 --- /dev/null +++ b/core/include/alibabacloud/core/Profile.h @@ -0,0 +1,52 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_PROFILE_H_ +#define ALIBABACLOUD_CORE_PROFILE_H_ + +#include +#include +#include + +namespace AlibabaCloud +{ + class Profile + { + public: + std::string name() const; + void setName(const std::string& value); + Credentials credentials() const; + void setCredentials(const Credentials& value); + std::string region() const; + void setRegion(const std::string& value); + std::string roleArn() const; + void setRoleArn(const std::string& value); + std::string sourceProfile() const; + void setSourceProfile(const std::string& value); + void setAllKeyValPairs(const std::map& map); + std::string value(const std::string& key); + + private: + std::string name_; + std::string region_; + Credentials credentials_; + std::string roleArn_; + std::string sourceProfile_; + std::map allKeyValPairs_; + }; + +} +#endif // !ALIBABACLOUD_CORE_PROFILE_H_ diff --git a/core/include/alibabacloud/core/RoaServiceClient.h b/core/include/alibabacloud/core/RoaServiceClient.h new file mode 100644 index 000000000..9550bc80d --- /dev/null +++ b/core/include/alibabacloud/core/RoaServiceClient.h @@ -0,0 +1,54 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_ROASERVICECLIENT_H_ +#define ALIBABACLOUD_CORE_ROASERVICECLIENT_H_ + +#include +#include +#include +#include "CoreClient.h" +#include "CredentialsProvider.h" +#include "HmacSha1Signer.h" +#include "HttpRequest.h" +#include "RoaServiceRequest.h" + +namespace AlibabaCloud +{ + class RoaErrorMarshaller; + class ALIBABACLOUD_CORE_EXPORT RoaServiceClient : public CoreClient + { + public: + typedef Outcome JsonOutcome; + + RoaServiceClient(const std::shared_ptr &credentialsProvider, + const ClientConfiguration &configuration, + const std::shared_ptr &signer = std::make_shared()); + virtual ~RoaServiceClient(); + + protected: + JsonOutcome makeRequest(const std::string &endpoint, const RoaServiceRequest &msg, HttpRequest::Method method = HttpRequest::Method::Get)const; + virtual HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const override; + HttpRequest buildHttpRequest(const std::string & endpoint, const RoaServiceRequest &msg, HttpRequest::Method method)const; + private: + std::string canonicalizedHeaders(const HttpMessage::HeaderCollection &headers)const; + std::string canonicalizedResource(const std::string &path, std::map ¶ms)const; + + std::shared_ptr credentialsProvider_; + std::shared_ptr signer_; + }; +} +#endif // !ALIBABACLOUD_CORE_ROASERVICECLIENT_H_ diff --git a/core/include/alibabacloud/core/RoaServiceRequest.h b/core/include/alibabacloud/core/RoaServiceRequest.h new file mode 100644 index 000000000..86440b8a6 --- /dev/null +++ b/core/include/alibabacloud/core/RoaServiceRequest.h @@ -0,0 +1,32 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_ROASERVICEREQUEST_H_ +#define ALIBABACLOUD_CORE_ROASERVICEREQUEST_H_ + +#include "ServiceRequest.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT RoaServiceRequest : public ServiceRequest + { + public: + RoaServiceRequest(const std::string &product, const std::string &version); + virtual ~RoaServiceRequest(); + + }; +} +#endif // !ALIBABACLOUD_CORE_ROASERVICEREQUEST_H_ \ No newline at end of file diff --git a/core/include/alibabacloud/core/RpcServiceClient.h b/core/include/alibabacloud/core/RpcServiceClient.h new file mode 100644 index 000000000..cd7d72a58 --- /dev/null +++ b/core/include/alibabacloud/core/RpcServiceClient.h @@ -0,0 +1,55 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_RPCSERVICECLIENT_H_ +#define ALIBABACLOUD_CORE_RPCSERVICECLIENT_H_ + +#include +#include +#include +#include "CoreClient.h" +#include "CredentialsProvider.h" +#include "HmacSha1Signer.h" +#include "HttpRequest.h" +#include "RpcServiceRequest.h" +#include "Outcome.h" + +namespace AlibabaCloud +{ + class RpcErrorMarshaller; + class RpcServiceRequest; + class ALIBABACLOUD_CORE_EXPORT RpcServiceClient : public CoreClient + { + public: + typedef Outcome JsonOutcome; + + RpcServiceClient(const std::shared_ptr &credentialsProvider, + const ClientConfiguration &configuration, + const std::shared_ptr &signer = std::make_shared()); + virtual ~RpcServiceClient(); + + protected: + JsonOutcome makeRequest(const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method = HttpRequest::Method::Get)const; + virtual HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const override; + HttpRequest buildHttpRequest(const std::string & endpoint, const RpcServiceRequest &msg, HttpRequest::Method method)const; + private: + std::string canonicalizedQuery(const std::map ¶ms)const; + + std::shared_ptr credentialsProvider_; + std::shared_ptr signer_; + }; +} +#endif // !ALIBABACLOUD_CORE_RPCSERVICECLIENT_H_ diff --git a/core/include/alibabacloud/core/RpcServiceRequest.h b/core/include/alibabacloud/core/RpcServiceRequest.h new file mode 100644 index 000000000..4ab0c0a75 --- /dev/null +++ b/core/include/alibabacloud/core/RpcServiceRequest.h @@ -0,0 +1,36 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_RPCSERVICEREQUEST_H_ +#define ALIBABACLOUD_CORE_RPCSERVICEREQUEST_H_ + +#include "ServiceRequest.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT RpcServiceRequest : public ServiceRequest + { + public: + RpcServiceRequest(const std::string &product, const std::string &version, const std::string & action); + virtual ~RpcServiceRequest(); + + std::string actionName()const; + protected: + void setActionName(const std::string &name); + + }; +} +#endif // !ALIBABACLOUD_CORE_RPCSERVICEREQUEST_H_ \ No newline at end of file diff --git a/core/include/alibabacloud/core/Runnable.h b/core/include/alibabacloud/core/Runnable.h new file mode 100644 index 000000000..a4c891a9b --- /dev/null +++ b/core/include/alibabacloud/core/Runnable.h @@ -0,0 +1,35 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_RUNNABLE_H_ +#define ALIBABACLOUD_CORE_RUNNABLE_H_ + +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT Runnable + { + public: + explicit Runnable(const std::function f); + void run()const; + + private: + std::function f_; + }; +} +#endif // !ALIBABACLOUD_CORE_RUNNABLE_H_ diff --git a/core/include/alibabacloud/core/ServiceRequest.h b/core/include/alibabacloud/core/ServiceRequest.h new file mode 100644 index 000000000..3067d9c8a --- /dev/null +++ b/core/include/alibabacloud/core/ServiceRequest.h @@ -0,0 +1,68 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_SERVICEREQUEST_H_ +#define ALIBABACLOUD_CORE_SERVICEREQUEST_H_ + +#include +#include "CoreExport.h" +#include "Url.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT ServiceRequest + { + public: + typedef std::string ParameterNameType; + typedef std::string ParameterValueType; + typedef std::map ParameterCollection; + + virtual ~ServiceRequest(); + + const char* content()const; + size_t contentSize()const; + bool hasContent()const; + ParameterCollection parameters()const; + std::string product()const; + std::string resourcePath()const; + std::string version()const; + protected: + ServiceRequest(const std::string &product, const std::string &version); + ServiceRequest(const ServiceRequest &other); + ServiceRequest(ServiceRequest &&other); + ServiceRequest& operator=(const ServiceRequest &other); + ServiceRequest& operator=(ServiceRequest &&other); + + void addParameter(const ParameterNameType &name, const ParameterValueType &value); + ParameterValueType parameter(const ParameterNameType &name)const; + void removeParameter(const ParameterNameType &name); + void setContent(const char *data, size_t size); + void setParameter(const ParameterNameType &name, const ParameterValueType &value); + void setParameters(const ParameterCollection ¶ms); + void setResourcePath(const std::string &path); + void setProduct(const std::string &product); + void setVersion(const std::string &version); + private: + char *content_; + size_t contentSize_; + ParameterCollection params_; + std::string product_; + std::string resourcePath_; + std::string version_; + }; +} + +#endif // !ALIBABACLOUD_CORE_SERVICEREQUEST_H_ \ No newline at end of file diff --git a/core/include/alibabacloud/core/ServiceResult.h b/core/include/alibabacloud/core/ServiceResult.h new file mode 100644 index 000000000..bc9b27073 --- /dev/null +++ b/core/include/alibabacloud/core/ServiceResult.h @@ -0,0 +1,40 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_SERVICERESULT_H_ +#define ALIBABACLOUD_CORE_SERVICERESULT_H_ + +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class PayloadReader; + class ALIBABACLOUD_CORE_EXPORT ServiceResult + { + public: + ServiceResult(); + virtual ~ServiceResult(); + + std::string requestId()const; + protected: + void setRequestId(const std::string &requestId); + private: + std::string requestId_; + }; +} + +#endif // !ALIBABACLOUD_CORE_SERVICERESULT_H_ diff --git a/core/include/alibabacloud/core/Signer.h b/core/include/alibabacloud/core/Signer.h new file mode 100644 index 000000000..28c259b2d --- /dev/null +++ b/core/include/alibabacloud/core/Signer.h @@ -0,0 +1,46 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_SIGNER_H_ +#define ALIBABACLOUD_CORE_SIGNER_H_ + +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT Signer + { + public: + enum Type + { + HmacSha1, + }; + virtual ~Signer(); + + virtual std::string generate(const std::string &src, const std::string &secret)const = 0; + std::string name()const; + Type type() const; + std::string version()const; + protected: + Signer(Type type, const std::string &name, const std::string &version = "1.0"); + private: + std::string name_; + std::string version_; + Type type_; + }; +} +#endif // !ALIBABACLOUD_CORE_SIGNER_H_ diff --git a/core/include/alibabacloud/core/SimpleCredentialsProvider.h b/core/include/alibabacloud/core/SimpleCredentialsProvider.h new file mode 100644 index 000000000..ac7cf777f --- /dev/null +++ b/core/include/alibabacloud/core/SimpleCredentialsProvider.h @@ -0,0 +1,37 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_SIMPLECREDENTIALSPROVIDER_H_ +#define ALIBABACLOUD_CORE_SIMPLECREDENTIALSPROVIDER_H_ + +#include "CredentialsProvider.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT SimpleCredentialsProvider : public CredentialsProvider + { + public: + SimpleCredentialsProvider(const Credentials &credentials); + SimpleCredentialsProvider(const std::string &accessKeyId, + const std::string &accessKeySecret); + ~SimpleCredentialsProvider(); + + virtual Credentials getCredentials() override; + private: + Credentials credentials_; + }; +} +#endif // !ALIBABACLOUD_CORE_SIMPLECREDENTIALSPROVIDER_H_ diff --git a/core/include/alibabacloud/core/StsAssumeRoleCredentialsProvider.h b/core/include/alibabacloud/core/StsAssumeRoleCredentialsProvider.h new file mode 100644 index 000000000..882e57186 --- /dev/null +++ b/core/include/alibabacloud/core/StsAssumeRoleCredentialsProvider.h @@ -0,0 +1,52 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_STS_STSASSUMEROLECREDENTIALSPROVIDER_H_ +#define ALIBABACLOUD_STS_STSASSUMEROLECREDENTIALSPROVIDER_H_ + +#include +#include +#include +#include + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT StsAssumeRoleCredentialsProvider : public CredentialsProvider + { + public: + StsAssumeRoleCredentialsProvider(const std::shared_ptr& stsClient, + const std::string &roleArn, + const std::string &roleSessionName, + const std::string &policy = "", + int durationSeconds = 3600); + ~StsAssumeRoleCredentialsProvider(); + + virtual Credentials getCredentials() override; + private: + void loadCredentials(); + bool checkExpiry()const; + + std::mutex cachedMutex_; + Credentials cachedCredentials_; + std::shared_ptr stsClient_; + int durationSeconds_; + std::chrono::system_clock::time_point expiry_; + std::string policy_; + std::string roleArn_; + std::string roleSessionName_; + }; +} +#endif // !ALIBABACLOUD_CORE_STSASSUMEROLECREDENTIALSPROVIDER_H_ \ No newline at end of file diff --git a/core/include/alibabacloud/core/Url.h b/core/include/alibabacloud/core/Url.h new file mode 100644 index 000000000..0bcfb19f3 --- /dev/null +++ b/core/include/alibabacloud/core/Url.h @@ -0,0 +1,75 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_URL_H_ +#define ALIBABACLOUD_CORE_URL_H_ + +#include +#include "CoreExport.h" + +namespace AlibabaCloud +{ + class ALIBABACLOUD_CORE_EXPORT Url + { + public: + explicit Url(const std::string &url = ""); + Url(const Url &other) = default; + Url(Url &&other) = default; + ~Url(); + Url & operator=(const Url &url) = default; + Url & operator=(Url &&other) = default; + bool operator==(const Url &url) const; + bool operator!=(const Url &url) const; + + std::string authority() const; + void clear(); + std::string fragment() const; + void fromString(const std::string &url); + bool hasFragment() const; + bool hasQuery() const; + std::string host()const; + bool isEmpty() const; + bool isValid() const; + int port()const; + std::string password() const; + std::string path() const; + std::string query() const; + std::string scheme() const; + void setAuthority(const std::string &authority); + void setFragment(const std::string &fragment); + void setHost(const std::string &host); + void setPassword(const std::string &password); + void setPath(const std::string &path); + void setPort(int port); + void setQuery(const std::string &query); + void setScheme(const std::string &scheme); + void setUserInfo(const std::string &userInfo); + void setUserName(const std::string &userName); + std::string toString()const; + std::string userInfo() const; + std::string userName() const; + private: + std::string scheme_; + std::string userName_; + std::string password_; + std::string host_; + std::string path_; + int port_; + std::string query_; + std::string fragment_; + }; +} +#endif // !ALIBABACLOUD_CORE_URL_H_ diff --git a/core/include/alibabacloud/core/location/LocationClient.h b/core/include/alibabacloud/core/location/LocationClient.h new file mode 100644 index 000000000..8669ff449 --- /dev/null +++ b/core/include/alibabacloud/core/location/LocationClient.h @@ -0,0 +1,51 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_LOCATION_LOCATIONCLIENT_H_ +#define ALIBABACLOUD_CORE_LOCATION_LOCATIONCLIENT_H_ + +#include +#include +#include +#include "model/DescribeEndpointsRequest.h" +#include "model/DescribeEndpointsResult.h" + +namespace AlibabaCloud +{ + namespace Location + { + class ALIBABACLOUD_CORE_EXPORT LocationClient : public RpcServiceClient + { + public: + typedef Outcome DescribeEndpointsOutcome; + typedef std::future DescribeEndpointsOutcomeCallable; + typedef std::function&)> DescribeEndpointsAsyncHandler; + + LocationClient(const Credentials &credentials, const ClientConfiguration &configuration); + LocationClient(const std::shared_ptr &credentialsProvider, const ClientConfiguration &configuration); + LocationClient(const std::string &accessKeyId, const std::string &accessKeySecret, const ClientConfiguration &configuration); + ~LocationClient(); + + DescribeEndpointsOutcome describeEndpoints(const Model::DescribeEndpointsRequest &request)const; + void describeEndpointsAsync(const Model::DescribeEndpointsRequest& request, const DescribeEndpointsAsyncHandler& handler, const std::shared_ptr& context = nullptr) const; + DescribeEndpointsOutcomeCallable describeEndpointsCallable(const Model::DescribeEndpointsRequest& request) const; + private: + virtual EndpointOutcome endpoint()const override; + }; + } +} + +#endif // !ALIBABACLOUD_CORE_LOCATION_LOCATIONCLIENT_H_ diff --git a/core/include/alibabacloud/core/location/LocationRequest.h b/core/include/alibabacloud/core/location/LocationRequest.h new file mode 100644 index 000000000..7423f17f7 --- /dev/null +++ b/core/include/alibabacloud/core/location/LocationRequest.h @@ -0,0 +1,38 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_LOCATION_LOCATIONREQUEST_H_ +#define ALIBABACLOUD_CORE_LOCATION_LOCATIONREQUEST_H_ + +#include + +namespace AlibabaCloud +{ + namespace Location + { + class ALIBABACLOUD_CORE_EXPORT LocationRequest : public RpcServiceRequest + { + public: + explicit LocationRequest(const std::string & action); + virtual ~LocationRequest(); + + private: + + }; + } +} + +#endif // !ALIBABACLOUD_CORE_LOCATION_LOCATIONREQUEST_H_ \ No newline at end of file diff --git a/core/include/alibabacloud/core/location/model/DescribeEndpointsRequest.h b/core/include/alibabacloud/core/location/model/DescribeEndpointsRequest.h new file mode 100644 index 000000000..5924c309e --- /dev/null +++ b/core/include/alibabacloud/core/location/model/DescribeEndpointsRequest.h @@ -0,0 +1,46 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_LOCATION_MODEL_DESCRIBEENDPOINTSREQUEST_H_ +#define ALIBABACLOUD_CORE_LOCATION_MODEL_DESCRIBEENDPOINTSREQUEST_H_ + +#include +#include + +namespace AlibabaCloud +{ + namespace Location + { + namespace Model + { + class ALIBABACLOUD_CORE_EXPORT DescribeEndpointsRequest : public LocationRequest + { + public: + DescribeEndpointsRequest(); + ~DescribeEndpointsRequest(); + + std::string serviceCode()const; + void setServiceCode(const std::string& serviceCode); + std::string id()const; + void setId(const std::string& id); + std::string type()const; + void setType(const std::string& type); + + }; + } + } +} +#endif // !ALIBABACLOUD_CORE_LOCATION_MODEL_DESCRIBEENDPOINTSREQUEST_H_ diff --git a/core/include/alibabacloud/core/location/model/DescribeEndpointsResult.h b/core/include/alibabacloud/core/location/model/DescribeEndpointsResult.h new file mode 100644 index 000000000..bca37cfc0 --- /dev/null +++ b/core/include/alibabacloud/core/location/model/DescribeEndpointsResult.h @@ -0,0 +1,64 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_LOCATION_MODEL_DESCRIBEENDPOINTSRESULT_H_ +#define ALIBABACLOUD_CORE_LOCATION_MODEL_DESCRIBEENDPOINTSRESULT_H_ + +#include +#include +#include +#include + +namespace AlibabaCloud +{ + namespace Location + { + namespace Model + { + class ALIBABACLOUD_CORE_EXPORT DescribeEndpointsResult : public ServiceResult + { + public: + struct Endpoint + { + std::string endpoint; + std::string id; + std::string namespace_; + std::string serivceCode; + std::string type; + std::vector protocols; + }; + + DescribeEndpointsResult(); + explicit DescribeEndpointsResult(const std::string &payload); + ~DescribeEndpointsResult(); + + std::vector endpoints()const; + void setEndpoints(const std::vector &endpoints); + + bool success()const; + void setSuccess(const bool &success); + + protected: + void parse(const std::string &payload); + private: + std::vector endpoints_; + bool success_; + + }; + } + } +} +#endif // !ALIBABACLOUD_CORE_LOCATION_MODEL_DESCRIBEENDPOINTSRESULT_H_ \ No newline at end of file diff --git a/core/include/alibabacloud/core/sts/StsClient.h b/core/include/alibabacloud/core/sts/StsClient.h new file mode 100644 index 000000000..6b4966046 --- /dev/null +++ b/core/include/alibabacloud/core/sts/StsClient.h @@ -0,0 +1,60 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_STS_STSCLIENT_H_ +#define ALIBABACLOUD_CORE_STS_STSCLIENT_H_ + +#include +#include +#include +#include "model/AssumeRoleRequest.h" +#include "model/AssumeRoleResult.h" +#include "model/GetCallerIdentityRequest.h" +#include "model/GetCallerIdentityResult.h" + +namespace AlibabaCloud +{ + namespace Sts + { + class ALIBABACLOUD_CORE_EXPORT StsClient : public RpcServiceClient + { + public: + typedef Outcome AssumeRoleOutcome; + typedef std::future AssumeRoleOutcomeCallable; + typedef std::function&)> AssumeRoleAsyncHandler; + + typedef Outcome GetCallerIdentityOutcome; + typedef std::future GetCallerIdentityOutcomeCallable; + typedef std::function&)> GetCallerIdentityAsyncHandler; + + StsClient(const Credentials &credentials, const ClientConfiguration &configuration); + StsClient(const std::shared_ptr &credentialsProvider, const ClientConfiguration &configuration); + StsClient(const std::string &accessKeyId, const std::string &accessKeySecret, const ClientConfiguration &configuration); + ~StsClient(); + + AssumeRoleOutcome assumeRole(const Model::AssumeRoleRequest &request)const; + void assumeRoleAsync(const Model::AssumeRoleRequest& request, const AssumeRoleAsyncHandler& handler, const std::shared_ptr& context = nullptr) const; + AssumeRoleOutcomeCallable assumeRoleCallable(const Model::AssumeRoleRequest& request) const; + GetCallerIdentityOutcome getCallerIdentity(const Model::GetCallerIdentityRequest &request)const; + void getCallerIdentityAsync(const Model::GetCallerIdentityRequest& request, const GetCallerIdentityAsyncHandler& handler, const std::shared_ptr& context = nullptr) const; + GetCallerIdentityOutcomeCallable getCallerIdentityCallable(const Model::GetCallerIdentityRequest& request) const; + private: + virtual EndpointOutcome endpoint()const override; + }; + } +} + +#endif // !ALIBABACLOUD_CORE_STS_STSCLIENT_H_ diff --git a/core/include/alibabacloud/core/sts/StsRequest.h b/core/include/alibabacloud/core/sts/StsRequest.h new file mode 100644 index 000000000..a2a0ba1b1 --- /dev/null +++ b/core/include/alibabacloud/core/sts/StsRequest.h @@ -0,0 +1,38 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_STS_STSSERVICEREQUEST_H_ +#define ALIBABACLOUD_CORE_STS_STSSERVICEREQUEST_H_ + +#include + +namespace AlibabaCloud +{ + namespace Sts + { + class ALIBABACLOUD_CORE_EXPORT StsRequest : public RpcServiceRequest + { + public: + explicit StsRequest(const std::string & action); + virtual ~StsRequest(); + + private: + + }; + } +} + +#endif // !ALIBABACLOUD_CORE_STS_STSSERVICEREQUEST_H_ diff --git a/core/include/alibabacloud/core/sts/model/AssumeRoleRequest.h b/core/include/alibabacloud/core/sts/model/AssumeRoleRequest.h new file mode 100644 index 000000000..35161475a --- /dev/null +++ b/core/include/alibabacloud/core/sts/model/AssumeRoleRequest.h @@ -0,0 +1,47 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_STS_MODEL_ASSUMEROLEREQUEST_H_ +#define ALIBABACLOUD_CORE_STS_MODEL_ASSUMEROLEREQUEST_H_ + +#include +#include + +namespace AlibabaCloud +{ + namespace Sts + { + namespace Model + { + class ALIBABACLOUD_CORE_EXPORT AssumeRoleRequest : public StsRequest + { + public: + AssumeRoleRequest(); + ~AssumeRoleRequest(); + + int durationSeconds()const; + std::string policy()const; + std::string roleArn()const; + std::string roleSessionName()const; + void setDurationSeconds(int durationSeconds); + void setPolicy(const std::string &policy); + void setRoleArn(const std::string & roleArn); + void setRoleSessionName(const std::string & roleSessionName); + }; + } + } +} +#endif // !ALIBABACLOUD_CORE_STS_MODEL_ASSUMEROLEREQUEST_H_ diff --git a/core/include/alibabacloud/core/sts/model/AssumeRoleResult.h b/core/include/alibabacloud/core/sts/model/AssumeRoleResult.h new file mode 100644 index 000000000..54a04f855 --- /dev/null +++ b/core/include/alibabacloud/core/sts/model/AssumeRoleResult.h @@ -0,0 +1,59 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_STS_MODEL_ASSUMEROLERESULT_H_ +#define ALIBABACLOUD_CORE_STS_MODEL_ASSUMEROLERESULT_H_ + +#include + +namespace AlibabaCloud +{ + namespace Sts + { + namespace Model + { + class ALIBABACLOUD_CORE_EXPORT AssumeRoleResult : public ServiceResult + { + public: + struct AssumedRoleUser + { + std::string arn; + std::string assumedRoleId; + }; + struct Credentials + { + std::string accessKeyId; + std::string accessKeySecret; + std::string expiration; + std::string securityToken; + }; + + AssumeRoleResult(); + explicit AssumeRoleResult(const std::string &payload); + ~AssumeRoleResult(); + + AssumedRoleUser assumedRoleUser()const; + Credentials credentials()const; + private: + void parse(const std::string &payload); + + AssumedRoleUser assumedRoleUser_; + Credentials credentials_; + }; + } + } +} +#endif // !ALIBABACLOUD_CORE_STS_MODEL_ASSUMEROLERESULT_H_ diff --git a/core/include/alibabacloud/core/sts/model/GetCallerIdentityRequest.h b/core/include/alibabacloud/core/sts/model/GetCallerIdentityRequest.h new file mode 100644 index 000000000..f84876978 --- /dev/null +++ b/core/include/alibabacloud/core/sts/model/GetCallerIdentityRequest.h @@ -0,0 +1,41 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_STS_MODEL_GETCALLERIDENTITYREQUEST_H_ +#define ALIBABACLOUD_CORE_STS_MODEL_GETCALLERIDENTITYREQUEST_H_ + +#include +#include + +namespace AlibabaCloud +{ + namespace Sts + { + namespace Model + { + class ALIBABACLOUD_CORE_EXPORT GetCallerIdentityRequest : public StsRequest + { + public: + GetCallerIdentityRequest(); + ~GetCallerIdentityRequest(); + + private: + + }; + } + } +} +#endif // !ALIBABACLOUD_CORE_STS_MODEL_GETCALLERIDENTITYREQUEST_H_ diff --git a/core/include/alibabacloud/core/sts/model/GetCallerIdentityResult.h b/core/include/alibabacloud/core/sts/model/GetCallerIdentityResult.h new file mode 100644 index 000000000..3ff4a3caa --- /dev/null +++ b/core/include/alibabacloud/core/sts/model/GetCallerIdentityResult.h @@ -0,0 +1,49 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_STS_MODEL_GETCALLERIDENTITYRESULT_H_ +#define ALIBABACLOUD_CORE_STS_MODEL_GETCALLERIDENTITYRESULT_H_ + +#include + +namespace AlibabaCloud +{ + namespace Sts + { + namespace Model + { + class ALIBABACLOUD_CORE_EXPORT GetCallerIdentityResult : public ServiceResult + { + public: + GetCallerIdentityResult(); + explicit GetCallerIdentityResult(const std::string &payload); + ~GetCallerIdentityResult(); + + std::string accountId(); + std::string arn()const; + std::string userId()const; + private: + void parse(const std::string &payload); + + std::string accountId_; + std::string arn_; + std::string userId_; + }; + } + } +} + +#endif // !ALIBABACLOUD_CORE_STS_MODEL_GETCALLERIDENTITYRESULT_H_ diff --git a/core/src/AlibabaCloud.cc b/core/src/AlibabaCloud.cc new file mode 100644 index 000000000..44f4eb824 --- /dev/null +++ b/core/src/AlibabaCloud.cc @@ -0,0 +1,44 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "Executor.h" + +static AlibabaCloud::Executor * executor = nullptr; + +void AlibabaCloud::InitializeSdk() +{ + if (IsSdkInitialized()) + return; + + executor = new Executor; + executor->start(); +} + +bool AlibabaCloud::IsSdkInitialized() +{ + return executor != nullptr; +} + +void AlibabaCloud::ShutdownSdk() +{ + if (!IsSdkInitialized()) + return; + + executor->shutdown(); + delete executor; + executor = nullptr; +} diff --git a/core/src/AsyncCallerContext.cc b/core/src/AsyncCallerContext.cc new file mode 100644 index 000000000..78d0f9efe --- /dev/null +++ b/core/src/AsyncCallerContext.cc @@ -0,0 +1,44 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "Utils.h" + +using namespace AlibabaCloud; + +AsyncCallerContext::AsyncCallerContext() : + uuid_(GenerateUuid()) +{ +} + +AsyncCallerContext::AsyncCallerContext(const std::string &uuid) : + uuid_(uuid) +{ +} + +AsyncCallerContext::~AsyncCallerContext() +{ +} + +std::string AsyncCallerContext::uuid()const +{ + return uuid_; +} + +void AsyncCallerContext::setUuid(const std::string &uuid) +{ + uuid_ = uuid; +} diff --git a/core/src/ClientConfiguration.cc b/core/src/ClientConfiguration.cc new file mode 100644 index 000000000..0d17e4944 --- /dev/null +++ b/core/src/ClientConfiguration.cc @@ -0,0 +1,61 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +ClientConfiguration::ClientConfiguration(const std::string ®ionId, + const NetworkProxy &proxy): + regionId_(regionId), + proxy_(proxy), + endpoint_() +{ +} + +ClientConfiguration::~ClientConfiguration() +{ +} + +std::string ClientConfiguration::endpoint() const +{ + return endpoint_; +} + +NetworkProxy ClientConfiguration::proxy()const +{ + return proxy_; +} + +std::string ClientConfiguration::regionId()const +{ + return regionId_; +} + +void ClientConfiguration::setEndpoint(const std::string & endpoint) +{ + endpoint_ = endpoint; +} + +void ClientConfiguration::setProxy(const NetworkProxy &proxy) +{ + proxy_ = proxy; +} + +void ClientConfiguration::setRegionId(const std::string ®ionId) +{ + regionId_ = regionId; +} \ No newline at end of file diff --git a/core/src/CommonClient.cc b/core/src/CommonClient.cc new file mode 100644 index 000000000..23702d886 --- /dev/null +++ b/core/src/CommonClient.cc @@ -0,0 +1,313 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include +#include +#include +#include +#include "Utils.h" + +using namespace AlibabaCloud; +using namespace Location; + +namespace +{ +#if defined(WIN32) && defined(_MSC_VER) +# define strcasecmp _stricmp +# define strncasecmp _strnicmp +#else +# include +#endif +} + +CommonClient::CommonClient(const Credentials &credentials, const ClientConfiguration &configuration) : + CoreClient(configuration), + credentialsProvider_(std::make_shared(credentials)) +{ + auto locationClient = std::make_shared(credentials, configuration); + endpointProvider_ = std::make_shared(locationClient, configuration.regionId(), "ecs"); +} + +CommonClient::CommonClient(const std::shared_ptr& credentialsProvider, const ClientConfiguration & configuration) : + CoreClient(configuration), + signer_(std::make_shared()) +{ + credentialsProvider_ = credentialsProvider; + auto locationClient = std::make_shared(credentialsProvider, configuration); + endpointProvider_ = std::make_shared(locationClient, configuration.regionId(), "ecs"); +} + +CommonClient::CommonClient(const std::string & accessKeyId, const std::string & accessKeySecret, const ClientConfiguration & configuration) : + CoreClient(configuration), + signer_(std::make_shared()) +{ + credentialsProvider_ = std::make_shared(accessKeyId, accessKeySecret); + + auto locationClient = std::make_shared(accessKeyId, accessKeySecret, configuration); + endpointProvider_ = std::make_shared(locationClient, configuration.regionId(), "ecs"); +} + +CommonClient::~CommonClient() +{ +} + +CommonClient::JsonOutcome CommonClient::makeRequest(const std::string &endpoint, const CommonRequest &msg, HttpRequest::Method method)const +{ + auto outcome = AttemptRequest(endpoint, msg, method); + if (outcome.isSuccess()) + return JsonOutcome(std::string(outcome.result().body(), + outcome.result().bodySize())); + else + return JsonOutcome(outcome.error()); +} + +CommonClient::CommonResponseOutcome CommonClient::commonResponse(const CommonRequest & request) const +{ + auto outcome = makeRequest(request.domain(), request, request.httpMethod()); + + if (outcome.isSuccess()) + return CommonResponseOutcome(CommonResponse(outcome.result())); + else + return CommonResponseOutcome(Error(outcome.error())); +} + +void CommonClient::commonResponseAsync(const CommonRequest & request, const CommonResponseAsyncHandler & handler, const std::shared_ptr& context) const +{ + auto fn = [this, request, handler, context]() + { + handler(this, request, commonResponse(request), context); + }; + + asyncExecute(new Runnable(fn)); +} + +CommonClient::CommonResponseOutcomeCallable CommonClient::commonResponseCallable(const CommonRequest & request) const +{ + auto task = std::make_shared>( + [this, request]() + { + return this->commonResponse(request); + }); + + asyncExecute(new Runnable([task]() { (*task)(); })); + return task->get_future(); +} + +CoreClient::EndpointOutcome CommonClient::endpoint() const +{ + return EndpointOutcome(); +} + +HttpRequest CommonClient::buildHttpRequest(const std::string & endpoint, const ServiceRequest & msg, HttpRequest::Method method) const +{ + return buildHttpRequest(endpoint, dynamic_cast(msg), method); +} + +HttpRequest CommonClient::buildHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const +{ + if (msg.uriPattern().empty() || + (strcasecmp(msg.uriPattern().c_str(),"rpc") == 0)) + return buildRpcHttpRequest(endpoint, msg, method); + else + return buildRoaHttpRequest(endpoint, msg, method); +} + +std::string CommonClient::canonicalizedHeaders(const HttpMessage::HeaderCollection &headers)const +{ + std::map materials; + for (const auto &p : headers) + { + std::string key = p.first; + std::transform(key.begin(), key.end(), key.begin(), ::tolower); + if (key.find("x-acs-") != 0) + continue; + + std::string value = p.second; + StringReplace(value, "\t", " "); + StringReplace(value, "\n", " "); + StringReplace(value, "\r", " "); + StringReplace(value, "\f", " "); + materials[key] = value; + } + + if (materials.empty()) + return std::string(); + std::stringstream ss; + for (const auto &p : materials) + ss << p.first << ":" << p.second << "\n"; + + return ss.str(); +} + +HttpRequest CommonClient::buildRoaHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const +{ + const Credentials credentials = credentialsProvider_->getCredentials(); + + Url url; + url.setScheme("https"); + url.setHost(endpoint); + url.setPath(msg.resourcePath()); + + auto params = msg.headerParameters(); + std::map queryParams; + for (const auto &p : params) { + if (!p.second.empty()) + queryParams[p.first] = p.second; + } + + if (!queryParams.empty()) { + std::stringstream queryString; + for (const auto &p : queryParams) + { + if (p.second.empty()) + queryString << "&" << p.first; + else + queryString << "&" << p.first << "=" << p.second; + } + url.setQuery(queryString.str().substr(1)); + } + + HttpRequest request(url); + request.setMethod(method); + request.setHeader("Accept", "application/json"); + if (msg.hasContent()) { + std::stringstream ss; + ss << msg.contentSize(); + request.setHeader("Content-Length", ss.str()); + request.setHeader("Content-Type", "application/octet-stream"); + request.setHeader("Content-MD5", ComputeContentMD5(msg.content(), msg.contentSize())); + } + + std::time_t t = std::time(nullptr); + std::stringstream date; +#if defined(__GNUG__) && __GNUC__ < 5 + char tmbuff[26]; + strftime(tmbuff, 26, "%a, %d %b %Y %T", std::gmtime(&t)); + date << tmbuff << " GMT"; +#else + date << std::put_time(std::gmtime(&t), "%a, %d %b %Y %T GMT"); +#endif + request.setHeader("Date", date.str()); + request.setHeader("Host", url.host()); + request.setHeader("x-sdk-client", std::string("CPP/").append(ALIBABACLOUD_VERSION_STR)); + request.setHeader("x-acs-region-id", configuration().regionId()); + request.setHeader("x-acs-security-token", credentials.sessionToken()); + request.setHeader("x-acs-signature-method", signer_->name()); + request.setHeader("x-acs-signature-nonce", GenerateUuid()); + request.setHeader("x-acs-signature-version", signer_->version()); + request.setHeader("x-acs-version", msg.version()); + + std::stringstream plaintext; + plaintext << HttpMethodToString(method) << "\n" + << request.header("Accept") << "\n" + << request.header("Content-MD5") << "\n" + << request.header("Content-Type") << "\n" + << request.header("Date") << "\n" + << canonicalizedHeaders(request.headers()); + if (!url.hasQuery()) + plaintext << url.path(); + else + plaintext << url.path() << "?" << url.query(); + + std::stringstream sign; + sign << "acs " + << credentials.accessKeyId() + << ":" + << signer_->generate(plaintext.str(), credentials.accessKeySecret()); + request.setHeader("Authorization", sign.str()); + + return request; +} + + +std::string CommonClient::canonicalizedQuery(const std::map& params) const +{ + if (params.empty()) + return std::string(); + + std::stringstream ss; + for (const auto &p : params) + { + std::string key = UrlEncode(p.first); + StringReplace(key, "+", "%20"); + StringReplace(key, "*", "%2A"); + StringReplace(key, "%7E", "~"); + std::string value = UrlEncode(p.second); + StringReplace(value, "+", "%20"); + StringReplace(value, "*", "%2A"); + StringReplace(value, "%7E", "~"); + ss << "&" << key << "=" << value; + } + return ss.str().substr(1); +} + +HttpRequest CommonClient::buildRpcHttpRequest(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) const +{ + const Credentials credentials = credentialsProvider_->getCredentials(); + + Url url; + url.setScheme("https"); + url.setHost(endpoint); + url.setPath(msg.resourcePath()); + + auto params = msg.queryParameters(); + std::map queryParams; + for (const auto &p : params) { + if (!p.second.empty()) + queryParams[p.first] = p.second; + } + + queryParams["AccessKeyId"] = credentials.accessKeyId(); + queryParams["Format"] = "JSON"; + queryParams["RegionId"] = configuration().regionId(); + queryParams["SecurityToken"] = credentials.sessionToken(); + queryParams["SignatureMethod"] = signer_->name(); + queryParams["SignatureNonce"] = GenerateUuid(); + queryParams["SignatureVersion"] = signer_->version(); + std::time_t t = std::time(nullptr); + std::stringstream ss; +#if defined(__GNUG__) && __GNUC__ < 5 + char tmbuff[26]; + strftime(tmbuff, 26, "%FT%TZ", std::gmtime(&t)); + ss << tmbuff; +#else + ss << std::put_time(std::gmtime(&t), "%FT%TZ"); +#endif + queryParams["Timestamp"] = ss.str(); + queryParams["Version"] = msg.version(); + + std::stringstream plaintext; + plaintext << HttpMethodToString(method) + << "&" + << UrlEncode(url.path()) + << "&" + << UrlEncode(canonicalizedQuery(queryParams)); + + queryParams["Signature"] = signer_->generate(plaintext.str(), credentials.accessKeySecret() + "&"); + + std::stringstream queryString; + for (const auto &p : queryParams) + queryString << "&" << p.first << "=" << UrlEncode(p.second); + url.setQuery(queryString.str().substr(1)); + + HttpRequest request(url); + request.setMethod(method); + request.setHeader("Host", url.host()); + request.setHeader("x-sdk-client", std::string("CPP/").append(ALIBABACLOUD_VERSION_STR)); + return request; +} \ No newline at end of file diff --git a/core/src/CommonRequest.cc b/core/src/CommonRequest.cc new file mode 100644 index 000000000..1e1c6bb97 --- /dev/null +++ b/core/src/CommonRequest.cc @@ -0,0 +1,93 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include + +using namespace AlibabaCloud; + +CommonRequest::CommonRequest(): + ServiceRequest("",""), + domain_(), + queryParams_(), + httpMethod_(HttpRequest::Get), + uriPattern_("rpc") +{ +} + +CommonRequest::~CommonRequest() +{} + +std::string CommonRequest::domain()const +{ + return domain_; +} + +void CommonRequest::setDomain(const std::string &domain) +{ + domain_ = domain; +} + +std::string CommonRequest::uriPattern() const +{ + return uriPattern_; +} + +void CommonRequest::setUriPattern(const std::string & uriPattern) +{ + uriPattern_ = uriPattern; +} + +void CommonRequest::setHttpMethod(HttpRequest::Method method) +{ + httpMethod_ = method; +} + +HttpRequest::Method CommonRequest::httpMethod() const +{ + return httpMethod_; +} + +CommonRequest::ParameterValueType CommonRequest::queryParameter(const ParameterNameType &name)const +{ + return queryParams_.at(name); +} + + +CommonRequest::ParameterCollection CommonRequest::queryParameters() const +{ + return queryParams_; +} + +void CommonRequest::setQueryParameter(const ParameterNameType &name, const ParameterValueType &value) +{ + queryParams_[name] = value; +} + +CommonRequest::ParameterValueType CommonRequest::headerParameter(const ParameterNameType &name)const +{ + return headerParams_.at(name); +} + +CommonRequest::ParameterCollection CommonRequest::headerParameters() const +{ + return headerParams_; +} + +void CommonRequest::setHeaderParameter(const ParameterNameType &name, const ParameterValueType &value) +{ + headerParams_[name] = value; +} + \ No newline at end of file diff --git a/core/src/CommonResponse.cc b/core/src/CommonResponse.cc new file mode 100644 index 000000000..7b032fe07 --- /dev/null +++ b/core/src/CommonResponse.cc @@ -0,0 +1,38 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include + +using namespace AlibabaCloud; + +CommonResponse::CommonResponse() : + payload_() +{ +} + +CommonResponse::CommonResponse(const std::string &payload) : + payload_(payload) +{ +} + +CommonResponse::~CommonResponse() +{ +} + +std::string CommonResponse::payload() const +{ + return payload_; +} diff --git a/core/src/Config.h.in b/core/src/Config.h.in new file mode 100644 index 000000000..5ef19abb8 --- /dev/null +++ b/core/src/Config.h.in @@ -0,0 +1,25 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_CONFIG_H_ +#define ALIBABACLOUD_CORE_CONFIG_H_ + +// version = (major << 16) + (minor << 8) + patch +#define ALIBABACLOUD_VERSION ((@PROJECT_VERSION_MAJOR@ << 16) + (@PROJECT_VERSION_MINOR@ << 8) + @PROJECT_VERSION_PATCH@) + +#define ALIBABACLOUD_VERSION_STR "@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@" + +#endif // !ALIBABACLOUD_CORE_CONFIG_H_ diff --git a/core/src/CoreClient.cc b/core/src/CoreClient.cc new file mode 100644 index 000000000..4e3e986b4 --- /dev/null +++ b/core/src/CoreClient.cc @@ -0,0 +1,82 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "CurlHttpClient.h" +#include "Executor.h" + +/*! + * \class AlibabaCloud::CoreClient CoreClient.h + * + */ + +using namespace AlibabaCloud; + +CoreClient::CoreClient(const ClientConfiguration &configuration) : + configuration_(configuration), + httpClient_(new CurlHttpClient) +{ + httpClient_->setProxy(configuration.proxy()); +} + +CoreClient::~CoreClient() +{ + delete httpClient_; +} + +ClientConfiguration CoreClient::configuration()const +{ + return configuration_; +} + +void CoreClient::asyncExecute(Runnable * r)const +{ + Executor::instance()->execute(r); +} + +HttpClient::HttpResponseOutcome CoreClient::AttemptRequest(const std::string & endpoint, const ServiceRequest & request, HttpRequest::Method method) const +{ + auto r = buildHttpRequest(endpoint, request, method); + auto outcome = httpClient_->makeRequest(r); + if (!outcome.isSuccess()) + return outcome; + if(hasResponseError(outcome.result())) + return HttpClient::HttpResponseOutcome(buildCoreError(outcome.result())); + else + return outcome; +} + +Error CoreClient::buildCoreError(const HttpResponse &response)const +{ + Json::Reader reader; + Json::Value value; + if (!reader.parse(std::string(response.body(), response.bodySize()), value)) + return Error("InvalidResponse", ""); + + Error error; + error.setErrorCode(value["Code"].asString()); + error.setErrorMessage(value["Message"].asString()); + error.setHost(value["HostId"].asString()); + error.setRequestId(value["RequestId"].asString()); + return error; +} + +bool CoreClient::hasResponseError(const HttpResponse &response)const +{ + return response.statusCode() < 200 || response.statusCode() > 299; +} \ No newline at end of file diff --git a/core/src/Credentials.cc b/core/src/Credentials.cc new file mode 100755 index 000000000..fda392b5d --- /dev/null +++ b/core/src/Credentials.cc @@ -0,0 +1,62 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +Credentials::Credentials(const std::string &accessKeyId, + const std::string &accessKeySecret, + const std::string &sessionToken) : + accessKeyId_(accessKeyId), + accessKeySecret_(accessKeySecret), + sessionToken_(sessionToken) +{ +} + +Credentials::~Credentials() +{ +} + +std::string Credentials::accessKeyId () const +{ + return accessKeyId_; +} + +std::string Credentials::accessKeySecret () const +{ + return accessKeySecret_; +} + +void Credentials::setAccessKeyId(const std::string &accessKeyId) +{ + accessKeyId_ = accessKeyId; +} + +void Credentials::setAccessKeySecret(const std::string &accessKeySecret) +{ + accessKeySecret_ = accessKeySecret; +} + +void Credentials::setSessionToken (const std::string &sessionToken) +{ + sessionToken_ = sessionToken; +} + +std::string Credentials::sessionToken () const +{ + return sessionToken_; +} diff --git a/core/src/CredentialsProvider.cc b/core/src/CredentialsProvider.cc new file mode 100644 index 000000000..886ca4c11 --- /dev/null +++ b/core/src/CredentialsProvider.cc @@ -0,0 +1,17 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include diff --git a/core/src/CurlHttpClient.cc b/core/src/CurlHttpClient.cc new file mode 100644 index 000000000..cafd7ae6b --- /dev/null +++ b/core/src/CurlHttpClient.cc @@ -0,0 +1,134 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CurlHttpClient.h" +#include +#include +#include + +using namespace AlibabaCloud; + +namespace +{ + size_t recvBody(char *ptr, size_t size, size_t nmemb, void *userdata) + { + HttpResponse *response = static_cast(userdata); + response->setBody(ptr, nmemb); + return nmemb * size; + } + + size_t recvHeaders(char *buffer, size_t size, size_t nitems, void *userdata) + { + HttpResponse *response = static_cast(userdata); + std::string line(buffer); + auto pos = line.find(':'); + if (pos != line.npos) + { + std::string name = line.substr(0, pos); + std::string value = line.substr(pos + 2); + size_t p = 0; + if ((p = value.rfind('\r')) != value.npos) + value[p] = '\0'; + response->setHeader(name, value); + } + return nitems * size; + } + + void setCUrlProxy(CURL *curlHandle, const NetworkProxy &proxy) + { + if (proxy.type() == NetworkProxy::Type::None) + return; + + long type; + switch (proxy.type()) + { + case NetworkProxy::Type::Socks5: + type = CURLPROXY_SOCKS5; + break; + case NetworkProxy::Type::Http: + default: + type = CURLPROXY_HTTP; + break; + } + curl_easy_setopt(curlHandle, CURLOPT_PROXYTYPE, type); + + std::ostringstream out; + out << proxy.hostName() << ":" << proxy.port(); + curl_easy_setopt(curlHandle, CURLOPT_PROXY, out.str().c_str()); + + if (!proxy.user().empty()) { + out.clear(); + out << proxy.user() << ":" << proxy.password(); + curl_easy_setopt(curlHandle, CURLOPT_PROXYUSERPWD, out.str().c_str()); + } + } +} + +CurlHttpClient::CurlHttpClient() : + HttpClient(), + curlHandle_(curl_easy_init()) +{ +} + +CurlHttpClient::~CurlHttpClient() +{ + curl_easy_cleanup(curlHandle_); +} + +HttpClient::HttpResponseOutcome CurlHttpClient::makeRequest(const HttpRequest &request) +{ + curl_easy_reset(curlHandle_); + HttpResponse response(request); + std::string url = request.url().toString(); + switch (request.method()) + { + case HttpRequest::Method::Get: + break; + case HttpRequest::Method::Put: + curl_easy_setopt(curlHandle_, CURLOPT_UPLOAD, 1L); + break; + default: + break; + } + curl_easy_setopt(curlHandle_, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curlHandle_, CURLOPT_SSL_VERIFYPEER, 1L); + curl_easy_setopt(curlHandle_, CURLOPT_SSL_VERIFYHOST, 2L); + curl_easy_setopt(curlHandle_, CURLOPT_HEADERDATA, &response); + curl_easy_setopt(curlHandle_, CURLOPT_HEADERFUNCTION, recvHeaders); + + curl_slist *list = nullptr; + auto headers = request.headers(); + for (const auto &p : headers) + { + std::string str = p.first; + str.append(": ").append(p.second); + list = curl_slist_append(list, str.c_str()); + } + curl_easy_setopt(curlHandle_, CURLOPT_HTTPHEADER, list); + curl_easy_setopt(curlHandle_, CURLOPT_WRITEDATA, &response); + curl_easy_setopt(curlHandle_, CURLOPT_WRITEFUNCTION, recvBody); + setCUrlProxy(curlHandle_, proxy()); + + CURLcode res = curl_easy_perform(curlHandle_); + if (res == CURLE_OK) { + long response_code; + curl_easy_getinfo(curlHandle_, CURLINFO_RESPONSE_CODE, &response_code); + response.setStatusCode(response_code); + return HttpResponseOutcome(response); + } + + return HttpResponseOutcome(Error("NetworkError", "")); +} diff --git a/core/src/CurlHttpClient.h b/core/src/CurlHttpClient.h new file mode 100644 index 000000000..4480419b7 --- /dev/null +++ b/core/src/CurlHttpClient.h @@ -0,0 +1,37 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_CURLHTTPCLIENT_H_ +#define ALIBABACLOUD_CORE_CURLHTTPCLIENT_H_ + +#include +#include + +namespace AlibabaCloud +{ + class CurlHttpClient : public HttpClient + { + public: + CurlHttpClient(); + ~CurlHttpClient(); + + virtual HttpResponseOutcome makeRequest(const HttpRequest &request) override; + private: + CURL *curlHandle_; + }; +} + +#endif // !ALIBABACLOUD_CORE_CURLHTTPCLIENT_H_ diff --git a/core/src/EcsInstanceProfileConfigLoader.cc b/core/src/EcsInstanceProfileConfigLoader.cc new file mode 100644 index 000000000..895dfd7df --- /dev/null +++ b/core/src/EcsInstanceProfileConfigLoader.cc @@ -0,0 +1,41 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "EcsMetadataClient.h" + +using namespace AlibabaCloud; + +EcsInstanceProfileConfigLoader::EcsInstanceProfileConfigLoader() : + metadataClient_(std::make_shared()) +{ +} + +EcsInstanceProfileConfigLoader::EcsInstanceProfileConfigLoader(const std::shared_ptr& client) : + metadataClient_(client) +{ +} + +EcsInstanceProfileConfigLoader::~EcsInstanceProfileConfigLoader() +{ +} + +bool EcsInstanceProfileConfigLoader::loadInternal() +{ + //TODO(fenglc): load form remote server + + return false; +} \ No newline at end of file diff --git a/core/src/EcsMetadataClient.cc b/core/src/EcsMetadataClient.cc new file mode 100644 index 000000000..7a613362b --- /dev/null +++ b/core/src/EcsMetadataClient.cc @@ -0,0 +1,27 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "EcsMetadataClient.h" + +using namespace AlibabaCloud; + +EcsMetadataClient::EcsMetadataClient() +{ +} + +EcsMetadataClient::~EcsMetadataClient() +{ +} \ No newline at end of file diff --git a/core/src/EcsMetadataClient.h b/core/src/EcsMetadataClient.h new file mode 100644 index 000000000..3abab443d --- /dev/null +++ b/core/src/EcsMetadataClient.h @@ -0,0 +1,32 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_ECSMETADATACLIENT_H_ +#define ALIBABACLOUD_CORE_ECSMETADATACLIENT_H_ + +namespace AlibabaCloud +{ + class EcsMetadataClient + { + public: + EcsMetadataClient(); + ~EcsMetadataClient(); + + private: + + }; +} +#endif // !ALIBABACLOUD_CORE_ECSMETADATACLIENT_H_ diff --git a/core/src/EndpointProvider.cc b/core/src/EndpointProvider.cc new file mode 100644 index 000000000..deb419c5e --- /dev/null +++ b/core/src/EndpointProvider.cc @@ -0,0 +1,77 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include + +using namespace AlibabaCloud; +using namespace AlibabaCloud::Location; + +EndpointProvider::EndpointProvider(const std::shared_ptr& locationClient, const std::string regionId, const std::string serviceCode, int durationSeconds) : + locationClient_(locationClient), + regionId_(regionId), + serviceCode_(serviceCode), + durationSeconds_(durationSeconds), + cachedMutex_(), + cachedEndpoint_(), + expiry_() +{ +} + +EndpointProvider::~EndpointProvider() +{ +} + +bool EndpointProvider::checkExpiry()const +{ + auto now = std::chrono::system_clock::now(); + auto diff = std::chrono::duration_cast(now - expiry_).count(); + + return (diff > 0 - 60); +} + +std::string EndpointProvider::getEndpoint() +{ + loadEndpoint(); + std::lock_guard locker(cachedMutex_); + return cachedEndpoint_; +} + +void EndpointProvider::loadEndpoint() +{ + if (checkExpiry()) + { + std::lock_guard locker(cachedMutex_); + if (checkExpiry()) + { + Model::DescribeEndpointsRequest request; + request.setId(regionId_); + request.setServiceCode(serviceCode_); + request.setType("openAPI"); + auto outcome = locationClient_->describeEndpoints(request); + if (outcome.isSuccess()) + { + auto all = outcome.result().endpoints(); + if (all.size() > 0) + cachedEndpoint_ = all.front().endpoint; + + std::time_t t = std::time(nullptr) + durationSeconds_; + expiry_ = std::chrono::system_clock::from_time_t(t); + } + } + } +} \ No newline at end of file diff --git a/core/src/Error.cc b/core/src/Error.cc new file mode 100644 index 000000000..cb7032652 --- /dev/null +++ b/core/src/Error.cc @@ -0,0 +1,36 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +Error::Error(std::string code, const std::string message) : + errorCode_(code), + message_(message), + host_(), + requestId_() +{ +} + +std::string Error::errorCode()const { return errorCode_; } +std::string Error::errorMessage() const { return message_; } +std::string Error::host() const { return host_; } +std::string Error::requestId() const { return requestId_; } +void Error::setErrorCode(const std::string &code) { errorCode_ = code; } +void Error::setErrorMessage(const std::string& message) { message_ = message; } +void Error::setHost(const std::string& host) { host_ = host; } +void Error::setRequestId(const std::string& request) { requestId_ = request; } diff --git a/core/src/Executor.cc b/core/src/Executor.cc new file mode 100644 index 000000000..5f2633e33 --- /dev/null +++ b/core/src/Executor.cc @@ -0,0 +1,124 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "Executor.h" +#include + +using namespace AlibabaCloud; + +Executor *Executor::self_ = nullptr; + +Executor::Executor() : + cvMutex_(), + shutdown_(true), + tasksQueue_(), + tasksQueueMutex_(), + thread_() +{ + self_ = this; +} + +Executor::~Executor() +{ + self_ = nullptr; + shutdown(); +} + +Executor * Executor::instance() +{ + return self_; +} + +bool Executor::start() +{ + if (!isShutdown()) + return true; + + auto threadMain = [this]() + { + while (!shutdown_) + { + while (!tasksQueue_.empty()) + { + Runnable *task = nullptr; + { + std::lock_guard lock(tasksQueueMutex_); + if (!tasksQueue_.empty()) + { + task = tasksQueue_.front(); + tasksQueue_.pop(); + } + } + + if (task) { + task->run(); + delete task; + } + } + + if (!shutdown_) { + std::unique_lock lk(cvMutex_); + cv_.wait(lk); + } + } + }; + + shutdown_ = false; + thread_ = std::thread(threadMain); + return true; +} + +bool Executor::isShutdown()const +{ + return shutdown_; +} + +void Executor::execute(Runnable* task) +{ + if (isShutdown()) + return; + + std::lock_guard locker(tasksQueueMutex_); + tasksQueue_.push(task); + wakeUp(); +} + +void Executor::wakeUp() +{ + std::unique_lock lk(cvMutex_); + cv_.notify_one(); +} + +void Executor::shutdown() +{ + if (isShutdown()) + return; + + { + std::lock_guard locker(tasksQueueMutex_); + while (tasksQueue_.size() > 0) { + auto task = tasksQueue_.front(); + delete task; + tasksQueue_.pop(); + } + } + + shutdown_ = true; + wakeUp(); + + if (thread_.joinable()) + thread_.join(); +} \ No newline at end of file diff --git a/core/src/Executor.h b/core/src/Executor.h new file mode 100644 index 000000000..5b717ba34 --- /dev/null +++ b/core/src/Executor.h @@ -0,0 +1,53 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef ALIBABACLOUD_CORE_EXECUTOR_H_ +#define ALIBABACLOUD_CORE_EXECUTOR_H_ + +#include +#include +#include +#include +#include +#include + +namespace AlibabaCloud +{ + class Runnable; + class Executor + { + public: + Executor(); + ~Executor(); + + static Executor * instance(); + void execute(Runnable* task); + bool isShutdown()const; + bool start(); + void shutdown(); + void wakeUp(); + private: + static Executor *self_; + std::atomic shutdown_; + std::queue tasksQueue_; + std::mutex tasksQueueMutex_; + std::thread thread_; + std::condition_variable cv_; + std::mutex cvMutex_; + }; +} + +#endif // !ALIBABACLOUD_CORE_EXECUTOR_H_ diff --git a/core/src/HmacSha1Signer.cc b/core/src/HmacSha1Signer.cc new file mode 100644 index 000000000..351f67606 --- /dev/null +++ b/core/src/HmacSha1Signer.cc @@ -0,0 +1,99 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#ifdef _WIN32 +#include +#include +#else +#include +#endif + +using namespace AlibabaCloud; + +HmacSha1Signer::HmacSha1Signer() : + Signer(HmacSha1, "HMAC-SHA1", "1.0") +{ +} + +HmacSha1Signer::~HmacSha1Signer() +{ +} + +std::string HmacSha1Signer::generate(const std::string & src, const std::string & secret) const +{ + if (src.empty()) + return std::string(); + +#ifdef _WIN32 + typedef struct _my_blob { + BLOBHEADER hdr; + DWORD dwKeySize; + BYTE rgbKeyData[]; + }my_blob; + + DWORD kbLen = sizeof(my_blob) + secret.size(); + my_blob * kb = (my_blob *)LocalAlloc(LPTR, kbLen); + kb->hdr.bType = PLAINTEXTKEYBLOB; + kb->hdr.bVersion = CUR_BLOB_VERSION; + kb->hdr.reserved = 0; + kb->hdr.aiKeyAlg = CALG_RC2; + kb->dwKeySize = secret.size(); + memcpy(&kb->rgbKeyData, secret.c_str(), secret.size()); + + HCRYPTPROV hProv = 0; + HCRYPTKEY hKey = 0; + HCRYPTHASH hHmacHash = 0; + BYTE pbHash[32]; + DWORD dwDataLen = 32; + HMAC_INFO HmacInfo; + ZeroMemory(&HmacInfo, sizeof(HmacInfo)); + HmacInfo.HashAlgid = CALG_SHA1; + + CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET); + CryptImportKey(hProv, (BYTE*)kb, kbLen, 0, CRYPT_IPSEC_HMAC_KEY, &hKey); + CryptCreateHash(hProv, CALG_HMAC, hKey, 0, &hHmacHash); + CryptSetHashParam(hHmacHash, HP_HMAC_INFO, (BYTE*)&HmacInfo, 0); + CryptHashData(hHmacHash, (BYTE*)(src.c_str()), src.size(), 0); + CryptGetHashParam(hHmacHash, HP_HASHVAL, pbHash, &dwDataLen, 0); + + LocalFree(kb); + CryptDestroyHash(hHmacHash); + CryptDestroyKey(hKey); + CryptReleaseContext(hProv, 0); + + DWORD dlen = 0; + CryptBinaryToString(pbHash, dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &dlen); + char* dest = new char[dlen]; + CryptBinaryToString(pbHash, dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &dlen); + + std::string ret = std::string(dest, dlen); + delete dest; + return ret; +#else + unsigned char md[EVP_MAX_BLOCK_LENGTH]; + unsigned int mdLen = EVP_MAX_BLOCK_LENGTH; + + if (HMAC(EVP_sha1(), secret.c_str(), secret.size(), + reinterpret_cast(src.c_str()), src.size(), + md, &mdLen) == nullptr) + return std::string(); + + char encodedData[100]; + EVP_EncodeBlock(reinterpret_cast(encodedData), md, mdLen); + return encodedData; +#endif +} \ No newline at end of file diff --git a/core/src/HttpClient.cc b/core/src/HttpClient.cc new file mode 100644 index 000000000..5e9311a8f --- /dev/null +++ b/core/src/HttpClient.cc @@ -0,0 +1,41 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +using namespace AlibabaCloud; + +HttpClient::HttpClient() : + proxy_() +{ +} + +HttpClient::~HttpClient() +{ +} + +NetworkProxy HttpClient::proxy()const +{ + return proxy_; +} + +void HttpClient::setProxy(const NetworkProxy &proxy) +{ + proxy_ = proxy; +} \ No newline at end of file diff --git a/core/src/HttpMessage.cc b/core/src/HttpMessage.cc new file mode 100644 index 000000000..6ce3064fa --- /dev/null +++ b/core/src/HttpMessage.cc @@ -0,0 +1,173 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +using namespace AlibabaCloud; + +namespace +{ +#if defined(WIN32) && defined(_MSC_VER) +# define strcasecmp _stricmp +# define strncasecmp _strnicmp +#else +# include +#endif + + std::string KnownHeaderMapper[] + { + "Accept", + "Accept-Charset", + "Accept-Encoding", + "Accept-Language", + "Authorization", + "Connection", + "Content-Length", + "Content-MD5", + "Content-Type", + "Date", + "Host", + "Server", + "User-Agent" + }; +} + +HttpMessage::HttpMessage() : + body_(nullptr), + bodySize_(0), + headers_() +{ +} + +HttpMessage::HttpMessage(const HttpMessage &other) : + body_(nullptr), + bodySize_(other.bodySize_), + headers_(other.headers_) +{ + setBody(other.body_, other.bodySize_); +} + +HttpMessage::HttpMessage(HttpMessage &&other) +{ + *this = std::move(other); +} + +HttpMessage& HttpMessage::operator=(const HttpMessage &other) +{ + if (this != &other) { + body_ = nullptr; + bodySize_ = 0; + headers_ = other.headers_; + setBody(other.body_, other.bodySize_); + } + return *this; +} + +HttpMessage& HttpMessage::operator=(HttpMessage &&other) +{ + if (this != &other) + *this = std::move(other); + return *this; +} + +void HttpMessage::addHeader(const HeaderNameType & name, const HeaderValueType & value) +{ + setHeader(name, value); +} + +void HttpMessage::addHeader(KnownHeader header, const HeaderValueType & value) +{ + setHeader(header, value); +} + +HttpMessage::HeaderValueType HttpMessage::header(const HeaderNameType & name) const +{ + auto it = headers_.find(name); + if (it != headers_.end()) + return it->second; + else + return std::string(); +} + +HttpMessage::HeaderCollection HttpMessage::headers() const +{ + return headers_; +} + +void HttpMessage::removeHeader(const HeaderNameType & name) +{ + headers_.erase(name); +} + +void HttpMessage::removeHeader(KnownHeader header) +{ + removeHeader(KnownHeaderMapper[header]); +} + +void HttpMessage::setHeader(const HeaderNameType & name, const HeaderValueType & value) +{ + headers_[name] = value; +} + +void HttpMessage::setHeader(KnownHeader header, const std::string & value) +{ + setHeader(KnownHeaderMapper[header], value); +} + +HttpMessage::~HttpMessage() +{ + setBody(nullptr, 0); +} + +const char* HttpMessage::body()const +{ + return body_; +} + +size_t HttpMessage::bodySize()const +{ + return bodySize_; +} + +bool HttpMessage::hasBody() const +{ + return (bodySize_ != 0); +} + +HttpMessage::HeaderValueType HttpMessage::header(KnownHeader header)const +{ + return this->header(KnownHeaderMapper[header]); +} + +void HttpMessage::setBody(const char *data, size_t size) +{ + if (body_) + delete body_; + body_ = nullptr; + bodySize_ = 0; + if (size) { + bodySize_ = size; + body_ = new char[size]; + std::copy(data, data + size, body_); + } +} + +bool HttpMessage::nocaseLess::operator()(const std::string & s1, + const std::string & s2) const +{ + return strcasecmp(s1.c_str(), s2.c_str()) < 0; +} diff --git a/core/src/HttpRequest.cc b/core/src/HttpRequest.cc new file mode 100644 index 000000000..6bd42084c --- /dev/null +++ b/core/src/HttpRequest.cc @@ -0,0 +1,51 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +HttpRequest::HttpRequest(const Url &url, Method method) : + HttpMessage(), + url_(url), + method_(method) +{ +} + +HttpRequest::~HttpRequest() +{ +} + +HttpRequest::Method HttpRequest::method() const +{ + return method_; +} + + +void HttpRequest::setMethod(Method method) +{ + method_ = method; +} + +void HttpRequest::setUrl(const Url &url) +{ + url_ = url; +} + +Url HttpRequest::url()const +{ + return url_; +} diff --git a/core/src/HttpResponse.cc b/core/src/HttpResponse.cc new file mode 100644 index 000000000..753b7ec2f --- /dev/null +++ b/core/src/HttpResponse.cc @@ -0,0 +1,57 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +namespace +{ + #define INVALID_STATUS_CODE -1 +} + +using namespace AlibabaCloud; + +HttpResponse::HttpResponse() : + HttpMessage(), + request_(), + statusCode_(INVALID_STATUS_CODE) +{ +} + +HttpResponse::HttpResponse(const HttpRequest & request) : + HttpMessage(), + request_(request), + statusCode_(INVALID_STATUS_CODE) +{ +} + +HttpResponse::~HttpResponse() +{ +} + +HttpRequest HttpResponse::request() const +{ + return request_; +} + +void HttpResponse::setStatusCode(int code) +{ + statusCode_ = code; +} + +int HttpResponse::statusCode() const +{ + return statusCode_; +} diff --git a/core/src/InstanceProfileConfigLoader.cc b/core/src/InstanceProfileConfigLoader.cc new file mode 100644 index 000000000..8eefeb158 --- /dev/null +++ b/core/src/InstanceProfileConfigLoader.cc @@ -0,0 +1,65 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +InstanceProfileConfigLoader::InstanceProfileConfigLoader() +{ +} + +InstanceProfileConfigLoader::~InstanceProfileConfigLoader() +{ +} + +std::map InstanceProfileConfigLoader::allProfiles() const +{ + return profiles_; +} + +std::chrono::system_clock::time_point InstanceProfileConfigLoader::lastLoadTime() const +{ + return lastLoadTime_; +} + +bool InstanceProfileConfigLoader::persistInternal(const std::map&) +{ + return false; +} + +bool InstanceProfileConfigLoader::load() +{ + if (loadInternal()) + { + lastLoadTime_ = std::chrono::system_clock::now(); + return true; + } + + return false; +} + +bool InstanceProfileConfigLoader::persistProfiles(const std::map& profiles) +{ + if (persistInternal(profiles)) + { + profiles_ = profiles; + lastLoadTime_ = std::chrono::system_clock::now(); + return true; + } + + return false; +} diff --git a/core/src/InstanceProfileCredentials.cc b/core/src/InstanceProfileCredentials.cc new file mode 100644 index 000000000..5542d7edf --- /dev/null +++ b/core/src/InstanceProfileCredentials.cc @@ -0,0 +1,28 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +InstanceProfileCredentials::InstanceProfileCredentials() : + BasicSessionCredentials(Credentials::InstanceProfile) +{ +} + +InstanceProfileCredentials::~InstanceProfileCredentials() +{ +} \ No newline at end of file diff --git a/core/src/InstanceProfileCredentialsProvider.cc b/core/src/InstanceProfileCredentialsProvider.cc new file mode 100644 index 000000000..e857c68a1 --- /dev/null +++ b/core/src/InstanceProfileCredentialsProvider.cc @@ -0,0 +1,78 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include + +using namespace AlibabaCloud; + +namespace +{ + const char* const INSTANCE_PROFILE_KEY = "InstanceProfile"; +} + +InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(size_t refreshRateMs) : + metadataConfigLoader_(std::make_shared()), + loadFrequencyMs_(refreshRateMs), + lastLoaded_() +{ +} + +InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std::shared_ptr& loader, size_t refreshRateMs) : + metadataConfigLoader_(loader), + loadFrequencyMs_(refreshRateMs), + lastLoaded_() +{ +} + +Credentials InstanceProfileCredentialsProvider::getCredentials() +{ + refreshIfExpired(); + + auto profileIter = metadataConfigLoader_->allProfiles().find(INSTANCE_PROFILE_KEY); + if (profileIter != metadataConfigLoader_->allProfiles().end()) + { + return profileIter->second.credentials(); + } + + return Credentials("",""); +} + +bool InstanceProfileCredentialsProvider::isTimeToRefresh(long reloadFrequency) +{ + auto now = std::chrono::system_clock::now(); + auto diff = std::chrono::duration_cast(now - lastLoaded_).count(); + + if(diff > reloadFrequency) + { + lastLoaded_ = now; + return true; + } + + return false; +} + +void InstanceProfileCredentialsProvider::refreshIfExpired() +{ + std::lock_guard locker(m_reloadMutex); + if (isTimeToRefresh(loadFrequencyMs_)) + metadataConfigLoader_->load(); +} diff --git a/core/src/NetworkProxy.cc b/core/src/NetworkProxy.cc new file mode 100644 index 000000000..9f1167bb8 --- /dev/null +++ b/core/src/NetworkProxy.cc @@ -0,0 +1,86 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +NetworkProxy::NetworkProxy(Type type, + const std::string &hostName, + uint16_t port, + const std::string &user, + const std::string &password) +: hostName_(hostName), + password_(password), + port_(port), + type_(type), + user_(user) +{ +} + +NetworkProxy::~NetworkProxy() +{ +} + +std::string NetworkProxy::hostName() const +{ + return hostName_; +} + +std::string NetworkProxy::password() const +{ + return password_; +} + +uint16_t NetworkProxy::port() const +{ + return port_; +} + +void NetworkProxy::setHostName(const std::string &hostName) +{ + hostName_ = hostName; +} + +void NetworkProxy::setPassword(const std::string &password) +{ + password_ = password; +} + +void NetworkProxy::setPort(uint16_t port) +{ + port_ = port; +} + +void NetworkProxy::setType(NetworkProxy::Type type) +{ + type_ = type; +} + +void NetworkProxy::setUser(const std::string &user) +{ + user_ = user; +} + +NetworkProxy::Type NetworkProxy::type() const +{ + return type_; +} + +std::string NetworkProxy::user() const +{ + return user_; +} diff --git a/core/src/Outcome.cc b/core/src/Outcome.cc new file mode 100644 index 000000000..b612fd59a --- /dev/null +++ b/core/src/Outcome.cc @@ -0,0 +1,17 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include \ No newline at end of file diff --git a/core/src/Profile.cc b/core/src/Profile.cc new file mode 100644 index 000000000..8b8aaeb5a --- /dev/null +++ b/core/src/Profile.cc @@ -0,0 +1,61 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +std::string Profile::name() const +{ return name_; } + +void Profile::setName(const std::string& value) +{ name_ = value; } + +Credentials Profile::credentials() const +{ return credentials_; } + +void Profile::setCredentials(const Credentials& value) +{ credentials_ = value; } + +std::string Profile::region() const +{ return region_; } + +void Profile::setRegion(const std::string& value) +{ region_ = value; } + +std::string Profile::roleArn() const +{ return roleArn_; } + +void Profile::setRoleArn(const std::string& value) +{ roleArn_ = value; } + +std::string Profile::sourceProfile() const +{ return sourceProfile_; } + +void Profile::setSourceProfile(const std::string& value) +{ sourceProfile_ = value; } + +void Profile::setAllKeyValPairs(const std::map& map) +{ + allKeyValPairs_ = map; +} + +std::string Profile::value(const std::string& key) +{ + auto iter = allKeyValPairs_.find(key); + if (iter == allKeyValPairs_.end()) return ""; + return iter->second; +} \ No newline at end of file diff --git a/core/src/RoaServiceClient.cc b/core/src/RoaServiceClient.cc new file mode 100644 index 000000000..9f6d03197 --- /dev/null +++ b/core/src/RoaServiceClient.cc @@ -0,0 +1,179 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +//#include +#include "Utils.h" + +using namespace AlibabaCloud; + +RoaServiceClient::RoaServiceClient(const std::shared_ptr &credentialsProvider, + const ClientConfiguration &configuration, + const std::shared_ptr &signer) : + CoreClient(configuration), + credentialsProvider_(credentialsProvider), + signer_(signer) +{ +} + +RoaServiceClient::~RoaServiceClient() +{ +} + +std::string RoaServiceClient::canonicalizedResource(const std::string &path, std::map ¶ms)const +{ + if (params.empty()) + return path; + + std::stringstream ss; + for (const auto &p : params) + { + if (p.second.empty()) + ss << "&" << p.first; + else + ss << "&" << p.first << "=" << p.second; + } + + std::string str = path; + str.append("?").append(ss.str().substr(1)); + return str; +} + +std::string RoaServiceClient::canonicalizedHeaders(const HttpMessage::HeaderCollection &headers)const +{ + std::map materials; + for (const auto &p : headers) + { + std::string key = p.first; + std::transform(key.begin(), key.end(), key.begin(), ::tolower); + if (key.find("x-acs-") != 0) + continue; + + std::string value = p.second; + StringReplace(value, "\t", " "); + StringReplace(value, "\n", " "); + StringReplace(value, "\r", " "); + StringReplace(value, "\f", " "); + materials[key] = value; + } + + if (materials.empty()) + return std::string(); + std::stringstream ss; + for (const auto &p : materials) + ss << p.first << ":" << p.second << "\n"; + + return ss.str(); +} + +RoaServiceClient::JsonOutcome RoaServiceClient::makeRequest(const std::string &endpoint, const RoaServiceRequest &msg, HttpRequest::Method method)const +{ + auto outcome = AttemptRequest(endpoint, msg, method); + if (outcome.isSuccess()) + return JsonOutcome(std::string(outcome.result().body(), + outcome.result().bodySize())); + else + return JsonOutcome(outcome.error()); +} + +HttpRequest RoaServiceClient::buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const +{ + return buildHttpRequest(endpoint, dynamic_cast(msg), method); +} + +HttpRequest RoaServiceClient::buildHttpRequest(const std::string & endpoint, const RoaServiceRequest &msg, HttpRequest::Method method) const +{ + const Credentials credentials = credentialsProvider_->getCredentials(); + + Url url; + url.setScheme("https"); + url.setHost(endpoint); + url.setPath(msg.resourcePath()); + + auto params = msg.parameters(); + std::map queryParams; + for (const auto &p : params){ + if (!p.second.empty()) + queryParams[p.first] = p.second; + } + + if (!queryParams.empty()) { + std::stringstream queryString; + for (const auto &p : queryParams) + { + if (p.second.empty()) + queryString << "&" << p.first; + else + queryString << "&" << p.first << "=" << p.second; + } + url.setQuery(queryString.str().substr(1)); + } + + HttpRequest request(url); + request.setMethod(method); + request.setHeader("Accept", "application/json"); + if (msg.hasContent()) { + std::stringstream ss; + ss << msg.contentSize(); + request.setHeader("Content-Length", ss.str()); + request.setHeader("Content-Type", "application/octet-stream"); + request.setHeader("Content-MD5", ComputeContentMD5(msg.content(),msg.contentSize())); + } + + std::time_t t = std::time(nullptr); + std::stringstream date; +#if defined(__GNUG__) && __GNUC__ < 5 + char tmbuff[26]; + strftime(tmbuff, 26, "%a, %d %b %Y %T", std::gmtime(&t)); + date << tmbuff << " GMT"; +#else + date << std::put_time(std::gmtime(&t), "%a, %d %b %Y %T GMT"); +#endif + request.setHeader("Date", date.str()); + request.setHeader("Host", url.host()); + request.setHeader("x-sdk-client", std::string("CPP/").append(ALIBABACLOUD_VERSION_STR)); + request.setHeader("x-acs-region-id", configuration().regionId()); + request.setHeader("x-acs-security-token", credentials.sessionToken()); + request.setHeader("x-acs-signature-method", signer_->name()); + request.setHeader("x-acs-signature-nonce", GenerateUuid()); + request.setHeader("x-acs-signature-version", signer_->version()); + request.setHeader("x-acs-version", msg.version()); + + std::stringstream plaintext; + plaintext << HttpMethodToString(method) << "\n" + << request.header("Accept") << "\n" + << request.header("Content-MD5") << "\n" + << request.header("Content-Type") << "\n" + << request.header("Date") << "\n" + << canonicalizedHeaders(request.headers()); + if (!url.hasQuery()) + plaintext << url.path(); + else + plaintext << url.path() << "?" << url.query(); + + std::stringstream sign; + sign << "acs " + << credentials.accessKeyId() + << ":" + << signer_->generate(plaintext.str(), credentials.accessKeySecret()); + request.setHeader("Authorization", sign.str()); + + return request; +} diff --git a/core/src/RoaServiceRequest.cc b/core/src/RoaServiceRequest.cc new file mode 100644 index 000000000..94d70a015 --- /dev/null +++ b/core/src/RoaServiceRequest.cc @@ -0,0 +1,32 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include "Utils.h" + +using namespace AlibabaCloud; + +RoaServiceRequest::RoaServiceRequest(const std::string & product, const std::string & version) : + ServiceRequest(product, version) +{ +} + +RoaServiceRequest::~RoaServiceRequest() +{ +} diff --git a/core/src/RpcServiceClient.cc b/core/src/RpcServiceClient.cc new file mode 100644 index 000000000..56817149e --- /dev/null +++ b/core/src/RpcServiceClient.cc @@ -0,0 +1,130 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include "Utils.h" +//#include + +using namespace AlibabaCloud; + +RpcServiceClient::RpcServiceClient(const std::shared_ptr &credentialsProvider, + const ClientConfiguration &configuration, + const std::shared_ptr &signer) : + CoreClient(configuration), + credentialsProvider_(credentialsProvider), + signer_(signer) +{ +} + +RpcServiceClient::~RpcServiceClient() +{ +} + +std::string RpcServiceClient::canonicalizedQuery(const std::map& params) const +{ + if (params.empty()) + return std::string(); + + std::stringstream ss; + for (const auto &p : params) + { + std::string key = UrlEncode(p.first); + StringReplace(key, "+", "%20"); + StringReplace(key, "*", "%2A"); + StringReplace(key, "%7E", "~"); + std::string value = UrlEncode(p.second); + StringReplace(value, "+", "%20"); + StringReplace(value, "*", "%2A"); + StringReplace(value, "%7E", "~"); + ss << "&" << key << "=" << value; + } + return ss.str().substr(1); +} + +RpcServiceClient::JsonOutcome RpcServiceClient::makeRequest(const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method)const +{ + auto outcome = AttemptRequest(endpoint, msg, method); + if (outcome.isSuccess()) + return JsonOutcome(std::string(outcome.result().body(), + outcome.result().bodySize())); + else + return JsonOutcome(outcome.error()); +} + +HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method )const +{ + return buildHttpRequest(endpoint, dynamic_cast(msg), method); +} + +HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint, const RpcServiceRequest &msg, HttpRequest::Method method) const +{ + const Credentials credentials = credentialsProvider_->getCredentials(); + + Url url; + url.setScheme("https"); + url.setHost(endpoint); + url.setPath(msg.resourcePath()); + + auto params = msg.parameters(); + std::map queryParams; + for (const auto &p : params) { + if (!p.second.empty()) + queryParams[p.first] = p.second; + } + + queryParams["AccessKeyId"] = credentials.accessKeyId(); + queryParams["Format"] = "JSON"; + queryParams["RegionId"] = configuration().regionId(); + queryParams["SecurityToken"] = credentials.sessionToken(); + queryParams["SignatureMethod"] = signer_->name(); + queryParams["SignatureNonce"] = GenerateUuid(); + queryParams["SignatureVersion"] = signer_->version(); + std::time_t t = std::time(nullptr); + std::stringstream ss; +#if defined(__GNUG__) && __GNUC__ < 5 + char tmbuff[26]; + strftime(tmbuff, 26, "%FT%TZ" , std::gmtime(&t)); + ss << tmbuff; +#else + ss << std::put_time(std::gmtime(&t), "%FT%TZ"); +#endif + queryParams["Timestamp"] = ss.str(); + queryParams["Version"] = msg.version(); + + std::stringstream plaintext; + plaintext << HttpMethodToString(method) + << "&" + << UrlEncode(url.path()) + << "&" + << UrlEncode(canonicalizedQuery(queryParams)); + + queryParams["Signature"] = signer_->generate(plaintext.str(), credentials.accessKeySecret() + "&"); + + std::stringstream queryString; + for (const auto &p : queryParams) + queryString << "&" << p.first << "=" << UrlEncode(p.second); + url.setQuery(queryString.str().substr(1)); + + HttpRequest request(url); + request.setMethod(method); + request.setHeader("Host", url.host()); + request.setHeader("x-sdk-client", std::string("CPP/").append(ALIBABACLOUD_VERSION_STR)); + return request; +} \ No newline at end of file diff --git a/core/src/RpcServiceRequest.cc b/core/src/RpcServiceRequest.cc new file mode 100644 index 000000000..ba07bb0c2 --- /dev/null +++ b/core/src/RpcServiceRequest.cc @@ -0,0 +1,40 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +RpcServiceRequest::RpcServiceRequest(const std::string & product, const std::string & version, const std::string & action) : + ServiceRequest(product, version) +{ + setActionName(action); +} + +RpcServiceRequest::~RpcServiceRequest() +{ +} + +std::string RpcServiceRequest::actionName()const +{ + return parameter("Action"); +} + + +void RpcServiceRequest::setActionName(const std::string & name) +{ + setParameter("Action", name); +} \ No newline at end of file diff --git a/core/src/Runnable.cc b/core/src/Runnable.cc new file mode 100644 index 000000000..2db5777b6 --- /dev/null +++ b/core/src/Runnable.cc @@ -0,0 +1,29 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include + +using namespace AlibabaCloud; + +Runnable::Runnable(const std::function f) : + f_(f) +{ +} + +void Runnable::run() const +{ + f_(); +} \ No newline at end of file diff --git a/core/src/ServiceRequest.cc b/core/src/ServiceRequest.cc new file mode 100644 index 000000000..7a95a7a31 --- /dev/null +++ b/core/src/ServiceRequest.cc @@ -0,0 +1,157 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +ServiceRequest::ServiceRequest(const std::string &product, const std::string &version) : + content_(nullptr), + contentSize_(0), + params_(), + product_(product), + resourcePath_("/"), + version_(version) +{ +} + +ServiceRequest::ServiceRequest(const ServiceRequest &other) : + content_(nullptr), + contentSize_(other.contentSize_), + params_(other.params_), + product_(other.product_), + resourcePath_(other.resourcePath_), + version_(other.version_) +{ + setContent(other.content_, other.contentSize_); +} + +ServiceRequest::ServiceRequest(ServiceRequest &&other) +{ + *this = std::move(other); +} + +ServiceRequest& ServiceRequest::operator=(const ServiceRequest &other) +{ + if (this != &other) { + content_ = nullptr; + contentSize_ = 0; + params_ = other.params_; + setContent(other.content_, other.contentSize_); + } + return *this; +} + +ServiceRequest& ServiceRequest::operator=(ServiceRequest &&other) +{ + if (this != &other) + *this = std::move(other); + return *this; +} + +ServiceRequest::~ServiceRequest() +{ + if (content_) + delete content_; +} + +const char * ServiceRequest::content() const +{ + return content_; +} + +size_t ServiceRequest::contentSize() const +{ + return contentSize_; +} + +bool ServiceRequest::hasContent() const +{ + return (contentSize_ != 0); +} + +void ServiceRequest::setContent(const char * data, size_t size) +{ + if (content_) + delete content_; + content_ = nullptr; + contentSize_ = 0; + if (size) { + contentSize_ = size; + content_ = new char[size]; + std::copy(data, data + size, content_); + } +} + +void ServiceRequest::addParameter(const ParameterNameType & name, const ParameterValueType & value) +{ + setParameter(name, value); +} + +ServiceRequest::ParameterValueType ServiceRequest::parameter(const ParameterNameType &name)const +{ + return params_.at(name); +} + +ServiceRequest::ParameterCollection ServiceRequest::parameters() const +{ + return params_; +} + +void ServiceRequest::removeParameter(const ParameterNameType & name) +{ + params_.erase(name); +} + +void ServiceRequest::setParameter(const ParameterNameType &name, const ParameterValueType &value) +{ + params_[name] = value; +} + +void ServiceRequest::setParameters(const ParameterCollection & params) +{ + params_ = params; +} + +std::string ServiceRequest::version()const +{ + return version_; +} + +void ServiceRequest::setVersion(const std::string &version) +{ + version_ = version; +} + +std::string ServiceRequest::product() const +{ + return product_; +} + +void ServiceRequest::setProduct(const std::string & product) +{ + product_ = product; +} + +std::string ServiceRequest::resourcePath() const +{ + return resourcePath_; +} + +void ServiceRequest::setResourcePath(const std::string & path) +{ + resourcePath_ = path; +} \ No newline at end of file diff --git a/core/src/ServiceResult.cc b/core/src/ServiceResult.cc new file mode 100644 index 000000000..94f671ed4 --- /dev/null +++ b/core/src/ServiceResult.cc @@ -0,0 +1,38 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +ServiceResult::ServiceResult() : + requestId_() +{ +} + +ServiceResult::~ServiceResult() +{ +} + +std::string ServiceResult::requestId() const +{ + return requestId_; +} + +void ServiceResult::setRequestId(const std::string & requestId) +{ + requestId_ = requestId; +} diff --git a/core/src/Signer.cc b/core/src/Signer.cc new file mode 100644 index 000000000..e84a4c9c1 --- /dev/null +++ b/core/src/Signer.cc @@ -0,0 +1,45 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +Signer::Signer(Type type, const std::string & name, const std::string & version) : + type_(type), + name_(name), + version_(version) +{ +} + +Signer::~Signer() +{ +} + +std::string Signer::name() const +{ + return name_; +} + +Signer::Type Signer::type() const +{ + return type_; +} + +std::string Signer::version() const +{ + return version_; +} diff --git a/core/src/SimpleCredentialsProvider.cc b/core/src/SimpleCredentialsProvider.cc new file mode 100644 index 000000000..3915b6f85 --- /dev/null +++ b/core/src/SimpleCredentialsProvider.cc @@ -0,0 +1,40 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud; + +SimpleCredentialsProvider::SimpleCredentialsProvider(const Credentials &credentials): + CredentialsProvider(), + credentials_(credentials) +{ +} + +SimpleCredentialsProvider::SimpleCredentialsProvider(const std::string & accessKeyId, const std::string & accessKeySecret) : + CredentialsProvider(), + credentials_(accessKeyId, accessKeySecret) +{ +} + +SimpleCredentialsProvider::~SimpleCredentialsProvider() +{ +} + +Credentials SimpleCredentialsProvider::getCredentials() +{ + return credentials_; +} diff --git a/core/src/StsAssumeRoleCredentialsProvider.cc b/core/src/StsAssumeRoleCredentialsProvider.cc new file mode 100644 index 000000000..7aad00a47 --- /dev/null +++ b/core/src/StsAssumeRoleCredentialsProvider.cc @@ -0,0 +1,88 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include + +using namespace AlibabaCloud; +using namespace AlibabaCloud::Sts; + +StsAssumeRoleCredentialsProvider::StsAssumeRoleCredentialsProvider(const std::shared_ptr& stsClient, const std::string & roleArn, const std::string & roleSessionName, const std::string & policy, int durationSeconds) : + CredentialsProvider(), + stsClient_(stsClient), + roleArn_(roleArn), + roleSessionName_(roleSessionName), + policy_(policy), + durationSeconds_(durationSeconds), + cachedMutex_(), + cachedCredentials_("", ""), + expiry_() +{ +} + +StsAssumeRoleCredentialsProvider::~StsAssumeRoleCredentialsProvider() +{ +} + +Credentials StsAssumeRoleCredentialsProvider::getCredentials() +{ + loadCredentials(); + std::lock_guard locker(cachedMutex_); + return cachedCredentials_; +} + +bool StsAssumeRoleCredentialsProvider::checkExpiry()const +{ + auto now = std::chrono::system_clock::now(); + auto diff = std::chrono::duration_cast(now - expiry_).count(); + + return (diff > 0 - 60); +} + +void StsAssumeRoleCredentialsProvider::loadCredentials() +{ + if (checkExpiry()) + { + std::lock_guard locker(cachedMutex_); + if (checkExpiry()) + { + Model::AssumeRoleRequest request; + request.setRoleArn(roleArn_); + request.setRoleSessionName(roleSessionName_); + request.setPolicy(policy_); + request.setDurationSeconds(durationSeconds_); + + auto assumeRoleOutcome = stsClient_->assumeRole(request); + if (assumeRoleOutcome.isSuccess()) + { + const auto stsCredentials = assumeRoleOutcome.result().credentials(); + cachedCredentials_ = Credentials(stsCredentials.accessKeyId, + stsCredentials.accessKeySecret, + stsCredentials.securityToken); + + std::tm tm = {}; +#if defined(__GNUG__) && __GNUC__ < 5 + strptime(stsCredentials.expiration.c_str(), "%Y-%m-%dT%H:%M:%SZ", &tm); +#else + std::stringstream ss(stsCredentials.expiration); + ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%SZ"); +#endif + expiry_ = std::chrono::system_clock::from_time_t(std::mktime(&tm)); + } + } + } +} \ No newline at end of file diff --git a/core/src/Url.cc b/core/src/Url.cc new file mode 100644 index 000000000..0ee58741c --- /dev/null +++ b/core/src/Url.cc @@ -0,0 +1,344 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +using namespace AlibabaCloud; + +namespace +{ +#define INVALID_PORT -1 +} + +Url::Url(const std::string & url) : + scheme_(), + userName_(), + password_(), + host_(), + path_(), + port_(INVALID_PORT), + query_(), + fragment_() +{ + if(!url.empty()) + fromString(url); +} + +Url::~Url() +{ +} + +bool Url::operator==(const Url &url) const +{ + return scheme_ == url.scheme_ + && userName_ == url.userName_ + && password_ == url.password_ + && host_ == url.host_ + && path_ == url.path_ + && port_ == url.port_ + && query_ == url.query_ + && fragment_ == url.fragment_; +} + +bool Url::operator!=(const Url &url) const +{ + return !(*this == url); +} + +std::string Url::authority() const +{ + if (!isValid()) + return std::string(); + + std::ostringstream out; + std::string str = userInfo(); + if (!str.empty()) + out << str << "@"; + out << host_; + if (port_ != INVALID_PORT) + out << ":" << port_; + return out.str(); +} + +void Url::clear() +{ + scheme_.clear(); + userName_.clear(); + password_.clear(); + host_.clear(); + path_.clear(); + port_ = INVALID_PORT; + query_.clear(); + fragment_.clear(); +} + +std::string Url::fragment() const +{ + return fragment_; +} + +void Url::fromString(const std::string & url) +{ + clear(); + if (url.empty()) + return; + + std::string str = url; + std::string::size_type pos = 0; + std::string authority, fragment, path, query, scheme; + + pos = str.find("://"); + if (pos != str.npos) { + scheme = str.substr(0, pos); + str.erase(0, pos + 3); + } + + pos = str.find('#'); + if (pos != str.npos) { + fragment = str.substr(pos + 1); + str.erase(pos); + } + + pos = str.find('?'); + if (pos != str.npos) { + query = str.substr(pos + 1); + str.erase(pos); + } + + pos = str.find('/'); + if (pos != str.npos) { + path = str.substr(pos); + str.erase(pos); + } + else + path = "/"; + + authority = str; + + setScheme(scheme); + setAuthority(authority); + setPath(path); + setQuery(query); + setFragment(fragment); +} + +bool Url::hasFragment() const +{ + return !fragment_.empty(); +} + +bool Url::hasQuery() const +{ + return !query_.empty(); +} + +std::string Url::host() const +{ + return host_; +} + +bool Url::isEmpty() const +{ + return scheme_.empty() + && userName_.empty() + && password_.empty() + && host_.empty() + && path_.empty() + && (port_ == INVALID_PORT) + && query_.empty() + && fragment_.empty(); +} + +bool Url::isValid() const +{ + if (isEmpty()) + return false; + + if (host_.empty()) + return false; + + bool valid = true; + if (userName_.empty()) + valid = password_.empty(); + return valid; +} + +int Url::port() const +{ + return port_; +} + +std::string Url::password() const +{ + return password_; +} + +std::string Url::path() const +{ + return path_; +} + +std::string Url::query() const +{ + return query_; +} + +std::string Url::scheme() const +{ + return scheme_; +} + +void Url::setAuthority(const std::string & authority) +{ + if (authority.empty()) { + setUserInfo(""); + setHost(""); + setPort(INVALID_PORT); + return; + } + + std::string userinfo, host, port; + std::string::size_type pos = 0, prevpos = 0; + + pos = authority.find('@'); + if (pos != authority.npos) { + userinfo = authority.substr(0, pos); + prevpos = pos + 1; + } + else { + pos = 0; + } + + pos = authority.find(':', prevpos); + if (pos == authority.npos) + host = authority.substr(prevpos); + else { + host = authority.substr(prevpos, pos - prevpos); + port = authority.substr(pos + 1); + } + + setUserInfo(userinfo); + setHost(host); + setPort(!port.empty() ? atoi(port.c_str()): INVALID_PORT); +} + +void Url::setFragment(const std::string & fragment) +{ + fragment_ = fragment; +} + +void Url::setHost(const std::string & host) +{ + if(host.empty()){ + host_.clear(); + return; + } + host_ = host; + std::transform(host_.begin(), host_.end(), host_.begin(), ::tolower); +} + +void Url::setPassword(const std::string & password) +{ + password_ = password; +} + +void Url::setPath(const std::string & path) +{ + path_ = path; +} + +void Url::setPort(int port) +{ + port_ = port; +} + +void Url::setQuery(const std::string & query) +{ + query_ = query; +} + +void Url::setScheme(const std::string & scheme) +{ + if(scheme.empty()){ + scheme_.clear(); + return; + } + scheme_ = scheme; + std::transform(scheme_.begin(), scheme_.end(), scheme_.begin(), ::tolower); +} + +void Url::setUserInfo(const std::string & userInfo) +{ + if (userInfo.empty()) { + userName_.clear(); + password_.clear(); + return; + } + + auto pos = userInfo.find(':'); + if (pos == userInfo.npos) + userName_ = userInfo; + else { + userName_ = userInfo.substr(0, pos); + password_ = userInfo.substr(pos + 1); + } +} + +void Url::setUserName(const std::string & userName) +{ + userName_ = userName; +} + +std::string Url::toString() const +{ + if (!isValid()) + return std::string(); + + std::ostringstream out; + if (!scheme_.empty()) + out << scheme_ << "://"; + std::string str = authority(); + if (!str.empty()) + out << authority(); + if (path_.empty()) + out << "/"; + else + out << path_; + if (hasQuery()) + out << "?" << query_; + if (hasFragment()) + out << "#" << fragment_; + return out.str(); +} + +std::string Url::userInfo() const +{ + if (!isValid()) + return std::string(); + + std::ostringstream out; + out << userName_; + if (!password_.empty()) + out << ":" << password_; + return out.str(); +} + +std::string Url::userName() const +{ + return userName_; +} diff --git a/core/src/Utils.cc b/core/src/Utils.cc new file mode 100644 index 000000000..b75ca1ab1 --- /dev/null +++ b/core/src/Utils.cc @@ -0,0 +1,142 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Utils.h" +#include +#ifdef _WIN32 +#include +#else +#include +#include +#include +#endif + +std::string AlibabaCloud::GenerateUuid() +{ +#ifdef _WIN32 + char *data; + UUID uuidhandle; + UuidCreate(&uuidhandle); + UuidToString(&uuidhandle, (RPC_CSTR*)&data); + std::string uuid(data); + RpcStringFree((RPC_CSTR*)&data); + return uuid; +#else + uuid_t uu; + uuid_generate(uu); + char buf[36]; + uuid_unparse(uu, buf); + return buf; +#endif +} + +std::string AlibabaCloud::UrlEncode(const std::string & src) +{ + CURL *curl = curl_easy_init(); + char *output = curl_easy_escape(curl, src.c_str(), src.size()); + std::string result(output); + curl_free(output); + return result; +} + +std::string AlibabaCloud::UrlDecode(const std::string & src) +{ + CURL *curl = curl_easy_init(); + int outlength = 0; + char *output = curl_easy_unescape(curl, src.c_str(), src.size(), &outlength); + std::string result(output, outlength); + curl_free(output); + return result; +} + +std::string AlibabaCloud::ComputeContentMD5(const char * data, size_t size) +{ +#ifdef _WIN32 + HCRYPTPROV hProv = 0; + HCRYPTHASH hHash = 0; + BYTE pbHash[16]; + DWORD dwDataLen = 16; + + CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); + CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash); + CryptHashData(hHash, (BYTE*)(data), size, 0); + CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &dwDataLen, 0); + + CryptDestroyHash(hHash); + CryptReleaseContext(hProv, 0); + + DWORD dlen = 0; + CryptBinaryToString(pbHash, dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &dlen); + char* dest = new char[dlen]; + CryptBinaryToString(pbHash, dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &dlen); + + std::string ret = std::string(dest, dlen); + delete dest; + return ret; +#else + unsigned char md[MD5_DIGEST_LENGTH]; + MD5(reinterpret_cast(data), size, (unsigned char*)&md); + + char encodedData[100]; + EVP_EncodeBlock(reinterpret_cast(encodedData), md, MD5_DIGEST_LENGTH); + return encodedData; +#endif +} + +void AlibabaCloud::StringReplace(std::string & src, const std::string & s1, const std::string & s2) +{ + std::string::size_type pos =0; + while ((pos = src.find(s1, pos)) != std::string::npos) + { + src.replace(pos, s1.length(), s2); + pos += s2.length(); + } +} + +std::string AlibabaCloud::HttpMethodToString(HttpRequest::Method method) +{ + switch (method) + { + case HttpRequest::Method::Head: + return "HEAD"; + break; + case HttpRequest::Method::Post: + return "POST"; + break; + case HttpRequest::Method::Put: + return "PUT"; + break; + case HttpRequest::Method::Delete: + return "DELETE"; + break; + case HttpRequest::Method::Connect: + return "CONNECT"; + break; + case HttpRequest::Method::Options: + return "OPTIONS"; + break; + case HttpRequest::Method::Patch: + return "PATCH"; + break; + case HttpRequest::Method::Trace: + return "TRACE"; + break; + case HttpRequest::Method::Get: + default: + return "GET"; + break; + } +} diff --git a/core/src/Utils.h b/core/src/Utils.h new file mode 100644 index 000000000..a99a960eb --- /dev/null +++ b/core/src/Utils.h @@ -0,0 +1,32 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ALIBABACLOUD_CORE_UTILS_H_ +#define ALIBABACLOUD_CORE_UTILS_H_ + +#include +#include + +namespace AlibabaCloud +{ + std::string ComputeContentMD5(const char *data, size_t size); + std::string GenerateUuid(); + std::string HttpMethodToString(HttpRequest::Method method); + void StringReplace(std::string &src, const std::string &s1, const std::string &s2); + std::string UrlEncode(const std::string &src); + std::string UrlDecode(const std::string &src); +} +#endif \ No newline at end of file diff --git a/core/src/location/LocationClient.cc b/core/src/location/LocationClient.cc new file mode 100644 index 000000000..56a7165cc --- /dev/null +++ b/core/src/location/LocationClient.cc @@ -0,0 +1,79 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +using namespace AlibabaCloud; +using namespace AlibabaCloud::Location; + +LocationClient::LocationClient(const Credentials &credentials, const ClientConfiguration &configuration) : + RpcServiceClient(std::make_shared(credentials), configuration) +{ +} + +LocationClient::LocationClient(const std::shared_ptr& credentialsProvider, const ClientConfiguration & configuration) : + RpcServiceClient(credentialsProvider, configuration) +{ +} + +LocationClient::LocationClient(const std::string & accessKeyId, const std::string & accessKeySecret, const ClientConfiguration & configuration) : + RpcServiceClient(std::make_shared(accessKeyId, accessKeySecret), configuration) +{ +} + +LocationClient::~LocationClient() +{} + +CoreClient::EndpointOutcome LocationClient::endpoint()const +{ + return CoreClient::EndpointOutcome("location.aliyuncs.com"); +} + +LocationClient::DescribeEndpointsOutcome LocationClient::describeEndpoints(const Model::DescribeEndpointsRequest &request) const +{ + auto endpointOutcome = endpoint(); + if (!endpointOutcome.isSuccess()) + return DescribeEndpointsOutcome(endpointOutcome.error()); + + auto outcome = makeRequest(endpointOutcome.result(), request); + + if (outcome.isSuccess()) + return DescribeEndpointsOutcome(Model::DescribeEndpointsResult(outcome.result())); + else + return DescribeEndpointsOutcome(outcome.error()); +} + +void LocationClient::describeEndpointsAsync(const Model::DescribeEndpointsRequest& request, const DescribeEndpointsAsyncHandler& handler, const std::shared_ptr& context) const +{ + std::async(std::launch::async, + [this, request, handler, context]() + { + handler(this, request, describeEndpoints(request), context); + }); +} + +LocationClient::DescribeEndpointsOutcomeCallable LocationClient::describeEndpointsCallable(const Model::DescribeEndpointsRequest &request) const +{ + auto task = std::make_shared>( + [this, request]() + { + return this->describeEndpoints(request); + }); + + asyncExecute(new Runnable([task]() { (*task)(); })); + return task->get_future(); +} \ No newline at end of file diff --git a/core/src/location/LocationRequest.cc b/core/src/location/LocationRequest.cc new file mode 100644 index 000000000..d60ae8694 --- /dev/null +++ b/core/src/location/LocationRequest.cc @@ -0,0 +1,26 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud::Location; + +LocationRequest::LocationRequest(const std::string & action) : + RpcServiceRequest("location", "2015-06-12", action) +{ } + +LocationRequest::~LocationRequest() +{ } \ No newline at end of file diff --git a/core/src/location/model/DescribeEndpointsRequest.cc b/core/src/location/model/DescribeEndpointsRequest.cc new file mode 100644 index 000000000..a9b1a7b1b --- /dev/null +++ b/core/src/location/model/DescribeEndpointsRequest.cc @@ -0,0 +1,58 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +using namespace AlibabaCloud::Location; +using namespace AlibabaCloud::Location::Model; + +DescribeEndpointsRequest::DescribeEndpointsRequest() : + LocationRequest("DescribeEndpoints") +{} + +DescribeEndpointsRequest::~DescribeEndpointsRequest() +{} + +std::string DescribeEndpointsRequest::serviceCode()const +{ + return parameter("ServiceCode"); +} + +void DescribeEndpointsRequest::setServiceCode(const std::string & serviceCode) +{ + setParameter("ServiceCode", serviceCode); +} + +std::string DescribeEndpointsRequest::id()const +{ + return parameter("Id"); +} + +void DescribeEndpointsRequest::setId(const std::string & id) +{ + setParameter("Id", id); +} + +std::string DescribeEndpointsRequest::type()const +{ + return parameter("Type"); +} + +void DescribeEndpointsRequest::setType(const std::string & type) +{ + setParameter("Type", type); +} + diff --git a/core/src/location/model/DescribeEndpointsResult.cc b/core/src/location/model/DescribeEndpointsResult.cc new file mode 100644 index 000000000..3ac44d2e6 --- /dev/null +++ b/core/src/location/model/DescribeEndpointsResult.cc @@ -0,0 +1,83 @@ +/* + * Copyright 2009-2017 Alibaba Cloud All rights reserved. + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +using namespace AlibabaCloud::Location; +using namespace AlibabaCloud::Location::Model; + +DescribeEndpointsResult::DescribeEndpointsResult() : + ServiceResult() +{ +} + +DescribeEndpointsResult::DescribeEndpointsResult(const std::string &payload) : + ServiceResult() +{ + parse(payload); +} + +DescribeEndpointsResult::~DescribeEndpointsResult() +{} + +void DescribeEndpointsResult::parse(const std::string &payload) +{ + Json::Reader reader; + Json::Value value; + reader.parse(payload, value); + + setRequestId(value["RequestId"].asString()); + + success_ = value["Success"].asBool(); + auto allEndpoints = value["Endpoints"]["Endpoint"]; + for (const auto &item : allEndpoints) + { + Endpoint region; + region.endpoint = item["Endpoint"].asString(); + region.id = item["Id"].asString(); + region.namespace_ = item["Namespace"].asString(); + region.serivceCode = item["SerivceCode"].asString(); + region.type = item["Type"].asString(); + auto allProtocols = item["Protocols"]["Protocols"]; + for (const auto &item : allProtocols) + { + region.protocols.push_back(item.asString()); + } + endpoints_.push_back(region); + } +} + +std::vector DescribeEndpointsResult::endpoints()const +{ + return endpoints_; +} + +void DescribeEndpointsResult::setEndpoints(const std::vector & endpoints) +{ + endpoints_ = endpoints; +} + +bool DescribeEndpointsResult::success()const +{ + return success_; +} + +void DescribeEndpointsResult::setSuccess(const bool & success) +{ + success_ = success; +} + diff --git a/core/src/sts/StsClient.cc b/core/src/sts/StsClient.cc new file mode 100644 index 000000000..3544f1fbc --- /dev/null +++ b/core/src/sts/StsClient.cc @@ -0,0 +1,118 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include + +using namespace AlibabaCloud; +using namespace AlibabaCloud::Sts; +using namespace AlibabaCloud::Sts::Model; + +StsClient::StsClient(const Credentials &credentials, const ClientConfiguration &configuration) : + RpcServiceClient(std::make_shared(credentials), configuration) +{ +} + +StsClient::StsClient(const std::shared_ptr& credentialsProvider, const ClientConfiguration & configuration) : + RpcServiceClient(credentialsProvider, configuration) +{ +} + +StsClient::StsClient(const std::string & accessKeyId, const std::string & accessKeySecret, const ClientConfiguration & configuration) : + RpcServiceClient(std::make_shared(accessKeyId, accessKeySecret), configuration) +{ +} + +StsClient::~StsClient() +{ +} + +CoreClient::EndpointOutcome StsClient::endpoint()const +{ + return CoreClient::EndpointOutcome("sts.aliyuncs.com"); +} + +StsClient::AssumeRoleOutcome StsClient::assumeRole(const Model::AssumeRoleRequest &request)const +{ + auto endpointOutcome = endpoint(); + if (!endpointOutcome.isSuccess()) + return AssumeRoleOutcome(endpointOutcome.error()); + + auto outcome = makeRequest(endpointOutcome.result(), request); + + if (outcome.isSuccess()) + return AssumeRoleOutcome(AssumeRoleResult(outcome.result())); + else + return AssumeRoleOutcome(Error(outcome.error())); +} + +void StsClient::assumeRoleAsync(const Model::AssumeRoleRequest & request, const AssumeRoleAsyncHandler & handler, const std::shared_ptr& context) const +{ + auto fn = [this, request, handler, context]() + { + handler(this, request, assumeRole(request), context); + }; + + asyncExecute(new Runnable(fn)); +} + +StsClient::AssumeRoleOutcomeCallable StsClient::assumeRoleCallable(const Model::AssumeRoleRequest & request) const +{ + auto task = std::make_shared>( + [this, request]() + { + return this->assumeRole(request); + }); + + asyncExecute(new Runnable([task]() { (*task)(); })); + return task->get_future(); +} + +StsClient::GetCallerIdentityOutcome StsClient::getCallerIdentity(const GetCallerIdentityRequest &request) const +{ + auto endpointOutcome = endpoint(); + if (!endpointOutcome.isSuccess()) + return GetCallerIdentityOutcome(endpointOutcome.error()); + + auto outcome = makeRequest(endpointOutcome.result(), request); + + if (outcome.isSuccess()) + return GetCallerIdentityOutcome(GetCallerIdentityResult(outcome.result())); + else + return GetCallerIdentityOutcome(Error(outcome.error())); +} + +void StsClient::getCallerIdentityAsync(const GetCallerIdentityRequest& request, const GetCallerIdentityAsyncHandler& handler, const std::shared_ptr& context) const +{ + auto fn = [this, request, handler, context]() + { + handler(this, request, getCallerIdentity(request), context); + }; + + asyncExecute(new Runnable(fn)); +} + +StsClient::GetCallerIdentityOutcomeCallable StsClient::getCallerIdentityCallable(const GetCallerIdentityRequest &request) const +{ + auto task = std::make_shared>( + [this, request]() + { + return this->getCallerIdentity(request); + }); + + asyncExecute(new Runnable([task]() { (*task)(); })); + return task->get_future(); +} diff --git a/core/src/sts/StsRequest.cc b/core/src/sts/StsRequest.cc new file mode 100644 index 000000000..f92cf1e8f --- /dev/null +++ b/core/src/sts/StsRequest.cc @@ -0,0 +1,29 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include + +using namespace AlibabaCloud; +using namespace AlibabaCloud::Sts; + +StsRequest::StsRequest(const std::string & action) : + RpcServiceRequest("sts", "2015-04-01", action) +{ +} + +StsRequest::~StsRequest() +{ +} \ No newline at end of file diff --git a/core/src/sts/model/AssumeRoleRequest.cc b/core/src/sts/model/AssumeRoleRequest.cc new file mode 100644 index 000000000..40efd8adb --- /dev/null +++ b/core/src/sts/model/AssumeRoleRequest.cc @@ -0,0 +1,73 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include + +using namespace AlibabaCloud; +using namespace AlibabaCloud::Sts; +using namespace AlibabaCloud::Sts::Model; + +AssumeRoleRequest::AssumeRoleRequest() : + StsRequest("AssumeRole") +{ + setRoleArn(""); + setRoleSessionName(""); + setDurationSeconds(3600); +} + +AssumeRoleRequest::~AssumeRoleRequest() +{ +} + +int AssumeRoleRequest::durationSeconds() const +{ + return std::stoi(parameter("DurationSeconds")); +} + +std::string AssumeRoleRequest::policy() const +{ + return parameter("Policy"); +} + +std::string AssumeRoleRequest::roleArn() const +{ + return parameter("RoleArn"); +} + +std::string AssumeRoleRequest::roleSessionName() const +{ + return parameter("RoleSessionName"); +} + +void AssumeRoleRequest::setDurationSeconds(int durationSeconds) +{ + setParameter("DurationSeconds", std::to_string(durationSeconds)); +} + +void AssumeRoleRequest::setPolicy(const std::string & policy) +{ + setParameter("Policy", policy); +} + +void AssumeRoleRequest::setRoleArn(const std::string & roleArn) +{ + setParameter("RoleArn", roleArn); +} + +void AssumeRoleRequest::setRoleSessionName(const std::string & roleSessionName) +{ + setParameter("RoleSessionName", roleSessionName); +} diff --git a/core/src/sts/model/AssumeRoleResult.cc b/core/src/sts/model/AssumeRoleResult.cc new file mode 100644 index 000000000..ca70b0fd2 --- /dev/null +++ b/core/src/sts/model/AssumeRoleResult.cc @@ -0,0 +1,69 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include + +using namespace AlibabaCloud::Sts; +using namespace AlibabaCloud::Sts::Model; + +AssumeRoleResult::AssumeRoleResult() : + ServiceResult(), + assumedRoleUser_(), + credentials_() +{ +} + +AssumeRoleResult::AssumeRoleResult(const std::string & payload) : + ServiceResult(), + assumedRoleUser_(), + credentials_() +{ + parse(payload); +} + +AssumeRoleResult::~AssumeRoleResult() +{ +} + +AssumeRoleResult::AssumedRoleUser AssumeRoleResult::assumedRoleUser() const +{ + return assumedRoleUser_; +} + +AssumeRoleResult::Credentials AssumeRoleResult::credentials() const +{ + return credentials_; +} + +void AssumeRoleResult::parse(const std::string & payload) +{ + Json::Reader reader; + Json::Value value; + reader.parse(payload, value); + + setRequestId(value["RequestId"].asString()); + + auto assumedRoleUserNode = value["AssumedRoleUser"]; + assumedRoleUser_.assumedRoleId = assumedRoleUserNode["AssumedRoleId"].asString(); + assumedRoleUser_.arn = assumedRoleUserNode["Arn"].asString(); + + auto credentialsNode = value["Credentials"]; + credentials_.accessKeyId = credentialsNode["AccessKeyId"].asString(); + credentials_.accessKeySecret = credentialsNode["AccessKeySecret"].asString(); + credentials_.expiration = credentialsNode["Expiration"].asString(); + credentials_.securityToken = credentialsNode["SecurityToken"].asString(); +} diff --git a/core/src/sts/model/GetCallerIdentityRequest.cc b/core/src/sts/model/GetCallerIdentityRequest.cc new file mode 100644 index 000000000..dcac86dfa --- /dev/null +++ b/core/src/sts/model/GetCallerIdentityRequest.cc @@ -0,0 +1,30 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include + +using namespace AlibabaCloud; +using namespace AlibabaCloud::Sts; +using namespace AlibabaCloud::Sts::Model; + +GetCallerIdentityRequest::GetCallerIdentityRequest() : + StsRequest("GetCallerIdentity") +{ +} + +GetCallerIdentityRequest::~GetCallerIdentityRequest() +{ +} \ No newline at end of file diff --git a/core/src/sts/model/GetCallerIdentityResult.cc b/core/src/sts/model/GetCallerIdentityResult.cc new file mode 100644 index 000000000..f8cfe7673 --- /dev/null +++ b/core/src/sts/model/GetCallerIdentityResult.cc @@ -0,0 +1,71 @@ +/* +* Copyright 2009-2017 Alibaba Cloud All rights reserved. +* +* 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. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include + +using namespace AlibabaCloud; +using namespace AlibabaCloud::Sts; +using namespace AlibabaCloud::Sts::Model; + +GetCallerIdentityResult::GetCallerIdentityResult() : + ServiceResult(), + accountId_(), + arn_(), + userId_() +{ +} + +GetCallerIdentityResult::GetCallerIdentityResult(const std::string & payload) : + ServiceResult(), + accountId_(), + arn_(), + userId_() +{ + parse(payload); +} + +GetCallerIdentityResult::~GetCallerIdentityResult() +{ +} + +std::string GetCallerIdentityResult::accountId() +{ + return accountId_; +} + +std::string GetCallerIdentityResult::arn() const +{ + return arn_; +} + +std::string GetCallerIdentityResult::userId() const +{ + return userId_; +} + +void GetCallerIdentityResult::parse(const std::string & payload) +{ + Json::Reader reader; + Json::Value value; + reader.parse(payload, value); + + setRequestId(value["RequestId"].asString()); + + accountId_ = value["AccountId"].asString(); + arn_ = value["Arn"].asString(); + userId_ = value["UserId"].asString(); +} \ No newline at end of file