HttpMessage.cpp 7.4 KB

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