HttpMessage.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include "HttpMessage.h"
  2. #include <string.h>
  3. #include "htime.h"
  4. #include "http_parser.h" // for http_parser_url
  5. #ifndef WITHOUT_HTTP_CONTENT
  6. // NOTE: json ignore number/string, 123/"123"
  7. using nlohmann::detail::value_t;
  8. std::string HttpMessage::GetValue(const char* key, const std::string& defvalue) {
  9. switch (content_type) {
  10. case APPLICATION_JSON:
  11. {
  12. auto iter = json.find(key);
  13. if (iter != json.end()) {
  14. switch (iter->type()) {
  15. case value_t::null:
  16. return "null";
  17. case value_t::string:
  18. return *iter;
  19. case value_t::boolean:
  20. {
  21. bool b = *iter;
  22. return b ? "true" : "false";
  23. }
  24. case value_t::number_integer:
  25. {
  26. std::int64_t n = *iter;
  27. return std::to_string(n);
  28. }
  29. case value_t::number_unsigned:
  30. {
  31. std::uint64_t n = *iter;
  32. return std::to_string(n);
  33. }
  34. case value_t::number_float:
  35. {
  36. double f = *iter;
  37. return std::to_string(f);
  38. }
  39. default:
  40. return defvalue;
  41. }
  42. }
  43. }
  44. break;
  45. case MULTIPART_FORM_DATA:
  46. {
  47. auto iter = form.find(key);
  48. if (iter != form.end()) {
  49. return iter->second.content;
  50. }
  51. }
  52. break;
  53. case APPLICATION_URLENCODED:
  54. {
  55. auto iter = kv.find(key);
  56. if (iter != kv.end()) {
  57. return iter->second;
  58. }
  59. }
  60. break;
  61. default:
  62. break;
  63. }
  64. return defvalue;
  65. }
  66. template<>
  67. int64_t HttpMessage::Get(const char* key, int64_t defvalue) {
  68. if (content_type == APPLICATION_JSON) {
  69. auto iter = json.find(key);
  70. if (iter != json.end()) {
  71. switch (iter->type()) {
  72. return *iter;
  73. case value_t::boolean:
  74. {
  75. bool b = *iter;
  76. return b;
  77. }
  78. case value_t::number_integer:
  79. {
  80. std::int64_t n = *iter;
  81. return n;
  82. }
  83. case value_t::number_unsigned:
  84. {
  85. std::uint64_t n = *iter;
  86. return n;
  87. }
  88. case value_t::number_float:
  89. {
  90. double f = *iter;
  91. return f;
  92. }
  93. case value_t::string:
  94. {
  95. std::string str = *iter;
  96. return atoll(str.c_str());
  97. }
  98. default:
  99. return defvalue;
  100. }
  101. }
  102. return defvalue;
  103. }
  104. else {
  105. std::string str = GetValue(key);
  106. return str.empty() ? defvalue : atoi(str.c_str());
  107. }
  108. }
  109. template<>
  110. double HttpMessage::Get(const char* key, double defvalue) {
  111. if (content_type == APPLICATION_JSON) {
  112. auto iter = json.find(key);
  113. if (iter != json.end()) {
  114. switch (iter->type()) {
  115. return *iter;
  116. case value_t::boolean:
  117. {
  118. bool b = *iter;
  119. return b;
  120. }
  121. case value_t::number_integer:
  122. {
  123. std::int64_t n = *iter;
  124. return n;
  125. }
  126. case value_t::number_unsigned:
  127. {
  128. std::uint64_t n = *iter;
  129. return n;
  130. }
  131. case value_t::number_float:
  132. {
  133. double f = *iter;
  134. return f;
  135. }
  136. case value_t::string:
  137. {
  138. std::string str = *iter;
  139. return atof(str.c_str());
  140. }
  141. default:
  142. return defvalue;
  143. }
  144. }
  145. return defvalue;
  146. }
  147. else {
  148. std::string str = GetValue(key);
  149. return str.empty() ? defvalue : atoi(str.c_str());
  150. }
  151. }
  152. template<>
  153. bool HttpMessage::Get(const char* key, bool defvalue) {
  154. if (content_type == APPLICATION_JSON) {
  155. auto iter = json.find(key);
  156. if (iter != json.end()) {
  157. switch (iter->type()) {
  158. return *iter;
  159. case value_t::boolean:
  160. {
  161. bool b = *iter;
  162. return b;
  163. }
  164. case value_t::number_integer:
  165. {
  166. std::int64_t n = *iter;
  167. return n;
  168. }
  169. case value_t::number_unsigned:
  170. {
  171. std::uint64_t n = *iter;
  172. return n;
  173. }
  174. case value_t::number_float:
  175. {
  176. double f = *iter;
  177. return f;
  178. }
  179. case value_t::string:
  180. {
  181. std::string str = *iter;
  182. return atof(str.c_str());
  183. }
  184. default:
  185. return defvalue;
  186. }
  187. }
  188. return defvalue;
  189. }
  190. else {
  191. std::string str = GetValue(key);
  192. return str.empty() ? defvalue : atoi(str.c_str());
  193. }
  194. }
  195. #endif
  196. void HttpMessage::FillContentType() {
  197. auto iter = headers.find("Content-Type");
  198. if (iter != headers.end()) {
  199. content_type = http_content_type_enum(iter->second.c_str());
  200. goto append;
  201. }
  202. #ifndef WITHOUT_HTTP_CONTENT
  203. if (content_type == CONTENT_TYPE_NONE) {
  204. if (json.size() != 0) {
  205. content_type = APPLICATION_JSON;
  206. }
  207. else if (form.size() != 0) {
  208. content_type = MULTIPART_FORM_DATA;
  209. }
  210. else if (kv.size() != 0) {
  211. content_type = X_WWW_FORM_URLENCODED;
  212. }
  213. else if (body.size() != 0) {
  214. content_type = TEXT_PLAIN;
  215. }
  216. }
  217. #endif
  218. if (content_type != CONTENT_TYPE_NONE) {
  219. headers["Content-Type"] = http_content_type_str(content_type);
  220. }
  221. append:
  222. #ifndef WITHOUT_HTTP_CONTENT
  223. if (content_type == MULTIPART_FORM_DATA) {
  224. auto iter = headers.find("Content-Type");
  225. if (iter != headers.end()) {
  226. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  227. if (boundary == NULL) {
  228. boundary = DEFAULT_MULTIPART_BOUNDARY;
  229. iter->second += "; boundary=";
  230. iter->second += boundary;
  231. }
  232. }
  233. }
  234. #endif
  235. return;
  236. }
  237. void HttpMessage::FillContentLength() {
  238. auto iter = headers.find("Content-Length");
  239. if (iter != headers.end()) {
  240. content_length = atoi(iter->second.c_str());
  241. }
  242. if (iter == headers.end() || content_length == 0) {
  243. if (content_length == 0) {
  244. content_length = body.size();
  245. }
  246. if (content_length == 0) {
  247. DumpBody();
  248. content_length = body.size();
  249. }
  250. char sz[64];
  251. snprintf(sz, sizeof(sz), "%d", content_length);
  252. headers["Content-Length"] = sz;
  253. }
  254. }
  255. void HttpMessage::DumpHeaders(std::string& str) {
  256. FillContentType();
  257. FillContentLength();
  258. for (auto& header: headers) {
  259. // http2 :method :path :scheme :authority :status
  260. if (*str.c_str() != ':') {
  261. // %s: %s\r\n
  262. str += header.first;
  263. str += ": ";
  264. str += header.second;
  265. str += "\r\n";
  266. }
  267. }
  268. }
  269. void HttpMessage::DumpBody() {
  270. if (body.size() != 0) {
  271. return;
  272. }
  273. FillContentType();
  274. #ifndef WITHOUT_HTTP_CONTENT
  275. switch(content_type) {
  276. case APPLICATION_JSON:
  277. body = dump_json(json);
  278. break;
  279. case MULTIPART_FORM_DATA:
  280. {
  281. auto iter = headers.find("Content-Type");
  282. if (iter == headers.end()) {
  283. return;
  284. }
  285. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  286. if (boundary == NULL) {
  287. return;
  288. }
  289. boundary += strlen("boundary=");
  290. body = dump_multipart(form, boundary);
  291. }
  292. break;
  293. case X_WWW_FORM_URLENCODED:
  294. body = dump_query_params(kv);
  295. break;
  296. default:
  297. // nothing to do
  298. break;
  299. }
  300. #endif
  301. }
  302. int HttpMessage::ParseBody() {
  303. if (body.size() == 0) {
  304. return -1;
  305. }
  306. FillContentType();
  307. #ifndef WITHOUT_HTTP_CONTENT
  308. switch(content_type) {
  309. case APPLICATION_JSON:
  310. return parse_json(body.c_str(), json);
  311. case MULTIPART_FORM_DATA:
  312. {
  313. auto iter = headers.find("Content-Type");
  314. if (iter == headers.end()) {
  315. return false;
  316. }
  317. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  318. if (boundary == NULL) {
  319. return false;
  320. }
  321. boundary += strlen("boundary=");
  322. return parse_multipart(body, form, boundary);
  323. }
  324. case X_WWW_FORM_URLENCODED:
  325. return parse_query_params(body.c_str(), kv);
  326. default:
  327. // nothing to do
  328. return 0;
  329. }
  330. #endif
  331. return 0;
  332. }
  333. std::string HttpMessage::Dump(bool is_dump_headers, bool is_dump_body) {
  334. std::string str;
  335. if (is_dump_headers) {
  336. DumpHeaders(str);
  337. }
  338. str += "\r\n";
  339. if (is_dump_body) {
  340. DumpBody();
  341. if (ContentLength() != 0) {
  342. str.insert(str.size(), (const char*)Content(), ContentLength());
  343. }
  344. }
  345. return str;
  346. }
  347. void HttpRequest::DumpUrl() {
  348. if (url.size() != 0 && strncmp(url.c_str(), "http", 4) == 0) {
  349. // have been complete url
  350. return;
  351. }
  352. std::string str;
  353. // scheme://
  354. str += "http";
  355. if (https) str += 's';
  356. str += "://";
  357. // host:port
  358. char c_str[256] = {0};
  359. if (url.size() != 0 && *url.c_str() != '/') {
  360. // url begin with host
  361. str += url;
  362. }
  363. else {
  364. if (port == 0 ||
  365. port == DEFAULT_HTTP_PORT ||
  366. port == DEFAULT_HTTPS_PORT) {
  367. str += host;
  368. }
  369. else {
  370. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  371. str += c_str;
  372. }
  373. }
  374. // /path
  375. if (url.size() != 0 && *url.c_str() == '/') {
  376. // url begin with path
  377. str += url;
  378. }
  379. else if (path.size() > 1 && *path.c_str() == '/') {
  380. str += path;
  381. }
  382. else if (url.size() == 0) {
  383. str += '/';
  384. }
  385. // ?query
  386. if (strchr(str.c_str(), '?') == NULL &&
  387. query_params.size() != 0) {
  388. str += '?';
  389. str += dump_query_params(query_params);
  390. }
  391. url = str;
  392. }
  393. void HttpRequest::ParseUrl() {
  394. DumpUrl();
  395. http_parser_url parser;
  396. http_parser_url_init(&parser);
  397. http_parser_parse_url(url.c_str(), url.size(), 0, &parser);
  398. // scheme
  399. https = !strncmp(url.c_str(), "https", 5);
  400. // host
  401. if (parser.field_set & (1<<UF_HOST)) {
  402. host = url.substr(parser.field_data[UF_HOST].off, parser.field_data[UF_HOST].len);
  403. }
  404. // port
  405. port = parser.port ? parser.port : https ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
  406. // path
  407. if (parser.field_set & (1<<UF_PATH)) {
  408. path = url.c_str() + parser.field_data[UF_PATH].off;
  409. }
  410. // query
  411. if (parser.field_set & (1<<UF_QUERY)) {
  412. parse_query_params(url.c_str()+parser.field_data[UF_QUERY].off, query_params);
  413. }
  414. }
  415. std::string HttpRequest::Dump(bool is_dump_headers, bool is_dump_body) {
  416. ParseUrl();
  417. char c_str[256] = {0};
  418. std::string str;
  419. // GET / HTTP/1.1\r\n
  420. snprintf(c_str, sizeof(c_str), "%s %s HTTP/%d.%d\r\n", http_method_str(method), path.c_str(), http_major, http_minor);
  421. str += c_str;
  422. if (is_dump_headers) {
  423. // Host:
  424. if (headers.find("Host") == headers.end()) {
  425. if (port == 0 ||
  426. port == DEFAULT_HTTP_PORT ||
  427. port == DEFAULT_HTTPS_PORT) {
  428. headers["Host"] = host;
  429. }
  430. else {
  431. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  432. headers["Host"] = c_str;
  433. }
  434. }
  435. DumpHeaders(str);
  436. }
  437. str += "\r\n";
  438. if (is_dump_body) {
  439. DumpBody();
  440. if (ContentLength() != 0) {
  441. str.insert(str.size(), (const char*)Content(), ContentLength());
  442. }
  443. }
  444. return str;
  445. }
  446. std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
  447. char c_str[256] = {0};
  448. std::string str;
  449. // HTTP/1.1 200 OK\r\n
  450. snprintf(c_str, sizeof(c_str), "HTTP/%d.%d %d %s\r\n", http_major, http_minor, status_code, http_status_str(status_code));
  451. str += c_str;
  452. if (is_dump_headers) {
  453. headers["Date"] = gmtime_fmt(time(NULL), c_str);
  454. DumpHeaders(str);
  455. }
  456. str += "\r\n";
  457. if (is_dump_body) {
  458. DumpBody();
  459. if (ContentLength() != 0) {
  460. str.insert(str.size(), (const char*)Content(), ContentLength());
  461. }
  462. }
  463. return str;
  464. }