ResponseData.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // Created by xcbosa on 2023/1/28.
  3. //
  4. #include "ResponseData.h"
  5. namespace xc {
  6. namespace utils {
  7. ResponseData::ResponseData(int statusCode, string body): headers() {
  8. this->statusCode = statusCode;
  9. this->body = body;
  10. this->headers["Server"] = "XCHttpServer";
  11. this->headers["Transfer-Encoding"] = "chunked";
  12. this->headers["Content-Type"] = "text/html";
  13. }
  14. ResponseData::ResponseData(int statusCode, string body, string contentType):
  15. ResponseData(statusCode, body) {
  16. this->headers["Content-Type"] = contentType;
  17. }
  18. void ResponseData::setHeader(string headerName, string value) {
  19. this->headers[headerName] = value;
  20. }
  21. void ResponseData::removeHeader(string headerName) {
  22. this->headers[headerName] = nullptr;
  23. }
  24. string ResponseData::getHeader(string headerName) {
  25. return this->headers[headerName];
  26. }
  27. void ResponseData::setStatusCode(int statusCode) {
  28. this->statusCode = statusCode;
  29. }
  30. void ResponseData::setContentType(string mimeType) {
  31. this->setHeader("Content-Type", mimeType);
  32. }
  33. int ResponseData::getStatusCode() {
  34. return this->statusCode;
  35. }
  36. void ResponseData::setBody(string body) {
  37. this->body = body;
  38. }
  39. string ResponseData::getBody() {
  40. return this->body;
  41. }
  42. void ResponseData::writeTo(::FILE *fp) {
  43. ::fprintf(fp, "HTTP/1.0 %d FRPCWebUI\r\n", this->statusCode);
  44. for (auto item : this->headers) {
  45. ::fprintf(fp, "%s: %s\r\n", item.first.c_str(), item.second.c_str());
  46. }
  47. ::fputs("\r\n", fp);
  48. ::fputs(this->body.c_str(), fp);
  49. ::fflush(fp);
  50. }
  51. } // xc
  52. } // utils