HttpMessage.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 form; // MULTIPART_FORM_DATA
  31. KeyValue kv; // X_WWW_FORM_URLENCODED
  32. // T=[bool, int64_t, double]
  33. template<typename T>
  34. T Get(const char* key, T defvalue = 0);
  35. std::string GetString(const char* key, const std::string& = "");
  36. bool GetBool(const char* key, bool defvalue = 0);
  37. int64_t GetInt(const char* key, int64_t defvalue = 0);
  38. double GetFloat(const char* key, double defvalue = 0);
  39. template<typename T>
  40. void Set(const char* key, const T& value) {
  41. switch (content_type) {
  42. case APPLICATION_JSON:
  43. json[key] = value;
  44. break;
  45. case MULTIPART_FORM_DATA:
  46. form[key] = FormData(value);
  47. break;
  48. case X_WWW_FORM_URLENCODED:
  49. kv[key] = hv::to_string(value);
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. #endif
  56. HttpMessage() {
  57. type = HTTP_BOTH;
  58. Init();
  59. }
  60. virtual ~HttpMessage() {}
  61. void Init() {
  62. http_major = 1;
  63. http_minor = 1;
  64. content = NULL;
  65. content_length = 0;
  66. content_type = CONTENT_TYPE_NONE;
  67. }
  68. virtual void Reset() {
  69. Init();
  70. headers.clear();
  71. body.clear();
  72. #ifndef WITHOUT_HTTP_CONTENT
  73. json.clear();
  74. form.clear();
  75. kv.clear();
  76. #endif
  77. }
  78. // structured-content -> content_type <-> headers Content-Type
  79. void FillContentType();
  80. // body.size -> content_length <-> headers Content-Length
  81. void FillContentLength();
  82. // headers -> string
  83. void DumpHeaders(std::string& str);
  84. // structured content -> body
  85. void DumpBody();
  86. // body -> structured content
  87. // @retval 0:succeed
  88. int ParseBody();
  89. virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
  90. void* Content() {
  91. if (content == NULL && body.size() != 0) {
  92. content = (void*)body.data();
  93. }
  94. return content;
  95. }
  96. int ContentLength() {
  97. if (content_length == 0) {
  98. FillContentLength();
  99. }
  100. return content_length;
  101. }
  102. http_content_type ContentType() {
  103. if (content_type == CONTENT_TYPE_NONE) {
  104. FillContentType();
  105. }
  106. return content_type;
  107. }
  108. };
  109. #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"
  110. class HttpRequest : public HttpMessage {
  111. public:
  112. http_method method;
  113. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  114. std::string url;
  115. // structured url
  116. bool https;
  117. std::string host;
  118. int port;
  119. std::string path;
  120. QueryParams query_params;
  121. // client_addr
  122. NetAddr client_addr;
  123. HttpRequest() : HttpMessage() {
  124. type = HTTP_REQUEST;
  125. Init();
  126. }
  127. void Init() {
  128. headers["User-Agent"] = DEFAULT_USER_AGENT;
  129. headers["Accept"] = "*/*";
  130. method = HTTP_GET;
  131. https = 0;
  132. host = "127.0.0.1";
  133. port = DEFAULT_HTTP_PORT;
  134. path = "/";
  135. }
  136. virtual void Reset() {
  137. HttpMessage::Reset();
  138. Init();
  139. url.clear();
  140. query_params.clear();
  141. }
  142. virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
  143. // structed url -> url
  144. void DumpUrl();
  145. // url -> structed url
  146. void ParseUrl();
  147. };
  148. class HttpResponse : public HttpMessage {
  149. public:
  150. http_status status_code;
  151. HttpResponse() : HttpMessage() {
  152. type = HTTP_RESPONSE;
  153. Init();
  154. }
  155. void Init() {
  156. status_code = HTTP_STATUS_OK;
  157. }
  158. virtual void Reset() {
  159. HttpMessage::Reset();
  160. Init();
  161. }
  162. virtual std::string Dump(bool is_dump_headers = true, bool is_dump_body = false);
  163. };
  164. #endif // HTTP_MESSAGE_H_