1
0

HttpMessage.h 3.7 KB

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