HttpMessage.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include "HttpMessage.h"
  2. #include <string.h>
  3. #include "htime.h"
  4. #include "http_parser.h" // for http_parser_url
  5. void HttpMessage::FillContentType() {
  6. auto iter = headers.find("Content-Type");
  7. if (iter != headers.end()) {
  8. content_type = http_content_type_enum(iter->second.c_str());
  9. goto append;
  10. }
  11. #ifndef WITHOUT_HTTP_CONTENT
  12. if (content_type == CONTENT_TYPE_NONE) {
  13. if (json.size() != 0) {
  14. content_type = APPLICATION_JSON;
  15. }
  16. else if (mp.size() != 0) {
  17. content_type = MULTIPART_FORM_DATA;
  18. }
  19. else if (kv.size() != 0) {
  20. content_type = X_WWW_FORM_URLENCODED;
  21. }
  22. else if (body.size() != 0) {
  23. content_type = TEXT_PLAIN;
  24. }
  25. }
  26. #endif
  27. if (content_type != CONTENT_TYPE_NONE) {
  28. headers["Content-Type"] = http_content_type_str(content_type);
  29. }
  30. append:
  31. #ifndef WITHOUT_HTTP_CONTENT
  32. if (content_type == MULTIPART_FORM_DATA) {
  33. auto iter = headers.find("Content-Type");
  34. if (iter != headers.end()) {
  35. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  36. if (boundary == NULL) {
  37. boundary = DEFAULT_MULTIPART_BOUNDARY;
  38. iter->second += "; boundary=";
  39. iter->second += boundary;
  40. }
  41. }
  42. }
  43. #endif
  44. return;
  45. }
  46. void HttpMessage::FillContentLength() {
  47. auto iter = headers.find("Content-Length");
  48. if (iter != headers.end()) {
  49. content_length = atoi(iter->second.c_str());
  50. }
  51. if (iter == headers.end() || content_length == 0) {
  52. if (content_length == 0) {
  53. content_length == body.size();
  54. }
  55. if (content_length == 0) {
  56. DumpBody();
  57. content_length = body.size();
  58. }
  59. char sz[64];
  60. snprintf(sz, sizeof(sz), "%d", content_length);
  61. headers["Content-Length"] = sz;
  62. }
  63. }
  64. void HttpMessage::DumpHeaders(std::string& str) {
  65. FillContentType();
  66. FillContentLength();
  67. for (auto& header: headers) {
  68. // http2 :method :path :scheme :authority :status
  69. if (*str.c_str() != ':') {
  70. // %s: %s\r\n
  71. str += header.first;
  72. str += ": ";
  73. str += header.second;
  74. str += "\r\n";
  75. }
  76. }
  77. }
  78. void HttpMessage::DumpBody() {
  79. if (body.size() != 0) {
  80. return;
  81. }
  82. FillContentType();
  83. #ifndef WITHOUT_HTTP_CONTENT
  84. switch(content_type) {
  85. case APPLICATION_JSON:
  86. body = dump_json(json);
  87. break;
  88. case MULTIPART_FORM_DATA:
  89. {
  90. auto iter = headers.find("Content-Type");
  91. if (iter == headers.end()) {
  92. return;
  93. }
  94. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  95. if (boundary == NULL) {
  96. return;
  97. }
  98. boundary += strlen("boundary=");
  99. body = dump_multipart(mp, boundary);
  100. }
  101. break;
  102. case X_WWW_FORM_URLENCODED:
  103. body = dump_query_params(kv);
  104. break;
  105. default:
  106. // nothing to do
  107. break;
  108. }
  109. #endif
  110. }
  111. int HttpMessage::ParseBody() {
  112. if (body.size() == 0) {
  113. return -1;
  114. }
  115. FillContentType();
  116. #ifndef WITHOUT_HTTP_CONTENT
  117. switch(content_type) {
  118. case APPLICATION_JSON:
  119. return parse_json(body.c_str(), json);
  120. case MULTIPART_FORM_DATA:
  121. {
  122. auto iter = headers.find("Content-Type");
  123. if (iter == headers.end()) {
  124. return false;
  125. }
  126. const char* boundary = strstr(iter->second.c_str(), "boundary=");
  127. if (boundary == NULL) {
  128. return false;
  129. }
  130. boundary += strlen("boundary=");
  131. return parse_multipart(body, mp, boundary);
  132. }
  133. case X_WWW_FORM_URLENCODED:
  134. return parse_query_params(body.c_str(), kv);
  135. default:
  136. // nothing to do
  137. return 0;
  138. }
  139. #endif
  140. return 0;
  141. }
  142. std::string HttpMessage::Dump(bool is_dump_headers, bool is_dump_body) {
  143. std::string str;
  144. if (is_dump_headers) {
  145. DumpHeaders(str);
  146. }
  147. str += "\r\n";
  148. if (is_dump_body) {
  149. DumpBody();
  150. if (ContentLength() != 0) {
  151. str.insert(str.size(), (const char*)Content(), ContentLength());
  152. }
  153. }
  154. return str;
  155. }
  156. void HttpRequest::DumpUrl() {
  157. if (url.size() != 0 && strncmp(url.c_str(), "http", 4) == 0) {
  158. // have been complete url
  159. return;
  160. }
  161. std::string str;
  162. // scheme://
  163. str += "http";
  164. if (https) str += 's';
  165. str += "://";
  166. // host:port
  167. char c_str[256] = {0};
  168. if (url.size() != 0 && *url.c_str() != '/') {
  169. // url begin with host
  170. str += url;
  171. }
  172. else {
  173. if (port == 0 ||
  174. port == DEFAULT_HTTP_PORT ||
  175. port == DEFAULT_HTTPS_PORT) {
  176. str += host;
  177. }
  178. else {
  179. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  180. str += c_str;
  181. }
  182. }
  183. // /path
  184. if (url.size() != 0 && *url.c_str() == '/') {
  185. // url begin with path
  186. str += url;
  187. }
  188. else if (path.size() > 1 && *path.c_str() == '/') {
  189. str += path;
  190. }
  191. else if (url.size() == 0) {
  192. str += '/';
  193. }
  194. // ?query
  195. if (strchr(str.c_str(), '?') == NULL &&
  196. query_params.size() != 0) {
  197. str += '?';
  198. str += dump_query_params(query_params);
  199. }
  200. url = str;
  201. }
  202. void HttpRequest::ParseUrl() {
  203. DumpUrl();
  204. http_parser_url parser;
  205. http_parser_url_init(&parser);
  206. http_parser_parse_url(url.c_str(), url.size(), 0, &parser);
  207. // scheme
  208. https = !strncmp(url.c_str(), "https", 5);
  209. // host
  210. if (parser.field_set & (1<<UF_HOST)) {
  211. host = url.substr(parser.field_data[UF_HOST].off, parser.field_data[UF_HOST].len);
  212. }
  213. // port
  214. port = parser.port ? parser.port : https ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
  215. // path
  216. if (parser.field_set & (1<<UF_PATH)) {
  217. path = url.c_str() + parser.field_data[UF_PATH].off;
  218. }
  219. // query
  220. if (parser.field_set & (1<<UF_QUERY)) {
  221. parse_query_params(url.c_str()+parser.field_data[UF_QUERY].off, query_params);
  222. }
  223. }
  224. std::string HttpRequest::Dump(bool is_dump_headers, bool is_dump_body) {
  225. ParseUrl();
  226. char c_str[256] = {0};
  227. std::string str;
  228. // GET / HTTP/1.1\r\n
  229. snprintf(c_str, sizeof(c_str), "%s %s HTTP/%d.%d\r\n", http_method_str(method), path.c_str(), http_major, http_minor);
  230. str += c_str;
  231. if (is_dump_headers) {
  232. // Host:
  233. if (headers.find("Host") == headers.end()) {
  234. if (port == 0 ||
  235. port == DEFAULT_HTTP_PORT ||
  236. port == DEFAULT_HTTPS_PORT) {
  237. headers["Host"] = host;
  238. }
  239. else {
  240. snprintf(c_str, sizeof(c_str), "%s:%d", host.c_str(), port);
  241. headers["Host"] = c_str;
  242. }
  243. }
  244. DumpHeaders(str);
  245. }
  246. str += "\r\n";
  247. if (is_dump_body) {
  248. DumpBody();
  249. if (ContentLength() != 0) {
  250. str.insert(str.size(), (const char*)Content(), ContentLength());
  251. }
  252. }
  253. return str;
  254. }
  255. std::string HttpResponse::Dump(bool is_dump_headers, bool is_dump_body) {
  256. char c_str[256] = {0};
  257. std::string str;
  258. // HTTP/1.1 200 OK\r\n
  259. snprintf(c_str, sizeof(c_str), "HTTP/%d.%d %d %s\r\n", http_major, http_minor, status_code, http_status_str(status_code));
  260. str += c_str;
  261. if (is_dump_headers) {
  262. headers["Date"] = gmtime_fmt(time(NULL), c_str);
  263. DumpHeaders(str);
  264. }
  265. str += "\r\n";
  266. if (is_dump_body) {
  267. DumpBody();
  268. if (ContentLength() != 0) {
  269. str.insert(str.size(), (const char*)Content(), ContentLength());
  270. }
  271. }
  272. return str;
  273. }