HttpRequest.h 11 KB

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