http header date server
This commit is contained in:
parent
6d3941ddf2
commit
4a41b678c3
@ -1,6 +1,5 @@
|
||||
#include <webserv/http/HttpResponse.hpp>
|
||||
|
||||
#include <webserv/http/HttpConstants.hpp> // for getStatusCodeReason
|
||||
#include <webserv/http/HttpResponse.hpp>
|
||||
|
||||
#include <string> // for basic_string, operator+, string, char_traits, to_string
|
||||
#include <vector> // for vector
|
||||
@ -54,11 +53,21 @@ const HttpHeaders &HttpResponse::getHeaders() const noexcept
|
||||
return *headers_;
|
||||
}
|
||||
|
||||
std::string HttpResponse::getContentLength() const
|
||||
std::string HttpResponse::getContentLengthHeader() const
|
||||
{
|
||||
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::string headerStr;
|
||||
@ -67,7 +76,11 @@ std::vector<uint8_t> HttpResponse::toBytes() const
|
||||
reason = Http::getStatusCodeReason(statusCode_);
|
||||
|
||||
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();
|
||||
|
||||
std::vector<uint8_t> responseData(headerStr.begin(), headerStr.end());
|
||||
|
||||
@ -42,7 +42,8 @@ class HttpResponse
|
||||
|
||||
private:
|
||||
[[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::unique_ptr<HttpHeaders> headers_;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user