123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- //
- // Created by xcbosa on 2023/1/28.
- //
- #include "TextResponseData.h"
- #include "../webuiconf.h"
- using namespace std;
- namespace xc {
- namespace utils {
- TextResponseData::TextResponseData(int statusCode, string body): headers() {
- this->statusCode = statusCode;
- this->body = body;
- this->headers["Server"] = "XCHttpServer";
- this->headers["Transfer-Encoding"] = "chunked";
- this->headers["Content-Type"] = "text/html";
- this->headers["keepalive"] = "false";
- }
- TextResponseData::TextResponseData(int statusCode, string body, string contentType):
- TextResponseData(statusCode, body) {
- this->headers["Content-Type"] = contentType;
- }
- void TextResponseData::setHeader(string headerName, string value) {
- this->headers[headerName] = value;
- }
- void TextResponseData::removeHeader(string headerName) {
- this->headers[headerName] = nullptr;
- }
- string TextResponseData::getHeader(string headerName) {
- return this->headers[headerName];
- }
- void TextResponseData::setStatusCode(int statusCode) {
- this->statusCode = statusCode;
- }
- void TextResponseData::setContentType(string mimeType) {
- this->setHeader("Content-Type", mimeType);
- }
- int TextResponseData::getStatusCode() const {
- return this->statusCode;
- }
- void TextResponseData::setBody(string body) {
- this->body = body;
- }
- string TextResponseData::getBody() {
- return this->body;
- }
- void TextResponseData::writeTo(::FILE *fp) const {
- ::fprintf(fp, "HTTP/1.1 %d XCHttpServer\r\n", this->statusCode);
- if (!this->cookies.empty()) {
- for (auto it : this->cookies) {
- ostringstream oss;
- oss << it.first;
- oss << "=";
- oss << it.second;
- string str = oss.str();
- ::fprintf(fp, "%s: %s\r\n", "Set-Cookie", str.c_str());
- }
- }
- for (auto item : this->headers) {
- ::fprintf(fp, "%s: %s\r\n", item.first.c_str(), item.second.c_str());
- }
- ::fputs("\r\n", fp);
- long mtu = conf::mtu;
- const char *data = this->body.c_str();
- const char *cursor = data;
- long dataSize = ::strlen(data);
- const char *endNextCursor = data + ::strlen(data);
- int writeTimes = dataSize / mtu;
- if (dataSize % mtu) {
- writeTimes++;
- }
- for (int i = 0; i < writeTimes; i++) {
- long writeSize = min((long)mtu, endNextCursor - cursor);
- ::fprintf(fp, "%x\r\n", writeSize);
- ::fwrite(cursor, 1, writeSize, fp);
- ::fprintf(fp, "\r\n");
- cursor += writeSize;
- }
- ::fprintf(fp, "0\r\n\r\n\r\n");
- ::fflush(fp);
- }
- void TextResponseData::writeResponseBodyTo(ostream &fp) const {
- fp << this->body;
- }
- void TextResponseData::addCookie(string key, string value) {
- this->cookies[key] = value;
- }
- } // xc
- } // utils
|