basic_file_body.hpp 14 KB

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