HttpMessage.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. hv::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. std::string GetHeader(const char* key) {
  83. auto iter = headers.find(key);
  84. if (iter != headers.end()) {
  85. return iter->second;
  86. }
  87. return "";
  88. }
  89. // headers -> string
  90. void DumpHeaders(std::string& str);
  91. // structured content -> body
  92. void DumpBody();
  93. // body -> structured content
  94. // @retval 0:succeed
  95. int ParseBody();
  96. virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
  97. void* Content() {
  98. if (content == NULL && body.size() != 0) {
  99. content = (void*)body.data();
  100. }
  101. return content;
  102. }
  103. int ContentLength() {
  104. if (content_length == 0) {
  105. FillContentLength();
  106. }
  107. return content_length;
  108. }
  109. http_content_type ContentType() {
  110. if (content_type == CONTENT_TYPE_NONE) {
  111. FillContentType();
  112. }
  113. return content_type;
  114. }
  115. };
  116. #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"
  117. class HttpRequest : public HttpMessage {
  118. public:
  119. http_method method;
  120. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  121. std::string url;
  122. // structured url
  123. bool https;
  124. std::string host;
  125. int port;
  126. std::string path;
  127. QueryParams query_params;
  128. // client_addr
  129. NetAddr client_addr;
  130. HttpRequest() : HttpMessage() {
  131. type = HTTP_REQUEST;
  132. Init();
  133. }
  134. void Init() {
  135. headers["User-Agent"] = DEFAULT_USER_AGENT;
  136. headers["Accept"] = "*/*";
  137. method = HTTP_GET;
  138. https = 0;
  139. host = "127.0.0.1";
  140. port = DEFAULT_HTTP_PORT;
  141. path = "/";
  142. }
  143. virtual void Reset() {
  144. HttpMessage::Reset();
  145. Init();
  146. url.clear();
  147. query_params.clear();
  148. }
  149. virtual std::string Dump(bool is_dump_headers, bool is_dump_body);
  150. std::string GetParam(const char* key) {
  151. auto iter = query_params.find(key);
  152. if (iter != query_params.end()) {
  153. return iter->second;
  154. }
  155. return "";
  156. }
  157. // structed url -> url
  158. void DumpUrl();
  159. // url -> structed url
  160. void ParseUrl();
  161. };
  162. class HttpResponse : public HttpMessage {
  163. public:
  164. http_status status_code;
  165. HttpResponse() : HttpMessage() {
  166. type = HTTP_RESPONSE;
  167. Init();
  168. }
  169. void Init() {
  170. status_code = HTTP_STATUS_OK;
  171. }
  172. virtual void Reset() {
  173. HttpMessage::Reset();
  174. Init();
  175. }
  176. virtual std::string Dump(bool is_dump_headers = true, bool is_dump_body = false);
  177. };
  178. #endif // HTTP_MESSAGE_H_