file_body_win32.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_HTTP_IMPL_FILE_BODY_WIN32_HPP
  10. #define BOOST_BEAST_HTTP_IMPL_FILE_BODY_WIN32_HPP
  11. #if BOOST_BEAST_USE_WIN32_FILE
  12. #include <boost/beast/core/async_base.hpp>
  13. #include <boost/beast/core/bind_handler.hpp>
  14. #include <boost/beast/core/buffers_range.hpp>
  15. #include <boost/beast/core/detail/clamp.hpp>
  16. #include <boost/beast/core/detail/is_invocable.hpp>
  17. #include <boost/beast/http/write.hpp>
  18. #include <boost/beast/http/serializer.hpp>
  19. #include <boost/asio/async_result.hpp>
  20. #include <boost/asio/basic_stream_socket.hpp>
  21. #include <boost/asio/windows/overlapped_ptr.hpp>
  22. #include <boost/make_unique.hpp>
  23. #include <boost/smart_ptr/make_shared_array.hpp>
  24. #include <boost/winapi/basic_types.hpp>
  25. #include <boost/winapi/get_last_error.hpp>
  26. #include <algorithm>
  27. #include <cstring>
  28. namespace boost {
  29. namespace beast {
  30. namespace http {
  31. namespace detail {
  32. template<class, class, bool, class, class>
  33. class write_some_win32_op;
  34. } // detail
  35. template<>
  36. struct basic_file_body<file_win32>
  37. {
  38. using file_type = file_win32;
  39. class writer;
  40. class reader;
  41. //--------------------------------------------------------------------------
  42. class value_type
  43. {
  44. friend class writer;
  45. friend class reader;
  46. friend struct basic_file_body<file_win32>;
  47. template<class, class, bool, class, class>
  48. friend class detail::write_some_win32_op;
  49. template<
  50. class Protocol, class Executor,
  51. bool isRequest, class Fields>
  52. friend
  53. std::size_t
  54. write_some(
  55. net::basic_stream_socket<Protocol, Executor>& sock,
  56. serializer<isRequest,
  57. basic_file_body<file_win32>, Fields>& sr,
  58. error_code& ec);
  59. file_win32 file_;
  60. std::uint64_t size_ = 0; // cached file size
  61. std::uint64_t first_; // starting offset of the range
  62. std::uint64_t last_; // ending offset of the range
  63. public:
  64. ~value_type() = default;
  65. value_type() = default;
  66. value_type(value_type&& other) = default;
  67. value_type& operator=(value_type&& other) = default;
  68. bool
  69. is_open() const
  70. {
  71. return file_.is_open();
  72. }
  73. std::uint64_t
  74. size() const
  75. {
  76. return size_;
  77. }
  78. void
  79. close();
  80. void
  81. open(char const* path, file_mode mode, error_code& ec);
  82. void
  83. reset(file_win32&& file, error_code& ec);
  84. };
  85. //--------------------------------------------------------------------------
  86. class writer
  87. {
  88. template<class, class, bool, class, class>
  89. friend class detail::write_some_win32_op;
  90. template<
  91. class Protocol, class Executor,
  92. bool isRequest, class Fields>
  93. friend
  94. std::size_t
  95. write_some(
  96. net::basic_stream_socket<Protocol, Executor>& sock,
  97. serializer<isRequest,
  98. basic_file_body<file_win32>, Fields>& sr,
  99. error_code& ec);
  100. value_type& body_; // The body we are reading from
  101. std::uint64_t pos_; // The current position in the file
  102. char buf_[4096]; // Small buffer for reading
  103. public:
  104. using const_buffers_type =
  105. net::const_buffer;
  106. template<bool isRequest, class Fields>
  107. writer(header<isRequest, Fields>&, value_type& b)
  108. : body_(b)
  109. {
  110. }
  111. void
  112. init(error_code&)
  113. {
  114. BOOST_ASSERT(body_.file_.is_open());
  115. pos_ = body_.first_;
  116. }
  117. boost::optional<std::pair<const_buffers_type, bool>>
  118. get(error_code& ec)
  119. {
  120. std::size_t const n = (std::min)(sizeof(buf_),
  121. beast::detail::clamp(body_.last_ - pos_));
  122. if(n == 0)
  123. {
  124. ec = {};
  125. return boost::none;
  126. }
  127. auto const nread = body_.file_.read(buf_, n, ec);
  128. if(ec)
  129. return boost::none;
  130. BOOST_ASSERT(nread != 0);
  131. pos_ += nread;
  132. ec = {};
  133. return {{
  134. {buf_, nread}, // buffer to return.
  135. pos_ < body_.last_}}; // `true` if there are more buffers.
  136. }
  137. };
  138. //--------------------------------------------------------------------------
  139. class reader
  140. {
  141. value_type& body_;
  142. public:
  143. template<bool isRequest, class Fields>
  144. explicit
  145. reader(header<isRequest, Fields>&, value_type& b)
  146. : body_(b)
  147. {
  148. }
  149. void
  150. init(boost::optional<
  151. std::uint64_t> const& content_length,
  152. error_code& ec)
  153. {
  154. // VFALCO We could reserve space in the file
  155. boost::ignore_unused(content_length);
  156. BOOST_ASSERT(body_.file_.is_open());
  157. ec = {};
  158. }
  159. template<class ConstBufferSequence>
  160. std::size_t
  161. put(ConstBufferSequence const& buffers,
  162. error_code& ec)
  163. {
  164. std::size_t nwritten = 0;
  165. for(auto buffer : beast::buffers_range_ref(buffers))
  166. {
  167. nwritten += body_.file_.write(
  168. buffer.data(), buffer.size(), ec);
  169. if(ec)
  170. return nwritten;
  171. }
  172. ec = {};
  173. return nwritten;
  174. }
  175. void
  176. finish(error_code& ec)
  177. {
  178. ec = {};
  179. }
  180. };
  181. //--------------------------------------------------------------------------
  182. static
  183. std::uint64_t
  184. size(value_type const& body)
  185. {
  186. return body.size();
  187. }
  188. };
  189. //------------------------------------------------------------------------------
  190. inline
  191. void
  192. basic_file_body<file_win32>::
  193. value_type::
  194. close()
  195. {
  196. error_code ignored;
  197. file_.close(ignored);
  198. }
  199. inline
  200. void
  201. basic_file_body<file_win32>::
  202. value_type::
  203. open(char const* path, file_mode mode, error_code& ec)
  204. {
  205. file_.open(path, mode, ec);
  206. if(ec)
  207. return;
  208. size_ = file_.size(ec);
  209. if(ec)
  210. {
  211. close();
  212. return;
  213. }
  214. first_ = 0;
  215. last_ = size_;
  216. }
  217. inline
  218. void
  219. basic_file_body<file_win32>::
  220. value_type::
  221. reset(file_win32&& file, error_code& ec)
  222. {
  223. if(file_.is_open())
  224. {
  225. error_code ignored;
  226. file_.close(ignored);
  227. }
  228. file_ = std::move(file);
  229. if(file_.is_open())
  230. {
  231. size_ = file_.size(ec);
  232. if(ec)
  233. {
  234. close();
  235. return;
  236. }
  237. first_ = 0;
  238. last_ = size_;
  239. }
  240. }
  241. //------------------------------------------------------------------------------
  242. namespace detail {
  243. template<class Unsigned>
  244. boost::winapi::DWORD_
  245. lowPart(Unsigned n)
  246. {
  247. return static_cast<
  248. boost::winapi::DWORD_>(
  249. n & 0xffffffff);
  250. }
  251. template<class Unsigned>
  252. boost::winapi::DWORD_
  253. highPart(Unsigned n, std::true_type)
  254. {
  255. return static_cast<
  256. boost::winapi::DWORD_>(
  257. (n>>32)&0xffffffff);
  258. }
  259. template<class Unsigned>
  260. boost::winapi::DWORD_
  261. highPart(Unsigned, std::false_type)
  262. {
  263. return 0;
  264. }
  265. template<class Unsigned>
  266. boost::winapi::DWORD_
  267. highPart(Unsigned n)
  268. {
  269. return highPart(n, std::integral_constant<
  270. bool, (sizeof(Unsigned)>4)>{});
  271. }
  272. class null_lambda
  273. {
  274. public:
  275. template<class ConstBufferSequence>
  276. void
  277. operator()(error_code&,
  278. ConstBufferSequence const&) const
  279. {
  280. BOOST_ASSERT(false);
  281. }
  282. };
  283. //------------------------------------------------------------------------------
  284. #if BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
  285. template<
  286. class Protocol, class Executor,
  287. bool isRequest, class Fields,
  288. class Handler>
  289. class write_some_win32_op
  290. : public beast::async_base<Handler, Executor>
  291. {
  292. net::basic_stream_socket<
  293. Protocol, Executor>& sock_;
  294. serializer<isRequest,
  295. basic_file_body<file_win32>, Fields>& sr_;
  296. std::size_t bytes_transferred_ = 0;
  297. bool header_ = false;
  298. public:
  299. template<class Handler_>
  300. write_some_win32_op(
  301. Handler_&& h,
  302. net::basic_stream_socket<
  303. Protocol, Executor>& s,
  304. serializer<isRequest,
  305. basic_file_body<file_win32>,Fields>& sr)
  306. : async_base<
  307. Handler, Executor>(
  308. std::forward<Handler_>(h),
  309. s.get_executor())
  310. , sock_(s)
  311. , sr_(sr)
  312. {
  313. (*this)();
  314. }
  315. void
  316. operator()()
  317. {
  318. if(! sr_.is_header_done())
  319. {
  320. header_ = true;
  321. sr_.split(true);
  322. return detail::async_write_some_impl(
  323. sock_, sr_, std::move(*this));
  324. }
  325. if(sr_.get().chunked())
  326. {
  327. return detail::async_write_some_impl(
  328. sock_, sr_, std::move(*this));
  329. }
  330. auto& w = sr_.writer_impl();
  331. boost::winapi::DWORD_ const nNumberOfBytesToWrite =
  332. static_cast<boost::winapi::DWORD_>(
  333. (std::min<std::uint64_t>)(
  334. (std::min<std::uint64_t>)(w.body_.last_ - w.pos_, sr_.limit()),
  335. (std::numeric_limits<boost::winapi::DWORD_>::max)()));
  336. net::windows::overlapped_ptr overlapped{
  337. sock_.get_executor(), std::move(*this)};
  338. // Note that we have moved *this, so we cannot access
  339. // the handler since it is now moved-from. We can still
  340. // access simple things like references and built-in types.
  341. auto& ov = *overlapped.get();
  342. ov.Offset = lowPart(w.pos_);
  343. ov.OffsetHigh = highPart(w.pos_);
  344. auto const bSuccess = ::TransmitFile(
  345. sock_.native_handle(),
  346. sr_.get().body().file_.native_handle(),
  347. nNumberOfBytesToWrite,
  348. 0,
  349. overlapped.get(),
  350. nullptr,
  351. 0);
  352. auto const dwError = boost::winapi::GetLastError();
  353. if(! bSuccess && dwError !=
  354. boost::winapi::ERROR_IO_PENDING_)
  355. {
  356. // VFALCO This needs review, is 0 the right number?
  357. // completed immediately (with error?)
  358. overlapped.complete(error_code{static_cast<int>(dwError),
  359. system_category()}, 0);
  360. return;
  361. }
  362. overlapped.release();
  363. }
  364. void
  365. operator()(
  366. error_code ec,
  367. std::size_t bytes_transferred = 0)
  368. {
  369. bytes_transferred_ += bytes_transferred;
  370. if(! ec)
  371. {
  372. if(header_)
  373. {
  374. header_ = false;
  375. return (*this)();
  376. }
  377. auto& w = sr_.writer_impl();
  378. w.pos_ += bytes_transferred;
  379. BOOST_ASSERT(w.pos_ <= w.body_.last_);
  380. if(w.pos_ >= w.body_.last_)
  381. {
  382. sr_.next(ec, null_lambda{});
  383. BOOST_ASSERT(! ec);
  384. BOOST_ASSERT(sr_.is_done());
  385. }
  386. }
  387. this->complete_now(ec, bytes_transferred_);
  388. }
  389. };
  390. struct run_write_some_win32_op
  391. {
  392. template<
  393. class Protocol, class Executor,
  394. bool isRequest, class Fields,
  395. class WriteHandler>
  396. void
  397. operator()(
  398. WriteHandler&& h,
  399. net::basic_stream_socket<
  400. Protocol, Executor>* s,
  401. serializer<isRequest,
  402. basic_file_body<file_win32>, Fields>* sr)
  403. {
  404. // If you get an error on the following line it means
  405. // that your handler does not meet the documented type
  406. // requirements for the handler.
  407. static_assert(
  408. beast::detail::is_invocable<WriteHandler,
  409. void(error_code, std::size_t)>::value,
  410. "WriteHandler type requirements not met");
  411. write_some_win32_op<
  412. Protocol, Executor,
  413. isRequest, Fields,
  414. typename std::decay<WriteHandler>::type>(
  415. std::forward<WriteHandler>(h), *s, *sr);
  416. }
  417. };
  418. #endif
  419. } // detail
  420. //------------------------------------------------------------------------------
  421. template<
  422. class Protocol, class Executor,
  423. bool isRequest, class Fields>
  424. std::size_t
  425. write_some(
  426. net::basic_stream_socket<
  427. Protocol, Executor>& sock,
  428. serializer<isRequest,
  429. basic_file_body<file_win32>, Fields>& sr,
  430. error_code& ec)
  431. {
  432. if(! sr.is_header_done())
  433. {
  434. sr.split(true);
  435. auto const bytes_transferred =
  436. detail::write_some_impl(sock, sr, ec);
  437. if(ec)
  438. return bytes_transferred;
  439. return bytes_transferred;
  440. }
  441. if(sr.get().chunked())
  442. {
  443. auto const bytes_transferred =
  444. detail::write_some_impl(sock, sr, ec);
  445. if(ec)
  446. return bytes_transferred;
  447. return bytes_transferred;
  448. }
  449. auto& w = sr.writer_impl();
  450. w.body_.file_.seek(w.pos_, ec);
  451. if(ec)
  452. return 0;
  453. boost::winapi::DWORD_ const nNumberOfBytesToWrite =
  454. static_cast<boost::winapi::DWORD_>(
  455. (std::min<std::uint64_t>)(
  456. (std::min<std::uint64_t>)(w.body_.last_ - w.pos_, sr.limit()),
  457. (std::numeric_limits<boost::winapi::DWORD_>::max)()));
  458. auto const bSuccess = ::TransmitFile(
  459. sock.native_handle(),
  460. w.body_.file_.native_handle(),
  461. nNumberOfBytesToWrite,
  462. 0,
  463. nullptr,
  464. nullptr,
  465. 0);
  466. if(! bSuccess)
  467. {
  468. ec.assign(static_cast<int>(
  469. boost::winapi::GetLastError()),
  470. system_category());
  471. return 0;
  472. }
  473. w.pos_ += nNumberOfBytesToWrite;
  474. BOOST_ASSERT(w.pos_ <= w.body_.last_);
  475. if(w.pos_ < w.body_.last_)
  476. {
  477. ec = {};
  478. }
  479. else
  480. {
  481. sr.next(ec, detail::null_lambda{});
  482. BOOST_ASSERT(! ec);
  483. BOOST_ASSERT(sr.is_done());
  484. }
  485. return nNumberOfBytesToWrite;
  486. }
  487. #if BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
  488. template<
  489. class Protocol, class Executor,
  490. bool isRequest, class Fields,
  491. class WriteHandler>
  492. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  493. async_write_some(
  494. net::basic_stream_socket<
  495. Protocol, Executor>& sock,
  496. serializer<isRequest,
  497. basic_file_body<file_win32>, Fields>& sr,
  498. WriteHandler&& handler)
  499. {
  500. return net::async_initiate<
  501. WriteHandler,
  502. void(error_code, std::size_t)>(
  503. detail::run_write_some_win32_op{},
  504. handler,
  505. &sock,
  506. &sr);
  507. }
  508. #endif
  509. } // http
  510. } // beast
  511. } // boost
  512. #endif
  513. #endif