HttpRequest.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #include "hstring.h"
  81. typedef std::map<std::string, std::string, StringCaseLess> http_headers;
  82. typedef std::string http_body;
  83. class HttpInfo {
  84. public:
  85. unsigned short http_major;
  86. unsigned short http_minor;
  87. http_headers headers;
  88. http_body body;
  89. // parsed content
  90. http_content_type content_type;
  91. Json json; // APPLICATION_JSON
  92. MultiPart mp; // FORM_DATA
  93. KeyValue kv; // X_WWW_FORM_URLENCODED
  94. HttpInfo() {
  95. init();
  96. }
  97. void init() {
  98. http_major = 1;
  99. http_minor = 1;
  100. content_type = CONTENT_TYPE_NONE;
  101. headers.clear();
  102. body.clear();
  103. json.clear();
  104. mp.clear();
  105. kv.clear();
  106. }
  107. void fill_content_type() {
  108. auto iter = headers.find("Content-Type");
  109. if (iter != headers.end()) {
  110. content_type = http_content_type_enum(iter->second.c_str());
  111. goto append;
  112. }
  113. if (content_type == CONTENT_TYPE_NONE) {
  114. if (json.size() != 0) {
  115. content_type = APPLICATION_JSON;
  116. }
  117. else if (mp.size() != 0) {
  118. content_type = MULTIPART_FORM_DATA;
  119. }
  120. else if (kv.size() != 0) {
  121. content_type = X_WWW_FORM_URLENCODED;
  122. }
  123. else if (body.size() != 0) {
  124. content_type = TEXT_PLAIN;
  125. }
  126. }
  127. if (content_type != CONTENT_TYPE_NONE) {
  128. headers["Content-Type"] = http_content_type_str(content_type);
  129. }
  130. append:
  131. if (content_type == MULTIPART_FORM_DATA) {
  132. auto iter = headers.find("Content-Type");
  133. if (iter != headers.end()) {
  134. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  135. if (boundary == NULL) {
  136. boundary = DEFAULT_MULTIPART_BOUNDARY;
  137. iter->second += "; boundary=";
  138. iter->second += boundary;
  139. }
  140. }
  141. }
  142. }
  143. void fill_content_lenght() {
  144. if (body.size() != 0) {
  145. headers["Content-Length"] = std::to_string(body.size());
  146. }
  147. }
  148. void dump_headers(std::string& str) {
  149. fill_content_type();
  150. fill_content_lenght();
  151. for (auto& header: headers) {
  152. // %s: %s\r\n
  153. str += header.first;
  154. str += ": ";
  155. str += header.second;
  156. str += "\r\n";
  157. }
  158. }
  159. void dump_body() {
  160. if (body.size() != 0) {
  161. return;
  162. }
  163. fill_content_type();
  164. switch(content_type) {
  165. case APPLICATION_JSON:
  166. body = dump_json(json);
  167. break;
  168. case MULTIPART_FORM_DATA:
  169. {
  170. auto iter = headers.find("Content-Type");
  171. if (iter == headers.end()) {
  172. return;
  173. }
  174. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  175. if (boundary == NULL) {
  176. return;
  177. }
  178. boundary += strlen("boundary=");
  179. body = dump_multipart(mp, boundary);
  180. }
  181. break;
  182. case X_WWW_FORM_URLENCODED:
  183. body = dump_query_params(kv);
  184. break;
  185. default:
  186. // nothing to do
  187. break;
  188. }
  189. }
  190. bool parse_body() {
  191. if (body.size() == 0) {
  192. return false;
  193. }
  194. fill_content_type();
  195. switch(content_type) {
  196. case APPLICATION_JSON:
  197. parse_json(body.c_str(), json);
  198. break;
  199. case MULTIPART_FORM_DATA:
  200. {
  201. auto iter = headers.find("Content-Type");
  202. if (iter == headers.end()) {
  203. return false;
  204. }
  205. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  206. if (boundary == NULL) {
  207. return false;
  208. }
  209. boundary += strlen("boundary=");
  210. parse_multipart(body, mp, boundary);
  211. }
  212. break;
  213. case X_WWW_FORM_URLENCODED:
  214. parse_query_params(body.c_str(), kv);
  215. break;
  216. default:
  217. // nothing to do
  218. break;
  219. }
  220. return true;
  221. }
  222. };
  223. #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"
  224. class HttpRequest : public HttpInfo {
  225. public:
  226. http_method method;
  227. // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
  228. std::string url;
  229. QueryParams query_params;
  230. HttpRequest() {
  231. init();
  232. }
  233. void init() {
  234. HttpInfo::init();
  235. method = HTTP_GET;
  236. headers["User-Agent"] = DEFAULT_USER_AGENT;
  237. headers["Accept"] = "*/*";
  238. }
  239. std::string dump_url() {
  240. std::string str;
  241. if (strstr(url.c_str(), "://") == NULL) {
  242. str += "http://";
  243. }
  244. if (*url.c_str() == '/') {
  245. str += headers["Host"];
  246. }
  247. str += url;
  248. if (strchr(url.c_str(), '?') || query_params.size() == 0) {
  249. return str;
  250. }
  251. str += '?';
  252. str += dump_query_params(query_params);
  253. return str;
  254. }
  255. void parse_url() {
  256. if (query_params.size() != 0) {
  257. return;
  258. }
  259. const char* token = strchr(url.c_str(), '?');
  260. if (token == NULL) {
  261. return;
  262. }
  263. parse_query_params(token+1, query_params);
  264. }
  265. std::string dump(bool is_dump_headers = true, bool is_dump_body = false) {
  266. char c_str[256] = {0};
  267. const char* path = "/";
  268. if (*url.c_str() == '/') {
  269. path = url.c_str();
  270. }
  271. else {
  272. std::string url = dump_url();
  273. http_parser_url parser;
  274. http_parser_url_init(&parser);
  275. http_parser_parse_url(url.c_str(), url.size(), 0, &parser);
  276. if (parser.field_set & (1<<UF_HOST)) {
  277. std::string host = url.substr(parser.field_data[UF_HOST].off, parser.field_data[UF_HOST].len);
  278. int port = parser.port;
  279. if (port == 0) {
  280. headers["Host"] = host;
  281. }
  282. else {
  283. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  284. headers["Host"] = c_str;
  285. }
  286. }
  287. if (parser.field_set & (1<<UF_PATH)) {
  288. path = url.c_str() + parser.field_data[UF_PATH].off;
  289. }
  290. }
  291. std::string str;
  292. // GET / HTTP/1.1\r\n
  293. snprintf(c_str, sizeof(c_str), "%s %s HTTP/%d.%d\r\n", http_method_str(method), path, http_major, http_minor);
  294. str += c_str;
  295. if (is_dump_headers) {
  296. dump_headers(str);
  297. }
  298. str += "\r\n";
  299. if (is_dump_body) {
  300. dump_body();
  301. str += body;
  302. }
  303. return str;
  304. }
  305. };
  306. class HttpResponse : public HttpInfo {
  307. public:
  308. http_status status_code;
  309. HttpResponse() {
  310. init();
  311. }
  312. void init() {
  313. HttpInfo::init();
  314. status_code = HTTP_STATUS_OK;
  315. }
  316. std::string dump(bool is_dump_headers = true, bool is_dump_body = false) {
  317. char c_str[256] = {0};
  318. std::string str;
  319. // HTTP/1.1 200 OK\r\n
  320. snprintf(c_str, sizeof(c_str), "HTTP/%d.%d %d %s\r\n", http_major, http_minor, status_code, http_status_str(status_code));
  321. str += c_str;
  322. if (is_dump_headers) {
  323. dump_headers(str);
  324. }
  325. str += "\r\n";
  326. if (is_dump_body) {
  327. dump_body();
  328. str += body;
  329. }
  330. return str;
  331. }
  332. };
  333. #endif // HTTP_REQUEST_H_