read_until.ipp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. //
  2. // read_until.ipp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_READ_UNTIL_IPP
  11. #define BOOST_ASIO_READ_UNTIL_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <algorithm>
  18. #include <limits>
  19. #include <string>
  20. #include <utility>
  21. #include <boost/asio/detail/pop_options.hpp>
  22. #include <boost/asio/buffer.hpp>
  23. #include <boost/asio/buffers_iterator.hpp>
  24. #include <boost/asio/detail/bind_handler.hpp>
  25. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  26. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  27. #include <boost/asio/detail/throw_error.hpp>
  28. namespace boost {
  29. namespace asio {
  30. template <typename SyncReadStream, typename Allocator>
  31. inline std::size_t read_until(SyncReadStream& s,
  32. boost::asio::basic_streambuf<Allocator>& b, char delim)
  33. {
  34. boost::system::error_code ec;
  35. std::size_t bytes_transferred = read_until(s, b, delim, ec);
  36. boost::asio::detail::throw_error(ec);
  37. return bytes_transferred;
  38. }
  39. template <typename SyncReadStream, typename Allocator>
  40. std::size_t read_until(SyncReadStream& s,
  41. boost::asio::basic_streambuf<Allocator>& b, char delim,
  42. boost::system::error_code& ec)
  43. {
  44. std::size_t next_search_start = 0;
  45. for (;;)
  46. {
  47. // Determine the range of the data to be searched.
  48. typedef typename boost::asio::basic_streambuf<
  49. Allocator>::const_buffers_type const_buffers_type;
  50. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  51. const_buffers_type buffers = b.data();
  52. iterator begin = iterator::begin(buffers);
  53. iterator start = begin + next_search_start;
  54. iterator end = iterator::end(buffers);
  55. // Look for a match.
  56. iterator iter = std::find(start, end, delim);
  57. if (iter != end)
  58. {
  59. // Found a match. We're done.
  60. ec = boost::system::error_code();
  61. return iter - begin + 1;
  62. }
  63. else
  64. {
  65. // No match. Next search can start with the new data.
  66. next_search_start = end - begin;
  67. }
  68. // Check if buffer is full.
  69. if (b.size() == b.max_size())
  70. {
  71. ec = error::not_found;
  72. return 0;
  73. }
  74. // Need more data.
  75. std::size_t bytes_available =
  76. std::min<std::size_t>(512, b.max_size() - b.size());
  77. b.commit(s.read_some(b.prepare(bytes_available), ec));
  78. if (ec)
  79. return 0;
  80. }
  81. }
  82. template <typename SyncReadStream, typename Allocator>
  83. inline std::size_t read_until(SyncReadStream& s,
  84. boost::asio::basic_streambuf<Allocator>& b, const std::string& delim)
  85. {
  86. boost::system::error_code ec;
  87. std::size_t bytes_transferred = read_until(s, b, delim, ec);
  88. boost::asio::detail::throw_error(ec);
  89. return bytes_transferred;
  90. }
  91. namespace detail
  92. {
  93. // Algorithm that finds a subsequence of equal values in a sequence. Returns
  94. // (iterator,true) if a full match was found, in which case the iterator
  95. // points to the beginning of the match. Returns (iterator,false) if a
  96. // partial match was found at the end of the first sequence, in which case
  97. // the iterator points to the beginning of the partial match. Returns
  98. // (last1,false) if no full or partial match was found.
  99. template <typename Iterator1, typename Iterator2>
  100. std::pair<Iterator1, bool> partial_search(
  101. Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
  102. {
  103. for (Iterator1 iter1 = first1; iter1 != last1; ++iter1)
  104. {
  105. Iterator1 test_iter1 = iter1;
  106. Iterator2 test_iter2 = first2;
  107. for (;; ++test_iter1, ++test_iter2)
  108. {
  109. if (test_iter2 == last2)
  110. return std::make_pair(iter1, true);
  111. if (test_iter1 == last1)
  112. {
  113. if (test_iter2 != first2)
  114. return std::make_pair(iter1, false);
  115. else
  116. break;
  117. }
  118. if (*test_iter1 != *test_iter2)
  119. break;
  120. }
  121. }
  122. return std::make_pair(last1, false);
  123. }
  124. } // namespace detail
  125. template <typename SyncReadStream, typename Allocator>
  126. std::size_t read_until(SyncReadStream& s,
  127. boost::asio::basic_streambuf<Allocator>& b, const std::string& delim,
  128. boost::system::error_code& ec)
  129. {
  130. std::size_t next_search_start = 0;
  131. for (;;)
  132. {
  133. // Determine the range of the data to be searched.
  134. typedef typename boost::asio::basic_streambuf<
  135. Allocator>::const_buffers_type const_buffers_type;
  136. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  137. const_buffers_type buffers = b.data();
  138. iterator begin = iterator::begin(buffers);
  139. iterator start = begin + next_search_start;
  140. iterator end = iterator::end(buffers);
  141. // Look for a match.
  142. std::pair<iterator, bool> result = boost::asio::detail::partial_search(
  143. start, end, delim.begin(), delim.end());
  144. if (result.first != end)
  145. {
  146. if (result.second)
  147. {
  148. // Full match. We're done.
  149. ec = boost::system::error_code();
  150. return result.first - begin + delim.length();
  151. }
  152. else
  153. {
  154. // Partial match. Next search needs to start from beginning of match.
  155. next_search_start = result.first - begin;
  156. }
  157. }
  158. else
  159. {
  160. // No match. Next search can start with the new data.
  161. next_search_start = end - begin;
  162. }
  163. // Check if buffer is full.
  164. if (b.size() == b.max_size())
  165. {
  166. ec = error::not_found;
  167. return 0;
  168. }
  169. // Need more data.
  170. std::size_t bytes_available =
  171. std::min<std::size_t>(512, b.max_size() - b.size());
  172. b.commit(s.read_some(b.prepare(bytes_available), ec));
  173. if (ec)
  174. return 0;
  175. }
  176. }
  177. template <typename SyncReadStream, typename Allocator>
  178. inline std::size_t read_until(SyncReadStream& s,
  179. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr)
  180. {
  181. boost::system::error_code ec;
  182. std::size_t bytes_transferred = read_until(s, b, expr, ec);
  183. boost::asio::detail::throw_error(ec);
  184. return bytes_transferred;
  185. }
  186. template <typename SyncReadStream, typename Allocator>
  187. std::size_t read_until(SyncReadStream& s,
  188. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  189. boost::system::error_code& ec)
  190. {
  191. std::size_t next_search_start = 0;
  192. for (;;)
  193. {
  194. // Determine the range of the data to be searched.
  195. typedef typename boost::asio::basic_streambuf<
  196. Allocator>::const_buffers_type const_buffers_type;
  197. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  198. const_buffers_type buffers = b.data();
  199. iterator begin = iterator::begin(buffers);
  200. iterator start = begin + next_search_start;
  201. iterator end = iterator::end(buffers);
  202. // Look for a match.
  203. boost::match_results<iterator> match_results;
  204. if (boost::regex_search(start, end, match_results, expr,
  205. boost::match_default | boost::match_partial))
  206. {
  207. if (match_results[0].matched)
  208. {
  209. // Full match. We're done.
  210. ec = boost::system::error_code();
  211. return match_results[0].second - begin;
  212. }
  213. else
  214. {
  215. // Partial match. Next search needs to start from beginning of match.
  216. next_search_start = match_results[0].first - begin;
  217. }
  218. }
  219. else
  220. {
  221. // No match. Next search can start with the new data.
  222. next_search_start = end - begin;
  223. }
  224. // Check if buffer is full.
  225. if (b.size() == b.max_size())
  226. {
  227. ec = error::not_found;
  228. return 0;
  229. }
  230. // Need more data.
  231. std::size_t bytes_available =
  232. std::min<std::size_t>(512, b.max_size() - b.size());
  233. b.commit(s.read_some(b.prepare(bytes_available), ec));
  234. if (ec)
  235. return 0;
  236. }
  237. }
  238. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  239. std::size_t read_until(SyncReadStream& s,
  240. boost::asio::basic_streambuf<Allocator>& b,
  241. MatchCondition match_condition, boost::system::error_code& ec,
  242. typename boost::enable_if<is_match_condition<MatchCondition> >::type*)
  243. {
  244. std::size_t next_search_start = 0;
  245. for (;;)
  246. {
  247. // Determine the range of the data to be searched.
  248. typedef typename boost::asio::basic_streambuf<
  249. Allocator>::const_buffers_type const_buffers_type;
  250. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  251. const_buffers_type buffers = b.data();
  252. iterator begin = iterator::begin(buffers);
  253. iterator start = begin + next_search_start;
  254. iterator end = iterator::end(buffers);
  255. // Look for a match.
  256. std::pair<iterator, bool> result = match_condition(start, end);
  257. if (result.second)
  258. {
  259. // Full match. We're done.
  260. ec = boost::system::error_code();
  261. return result.first - begin;
  262. }
  263. else if (result.first != end)
  264. {
  265. // Partial match. Next search needs to start from beginning of match.
  266. next_search_start = result.first - begin;
  267. }
  268. else
  269. {
  270. // No match. Next search can start with the new data.
  271. next_search_start = end - begin;
  272. }
  273. // Check if buffer is full.
  274. if (b.size() == b.max_size())
  275. {
  276. ec = error::not_found;
  277. return 0;
  278. }
  279. // Need more data.
  280. std::size_t bytes_available =
  281. std::min<std::size_t>(512, b.max_size() - b.size());
  282. b.commit(s.read_some(b.prepare(bytes_available), ec));
  283. if (ec)
  284. return 0;
  285. }
  286. }
  287. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  288. inline std::size_t read_until(SyncReadStream& s,
  289. boost::asio::basic_streambuf<Allocator>& b, MatchCondition match_condition,
  290. typename boost::enable_if<is_match_condition<MatchCondition> >::type*)
  291. {
  292. boost::system::error_code ec;
  293. std::size_t bytes_transferred = read_until(s, b, match_condition, ec);
  294. boost::asio::detail::throw_error(ec);
  295. return bytes_transferred;
  296. }
  297. namespace detail
  298. {
  299. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  300. class read_until_delim_handler
  301. {
  302. public:
  303. read_until_delim_handler(AsyncReadStream& stream,
  304. boost::asio::basic_streambuf<Allocator>& streambuf, char delim,
  305. std::size_t next_search_start, ReadHandler handler)
  306. : stream_(stream),
  307. streambuf_(streambuf),
  308. delim_(delim),
  309. next_search_start_(next_search_start),
  310. handler_(handler)
  311. {
  312. }
  313. void operator()(const boost::system::error_code& ec,
  314. std::size_t bytes_transferred)
  315. {
  316. // Check for errors.
  317. if (ec)
  318. {
  319. std::size_t bytes = 0;
  320. handler_(ec, bytes);
  321. return;
  322. }
  323. // Commit received data to streambuf's get area.
  324. streambuf_.commit(bytes_transferred);
  325. // Determine the range of the data to be searched.
  326. typedef typename boost::asio::basic_streambuf<
  327. Allocator>::const_buffers_type const_buffers_type;
  328. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  329. const_buffers_type buffers = streambuf_.data();
  330. iterator begin = iterator::begin(buffers);
  331. iterator start = begin + next_search_start_;
  332. iterator end = iterator::end(buffers);
  333. // Look for a match.
  334. iterator iter = std::find(start, end, delim_);
  335. if (iter != end)
  336. {
  337. // Found a match. We're done.
  338. std::size_t bytes = iter - begin + 1;
  339. handler_(ec, bytes);
  340. return;
  341. }
  342. // No match. Check if buffer is full.
  343. if (streambuf_.size() == streambuf_.max_size())
  344. {
  345. std::size_t bytes = 0;
  346. boost::system::error_code ec(error::not_found);
  347. handler_(ec, bytes);
  348. return;
  349. }
  350. // Next search can start with the new data.
  351. next_search_start_ = end - begin;
  352. // Start a new asynchronous read operation to obtain more data.
  353. std::size_t bytes_available =
  354. std::min<std::size_t>(512, streambuf_.max_size() - streambuf_.size());
  355. stream_.async_read_some(streambuf_.prepare(bytes_available), *this);
  356. }
  357. //private:
  358. AsyncReadStream& stream_;
  359. boost::asio::basic_streambuf<Allocator>& streambuf_;
  360. char delim_;
  361. std::size_t next_search_start_;
  362. ReadHandler handler_;
  363. };
  364. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  365. inline void* asio_handler_allocate(std::size_t size,
  366. read_until_delim_handler<AsyncReadStream,
  367. Allocator, ReadHandler>* this_handler)
  368. {
  369. return boost_asio_handler_alloc_helpers::allocate(
  370. size, &this_handler->handler_);
  371. }
  372. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  373. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  374. read_until_delim_handler<AsyncReadStream,
  375. Allocator, ReadHandler>* this_handler)
  376. {
  377. boost_asio_handler_alloc_helpers::deallocate(
  378. pointer, size, &this_handler->handler_);
  379. }
  380. template <typename Function, typename AsyncReadStream, typename Allocator,
  381. typename ReadHandler>
  382. inline void asio_handler_invoke(const Function& function,
  383. read_until_delim_handler<AsyncReadStream,
  384. Allocator, ReadHandler>* this_handler)
  385. {
  386. boost_asio_handler_invoke_helpers::invoke(
  387. function, &this_handler->handler_);
  388. }
  389. } // namespace detail
  390. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  391. void async_read_until(AsyncReadStream& s,
  392. boost::asio::basic_streambuf<Allocator>& b, char delim, ReadHandler handler)
  393. {
  394. // Determine the range of the data to be searched.
  395. typedef typename boost::asio::basic_streambuf<
  396. Allocator>::const_buffers_type const_buffers_type;
  397. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  398. const_buffers_type buffers = b.data();
  399. iterator begin = iterator::begin(buffers);
  400. iterator end = iterator::end(buffers);
  401. // Look for a match.
  402. iterator iter = std::find(begin, end, delim);
  403. if (iter != end)
  404. {
  405. // Found a match. We're done.
  406. boost::system::error_code ec;
  407. std::size_t bytes = iter - begin + 1;
  408. s.get_io_service().post(detail::bind_handler(handler, ec, bytes));
  409. return;
  410. }
  411. // No match. Check if buffer is full.
  412. if (b.size() == b.max_size())
  413. {
  414. boost::system::error_code ec(error::not_found);
  415. s.get_io_service().post(detail::bind_handler(handler, ec, 0));
  416. return;
  417. }
  418. // Start a new asynchronous read operation to obtain more data.
  419. std::size_t bytes_available =
  420. std::min<std::size_t>(512, b.max_size() - b.size());
  421. s.async_read_some(b.prepare(bytes_available),
  422. detail::read_until_delim_handler<AsyncReadStream, Allocator, ReadHandler>(
  423. s, b, delim, end - begin, handler));
  424. }
  425. namespace detail
  426. {
  427. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  428. class read_until_delim_string_handler
  429. {
  430. public:
  431. read_until_delim_string_handler(AsyncReadStream& stream,
  432. boost::asio::basic_streambuf<Allocator>& streambuf,
  433. const std::string& delim, std::size_t next_search_start,
  434. ReadHandler handler)
  435. : stream_(stream),
  436. streambuf_(streambuf),
  437. delim_(delim),
  438. next_search_start_(next_search_start),
  439. handler_(handler)
  440. {
  441. }
  442. void operator()(const boost::system::error_code& ec,
  443. std::size_t bytes_transferred)
  444. {
  445. // Check for errors.
  446. if (ec)
  447. {
  448. std::size_t bytes = 0;
  449. handler_(ec, bytes);
  450. return;
  451. }
  452. // Commit received data to streambuf's get area.
  453. streambuf_.commit(bytes_transferred);
  454. // Determine the range of the data to be searched.
  455. typedef typename boost::asio::basic_streambuf<
  456. Allocator>::const_buffers_type const_buffers_type;
  457. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  458. const_buffers_type buffers = streambuf_.data();
  459. iterator begin = iterator::begin(buffers);
  460. iterator start = begin + next_search_start_;
  461. iterator end = iterator::end(buffers);
  462. // Look for a match.
  463. std::pair<iterator, bool> result = boost::asio::detail::partial_search(
  464. start, end, delim_.begin(), delim_.end());
  465. if (result.first != end)
  466. {
  467. if (result.second)
  468. {
  469. // Full match. We're done.
  470. std::size_t bytes = result.first - begin + delim_.length();
  471. handler_(ec, bytes);
  472. return;
  473. }
  474. else
  475. {
  476. // Partial match. Next search needs to start from beginning of match.
  477. next_search_start_ = result.first - begin;
  478. }
  479. }
  480. else
  481. {
  482. // No match. Next search can start with the new data.
  483. next_search_start_ = end - begin;
  484. }
  485. // Check if buffer is full.
  486. if (streambuf_.size() == streambuf_.max_size())
  487. {
  488. std::size_t bytes = 0;
  489. boost::system::error_code ec2(error::not_found);
  490. handler_(ec2, bytes);
  491. return;
  492. }
  493. // Start a new asynchronous read operation to obtain more data.
  494. std::size_t bytes_available =
  495. std::min<std::size_t>(512, streambuf_.max_size() - streambuf_.size());
  496. stream_.async_read_some(streambuf_.prepare(bytes_available), *this);
  497. }
  498. //private:
  499. AsyncReadStream& stream_;
  500. boost::asio::basic_streambuf<Allocator>& streambuf_;
  501. std::string delim_;
  502. std::size_t next_search_start_;
  503. ReadHandler handler_;
  504. };
  505. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  506. inline void* asio_handler_allocate(std::size_t size,
  507. read_until_delim_string_handler<AsyncReadStream,
  508. Allocator, ReadHandler>* this_handler)
  509. {
  510. return boost_asio_handler_alloc_helpers::allocate(
  511. size, &this_handler->handler_);
  512. }
  513. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  514. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  515. read_until_delim_string_handler<AsyncReadStream,
  516. Allocator, ReadHandler>* this_handler)
  517. {
  518. boost_asio_handler_alloc_helpers::deallocate(
  519. pointer, size, &this_handler->handler_);
  520. }
  521. template <typename Function, typename AsyncReadStream,
  522. typename Allocator, typename ReadHandler>
  523. inline void asio_handler_invoke(const Function& function,
  524. read_until_delim_string_handler<AsyncReadStream,
  525. Allocator, ReadHandler>* this_handler)
  526. {
  527. boost_asio_handler_invoke_helpers::invoke(
  528. function, &this_handler->handler_);
  529. }
  530. } // namespace detail
  531. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  532. void async_read_until(AsyncReadStream& s,
  533. boost::asio::basic_streambuf<Allocator>& b, const std::string& delim,
  534. ReadHandler handler)
  535. {
  536. // Determine the range of the data to be searched.
  537. typedef typename boost::asio::basic_streambuf<
  538. Allocator>::const_buffers_type const_buffers_type;
  539. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  540. const_buffers_type buffers = b.data();
  541. iterator begin = iterator::begin(buffers);
  542. iterator end = iterator::end(buffers);
  543. // Look for a match.
  544. std::size_t next_search_start;
  545. std::pair<iterator, bool> result = boost::asio::detail::partial_search(
  546. begin, end, delim.begin(), delim.end());
  547. if (result.first != end)
  548. {
  549. if (result.second)
  550. {
  551. // Full match. We're done.
  552. boost::system::error_code ec;
  553. std::size_t bytes = result.first - begin + delim.length();
  554. s.get_io_service().post(detail::bind_handler(handler, ec, bytes));
  555. return;
  556. }
  557. else
  558. {
  559. // Partial match. Next search needs to start from beginning of match.
  560. next_search_start = result.first - begin;
  561. }
  562. }
  563. else
  564. {
  565. // No match. Next search can start with the new data.
  566. next_search_start = end - begin;
  567. }
  568. // Check if buffer is full.
  569. if (b.size() == b.max_size())
  570. {
  571. boost::system::error_code ec(error::not_found);
  572. s.get_io_service().post(detail::bind_handler(handler, ec, 0));
  573. return;
  574. }
  575. // Start a new asynchronous read operation to obtain more data.
  576. std::size_t bytes_available =
  577. std::min<std::size_t>(512, b.max_size() - b.size());
  578. s.async_read_some(b.prepare(bytes_available),
  579. detail::read_until_delim_string_handler<
  580. AsyncReadStream, Allocator, ReadHandler>(
  581. s, b, delim, next_search_start, handler));
  582. }
  583. namespace detail
  584. {
  585. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  586. class read_until_expr_handler
  587. {
  588. public:
  589. read_until_expr_handler(AsyncReadStream& stream,
  590. boost::asio::basic_streambuf<Allocator>& streambuf,
  591. const boost::regex& expr, std::size_t next_search_start,
  592. ReadHandler handler)
  593. : stream_(stream),
  594. streambuf_(streambuf),
  595. expr_(expr),
  596. next_search_start_(next_search_start),
  597. handler_(handler)
  598. {
  599. }
  600. void operator()(const boost::system::error_code& ec,
  601. std::size_t bytes_transferred)
  602. {
  603. // Check for errors.
  604. if (ec)
  605. {
  606. std::size_t bytes = 0;
  607. handler_(ec, bytes);
  608. return;
  609. }
  610. // Commit received data to streambuf's get area.
  611. streambuf_.commit(bytes_transferred);
  612. // Determine the range of the data to be searched.
  613. typedef typename boost::asio::basic_streambuf<
  614. Allocator>::const_buffers_type const_buffers_type;
  615. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  616. const_buffers_type buffers = streambuf_.data();
  617. iterator begin = iterator::begin(buffers);
  618. iterator start = begin + next_search_start_;
  619. iterator end = iterator::end(buffers);
  620. // Look for a match.
  621. boost::match_results<iterator> match_results;
  622. if (boost::regex_search(start, end, match_results, expr_,
  623. boost::match_default | boost::match_partial))
  624. {
  625. if (match_results[0].matched)
  626. {
  627. // Full match. We're done.
  628. std::size_t bytes = match_results[0].second - begin;
  629. handler_(ec, bytes);
  630. return;
  631. }
  632. else
  633. {
  634. // Partial match. Next search needs to start from beginning of match.
  635. next_search_start_ = match_results[0].first - begin;
  636. }
  637. }
  638. else
  639. {
  640. // No match. Next search can start with the new data.
  641. next_search_start_ = end - begin;
  642. }
  643. // Check if buffer is full.
  644. if (streambuf_.size() == streambuf_.max_size())
  645. {
  646. std::size_t bytes = 0;
  647. boost::system::error_code ec(error::not_found);
  648. handler_(ec, bytes);
  649. return;
  650. }
  651. // Start a new asynchronous read operation to obtain more data.
  652. std::size_t bytes_available =
  653. std::min<std::size_t>(512, streambuf_.max_size() - streambuf_.size());
  654. stream_.async_read_some(streambuf_.prepare(bytes_available), *this);
  655. }
  656. //private:
  657. AsyncReadStream& stream_;
  658. boost::asio::basic_streambuf<Allocator>& streambuf_;
  659. boost::regex expr_;
  660. std::size_t next_search_start_;
  661. ReadHandler handler_;
  662. };
  663. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  664. inline void* asio_handler_allocate(std::size_t size,
  665. read_until_expr_handler<AsyncReadStream,
  666. Allocator, ReadHandler>* this_handler)
  667. {
  668. return boost_asio_handler_alloc_helpers::allocate(
  669. size, &this_handler->handler_);
  670. }
  671. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  672. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  673. read_until_expr_handler<AsyncReadStream,
  674. Allocator, ReadHandler>* this_handler)
  675. {
  676. boost_asio_handler_alloc_helpers::deallocate(
  677. pointer, size, &this_handler->handler_);
  678. }
  679. template <typename Function, typename AsyncReadStream, typename Allocator,
  680. typename ReadHandler>
  681. inline void asio_handler_invoke(const Function& function,
  682. read_until_expr_handler<AsyncReadStream,
  683. Allocator, ReadHandler>* this_handler)
  684. {
  685. boost_asio_handler_invoke_helpers::invoke(
  686. function, &this_handler->handler_);
  687. }
  688. } // namespace detail
  689. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  690. void async_read_until(AsyncReadStream& s,
  691. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  692. ReadHandler handler)
  693. {
  694. // Determine the range of the data to be searched.
  695. typedef typename boost::asio::basic_streambuf<
  696. Allocator>::const_buffers_type const_buffers_type;
  697. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  698. const_buffers_type buffers = b.data();
  699. iterator begin = iterator::begin(buffers);
  700. iterator end = iterator::end(buffers);
  701. // Look for a match.
  702. std::size_t next_search_start;
  703. boost::match_results<iterator> match_results;
  704. if (boost::regex_search(begin, end, match_results, expr,
  705. boost::match_default | boost::match_partial))
  706. {
  707. if (match_results[0].matched)
  708. {
  709. // Full match. We're done.
  710. boost::system::error_code ec;
  711. std::size_t bytes = match_results[0].second - begin;
  712. s.get_io_service().post(detail::bind_handler(handler, ec, bytes));
  713. return;
  714. }
  715. else
  716. {
  717. // Partial match. Next search needs to start from beginning of match.
  718. next_search_start = match_results[0].first - begin;
  719. }
  720. }
  721. else
  722. {
  723. // No match. Next search can start with the new data.
  724. next_search_start = end - begin;
  725. }
  726. // Check if buffer is full.
  727. if (b.size() == b.max_size())
  728. {
  729. boost::system::error_code ec(error::not_found);
  730. s.get_io_service().post(detail::bind_handler(handler, ec, 0));
  731. return;
  732. }
  733. // Start a new asynchronous read operation to obtain more data.
  734. std::size_t bytes_available =
  735. std::min<std::size_t>(512, b.max_size() - b.size());
  736. s.async_read_some(b.prepare(bytes_available),
  737. detail::read_until_expr_handler<AsyncReadStream, Allocator, ReadHandler>(
  738. s, b, expr, next_search_start, handler));
  739. }
  740. namespace detail
  741. {
  742. template <typename AsyncReadStream, typename Allocator,
  743. typename MatchCondition, typename ReadHandler>
  744. class read_until_match_handler
  745. {
  746. public:
  747. read_until_match_handler(AsyncReadStream& stream,
  748. boost::asio::basic_streambuf<Allocator>& streambuf,
  749. MatchCondition match_condition, std::size_t next_search_start,
  750. ReadHandler handler)
  751. : stream_(stream),
  752. streambuf_(streambuf),
  753. match_condition_(match_condition),
  754. next_search_start_(next_search_start),
  755. handler_(handler)
  756. {
  757. }
  758. void operator()(const boost::system::error_code& ec,
  759. std::size_t bytes_transferred)
  760. {
  761. // Check for errors.
  762. if (ec)
  763. {
  764. std::size_t bytes = 0;
  765. handler_(ec, bytes);
  766. return;
  767. }
  768. // Commit received data to streambuf's get area.
  769. streambuf_.commit(bytes_transferred);
  770. // Determine the range of the data to be searched.
  771. typedef typename boost::asio::basic_streambuf<
  772. Allocator>::const_buffers_type const_buffers_type;
  773. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  774. const_buffers_type buffers = streambuf_.data();
  775. iterator begin = iterator::begin(buffers);
  776. iterator start = begin + next_search_start_;
  777. iterator end = iterator::end(buffers);
  778. // Look for a match.
  779. std::pair<iterator, bool> result = match_condition_(start, end);
  780. if (result.second)
  781. {
  782. // Full match. We're done.
  783. std::size_t bytes = result.first - begin;
  784. handler_(ec, bytes);
  785. return;
  786. }
  787. else if (result.first != end)
  788. {
  789. // Partial match. Next search needs to start from beginning of match.
  790. next_search_start_ = result.first - begin;
  791. }
  792. else
  793. {
  794. // No match. Next search can start with the new data.
  795. next_search_start_ = end - begin;
  796. }
  797. // Check if buffer is full.
  798. if (streambuf_.size() == streambuf_.max_size())
  799. {
  800. std::size_t bytes = 0;
  801. boost::system::error_code ec(error::not_found);
  802. handler_(ec, bytes);
  803. return;
  804. }
  805. // Start a new asynchronous read operation to obtain more data.
  806. std::size_t bytes_available =
  807. std::min<std::size_t>(512, streambuf_.max_size() - streambuf_.size());
  808. stream_.async_read_some(streambuf_.prepare(bytes_available), *this);
  809. }
  810. //private:
  811. AsyncReadStream& stream_;
  812. boost::asio::basic_streambuf<Allocator>& streambuf_;
  813. MatchCondition match_condition_;
  814. std::size_t next_search_start_;
  815. ReadHandler handler_;
  816. };
  817. template <typename AsyncReadStream, typename Allocator,
  818. typename MatchCondition, typename ReadHandler>
  819. inline void* asio_handler_allocate(std::size_t size,
  820. read_until_match_handler<AsyncReadStream,
  821. Allocator, MatchCondition, ReadHandler>* this_handler)
  822. {
  823. return boost_asio_handler_alloc_helpers::allocate(
  824. size, &this_handler->handler_);
  825. }
  826. template <typename AsyncReadStream, typename Allocator,
  827. typename MatchCondition, typename ReadHandler>
  828. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  829. read_until_match_handler<AsyncReadStream,
  830. Allocator, MatchCondition, ReadHandler>* this_handler)
  831. {
  832. boost_asio_handler_alloc_helpers::deallocate(
  833. pointer, size, &this_handler->handler_);
  834. }
  835. template <typename Function, typename AsyncReadStream, typename Allocator,
  836. typename MatchCondition, typename ReadHandler>
  837. inline void asio_handler_invoke(const Function& function,
  838. read_until_match_handler<AsyncReadStream,
  839. Allocator, MatchCondition, ReadHandler>* this_handler)
  840. {
  841. boost_asio_handler_invoke_helpers::invoke(
  842. function, &this_handler->handler_);
  843. }
  844. } // namespace detail
  845. template <typename AsyncReadStream, typename Allocator,
  846. typename MatchCondition, typename ReadHandler>
  847. void async_read_until(AsyncReadStream& s,
  848. boost::asio::basic_streambuf<Allocator>& b,
  849. MatchCondition match_condition, ReadHandler handler,
  850. typename boost::enable_if<is_match_condition<MatchCondition> >::type*)
  851. {
  852. // Determine the range of the data to be searched.
  853. typedef typename boost::asio::basic_streambuf<
  854. Allocator>::const_buffers_type const_buffers_type;
  855. typedef boost::asio::buffers_iterator<const_buffers_type> iterator;
  856. const_buffers_type buffers = b.data();
  857. iterator begin = iterator::begin(buffers);
  858. iterator end = iterator::end(buffers);
  859. // Look for a match.
  860. std::size_t next_search_start;
  861. std::pair<iterator, bool> result = match_condition(begin, end);
  862. if (result.second)
  863. {
  864. // Full match. We're done.
  865. boost::system::error_code ec;
  866. std::size_t bytes = result.first - begin;
  867. s.get_io_service().post(detail::bind_handler(handler, ec, bytes));
  868. return;
  869. }
  870. else if (result.first != end)
  871. {
  872. // Partial match. Next search needs to start from beginning of match.
  873. next_search_start = result.first - begin;
  874. }
  875. else
  876. {
  877. // No match. Next search can start with the new data.
  878. next_search_start = end - begin;
  879. }
  880. // Check if buffer is full.
  881. if (b.size() == b.max_size())
  882. {
  883. boost::system::error_code ec(error::not_found);
  884. s.get_io_service().post(detail::bind_handler(handler, ec, 0));
  885. return;
  886. }
  887. // Start a new asynchronous read operation to obtain more data.
  888. std::size_t bytes_available =
  889. std::min<std::size_t>(512, b.max_size() - b.size());
  890. s.async_read_some(b.prepare(bytes_available),
  891. detail::read_until_match_handler<
  892. AsyncReadStream, Allocator, MatchCondition, ReadHandler>(
  893. s, b, match_condition, next_search_start, handler));
  894. }
  895. } // namespace asio
  896. } // namespace boost
  897. #include <boost/asio/detail/pop_options.hpp>
  898. #endif // BOOST_ASIO_READ_UNTIL_IPP