HttpResponseWriter.h 4.7 KB

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