feat(workflow): example stats page with embedded Chart.js, inline form validation errors
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+30
-4
@@ -5,6 +5,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
|
||||
@@ -18,6 +19,11 @@ Server::Server(std::shared_ptr<Application> app) : app_(std::move(app)) {
|
||||
|
||||
Server::~Server() { stop(); }
|
||||
|
||||
void Server::set_stats(std::shared_ptr<Stats> stats) {
|
||||
stats_ = std::move(stats);
|
||||
if (stats_) stats_->set_connection_cap(caps::kMaxConnections);
|
||||
}
|
||||
|
||||
bool Server::start(const std::string& host, int port) {
|
||||
if (running_) return false;
|
||||
listen_fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
@@ -87,28 +93,44 @@ void Server::loop() {
|
||||
struct timeval tv{};
|
||||
tv.tv_sec = caps::kServerTimeoutSec;
|
||||
::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
handle_one(fd);
|
||||
if (stats_) stats_->connection_opened();
|
||||
std::size_t bytes_in = 0, bytes_out = 0;
|
||||
ConnEnd how = ConnEnd::Closed;
|
||||
handle_one(fd, bytes_in, bytes_out, how);
|
||||
::close(fd);
|
||||
if (stats_) stats_->connection_closed(how, bytes_in, bytes_out);
|
||||
++served;
|
||||
}
|
||||
// The accept loop stops at the cap; the listener stays open but unserved.
|
||||
if (stats_ && served >= caps::kMaxConnections) stats_->connection_rejected();
|
||||
}
|
||||
|
||||
void Server::handle_one(int fd) {
|
||||
void Server::handle_one(int fd, std::size_t& bytes_in,
|
||||
std::size_t& bytes_out, ConnEnd& how) {
|
||||
std::string raw;
|
||||
char buf[4096];
|
||||
while (raw.size() < caps::kMaxHeaderBytes) {
|
||||
ssize_t n = ::recv(fd, buf, sizeof(buf), 0);
|
||||
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
|
||||
how = ConnEnd::Timeout;
|
||||
if (n <= 0) break;
|
||||
bytes_in += (std::size_t)n;
|
||||
raw.append(buf, (std::size_t)n);
|
||||
if (raw.find("\r\n\r\n") != std::string::npos) break;
|
||||
}
|
||||
std::size_t hdr_end = raw.find("\r\n\r\n");
|
||||
if (hdr_end == std::string::npos) return;
|
||||
if (hdr_end == std::string::npos) {
|
||||
if (how == ConnEnd::Closed) how = ConnEnd::Malformed;
|
||||
return;
|
||||
}
|
||||
std::string head = raw.substr(0, hdr_end);
|
||||
std::string rest = raw.substr(hdr_end + 4);
|
||||
std::istringstream lines(head);
|
||||
std::string line;
|
||||
if (!std::getline(lines, line)) return;
|
||||
if (!std::getline(lines, line)) {
|
||||
how = ConnEnd::Malformed;
|
||||
return;
|
||||
}
|
||||
if (!line.empty() && line.back() == '\r') line.pop_back();
|
||||
std::istringstream rl(line);
|
||||
std::string method_s, target, version;
|
||||
@@ -148,7 +170,10 @@ void Server::handle_one(int fd) {
|
||||
while (req.body.size() < content_length &&
|
||||
req.body.size() < caps::kMaxRequestBody) {
|
||||
ssize_t n = ::recv(fd, buf, sizeof(buf), 0);
|
||||
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
|
||||
how = ConnEnd::Timeout;
|
||||
if (n <= 0) break;
|
||||
bytes_in += (std::size_t)n;
|
||||
req.body.append(buf, (std::size_t)n);
|
||||
}
|
||||
Response res = app_->dispatch(std::move(req));
|
||||
@@ -172,6 +197,7 @@ void Server::handle_one(int fd) {
|
||||
ssize_t n = ::send(fd, wire.data() + sent, wire.size() - sent, 0);
|
||||
if (n <= 0) break;
|
||||
sent += (std::size_t)n;
|
||||
bytes_out += (std::size_t)n;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user