Skip to content

Commit

Permalink
http: Decode<HttpRequest>(reader)
Browse files Browse the repository at this point in the history
  • Loading branch information
uchenily committed Jun 5, 2024
1 parent 7c00744 commit cecba91
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion examples/coro_curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion uvio/net/http/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class HttpClient {
CURLMOPT_TIMERDATA,
this);

http_request(req.url);
http_request(req.uri);
}

void http_request(std::string_view url) {
Expand Down
12 changes: 7 additions & 5 deletions uvio/net/http/http_frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using BufferedWriter = TcpWriter;
class HttpCodec : public Codec<HttpCodec> {
public:
template <typename Reader>
auto decode(Reader &reader) -> Task<Result<std::string>> {
auto decode(Reader &reader) -> Task<Result<HttpRequest>> {
HttpRequest req;
std::string request_line;
if (auto ret = co_await reader.read_until(request_line, "\r\n"); !ret) {
Expand All @@ -37,6 +37,8 @@ class HttpCodec : public Codec<HttpCodec> {
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");
Expand All @@ -47,6 +49,7 @@ class HttpCodec : public Codec<HttpCodec> {
// Host: xxx
// Content-Type: application/html
auto headers = parse_headers(request_headers);
req.headers = headers;

std::string body;

Expand All @@ -58,9 +61,10 @@ class HttpCodec : public Codec<HttpCodec> {
co_return unexpected{ret.error()};
}
LOG_DEBUG("body: `{}`", body);
req.body = body;
}

co_return body;
co_return req;
}

template <typename Writer>
Expand Down Expand Up @@ -188,9 +192,7 @@ class HttpFramed {

[[REMEMBER_CO_AWAIT]]
auto read_request() -> Task<Result<HttpRequest>> {
auto result = co_await codec_.Decode<std::string>(buffered_reader_);
// FIXME: Decode() -> HttpRequest?
co_return HttpRequest{.url = "/", .body = result.value()};
co_return co_await codec_.Decode<HttpRequest>(buffered_reader_);
}

private:
Expand Down
6 changes: 4 additions & 2 deletions uvio/net/http/http_protocol.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include "uvio/net/http/http_util.hpp"
#include <string>

namespace uvio::net::http {

struct HttpRequest {
std::string method{"GET"};
std::string url;
std::string method;
std::string uri;
HttpHeader headers;
std::string body;
};

Expand Down
8 changes: 4 additions & 4 deletions uvio/net/http/http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down

0 comments on commit cecba91

Please sign in to comment.