1
0

HttpMessage.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include "HttpMessage.h"
  2. #include <string.h>
  3. #include "htime.h"
  4. #include "hlog.h"
  5. #include "hurl.h"
  6. using namespace hv;
  7. http_headers DefaultHeaders;
  8. http_body NoBody;
  9. HttpCookie NoCookie;
  10. char HttpMessage::s_date[32] = {0};
  11. bool HttpCookie::parse(const std::string& str) {
  12. std::stringstream ss;
  13. ss << str;
  14. std::string line;
  15. std::string::size_type pos;
  16. std::string key;
  17. std::string val;
  18. reset();
  19. while (std::getline(ss, line, ';')) {
  20. pos = line.find_first_of('=');
  21. if (pos != std::string::npos) {
  22. key = trim(line.substr(0, pos));
  23. val = trim(line.substr(pos+1));
  24. const char* pkey = key.c_str();
  25. if (stricmp(pkey, "Domain") == 0) {
  26. domain = val;
  27. }
  28. else if (stricmp(pkey, "Path") == 0) {
  29. path = val;
  30. }
  31. else if (stricmp(pkey, "Expires") == 0) {
  32. expires = val;
  33. }
  34. else if (stricmp(pkey, "Max-Age") == 0) {
  35. max_age = atoi(val.c_str());
  36. }
  37. else if (stricmp(pkey, "SameSite") == 0) {
  38. samesite = stricmp(val.c_str(), "Strict") == 0 ? HttpCookie::SameSite::Strict :
  39. stricmp(val.c_str(), "Lax") == 0 ? HttpCookie::SameSite::Lax :
  40. stricmp(val.c_str(), "None") == 0 ? HttpCookie::SameSite::None :
  41. HttpCookie::SameSite::Default;
  42. }
  43. else {
  44. if (name.empty()) {
  45. name = key;
  46. value = val;
  47. }
  48. kv[key] = val;
  49. }
  50. } else {
  51. key = trim(line);
  52. const char* pkey = key.c_str();
  53. if (stricmp(pkey, "Secure") == 0) {
  54. secure = true;
  55. }
  56. else if (stricmp(pkey, "HttpOnly") == 0) {
  57. httponly = true;
  58. }
  59. else {
  60. hlogw("Unrecognized key '%s'", key.c_str());
  61. }
  62. }
  63. }
  64. return !name.empty();
  65. }
  66. std::string HttpCookie::dump() const {
  67. assert(!name.empty());
  68. std::string res;
  69. if (!name.empty()) {
  70. res = name;
  71. res += "=";
  72. res += value;
  73. }
  74. for (auto& pair : kv) {
  75. if (!res.empty()) res += "; ";
  76. res += pair.first;
  77. res += "=";
  78. res += pair.second;
  79. }
  80. if (!domain.empty()) {
  81. res += "; Domain=";
  82. res += domain;
  83. }
  84. if (!path.empty()) {
  85. res += "; Path=";
  86. res += path;
  87. }
  88. if (max_age > 0) {
  89. res += "; Max-Age=";
  90. res += hv::to_string(max_age);
  91. } else if (!expires.empty()) {
  92. res += "; Expires=";
  93. res += expires;
  94. }
  95. if (samesite != HttpCookie::SameSite::Default) {
  96. res += "; SameSite=";
  97. res += samesite == HttpCookie::SameSite::Strict ? "Strict" :
  98. samesite == HttpCookie::SameSite::Lax ? "Lax" :
  99. "None" ;
  100. }
  101. if (secure) {
  102. res += "; Secure";
  103. }
  104. if (httponly) {
  105. res += "; HttpOnly";
  106. }
  107. return res;
  108. }
  109. #ifndef WITHOUT_HTTP_CONTENT
  110. // NOTE: json ignore number/string, 123/"123"
  111. std::string HttpMessage::GetString(const char* key, const std::string& defvalue) {
  112. switch (ContentType()) {
  113. case APPLICATION_JSON:
  114. {
  115. if (json.empty()) {
  116. ParseBody();
  117. }
  118. if (!json.is_object()) {
  119. return defvalue;
  120. }
  121. const auto& value = json[key];
  122. if (value.is_string()) {
  123. return value;
  124. }
  125. else if (value.is_number()) {
  126. return hv::to_string(value);
  127. }
  128. else if (value.is_null()) {
  129. return "null";
  130. }
  131. else if (value.is_boolean()) {
  132. bool b = value;
  133. return b ? "true" : "false";
  134. }
  135. else {
  136. return defvalue;
  137. }
  138. }
  139. break;
  140. case MULTIPART_FORM_DATA:
  141. {
  142. if (form.empty()) {
  143. ParseBody();
  144. }
  145. auto iter = form.find(key);
  146. if (iter != form.end()) {
  147. return iter->second.content;
  148. }
  149. }
  150. break;
  151. case APPLICATION_URLENCODED:
  152. {
  153. if (kv.empty()) {
  154. ParseBody();
  155. }
  156. auto iter = kv.find(key);
  157. if (iter != kv.end()) {
  158. return iter->second;
  159. }
  160. }
  161. break;
  162. default:
  163. break;
  164. }
  165. return defvalue;
  166. }
  167. template<>
  168. HV_EXPORT int64_t HttpMessage::Get(const char* key, int64_t defvalue) {
  169. if (ContentType() == APPLICATION_JSON) {
  170. if (json.empty()) {
  171. ParseBody();
  172. }
  173. if (!json.is_object()) {
  174. return defvalue;
  175. }
  176. const auto& value = json[key];
  177. if (value.is_number()) {
  178. return value;
  179. }
  180. else if (value.is_string()) {
  181. std::string str = value;
  182. return atoll(str.c_str());
  183. }
  184. else if (value.is_null()) {
  185. return 0;
  186. }
  187. else if (value.is_boolean()) {
  188. bool b = value;
  189. return b ? 1 : 0;
  190. }
  191. else {
  192. return defvalue;
  193. }
  194. }
  195. else {
  196. std::string str = GetString(key);
  197. return str.empty() ? defvalue : atoll(str.c_str());
  198. }
  199. }
  200. template<>
  201. HV_EXPORT int HttpMessage::Get(const char* key, int defvalue) {
  202. return (int)Get<int64_t>(key, defvalue);
  203. }
  204. template<>
  205. HV_EXPORT double HttpMessage::Get(const char* key, double defvalue) {
  206. if (ContentType() == APPLICATION_JSON) {
  207. if (json.empty()) {
  208. ParseBody();
  209. }
  210. if (!json.is_object()) {
  211. return defvalue;
  212. }
  213. const auto& value = json[key];
  214. if (value.is_number()) {
  215. return value;
  216. }
  217. else if (value.is_string()) {
  218. std::string str = value;
  219. return atof(str.c_str());
  220. }
  221. else if (value.is_null()) {
  222. return 0.0f;
  223. }
  224. else {
  225. return defvalue;
  226. }
  227. }
  228. else {
  229. std::string str = GetString(key);
  230. return str.empty() ? defvalue : atof(str.c_str());
  231. }
  232. }
  233. template<>
  234. HV_EXPORT float HttpMessage::Get(const char* key, float defvalue) {
  235. return (float)Get<double>(key, defvalue);
  236. }
  237. template<>
  238. HV_EXPORT bool HttpMessage::Get(const char* key, bool defvalue) {
  239. if (ContentType() == APPLICATION_JSON) {
  240. if (json.empty()) {
  241. ParseBody();
  242. }
  243. if (!json.is_object()) {
  244. return defvalue;
  245. }
  246. const auto& value = json[key];
  247. if (value.is_boolean()) {
  248. return value;
  249. }
  250. else if (value.is_string()) {
  251. std::string str = value;
  252. return hv_getboolean(str.c_str());
  253. }
  254. else if (value.is_null()) {
  255. return false;
  256. }
  257. else if (value.is_number()) {
  258. return value != 0;
  259. }
  260. else {
  261. return defvalue;
  262. }
  263. }
  264. else {
  265. std::string str = GetString(key);
  266. return str.empty() ? defvalue : hv_getboolean(str.c_str());
  267. }
  268. }
  269. bool HttpMessage::GetBool(const char* key, bool defvalue) {
  270. return Get<bool>(key, defvalue);
  271. }
  272. int64_t HttpMessage::GetInt(const char* key, int64_t defvalue) {
  273. return Get<int64_t>(key, defvalue);
  274. }
  275. double HttpMessage::GetFloat(const char* key, double defvalue) {
  276. return Get<double>(key, defvalue);
  277. }
  278. #endif
  279. void HttpMessage::FillContentType() {
  280. auto iter = headers.find("Content-Type");
  281. if (iter != headers.end()) {
  282. content_type = http_content_type_enum(iter->second.c_str());
  283. goto append;
  284. }
  285. #ifndef WITHOUT_HTTP_CONTENT
  286. if (content_type == CONTENT_TYPE_NONE) {
  287. if (json.size() != 0) {
  288. content_type = APPLICATION_JSON;
  289. }
  290. else if (form.size() != 0) {
  291. content_type = MULTIPART_FORM_DATA;
  292. }
  293. else if (kv.size() != 0) {
  294. content_type = X_WWW_FORM_URLENCODED;
  295. }
  296. else if (body.size() != 0) {
  297. content_type = TEXT_PLAIN;
  298. }
  299. }
  300. #endif
  301. if (content_type != CONTENT_TYPE_NONE) {
  302. headers["Content-Type"] = http_content_type_str(content_type);
  303. }
  304. append:
  305. #ifndef WITHOUT_HTTP_CONTENT
  306. if (content_type == MULTIPART_FORM_DATA) {
  307. auto iter = headers.find("Content-Type");
  308. if (iter != headers.end()) {
  309. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  310. if (boundary == NULL) {
  311. boundary = DEFAULT_MULTIPART_BOUNDARY;
  312. iter->second += "; boundary=";
  313. iter->second += boundary;
  314. }
  315. }
  316. }
  317. #endif
  318. return;
  319. }
  320. void HttpMessage::FillContentLength() {
  321. auto iter = headers.find("Content-Length");
  322. if (iter != headers.end()) {
  323. content_length = atoll(iter->second.c_str());
  324. }
  325. if (content_length == 0) {
  326. DumpBody();
  327. content_length = body.size();
  328. }
  329. if (iter == headers.end() && !IsChunked() && content_type != TEXT_EVENT_STREAM) {
  330. if (content_length != 0 || type == HTTP_RESPONSE) {
  331. headers["Content-Length"] = hv::to_string(content_length);
  332. }
  333. }
  334. }
  335. bool HttpMessage::IsChunked() {
  336. auto iter = headers.find("Transfer-Encoding");
  337. return iter != headers.end() && stricmp(iter->second.c_str(), "chunked") == 0;
  338. }
  339. bool HttpMessage::IsKeepAlive() {
  340. bool keepalive = true;
  341. auto iter = headers.find("connection");
  342. if (iter != headers.end()) {
  343. const char* keepalive_value = iter->second.c_str();
  344. if (stricmp(keepalive_value, "keep-alive") == 0) {
  345. keepalive = true;
  346. }
  347. else if (stricmp(keepalive_value, "close") == 0) {
  348. keepalive = false;
  349. }
  350. else if (stricmp(keepalive_value, "upgrade") == 0) {
  351. keepalive = true;
  352. }
  353. }
  354. else if (http_major == 1 && http_minor == 0) {
  355. keepalive = false;
  356. }
  357. return keepalive;
  358. }
  359. void HttpMessage::DumpHeaders(std::string& str) {
  360. FillContentType();
  361. FillContentLength();
  362. // headers
  363. for (auto& header: headers) {
  364. // http2 :method :path :scheme :authority :status
  365. if (*str.c_str() != ':') {
  366. // %s: %s\r\n
  367. str += header.first;
  368. str += ": ";
  369. str += header.second;
  370. str += "\r\n";
  371. }
  372. }
  373. // cookies
  374. const char* cookie_field = "Cookie";
  375. if (type == HTTP_RESPONSE) {
  376. cookie_field = "Set-Cookie";
  377. }
  378. for (auto& cookie : cookies) {
  379. str += cookie_field;
  380. str += ": ";
  381. str += cookie.dump();
  382. str += "\r\n";
  383. }
  384. }
  385. void HttpMessage::DumpBody() {
  386. if (body.size() != 0) {
  387. return;
  388. }
  389. FillContentType();
  390. #ifndef WITHOUT_HTTP_CONTENT
  391. switch(content_type) {
  392. case APPLICATION_JSON:
  393. body = dump_json(json, 2);
  394. break;
  395. case MULTIPART_FORM_DATA:
  396. {
  397. auto iter = headers.find("Content-Type");
  398. if (iter == headers.end()) {
  399. return;
  400. }
  401. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  402. if (boundary == NULL) {
  403. return;
  404. }
  405. boundary += strlen("boundary=");
  406. body = dump_multipart(form, boundary);
  407. }
  408. break;
  409. case X_WWW_FORM_URLENCODED:
  410. body = dump_query_params(kv);
  411. break;
  412. default:
  413. // nothing to do
  414. break;
  415. }
  416. #endif
  417. }
  418. void HttpMessage::DumpBody(std::string& str) {
  419. DumpBody();
  420. const char* content = (const char*)Content();
  421. size_t content_length = ContentLength();
  422. if (content && content_length) {
  423. str.append(content, content_length);
  424. }
  425. }
  426. int HttpMessage::ParseBody() {
  427. if (body.size() == 0) {
  428. return -1;
  429. }
  430. FillContentType();
  431. #ifndef WITHOUT_HTTP_CONTENT
  432. switch(content_type) {
  433. case APPLICATION_JSON:
  434. {
  435. std::string errmsg;
  436. int ret = parse_json(body.c_str(), json, errmsg);
  437. if (ret != 0 && errmsg.size() != 0) {
  438. hloge("%s", errmsg.c_str());
  439. }
  440. return ret;
  441. }
  442. case MULTIPART_FORM_DATA:
  443. {
  444. auto iter = headers.find("Content-Type");
  445. if (iter == headers.end()) {
  446. return -1;
  447. }
  448. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  449. if (boundary == NULL) {
  450. return -1;
  451. }
  452. boundary += strlen("boundary=");
  453. std::string strBoundary(boundary);
  454. strBoundary = trim_pairs(strBoundary, "\"\"\'\'");
  455. return parse_multipart(body, form, strBoundary.c_str());
  456. }
  457. case X_WWW_FORM_URLENCODED:
  458. return parse_query_params(body.c_str(), kv);
  459. default:
  460. // nothing to do
  461. return 0;
  462. }
  463. #endif
  464. return 0;
  465. }
  466. std::string HttpMessage::Dump(bool is_dump_headers, bool is_dump_body) {
  467. std::string str;
  468. if (is_dump_headers) {
  469. DumpHeaders(str);
  470. }
  471. str += "\r\n";
  472. if (is_dump_body) {
  473. DumpBody(str);
  474. }
  475. return str;
  476. }
  477. void HttpRequest::DumpUrl() {
  478. std::string str;
  479. if (url.size() != 0 &&
  480. *url.c_str() != '/' &&
  481. strstr(url.c_str(), "://") != NULL) {
  482. // have been complete url
  483. goto query;
  484. }
  485. // scheme://
  486. str = scheme;
  487. str += "://";
  488. // host:port
  489. if (url.size() != 0 && *url.c_str() != '/') {
  490. // url begin with host
  491. str += url;
  492. }
  493. else {
  494. if (port == 0 ||
  495. port == DEFAULT_HTTP_PORT ||
  496. port == DEFAULT_HTTPS_PORT) {
  497. str += Host();
  498. }
  499. else {
  500. str += hv::asprintf("%s:%d", host.c_str(), port);
  501. }
  502. }
  503. // /path
  504. if (url.size() != 0 && *url.c_str() == '/') {
  505. // url begin with path
  506. str += url;
  507. }
  508. else if (path.size() > 1 && *path.c_str() == '/') {
  509. str += path;
  510. }
  511. else if (url.size() == 0) {
  512. str += '/';
  513. }
  514. url = str;
  515. query:
  516. // ?query
  517. if (strchr(url.c_str(), '?') == NULL &&
  518. query_params.size() != 0) {
  519. url += '?';
  520. url += dump_query_params(query_params);
  521. }
  522. }
  523. void HttpRequest::ParseUrl() {
  524. DumpUrl();
  525. hurl_t parser;
  526. hv_parse_url(&parser, url.c_str());
  527. // scheme
  528. std::string scheme_ = url.substr(parser.fields[HV_URL_SCHEME].off, parser.fields[HV_URL_SCHEME].len);
  529. // host
  530. std::string host_(host);
  531. if (parser.fields[HV_URL_HOST].len > 0) {
  532. host_ = url.substr(parser.fields[HV_URL_HOST].off, parser.fields[HV_URL_HOST].len);
  533. }
  534. // port
  535. int port_ = parser.port ? parser.port : strcmp(scheme_.c_str(), "https") ? DEFAULT_HTTP_PORT : DEFAULT_HTTPS_PORT;
  536. if (!proxy) {
  537. scheme = scheme_;
  538. host = host_;
  539. port = port_;
  540. }
  541. FillHost(host_.c_str(), port_);
  542. // path
  543. if (parser.fields[HV_URL_PATH].len > 0) {
  544. path = url.substr(parser.fields[HV_URL_PATH].off);
  545. }
  546. // query
  547. if (parser.fields[HV_URL_QUERY].len > 0) {
  548. parse_query_params(url.c_str()+parser.fields[HV_URL_QUERY].off, query_params);
  549. }
  550. }
  551. std::string HttpRequest::Path() {
  552. const char* s = path.c_str();
  553. const char* e = s;
  554. while (*e && *e != '?' && *e != '#') ++e;
  555. return HUrl::unescape(std::string(s, e));
  556. }
  557. void HttpRequest::FillHost(const char* host, int port) {
  558. if (headers.find("Host") == headers.end()) {
  559. if (port == 0 ||
  560. port == DEFAULT_HTTP_PORT ||
  561. port == DEFAULT_HTTPS_PORT) {
  562. headers["Host"] = host;
  563. } else {
  564. headers["Host"] = asprintf("%s:%d", host, port);
  565. }
  566. }
  567. }
  568. void HttpRequest::SetHost(const char* host, int port) {
  569. this->host = host;
  570. this->port = port;
  571. FillHost(host, port);
  572. }
  573. void HttpRequest::SetProxy(const char* host, int port) {
  574. this->scheme = "http";
  575. this->host = host;
  576. this->port = port;
  577. proxy = 1;
  578. }
  579. std::string HttpRequest::Dump(bool is_dump_headers, bool is_dump_body) {
  580. ParseUrl();
  581. std::string str;
  582. str.reserve(MAX(512, path.size() + 128));
  583. // GET / HTTP/1.1\r\n
  584. str = asprintf("%s %s HTTP/%d.%d\r\n",
  585. http_method_str(method),
  586. proxy ? url.c_str() : path.c_str(),
  587. (int)http_major, (int)http_minor);
  588. if (is_dump_headers) {
  589. DumpHeaders(str);
  590. }
  591. str += "\r\n";
  592. if (is_dump_body) {
  593. DumpBody(str);
  594. }
  595. return str;
  596. }
  597. std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
  598. char c_str[256] = {0};
  599. std::string str;
  600. str.reserve(512);
  601. // HTTP/1.1 200 OK\r\n
  602. snprintf(c_str, sizeof(c_str), "HTTP/%d.%d %d %s\r\n",
  603. (int)http_major, (int)http_minor,
  604. (int)status_code, http_status_str(status_code));
  605. str = c_str;
  606. if (is_dump_headers) {
  607. if (*s_date) {
  608. headers["Date"] = s_date;
  609. } else {
  610. headers["Date"] = gmtime_fmt(time(NULL), c_str);
  611. }
  612. DumpHeaders(str);
  613. }
  614. str += "\r\n";
  615. if (is_dump_body) {
  616. DumpBody(str);
  617. }
  618. return str;
  619. }