basic_file_body.hpp 15 KB

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