COROIO: coroio/http/httpd.hpp Source File
COROIO
 
Loading...
Searching...
No Matches
httpd.hpp
1#pragma once
2
3#include <coroio/socket.hpp>
5#include <coroio/sockutils.hpp>
6
7#include <chrono>
8#include <ctime>
9#include <iomanip>
10#include <sstream>
11#include <cstdio>
12#include <cstdlib>
13
14namespace NNet
15{
16
17// /path?arg1=value1&arg2=value2#fragment
18class TUri {
19public:
20 TUri() = default;
21 TUri(const std::string& uriStr);
22 const std::string& Path() const;
23 const std::map<std::string, std::string>& QueryParameters() const;
24 const std::string& Fragment() const;
25
26private:
27 void Parse(const std::string& uriStr);
28 std::string Path_;
29 std::map<std::string, std::string> QueryParameters_;
30 std::string Fragment_;
31};
32
33class TRequest {
34public:
35 TRequest(std::string&& header,
36 std::function<TFuture<ssize_t>(char*, size_t)> bodyReader,
37 std::function<TFuture<std::string>()> chunkHeaderReader = {});
38 std::string_view Method() const;
39 const TUri& Uri() const;
40 std::string_view Version() const { return Version_; }
41
42 bool HasBody() const;
43 TFuture<std::string> ReadBodyFull();
44 TFuture<ssize_t> ReadBodySome(char* buffer, size_t size); // read up to Content-Length
45 bool BodyConsumed() const;
46 bool RequireConnectionClose() const;
47
48private:
49 void ParseRequestLine();
50 void ParseHeaders();
51
52 TFuture<ssize_t> ReadBodySomeContentLength(char* buffer, size_t size);
53 TFuture<ssize_t> ReadBodySomeChunked(char* buffer, size_t size);
54
55 std::string Header_;
56 size_t HeaderStartPos_ = 0;
57 std::map<std::string_view, std::string_view> Headers_;
58 size_t ContentLength_ = 0;
59 bool HasBody_ = false;
60 bool Chunked_ = false;
61 bool BodyConsumed_ = false;
62 size_t CurrentChunkSize_ = 0;
63 std::function<TFuture<ssize_t>(char*, size_t)> BodyReader_;
64 std::function<TFuture<std::string>()> ChunkHeaderReader_;
65 std::string_view Method_;
66 TUri Uri_;
67 std::string_view Version_;
68};
69
70class TResponse {
71public:
72 TResponse(std::function<TFuture<ssize_t>(const void*, size_t)> writer)
73 : Writer_(std::move(writer))
74 {}
75 void SetStatus(int statusCode);
76 void SetHeader(const std::string& name, const std::string& value);
77 TFuture<void> SendHeaders();
78 TFuture<void> WriteBodyChunk(const char* data, size_t size); // Chunked transfer encoding
79 TFuture<void> WriteBodyFull(const std::string& data); // Content-Length + body
80 bool IsClosed() const;
81 int StatusCode() const {
82 return StatusCode_;
83 }
84
85private:
86 TFuture<void> CompleteWrite(const char* data, size_t size);
87
88 int StatusCode_ = 200;
89 std::map<std::string, std::string> Headers_;
90 bool HeadersSent_ = false;
91 bool Chunked_ = false;
92 bool IsClosed_ = false;
93 std::function<TFuture<ssize_t>(const void*, size_t)> Writer_;
94};
95
96struct IRouter {
97 virtual TFuture<void> HandleRequest(const TRequest& request, TResponse& response) = 0;
98};
99
101public:
102 TFuture<void> HandleRequest(const TRequest& request, TResponse& response) override {
103 if (request.Uri().Path() == "/") {
104 response.SetStatus(200);
105 response.SetHeader("Content-Type", "text/plain");
106 response.SetHeader("Connection", "close");
107 co_await response.SendHeaders();
108 co_await response.WriteBodyFull("Hello, World!");
109 } else {
110 response.SetStatus(404);
111 response.SetHeader("Content-Type", "text/plain");
112 response.SetHeader("Connection", "close");
113 co_await response.SendHeaders();
114 co_await response.WriteBodyFull("Not Found");
115 }
116 }
117};
118
119template<typename TSocket>
121public:
122 TWebServer(TSocket&& serverSocket, IRouter& router, std::function<void(const std::string&)> logger = {})
123 : ServerSocket(std::move(serverSocket))
124 , Router(router)
125 , Logger(std::move(logger))
126 {}
127
128 TVoidTask Start() {
129 while (true) {
130 auto clientSocket = co_await ServerSocket.Accept();
131 HandleClient(std::move(clientSocket));
132 }
133 }
134
135private:
136 TVoidTask HandleClient(TSocket clientSocket) {
137 auto byteReader = TByteReader<TSocket>(clientSocket);
138
139 auto bodyReader = [&](char* buffer, size_t size) -> TFuture<ssize_t> {
140 co_return co_await byteReader.ReadSome(buffer, size);
141 };
142
143 auto chunkHeaderReader = [&]() -> TFuture<std::string> {
144 co_return co_await byteReader.ReadUntil("\r\n");
145 };
146
147 auto bodyWriter = [&](const void* data, size_t size) -> TFuture<ssize_t> {
148 co_return co_await clientSocket.WriteSome(data, size);
149 };
150
151 std::string clientString = clientSocket.RemoteAddr() ? clientSocket.RemoteAddr()->ToString() : "unknown";
152
153 try {
154 while (true) {
155 auto header = co_await byteReader.ReadUntil("\r\n\r\n");
156 TRequest request(std::move(header), bodyReader, chunkHeaderReader);
157 TResponse response(bodyWriter);
158 co_await Router.HandleRequest(request, response);
159 Log(request, response, clientString);
160 if (response.IsClosed() || request.RequireConnectionClose()) {
161 break;
162 }
163 }
164 } catch (const std::exception& ex) {
165 if (Logger) {
166 Logger(std::string("Client handler exception: ") + ex.what());
167 }
168 }
169 co_return;
170 }
171
172 void Log(const TRequest& request, const TResponse& response, const std::string& clientString) {
173 if (!Logger) {
174 return;
175 }
176 LogStream.str("");
177 LogStream.clear();
178
179 // Build full path with query parameters if any
180 std::string fullPath = request.Uri().Path();
181 const auto& qp = request.Uri().QueryParameters();
182 if (!qp.empty()) {
183 fullPath.push_back('?');
184 bool first = true;
185 for (const auto& [k,v] : qp) {
186 if (!first) fullPath.push_back('&');
187 first = false;
188 fullPath.append(k);
189 fullPath.push_back('=');
190 fullPath.append(v);
191 }
192 }
193
194 // Timestamp in nginx style: [08/Nov/2025:15:23:10 +0100]
195 auto now = std::chrono::system_clock::now();
196 std::time_t raw = std::chrono::system_clock::to_time_t(now);
197 std::tm localTm{};
198 std::tm gmTm{};
199#if defined(_WIN32)
200 localtime_s(&localTm, &raw);
201 gmtime_s(&gmTm, &raw);
202#else
203 localtime_r(&raw, &localTm);
204 gmtime_r(&raw, &gmTm);
205#endif
206 // Compute timezone offset
207 // mktime converts tm in local time to time_t; difference gives offset vs UTC
208 std::time_t localTime = std::mktime(&localTm);
209 std::time_t gmTime = std::mktime(&gmTm);
210 long tzOffsetSec = static_cast<long>(difftime(localTime, gmTime));
211 int tzSign = tzOffsetSec >= 0 ? 1 : -1;
212 tzOffsetSec = std::labs(tzOffsetSec);
213 int tzHours = static_cast<int>(tzOffsetSec / 3600);
214 int tzMins = static_cast<int>((tzOffsetSec % 3600) / 60);
215 char tzBuf[8];
216 std::snprintf(tzBuf, sizeof(tzBuf), "%c%02d%02d", tzSign > 0 ? '+' : '-', tzHours, tzMins);
217
218 // Prepare HTTP version string
219 std::string_view ver = request.Version();
220 bool hasHttpPrefix = ver.size() >= 5 && ver.substr(0,5) == "HTTP/";
221
222 LogStream << clientString << " - - [" << std::put_time(&localTm, "%d/%b/%Y:%H:%M:%S ") << tzBuf << "] \""
223 << request.Method() << ' ' << fullPath << ' ' << (hasHttpPrefix ? std::string(ver) : (std::string("HTTP/") + std::string(ver))) << "\" "
224 << response.StatusCode() << ' ' << '-' << ' ' << "\"-\" \"-\"";
225 Logger(LogStream.str());
226 }
227
228 TSocket ServerSocket;
229 IRouter& Router;
230 std::function<void(const std::string&)> Logger;
231 std::ostringstream LogStream;
232};
233
234} // namespace NNet {
Definition httpd.hpp:100
Definition httpd.hpp:33
Definition httpd.hpp:70
auto WriteSome(const void *buf, size_t size)
Asynchronously writes data from the provided buffer to the socket.
Definition socket.hpp:186
High-level asynchronous socket for network communication.
Definition socket.hpp:364
const std::optional< TAddress > & RemoteAddr() const
Returns the remote address of the connected peer.
Definition socket.cpp:109
auto Accept()
Asynchronously accepts an incoming connection.
Definition socket.hpp:453
Definition httpd.hpp:18
Definition httpd.hpp:120
Implementation of a promise/future system for coroutines.
Definition httpd.hpp:96
A utility for reading data from a socket-like object, either a fixed number of bytes or until a speci...
Definition sockutils.hpp:64
Future type for coroutines returning a value of type T.
Definition corochain.hpp:182
Definition promises.hpp:9