basic_file_body.hpp 15 KB

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