HttpPayload.h 3.7 KB

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