From d988bad81b8051d00f704d3bf48f3227bc0866cf Mon Sep 17 00:00:00 2001 From: pingkai Date: Fri, 7 Aug 2020 17:20:15 +0800 Subject: [PATCH] feat(curl): support http post Signed-off-by: pingkai --- framework/data_source/curl/CURLConnection.cpp | 11 ++++++++++ framework/data_source/curl/CURLConnection.h | 2 ++ .../data_source/curl/curl_data_source.cpp | 8 +++++++ framework/data_source/curl/curl_data_source.h | 9 ++++++-- .../tests/dataSource/dataSourceUnitTest.cpp | 21 +++++++++++++++---- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/framework/data_source/curl/CURLConnection.cpp b/framework/data_source/curl/CURLConnection.cpp index 0cc1126fd..f0e0ab27c 100644 --- a/framework/data_source/curl/CURLConnection.cpp +++ b/framework/data_source/curl/CURLConnection.cpp @@ -292,6 +292,17 @@ void Cicada::CURLConnection::setSource(const string &location, struct curl_slist curl_easy_setopt(mHttp_handle, CURLOPT_RESOLVE, reSolveList); } } +void CURLConnection::setPost(bool post, int64_t size, const uint8_t *data) +{ + if (post) { + curl_easy_setopt(mHttp_handle, CURLOPT_POST, 1); + curl_easy_setopt(mHttp_handle, CURLOPT_POSTFIELDSIZE, (long) size); + curl_easy_setopt(mHttp_handle, CURLOPT_POSTFIELDS, data); + + } else { + curl_easy_setopt(mHttp_handle, CURLOPT_POST, 0); + } +} int CURLConnection::my_trace(CURL *handle, curl_infotype type, char *data, size_t size, diff --git a/framework/data_source/curl/CURLConnection.h b/framework/data_source/curl/CURLConnection.h index 037a994b9..71405531c 100644 --- a/framework/data_source/curl/CURLConnection.h +++ b/framework/data_source/curl/CURLConnection.h @@ -22,6 +22,8 @@ namespace Cicada { void setSource(const std::string &location, struct curl_slist *headerList); + void setPost(bool post, int64_t size, const uint8_t *data); + void updateSource(const std::string &location); void setInterrupt(std::atomic_bool *inter); diff --git a/framework/data_source/curl/curl_data_source.cpp b/framework/data_source/curl/curl_data_source.cpp index fa56665df..5055d16d4 100644 --- a/framework/data_source/curl/curl_data_source.cpp +++ b/framework/data_source/curl/curl_data_source.cpp @@ -49,6 +49,7 @@ CURLConnection *CurlDataSource::initConnection() { auto *pHandle = new CURLConnection(pConfig); pHandle->setSource(mLocation, headerList); + pHandle->setPost(mBPost, mPostSize, mPostData); return pHandle; } @@ -150,6 +151,13 @@ CurlDataSource::~CurlDataSource() Close(); } +void CurlDataSource::setPost(bool post, int64_t size, const uint8_t *data) +{ + mBPost = post; + mPostSize = size; + mPostData = data; +} + int CurlDataSource::Open(int flags) { // TODO: deal with ret diff --git a/framework/data_source/curl/curl_data_source.h b/framework/data_source/curl/curl_data_source.h index 8afff17de..915194920 100644 --- a/framework/data_source/curl/curl_data_source.h +++ b/framework/data_source/curl/curl_data_source.h @@ -23,6 +23,8 @@ namespace Cicada { ~CurlDataSource() override; + void setPost(bool post, int64_t size, const uint8_t *data); + int Open(int flags) override; int Open(const std::string &url) override; @@ -62,6 +64,8 @@ namespace Cicada { static CurlDataSource se; + void closeConnections(bool current); + private: const static int max_connection = 1; std::string mLocation; @@ -82,8 +86,9 @@ namespace Cicada { std::string mConnectInfo; bool mBDummy = false; std::vector* mConnections {nullptr}; - - void closeConnections(bool current); + bool mBPost{false}; + const uint8_t *mPostData{nullptr}; + int64_t mPostSize{0}; }; } diff --git a/framework/tests/dataSource/dataSourceUnitTest.cpp b/framework/tests/dataSource/dataSourceUnitTest.cpp index 96717d201..7486dde4c 100644 --- a/framework/tests/dataSource/dataSourceUnitTest.cpp +++ b/framework/tests/dataSource/dataSourceUnitTest.cpp @@ -3,14 +3,15 @@ // #include "gtest/gtest.h" +#include #include #include -#include -#include -#include +#include #include #include -#include +#include +#include +#include using namespace std; using namespace Cicada; @@ -189,6 +190,18 @@ TEST(http, keep_alive) free(buf); } +TEST(http, post) +{ + string url = "https://apizza.net/"; + CurlDataSource source(url); + uint8_t c = 'c'; + source.setPost(true, 1, &c); + int ret = source.Open(0); + ASSERT_GE(ret, 0); + ret = source.Seek(0, SEEK_SIZE); + AF_LOGD("size is %d\n", ret); +} + #include "ipList.h" TEST(https, ipList)