add POST support and fix user specified scheme via commonrequest

This commit is contained in:
zhangzifa
2019-01-31 15:48:30 +08:00
committed by Jackson Tian
parent b429dcc915
commit 138dbdc61e
23 changed files with 215 additions and 49 deletions

View File

@@ -42,6 +42,7 @@ if(BUILD_FUNCTION_TESTS)
add_subdirectory(test/function_test/cdn)
add_subdirectory(test/function_test/core)
add_subdirectory(test/function_test/ecs)
add_subdirectory(test/function_test/nlp)
add_subdirectory(test/function_test/rds)
add_subdirectory(test/function_test/slb)
add_subdirectory(test/function_test/vpc)

View File

@@ -50,6 +50,7 @@ namespace AlibabaCloud
using ServiceRequest::setResourcePath;
void setRequestPattern(RequestPattern pattern);
using ServiceRequest::setVersion;
using ServiceRequest::setScheme;
RequestPattern requestPattern()const;
protected:
using ServiceRequest::product;

View File

@@ -1,12 +1,12 @@
/*
* 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.
@@ -26,7 +26,7 @@ namespace AlibabaCloud
public:
HmacSha1Signer();
~HmacSha1Signer();
virtual std::string generate(const std::string &src, const std::string &secret)const override;
};
}

View File

@@ -1,12 +1,12 @@
/*
* 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.
@@ -28,6 +28,8 @@ namespace AlibabaCloud
virtual ~RoaServiceRequest();
using ServiceRequest::setParameter;
using ServiceRequest::setContent;
using ServiceRequest::parameter;
using ServiceRequest::setScheme;
};
}
#endif // !ALIBABACLOUD_CORE_ROASERVICEREQUEST_H_

View File

@@ -1,12 +1,12 @@
/*
* 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.
@@ -28,6 +28,7 @@ namespace AlibabaCloud
virtual ~RpcServiceRequest();
std::string actionName()const;
using ServiceRequest::setScheme;
protected:
void setActionName(const std::string &name);

View File

@@ -1,12 +1,12 @@
/*
* 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.
@@ -39,6 +39,7 @@ namespace AlibabaCloud
std::string product()const;
std::string resourcePath()const;
std::string version()const;
std::string scheme()const;
protected:
ServiceRequest(const std::string &product, const std::string &version);
ServiceRequest(const ServiceRequest &other);
@@ -55,6 +56,7 @@ namespace AlibabaCloud
void setResourcePath(const std::string &path);
void setProduct(const std::string &product);
void setVersion(const std::string &version);
void setScheme(const std::string scheme);
private:
char *content_;
size_t contentSize_;
@@ -62,6 +64,7 @@ namespace AlibabaCloud
std::string product_;
std::string resourcePath_;
std::string version_;
std::string scheme_;
};
}

View File

@@ -1,12 +1,12 @@
/*
* 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.

View File

@@ -143,7 +143,12 @@ HttpRequest CommonClient::buildRoaHttpRequest(const std::string & endpoint, cons
const Credentials credentials = credentialsProvider_->getCredentials();
Url url;
url.setScheme("https");
if (msg.scheme().empty()) {
url.setScheme("https");
} else {
url.setScheme(msg.scheme());
}
url.setHost(endpoint);
url.setPath(msg.resourcePath());
@@ -168,13 +173,23 @@ HttpRequest CommonClient::buildRoaHttpRequest(const std::string & endpoint, cons
HttpRequest request(url);
request.setMethod(method);
request.setHeader("Accept", "application/json");
if (msg.headerParameter("Accept").empty()) {
request.setHeader("Accept", "application/json");
} else {
request.setHeader("Accept", msg.headerParameter("Accept"));
}
if (msg.hasContent()) {
std::stringstream ss;
ss << msg.contentSize();
request.setHeader("Content-Length", ss.str());
request.setHeader("Content-Type", "application/octet-stream");
if(msg.headerParameter("Content-Type").empty()) {
request.setHeader("Content-Type", "application/octet-stream");
} else {
request.setHeader("Content-Type", msg.headerParameter("Content-Type"));
}
request.setHeader("Content-MD5", ComputeContentMD5(msg.content(), msg.contentSize()));
request.setBody(msg.content(), msg.contentSize());
}
std::time_t t = std::time(nullptr);
@@ -215,7 +230,6 @@ HttpRequest CommonClient::buildRoaHttpRequest(const std::string & endpoint, cons
<< ":"
<< signer_->generate(plaintext.str(), credentials.accessKeySecret());
request.setHeader("Authorization", sign.str());
return request;
}
@@ -246,7 +260,11 @@ HttpRequest CommonClient::buildRpcHttpRequest(const std::string & endpoint, cons
const Credentials credentials = credentialsProvider_->getCredentials();
Url url;
url.setScheme("https");
if (msg.scheme().empty()) {
url.setScheme("https");
} else {
url.setScheme(msg.scheme());
}
url.setHost(endpoint);
url.setPath(msg.resourcePath());

View File

@@ -62,7 +62,11 @@ HttpRequest::Method CommonRequest::httpMethod() const
CommonRequest::ParameterValueType CommonRequest::queryParameter(const ParameterNameType &name)const
{
return queryParams_.at(name);
ParameterCollection::const_iterator it = queryParams_.find(name);
if (it == queryParams_.end()) {
return ParameterValueType("");
}
return it->second;
}
@@ -75,10 +79,14 @@ void CommonRequest::setQueryParameter(const ParameterNameType &name, const Param
{
queryParams_[name] = value;
}
CommonRequest::ParameterValueType CommonRequest::headerParameter(const ParameterNameType &name)const
{
return headerParams_.at(name);
const ParameterCollection::const_iterator it = headerParams_.find(name);
if (it == headerParams_.end()) {
return ParameterValueType("");
}
return it->second;
}
CommonRequest::ParameterCollection CommonRequest::headerParameters() const
@@ -90,4 +98,3 @@ void CommonRequest::setHeaderParameter(const ParameterNameType &name, const Para
{
headerParams_[name] = value;
}

View File

@@ -92,17 +92,22 @@ HttpClient::HttpResponseOutcome CurlHttpClient::makeRequest(const HttpRequest &r
{
curl_easy_reset(curlHandle_);
HttpResponse response(request);
std::string url = request.url().toString();
switch (request.method())
{
case HttpRequest::Method::Get:
break;
case HttpRequest::Method::Post:
curl_easy_setopt(curlHandle_, CURLOPT_POSTFIELDS, request.body());
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);

View File

@@ -1,12 +1,12 @@
/*
* 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.

View File

@@ -1,12 +1,12 @@
/*
* 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.

View File

@@ -1,12 +1,12 @@
/*
* 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.
@@ -103,7 +103,11 @@ HttpRequest RoaServiceClient::buildHttpRequest(const std::string & endpoint, con
const Credentials credentials = credentialsProvider_->getCredentials();
Url url;
url.setScheme("https");
if (msg.scheme().empty()) {
url.setScheme("https");
} else {
url.setScheme(msg.scheme());
}
url.setHost(endpoint);
url.setPath(msg.resourcePath());
@@ -128,13 +132,23 @@ HttpRequest RoaServiceClient::buildHttpRequest(const std::string & endpoint, con
HttpRequest request(url);
request.setMethod(method);
request.setHeader("Accept", "application/json");
if (msg.parameter("Accept").empty()) {
request.setHeader("Accept", "application/json");
} else {
request.setHeader("Accept", msg.parameter("Accept"));
}
if (msg.hasContent()) {
std::stringstream ss;
ss << msg.contentSize();
request.setHeader("Content-Length", ss.str());
request.setHeader("Content-Type", "application/octet-stream");
if(msg.parameter("Content-Type").empty()) {
request.setHeader("Content-Type", "application/octet-stream");
} else {
request.setHeader("Content-Type", msg.parameter("Content-Type"));
}
request.setHeader("Content-MD5", ComputeContentMD5(msg.content(),msg.contentSize()));
request.setBody(msg.content(), msg.contentSize());
}
std::time_t t = std::time(nullptr);

View File

@@ -77,7 +77,12 @@ HttpRequest RpcServiceClient::buildHttpRequest(const std::string & endpoint, con
const Credentials credentials = credentialsProvider_->getCredentials();
Url url;
url.setScheme("https");
if (msg.scheme().empty()) {
url.setScheme("https");
} else {
url.setScheme(msg.scheme());
}
url.setHost(endpoint);
url.setPath(msg.resourcePath());

View File

@@ -1,12 +1,12 @@
/*
* 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.
@@ -24,7 +24,8 @@ ServiceRequest::ServiceRequest(const std::string &product, const std::string &ve
params_(),
product_(product),
resourcePath_("/"),
version_(version)
version_(version),
scheme_("https")
{
}
@@ -34,7 +35,8 @@ ServiceRequest::ServiceRequest(const ServiceRequest &other) :
params_(other.params_),
product_(other.product_),
resourcePath_(other.resourcePath_),
version_(other.version_)
version_(other.version_),
scheme_(other.scheme_)
{
setContent(other.content_, other.contentSize_);
}
@@ -103,7 +105,11 @@ void ServiceRequest::addParameter(const ParameterNameType & name, const Paramete
ServiceRequest::ParameterValueType ServiceRequest::parameter(const ParameterNameType &name)const
{
return params_.at(name);
ParameterCollection::const_iterator it = params_.find(name);
if (it == params_.end()) {
return ParameterValueType("");
}
return it->second;
}
ServiceRequest::ParameterCollection ServiceRequest::parameters() const
@@ -154,4 +160,12 @@ std::string ServiceRequest::resourcePath() const
void ServiceRequest::setResourcePath(const std::string & path)
{
resourcePath_ = path;
}
}
void ServiceRequest::setScheme(const std::string scheme) {
scheme_ = scheme;
}
std::string ServiceRequest::scheme() const {
return scheme_;
}

View File

@@ -8,7 +8,7 @@ rm -rf $FT_BUILD_DIR
mkdir $FT_BUILD_DIR
cd $FT_BUILD_DIR
cmake -DBUILD_FUNCTION_TESTS=ON -DBUILD_UNIT_TESTS=OFF ..
make cdn core ecs rds slb vpc cdn_ft core_ft ecs_ft rds_ft slb_ft vpc_ft
make cdn core ecs rds slb vpc cdn_ft core_ft ecs_ft nlp_ft rds_ft slb_ft vpc_ft
echo '------- run function test -----------'

View File

@@ -101,7 +101,8 @@ https://cn-hangzhou/?
cr.setQueryParameter("query_k1", "query_v1");
cr.setHeaderParameter("header_k1", "header_v1");
cr.setScheme("");
cr.setHeaderParameter("Accept", "");
HttpRequest rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
EXPECT_TRUE(rr.method() == HttpRequest::Method::Get);
EXPECT_TRUE(rr.header("Accept") == "application/json");
@@ -121,8 +122,22 @@ https://cn-hangzhou/?
// acs accessKeyId:JZD81jGWLp1F3ZIkaLp1yuEZmKc=
EXPECT_TRUE(rr.header("Authorization").find("acs accessKeyId:") != string::npos);
EXPECT_TRUE(rr.header("unknown-header") == "");
cr.setScheme("http");
cr.setHeaderParameter("Accept", "test-accept");
cr.setHeaderParameter("Content-Type", "test-content-type");
rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
EXPECT_TRUE(rr.url().toString() == "http://cn-shanghai/?Accept=test-accept&Content-Type=test-content-type&header_k1=header_v1");
EXPECT_TRUE(rr.header("Accept") == "test-accept");
EXPECT_TRUE(rr.header("Content-Type") == "test-content-type");
HttpRequest rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
EXPECT_TRUE(client->serviceName() == "Common");
EXPECT_TRUE(rrr.url().scheme() == "http");
cr.setScheme("");
rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
EXPECT_TRUE(rrr.url().scheme() == "https");
std::function<void()> func(task);
Runnable* rf = new Runnable(func);

View File

@@ -69,6 +69,7 @@ TEST(CurlHttpClient, basic) {
EXPECT_TRUE(out1.result().body() == testBody);
request.setMethod(HttpRequest::Method::Post);
request.setBody("test-body", 9);
request.setUrl(url);
HttpClient::HttpResponseOutcome out2 = client.makeRequest(request);

View File

@@ -68,6 +68,7 @@ TEST(RoaServiceClient, basic) {
RoaServiceRequest req(product, version);
req.setParameter("a", "b");
req.setScheme("");
req.setContent("123456789", 9);
HttpRequest http_req = client.buildHttpRequest("cn-shanghai", req, HttpRequest::Method::Get);
@@ -88,4 +89,11 @@ TEST(RoaServiceClient, basic) {
const string nounce = "8a013b14-7bac-4652-8b5f-c02c8924e4ae";
EXPECT_TRUE(http_req.header("x-acs-signature-nonce").length() == nounce.length());
EXPECT_TRUE(http_req.header("Authorization").find("acs accessKeyId:") != string::npos);
req.setScheme("http");
req.setParameter("Accept", "tets-accept");
req.setParameter("Content-Type", "test-content-type");
http_req = client.buildHttpRequest("cn-shanghai", req, HttpRequest::Method::Get);
EXPECT_TRUE(http_req.url().toString() == "http://cn-shanghai/?Accept=tets-accept&Content-Type=test-content-type&a=b");
}

View File

@@ -63,6 +63,7 @@ TEST(RpcServiceClient, basic) {
RpcServiceRequest req(product, version, action);
req.setScheme("");
HttpRequest http_req = client.buildHttpRequest(endpoint, req, HttpRequest::Method::Get);
EXPECT_TRUE(http_req.header("x-sdk-client") == std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
EXPECT_TRUE(http_req.header("Host") == endpoint);
@@ -79,6 +80,11 @@ TEST(RpcServiceClient, basic) {
EXPECT_TRUE(http_req.url().query().find("&SignatureVersion=1.0") != string::npos);
EXPECT_TRUE(http_req.url().query().find("&Version=1.0") != string::npos);
EXPECT_TRUE(http_req.url().query().find("&Timestamp=") != string::npos);
EXPECT_TRUE(http_req.url().scheme() == "https");
req.setScheme("http");
http_req = client.buildHttpRequest(endpoint, req, HttpRequest::Method::Get);
EXPECT_TRUE(http_req.url().scheme() == "http");
}
TEST(RpcServiceClient, mock) {

View File

@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.0)
project(demo)
set(CMAKE_CXX_STANDARD 11)
if(CMAKE_HOST_WIN32)
include_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\include")
link_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\lib")
endif()
# note ft_build is the dir you build sdk
include_directories("../../../core/include/")
link_directories(${CMAKE_SOURCE_DIR}/ft_build/lib)
add_executable(nlp_ft nlp_wordsegment_ft.cc)
target_link_libraries(nlp_ft alibabacloud-sdk-core)
target_link_libraries(nlp_ft core gtest gmock_main)
set_target_properties(nlp_ft
PROPERTIES
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}nlp_ft
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_test(NAME nlp_ft COMMAND nlp_ft)

View File

@@ -0,0 +1,39 @@
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "gtest/gtest.h"
#include "../utils.h"
#include "alibabacloud/core/AlibabaCloud.h"
#include "alibabacloud/core/CommonClient.h"
#include "alibabacloud/core/HttpRequest.h"
using namespace std;
using namespace AlibabaCloud;
namespace {
TEST(nlp, wordsegment) {
utUtils utils;
string key = utils.get_env("ENV_AccessKeyId");
string secret = utils.get_env("ENV_AccessKeySecret");
AlibabaCloud::InitializeSdk();
ClientConfiguration configuration("cn-shanghai");
CommonClient client(key, secret, configuration);
// 创建API请求并设置参数
CommonRequest request(CommonRequest::RoaPattern);
request.setScheme("http");
request.setDomain("nlp.cn-shanghai.aliyuncs.com");
request.setResourcePath("/nlp/api/wordsegment/general");
request.setHttpMethod(HttpRequest::Post);
const char * data = "{\"lang\":\"ZH\",\"text\":\"Iphone is a bad choice 专用数据线\"}";
request.setContent(data, strlen(data));
request.setHeaderParameter("Content-Type", "application/json;chrset=utf-8");
request.setVersion("2018-04-08");
auto outcome = client.commonResponse(request);
EXPECT_TRUE(outcome.error().errorCode().empty());
const std::string expected = "{\"data\":[{\"id\":0,\"word\":\"Iphone\"},{\"id\":1,\"word\":\" \"},{\"id\":2,\"word\":\"is\"},{\"id\":3,\"word\":\" \"},{\"id\":4,\"word\":\"a\"},{\"id\":5,\"word\":\" \"},{\"id\":6,\"word\":\"bad\"},{\"id\":7,\"word\":\" \"},{\"id\":8,\"word\":\"choice\"},{\"id\":9,\"word\":\" \"},{\"id\":10,\"word\":\"专用\"},{\"id\":11,\"word\":\"数据线\"}]}";
EXPECT_TRUE(outcome.result().payload() == expected);
AlibabaCloud::ShutdownSdk();
}
}

View File

@@ -130,9 +130,10 @@ void ServerThread::Run() {
Terminate(true);
} else if (!strcmp("POST", method)) {
if (fopen(path, "r") > 0) {
sprintf(buffer, "%s\r\n\r\n", "HTTP/1.1 403 Forbidden");
int size = strlen(echo_buffer);
sprintf(buffer, "%s%d\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: ", size);
send(socket, buffer, strlen(buffer), 0);
cout << "402 " << path << endl;
send(socket, echo_buffer, size, 0);
continue;
}
FILE *file = fopen(path, "w+");