forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* use libcurl * remove some useless curl * address some comment
- Loading branch information
1 parent
1aee962
commit d59ee99
Showing
35 changed files
with
233 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,121 @@ | ||
/* Copyright (c) 2019 vesoft inc. All rights reserved. | ||
/* Copyright (c) 2022 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
#include "common/http/HttpClient.h" | ||
|
||
#include "common/process/ProcessUtils.h" | ||
|
||
#include "curl/curl.h" | ||
namespace nebula { | ||
namespace http { | ||
|
||
StatusOr<std::string> HttpClient::get(const std::string& path, const std::string& options) { | ||
auto command = folly::stringPrintf("/usr/bin/curl %s \"%s\"", options.c_str(), path.c_str()); | ||
LOG(INFO) << "HTTP Get Command: " << command; | ||
auto result = nebula::ProcessUtils::runCommand(command.c_str()); | ||
if (result.ok()) { | ||
return result.value(); | ||
} else { | ||
return Status::Error(folly::stringPrintf("Http Get Failed: %s", path.c_str())); | ||
} | ||
|
||
CurlHandle::CurlHandle() { | ||
curl_global_init(CURL_GLOBAL_ALL); | ||
} | ||
|
||
StatusOr<std::string> HttpClient::post(const std::string& path, const std::string& header) { | ||
auto command = | ||
folly::stringPrintf("/usr/bin/curl -X POST %s \"%s\"", header.c_str(), path.c_str()); | ||
LOG(INFO) << "HTTP POST Command: " << command; | ||
auto result = nebula::ProcessUtils::runCommand(command.c_str()); | ||
if (result.ok()) { | ||
return result.value(); | ||
} else { | ||
return Status::Error(folly::stringPrintf("Http Post Failed: %s", path.c_str())); | ||
} | ||
CurlHandle::~CurlHandle() { | ||
curl_global_cleanup(); | ||
} | ||
|
||
StatusOr<std::string> HttpClient::post(const std::string& path, | ||
const std::unordered_map<std::string, std::string>& header) { | ||
folly::dynamic mapData = folly::dynamic::object; | ||
// Build a dynamic object from map | ||
for (auto const& it : header) { | ||
mapData[it.first] = it.second; | ||
} | ||
return post(path, mapData); | ||
CurlHandle* CurlHandle::instance() { | ||
static CurlHandle handle; | ||
return &handle; | ||
} | ||
|
||
HttpResponse HttpClient::get(const std::string& url) { | ||
return HttpClient::get(url, {}); | ||
} | ||
|
||
HttpResponse HttpClient::get(const std::string& url, const std::vector<std::string>& header) { | ||
return sendRequest(url, "GET", header, ""); | ||
} | ||
|
||
StatusOr<std::string> HttpClient::post(const std::string& path, const folly::dynamic& data) { | ||
return sendRequest(path, data, "POST"); | ||
HttpResponse HttpClient::post(const std::string& url, | ||
const std::vector<std::string>& header, | ||
const std::string& body) { | ||
return sendRequest(url, "POST", header, body); | ||
} | ||
|
||
StatusOr<std::string> HttpClient::put(const std::string& path, | ||
const std::unordered_map<std::string, std::string>& header) { | ||
folly::dynamic mapData = folly::dynamic::object; | ||
// Build a dynamic object from map | ||
for (auto const& it : header) { | ||
mapData[it.first] = it.second; | ||
HttpResponse HttpClient::delete_(const std::string& url, const std::vector<std::string>& header) { | ||
return sendRequest(url, "DELETE", header, ""); | ||
} | ||
|
||
HttpResponse HttpClient::put(const std::string& url, | ||
const std::vector<std::string>& header, | ||
const std::string& body) { | ||
return sendRequest(url, "PUT", header, body); | ||
} | ||
|
||
HttpResponse HttpClient::sendRequest(const std::string& url, | ||
const std::string& method, | ||
const std::vector<std::string>& header, | ||
const std::string& body) { | ||
CurlHandle::instance(); | ||
HttpResponse resp; | ||
CURL* curl = curl_easy_init(); | ||
CHECK(curl); | ||
setUrl(curl, url); | ||
setMethod(curl, method); | ||
curl_slist* h = setHeaders(curl, header); | ||
if (!body.empty()) { | ||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str()); | ||
} | ||
setRespHeader(curl, resp.header); | ||
setRespBody(curl, resp.body); | ||
setTimeout(curl); | ||
resp.curlCode = curl_easy_perform(curl); | ||
if (resp.curlCode != 0) { | ||
resp.curlMessage = std::string(curl_easy_strerror(resp.curlCode)); | ||
} | ||
return put(path, mapData); | ||
} | ||
|
||
StatusOr<std::string> HttpClient::put(const std::string& path, const std::string& header) { | ||
auto command = | ||
folly::stringPrintf("/usr/bin/curl -X PUT %s \"%s\"", header.c_str(), path.c_str()); | ||
LOG(INFO) << "HTTP PUT Command: " << command; | ||
auto result = nebula::ProcessUtils::runCommand(command.c_str()); | ||
if (result.ok()) { | ||
return result.value(); | ||
} else { | ||
return Status::Error(folly::stringPrintf("Http Put Failed: %s", path.c_str())); | ||
if (h) { | ||
curl_slist_free_all(h); | ||
} | ||
curl_easy_cleanup(curl); | ||
return resp; | ||
} | ||
|
||
StatusOr<std::string> HttpClient::put(const std::string& path, const folly::dynamic& data) { | ||
return sendRequest(path, data, "PUT"); | ||
void HttpClient::setUrl(CURL* curl, const std::string& url) { | ||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | ||
} | ||
|
||
StatusOr<std::string> HttpClient::sendRequest(const std::string& path, | ||
const folly::dynamic& data, | ||
const std::string& reqType) { | ||
std::string command; | ||
if (data.empty()) { | ||
command = folly::stringPrintf("curl -X %s -s \"%s\"", reqType.c_str(), path.c_str()); | ||
} else { | ||
command = | ||
folly::stringPrintf("curl -X %s -H \"Content-Type: application/json\" -d'%s' -s \"%s\"", | ||
reqType.c_str(), | ||
folly::toJson(data).c_str(), | ||
path.c_str()); | ||
void HttpClient::setMethod(CURL* curl, const std::string& method) { | ||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str()); | ||
} | ||
curl_slist* HttpClient::setHeaders(CURL* curl, const std::vector<std::string>& headers) { | ||
curl_slist* h = nullptr; | ||
for (auto& header : headers) { | ||
h = curl_slist_append(h, header.c_str()); | ||
} | ||
LOG(INFO) << folly::stringPrintf("HTTP %s Command: %s", reqType.c_str(), command.c_str()); | ||
auto result = nebula::ProcessUtils::runCommand(command.c_str()); | ||
if (result.ok()) { | ||
return result.value(); | ||
} else { | ||
return Status::Error(folly::stringPrintf("Http %s Failed: %s", reqType.c_str(), path.c_str())); | ||
if (h) { | ||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, h); | ||
} | ||
return h; | ||
} | ||
|
||
} // namespace http | ||
void HttpClient::setRespHeader(CURL* curl, const std::string& header) { | ||
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, onWriteData); | ||
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header); | ||
} | ||
|
||
void HttpClient::setRespBody(CURL* curl, const std::string& body) { | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onWriteData); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body); | ||
} | ||
|
||
void HttpClient::setTimeout(CURL* curl) { | ||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); | ||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1); | ||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); | ||
} | ||
|
||
size_t HttpClient::onWriteData(void* ptr, size_t size, size_t nmemb, void* stream) { | ||
if (ptr == nullptr || size == 0) { | ||
return 0; | ||
} | ||
CHECK(stream); | ||
size_t realsize = size * nmemb; | ||
std::string* buffer = static_cast<std::string*>(stream); | ||
CHECK(buffer); | ||
buffer->append(static_cast<char*>(ptr), realsize); | ||
return realsize; | ||
} | ||
} // namespace nebula |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,63 @@ | ||
/* Copyright (c) 2019 vesoft inc. All rights reserved. | ||
/* Copyright (c) 2022 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
#ifndef COMMON_HTTPCLIENT_H | ||
#define COMMON_HTTPCLIENT_H | ||
#ifndef COMMON_HTTP_HTTPCLIENT_H_ | ||
#define COMMON_HTTP_HTTPCLIENT_H_ | ||
#include <memory> | ||
#include <mutex> | ||
#include <string> | ||
|
||
#include "common/base/Base.h" | ||
#include "common/base/StatusOr.h" | ||
#include "curl/curl.h" | ||
|
||
namespace nebula { | ||
namespace http { | ||
|
||
class CurlHandle { | ||
public: | ||
static CurlHandle* instance(); | ||
|
||
private: | ||
CurlHandle(); | ||
~CurlHandle(); | ||
}; | ||
|
||
struct HttpResponse { | ||
CURLcode curlCode; | ||
std::string curlMessage; | ||
std::string header; | ||
std::string body; | ||
}; | ||
|
||
class HttpClient { | ||
public: | ||
HttpClient() = delete; | ||
|
||
~HttpClient() = default; | ||
// Send a http GET request | ||
static StatusOr<std::string> get(const std::string& path, const std::string& options = "-G"); | ||
|
||
// Send a http POST request | ||
static StatusOr<std::string> post(const std::string& path, const std::string& header); | ||
// Send a http POST request with a different function signature | ||
static StatusOr<std::string> post(const std::string& path, | ||
const std::unordered_map<std::string, std::string>& header); | ||
static StatusOr<std::string> post(const std::string& path, | ||
const folly::dynamic& data = folly::dynamic::object()); | ||
|
||
// Send a http PUT request | ||
static StatusOr<std::string> put(const std::string& path, const std::string& header); | ||
static StatusOr<std::string> put(const std::string& path, | ||
const std::unordered_map<std::string, std::string>& header); | ||
static StatusOr<std::string> put(const std::string& path, | ||
const folly::dynamic& data = folly::dynamic::object()); | ||
|
||
protected: | ||
static StatusOr<std::string> sendRequest(const std::string& path, | ||
const folly::dynamic& data, | ||
const std::string& reqType); | ||
static HttpResponse get(const std::string& url); | ||
static HttpResponse get(const std::string& url, const std::vector<std::string>& headers); | ||
|
||
static HttpResponse post(const std::string& url, | ||
const std::vector<std::string>& headers, | ||
const std::string& body); | ||
static HttpResponse delete_(const std::string& url, const std::vector<std::string>& headers); | ||
static HttpResponse put(const std::string& url, | ||
const std::vector<std::string>& headers, | ||
const std::string& body); | ||
|
||
private: | ||
static HttpResponse sendRequest(const std::string& url, | ||
const std::string& method, | ||
const std::vector<std::string>& headers, | ||
const std::string& body); | ||
static void setUrl(CURL* curl, const std::string& url); | ||
static void setMethod(CURL* curl, const std::string& method); | ||
static curl_slist* setHeaders(CURL* curl, const std::vector<std::string>& headers); | ||
|
||
static void setRespBody(CURL* curl, const std::string& body); | ||
static void setRespHeader(CURL* curl, const std::string& header); | ||
static void setTimeout(CURL* curl); | ||
static size_t onWriteData(void* ptr, size_t size, size_t nmemb, void* stream); | ||
}; | ||
|
||
} // namespace http | ||
} // namespace nebula | ||
|
||
#endif // COMMON_HTTPCLIENT_H | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.