HttpResponseWriter.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 = 0,
  11. SEND_HEADER,
  12. SEND_BODY,
  13. SEND_CHUNKED,
  14. SEND_CHUNKED_END,
  15. SEND_END,
  16. } state: 8, end: 8;
  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 = end = 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 WriteCookie(const HttpCookie& cookie) {
  47. response->cookies.push_back(cookie);
  48. return 0;
  49. }
  50. int EndHeaders(const char* key = NULL, const char* value = NULL) {
  51. if (state != SEND_BEGIN) return -1;
  52. if (key && value) {
  53. response->headers[key] = value;
  54. }
  55. std::string headers = response->Dump(true, false);
  56. state = SEND_HEADER;
  57. return write(headers);
  58. }
  59. template<typename T>
  60. int EndHeaders(const char* key, T num) {
  61. std::string value = hv::to_string(num);
  62. return EndHeaders(key, value.c_str());
  63. }
  64. int WriteChunked(const char* buf, int len = -1) {
  65. int ret = 0;
  66. if (len == -1) len = strlen(buf);
  67. if (state == SEND_BEGIN) {
  68. EndHeaders("Transfer-Encoding", "chunked");
  69. }
  70. char chunked_header[64];
  71. int chunked_header_len = snprintf(chunked_header, sizeof(chunked_header), "%x\r\n", len);
  72. write(chunked_header, chunked_header_len);
  73. if (buf && len) {
  74. state = SEND_CHUNKED;
  75. ret = write(buf, len);
  76. } else {
  77. state = SEND_CHUNKED_END;
  78. }
  79. write("\r\n", 2);
  80. return ret;
  81. }
  82. int WriteChunked(const std::string& str) {
  83. return WriteChunked(str.c_str(), str.size());
  84. }
  85. int EndChunked() {
  86. return WriteChunked(NULL, 0);
  87. }
  88. int WriteBody(const char* buf, int len = -1) {
  89. if (response->IsChunked()) {
  90. return WriteChunked(buf, len);
  91. }
  92. if (len == -1) len = strlen(buf);
  93. if (state == SEND_BEGIN) {
  94. response->body.append(buf, len);
  95. return len;
  96. } else {
  97. state = SEND_BODY;
  98. return write(buf, len);
  99. }
  100. }
  101. int WriteBody(const std::string& str) {
  102. return WriteBody(str.c_str(), str.size());
  103. }
  104. int WriteResponse(HttpResponse* resp) {
  105. if (resp == NULL) {
  106. response->status_code = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  107. return 0;
  108. }
  109. bool is_dump_headers = state == SEND_BEGIN ? true : false;
  110. std::string msg = resp->Dump(is_dump_headers, true);
  111. state = SEND_BODY;
  112. return write(msg);
  113. }
  114. int End(const char* buf = NULL, int len = -1) {
  115. if (end == SEND_END) return 0;
  116. end = SEND_END;
  117. if (!isConnected()) {
  118. return -1;
  119. }
  120. int ret = 0;
  121. bool keepAlive = response->IsKeepAlive();
  122. if (state == SEND_CHUNKED) {
  123. if (buf) {
  124. ret = WriteChunked(buf, len);
  125. }
  126. if (state == SEND_CHUNKED) {
  127. EndChunked();
  128. }
  129. } else {
  130. if (buf) {
  131. ret = WriteBody(buf, len);
  132. }
  133. bool is_dump_headers = true;
  134. bool is_dump_body = true;
  135. if (state == SEND_HEADER) {
  136. is_dump_headers = false;
  137. } else if (state == SEND_BODY) {
  138. is_dump_headers = false;
  139. is_dump_body = false;
  140. }
  141. if (is_dump_body) {
  142. std::string msg = response->Dump(is_dump_headers, is_dump_body);
  143. state = SEND_BODY;
  144. ret = write(msg);
  145. }
  146. }
  147. if (!keepAlive) {
  148. close(true);
  149. }
  150. return ret;
  151. }
  152. int End(const std::string& str) {
  153. return End(str.c_str(), str.size());
  154. }
  155. };
  156. }
  157. typedef std::shared_ptr<hv::HttpResponseWriter> HttpResponseWriterPtr;
  158. #endif // HV_HTTP_RESPONSE_WRITER_H_