HttpMessage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. bool HttpMessage::IsKeepAlive() {
  266. bool keepalive = true;
  267. auto iter = headers.find("connection");
  268. if (iter != headers.end()) {
  269. const char* keepalive_value = iter->second.c_str();
  270. if (stricmp(keepalive_value, "keep-alive") == 0) {
  271. keepalive = true;
  272. }
  273. else if (stricmp(keepalive_value, "close") == 0) {
  274. keepalive = false;
  275. }
  276. else if (stricmp(keepalive_value, "upgrade") == 0) {
  277. keepalive = true;
  278. }
  279. }
  280. else if (http_major == 1 && http_minor == 0) {
  281. keepalive = false;
  282. }
  283. return keepalive;
  284. }
  285. void HttpMessage::DumpHeaders(std::string& str) {
  286. FillContentType();
  287. FillContentLength();
  288. for (auto& header: headers) {
  289. // http2 :method :path :scheme :authority :status
  290. if (*str.c_str() != ':') {
  291. // %s: %s\r\n
  292. str += header.first;
  293. str += ": ";
  294. str += header.second;
  295. str += "\r\n";
  296. }
  297. }
  298. }
  299. void HttpMessage::DumpBody() {
  300. if (body.size() != 0) {
  301. return;
  302. }
  303. FillContentType();
  304. #ifndef WITHOUT_HTTP_CONTENT
  305. switch(content_type) {
  306. case APPLICATION_JSON:
  307. body = dump_json(json);
  308. break;
  309. case MULTIPART_FORM_DATA:
  310. {
  311. auto iter = headers.find("Content-Type");
  312. if (iter == headers.end()) {
  313. return;
  314. }
  315. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  316. if (boundary == NULL) {
  317. return;
  318. }
  319. boundary += strlen("boundary=");
  320. body = dump_multipart(form, boundary);
  321. }
  322. break;
  323. case X_WWW_FORM_URLENCODED:
  324. body = dump_query_params(kv);
  325. break;
  326. default:
  327. // nothing to do
  328. break;
  329. }
  330. #endif
  331. }
  332. void HttpMessage::DumpBody(std::string& str) {
  333. DumpBody();
  334. const char* content = (const char*)Content();
  335. int content_length = ContentLength();
  336. if (content && content_length) {
  337. str.append(content, content_length);
  338. }
  339. }
  340. int HttpMessage::ParseBody() {
  341. if (body.size() == 0) {
  342. return -1;
  343. }
  344. FillContentType();
  345. #ifndef WITHOUT_HTTP_CONTENT
  346. switch(content_type) {
  347. case APPLICATION_JSON:
  348. {
  349. std::string errmsg;
  350. int ret = parse_json(body.c_str(), json, errmsg);
  351. if (ret != 0 && errmsg.size() != 0) {
  352. hloge("%s", errmsg.c_str());
  353. }
  354. return ret;
  355. }
  356. case MULTIPART_FORM_DATA:
  357. {
  358. auto iter = headers.find("Content-Type");
  359. if (iter == headers.end()) {
  360. return false;
  361. }
  362. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  363. if (boundary == NULL) {
  364. return false;
  365. }
  366. boundary += strlen("boundary=");
  367. string strBoundary(boundary);
  368. strBoundary = trim_pairs(strBoundary, "\"\"\'\'");
  369. return parse_multipart(body, form, strBoundary.c_str());
  370. }
  371. case X_WWW_FORM_URLENCODED:
  372. return parse_query_params(body.c_str(), kv);
  373. default:
  374. // nothing to do
  375. return 0;
  376. }
  377. #endif
  378. return 0;
  379. }
  380. std::string HttpMessage::Dump(bool is_dump_headers, bool is_dump_body) {
  381. std::string str;
  382. if (is_dump_headers) {
  383. DumpHeaders(str);
  384. }
  385. str += "\r\n";
  386. if (is_dump_body) {
  387. DumpBody(str);
  388. }
  389. return str;
  390. }
  391. void HttpRequest::DumpUrl() {
  392. if (url.size() != 0 && strstr(url.c_str(), "://") != NULL) {
  393. // have been complete url
  394. return;
  395. }
  396. std::string str;
  397. // scheme://
  398. str = scheme;
  399. str += "://";
  400. // host:port
  401. char c_str[256] = {0};
  402. if (url.size() != 0 && *url.c_str() != '/') {
  403. // url begin with host
  404. str += url;
  405. }
  406. else {
  407. if (port == 0 ||
  408. port == DEFAULT_HTTP_PORT ||
  409. port == DEFAULT_HTTPS_PORT) {
  410. str += host;
  411. }
  412. else {
  413. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  414. str += c_str;
  415. }
  416. }
  417. // /path
  418. if (url.size() != 0 && *url.c_str() == '/') {
  419. // url begin with path
  420. str += url;
  421. }
  422. else if (path.size() > 1 && *path.c_str() == '/') {
  423. str += path;
  424. }
  425. else if (url.size() == 0) {
  426. str += '/';
  427. }
  428. // ?query
  429. if (strchr(str.c_str(), '?') == NULL &&
  430. query_params.size() != 0) {
  431. str += '?';
  432. str += dump_query_params(query_params);
  433. }
  434. url = str;
  435. }
  436. void HttpRequest::ParseUrl() {
  437. DumpUrl();
  438. http_parser_url parser;
  439. http_parser_url_init(&parser);
  440. http_parser_parse_url(url.c_str(), url.size(), 0, &parser);
  441. // scheme
  442. scheme = url.substr(parser.field_data[UF_SCHEMA].off, parser.field_data[UF_SCHEMA].len);
  443. // host
  444. if (parser.field_set & (1<<UF_HOST)) {
  445. host = url.substr(parser.field_data[UF_HOST].off, parser.field_data[UF_HOST].len);
  446. }
  447. // port
  448. port = parser.port ? parser.port : strcmp(scheme.c_str(), "https") ? DEFAULT_HTTP_PORT : DEFAULT_HTTPS_PORT;
  449. // path
  450. if (parser.field_set & (1<<UF_PATH)) {
  451. path = url.c_str() + parser.field_data[UF_PATH].off;
  452. }
  453. // query
  454. if (parser.field_set & (1<<UF_QUERY)) {
  455. parse_query_params(url.c_str()+parser.field_data[UF_QUERY].off, query_params);
  456. }
  457. }
  458. std::string HttpRequest::Dump(bool is_dump_headers, bool is_dump_body) {
  459. ParseUrl();
  460. char c_str[256] = {0};
  461. std::string str;
  462. // GET / HTTP/1.1\r\n
  463. snprintf(c_str, sizeof(c_str), "%s %s HTTP/%d.%d\r\n", http_method_str(method), path.c_str(), http_major, http_minor);
  464. str += c_str;
  465. if (is_dump_headers) {
  466. // Host:
  467. if (headers.find("Host") == headers.end()) {
  468. if (port == 0 ||
  469. port == DEFAULT_HTTP_PORT ||
  470. port == DEFAULT_HTTPS_PORT) {
  471. headers["Host"] = host;
  472. }
  473. else {
  474. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  475. headers["Host"] = c_str;
  476. }
  477. }
  478. DumpHeaders(str);
  479. }
  480. str += "\r\n";
  481. if (is_dump_body) {
  482. DumpBody(str);
  483. }
  484. return str;
  485. }
  486. std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
  487. char c_str[256] = {0};
  488. std::string str;
  489. // HTTP/1.1 200 OK\r\n
  490. snprintf(c_str, sizeof(c_str), "HTTP/%d.%d %d %s\r\n", http_major, http_minor, status_code, http_status_str(status_code));
  491. str += c_str;
  492. if (is_dump_headers) {
  493. if (*s_date) {
  494. headers["Date"] = s_date;
  495. } else {
  496. headers["Date"] = gmtime_fmt(time(NULL), c_str);
  497. }
  498. DumpHeaders(str);
  499. }
  500. str += "\r\n";
  501. if (is_dump_body) {
  502. DumpBody(str);
  503. }
  504. return str;
  505. }