HttpRequest.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #ifndef HTTP_REQUEST_H_
  2. #define HTTP_REQUEST_H_
  3. #include <string.h>
  4. #include <string>
  5. #include <map>
  6. // for http_method, http_status
  7. #include "http_parser.h"
  8. inline http_method http_method_enum(const char* str) {
  9. #define XX(num, name, string) \
  10. if (strcmp(str, #string) == 0) { \
  11. return HTTP_##name; \
  12. }
  13. HTTP_METHOD_MAP(XX)
  14. #undef XX
  15. return HTTP_GET;
  16. }
  17. // http_content_type
  18. // XX(name, string, suffix)
  19. #define HTTP_CONTENT_TYPE_MAP(XX) \
  20. XX(TEXT_PLAIN, "text/plain", "txt") \
  21. XX(TEXT_HTML, "text/html", "html") \
  22. XX(TEXT_CSS, "text/css", "css") \
  23. XX(APPLICATION_JAVASCRIPT, "application/javascript", "js") \
  24. XX(APPLICATION_XML, "application/xml", "xml") \
  25. XX(APPLICATION_JSON, "application/json", "json") \
  26. XX(X_WWW_FORM_URLENCODED, "application/x-www-form-urlencoded", ".null.") \
  27. XX(MULTIPART_FORM_DATA, "multipart/form-data", ".null.") \
  28. XX(IMAGE_JPEG, "image/jpeg", "jpg") \
  29. XX(IMAGE_PNG, "image/png", "png") \
  30. XX(IMAGE_gif, "image/gif", "gif")
  31. enum http_content_type {
  32. #define XX(name, string, suffix) name,
  33. CONTENT_TYPE_NONE,
  34. HTTP_CONTENT_TYPE_MAP(XX)
  35. CONTENT_TYPE_UNDEFINED
  36. #undef XX
  37. };
  38. inline const char* http_content_type_str(enum http_content_type type) {
  39. switch (type) {
  40. #define XX(name, string, suffix) \
  41. case name: return string;
  42. HTTP_CONTENT_TYPE_MAP(XX)
  43. default: return "";
  44. #undef XX
  45. }
  46. }
  47. // replace strncmp(s1, s2, strlen(s2))
  48. inline int mystrcmp(const char* s1, const char* s2) {
  49. while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2) {++s1;++s2;}
  50. return *s2 == 0 ? 0 : (*s1-*s2);
  51. }
  52. inline enum http_content_type http_content_type_enum(const char* str) {
  53. #define XX(name, string, suffix) \
  54. if (mystrcmp(str, string) == 0) { \
  55. return name; \
  56. }
  57. HTTP_CONTENT_TYPE_MAP(XX)
  58. #undef XX
  59. return CONTENT_TYPE_UNDEFINED;
  60. }
  61. inline enum http_content_type http_content_type_enum_by_suffix(const char* suf) {
  62. #define XX(name, string, suffix) \
  63. if (strcmp(suf, suffix) == 0) { \
  64. return name; \
  65. }
  66. HTTP_CONTENT_TYPE_MAP(XX)
  67. #undef XX
  68. return CONTENT_TYPE_UNDEFINED;
  69. }
  70. inline const char* http_content_type_str_by_suffix(const char* suf) {
  71. #define XX(name, string, suffix) \
  72. if (strcmp(suf, suffix) == 0) { \
  73. return string; \
  74. }
  75. HTTP_CONTENT_TYPE_MAP(XX)
  76. #undef XX
  77. return "";
  78. }
  79. #include "http_content.h"
  80. typedef std::map<std::string, std::string> http_headers;
  81. typedef std::string http_body;
  82. class HttpInfo {
  83. public:
  84. unsigned short http_major;
  85. unsigned short http_minor;
  86. http_headers headers;
  87. http_body body;
  88. // parsed content
  89. http_content_type content_type;
  90. Json json; // APPLICATION_JSON
  91. MultiPart mp; // FORM_DATA
  92. KeyValue kv; // X_WWW_FORM_URLENCODED
  93. HttpInfo() {
  94. http_major = 1;
  95. http_minor = 1;
  96. content_type = CONTENT_TYPE_NONE;
  97. }
  98. void fill_content_type() {
  99. auto iter = headers.find("Content-Type");
  100. if (iter != headers.end()) {
  101. content_type = http_content_type_enum(iter->second.c_str());
  102. return;
  103. }
  104. if (content_type == CONTENT_TYPE_NONE) {
  105. if (json.size() != 0) {
  106. content_type = APPLICATION_JSON;
  107. }
  108. else if (mp.size() != 0) {
  109. content_type = MULTIPART_FORM_DATA;
  110. }
  111. else if (kv.size() != 0) {
  112. content_type = X_WWW_FORM_URLENCODED;
  113. }
  114. else if (body.size() != 0) {
  115. content_type = TEXT_PLAIN;
  116. }
  117. }
  118. if (content_type != CONTENT_TYPE_NONE) {
  119. headers["Content-Type"] = http_content_type_str(content_type);
  120. }
  121. }
  122. void fill_content_lenght() {
  123. if (body.size() != 0) {
  124. headers["Content-Length"] = std::to_string(body.size());
  125. }
  126. }
  127. void dump_headers(std::string& str) {
  128. fill_content_type();
  129. fill_content_lenght();
  130. for (auto& header: headers) {
  131. // %s: %s\r\n
  132. str += header.first;
  133. str += ": ";
  134. str += header.second;
  135. str += "\r\n";
  136. }
  137. }
  138. void dump_body() {
  139. if (body.size() != 0) {
  140. return;
  141. }
  142. fill_content_type();
  143. switch(content_type) {
  144. case APPLICATION_JSON:
  145. body = dump_json(json);
  146. break;
  147. case MULTIPART_FORM_DATA:
  148. body = dump_multipart(mp);
  149. break;
  150. case X_WWW_FORM_URLENCODED:
  151. body = dump_query_params(kv);
  152. break;
  153. default:
  154. // nothing to do
  155. break;
  156. }
  157. }
  158. bool parse_body() {
  159. if (body.size() == 0) {
  160. return false;
  161. }
  162. fill_content_type();
  163. switch(content_type) {
  164. case APPLICATION_JSON:
  165. parse_json(body.c_str(), json);
  166. break;
  167. case MULTIPART_FORM_DATA:
  168. {
  169. auto iter = headers.find("Content-Type");
  170. if (iter == headers.end()) {
  171. return false;
  172. }
  173. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  174. if (boundary == NULL) {
  175. return false;
  176. }
  177. boundary += strlen("boundary=");
  178. parse_multipart(body, mp, boundary);
  179. }
  180. break;
  181. case X_WWW_FORM_URLENCODED:
  182. parse_query_params(body.c_str(), kv);
  183. break;
  184. default:
  185. // nothing to do
  186. break;
  187. }
  188. return true;
  189. }
  190. };
  191. #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"
  192. class HttpRequest : public HttpInfo {
  193. public:
  194. http_method method;
  195. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  196. std::string url;
  197. QueryParams query_params;
  198. HttpRequest() {
  199. method = HTTP_GET;
  200. headers["User-Agent"] = DEFAULT_USER_AGENT;
  201. headers["Accept"] = "*/*";
  202. }
  203. std::string dump_url() {
  204. std::string str;
  205. if (strstr(url.c_str(), "://") == NULL) {
  206. str += "http://";
  207. }
  208. if (*url.c_str() == '/') {
  209. str += headers["Host"];
  210. }
  211. str += url;
  212. if (strchr(url.c_str(), '?') || query_params.size() == 0) {
  213. return str;
  214. }
  215. str += '?';
  216. str += dump_query_params(query_params);
  217. return str;
  218. }
  219. void parse_url() {
  220. if (query_params.size() != 0) {
  221. return;
  222. }
  223. const char* token = strchr(url.c_str(), '?');
  224. if (token == NULL) {
  225. return;
  226. }
  227. parse_query_params(token+1, query_params);
  228. }
  229. std::string dump(bool is_dump_headers = true, bool is_dump_body = false) {
  230. char c_str[256] = {0};
  231. const char* path = "/";
  232. if (*url.c_str() == '/') {
  233. path = url.c_str();
  234. }
  235. else {
  236. std::string url = dump_url();
  237. http_parser_url parser;
  238. http_parser_url_init(&parser);
  239. http_parser_parse_url(url.c_str(), url.size(), 0, &parser);
  240. std::string host;
  241. if (parser.field_set & (1<<UF_HOST)) {
  242. host = url.substr(parser.field_data[UF_HOST].off, parser.field_data[UF_HOST].len);
  243. }
  244. int port = parser.port;
  245. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  246. headers["Host"] = c_str;
  247. if (parser.field_set & (1<<UF_PATH)) {
  248. path = url.c_str() + parser.field_data[UF_PATH].off;
  249. }
  250. }
  251. std::string str;
  252. // GET / HTTP/1.1\r\n
  253. snprintf(c_str, sizeof(c_str), "%s %s HTTP/%d.%d\r\n", http_method_str(method), path, http_major, http_minor);
  254. str += c_str;
  255. if (is_dump_headers) {
  256. dump_headers(str);
  257. }
  258. str += "\r\n";
  259. if (is_dump_body) {
  260. dump_body();
  261. str += body;
  262. }
  263. return str;
  264. }
  265. };
  266. class HttpResponse : public HttpInfo {
  267. public:
  268. http_status status_code;
  269. HttpResponse() {
  270. status_code = HTTP_STATUS_OK;
  271. }
  272. std::string dump(bool is_dump_headers = true, bool is_dump_body = false) {
  273. char c_str[256] = {0};
  274. std::string str;
  275. // HTTP/1.1 200 OK\r\n
  276. snprintf(c_str, sizeof(c_str), "HTTP/%d.%d %d %s\r\n", http_major, http_minor, status_code, http_status_str(status_code));
  277. str += c_str;
  278. if (is_dump_headers) {
  279. dump_headers(str);
  280. }
  281. str += "\r\n";
  282. if (is_dump_body) {
  283. dump_body();
  284. str += body;
  285. }
  286. return str;
  287. }
  288. };
  289. #endif // HTTP_REQUEST_H_