-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add http post feature for HttpClient #773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c2245a4
Change HttpClient to support http post
wuyunfeng e66df40
Change HttpClient to support http post
wuyunfeng 2cf86f8
Change HttpClient to support http post
wuyunfeng 63f8593
Add http post feature for HttpClient
wuyunfeng 92904e0
Add http post feature for HttpClient
wuyunfeng 513f710
Add http post feature for HttpClient
wuyunfeng 084082c
Add http post feature for HttpClient
wuyunfeng 017ac0c
Add http post feature for HttpClient
wuyunfeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -8,3 +8,5 @@ gensrc/build | |
| fe/target | ||
| thirdparty/src | ||
| *.so.tmp | ||
| .DS_Store | ||
| *.iml | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ | |
| #include "http/http_headers.h" | ||
| #include "http/http_method.h" | ||
| #include "http/utils.h" | ||
|
|
||
| #include "http/http_response.h" | ||
| namespace doris { | ||
|
|
||
| // Helper class to access HTTP resource | ||
|
|
@@ -54,6 +54,19 @@ class HttpClient { | |
| curl_easy_setopt(_curl, CURLOPT_PASSWORD, passwd.c_str()); | ||
| } | ||
|
|
||
| // content_type such as "application/json" | ||
| void set_content_type(const std::string content_type) { | ||
| std::string scratch_str = "Content-Type: " + content_type; | ||
| _header_list = curl_slist_append(_header_list, scratch_str.c_str()); | ||
| curl_easy_setopt(_curl, CURLOPT_HTTPHEADER, _header_list); | ||
| } | ||
|
|
||
| // you must set CURLOPT_POSTFIELDSIZE before CURLOPT_COPYPOSTFIELDS options, otherwise will cause request hanging up | ||
| void set_post_body(const std::string& post_body) { | ||
| curl_easy_setopt(_curl, CURLOPT_POSTFIELDSIZE, (long)post_body.length()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. post_body.data(), post_body.size() |
||
| curl_easy_setopt(_curl, CURLOPT_COPYPOSTFIELDS, post_body.c_str()); | ||
| } | ||
|
|
||
| // TODO(zc): support set header | ||
| // void set_header(const std::string& key, const std::string& value) { | ||
| // _cntl.http_request().SetHeader(key, value); | ||
|
|
@@ -85,6 +98,12 @@ class HttpClient { | |
| return cl; | ||
| } | ||
|
|
||
| long get_http_status() const { | ||
| long code; | ||
| curl_easy_getinfo(_curl, CURLINFO_RESPONSE_CODE, &code); | ||
| return code; | ||
| } | ||
|
|
||
| // execute a head method | ||
| Status head() { | ||
| set_method(HEAD); | ||
|
|
@@ -95,6 +114,8 @@ class HttpClient { | |
| // a file to local_path | ||
| Status download(const std::string& local_path); | ||
|
|
||
| Status execute_post_request(const std::string& post_data, std::string* response); | ||
|
|
||
| // execute a simple method, and its response is saved in response argument | ||
| Status execute(std::string* response); | ||
|
|
||
|
|
@@ -111,6 +132,7 @@ class HttpClient { | |
| using HttpCallback = std::function<bool(const void* data, size_t length)>; | ||
| const HttpCallback* _callback = nullptr; | ||
| char _error_buf[CURL_ERROR_SIZE]; | ||
| curl_slist *_header_list = nullptr; | ||
| }; | ||
|
|
||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -47,7 +47,28 @@ class HttpClientTestSimpleGetHandler : public HttpHandler { | |
| } | ||
| }; | ||
|
|
||
| class HttpClientTestSimplePostHandler : public HttpHandler { | ||
| public: | ||
| void handle(HttpRequest* req) override { | ||
| std::string user; | ||
| std::string passwd; | ||
| if (!parse_basic_auth(*req, &user, &passwd) || user != "test1") { | ||
| HttpChannel::send_basic_challenge(req, "abc"); | ||
| return; | ||
| } | ||
| if (req->method() == HttpMethod::POST) { | ||
| std::string post_body = req->get_request_body(); | ||
| if (!post_body.empty()) { | ||
| HttpChannel::send_reply(req, post_body); | ||
| } else { | ||
| HttpChannel::send_reply(req, "empty"); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| static HttpClientTestSimpleGetHandler s_simple_get_handler = HttpClientTestSimpleGetHandler(); | ||
| static HttpClientTestSimplePostHandler s_simple_post_handler = HttpClientTestSimplePostHandler(); | ||
| static EvHttpServer* s_server = nullptr; | ||
|
|
||
| class HttpClientTest : public testing::Test { | ||
|
|
@@ -59,6 +80,7 @@ class HttpClientTest : public testing::Test { | |
| s_server = new EvHttpServer(29386); | ||
| s_server->register_handler(GET, "/simple_get", &s_simple_get_handler); | ||
| s_server->register_handler(HEAD, "/simple_get", &s_simple_get_handler); | ||
| s_server->register_handler(POST, "/simple_post", &s_simple_post_handler); | ||
| s_server->start(); | ||
| } | ||
|
|
||
|
|
@@ -109,9 +131,24 @@ TEST_F(HttpClientTest, get_failed) { | |
| auto st = client.init("http://127.0.0.1:29386/simple_get"); | ||
| ASSERT_TRUE(st.ok()); | ||
| client.set_method(GET); | ||
| client.set_basic_auth("test1", ""); | ||
| std::string response; | ||
| st = client.execute(&response); | ||
| ASSERT_FALSE(st.ok()); | ||
| ASSERT_FALSE(!st.ok()); | ||
| } | ||
|
|
||
| TEST_F(HttpClientTest, post_normal) { | ||
| HttpClient client; | ||
| auto st = client.init("http://127.0.0.1:29386/simple_post"); | ||
| ASSERT_TRUE(st.ok()); | ||
| client.set_method(POST); | ||
| client.set_basic_auth("test1", ""); | ||
| std::string response; | ||
| std::string request_body = "simple post body query"; | ||
| st = client.execute_post_request(request_body, &response); | ||
| ASSERT_TRUE(st.ok()); | ||
| ASSERT_EQ(response.length(), request_body.length()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ASSERT_STRCMP(.c_str(), .c_str()) |
||
| ASSERT_STREQ(response.c_str(), request_body.c_str()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_header_list = nullptr