HttpPayload.cpp 7.3 KB

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