http_response.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #pragma once
  2. #include "http_define.h"
  3. #if USE_NET_HTTP_WEBSITE
  4. #include <string>
  5. #include <vector>
  6. #include <list>
  7. #include "http_header.h"
  8. #include "http_interface.h"
  9. namespace ylib
  10. {
  11. namespace network
  12. {
  13. namespace http
  14. {
  15. class server;
  16. class reqpack;
  17. class response :public network::http::interface_
  18. {
  19. public:
  20. response(network::http::reqpack* reqpack);
  21. ~response();
  22. bool send_header(ushort stateNum, const std::string& stateDesc = "OK");
  23. /// <summary>发送 WebSocket 帧。opcode 默认 0x1 文本;0x2二进制 0x8关闭 0xA pong</summary>
  24. bool send_ws(const char* buf, size_t buf_len, int opcode = 0x1);
  25. bool send(const char* buf, size_t buf_len, ushort stateNum = 200, const std::string& stateDesc = "OK");
  26. bool send(const ylib::buffer& value, ushort stateNum = 200, const std::string& stateDesc = "OK");
  27. bool send(const std::string& value, ushort stateNum = 200, const std::string& stateDesc = "OK");
  28. bool send(const ylib::json& json, ushort stateNum = 200, const std::string& stateDesc = "OK");
  29. bool send_file(const std::string& filepath, int32 downbaud = -1, ushort stateNum = 200, const std::string& stateDesc = "OK");
  30. std::map<std::string, std::string>* headers();
  31. bool redirect(const std::string& filepath, bool MovedPermanently = false);
  32. bool forward(const std::string& filepath);
  33. void response_done(){m_response = true;}
  34. bool is_response_done(){return m_response;}
  35. public:
  36. ylib::json sjson;
  37. private:
  38. bool filecache(const uint64& last_modify_time);
  39. bool fileoffset(long long filesize, long long& start, long long& len);
  40. private:
  41. std::map<std::string, std::string> m_headers;
  42. bool m_response = false;
  43. network::http::reqpack* m_reqpack = nullptr;
  44. };
  45. }
  46. }
  47. }
  48. #endif