Skip to content

Commit

Permalink
feat(curl): support http post
Browse files Browse the repository at this point in the history
Signed-off-by: pingkai <pingkai010@gmail.com>
  • Loading branch information
pingkai authored and I-m-SuperMan committed Aug 7, 2020
1 parent 25af398 commit d988bad
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
11 changes: 11 additions & 0 deletions framework/data_source/curl/CURLConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions framework/data_source/curl/CURLConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions framework/data_source/curl/curl_data_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ CURLConnection *CurlDataSource::initConnection()
{
auto *pHandle = new CURLConnection(pConfig);
pHandle->setSource(mLocation, headerList);
pHandle->setPost(mBPost, mPostSize, mPostData);
return pHandle;
}

Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions framework/data_source/curl/curl_data_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +64,8 @@ namespace Cicada {

static CurlDataSource se;

void closeConnections(bool current);

private:
const static int max_connection = 1;
std::string mLocation;
Expand All @@ -82,8 +86,9 @@ namespace Cicada {
std::string mConnectInfo;
bool mBDummy = false;
std::vector<CURLConnection *>* mConnections {nullptr};

void closeConnections(bool current);
bool mBPost{false};
const uint8_t *mPostData{nullptr};
int64_t mPostSize{0};
};
}

Expand Down
21 changes: 17 additions & 4 deletions framework/tests/dataSource/dataSourceUnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
//

#include "gtest/gtest.h"
#include <data_source/curl/curl_data_source.h>
#include <data_source/dataSourcePrototype.h>
#include <memory>
#include <utils/timer.h>
#include <utils/globalSettings.h>
#include <utils/frame_work_log.h>
#include <utils/AFUtils.h>
#include <utils/CicadaJSON.h>
#include <utils/errors/framework_error.h>
#include <utils/AFUtils.h>
#include <utils/frame_work_log.h>
#include <utils/globalSettings.h>
#include <utils/timer.h>

using namespace std;
using namespace Cicada;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d988bad

Please sign in to comment.