1
0

HttpMessage.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. 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. 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. 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. int HttpMessage::ParseBody() {
  245. if (body.size() == 0) {
  246. return -1;
  247. }
  248. FillContentType();
  249. #ifndef WITHOUT_HTTP_CONTENT
  250. switch(content_type) {
  251. case APPLICATION_JSON:
  252. {
  253. std::string errmsg;
  254. int ret = parse_json(body.c_str(), json, errmsg);
  255. if (ret != 0 && errmsg.size() != 0) {
  256. hloge("%s", errmsg.c_str());
  257. }
  258. return ret;
  259. }
  260. case MULTIPART_FORM_DATA:
  261. {
  262. auto iter = headers.find("Content-Type");
  263. if (iter == headers.end()) {
  264. return false;
  265. }
  266. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  267. if (boundary == NULL) {
  268. return false;
  269. }
  270. boundary += strlen("boundary=");
  271. string strBoundary(boundary);
  272. strBoundary = trim_pairs(strBoundary, "\"\"\'\'");
  273. return parse_multipart(body, form, strBoundary.c_str());
  274. }
  275. case X_WWW_FORM_URLENCODED:
  276. return parse_query_params(body.c_str(), kv);
  277. default:
  278. // nothing to do
  279. return 0;
  280. }
  281. #endif
  282. return 0;
  283. }
  284. std::string HttpMessage::Dump(bool is_dump_headers, bool is_dump_body) {
  285. std::string str;
  286. if (is_dump_headers) {
  287. DumpHeaders(str);
  288. }
  289. str += "\r\n";
  290. if (is_dump_body) {
  291. DumpBody();
  292. if (ContentLength() != 0) {
  293. str.insert(str.size(), (const char*)Content(), ContentLength());
  294. }
  295. }
  296. return str;
  297. }
  298. void HttpRequest::DumpUrl() {
  299. if (url.size() != 0 && strncmp(url.c_str(), "http", 4) == 0) {
  300. // have been complete url
  301. return;
  302. }
  303. std::string str;
  304. // scheme://
  305. str += "http";
  306. if (https) str += 's';
  307. str += "://";
  308. // host:port
  309. char c_str[256] = {0};
  310. if (url.size() != 0 && *url.c_str() != '/') {
  311. // url begin with host
  312. str += url;
  313. }
  314. else {
  315. if (port == 0 ||
  316. port == DEFAULT_HTTP_PORT ||
  317. port == DEFAULT_HTTPS_PORT) {
  318. str += host;
  319. }
  320. else {
  321. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  322. str += c_str;
  323. }
  324. }
  325. // /path
  326. if (url.size() != 0 && *url.c_str() == '/') {
  327. // url begin with path
  328. str += url;
  329. }
  330. else if (path.size() > 1 && *path.c_str() == '/') {
  331. str += path;
  332. }
  333. else if (url.size() == 0) {
  334. str += '/';
  335. }
  336. // ?query
  337. if (strchr(str.c_str(), '?') == NULL &&
  338. query_params.size() != 0) {
  339. str += '?';
  340. str += dump_query_params(query_params);
  341. }
  342. url = str;
  343. }
  344. void HttpRequest::ParseUrl() {
  345. DumpUrl();
  346. http_parser_url parser;
  347. http_parser_url_init(&parser);
  348. http_parser_parse_url(url.c_str(), url.size(), 0, &parser);
  349. // scheme
  350. https = !strncmp(url.c_str(), "https", 5);
  351. // host
  352. if (parser.field_set & (1<<UF_HOST)) {
  353. host = url.substr(parser.field_data[UF_HOST].off, parser.field_data[UF_HOST].len);
  354. }
  355. // port
  356. port = parser.port ? parser.port : https ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
  357. // path
  358. if (parser.field_set & (1<<UF_PATH)) {
  359. path = url.c_str() + parser.field_data[UF_PATH].off;
  360. }
  361. // query
  362. if (parser.field_set & (1<<UF_QUERY)) {
  363. parse_query_params(url.c_str()+parser.field_data[UF_QUERY].off, query_params);
  364. }
  365. }
  366. std::string HttpRequest::Dump(bool is_dump_headers, bool is_dump_body) {
  367. ParseUrl();
  368. char c_str[256] = {0};
  369. std::string str;
  370. // GET / HTTP/1.1\r\n
  371. snprintf(c_str, sizeof(c_str), "%s %s HTTP/%d.%d\r\n", http_method_str(method), path.c_str(), http_major, http_minor);
  372. str += c_str;
  373. if (is_dump_headers) {
  374. // Host:
  375. if (headers.find("Host") == headers.end()) {
  376. if (port == 0 ||
  377. port == DEFAULT_HTTP_PORT ||
  378. port == DEFAULT_HTTPS_PORT) {
  379. headers["Host"] = host;
  380. }
  381. else {
  382. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  383. headers["Host"] = c_str;
  384. }
  385. }
  386. DumpHeaders(str);
  387. }
  388. str += "\r\n";
  389. if (is_dump_body) {
  390. DumpBody();
  391. if (ContentLength() != 0) {
  392. str.insert(str.size(), (const char*)Content(), ContentLength());
  393. }
  394. }
  395. return str;
  396. }
  397. std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
  398. char c_str[256] = {0};
  399. std::string str;
  400. // HTTP/1.1 200 OK\r\n
  401. snprintf(c_str, sizeof(c_str), "HTTP/%d.%d %d %s\r\n", http_major, http_minor, status_code, http_status_str(status_code));
  402. str += c_str;
  403. if (is_dump_headers) {
  404. headers["Date"] = gmtime_fmt(time(NULL), c_str);
  405. DumpHeaders(str);
  406. }
  407. str += "\r\n";
  408. if (is_dump_body) {
  409. DumpBody();
  410. if (ContentLength() != 0) {
  411. str.insert(str.size(), (const char*)Content(), ContentLength());
  412. }
  413. }
  414. return str;
  415. }