TextResponseData.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "TextResponseData.h"
  5. #include "../webuiconf.h"
  6. using namespace std;
  7. namespace xc {
  8. namespace utils {
  9. TextResponseData::TextResponseData(int statusCode, string body): headers() {
  10. this->statusCode = statusCode;
  11. this->body = body;
  12. this->headers["Server"] = "XCHttpServer";
  13. this->headers["Transfer-Encoding"] = "chunked";
  14. this->headers["Content-Type"] = "text/html";
  15. this->headers["keepalive"] = "false";
  16. }
  17. TextResponseData::TextResponseData(int statusCode, string body, string contentType):
  18. TextResponseData(statusCode, body) {
  19. this->headers["Content-Type"] = contentType;
  20. }
  21. void TextResponseData::setHeader(string headerName, string value) {
  22. this->headers[headerName] = value;
  23. }
  24. void TextResponseData::removeHeader(string headerName) {
  25. this->headers[headerName] = nullptr;
  26. }
  27. string TextResponseData::getHeader(string headerName) {
  28. return this->headers[headerName];
  29. }
  30. void TextResponseData::setStatusCode(int statusCode) {
  31. this->statusCode = statusCode;
  32. }
  33. void TextResponseData::setContentType(string mimeType) {
  34. this->setHeader("Content-Type", mimeType);
  35. }
  36. int TextResponseData::getStatusCode() const {
  37. return this->statusCode;
  38. }
  39. void TextResponseData::setBody(string body) {
  40. this->body = body;
  41. }
  42. string TextResponseData::getBody() {
  43. return this->body;
  44. }
  45. void TextResponseData::writeTo(::FILE *fp) const {
  46. ::fprintf(fp, "HTTP/1.1 %d XCHttpServer\r\n", this->statusCode);
  47. if (!this->cookies.empty()) {
  48. for (auto it : this->cookies) {
  49. ostringstream oss;
  50. oss << it.first;
  51. oss << "=";
  52. oss << it.second;
  53. string str = oss.str();
  54. ::fprintf(fp, "%s: %s\r\n", "Set-Cookie", str.c_str());
  55. }
  56. }
  57. for (auto item : this->headers) {
  58. ::fprintf(fp, "%s: %s\r\n", item.first.c_str(), item.second.c_str());
  59. }
  60. ::fputs("\r\n", fp);
  61. long mtu = conf::mtu;
  62. const char *data = this->body.c_str();
  63. const char *cursor = data;
  64. long dataSize = ::strlen(data);
  65. const char *endNextCursor = data + ::strlen(data);
  66. int writeTimes = dataSize / mtu;
  67. if (dataSize % mtu) {
  68. writeTimes++;
  69. }
  70. for (int i = 0; i < writeTimes; i++) {
  71. long writeSize = min((long)mtu, endNextCursor - cursor);
  72. ::fprintf(fp, "%x\r\n", writeSize);
  73. ::fwrite(cursor, 1, writeSize, fp);
  74. ::fprintf(fp, "\r\n");
  75. cursor += writeSize;
  76. }
  77. ::fprintf(fp, "0\r\n\r\n\r\n");
  78. ::fflush(fp);
  79. }
  80. void TextResponseData::writeResponseBodyTo(ostream &fp) const {
  81. fp << this->body;
  82. }
  83. void TextResponseData::addCookie(string key, string value) {
  84. this->cookies[key] = value;
  85. }
  86. } // xc
  87. } // utils