HttpRequest.h 11 KB

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