HttpResponseWriter.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef HV_HTTP_RESPONSE_WRITER_H_
  2. #define HV_HTTP_RESPONSE_WRITER_H_
  3. #include "Channel.h"
  4. #include "HttpMessage.h"
  5. namespace hv {
  6. class HttpResponseWriter : public SocketChannel {
  7. public:
  8. HttpResponsePtr response;
  9. enum State {
  10. SEND_BEGIN,
  11. SEND_HEADER,
  12. SEND_BODY,
  13. SEND_END,
  14. } state;
  15. HttpResponseWriter(hio_t* io, const HttpResponsePtr& resp)
  16. : SocketChannel(io)
  17. , response(resp)
  18. , state(SEND_BEGIN)
  19. {}
  20. ~HttpResponseWriter() {}
  21. // Begin -> End
  22. // Begin -> WriteResponse -> End
  23. // Begin -> WriteStatus -> WriteHeader -> WriteBody -> End
  24. // Begin -> WriteHeader -> EndHeaders -> WriteBody -> WriteBody -> ... -> End
  25. int Begin() {
  26. state = SEND_BEGIN;
  27. return 0;
  28. }
  29. int WriteStatus(http_status status_codes) {
  30. response->status_code = status_codes;
  31. return 0;
  32. }
  33. int WriteHeader(const char* key, const char* value) {
  34. response->headers[key] = value;
  35. return 0;
  36. }
  37. template<typename T>
  38. int WriteHeader(const char* key, T num) {
  39. response->headers[key] = hv::to_string(num);
  40. return 0;
  41. }
  42. int EndHeaders(const char* key = NULL, const char* value = NULL) {
  43. if (state != SEND_BEGIN) return -1;
  44. if (key && value) {
  45. response->headers[key] = value;
  46. }
  47. std::string headers = response->Dump(true, false);
  48. state = SEND_HEADER;
  49. return write(headers);
  50. }
  51. int WriteBody(const char* buf, int len = -1) {
  52. if (len == -1) len = strlen(buf);
  53. if (state == SEND_BEGIN) {
  54. response->body.append(buf, len);
  55. return len;
  56. } else {
  57. state = SEND_BODY;
  58. return write(buf, len);
  59. }
  60. }
  61. int WriteBody(const std::string& str) {
  62. return WriteBody(str.c_str(), str.size());
  63. }
  64. int WriteResponse(HttpResponse* resp) {
  65. if (resp == NULL) {
  66. response->status_code = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  67. return 0;
  68. }
  69. bool is_dump_headers = state == SEND_BEGIN ? true : false;
  70. std::string msg = resp->Dump(is_dump_headers, true);
  71. state = SEND_BODY;
  72. return write(msg);
  73. }
  74. int End(const char* buf = NULL, int len = -1) {
  75. if (state == SEND_END) return 0;
  76. int ret = 0;
  77. if (buf) {
  78. ret = WriteBody(buf, len);
  79. }
  80. bool is_dump_headers = true;
  81. bool is_dump_body = true;
  82. if (state == SEND_HEADER) {
  83. is_dump_headers = false;
  84. } else if (state == SEND_BODY) {
  85. is_dump_headers = false;
  86. is_dump_body = false;
  87. }
  88. if (is_dump_body) {
  89. std::string msg = response->Dump(is_dump_headers, is_dump_body);
  90. ret = write(msg);
  91. }
  92. state = SEND_END;
  93. if (!response->IsKeepAlive()) {
  94. close();
  95. }
  96. return ret;
  97. }
  98. int End(const std::string& str) {
  99. return End(str.c_str(), str.size());
  100. }
  101. };
  102. }
  103. typedef std::shared_ptr<hv::HttpResponseWriter> HttpResponseWriterPtr;
  104. #endif // HV_HTTP_RESPONSE_WRITER_H_