HttpMessage.cpp 11 KB

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