-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d35ff9c
commit 623e80b
Showing
33 changed files
with
343 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
add_library(http_client_obj OBJECT HttpClient.cpp) | ||
|
||
add_dependencies(http_client_obj process_obj base_obj) | ||
|
||
add_subdirectory(test) |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* Copyright (c) 2019 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#include "http/HttpClient.h" | ||
#include "process/ProcessUtils.h" | ||
|
||
namespace nebula { | ||
namespace http { | ||
|
||
StatusOr<std::string> HttpClient::get(const std::string& path) { | ||
auto command = folly::stringPrintf("/usr/bin/curl -G %s", 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())); | ||
} | ||
} | ||
|
||
} // namespace http | ||
} // 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* Copyright (c) 2019 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#ifndef COMMON_HTTPCLIENT_H | ||
#define COMMON_HTTPCLIENT_H | ||
|
||
#include "base/Base.h" | ||
#include "base/StatusOr.h" | ||
|
||
namespace nebula { | ||
namespace http { | ||
|
||
class HttpClient { | ||
public: | ||
HttpClient() = delete; | ||
|
||
~HttpClient() = default; | ||
|
||
static StatusOr<std::string> get(const std::string& path); | ||
}; | ||
|
||
} // namespace http | ||
} // namespace nebula | ||
|
||
#endif // COMMON_HTTPCLIENT_H | ||
|
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
add_executable( | ||
http_client_test | ||
HttpClientTest.cpp | ||
$<TARGET_OBJECTS:http_client_obj> | ||
$<TARGET_OBJECTS:ws_obj> | ||
$<TARGET_OBJECTS:process_obj> | ||
$<TARGET_OBJECTS:fs_obj> | ||
$<TARGET_OBJECTS:stats_obj> | ||
$<TARGET_OBJECTS:base_obj> | ||
) | ||
nebula_link_libraries( | ||
http_client_test | ||
proxygenhttpserver | ||
proxygenlib | ||
wangle | ||
gtest | ||
gtest_main | ||
) | ||
nebula_add_test(http_client_test) | ||
|
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* Copyright (c) 2019 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#include "base/Base.h" | ||
#include "http/HttpClient.h" | ||
#include <gtest/gtest.h> | ||
#include "webservice/Common.h" | ||
#include "webservice/WebService.h" | ||
#include "webservice/test/TestUtils.h" | ||
#include "proxygen/httpserver/RequestHandler.h" | ||
#include <proxygen/httpserver/ResponseBuilder.h> | ||
|
||
namespace nebula { | ||
namespace http { | ||
|
||
class HttpClientHandler : public proxygen::RequestHandler { | ||
public: | ||
HttpClientHandler() = default; | ||
|
||
void onRequest(std::unique_ptr<proxygen::HTTPMessage>) noexcept override { | ||
} | ||
|
||
void onBody(std::unique_ptr<folly::IOBuf>) noexcept override { | ||
} | ||
|
||
void onEOM() noexcept override { | ||
proxygen::ResponseBuilder(downstream_) | ||
.status(WebServiceUtils::to(HttpStatusCode::OK), | ||
WebServiceUtils::toString(HttpStatusCode::OK)) | ||
.body("HttpClientHandler successfully") | ||
.sendWithEOM(); | ||
} | ||
|
||
void onUpgrade(proxygen::UpgradeProtocol) noexcept override { | ||
} | ||
|
||
void requestComplete() noexcept override { | ||
delete this; | ||
} | ||
|
||
void onError(proxygen::ProxygenError error) noexcept override { | ||
LOG(ERROR) << "HttpClientHandler Error: " | ||
<< proxygen::getErrorString(error); | ||
} | ||
}; | ||
class HttpClientTestEnv : public ::testing::Environment { | ||
public: | ||
void SetUp() override { | ||
FLAGS_ws_http_port = 0; | ||
FLAGS_ws_h2_port = 0; | ||
LOG(INFO) << "Starting web service..."; | ||
|
||
WebService::registerHandler("/path", [] { | ||
return new HttpClientHandler(); | ||
}); | ||
auto status = WebService::start(); | ||
ASSERT_TRUE(status.ok()) << status; | ||
} | ||
|
||
void TearDown() override { | ||
WebService::stop(); | ||
VLOG(1) << "Web service stopped"; | ||
} | ||
}; | ||
|
||
TEST(HttpClient, get) { | ||
{ | ||
auto url = folly::stringPrintf("http://%s:%d%s", FLAGS_ws_ip.c_str(), | ||
FLAGS_ws_http_port, "/path"); | ||
auto result = HttpClient::get(url); | ||
ASSERT_TRUE(result.ok()); | ||
ASSERT_EQ("HttpClientHandler successfully", result.value()); | ||
} | ||
{ | ||
auto url = folly::stringPrintf("http://%s:%d%s", FLAGS_ws_ip.c_str(), | ||
FLAGS_ws_http_port, "/not_exist"); | ||
auto result = HttpClient::get(url); | ||
ASSERT_TRUE(result.ok()); | ||
ASSERT_EQ("", result.value()); | ||
} | ||
} | ||
|
||
} // namespace http | ||
} // namespace nebula | ||
|
||
int main(int argc, char** argv) { | ||
testing::InitGoogleTest(&argc, argv); | ||
folly::init(&argc, &argv, true); | ||
google::SetStderrLogging(google::INFO); | ||
|
||
::testing::AddGlobalTestEnvironment(new nebula::http::HttpClientTestEnv()); | ||
return RUN_ALL_TESTS(); | ||
} |
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.