http header date server

This commit is contained in:
whaffman 2025-10-30 15:56:30 +01:00
parent 6d3941ddf2
commit 4a41b678c3
2 changed files with 19 additions and 5 deletions

View File

@ -1,6 +1,5 @@
#include <webserv/http/HttpResponse.hpp>
#include <webserv/http/HttpConstants.hpp> // for getStatusCodeReason #include <webserv/http/HttpConstants.hpp> // for getStatusCodeReason
#include <webserv/http/HttpResponse.hpp>
#include <string> // for basic_string, operator+, string, char_traits, to_string #include <string> // for basic_string, operator+, string, char_traits, to_string
#include <vector> // for vector #include <vector> // for vector
@ -54,11 +53,21 @@ const HttpHeaders &HttpResponse::getHeaders() const noexcept
return *headers_; return *headers_;
} }
std::string HttpResponse::getContentLength() const std::string HttpResponse::getContentLengthHeader() const
{ {
return "Content-Length: " + std::to_string(body_.size()) + "\r\n"; return "Content-Length: " + std::to_string(body_.size()) + "\r\n";
} }
std::string HttpResponse::getDateHeader() const
{
time_t now = time(nullptr);
struct tm *gmt = gmtime(&now);
char buffer[100];
strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S GMT", gmt);
return "Date: " + std::string(buffer) + "\r\n";
}
std::vector<uint8_t> HttpResponse::toBytes() const std::vector<uint8_t> HttpResponse::toBytes() const
{ {
std::string headerStr; std::string headerStr;
@ -67,7 +76,11 @@ std::vector<uint8_t> HttpResponse::toBytes() const
reason = Http::getStatusCodeReason(statusCode_); reason = Http::getStatusCodeReason(statusCode_);
headerStr = "HTTP/1.1 " + std::to_string(statusCode_) + " " + reason + "\r\n"; // todo: status line headerStr = "HTTP/1.1 " + std::to_string(statusCode_) + " " + reason + "\r\n"; // todo: status line
headerStr += getContentLength(); headerStr += getContentLengthHeader();
headerStr += getDateHeader();
headerStr += "Connection: close\r\n";
headerStr += "Server: Webserv/1.0\r\n";
headerStr += headers_->toString(); headerStr += headers_->toString();
std::vector<uint8_t> responseData(headerStr.begin(), headerStr.end()); std::vector<uint8_t> responseData(headerStr.begin(), headerStr.end());

View File

@ -42,7 +42,8 @@ class HttpResponse
private: private:
[[nodiscard]] std::string getStatusLine() const; [[nodiscard]] std::string getStatusLine() const;
[[nodiscard]] std::string getContentLength() const; [[nodiscard]] std::string getContentLengthHeader() const;
[[nodiscard]] std::string getDateHeader() const;
std::vector<uint8_t> body_; std::vector<uint8_t> body_;
std::unique_ptr<HttpHeaders> headers_; std::unique_ptr<HttpHeaders> headers_;