Repository navigation
feat: add public Client core execution path #15
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
6 commits
Select commit
Hold shift + click to select a range
a95a89f
feat: add public Client interface
zuudevs 6c181fe
feat: implement Client request execution
zuudevs 0e0b1a0
build: compile Client execution path
zuudevs c54d86a
test: add end-to-end Client coverage
zuudevs ffca32b
test: wire Client integration tests
zuudevs a5a55c5
test: include system error explicitly
zuudevs 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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #pragma once | ||
|
|
||
| #include <chrono> | ||
| #include <string_view> | ||
|
|
||
| #include <cpp_request/request.hpp> | ||
| #include <cpp_request/response.hpp> | ||
| #include <cpp_request/result.hpp> | ||
|
|
||
| namespace cpp_request { | ||
|
|
||
| class Client final { | ||
| public: | ||
| Client() noexcept = default; | ||
|
|
||
| [[nodiscard]] Result<Response> request(const Request& request); | ||
|
|
||
| [[nodiscard]] Result<Response> get(std::string_view url); | ||
| [[nodiscard]] Result<Response> head(std::string_view url); | ||
| [[nodiscard]] Result<Response> post( | ||
| std::string_view url, | ||
| std::string_view body = {}); | ||
| [[nodiscard]] Result<Response> put( | ||
| std::string_view url, | ||
| std::string_view body = {}); | ||
| [[nodiscard]] Result<Response> patch( | ||
| std::string_view url, | ||
| std::string_view body = {}); | ||
| [[nodiscard]] Result<Response> del(std::string_view url); | ||
|
|
||
| void set_connect_timeout(std::chrono::milliseconds timeout) noexcept { | ||
| connect_timeout_ = timeout; | ||
| } | ||
|
|
||
| void set_read_timeout(std::chrono::milliseconds timeout) noexcept { | ||
| read_timeout_ = timeout; | ||
| } | ||
|
|
||
| void set_write_timeout(std::chrono::milliseconds timeout) noexcept { | ||
| write_timeout_ = timeout; | ||
| } | ||
|
|
||
| private: | ||
| std::chrono::milliseconds connect_timeout_{5000}; | ||
| std::chrono::milliseconds read_timeout_{30000}; | ||
| std::chrono::milliseconds write_timeout_{30000}; | ||
| }; | ||
|
|
||
| [[nodiscard]] Result<Response> get(std::string_view url); | ||
| [[nodiscard]] Result<Response> head(std::string_view url); | ||
| [[nodiscard]] Result<Response> post( | ||
| std::string_view url, | ||
| std::string_view body = {}); | ||
| [[nodiscard]] Result<Response> put( | ||
| std::string_view url, | ||
| std::string_view body = {}); | ||
| [[nodiscard]] Result<Response> patch( | ||
| std::string_view url, | ||
| std::string_view body = {}); | ||
| [[nodiscard]] Result<Response> del(std::string_view url); | ||
|
|
||
| } // namespace cpp_request |
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 |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| #include <cpp_request/client.hpp> | ||
|
|
||
| #include "http/request_serializer.hpp" | ||
| #include "http/response_parser.hpp" | ||
| #include "net/resolver.hpp" | ||
| #include "net/tcp_connection.hpp" | ||
|
|
||
| #include <array> | ||
| #include <chrono> | ||
| #include <cstddef> | ||
| #include <string_view> | ||
| #include <utility> | ||
|
|
||
| namespace cpp_request { | ||
| namespace { | ||
|
|
||
| [[nodiscard]] std::chrono::milliseconds remaining_timeout( | ||
| std::chrono::steady_clock::time_point deadline) noexcept { | ||
| const auto now = std::chrono::steady_clock::now(); | ||
| if (now >= deadline) { | ||
| return std::chrono::milliseconds{0}; | ||
| } | ||
|
|
||
| auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| deadline - now); | ||
| if (remaining.count() <= 0) { | ||
| remaining = std::chrono::milliseconds{1}; | ||
| } | ||
| return remaining; | ||
| } | ||
|
|
||
| [[nodiscard]] Request make_request( | ||
| Method method, | ||
| std::string_view url, | ||
| std::string_view body = {}) noexcept { | ||
| Request request{method, url}; | ||
| request.set_body(body); | ||
| return request; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| Result<Response> Client::request(const Request& request_value) { | ||
| auto serialized_result = detail::http::serialize_request(request_value); | ||
| if (!serialized_result) { | ||
| return serialized_result.error(); | ||
| } | ||
| auto serialized = std::move(serialized_result).value(); | ||
|
|
||
| auto endpoints_result = detail::net::Resolver::resolve( | ||
| serialized.url.host(), | ||
| serialized.url.port()); | ||
| if (!endpoints_result) { | ||
| return endpoints_result.error(); | ||
| } | ||
|
|
||
| auto connection_result = detail::net::TcpConnection::connect( | ||
| endpoints_result.value(), | ||
| connect_timeout_); | ||
| if (!connection_result) { | ||
| return connection_result.error(); | ||
| } | ||
| auto connection = std::move(connection_result).value(); | ||
|
|
||
| const auto write_deadline = std::chrono::steady_clock::now() + write_timeout_; | ||
|
|
||
| auto head_write = connection.write_all( | ||
| serialized.head, | ||
| remaining_timeout(write_deadline)); | ||
| if (!head_write) { | ||
| return head_write.error(); | ||
| } | ||
|
|
||
| if (!serialized.body.empty()) { | ||
| auto body_write = connection.write_all( | ||
| serialized.body, | ||
| remaining_timeout(write_deadline)); | ||
| if (!body_write) { | ||
| return body_write.error(); | ||
| } | ||
| } | ||
|
|
||
| detail::http::ResponseParser parser{request_value.method()}; | ||
| std::array<char, 16 * 1024> read_buffer{}; | ||
|
|
||
| while (!parser.complete()) { | ||
| auto read_result = connection.read_some( | ||
| read_buffer.data(), | ||
| read_buffer.size(), | ||
| read_timeout_); | ||
|
|
||
| if (!read_result) { | ||
| if (read_result.error().code == ErrorCode::ConnectionClosed) { | ||
| auto eof_result = parser.finish_eof(); | ||
| if (!eof_result) { | ||
| return eof_result.error(); | ||
| } | ||
| if (parser.complete()) { | ||
| break; | ||
| } | ||
| } | ||
| return read_result.error(); | ||
| } | ||
|
|
||
| auto parse_result = parser.feed(std::string_view{ | ||
| read_buffer.data(), | ||
| read_result.value()}); | ||
| if (!parse_result) { | ||
| return parse_result.error(); | ||
| } | ||
| } | ||
|
|
||
| return parser.take_response(); | ||
| } | ||
|
|
||
| Result<Response> Client::get(std::string_view url) { | ||
| return request(make_request(Method::Get, url)); | ||
| } | ||
|
|
||
| Result<Response> Client::head(std::string_view url) { | ||
| return request(make_request(Method::Head, url)); | ||
| } | ||
|
|
||
| Result<Response> Client::post(std::string_view url, std::string_view body) { | ||
| return request(make_request(Method::Post, url, body)); | ||
| } | ||
|
|
||
| Result<Response> Client::put(std::string_view url, std::string_view body) { | ||
| return request(make_request(Method::Put, url, body)); | ||
| } | ||
|
|
||
| Result<Response> Client::patch(std::string_view url, std::string_view body) { | ||
| return request(make_request(Method::Patch, url, body)); | ||
| } | ||
|
|
||
| Result<Response> Client::del(std::string_view url) { | ||
| return request(make_request(Method::Delete, url)); | ||
| } | ||
|
|
||
| Result<Response> get(std::string_view url) { | ||
| Client client; | ||
| return client.get(url); | ||
| } | ||
|
|
||
| Result<Response> head(std::string_view url) { | ||
| Client client; | ||
| return client.head(url); | ||
| } | ||
|
|
||
| Result<Response> post(std::string_view url, std::string_view body) { | ||
| Client client; | ||
| return client.post(url, body); | ||
| } | ||
|
|
||
| Result<Response> put(std::string_view url, std::string_view body) { | ||
| Client client; | ||
| return client.put(url, body); | ||
| } | ||
|
|
||
| Result<Response> patch(std::string_view url, std::string_view body) { | ||
| Client client; | ||
| return client.patch(url, body); | ||
| } | ||
|
|
||
| Result<Response> del(std::string_view url) { | ||
| Client client; | ||
| return client.del(url); | ||
| } | ||
|
|
||
| } // namespace cpp_request | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
When a close-delimited response is interrupted by
ECONNRESET/WSAECONNRESET,TcpConnection::read_some()reportsConnectionClosedwith a nonzero native error, so this branch callsfinish_eof()and returns the partially received body as a successful response. Only an orderlyrecv() == 0closure is valid EOF; reset-style closures must remain errors to avoid silently accepting truncated response data.Useful? React with 👍 / 👎.