HttpMessage.cpp 11 KB

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