1
0

HttpRequest.h 9.9 KB

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