#include "HTTPResponse.h" #include namespace coreutils { HTTPResponse::HTTPResponse() { statusText = "OK"; } void HTTPResponse::setStatus(int code, ZString text) { statusCode = code; statusText = text; } void HTTPResponse::setHeader(ZString name, ZString value) { headers[MString(name)] = MString(value); } void HTTPResponse::setBody(ZString bodyIn) { body = bodyIn; } MString HTTPResponse::serialize() { bool hasContentLength = false; for (auto &pair : headers) { MString key = pair.first; if (strcasecmp(key.c_str(), "Content-Length") == 0) hasContentLength = true; } MString out; out << "HTTP/1.1 "; out << MString(std::to_string(statusCode)); out << " "; out << statusText; out << "\r\n"; for (auto &pair : headers) { out << pair.first; out << ": "; out << pair.second; out << "\r\n"; } if (!hasContentLength) { out << "Content-Length: "; out << MString(std::to_string(body.getLength())); out << "\r\n"; } out << "\r\n"; out << body; return out; } }