HttpResponseWriter.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. int EndHeaders(const char* key = NULL, const char* value = NULL) {
  38. if (state != SEND_BEGIN) return -1;
  39. if (key && value) {
  40. response->headers[key] = value;
  41. }
  42. std::string headers = response->Dump(true, false);
  43. state = SEND_HEADER;
  44. return write(headers);
  45. }
  46. int WriteBody(const char* buf, int len = -1) {
  47. if (len == -1) len = strlen(buf);
  48. if (state == SEND_BEGIN) {
  49. response->body.append(buf, len);
  50. return len;
  51. } else {
  52. state = SEND_BODY;
  53. return write(buf, len);
  54. }
  55. }
  56. int WriteBody(const std::string& str) {
  57. return WriteBody(str.c_str(), str.size());
  58. }
  59. int WriteResponse(HttpResponse* resp) {
  60. if (resp == NULL) {
  61. response->status_code = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  62. return 0;
  63. }
  64. bool is_dump_headers = state == SEND_BEGIN ? true : false;
  65. std::string msg = resp->Dump(is_dump_headers, true);
  66. state = SEND_BODY;
  67. return write(msg);
  68. }
  69. int End(const char* buf = NULL, int len = -1) {
  70. if (state == SEND_END) return 0;
  71. int ret = 0;
  72. if (buf) {
  73. ret = WriteBody(buf, len);
  74. }
  75. bool is_dump_headers = true;
  76. bool is_dump_body = true;
  77. if (state == SEND_HEADER) {
  78. is_dump_headers = false;
  79. } else if (state == SEND_BODY) {
  80. is_dump_headers = false;
  81. is_dump_body = false;
  82. }
  83. if (is_dump_body) {
  84. std::string msg = response->Dump(is_dump_headers, is_dump_body);
  85. ret = write(msg);
  86. }
  87. state = SEND_END;
  88. if (!response->IsKeepAlive()) {
  89. close();
  90. }
  91. return ret;
  92. }
  93. int End(const std::string& str) {
  94. return End(str.c_str(), str.size());
  95. }
  96. };
  97. }
  98. typedef std::shared_ptr<hv::HttpResponseWriter> HttpResponseWriterPtr;
  99. #endif // HV_HTTP_RESPONSE_WRITER_H_