HttpRequest.h 11 KB

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