HttpMessage.cpp 13 KB

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