From cecba9186718337fed754e674d1a7b295ee401d2 Mon Sep 17 00:00:00 2001 From: uchenily Date: Wed, 5 Jun 2024 18:25:14 +0800 Subject: [PATCH] http: Decode(reader) --- examples/coro_curl.cpp | 3 ++- uvio/net/http/http_client.hpp | 2 +- uvio/net/http/http_frame.hpp | 12 +++++++----- uvio/net/http/http_protocol.hpp | 6 ++++-- uvio/net/http/http_server.hpp | 8 ++++---- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/examples/coro_curl.cpp b/examples/coro_curl.cpp index 9ac90df..e4f2e6d 100644 --- a/examples/coro_curl.cpp +++ b/examples/coro_curl.cpp @@ -12,7 +12,8 @@ using namespace uvio::net::http; auto client() -> Task<> { HttpRequest req = { .method = "GET", - .url = "http://httpbin.org/json", + // TODO(x) + .uri = "http://httpbin.org/json", }; auto ret = co_await HttpClient::request(req); if (!ret) { diff --git a/uvio/net/http/http_client.hpp b/uvio/net/http/http_client.hpp index 58423cd..5b34733 100644 --- a/uvio/net/http/http_client.hpp +++ b/uvio/net/http/http_client.hpp @@ -51,7 +51,7 @@ class HttpClient { CURLMOPT_TIMERDATA, this); - http_request(req.url); + http_request(req.uri); } void http_request(std::string_view url) { diff --git a/uvio/net/http/http_frame.hpp b/uvio/net/http/http_frame.hpp index f90ce5b..50dd21a 100644 --- a/uvio/net/http/http_frame.hpp +++ b/uvio/net/http/http_frame.hpp @@ -21,7 +21,7 @@ using BufferedWriter = TcpWriter; class HttpCodec : public Codec { public: template - auto decode(Reader &reader) -> Task> { + auto decode(Reader &reader) -> Task> { HttpRequest req; std::string request_line; if (auto ret = co_await reader.read_until(request_line, "\r\n"); !ret) { @@ -37,6 +37,8 @@ class HttpCodec : public Codec { LOG_DEBUG("method: {}", method); LOG_DEBUG("uri: {}", uri); LOG_DEBUG("version: {}", version); + req.method = method; + req.uri = uri; std::string request_headers; if (auto ret = co_await reader.read_until(request_headers, "\r\n\r\n"); @@ -47,6 +49,7 @@ class HttpCodec : public Codec { // Host: xxx // Content-Type: application/html auto headers = parse_headers(request_headers); + req.headers = headers; std::string body; @@ -58,9 +61,10 @@ class HttpCodec : public Codec { co_return unexpected{ret.error()}; } LOG_DEBUG("body: `{}`", body); + req.body = body; } - co_return body; + co_return req; } template @@ -188,9 +192,7 @@ class HttpFramed { [[REMEMBER_CO_AWAIT]] auto read_request() -> Task> { - auto result = co_await codec_.Decode(buffered_reader_); - // FIXME: Decode() -> HttpRequest? - co_return HttpRequest{.url = "/", .body = result.value()}; + co_return co_await codec_.Decode(buffered_reader_); } private: diff --git a/uvio/net/http/http_protocol.hpp b/uvio/net/http/http_protocol.hpp index 66b5d5f..b82db71 100644 --- a/uvio/net/http/http_protocol.hpp +++ b/uvio/net/http/http_protocol.hpp @@ -1,12 +1,14 @@ #pragma once +#include "uvio/net/http/http_util.hpp" #include namespace uvio::net::http { struct HttpRequest { - std::string method{"GET"}; - std::string url; + std::string method; + std::string uri; + HttpHeader headers; std::string body; }; diff --git a/uvio/net/http/http_server.hpp b/uvio/net/http/http_server.hpp index b118b40..2d1e02d 100644 --- a/uvio/net/http/http_server.hpp +++ b/uvio/net/http/http_server.hpp @@ -20,8 +20,8 @@ class HttpServer { , port_{port} {} public: - auto set_handler(std::string_view url, HandlerFunc &&func) { - map_handles_[url] = std::move(func); + auto set_handler(std::string_view uri, HandlerFunc &&func) { + map_handles_[uri] = std::move(func); } auto run() { @@ -47,10 +47,10 @@ class HttpServer { } auto request = std::move(req.value()); - LOG_DEBUG("request url: {} body: {}", request.url, request.body); + LOG_DEBUG("request uri: {} body: {}", request.uri, request.body); HttpResponse resp; - if (auto it = map_handles_.find(request.url); + if (auto it = map_handles_.find(request.uri); it != map_handles_.end()) { it->second(request, resp); resp.http_code = 200;