HttpMessage.cpp 15 KB

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