Webserv
Loading...
Searching...
No Matches
Response.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "http/Headers.hpp"
4#include "http/HttpStatus.hpp"
5#include <vector>
6
7namespace http {
8
9// Forward-declare the body interface
10class IResponseBody;
11
17public:
19
20 std::string protocol;
22 std::string reasonPhrase;
23};
24
33class Response {
34public:
35 Response();
36 ~Response();
37
43 Response &clear();
44
45 // --- Body Management ---
46
51 IResponseBody *body() const;
52
58 Response &setNoBody(bool removeContentHeaders = true);
59
67 Response &setBodyInMemory(std::string const &data, std::string const &contentType);
68
76 Response &setBodyFromFile(std::string const &fpath, std::string const &contentType);
77
85 Response &setBodyFromCgi(int pipeFd, bool hasHeaderParsing);
86
87 // --- Header & Status Management ---
88
93
97 Headers const &headers() const;
98
104 void buildHeaders(std::vector<char> &buffer, bool addBodyLine = true) const;
105
111 Response &status(HttpStatus statusCode);
112
114 Response &status(HttpStatus statusCode, std::string const &customMessage);
115
119 HttpStatus status() const;
120
124 std::string const &protocol() const;
125
129 std::string const &reasonPhrase() const;
130
134 std::string const &customMessage() const;
135
136private:
138 Response const &operator=(Response const &);
139
144
148 static char const *getReasonPhrase_(HttpStatus status);
149
153 std::string customError_;
154};
155
156} // namespace http
Manages a collection of HTTP headers.
Definition Headers.hpp:17
An abstract interface for all HTTP response body sources.
Definition ResponseBody.hpp:16
A simple data aggregate for the HTTP response status line.
Definition Response.hpp:16
ResponseStartLine()
Definition Response.cpp:8
std::string protocol
The HTTP protocol version (e.g., "HTTP/1.1").
Definition Response.hpp:20
std::string reasonPhrase
The human-readable reason (e.g., "OK", "Not Found").
Definition Response.hpp:22
HttpStatus statusCode
The numerical status code (e.g., 200, 404).
Definition Response.hpp:21
void cleanupBody_()
Internal: safely deletes the current body object.
Headers headers_
Definition Response.hpp:151
Headers & headers()
Gets a mutable reference to the response headers.
Definition Response.cpp:75
Response & clear()
Resets the response to a default state, ready for reuse. Cleans up any existing body and resets heade...
Definition Response.cpp:68
Response & setNoBody(bool removeContentHeaders=true)
Sets the response to have no body. Cleans up any existing body.
Definition Response.cpp:14
~Response()
Definition Response.cpp:11
Response & setBodyFromFile(std::string const &fpath, std::string const &contentType)
Sets the response body to stream from a file. Creates a FileBody object to manage the file descriptor...
Definition Response.cpp:32
static char const * getReasonPhrase_(HttpStatus status)
Internal: maps a Status enum to its standard string.
std::string const & reasonPhrase() const
Gets the current reason phrase (e.g., "Not Found").
Definition Response.cpp:79
HttpStatus status() const
Gets the current status code.
Definition Response.cpp:77
Response const & operator=(Response const &)
Response(Response const &)
ResponseStartLine startLine_
Definition Response.hpp:150
std::string const & customMessage() const
Gets the custom error message, if any.
Definition Response.cpp:80
Response & setBodyInMemory(std::string const &data, std::string const &contentType)
Sets the response body from an in-memory string. This takes ownership of the data (by copying) and se...
Definition Response.cpp:24
IResponseBody * body() const
Gets a pointer to the (read-only) response body.
Definition Response.cpp:13
Response & status(HttpStatus statusCode)
Sets the response status and automatically syncs the reason phrase.
Definition Response.cpp:61
Response & setBodyFromCgi(int pipeFd, bool hasHeaderParsing)
Sets the response body to stream from a CGI pipe.
Definition Response.cpp:40
std::string customError_
Definition Response.hpp:153
void buildHeaders(std::vector< char > &buffer, bool addBodyLine=true) const
Appends the fully formatted start-line and headers to a buffer.
Definition Response.cpp:46
IResponseBody * body_
Definition Response.hpp:152
Response()
Definition Response.cpp:10
std::string const & protocol() const
Gets the current protocol string (e.g., "HTTP/1.1").
Definition Response.cpp:78
Definition IArgument.hpp:5
HttpStatus
Definition HttpStatus.hpp:11