basic_file_body.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 BHO_BEAST_HTTP_BASIC_FILE_BODY_HPP
  10. #define BHO_BEAST_HTTP_BASIC_FILE_BODY_HPP
  11. #include <asio2/bho/beast/core/detail/config.hpp>
  12. #include <asio2/bho/beast/core/error.hpp>
  13. #include <asio2/bho/beast/core/file_base.hpp>
  14. #include <asio2/bho/beast/http/message.hpp>
  15. #include <asio2/bho/assert.hpp>
  16. #include <optional>
  17. #include <algorithm>
  18. #include <cstdio>
  19. #include <cstdint>
  20. #include <utility>
  21. namespace bho {
  22. namespace beast {
  23. namespace http {
  24. //[example_http_file_body_1
  25. /** A message body represented by a file on the filesystem.
  26. Messages with this type have bodies represented by a
  27. file on the file system. When parsing a message using
  28. this body type, the data is stored in the file pointed
  29. to by the path, which must be writable. When serializing,
  30. the implementation will read the file and present those
  31. octets as the body content. This may be used to serve
  32. content from a directory as part of a web service.
  33. @tparam File The implementation to use for accessing files.
  34. This type must meet the requirements of <em>File</em>.
  35. */
  36. template<class File>
  37. struct basic_file_body
  38. {
  39. // Make sure the type meets the requirements
  40. static_assert(is_file<File>::value,
  41. "File type requirements not met");
  42. /// The type of File this body uses
  43. using file_type = File;
  44. // Algorithm for storing buffers when parsing.
  45. class reader;
  46. // Algorithm for retrieving buffers when serializing.
  47. class writer;
  48. // The type of the @ref message::body member.
  49. class value_type;
  50. /** Returns the size of the body
  51. @param body The file body to use
  52. */
  53. static
  54. std::uint64_t
  55. size(value_type const& body);
  56. };
  57. //]
  58. //[example_http_file_body_2
  59. /** The type of the @ref message::body member.
  60. Messages declared using `basic_file_body` will have this type for
  61. the body member. This rich class interface allow the file to be
  62. opened with the file handle maintained directly in the object,
  63. which is attached to the message.
  64. */
  65. template<class File>
  66. class basic_file_body<File>::value_type
  67. {
  68. // This body container holds a handle to the file
  69. // when it is open, and also caches the size when set.
  70. #ifndef BHO_BEAST_DOXYGEN
  71. friend class reader;
  72. friend class writer;
  73. friend struct basic_file_body;
  74. #endif
  75. // This represents the open file
  76. std::shared_ptr<File> file_ = std::make_shared<File>();
  77. // The cached file size
  78. std::uint64_t file_size_ = 0;
  79. public:
  80. /** Destructor.
  81. If the file is open, it is closed first.
  82. */
  83. ~value_type() = default;
  84. /// Constructor
  85. value_type() = default;
  86. /// Constructor
  87. value_type(value_type const&) = default;
  88. /// Assignment
  89. value_type& operator=(value_type const&) = default;
  90. /// Constructor
  91. value_type(value_type&& other)
  92. : file_(std::move(other.file_)), file_size_(std::move(other.file_size_))
  93. {
  94. other.file_ = std::make_shared<File>();
  95. other.file_size_ = 0;
  96. }
  97. /// Move assignment
  98. value_type& operator=(value_type&& other)
  99. {
  100. file_ = std::move(other.file_);
  101. file_size_ = std::move(other.file_size_);
  102. other.file_ = std::make_shared<File>();
  103. other.file_size_ = 0;
  104. return *this;
  105. }
  106. /// Return the file
  107. File& file()
  108. {
  109. return *file_;
  110. }
  111. /// Return the file shared_ptr
  112. std::shared_ptr<File> file_ptr()
  113. {
  114. return file_;
  115. }
  116. /// Returns `true` if the file is open
  117. bool
  118. is_open() const
  119. {
  120. return file_->is_open();
  121. }
  122. /// Returns the size of the file if open
  123. std::uint64_t
  124. size() const
  125. {
  126. return file_size_;
  127. }
  128. /// Close the file if open
  129. void
  130. close();
  131. /** Open a file at the given path with the specified mode
  132. @param path The utf-8 encoded path to the file
  133. @param mode The file mode to use
  134. @param ec Set to the error, if any occurred
  135. */
  136. void
  137. open(char const* path, file_mode mode, error_code& ec);
  138. /** Set the open file
  139. This function is used to set the open file. Any previously
  140. set file will be closed.
  141. @param file The file to set. The file must be open or else
  142. an error occurs
  143. @param ec Set to the error, if any occurred
  144. */
  145. void
  146. reset(File&& file, error_code& ec);
  147. void
  148. reset(std::shared_ptr<File> file, error_code& ec);
  149. /** Set the cursor position of the file.
  150. This function can be used to move the cursor of the file ahead
  151. so that only a part gets read. This file will also adjust the
  152. value_type, in case the file is already part of a body.
  153. @param offset The offset in bytes from the beginning of the file
  154. @param ec Set to the error, if any occurred
  155. */
  156. void seek(std::uint64_t offset, error_code& ec);
  157. };
  158. template<class File>
  159. void
  160. basic_file_body<File>::
  161. value_type::
  162. close()
  163. {
  164. error_code ignored;
  165. file_->close(ignored);
  166. }
  167. template<class File>
  168. void
  169. basic_file_body<File>::
  170. value_type::
  171. open(char const* path, file_mode mode, error_code& ec)
  172. {
  173. // Open the file
  174. file_->open(path, mode, ec);
  175. if(ec)
  176. return;
  177. // Cache the size
  178. file_size_ = file_->size(ec);
  179. if(ec)
  180. {
  181. close();
  182. return;
  183. }
  184. }
  185. template<class File>
  186. void
  187. basic_file_body<File>::
  188. value_type::
  189. reset(File&& file, error_code& ec)
  190. {
  191. // First close the file if open
  192. if(file_->is_open())
  193. {
  194. error_code ignored;
  195. file_->close(ignored);
  196. }
  197. // Take ownership of the new file
  198. *file_ = std::move(file);
  199. // Cache the size
  200. file_size_ = file_->size(ec);
  201. }
  202. template<class File>
  203. void
  204. basic_file_body<File>::
  205. value_type::
  206. reset(std::shared_ptr<File> file, error_code& ec)
  207. {
  208. // First close the file if open
  209. if(file_->is_open())
  210. {
  211. error_code ignored;
  212. file_->close(ignored);
  213. }
  214. // Take ownership of the new file
  215. file_ = std::move(file);
  216. // Cache the size
  217. file_size_ = file_->size(ec);
  218. // Consider the offset
  219. if (!ec)
  220. file_size_ -= file_->pos(ec);
  221. }
  222. template<class File>
  223. void
  224. basic_file_body<File>::
  225. value_type::
  226. seek(std::uint64_t offset, error_code& ec)
  227. {
  228. file_->seek(offset, ec);
  229. // Cache the size
  230. if (!ec)
  231. file_size_ = file_->size(ec);
  232. // Consider the offset
  233. if (!ec)
  234. file_size_ -= file_->pos(ec);
  235. }
  236. // This is called from message::payload_size
  237. template<class File>
  238. std::uint64_t
  239. basic_file_body<File>::
  240. size(value_type const& body)
  241. {
  242. // Forward the call to the body
  243. return body.size();
  244. }
  245. //]
  246. //[example_http_file_body_3
  247. /** Algorithm for retrieving buffers when serializing.
  248. Objects of this type are created during serialization
  249. to extract the buffers representing the body.
  250. */
  251. template<class File>
  252. class basic_file_body<File>::writer
  253. {
  254. value_type& body_; // The body we are reading from
  255. std::uint64_t remain_; // The number of unread bytes
  256. char buf_[BHO_BEAST_FILE_BUFFER_SIZE]; // Small buffer for reading
  257. public:
  258. // The type of buffer sequence returned by `get`.
  259. //
  260. using const_buffers_type =
  261. net::const_buffer;
  262. // Constructor.
  263. //
  264. // `h` holds the headers of the message we are
  265. // serializing, while `b` holds the body.
  266. //
  267. // Note that the message is passed by non-const reference.
  268. // This is intentional, because reading from the file
  269. // changes its "current position" which counts makes the
  270. // operation logically not-const (although it is bitwise
  271. // const).
  272. //
  273. // The BodyWriter concept allows the writer to choose
  274. // whether to take the message by const reference or
  275. // non-const reference. Depending on the choice, a
  276. // serializer constructed using that body type will
  277. // require the same const or non-const reference to
  278. // construct.
  279. //
  280. // Readers which accept const messages usually allow
  281. // the same body to be serialized by multiple threads
  282. // concurrently, while readers accepting non-const
  283. // messages may only be serialized by one thread at
  284. // a time.
  285. //
  286. template<bool isRequest, class Fields>
  287. writer(header<isRequest, Fields>& h, value_type& b);
  288. // Initializer
  289. //
  290. // This is called before the body is serialized and
  291. // gives the writer a chance to do something that might
  292. // need to return an error code.
  293. //
  294. void
  295. init(error_code& ec);
  296. // This function is called zero or more times to
  297. // retrieve buffers. A return value of `std::nullopt`
  298. // means there are no more buffers. Otherwise,
  299. // the contained pair will have the next buffer
  300. // to serialize, and a `bool` indicating whether
  301. // or not there may be additional buffers.
  302. std::optional<std::pair<const_buffers_type, bool>>
  303. get(error_code& ec);
  304. };
  305. //]
  306. //[example_http_file_body_4
  307. // Here we just stash a reference to the path for later.
  308. // Rather than dealing with messy constructor exceptions,
  309. // we save the things that might fail for the call to `init`.
  310. //
  311. template<class File>
  312. template<bool isRequest, class Fields>
  313. basic_file_body<File>::
  314. writer::
  315. writer(header<isRequest, Fields>& h, value_type& b)
  316. : body_(b)
  317. {
  318. bho::ignore_unused(h);
  319. // The file must already be open
  320. //BHO_ASSERT(body_.file_->is_open());
  321. // Get the size of the file
  322. remain_ = body_.file_size_;
  323. }
  324. // Initializer
  325. template<class File>
  326. void
  327. basic_file_body<File>::
  328. writer::
  329. init(error_code& ec)
  330. {
  331. // The error_code specification requires that we
  332. // either set the error to some value, or set it
  333. // to indicate no error.
  334. //
  335. // We don't do anything fancy so set "no error"
  336. // The file must already be open
  337. BHO_ASSERT(body_.file_->is_open());
  338. body_.file_->seek(0, ec);
  339. // Get the size of the file
  340. remain_ = ec ? 0 : body_.file_size_;
  341. //ec = {};
  342. }
  343. // This function is called repeatedly by the serializer to
  344. // retrieve the buffers representing the body. Our strategy
  345. // is to read into our buffer and return it until we have
  346. // read through the whole file.
  347. //
  348. template<class File>
  349. auto
  350. basic_file_body<File>::
  351. writer::
  352. get(error_code& ec) ->
  353. std::optional<std::pair<const_buffers_type, bool>>
  354. {
  355. // Calculate the smaller of our buffer size,
  356. // or the amount of unread data in the file.
  357. auto const amount = remain_ > sizeof(buf_) ?
  358. sizeof(buf_) : static_cast<std::size_t>(remain_);
  359. // Handle the case where the file is zero length
  360. if(amount == 0)
  361. {
  362. // Modify the error code to indicate success
  363. // This is required by the error_code specification.
  364. //
  365. // NOTE We use the existing category instead of calling
  366. // into the library to get the generic category because
  367. // that saves us a possibly expensive atomic operation.
  368. //
  369. ec = {};
  370. return std::nullopt;
  371. }
  372. // Now read the next buffer
  373. auto const nread = body_.file_->read(buf_, amount, ec);
  374. if(ec)
  375. return std::nullopt;
  376. if (nread == 0)
  377. {
  378. BHO_BEAST_ASSIGN_EC(ec, error::short_read);
  379. return std::nullopt;
  380. }
  381. // Make sure there is forward progress
  382. BHO_ASSERT(nread != 0);
  383. BHO_ASSERT(nread <= remain_);
  384. // Update the amount remaining based on what we got
  385. remain_ -= nread;
  386. // Return the buffer to the caller.
  387. //
  388. // The second element of the pair indicates whether or
  389. // not there is more data. As long as there is some
  390. // unread bytes, there will be more data. Otherwise,
  391. // we set this bool to `false` so we will not be called
  392. // again.
  393. //
  394. ec = {};
  395. return {{
  396. const_buffers_type{buf_, nread}, // buffer to return.
  397. remain_ > 0 // `true` if there are more buffers.
  398. }};
  399. }
  400. //]
  401. //[example_http_file_body_5
  402. /** Algorithm for storing buffers when parsing.
  403. Objects of this type are created during parsing
  404. to store incoming buffers representing the body.
  405. */
  406. template<class File>
  407. class basic_file_body<File>::reader
  408. {
  409. value_type& body_; // The body we are writing to
  410. public:
  411. // Constructor.
  412. //
  413. // This is called after the header is parsed and
  414. // indicates that a non-zero sized body may be present.
  415. // `h` holds the received message headers.
  416. // `b` is an instance of `basic_file_body`.
  417. //
  418. template<bool isRequest, class Fields>
  419. explicit
  420. reader(header<isRequest, Fields>&h, value_type& b);
  421. // Initializer
  422. //
  423. // This is called before the body is parsed and
  424. // gives the reader a chance to do something that might
  425. // need to return an error code. It informs us of
  426. // the payload size (`content_length`) which we can
  427. // optionally use for optimization.
  428. //
  429. void
  430. init(std::optional<std::uint64_t> const&, error_code& ec);
  431. // This function is called one or more times to store
  432. // buffer sequences corresponding to the incoming body.
  433. //
  434. template<class ConstBufferSequence>
  435. std::size_t
  436. put(ConstBufferSequence const& buffers,
  437. error_code& ec);
  438. // This function is called when writing is complete.
  439. // It is an opportunity to perform any final actions
  440. // which might fail, in order to return an error code.
  441. // Operations that might fail should not be attempted in
  442. // destructors, since an exception thrown from there
  443. // would terminate the program.
  444. //
  445. void
  446. finish(error_code& ec);
  447. };
  448. //]
  449. //[example_http_file_body_6
  450. // We don't do much in the reader constructor since the
  451. // file is already open.
  452. //
  453. template<class File>
  454. template<bool isRequest, class Fields>
  455. basic_file_body<File>::
  456. reader::
  457. reader(header<isRequest, Fields>& h, value_type& body)
  458. : body_(body)
  459. {
  460. bho::ignore_unused(h);
  461. }
  462. template<class File>
  463. void
  464. basic_file_body<File>::
  465. reader::
  466. init(
  467. std::optional<std::uint64_t> const& content_length,
  468. error_code& ec)
  469. {
  470. // The file must already be open for writing
  471. BHO_ASSERT(body_.file_->is_open());
  472. // We don't do anything with this but a sophisticated
  473. // application might check available space on the device
  474. // to see if there is enough room to store the body.
  475. bho::ignore_unused(content_length);
  476. // The error_code specification requires that we
  477. // either set the error to some value, or set it
  478. // to indicate no error.
  479. //
  480. // We don't do anything fancy so set "no error"
  481. ec = {};
  482. }
  483. // This will get called one or more times with body buffers
  484. //
  485. template<class File>
  486. template<class ConstBufferSequence>
  487. std::size_t
  488. basic_file_body<File>::
  489. reader::
  490. put(ConstBufferSequence const& buffers, error_code& ec)
  491. {
  492. // This function must return the total number of
  493. // bytes transferred from the input buffers.
  494. std::size_t nwritten = 0;
  495. // Loop over all the buffers in the sequence,
  496. // and write each one to the file.
  497. for(auto it = net::buffer_sequence_begin(buffers);
  498. it != net::buffer_sequence_end(buffers); ++it)
  499. {
  500. // Write this buffer to the file
  501. net::const_buffer buffer = *it;
  502. nwritten += body_.file_->write(
  503. buffer.data(), buffer.size(), ec);
  504. if(ec)
  505. return nwritten;
  506. }
  507. // Indicate success
  508. // This is required by the error_code specification
  509. ec = {};
  510. return nwritten;
  511. }
  512. // Called after writing is done when there's no error.
  513. template<class File>
  514. void
  515. basic_file_body<File>::
  516. reader::
  517. finish(error_code& ec)
  518. {
  519. // This has to be cleared before returning, to
  520. // indicate no error. The specification requires it.
  521. ec = {};
  522. }
  523. //]
  524. #if ! BHO_BEAST_DOXYGEN
  525. // operator<< is not supported for file_body
  526. template<bool isRequest, class File, class Fields>
  527. std::ostream&
  528. operator<<(std::ostream&, message<
  529. isRequest, basic_file_body<File>, Fields> const&) = delete;
  530. #endif
  531. } // http
  532. } // beast
  533. } // bho
  534. #endif