1
0

HttpMessage.h 5.6 KB

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