HttpMessage.h 4.7 KB

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