read_until.ipp 30 KB

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