1
0

HttpMessage.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef HTTP_MESSAGE_H_
  2. #define HTTP_MESSAGE_H_
  3. #include <string>
  4. #include <map>
  5. #include "hexport.h"
  6. #include "hstring.h"
  7. #include "httpdef.h"
  8. #include "http_content.h"
  9. typedef std::map<std::string, std::string, StringCaseLess> http_headers;
  10. typedef std::string http_body;
  11. struct NetAddr {
  12. std::string ip;
  13. int port;
  14. std::string ipport() {
  15. return asprintf("%s:%d", ip.c_str(), port);
  16. }
  17. };
  18. class HV_EXPORT HttpMessage {
  19. public:
  20. int type;
  21. unsigned short http_major;
  22. unsigned short http_minor;
  23. http_headers headers;
  24. http_body body;
  25. // structured content
  26. void* content; // DATA_NO_COPY
  27. int content_length;
  28. http_content_type content_type;
  29. #ifndef WITHOUT_HTTP_CONTENT
  30. Json json; // APPLICATION_JSON
  31. MultiPart form; // MULTIPART_FORM_DATA
  32. hv::KeyValue kv; // X_WWW_FORM_URLENCODED
  33. // T=[bool, int64_t, double]
  34. template<typename T>
  35. T Get(const char* key, T defvalue = 0);
  36. std::string GetString(const char* key, const std::string& = "");
  37. bool GetBool(const char* key, bool defvalue = 0);
  38. int64_t GetInt(const char* key, int64_t defvalue = 0);
  39. double GetFloat(const char* key, double defvalue = 0);
  40. template<typename T>
  41. void Set(const char* key, const T& value) {
  42. switch (content_type) {
  43. case APPLICATION_JSON:
  44. json[key] = value;
  45. break;
  46. case MULTIPART_FORM_DATA:
  47. form[key] = FormData(value);
  48. break;
  49. case X_WWW_FORM_URLENCODED:
  50. kv[key] = hv::to_string(value);
  51. break;
  52. default:
  53. break;
  54. }
  55. }
  56. #endif
  57. HttpMessage() {
  58. type = HTTP_BOTH;
  59. Init();
  60. }
  61. virtual ~HttpMessage() {}
  62. void Init() {
  63. http_major = 1;
  64. http_minor = 1;
  65. content = NULL;
  66. content_length = 0;
  67. content_type = CONTENT_TYPE_NONE;
  68. }
  69. virtual void Reset() {
  70. Init();
  71. headers.clear();
  72. body.clear();
  73. #ifndef WITHOUT_HTTP_CONTENT
  74. json.clear();
  75. form.clear();
  76. kv.clear();
  77. #endif
  78. }
  79. // structured-content -> content_type <-> headers Content-Type
  80. void FillContentType();
  81. // body.size -> content_length <-> headers Content-Length
  82. void FillContentLength();
  83. std::string GetHeader(const char* key) {
  84. auto iter = headers.find(key);
  85. if (iter != headers.end()) {
  86. return iter->second;
  87. }
  88. return "";
  89. }
  90. // headers -> string
  91. void DumpHeaders(std::string& str);
  92. // structured content -> body
  93. void DumpBody();
  94. // body -> structured content
  95. // @retval 0:succeed
  96. int ParseBody();
  97. virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
  98. void* Content() {
  99. if (content == NULL && body.size() != 0) {
  100. content = (void*)body.data();
  101. }
  102. return content;
  103. }
  104. int ContentLength() {
  105. if (content_length == 0) {
  106. FillContentLength();
  107. }
  108. return content_length;
  109. }
  110. http_content_type ContentType() {
  111. if (content_type == CONTENT_TYPE_NONE) {
  112. FillContentType();
  113. }
  114. return content_type;
  115. }
  116. };
  117. #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"
  118. class HV_EXPORT HttpRequest : public HttpMessage {
  119. public:
  120. http_method method;
  121. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  122. std::string url;
  123. // structured url
  124. bool https;
  125. std::string host;
  126. int port;
  127. std::string path;
  128. QueryParams query_params;
  129. // client_addr
  130. NetAddr client_addr;
  131. HttpRequest() : HttpMessage() {
  132. type = HTTP_REQUEST;
  133. Init();
  134. }
  135. void Init() {
  136. headers["User-Agent"] = DEFAULT_USER_AGENT;
  137. headers["Accept"] = "*/*";
  138. method = HTTP_GET;
  139. https = 0;
  140. host = "127.0.0.1";
  141. port = DEFAULT_HTTP_PORT;
  142. path = "/";
  143. }
  144. virtual void Reset() {
  145. HttpMessage::Reset();
  146. Init();
  147. url.clear();
  148. query_params.clear();
  149. }
  150. virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
  151. std::string GetParam(const char* key) {
  152. auto iter = query_params.find(key);
  153. if (iter != query_params.end()) {
  154. return iter->second;
  155. }
  156. return "";
  157. }
  158. // structed url -> url
  159. void DumpUrl();
  160. // url -> structed url
  161. void ParseUrl();
  162. };
  163. class HV_EXPORT HttpResponse : public HttpMessage {
  164. public:
  165. http_status status_code;
  166. HttpResponse() : HttpMessage() {
  167. type = HTTP_RESPONSE;
  168. Init();
  169. }
  170. void Init() {
  171. status_code = HTTP_STATUS_OK;
  172. }
  173. virtual void Reset() {
  174. HttpMessage::Reset();
  175. Init();
  176. }
  177. virtual std::string Dump(bool is_dump_headers = true, bool is_dump_body = false);
  178. };
  179. #endif // HTTP_MESSAGE_H_