HttpResponseWriter.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_CHUNKED,
  14. SEND_CHUNKED_END,
  15. SEND_END,
  16. } state;
  17. HttpResponseWriter(hio_t* io, const HttpResponsePtr& resp)
  18. : SocketChannel(io)
  19. , response(resp)
  20. , state(SEND_BEGIN)
  21. {}
  22. ~HttpResponseWriter() {}
  23. // Begin -> End
  24. // Begin -> WriteResponse -> End
  25. // Begin -> WriteStatus -> WriteHeader -> WriteBody -> End
  26. // Begin -> EndHeaders("Content-Type", "text/event-stream") -> write -> write -> ... -> close
  27. // Begin -> EndHeaders("Content-Length", content_length) -> WriteBody -> WriteBody -> ... -> End
  28. // Begin -> EndHeaders("Transfer-Encoding", "chunked") -> WriteChunked -> WriteChunked -> ... -> End
  29. int Begin() {
  30. state = SEND_BEGIN;
  31. return 0;
  32. }
  33. int WriteStatus(http_status status_codes) {
  34. response->status_code = status_codes;
  35. return 0;
  36. }
  37. int WriteHeader(const char* key, const char* value) {
  38. response->headers[key] = value;
  39. return 0;
  40. }
  41. template<typename T>
  42. int WriteHeader(const char* key, T num) {
  43. response->headers[key] = hv::to_string(num);
  44. return 0;
  45. }
  46. int EndHeaders(const char* key = NULL, const char* value = NULL) {
  47. if (state != SEND_BEGIN) return -1;
  48. if (key && value) {
  49. response->headers[key] = value;
  50. }
  51. std::string headers = response->Dump(true, false);
  52. state = SEND_HEADER;
  53. return write(headers);
  54. }
  55. template<typename T>
  56. int EndHeaders(const char* key, T num) {
  57. std::string value = hv::to_string(num);
  58. return EndHeaders(key, value.c_str());
  59. }
  60. int WriteChunked(const char* buf, int len = -1) {
  61. int ret = 0;
  62. if (len == -1) len = strlen(buf);
  63. if (state == SEND_BEGIN) {
  64. EndHeaders("Transfer-Encoding", "chunked");
  65. }
  66. char chunked_header[64];
  67. int chunked_header_len = snprintf(chunked_header, sizeof(chunked_header), "%x\r\n", len);
  68. write(chunked_header, chunked_header_len);
  69. if (buf && len) {
  70. ret = write(buf, len);
  71. state = SEND_CHUNKED;
  72. } else {
  73. state = SEND_CHUNKED_END;
  74. }
  75. write("\r\n", 2);
  76. return ret;
  77. }
  78. int WriteChunked(const std::string& str) {
  79. return WriteChunked(str.c_str(), str.size());
  80. }
  81. int EndChunked() {
  82. return WriteChunked(NULL, 0);
  83. }
  84. int WriteBody(const char* buf, int len = -1) {
  85. if (response->IsChunked()) {
  86. return WriteChunked(buf, len);
  87. }
  88. if (len == -1) len = strlen(buf);
  89. if (state == SEND_BEGIN) {
  90. response->body.append(buf, len);
  91. return len;
  92. } else {
  93. state = SEND_BODY;
  94. return write(buf, len);
  95. }
  96. }
  97. int WriteBody(const std::string& str) {
  98. return WriteBody(str.c_str(), str.size());
  99. }
  100. int WriteResponse(HttpResponse* resp) {
  101. if (resp == NULL) {
  102. response->status_code = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  103. return 0;
  104. }
  105. bool is_dump_headers = state == SEND_BEGIN ? true : false;
  106. std::string msg = resp->Dump(is_dump_headers, true);
  107. state = SEND_BODY;
  108. return write(msg);
  109. }
  110. int End(const char* buf = NULL, int len = -1) {
  111. if (state == SEND_END) return 0;
  112. if (!isConnected()) {
  113. state = SEND_END;
  114. return -1;
  115. }
  116. int ret = 0;
  117. bool keepAlive = response->IsKeepAlive();
  118. if (state == SEND_CHUNKED) {
  119. if (buf) {
  120. ret = WriteChunked(buf, len);
  121. }
  122. if (state == SEND_CHUNKED) {
  123. EndChunked();
  124. }
  125. } else {
  126. if (buf) {
  127. ret = WriteBody(buf, len);
  128. }
  129. bool is_dump_headers = true;
  130. bool is_dump_body = true;
  131. if (state == SEND_HEADER) {
  132. is_dump_headers = false;
  133. } else if (state == SEND_BODY) {
  134. is_dump_headers = false;
  135. is_dump_body = false;
  136. }
  137. if (is_dump_body) {
  138. std::string msg = response->Dump(is_dump_headers, is_dump_body);
  139. ret = write(msg);
  140. }
  141. }
  142. state = SEND_END;
  143. if (!keepAlive) {
  144. close(true);
  145. }
  146. return ret;
  147. }
  148. int End(const std::string& str) {
  149. return End(str.c_str(), str.size());
  150. }
  151. };
  152. }
  153. typedef std::shared_ptr<hv::HttpResponseWriter> HttpResponseWriterPtr;
  154. #endif // HV_HTTP_RESPONSE_WRITER_H_