-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseBuilder.cpp
More file actions
183 lines (153 loc) · 6.43 KB
/
Copy pathResponseBuilder.cpp
File metadata and controls
183 lines (153 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "ResponseBuilder.hpp"
#include "Utils.hpp"
#include <set>
ResponseBuilder::ResponseBuilder() {
reset();
}
ResponseBuilder::~ResponseBuilder() {
// No resources to clean up
}
ResponseBuilder& ResponseBuilder::setStatus(int code, const std::string& message) {
debug("[ResponseBuilder]: Setting status: " + Utils::to_string(code) + " " + message);
statusCode = code;
statusMessage = message;
return *this;
}
ResponseBuilder& ResponseBuilder::setHeader(const std::string& name, const std::string& value) {
debug("[ResponseBuilder]: Setting header: " + name + ": " + value);
//headers[name] = value;
headers.set(name, value);
return *this;
}
ResponseBuilder& ResponseBuilder::setContentType(const std::string& contentType) {
debug("[ResponseBuilder]: Setting Content-Type: " + contentType);
if (contentType.find("charset=") == std::string::npos &&
(contentType.find("text/html") != std::string::npos ||
contentType.find("text/plain") != std::string::npos)) {
return setHeader("Content-Type", contentType + "; charset=UTF-8");
} else {
return setHeader("Content-Type", contentType);
}
}
ResponseBuilder& ResponseBuilder::setBody(const std::string& newBody) {
debug("[ResponseBuilder]: Setting body with length: " + Utils::to_string(newBody.size()));
body = newBody;
setHeader("Content-Length", Utils::to_string(body.size()));
return *this;
}
std::string ResponseBuilder::buildOk(const std::string& responseBody, const std::string& contentType) {
debug("[ResponseBuilder]: Building 200 OK response");
reset();
setStatus(200, "OK");
setContentType(contentType);
setBody(responseBody);
return build();
}
std::string ResponseBuilder::buildError(int statusCode, const std::string& message) {
debug("[ResponseBuilder]: Building error response: " + Utils::to_string(statusCode));
reset();
setStatus(statusCode, getStatusText(statusCode));
// Create an HTML error message
std::string errorHtml = "<html><body><h1>" + Utils::to_string(statusCode) + " - " + message + "</h1></body></html>";
setContentType("text/html");
setBody(errorHtml);
return build();
}
std::string ResponseBuilder::buildRedirect(int statusCode, const std::string& location) {
debug("[ResponseBuilder]: Building redirect: " + Utils::to_string(statusCode) + " to " + location);
reset();
setStatus(statusCode, getStatusText(statusCode));
setHeader("Location", location);
setBody(""); // Empty body for redirects
return build();
}
std::string ResponseBuilder::buildFileResponse(const std::string& fileContent, const std::string& contentType) {
debug("[ResponseBuilder]: Building file response with content type: " + contentType);
// Start fresh
reset();
// Set basic response properties
setStatus(200, "OK");
setContentType(contentType);
// Calculate content length
std::string contentLength = Utils::to_string(fileContent.size());
setHeader("Content-Length", contentLength);
// Build header portion only
std::string response = "HTTP/1.1 200 OK\r\n";
// Add headers
const std::vector<std::pair<std::string, std::string> >& allHeaders = headers.getAll();
for (std::vector<std::pair<std::string, std::string> >::const_iterator it = allHeaders.begin();
it != allHeaders.end(); ++it) {
response += headers.getCanonicalHeaderName(it->first) + ": " + it->second + "\r\n";
}
// Add separator
response += "\r\n";
// For binary data, use a vector to safely concatenate the response headers with the file content
std::vector<char> fullResponse(response.begin(), response.end());
fullResponse.insert(fullResponse.end(), fileContent.begin(), fileContent.end());
return std::string(fullResponse.begin(), fullResponse.end());
}
std::string ResponseBuilder::build() {
debug("[ResponseBuilder]: Building complete HTTP response");
// Use a string instead of an ostringstream to avoid potential buffer issues
std::string response = "HTTP/1.1 " + Utils::to_string(statusCode) + " " + statusMessage + "\r\n";
// Headers
const std::vector<std::pair<std::string, std::string> >& allHeaders = headers.getAll();
for (std::vector<std::pair<std::string, std::string> >::const_iterator it = allHeaders.begin();
it != allHeaders.end(); ++it) {
response += headers.getCanonicalHeaderName(it->first) + ": " + it->second + "\r\n";
}
// Empty line separating headers from body
response += "\r\n";
// Body (if any)
if (!body.empty()) {
response += body;
}
return response;
}
void ResponseBuilder::reset() {
debug("[ResponseBuilder]: Resetting builder");
statusCode = 200;
statusMessage = "OK";
headers.clear();
// Set standard headers
headers.set("Server", "webserv/1.0"); // Add a server identifier
// Current date
char timeBuffer[100];
time_t now = time(NULL);
struct tm* tm_info = gmtime(&now);
strftime(timeBuffer, sizeof(timeBuffer), "%a, %d %b %Y %H:%M:%S GMT", tm_info);
headers.set("Date", timeBuffer);
headers.set("Connection", "keep-alive"); // Default to closing connection
tm_info = NULL;
body = "";
}
std::string ResponseBuilder::getStatusText(int code) {
switch (code) {
case 200: return "OK";
case 201: return "Created";
case 204: return "No Content";
case 301: return "Moved Permanently";
case 302: return "Found";
case 303: return "See Other";
case 307: return "Temporary Redirect";
case 308: return "Permanent Redirect";
case 400: return "Bad Request";
case 401: return "Unauthorized";
case 403: return "Forbidden";
case 404: return "Not Found";
case 405: return "Method Not Allowed";
case 408: return "Request Timeout";
case 413: return "Payload Too Large";
case 414: return "URI Too Long";
case 415: return "Unsupported Media Type";
case 429: return "Too Many Requests";
case 431: return "Request Header Fields Too Large";
case 500: return "Internal Server Error";
case 501: return "Not Implemented";
case 502: return "Bad Gateway";
case 503: return "Service Unavailable";
case 504: return "Gateway Timeout";
case 505: return "HTTP Version Not Supported";
default: return "Unknown Status";
}
}