HttpMessage.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef HTTP_MESSAGE_H_
  2. #define HTTP_MESSAGE_H_
  3. #include <string>
  4. #include <map>
  5. #include "hstring.h"
  6. #include "httpdef.h"
  7. #include "http_content.h"
  8. typedef std::map<std::string, std::string, StringCaseLess> http_headers;
  9. typedef std::string http_body;
  10. struct NetAddr {
  11. std::string ip;
  12. int port;
  13. std::string ipport() {
  14. return asprintf("%s:%d", ip.c_str(), port);
  15. }
  16. };
  17. class HttpMessage {
  18. public:
  19. int type;
  20. unsigned short http_major;
  21. unsigned short http_minor;
  22. http_headers headers;
  23. http_body body;
  24. // structured content
  25. void* content; // DATA_NO_COPY
  26. int content_length;
  27. http_content_type content_type;
  28. #ifndef WITHOUT_HTTP_CONTENT
  29. Json json; // APPLICATION_JSON
  30. MultiPart mp; // MULTIPART_FORM_DATA
  31. KeyValue kv; // X_WWW_FORM_URLENCODED
  32. #endif
  33. HttpMessage() {
  34. type = HTTP_BOTH;
  35. Init();
  36. }
  37. virtual ~HttpMessage() {}
  38. void Init() {
  39. http_major = 1;
  40. http_minor = 1;
  41. content = NULL;
  42. content_length = 0;
  43. content_type = CONTENT_TYPE_NONE;
  44. }
  45. virtual void Reset() {
  46. Init();
  47. headers.clear();
  48. body.clear();
  49. #ifndef WITHOUT_HTTP_CONTENT
  50. json.clear();
  51. mp.clear();
  52. kv.clear();
  53. #endif
  54. }
  55. // structured-content -> content_type <-> headers Content-Type
  56. void FillContentType();
  57. // body.size -> content_length <-> headers Content-Length
  58. void FillContentLength();
  59. // headers -> string
  60. void DumpHeaders(std::string& str);
  61. // structured content -> body
  62. void DumpBody();
  63. // body -> structured content
  64. // @retval 0:succeed
  65. int ParseBody();
  66. virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
  67. void* Content() {
  68. if (content == NULL && body.size() != 0) {
  69. content = (void*)body.data();
  70. }
  71. return content;
  72. }
  73. int ContentLength() {
  74. if (content_length == 0) {
  75. FillContentLength();
  76. }
  77. return content_length;
  78. }
  79. http_content_type ContentType() {
  80. if (content_type == CONTENT_TYPE_NONE) {
  81. FillContentType();
  82. }
  83. return content_type;
  84. }
  85. };
  86. #define DEFAULT_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
  87. class HttpRequest : public HttpMessage {
  88. public:
  89. http_method method;
  90. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  91. std::string url;
  92. // structured url
  93. bool https;
  94. std::string host;
  95. int port;
  96. std::string path;
  97. QueryParams query_params;
  98. // client_addr
  99. NetAddr client_addr;
  100. HttpRequest() : HttpMessage() {
  101. type = HTTP_REQUEST;
  102. Init();
  103. }
  104. void Init() {
  105. headers["User-Agent"] = DEFAULT_USER_AGENT;
  106. headers["Accept"] = "*/*";
  107. method = HTTP_GET;
  108. https = 0;
  109. host = "127.0.0.1";
  110. port = DEFAULT_HTTP_PORT;
  111. path = "/";
  112. }
  113. virtual void Reset() {
  114. HttpMessage::Reset();
  115. Init();
  116. url.clear();
  117. query_params.clear();
  118. }
  119. virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
  120. // structed url -> url
  121. void DumpUrl();
  122. // url -> structed url
  123. void ParseUrl();
  124. };
  125. class HttpResponse : public HttpMessage {
  126. public:
  127. http_status status_code;
  128. HttpResponse() : HttpMessage() {
  129. type = HTTP_RESPONSE;
  130. Init();
  131. }
  132. void Init() {
  133. status_code = HTTP_STATUS_OK;
  134. }
  135. virtual void Reset() {
  136. HttpMessage::Reset();
  137. Init();
  138. }
  139. virtual std::string Dump(bool is_dump_headers = true, bool is_dump_body = false);
  140. };
  141. #endif // HTTP_MESSAGE_H_