http_make.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef __ASIO2_HTTP_MAKE_HPP__
  11. #define __ASIO2_HTTP_MAKE_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <asio2/http/request.hpp>
  16. #include <asio2/http/response.hpp>
  17. #ifdef ASIO2_HEADER_ONLY
  18. namespace bho::beast::http
  19. #else
  20. namespace boost::beast::http
  21. #endif
  22. {
  23. /**
  24. * @brief make A typical HTTP request struct from the uri
  25. * if the string is a url, it must start with http or https,
  26. * eg : the url must be "https://www.github.com", the url can't be "www.github.com"
  27. */
  28. template<class String>
  29. http::web_request make_request(String&& uri)
  30. {
  31. http::web_request req;
  32. std::string url = asio2::detail::to_string(std::forward<String>(uri));
  33. // if uri is empty, break
  34. if (url.empty())
  35. {
  36. asio2::set_last_error(::asio::error::invalid_argument);
  37. return req;
  38. }
  39. asio2::clear_last_error();
  40. bool has_crlf = (url.find("\r\n") != std::string::npos);
  41. http::parses::http_parser_url& url_parser = req.url().parser();
  42. int f = http::parses::http_parser_parse_url(url.data(), url.size(), 0, std::addressof(url_parser));
  43. // If \r\n string is found, and parse the uri failed, it means that the uri is not a URL.
  44. if (has_crlf && f != 0)
  45. {
  46. http::request_parser<http::string_body> req_parser;
  47. req_parser.eager(true);
  48. req_parser.put(::asio::buffer(url), asio2::get_last_error());
  49. req = req_parser.get();
  50. req.url().reset(std::string(req.target()));
  51. }
  52. // It is a URL
  53. else
  54. {
  55. if (f != 0)
  56. return req;
  57. if (http::has_unencode_char(url))
  58. {
  59. req.url().reset(http::url_encode(url));
  60. }
  61. else
  62. {
  63. req.url().string() = std::move(url);
  64. }
  65. if (asio2::get_last_error())
  66. return req;
  67. /* <scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<fragment> */
  68. std::string_view host = req.url().host();
  69. std::string_view port = req.url().port();
  70. std::string_view target = req.url().target();
  71. // Set up an HTTP GET request message
  72. req.method(http::verb::get);
  73. req.version(11);
  74. req.target(beast::string_view(target.data(), target.size()));
  75. //req.set(http::field::server, BEAST_VERSION_STRING);
  76. if (host.empty())
  77. {
  78. asio2::set_last_error(::asio::error::invalid_argument);
  79. return req;
  80. }
  81. if (!port.empty() && port != "443" && port != "80")
  82. req.set(http::field::host, std::string(host) + ":" + std::string(port));
  83. else
  84. req.set(http::field::host, host);
  85. }
  86. return req;
  87. }
  88. /**
  89. * @brief make A typical HTTP request struct from the uri
  90. */
  91. template<typename String, typename StrOrInt>
  92. inline http::web_request make_request(String&& host, StrOrInt&& port,
  93. std::string_view target, http::verb method = http::verb::get, unsigned version = 11)
  94. {
  95. http::web_request req;
  96. asio2::clear_last_error();
  97. std::string h = asio2::detail::to_string(std::forward<String>(host));
  98. asio2::trim_both(h);
  99. std::string p = asio2::detail::to_string(std::forward<StrOrInt>(port));
  100. asio2::trim_both(p);
  101. std::string_view schema{ h.data(), (std::min<std::string_view::size_type>)(4, h.size()) };
  102. std::string url;
  103. if (!asio2::iequals(schema, "http"))
  104. {
  105. if /**/ (p == "80")
  106. url += "http://";
  107. else if (p == "443")
  108. url += "https://";
  109. else
  110. url += "http://";
  111. }
  112. url += h;
  113. if (!p.empty() && p != "443" && p != "80")
  114. {
  115. url += ":";
  116. url += p;
  117. }
  118. url += target;
  119. req.url() = http::url{ std::move(url) };
  120. // if url is invalid, break
  121. if (asio2::get_last_error())
  122. return req;
  123. // Set up an HTTP GET request message
  124. req.method(method);
  125. req.version(version);
  126. if (target.empty())
  127. {
  128. req.target(beast::string_view{ "/" });
  129. }
  130. else
  131. {
  132. if (http::has_unencode_char(target, 1))
  133. {
  134. std::string encoded = http::url_encode(target);
  135. req.target(beast::string_view(encoded.data(), encoded.size()));
  136. }
  137. else
  138. {
  139. req.target(beast::string_view(target.data(), target.size()));
  140. }
  141. }
  142. if (!p.empty() && p != "443" && p != "80")
  143. {
  144. req.set(http::field::host, h + ":" + p);
  145. }
  146. else
  147. {
  148. req.set(http::field::host, std::move(h));
  149. }
  150. //req.set(http::field::server, BEAST_VERSION_STRING);
  151. return req;
  152. }
  153. template<class Body = http::string_body, class Fields = http::fields>
  154. inline http::response<Body, Fields> make_response(std::string_view uri)
  155. {
  156. asio2::clear_last_error();
  157. http::response_parser<Body> rep_parser;
  158. rep_parser.eager(true);
  159. rep_parser.put(::asio::buffer(uri), asio2::get_last_error());
  160. http::response<Body, Fields> rep = rep_parser.get();
  161. return rep;
  162. }
  163. template<class Body = http::string_body, class Fields = http::fields>
  164. inline typename std::enable_if_t<std::is_same_v<Body, http::string_body>, http::response<Body, Fields>>
  165. make_response(http::status code, std::string_view body, unsigned version = 11)
  166. {
  167. http::response<Body, Fields> rep;
  168. rep.version(version);
  169. //rep.set(http::field::server, BEAST_VERSION_STRING);
  170. rep.result(code);
  171. rep.body() = body;
  172. http::try_prepare_payload(rep);
  173. return rep;
  174. }
  175. template<class Body = http::string_body, class Fields = http::fields>
  176. inline typename std::enable_if_t<std::is_same_v<Body, http::string_body>, http::response<Body, Fields>>
  177. make_response(unsigned code, std::string_view body, unsigned version = 11)
  178. {
  179. return make_response(http::int_to_status(code), body, version);
  180. }
  181. }
  182. #endif // !__ASIO2_HTTP_MAKE_HPP__