read_until.hpp 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007
  1. //
  2. // impl/read_until.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_IMPL_READ_UNTIL_HPP
  11. #define BOOST_ASIO_IMPL_READ_UNTIL_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <algorithm>
  16. #include <string>
  17. #include <vector>
  18. #include <utility>
  19. #include <boost/asio/associated_allocator.hpp>
  20. #include <boost/asio/associated_executor.hpp>
  21. #include <boost/asio/buffer.hpp>
  22. #include <boost/asio/buffers_iterator.hpp>
  23. #include <boost/asio/detail/bind_handler.hpp>
  24. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  25. #include <boost/asio/detail/handler_cont_helpers.hpp>
  26. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  27. #include <boost/asio/detail/handler_type_requirements.hpp>
  28. #include <boost/asio/detail/limits.hpp>
  29. #include <boost/asio/detail/non_const_lvalue.hpp>
  30. #include <boost/asio/detail/throw_error.hpp>
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace detail
  35. {
  36. // Algorithm that finds a subsequence of equal values in a sequence. Returns
  37. // (iterator,true) if a full match was found, in which case the iterator
  38. // points to the beginning of the match. Returns (iterator,false) if a
  39. // partial match was found at the end of the first sequence, in which case
  40. // the iterator points to the beginning of the partial match. Returns
  41. // (last1,false) if no full or partial match was found.
  42. template <typename Iterator1, typename Iterator2>
  43. std::pair<Iterator1, bool> partial_search(
  44. Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
  45. {
  46. for (Iterator1 iter1 = first1; iter1 != last1; ++iter1)
  47. {
  48. Iterator1 test_iter1 = iter1;
  49. Iterator2 test_iter2 = first2;
  50. for (;; ++test_iter1, ++test_iter2)
  51. {
  52. if (test_iter2 == last2)
  53. return std::make_pair(iter1, true);
  54. if (test_iter1 == last1)
  55. {
  56. if (test_iter2 != first2)
  57. return std::make_pair(iter1, false);
  58. else
  59. break;
  60. }
  61. if (*test_iter1 != *test_iter2)
  62. break;
  63. }
  64. }
  65. return std::make_pair(last1, false);
  66. }
  67. } // namespace detail
  68. #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  69. template <typename SyncReadStream, typename DynamicBuffer_v1>
  70. inline std::size_t read_until(SyncReadStream& s,
  71. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers, char delim,
  72. typename enable_if<
  73. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  74. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  75. >::type*)
  76. {
  77. boost::system::error_code ec;
  78. std::size_t bytes_transferred = read_until(s,
  79. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers), delim, ec);
  80. boost::asio::detail::throw_error(ec, "read_until");
  81. return bytes_transferred;
  82. }
  83. template <typename SyncReadStream, typename DynamicBuffer_v1>
  84. std::size_t read_until(SyncReadStream& s,
  85. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  86. char delim, boost::system::error_code& ec,
  87. typename enable_if<
  88. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  89. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  90. >::type*)
  91. {
  92. typename decay<DynamicBuffer_v1>::type b(
  93. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers));
  94. std::size_t search_position = 0;
  95. for (;;)
  96. {
  97. // Determine the range of the data to be searched.
  98. typedef typename DynamicBuffer_v1::const_buffers_type buffers_type;
  99. typedef buffers_iterator<buffers_type> iterator;
  100. buffers_type data_buffers = b.data();
  101. iterator begin = iterator::begin(data_buffers);
  102. iterator start_pos = begin + search_position;
  103. iterator end = iterator::end(data_buffers);
  104. // Look for a match.
  105. iterator iter = std::find(start_pos, end, delim);
  106. if (iter != end)
  107. {
  108. // Found a match. We're done.
  109. ec = boost::system::error_code();
  110. return iter - begin + 1;
  111. }
  112. else
  113. {
  114. // No match. Next search can start with the new data.
  115. search_position = end - begin;
  116. }
  117. // Check if buffer is full.
  118. if (b.size() == b.max_size())
  119. {
  120. ec = error::not_found;
  121. return 0;
  122. }
  123. // Need more data.
  124. std::size_t bytes_to_read = std::min<std::size_t>(
  125. std::max<std::size_t>(512, b.capacity() - b.size()),
  126. std::min<std::size_t>(65536, b.max_size() - b.size()));
  127. b.commit(s.read_some(b.prepare(bytes_to_read), ec));
  128. if (ec)
  129. return 0;
  130. }
  131. }
  132. template <typename SyncReadStream, typename DynamicBuffer_v1>
  133. inline std::size_t read_until(SyncReadStream& s,
  134. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  135. BOOST_ASIO_STRING_VIEW_PARAM delim,
  136. typename enable_if<
  137. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  138. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  139. >::type*)
  140. {
  141. boost::system::error_code ec;
  142. std::size_t bytes_transferred = read_until(s,
  143. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers), delim, ec);
  144. boost::asio::detail::throw_error(ec, "read_until");
  145. return bytes_transferred;
  146. }
  147. template <typename SyncReadStream, typename DynamicBuffer_v1>
  148. std::size_t read_until(SyncReadStream& s,
  149. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  150. BOOST_ASIO_STRING_VIEW_PARAM delim, boost::system::error_code& ec,
  151. typename enable_if<
  152. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  153. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  154. >::type*)
  155. {
  156. typename decay<DynamicBuffer_v1>::type b(
  157. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers));
  158. std::size_t search_position = 0;
  159. for (;;)
  160. {
  161. // Determine the range of the data to be searched.
  162. typedef typename DynamicBuffer_v1::const_buffers_type buffers_type;
  163. typedef buffers_iterator<buffers_type> iterator;
  164. buffers_type data_buffers = b.data();
  165. iterator begin = iterator::begin(data_buffers);
  166. iterator start_pos = begin + search_position;
  167. iterator end = iterator::end(data_buffers);
  168. // Look for a match.
  169. std::pair<iterator, bool> result = detail::partial_search(
  170. start_pos, end, delim.begin(), delim.end());
  171. if (result.first != end)
  172. {
  173. if (result.second)
  174. {
  175. // Full match. We're done.
  176. ec = boost::system::error_code();
  177. return result.first - begin + delim.length();
  178. }
  179. else
  180. {
  181. // Partial match. Next search needs to start from beginning of match.
  182. search_position = result.first - begin;
  183. }
  184. }
  185. else
  186. {
  187. // No match. Next search can start with the new data.
  188. search_position = end - begin;
  189. }
  190. // Check if buffer is full.
  191. if (b.size() == b.max_size())
  192. {
  193. ec = error::not_found;
  194. return 0;
  195. }
  196. // Need more data.
  197. std::size_t bytes_to_read = std::min<std::size_t>(
  198. std::max<std::size_t>(512, b.capacity() - b.size()),
  199. std::min<std::size_t>(65536, b.max_size() - b.size()));
  200. b.commit(s.read_some(b.prepare(bytes_to_read), ec));
  201. if (ec)
  202. return 0;
  203. }
  204. }
  205. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  206. #if defined(BOOST_ASIO_HAS_BOOST_REGEX)
  207. template <typename SyncReadStream, typename DynamicBuffer_v1>
  208. inline std::size_t read_until(SyncReadStream& s,
  209. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  210. const boost::regex& expr,
  211. typename enable_if<
  212. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  213. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  214. >::type*)
  215. {
  216. boost::system::error_code ec;
  217. std::size_t bytes_transferred = read_until(s,
  218. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers), expr, ec);
  219. boost::asio::detail::throw_error(ec, "read_until");
  220. return bytes_transferred;
  221. }
  222. template <typename SyncReadStream, typename DynamicBuffer_v1>
  223. std::size_t read_until(SyncReadStream& s,
  224. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  225. const boost::regex& expr, boost::system::error_code& ec,
  226. typename enable_if<
  227. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  228. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  229. >::type*)
  230. {
  231. typename decay<DynamicBuffer_v1>::type b(
  232. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers));
  233. std::size_t search_position = 0;
  234. for (;;)
  235. {
  236. // Determine the range of the data to be searched.
  237. typedef typename DynamicBuffer_v1::const_buffers_type buffers_type;
  238. typedef buffers_iterator<buffers_type> iterator;
  239. buffers_type data_buffers = b.data();
  240. iterator begin = iterator::begin(data_buffers);
  241. iterator start_pos = begin + search_position;
  242. iterator end = iterator::end(data_buffers);
  243. // Look for a match.
  244. boost::match_results<iterator,
  245. typename std::vector<boost::sub_match<iterator> >::allocator_type>
  246. match_results;
  247. if (regex_search(start_pos, end, match_results, expr,
  248. boost::match_default | boost::match_partial))
  249. {
  250. if (match_results[0].matched)
  251. {
  252. // Full match. We're done.
  253. ec = boost::system::error_code();
  254. return match_results[0].second - begin;
  255. }
  256. else
  257. {
  258. // Partial match. Next search needs to start from beginning of match.
  259. search_position = match_results[0].first - begin;
  260. }
  261. }
  262. else
  263. {
  264. // No match. Next search can start with the new data.
  265. search_position = end - begin;
  266. }
  267. // Check if buffer is full.
  268. if (b.size() == b.max_size())
  269. {
  270. ec = error::not_found;
  271. return 0;
  272. }
  273. // Need more data.
  274. std::size_t bytes_to_read = std::min<std::size_t>(
  275. std::max<std::size_t>(512, b.capacity() - b.size()),
  276. std::min<std::size_t>(65536, b.max_size() - b.size()));
  277. b.commit(s.read_some(b.prepare(bytes_to_read), ec));
  278. if (ec)
  279. return 0;
  280. }
  281. }
  282. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  283. template <typename SyncReadStream,
  284. typename DynamicBuffer_v1, typename MatchCondition>
  285. inline std::size_t read_until(SyncReadStream& s,
  286. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  287. MatchCondition match_condition,
  288. typename enable_if<
  289. is_match_condition<MatchCondition>::value
  290. && is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  291. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  292. >::type*)
  293. {
  294. boost::system::error_code ec;
  295. std::size_t bytes_transferred = read_until(s,
  296. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
  297. match_condition, ec);
  298. boost::asio::detail::throw_error(ec, "read_until");
  299. return bytes_transferred;
  300. }
  301. template <typename SyncReadStream,
  302. typename DynamicBuffer_v1, typename MatchCondition>
  303. std::size_t read_until(SyncReadStream& s,
  304. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  305. MatchCondition match_condition, boost::system::error_code& ec,
  306. typename enable_if<
  307. is_match_condition<MatchCondition>::value
  308. && is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  309. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  310. >::type*)
  311. {
  312. typename decay<DynamicBuffer_v1>::type b(
  313. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers));
  314. std::size_t search_position = 0;
  315. for (;;)
  316. {
  317. // Determine the range of the data to be searched.
  318. typedef typename DynamicBuffer_v1::const_buffers_type buffers_type;
  319. typedef buffers_iterator<buffers_type> iterator;
  320. buffers_type data_buffers = b.data();
  321. iterator begin = iterator::begin(data_buffers);
  322. iterator start_pos = begin + search_position;
  323. iterator end = iterator::end(data_buffers);
  324. // Look for a match.
  325. std::pair<iterator, bool> result = match_condition(start_pos, end);
  326. if (result.second)
  327. {
  328. // Full match. We're done.
  329. ec = boost::system::error_code();
  330. return result.first - begin;
  331. }
  332. else if (result.first != end)
  333. {
  334. // Partial match. Next search needs to start from beginning of match.
  335. search_position = result.first - begin;
  336. }
  337. else
  338. {
  339. // No match. Next search can start with the new data.
  340. search_position = end - begin;
  341. }
  342. // Check if buffer is full.
  343. if (b.size() == b.max_size())
  344. {
  345. ec = error::not_found;
  346. return 0;
  347. }
  348. // Need more data.
  349. std::size_t bytes_to_read = std::min<std::size_t>(
  350. std::max<std::size_t>(512, b.capacity() - b.size()),
  351. std::min<std::size_t>(65536, b.max_size() - b.size()));
  352. b.commit(s.read_some(b.prepare(bytes_to_read), ec));
  353. if (ec)
  354. return 0;
  355. }
  356. }
  357. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  358. template <typename SyncReadStream, typename Allocator>
  359. inline std::size_t read_until(SyncReadStream& s,
  360. boost::asio::basic_streambuf<Allocator>& b, char delim)
  361. {
  362. return read_until(s, basic_streambuf_ref<Allocator>(b), delim);
  363. }
  364. template <typename SyncReadStream, typename Allocator>
  365. inline std::size_t read_until(SyncReadStream& s,
  366. boost::asio::basic_streambuf<Allocator>& b, char delim,
  367. boost::system::error_code& ec)
  368. {
  369. return read_until(s, basic_streambuf_ref<Allocator>(b), delim, ec);
  370. }
  371. template <typename SyncReadStream, typename Allocator>
  372. inline std::size_t read_until(SyncReadStream& s,
  373. boost::asio::basic_streambuf<Allocator>& b,
  374. BOOST_ASIO_STRING_VIEW_PARAM delim)
  375. {
  376. return read_until(s, basic_streambuf_ref<Allocator>(b), delim);
  377. }
  378. template <typename SyncReadStream, typename Allocator>
  379. inline std::size_t read_until(SyncReadStream& s,
  380. boost::asio::basic_streambuf<Allocator>& b,
  381. BOOST_ASIO_STRING_VIEW_PARAM delim, boost::system::error_code& ec)
  382. {
  383. return read_until(s, basic_streambuf_ref<Allocator>(b), delim, ec);
  384. }
  385. #if defined(BOOST_ASIO_HAS_BOOST_REGEX)
  386. template <typename SyncReadStream, typename Allocator>
  387. inline std::size_t read_until(SyncReadStream& s,
  388. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr)
  389. {
  390. return read_until(s, basic_streambuf_ref<Allocator>(b), expr);
  391. }
  392. template <typename SyncReadStream, typename Allocator>
  393. inline std::size_t read_until(SyncReadStream& s,
  394. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  395. boost::system::error_code& ec)
  396. {
  397. return read_until(s, basic_streambuf_ref<Allocator>(b), expr, ec);
  398. }
  399. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  400. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  401. inline std::size_t read_until(SyncReadStream& s,
  402. boost::asio::basic_streambuf<Allocator>& b, MatchCondition match_condition,
  403. typename enable_if<is_match_condition<MatchCondition>::value>::type*)
  404. {
  405. return read_until(s, basic_streambuf_ref<Allocator>(b), match_condition);
  406. }
  407. template <typename SyncReadStream, typename Allocator, typename MatchCondition>
  408. inline std::size_t read_until(SyncReadStream& s,
  409. boost::asio::basic_streambuf<Allocator>& b,
  410. MatchCondition match_condition, boost::system::error_code& ec,
  411. typename enable_if<is_match_condition<MatchCondition>::value>::type*)
  412. {
  413. return read_until(s, basic_streambuf_ref<Allocator>(b), match_condition, ec);
  414. }
  415. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  416. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  417. #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  418. template <typename SyncReadStream, typename DynamicBuffer_v2>
  419. inline std::size_t read_until(SyncReadStream& s,
  420. DynamicBuffer_v2 buffers, char delim,
  421. typename enable_if<
  422. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  423. >::type*)
  424. {
  425. boost::system::error_code ec;
  426. std::size_t bytes_transferred = read_until(s,
  427. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers), delim, ec);
  428. boost::asio::detail::throw_error(ec, "read_until");
  429. return bytes_transferred;
  430. }
  431. template <typename SyncReadStream, typename DynamicBuffer_v2>
  432. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  433. char delim, boost::system::error_code& ec,
  434. typename enable_if<
  435. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  436. >::type*)
  437. {
  438. DynamicBuffer_v2& b = buffers;
  439. std::size_t search_position = 0;
  440. for (;;)
  441. {
  442. // Determine the range of the data to be searched.
  443. typedef typename DynamicBuffer_v2::const_buffers_type buffers_type;
  444. typedef buffers_iterator<buffers_type> iterator;
  445. buffers_type data_buffers =
  446. const_cast<const DynamicBuffer_v2&>(b).data(0, b.size());
  447. iterator begin = iterator::begin(data_buffers);
  448. iterator start_pos = begin + search_position;
  449. iterator end = iterator::end(data_buffers);
  450. // Look for a match.
  451. iterator iter = std::find(start_pos, end, delim);
  452. if (iter != end)
  453. {
  454. // Found a match. We're done.
  455. ec = boost::system::error_code();
  456. return iter - begin + 1;
  457. }
  458. else
  459. {
  460. // No match. Next search can start with the new data.
  461. search_position = end - begin;
  462. }
  463. // Check if buffer is full.
  464. if (b.size() == b.max_size())
  465. {
  466. ec = error::not_found;
  467. return 0;
  468. }
  469. // Need more data.
  470. std::size_t bytes_to_read = std::min<std::size_t>(
  471. std::max<std::size_t>(512, b.capacity() - b.size()),
  472. std::min<std::size_t>(65536, b.max_size() - b.size()));
  473. std::size_t pos = b.size();
  474. b.grow(bytes_to_read);
  475. std::size_t bytes_transferred = s.read_some(b.data(pos, bytes_to_read), ec);
  476. b.shrink(bytes_to_read - bytes_transferred);
  477. if (ec)
  478. return 0;
  479. }
  480. }
  481. template <typename SyncReadStream, typename DynamicBuffer_v2>
  482. inline std::size_t read_until(SyncReadStream& s,
  483. DynamicBuffer_v2 buffers, BOOST_ASIO_STRING_VIEW_PARAM delim,
  484. typename enable_if<
  485. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  486. >::type*)
  487. {
  488. boost::system::error_code ec;
  489. std::size_t bytes_transferred = read_until(s,
  490. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers), delim, ec);
  491. boost::asio::detail::throw_error(ec, "read_until");
  492. return bytes_transferred;
  493. }
  494. template <typename SyncReadStream, typename DynamicBuffer_v2>
  495. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  496. BOOST_ASIO_STRING_VIEW_PARAM delim, boost::system::error_code& ec,
  497. typename enable_if<
  498. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  499. >::type*)
  500. {
  501. DynamicBuffer_v2& b = buffers;
  502. std::size_t search_position = 0;
  503. for (;;)
  504. {
  505. // Determine the range of the data to be searched.
  506. typedef typename DynamicBuffer_v2::const_buffers_type buffers_type;
  507. typedef buffers_iterator<buffers_type> iterator;
  508. buffers_type data_buffers =
  509. const_cast<const DynamicBuffer_v2&>(b).data(0, b.size());
  510. iterator begin = iterator::begin(data_buffers);
  511. iterator start_pos = begin + search_position;
  512. iterator end = iterator::end(data_buffers);
  513. // Look for a match.
  514. std::pair<iterator, bool> result = detail::partial_search(
  515. start_pos, end, delim.begin(), delim.end());
  516. if (result.first != end)
  517. {
  518. if (result.second)
  519. {
  520. // Full match. We're done.
  521. ec = boost::system::error_code();
  522. return result.first - begin + delim.length();
  523. }
  524. else
  525. {
  526. // Partial match. Next search needs to start from beginning of match.
  527. search_position = result.first - begin;
  528. }
  529. }
  530. else
  531. {
  532. // No match. Next search can start with the new data.
  533. search_position = end - begin;
  534. }
  535. // Check if buffer is full.
  536. if (b.size() == b.max_size())
  537. {
  538. ec = error::not_found;
  539. return 0;
  540. }
  541. // Need more data.
  542. std::size_t bytes_to_read = std::min<std::size_t>(
  543. std::max<std::size_t>(512, b.capacity() - b.size()),
  544. std::min<std::size_t>(65536, b.max_size() - b.size()));
  545. std::size_t pos = b.size();
  546. b.grow(bytes_to_read);
  547. std::size_t bytes_transferred = s.read_some(b.data(pos, bytes_to_read), ec);
  548. b.shrink(bytes_to_read - bytes_transferred);
  549. if (ec)
  550. return 0;
  551. }
  552. }
  553. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  554. #if defined(BOOST_ASIO_HAS_BOOST_REGEX)
  555. template <typename SyncReadStream, typename DynamicBuffer_v2>
  556. inline std::size_t read_until(SyncReadStream& s,
  557. DynamicBuffer_v2 buffers, const boost::regex& expr,
  558. typename enable_if<
  559. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  560. >::type*)
  561. {
  562. boost::system::error_code ec;
  563. std::size_t bytes_transferred = read_until(s,
  564. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers), expr, ec);
  565. boost::asio::detail::throw_error(ec, "read_until");
  566. return bytes_transferred;
  567. }
  568. template <typename SyncReadStream, typename DynamicBuffer_v2>
  569. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  570. const boost::regex& expr, boost::system::error_code& ec,
  571. typename enable_if<
  572. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  573. >::type*)
  574. {
  575. DynamicBuffer_v2& b = buffers;
  576. std::size_t search_position = 0;
  577. for (;;)
  578. {
  579. // Determine the range of the data to be searched.
  580. typedef typename DynamicBuffer_v2::const_buffers_type buffers_type;
  581. typedef buffers_iterator<buffers_type> iterator;
  582. buffers_type data_buffers =
  583. const_cast<const DynamicBuffer_v2&>(b).data(0, b.size());
  584. iterator begin = iterator::begin(data_buffers);
  585. iterator start_pos = begin + search_position;
  586. iterator end = iterator::end(data_buffers);
  587. // Look for a match.
  588. boost::match_results<iterator,
  589. typename std::vector<boost::sub_match<iterator> >::allocator_type>
  590. match_results;
  591. if (regex_search(start_pos, end, match_results, expr,
  592. boost::match_default | boost::match_partial))
  593. {
  594. if (match_results[0].matched)
  595. {
  596. // Full match. We're done.
  597. ec = boost::system::error_code();
  598. return match_results[0].second - begin;
  599. }
  600. else
  601. {
  602. // Partial match. Next search needs to start from beginning of match.
  603. search_position = match_results[0].first - begin;
  604. }
  605. }
  606. else
  607. {
  608. // No match. Next search can start with the new data.
  609. search_position = end - begin;
  610. }
  611. // Check if buffer is full.
  612. if (b.size() == b.max_size())
  613. {
  614. ec = error::not_found;
  615. return 0;
  616. }
  617. // Need more data.
  618. std::size_t bytes_to_read = std::min<std::size_t>(
  619. std::max<std::size_t>(512, b.capacity() - b.size()),
  620. std::min<std::size_t>(65536, b.max_size() - b.size()));
  621. std::size_t pos = b.size();
  622. b.grow(bytes_to_read);
  623. std::size_t bytes_transferred = s.read_some(b.data(pos, bytes_to_read), ec);
  624. b.shrink(bytes_to_read - bytes_transferred);
  625. if (ec)
  626. return 0;
  627. }
  628. }
  629. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  630. template <typename SyncReadStream,
  631. typename DynamicBuffer_v2, typename MatchCondition>
  632. inline std::size_t read_until(SyncReadStream& s,
  633. DynamicBuffer_v2 buffers, MatchCondition match_condition,
  634. typename enable_if<
  635. is_match_condition<MatchCondition>::value
  636. && is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  637. >::type*)
  638. {
  639. boost::system::error_code ec;
  640. std::size_t bytes_transferred = read_until(s,
  641. BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
  642. match_condition, ec);
  643. boost::asio::detail::throw_error(ec, "read_until");
  644. return bytes_transferred;
  645. }
  646. template <typename SyncReadStream,
  647. typename DynamicBuffer_v2, typename MatchCondition>
  648. std::size_t read_until(SyncReadStream& s, DynamicBuffer_v2 buffers,
  649. MatchCondition match_condition, boost::system::error_code& ec,
  650. typename enable_if<
  651. is_match_condition<MatchCondition>::value
  652. && is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  653. >::type*)
  654. {
  655. DynamicBuffer_v2& b = buffers;
  656. std::size_t search_position = 0;
  657. for (;;)
  658. {
  659. // Determine the range of the data to be searched.
  660. typedef typename DynamicBuffer_v2::const_buffers_type buffers_type;
  661. typedef buffers_iterator<buffers_type> iterator;
  662. buffers_type data_buffers =
  663. const_cast<const DynamicBuffer_v2&>(b).data(0, b.size());
  664. iterator begin = iterator::begin(data_buffers);
  665. iterator start_pos = begin + search_position;
  666. iterator end = iterator::end(data_buffers);
  667. // Look for a match.
  668. std::pair<iterator, bool> result = match_condition(start_pos, end);
  669. if (result.second)
  670. {
  671. // Full match. We're done.
  672. ec = boost::system::error_code();
  673. return result.first - begin;
  674. }
  675. else if (result.first != end)
  676. {
  677. // Partial match. Next search needs to start from beginning of match.
  678. search_position = result.first - begin;
  679. }
  680. else
  681. {
  682. // No match. Next search can start with the new data.
  683. search_position = end - begin;
  684. }
  685. // Check if buffer is full.
  686. if (b.size() == b.max_size())
  687. {
  688. ec = error::not_found;
  689. return 0;
  690. }
  691. // Need more data.
  692. std::size_t bytes_to_read = std::min<std::size_t>(
  693. std::max<std::size_t>(512, b.capacity() - b.size()),
  694. std::min<std::size_t>(65536, b.max_size() - b.size()));
  695. std::size_t pos = b.size();
  696. b.grow(bytes_to_read);
  697. std::size_t bytes_transferred = s.read_some(b.data(pos, bytes_to_read), ec);
  698. b.shrink(bytes_to_read - bytes_transferred);
  699. if (ec)
  700. return 0;
  701. }
  702. }
  703. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  704. #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  705. namespace detail
  706. {
  707. template <typename AsyncReadStream,
  708. typename DynamicBuffer_v1, typename ReadHandler>
  709. class read_until_delim_op_v1
  710. {
  711. public:
  712. template <typename BufferSequence>
  713. read_until_delim_op_v1(AsyncReadStream& stream,
  714. BOOST_ASIO_MOVE_ARG(BufferSequence) buffers,
  715. char delim, ReadHandler& handler)
  716. : stream_(stream),
  717. buffers_(BOOST_ASIO_MOVE_CAST(BufferSequence)(buffers)),
  718. delim_(delim),
  719. start_(0),
  720. search_position_(0),
  721. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  722. {
  723. }
  724. #if defined(BOOST_ASIO_HAS_MOVE)
  725. read_until_delim_op_v1(const read_until_delim_op_v1& other)
  726. : stream_(other.stream_),
  727. buffers_(other.buffers_),
  728. delim_(other.delim_),
  729. start_(other.start_),
  730. search_position_(other.search_position_),
  731. handler_(other.handler_)
  732. {
  733. }
  734. read_until_delim_op_v1(read_until_delim_op_v1&& other)
  735. : stream_(other.stream_),
  736. buffers_(BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(other.buffers_)),
  737. delim_(other.delim_),
  738. start_(other.start_),
  739. search_position_(other.search_position_),
  740. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  741. {
  742. }
  743. #endif // defined(BOOST_ASIO_HAS_MOVE)
  744. void operator()(const boost::system::error_code& ec,
  745. std::size_t bytes_transferred, int start = 0)
  746. {
  747. const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();
  748. std::size_t bytes_to_read;
  749. switch (start_ = start)
  750. {
  751. case 1:
  752. for (;;)
  753. {
  754. {
  755. // Determine the range of the data to be searched.
  756. typedef typename DynamicBuffer_v1::const_buffers_type
  757. buffers_type;
  758. typedef buffers_iterator<buffers_type> iterator;
  759. buffers_type data_buffers = buffers_.data();
  760. iterator begin = iterator::begin(data_buffers);
  761. iterator start_pos = begin + search_position_;
  762. iterator end = iterator::end(data_buffers);
  763. // Look for a match.
  764. iterator iter = std::find(start_pos, end, delim_);
  765. if (iter != end)
  766. {
  767. // Found a match. We're done.
  768. search_position_ = iter - begin + 1;
  769. bytes_to_read = 0;
  770. }
  771. // No match yet. Check if buffer is full.
  772. else if (buffers_.size() == buffers_.max_size())
  773. {
  774. search_position_ = not_found;
  775. bytes_to_read = 0;
  776. }
  777. // Need to read some more data.
  778. else
  779. {
  780. // Next search can start with the new data.
  781. search_position_ = end - begin;
  782. bytes_to_read = std::min<std::size_t>(
  783. std::max<std::size_t>(512,
  784. buffers_.capacity() - buffers_.size()),
  785. std::min<std::size_t>(65536,
  786. buffers_.max_size() - buffers_.size()));
  787. }
  788. }
  789. // Check if we're done.
  790. if (!start && bytes_to_read == 0)
  791. break;
  792. // Start a new asynchronous read op_v1eration to obtain more data.
  793. stream_.async_read_some(buffers_.prepare(bytes_to_read),
  794. BOOST_ASIO_MOVE_CAST(read_until_delim_op_v1)(*this));
  795. return; default:
  796. buffers_.commit(bytes_transferred);
  797. if (ec || bytes_transferred == 0)
  798. break;
  799. }
  800. const boost::system::error_code result_ec =
  801. (search_position_ == not_found)
  802. ? error::not_found : ec;
  803. const std::size_t result_n =
  804. (ec || search_position_ == not_found)
  805. ? 0 : search_position_;
  806. handler_(result_ec, result_n);
  807. }
  808. }
  809. //private:
  810. AsyncReadStream& stream_;
  811. DynamicBuffer_v1 buffers_;
  812. char delim_;
  813. int start_;
  814. std::size_t search_position_;
  815. ReadHandler handler_;
  816. };
  817. template <typename AsyncReadStream,
  818. typename DynamicBuffer_v1, typename ReadHandler>
  819. inline void* asio_handler_allocate(std::size_t size,
  820. read_until_delim_op_v1<AsyncReadStream,
  821. DynamicBuffer_v1, ReadHandler>* this_handler)
  822. {
  823. return boost_asio_handler_alloc_helpers::allocate(
  824. size, this_handler->handler_);
  825. }
  826. template <typename AsyncReadStream,
  827. typename DynamicBuffer_v1, typename ReadHandler>
  828. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  829. read_until_delim_op_v1<AsyncReadStream,
  830. DynamicBuffer_v1, ReadHandler>* this_handler)
  831. {
  832. boost_asio_handler_alloc_helpers::deallocate(
  833. pointer, size, this_handler->handler_);
  834. }
  835. template <typename AsyncReadStream,
  836. typename DynamicBuffer_v1, typename ReadHandler>
  837. inline bool asio_handler_is_continuation(
  838. read_until_delim_op_v1<AsyncReadStream,
  839. DynamicBuffer_v1, ReadHandler>* this_handler)
  840. {
  841. return this_handler->start_ == 0 ? true
  842. : boost_asio_handler_cont_helpers::is_continuation(
  843. this_handler->handler_);
  844. }
  845. template <typename Function, typename AsyncReadStream,
  846. typename DynamicBuffer_v1, typename ReadHandler>
  847. inline void asio_handler_invoke(Function& function,
  848. read_until_delim_op_v1<AsyncReadStream,
  849. DynamicBuffer_v1, ReadHandler>* this_handler)
  850. {
  851. boost_asio_handler_invoke_helpers::invoke(
  852. function, this_handler->handler_);
  853. }
  854. template <typename Function, typename AsyncReadStream,
  855. typename DynamicBuffer_v1, typename ReadHandler>
  856. inline void asio_handler_invoke(const Function& function,
  857. read_until_delim_op_v1<AsyncReadStream,
  858. DynamicBuffer_v1, ReadHandler>* this_handler)
  859. {
  860. boost_asio_handler_invoke_helpers::invoke(
  861. function, this_handler->handler_);
  862. }
  863. struct initiate_async_read_until_delim_v1
  864. {
  865. template <typename ReadHandler, typename AsyncReadStream,
  866. typename DynamicBuffer_v1>
  867. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  868. AsyncReadStream* s, BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  869. char delim) const
  870. {
  871. // If you get an error on the following line it means that your handler
  872. // does not meet the documented type requirements for a ReadHandler.
  873. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  874. non_const_lvalue<ReadHandler> handler2(handler);
  875. read_until_delim_op_v1<AsyncReadStream,
  876. typename decay<DynamicBuffer_v1>::type,
  877. typename decay<ReadHandler>::type>(
  878. *s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
  879. delim, handler2.value)(boost::system::error_code(), 0, 1);
  880. }
  881. };
  882. } // namespace detail
  883. #if !defined(GENERATING_DOCUMENTATION)
  884. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  885. typename ReadHandler, typename Allocator>
  886. struct associated_allocator<
  887. detail::read_until_delim_op_v1<AsyncReadStream,
  888. DynamicBuffer_v1, ReadHandler>,
  889. Allocator>
  890. {
  891. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  892. static type get(
  893. const detail::read_until_delim_op_v1<AsyncReadStream,
  894. DynamicBuffer_v1, ReadHandler>& h,
  895. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  896. {
  897. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  898. }
  899. };
  900. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  901. typename ReadHandler, typename Executor>
  902. struct associated_executor<
  903. detail::read_until_delim_op_v1<AsyncReadStream,
  904. DynamicBuffer_v1, ReadHandler>,
  905. Executor>
  906. {
  907. typedef typename associated_executor<ReadHandler, Executor>::type type;
  908. static type get(
  909. const detail::read_until_delim_op_v1<AsyncReadStream,
  910. DynamicBuffer_v1, ReadHandler>& h,
  911. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  912. {
  913. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  914. }
  915. };
  916. #endif // !defined(GENERATING_DOCUMENTATION)
  917. template <typename AsyncReadStream,
  918. typename DynamicBuffer_v1, typename ReadHandler>
  919. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  920. void (boost::system::error_code, std::size_t))
  921. async_read_until(AsyncReadStream& s,
  922. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  923. char delim, BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  924. typename enable_if<
  925. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  926. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  927. >::type*)
  928. {
  929. return async_initiate<ReadHandler,
  930. void (boost::system::error_code, std::size_t)>(
  931. detail::initiate_async_read_until_delim_v1(), handler,
  932. &s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers), delim);
  933. }
  934. namespace detail
  935. {
  936. template <typename AsyncReadStream,
  937. typename DynamicBuffer_v1, typename ReadHandler>
  938. class read_until_delim_string_op_v1
  939. {
  940. public:
  941. template <typename BufferSequence>
  942. read_until_delim_string_op_v1(AsyncReadStream& stream,
  943. BOOST_ASIO_MOVE_ARG(BufferSequence) buffers,
  944. const std::string& delim, ReadHandler& handler)
  945. : stream_(stream),
  946. buffers_(BOOST_ASIO_MOVE_CAST(BufferSequence)(buffers)),
  947. delim_(delim),
  948. start_(0),
  949. search_position_(0),
  950. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  951. {
  952. }
  953. #if defined(BOOST_ASIO_HAS_MOVE)
  954. read_until_delim_string_op_v1(const read_until_delim_string_op_v1& other)
  955. : stream_(other.stream_),
  956. buffers_(other.buffers_),
  957. delim_(other.delim_),
  958. start_(other.start_),
  959. search_position_(other.search_position_),
  960. handler_(other.handler_)
  961. {
  962. }
  963. read_until_delim_string_op_v1(read_until_delim_string_op_v1&& other)
  964. : stream_(other.stream_),
  965. buffers_(BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(other.buffers_)),
  966. delim_(BOOST_ASIO_MOVE_CAST(std::string)(other.delim_)),
  967. start_(other.start_),
  968. search_position_(other.search_position_),
  969. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  970. {
  971. }
  972. #endif // defined(BOOST_ASIO_HAS_MOVE)
  973. void operator()(const boost::system::error_code& ec,
  974. std::size_t bytes_transferred, int start = 0)
  975. {
  976. const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();
  977. std::size_t bytes_to_read;
  978. switch (start_ = start)
  979. {
  980. case 1:
  981. for (;;)
  982. {
  983. {
  984. // Determine the range of the data to be searched.
  985. typedef typename DynamicBuffer_v1::const_buffers_type
  986. buffers_type;
  987. typedef buffers_iterator<buffers_type> iterator;
  988. buffers_type data_buffers = buffers_.data();
  989. iterator begin = iterator::begin(data_buffers);
  990. iterator start_pos = begin + search_position_;
  991. iterator end = iterator::end(data_buffers);
  992. // Look for a match.
  993. std::pair<iterator, bool> result = detail::partial_search(
  994. start_pos, end, delim_.begin(), delim_.end());
  995. if (result.first != end && result.second)
  996. {
  997. // Full match. We're done.
  998. search_position_ = result.first - begin + delim_.length();
  999. bytes_to_read = 0;
  1000. }
  1001. // No match yet. Check if buffer is full.
  1002. else if (buffers_.size() == buffers_.max_size())
  1003. {
  1004. search_position_ = not_found;
  1005. bytes_to_read = 0;
  1006. }
  1007. // Need to read some more data.
  1008. else
  1009. {
  1010. if (result.first != end)
  1011. {
  1012. // Partial match. Next search needs to start from beginning of
  1013. // match.
  1014. search_position_ = result.first - begin;
  1015. }
  1016. else
  1017. {
  1018. // Next search can start with the new data.
  1019. search_position_ = end - begin;
  1020. }
  1021. bytes_to_read = std::min<std::size_t>(
  1022. std::max<std::size_t>(512,
  1023. buffers_.capacity() - buffers_.size()),
  1024. std::min<std::size_t>(65536,
  1025. buffers_.max_size() - buffers_.size()));
  1026. }
  1027. }
  1028. // Check if we're done.
  1029. if (!start && bytes_to_read == 0)
  1030. break;
  1031. // Start a new asynchronous read op_v1eration to obtain more data.
  1032. stream_.async_read_some(buffers_.prepare(bytes_to_read),
  1033. BOOST_ASIO_MOVE_CAST(read_until_delim_string_op_v1)(*this));
  1034. return; default:
  1035. buffers_.commit(bytes_transferred);
  1036. if (ec || bytes_transferred == 0)
  1037. break;
  1038. }
  1039. const boost::system::error_code result_ec =
  1040. (search_position_ == not_found)
  1041. ? error::not_found : ec;
  1042. const std::size_t result_n =
  1043. (ec || search_position_ == not_found)
  1044. ? 0 : search_position_;
  1045. handler_(result_ec, result_n);
  1046. }
  1047. }
  1048. //private:
  1049. AsyncReadStream& stream_;
  1050. DynamicBuffer_v1 buffers_;
  1051. std::string delim_;
  1052. int start_;
  1053. std::size_t search_position_;
  1054. ReadHandler handler_;
  1055. };
  1056. template <typename AsyncReadStream,
  1057. typename DynamicBuffer_v1, typename ReadHandler>
  1058. inline void* asio_handler_allocate(std::size_t size,
  1059. read_until_delim_string_op_v1<AsyncReadStream,
  1060. DynamicBuffer_v1, ReadHandler>* this_handler)
  1061. {
  1062. return boost_asio_handler_alloc_helpers::allocate(
  1063. size, this_handler->handler_);
  1064. }
  1065. template <typename AsyncReadStream,
  1066. typename DynamicBuffer_v1, typename ReadHandler>
  1067. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  1068. read_until_delim_string_op_v1<AsyncReadStream,
  1069. DynamicBuffer_v1, ReadHandler>* this_handler)
  1070. {
  1071. boost_asio_handler_alloc_helpers::deallocate(
  1072. pointer, size, this_handler->handler_);
  1073. }
  1074. template <typename AsyncReadStream,
  1075. typename DynamicBuffer_v1, typename ReadHandler>
  1076. inline bool asio_handler_is_continuation(
  1077. read_until_delim_string_op_v1<AsyncReadStream,
  1078. DynamicBuffer_v1, ReadHandler>* this_handler)
  1079. {
  1080. return this_handler->start_ == 0 ? true
  1081. : boost_asio_handler_cont_helpers::is_continuation(
  1082. this_handler->handler_);
  1083. }
  1084. template <typename Function, typename AsyncReadStream,
  1085. typename DynamicBuffer_v1, typename ReadHandler>
  1086. inline void asio_handler_invoke(Function& function,
  1087. read_until_delim_string_op_v1<AsyncReadStream,
  1088. DynamicBuffer_v1, ReadHandler>* this_handler)
  1089. {
  1090. boost_asio_handler_invoke_helpers::invoke(
  1091. function, this_handler->handler_);
  1092. }
  1093. template <typename Function, typename AsyncReadStream,
  1094. typename DynamicBuffer_v1, typename ReadHandler>
  1095. inline void asio_handler_invoke(const Function& function,
  1096. read_until_delim_string_op_v1<AsyncReadStream,
  1097. DynamicBuffer_v1, ReadHandler>* this_handler)
  1098. {
  1099. boost_asio_handler_invoke_helpers::invoke(
  1100. function, this_handler->handler_);
  1101. }
  1102. struct initiate_async_read_until_delim_string_v1
  1103. {
  1104. template <typename ReadHandler, typename AsyncReadStream,
  1105. typename DynamicBuffer_v1>
  1106. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1107. AsyncReadStream* s, BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1108. const std::string& delim) const
  1109. {
  1110. // If you get an error on the following line it means that your handler
  1111. // does not meet the documented type requirements for a ReadHandler.
  1112. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  1113. non_const_lvalue<ReadHandler> handler2(handler);
  1114. read_until_delim_string_op_v1<AsyncReadStream,
  1115. typename decay<DynamicBuffer_v1>::type,
  1116. typename decay<ReadHandler>::type>(
  1117. *s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
  1118. delim, handler2.value)(boost::system::error_code(), 0, 1);
  1119. }
  1120. };
  1121. } // namespace detail
  1122. #if !defined(GENERATING_DOCUMENTATION)
  1123. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1124. typename ReadHandler, typename Allocator>
  1125. struct associated_allocator<
  1126. detail::read_until_delim_string_op_v1<AsyncReadStream,
  1127. DynamicBuffer_v1, ReadHandler>,
  1128. Allocator>
  1129. {
  1130. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  1131. static type get(
  1132. const detail::read_until_delim_string_op_v1<AsyncReadStream,
  1133. DynamicBuffer_v1, ReadHandler>& h,
  1134. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  1135. {
  1136. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  1137. }
  1138. };
  1139. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1140. typename ReadHandler, typename Executor>
  1141. struct associated_executor<
  1142. detail::read_until_delim_string_op_v1<AsyncReadStream,
  1143. DynamicBuffer_v1, ReadHandler>,
  1144. Executor>
  1145. {
  1146. typedef typename associated_executor<ReadHandler, Executor>::type type;
  1147. static type get(
  1148. const detail::read_until_delim_string_op_v1<AsyncReadStream,
  1149. DynamicBuffer_v1, ReadHandler>& h,
  1150. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  1151. {
  1152. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  1153. }
  1154. };
  1155. #endif // !defined(GENERATING_DOCUMENTATION)
  1156. template <typename AsyncReadStream,
  1157. typename DynamicBuffer_v1, typename ReadHandler>
  1158. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1159. void (boost::system::error_code, std::size_t))
  1160. async_read_until(AsyncReadStream& s,
  1161. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1162. BOOST_ASIO_STRING_VIEW_PARAM delim,
  1163. BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1164. typename enable_if<
  1165. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1166. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1167. >::type*)
  1168. {
  1169. return async_initiate<ReadHandler,
  1170. void (boost::system::error_code, std::size_t)>(
  1171. detail::initiate_async_read_until_delim_string_v1(),
  1172. handler, &s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
  1173. static_cast<std::string>(delim));
  1174. }
  1175. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  1176. #if defined(BOOST_ASIO_HAS_BOOST_REGEX)
  1177. namespace detail
  1178. {
  1179. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1180. typename RegEx, typename ReadHandler>
  1181. class read_until_expr_op_v1
  1182. {
  1183. public:
  1184. template <typename BufferSequence>
  1185. read_until_expr_op_v1(AsyncReadStream& stream,
  1186. BOOST_ASIO_MOVE_ARG(BufferSequence) buffers,
  1187. const boost::regex& expr, ReadHandler& handler)
  1188. : stream_(stream),
  1189. buffers_(BOOST_ASIO_MOVE_CAST(BufferSequence)(buffers)),
  1190. expr_(expr),
  1191. start_(0),
  1192. search_position_(0),
  1193. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  1194. {
  1195. }
  1196. #if defined(BOOST_ASIO_HAS_MOVE)
  1197. read_until_expr_op_v1(const read_until_expr_op_v1& other)
  1198. : stream_(other.stream_),
  1199. buffers_(other.buffers_),
  1200. expr_(other.expr_),
  1201. start_(other.start_),
  1202. search_position_(other.search_position_),
  1203. handler_(other.handler_)
  1204. {
  1205. }
  1206. read_until_expr_op_v1(read_until_expr_op_v1&& other)
  1207. : stream_(other.stream_),
  1208. buffers_(BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(other.buffers_)),
  1209. expr_(other.expr_),
  1210. start_(other.start_),
  1211. search_position_(other.search_position_),
  1212. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  1213. {
  1214. }
  1215. #endif // defined(BOOST_ASIO_HAS_MOVE)
  1216. void operator()(const boost::system::error_code& ec,
  1217. std::size_t bytes_transferred, int start = 0)
  1218. {
  1219. const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();
  1220. std::size_t bytes_to_read;
  1221. switch (start_ = start)
  1222. {
  1223. case 1:
  1224. for (;;)
  1225. {
  1226. {
  1227. // Determine the range of the data to be searched.
  1228. typedef typename DynamicBuffer_v1::const_buffers_type
  1229. buffers_type;
  1230. typedef buffers_iterator<buffers_type> iterator;
  1231. buffers_type data_buffers = buffers_.data();
  1232. iterator begin = iterator::begin(data_buffers);
  1233. iterator start_pos = begin + search_position_;
  1234. iterator end = iterator::end(data_buffers);
  1235. // Look for a match.
  1236. boost::match_results<iterator,
  1237. typename std::vector<boost::sub_match<iterator> >::allocator_type>
  1238. match_results;
  1239. bool match = regex_search(start_pos, end, match_results, expr_,
  1240. boost::match_default | boost::match_partial);
  1241. if (match && match_results[0].matched)
  1242. {
  1243. // Full match. We're done.
  1244. search_position_ = match_results[0].second - begin;
  1245. bytes_to_read = 0;
  1246. }
  1247. // No match yet. Check if buffer is full.
  1248. else if (buffers_.size() == buffers_.max_size())
  1249. {
  1250. search_position_ = not_found;
  1251. bytes_to_read = 0;
  1252. }
  1253. // Need to read some more data.
  1254. else
  1255. {
  1256. if (match)
  1257. {
  1258. // Partial match. Next search needs to start from beginning of
  1259. // match.
  1260. search_position_ = match_results[0].first - begin;
  1261. }
  1262. else
  1263. {
  1264. // Next search can start with the new data.
  1265. search_position_ = end - begin;
  1266. }
  1267. bytes_to_read = std::min<std::size_t>(
  1268. std::max<std::size_t>(512,
  1269. buffers_.capacity() - buffers_.size()),
  1270. std::min<std::size_t>(65536,
  1271. buffers_.max_size() - buffers_.size()));
  1272. }
  1273. }
  1274. // Check if we're done.
  1275. if (!start && bytes_to_read == 0)
  1276. break;
  1277. // Start a new asynchronous read op_v1eration to obtain more data.
  1278. stream_.async_read_some(buffers_.prepare(bytes_to_read),
  1279. BOOST_ASIO_MOVE_CAST(read_until_expr_op_v1)(*this));
  1280. return; default:
  1281. buffers_.commit(bytes_transferred);
  1282. if (ec || bytes_transferred == 0)
  1283. break;
  1284. }
  1285. const boost::system::error_code result_ec =
  1286. (search_position_ == not_found)
  1287. ? error::not_found : ec;
  1288. const std::size_t result_n =
  1289. (ec || search_position_ == not_found)
  1290. ? 0 : search_position_;
  1291. handler_(result_ec, result_n);
  1292. }
  1293. }
  1294. //private:
  1295. AsyncReadStream& stream_;
  1296. DynamicBuffer_v1 buffers_;
  1297. RegEx expr_;
  1298. int start_;
  1299. std::size_t search_position_;
  1300. ReadHandler handler_;
  1301. };
  1302. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1303. typename RegEx, typename ReadHandler>
  1304. inline void* asio_handler_allocate(std::size_t size,
  1305. read_until_expr_op_v1<AsyncReadStream,
  1306. DynamicBuffer_v1, RegEx, ReadHandler>* this_handler)
  1307. {
  1308. return boost_asio_handler_alloc_helpers::allocate(
  1309. size, this_handler->handler_);
  1310. }
  1311. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1312. typename RegEx, typename ReadHandler>
  1313. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  1314. read_until_expr_op_v1<AsyncReadStream,
  1315. DynamicBuffer_v1, RegEx, ReadHandler>* this_handler)
  1316. {
  1317. boost_asio_handler_alloc_helpers::deallocate(
  1318. pointer, size, this_handler->handler_);
  1319. }
  1320. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1321. typename RegEx, typename ReadHandler>
  1322. inline bool asio_handler_is_continuation(
  1323. read_until_expr_op_v1<AsyncReadStream,
  1324. DynamicBuffer_v1, RegEx, ReadHandler>* this_handler)
  1325. {
  1326. return this_handler->start_ == 0 ? true
  1327. : boost_asio_handler_cont_helpers::is_continuation(
  1328. this_handler->handler_);
  1329. }
  1330. template <typename Function, typename AsyncReadStream,
  1331. typename DynamicBuffer_v1, typename RegEx, typename ReadHandler>
  1332. inline void asio_handler_invoke(Function& function,
  1333. read_until_expr_op_v1<AsyncReadStream,
  1334. DynamicBuffer_v1, RegEx, ReadHandler>* this_handler)
  1335. {
  1336. boost_asio_handler_invoke_helpers::invoke(
  1337. function, this_handler->handler_);
  1338. }
  1339. template <typename Function, typename AsyncReadStream,
  1340. typename DynamicBuffer_v1, typename RegEx, typename ReadHandler>
  1341. inline void asio_handler_invoke(const Function& function,
  1342. read_until_expr_op_v1<AsyncReadStream,
  1343. DynamicBuffer_v1, RegEx, ReadHandler>* this_handler)
  1344. {
  1345. boost_asio_handler_invoke_helpers::invoke(
  1346. function, this_handler->handler_);
  1347. }
  1348. struct initiate_async_read_until_expr_v1
  1349. {
  1350. template <typename ReadHandler, typename AsyncReadStream,
  1351. typename DynamicBuffer_v1, typename RegEx>
  1352. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1353. AsyncReadStream* s, BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1354. const RegEx& expr) const
  1355. {
  1356. // If you get an error on the following line it means that your handler
  1357. // does not meet the documented type requirements for a ReadHandler.
  1358. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  1359. non_const_lvalue<ReadHandler> handler2(handler);
  1360. read_until_expr_op_v1<AsyncReadStream,
  1361. typename decay<DynamicBuffer_v1>::type,
  1362. RegEx, typename decay<ReadHandler>::type>(
  1363. *s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
  1364. expr, handler2.value)(boost::system::error_code(), 0, 1);
  1365. }
  1366. };
  1367. } // namespace detail
  1368. #if !defined(GENERATING_DOCUMENTATION)
  1369. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1370. typename RegEx, typename ReadHandler, typename Allocator>
  1371. struct associated_allocator<
  1372. detail::read_until_expr_op_v1<AsyncReadStream,
  1373. DynamicBuffer_v1, RegEx, ReadHandler>,
  1374. Allocator>
  1375. {
  1376. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  1377. static type get(
  1378. const detail::read_until_expr_op_v1<AsyncReadStream,
  1379. DynamicBuffer_v1, RegEx, ReadHandler>& h,
  1380. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  1381. {
  1382. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  1383. }
  1384. };
  1385. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1386. typename RegEx, typename ReadHandler, typename Executor>
  1387. struct associated_executor<
  1388. detail::read_until_expr_op_v1<AsyncReadStream,
  1389. DynamicBuffer_v1, RegEx, ReadHandler>,
  1390. Executor>
  1391. {
  1392. typedef typename associated_executor<ReadHandler, Executor>::type type;
  1393. static type get(
  1394. const detail::read_until_expr_op_v1<AsyncReadStream,
  1395. DynamicBuffer_v1, RegEx, ReadHandler>& h,
  1396. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  1397. {
  1398. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  1399. }
  1400. };
  1401. #endif // !defined(GENERATING_DOCUMENTATION)
  1402. template <typename AsyncReadStream,
  1403. typename DynamicBuffer_v1, typename ReadHandler>
  1404. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1405. void (boost::system::error_code, std::size_t))
  1406. async_read_until(AsyncReadStream& s,
  1407. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1408. const boost::regex& expr,
  1409. BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1410. typename enable_if<
  1411. is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1412. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1413. >::type*)
  1414. {
  1415. return async_initiate<ReadHandler,
  1416. void (boost::system::error_code, std::size_t)>(
  1417. detail::initiate_async_read_until_expr_v1(), handler,
  1418. &s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers), expr);
  1419. }
  1420. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  1421. namespace detail
  1422. {
  1423. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1424. typename MatchCondition, typename ReadHandler>
  1425. class read_until_match_op_v1
  1426. {
  1427. public:
  1428. template <typename BufferSequence>
  1429. read_until_match_op_v1(AsyncReadStream& stream,
  1430. BOOST_ASIO_MOVE_ARG(BufferSequence) buffers,
  1431. MatchCondition match_condition, ReadHandler& handler)
  1432. : stream_(stream),
  1433. buffers_(BOOST_ASIO_MOVE_CAST(BufferSequence)(buffers)),
  1434. match_condition_(match_condition),
  1435. start_(0),
  1436. search_position_(0),
  1437. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  1438. {
  1439. }
  1440. #if defined(BOOST_ASIO_HAS_MOVE)
  1441. read_until_match_op_v1(const read_until_match_op_v1& other)
  1442. : stream_(other.stream_),
  1443. buffers_(other.buffers_),
  1444. match_condition_(other.match_condition_),
  1445. start_(other.start_),
  1446. search_position_(other.search_position_),
  1447. handler_(other.handler_)
  1448. {
  1449. }
  1450. read_until_match_op_v1(read_until_match_op_v1&& other)
  1451. : stream_(other.stream_),
  1452. buffers_(BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(other.buffers_)),
  1453. match_condition_(other.match_condition_),
  1454. start_(other.start_),
  1455. search_position_(other.search_position_),
  1456. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  1457. {
  1458. }
  1459. #endif // defined(BOOST_ASIO_HAS_MOVE)
  1460. void operator()(const boost::system::error_code& ec,
  1461. std::size_t bytes_transferred, int start = 0)
  1462. {
  1463. const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();
  1464. std::size_t bytes_to_read;
  1465. switch (start_ = start)
  1466. {
  1467. case 1:
  1468. for (;;)
  1469. {
  1470. {
  1471. // Determine the range of the data to be searched.
  1472. typedef typename DynamicBuffer_v1::const_buffers_type
  1473. buffers_type;
  1474. typedef buffers_iterator<buffers_type> iterator;
  1475. buffers_type data_buffers = buffers_.data();
  1476. iterator begin = iterator::begin(data_buffers);
  1477. iterator start_pos = begin + search_position_;
  1478. iterator end = iterator::end(data_buffers);
  1479. // Look for a match.
  1480. std::pair<iterator, bool> result = match_condition_(start_pos, end);
  1481. if (result.second)
  1482. {
  1483. // Full match. We're done.
  1484. search_position_ = result.first - begin;
  1485. bytes_to_read = 0;
  1486. }
  1487. // No match yet. Check if buffer is full.
  1488. else if (buffers_.size() == buffers_.max_size())
  1489. {
  1490. search_position_ = not_found;
  1491. bytes_to_read = 0;
  1492. }
  1493. // Need to read some more data.
  1494. else
  1495. {
  1496. if (result.first != end)
  1497. {
  1498. // Partial match. Next search needs to start from beginning of
  1499. // match.
  1500. search_position_ = result.first - begin;
  1501. }
  1502. else
  1503. {
  1504. // Next search can start with the new data.
  1505. search_position_ = end - begin;
  1506. }
  1507. bytes_to_read = std::min<std::size_t>(
  1508. std::max<std::size_t>(512,
  1509. buffers_.capacity() - buffers_.size()),
  1510. std::min<std::size_t>(65536,
  1511. buffers_.max_size() - buffers_.size()));
  1512. }
  1513. }
  1514. // Check if we're done.
  1515. if (!start && bytes_to_read == 0)
  1516. break;
  1517. // Start a new asynchronous read op_v1eration to obtain more data.
  1518. stream_.async_read_some(buffers_.prepare(bytes_to_read),
  1519. BOOST_ASIO_MOVE_CAST(read_until_match_op_v1)(*this));
  1520. return; default:
  1521. buffers_.commit(bytes_transferred);
  1522. if (ec || bytes_transferred == 0)
  1523. break;
  1524. }
  1525. const boost::system::error_code result_ec =
  1526. (search_position_ == not_found)
  1527. ? error::not_found : ec;
  1528. const std::size_t result_n =
  1529. (ec || search_position_ == not_found)
  1530. ? 0 : search_position_;
  1531. handler_(result_ec, result_n);
  1532. }
  1533. }
  1534. //private:
  1535. AsyncReadStream& stream_;
  1536. DynamicBuffer_v1 buffers_;
  1537. MatchCondition match_condition_;
  1538. int start_;
  1539. std::size_t search_position_;
  1540. ReadHandler handler_;
  1541. };
  1542. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1543. typename MatchCondition, typename ReadHandler>
  1544. inline void* asio_handler_allocate(std::size_t size,
  1545. read_until_match_op_v1<AsyncReadStream, DynamicBuffer_v1,
  1546. MatchCondition, ReadHandler>* this_handler)
  1547. {
  1548. return boost_asio_handler_alloc_helpers::allocate(
  1549. size, this_handler->handler_);
  1550. }
  1551. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1552. typename MatchCondition, typename ReadHandler>
  1553. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  1554. read_until_match_op_v1<AsyncReadStream, DynamicBuffer_v1,
  1555. MatchCondition, ReadHandler>* this_handler)
  1556. {
  1557. boost_asio_handler_alloc_helpers::deallocate(
  1558. pointer, size, this_handler->handler_);
  1559. }
  1560. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1561. typename MatchCondition, typename ReadHandler>
  1562. inline bool asio_handler_is_continuation(
  1563. read_until_match_op_v1<AsyncReadStream, DynamicBuffer_v1,
  1564. MatchCondition, ReadHandler>* this_handler)
  1565. {
  1566. return this_handler->start_ == 0 ? true
  1567. : boost_asio_handler_cont_helpers::is_continuation(
  1568. this_handler->handler_);
  1569. }
  1570. template <typename Function, typename AsyncReadStream,
  1571. typename DynamicBuffer_v1, typename MatchCondition,
  1572. typename ReadHandler>
  1573. inline void asio_handler_invoke(Function& function,
  1574. read_until_match_op_v1<AsyncReadStream, DynamicBuffer_v1,
  1575. MatchCondition, ReadHandler>* this_handler)
  1576. {
  1577. boost_asio_handler_invoke_helpers::invoke(
  1578. function, this_handler->handler_);
  1579. }
  1580. template <typename Function, typename AsyncReadStream,
  1581. typename DynamicBuffer_v1, typename MatchCondition,
  1582. typename ReadHandler>
  1583. inline void asio_handler_invoke(const Function& function,
  1584. read_until_match_op_v1<AsyncReadStream, DynamicBuffer_v1,
  1585. MatchCondition, ReadHandler>* this_handler)
  1586. {
  1587. boost_asio_handler_invoke_helpers::invoke(
  1588. function, this_handler->handler_);
  1589. }
  1590. struct initiate_async_read_until_match_v1
  1591. {
  1592. template <typename ReadHandler, typename AsyncReadStream,
  1593. typename DynamicBuffer_v1, typename MatchCondition>
  1594. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1595. AsyncReadStream* s, BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1596. MatchCondition match_condition) const
  1597. {
  1598. // If you get an error on the following line it means that your handler
  1599. // does not meet the documented type requirements for a ReadHandler.
  1600. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  1601. non_const_lvalue<ReadHandler> handler2(handler);
  1602. read_until_match_op_v1<AsyncReadStream,
  1603. typename decay<DynamicBuffer_v1>::type,
  1604. MatchCondition, typename decay<ReadHandler>::type>(
  1605. *s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
  1606. match_condition, handler2.value)(boost::system::error_code(), 0, 1);
  1607. }
  1608. };
  1609. } // namespace detail
  1610. #if !defined(GENERATING_DOCUMENTATION)
  1611. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1612. typename MatchCondition, typename ReadHandler, typename Allocator>
  1613. struct associated_allocator<
  1614. detail::read_until_match_op_v1<AsyncReadStream,
  1615. DynamicBuffer_v1, MatchCondition, ReadHandler>,
  1616. Allocator>
  1617. {
  1618. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  1619. static type get(
  1620. const detail::read_until_match_op_v1<AsyncReadStream,
  1621. DynamicBuffer_v1, MatchCondition, ReadHandler>& h,
  1622. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  1623. {
  1624. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  1625. }
  1626. };
  1627. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1628. typename MatchCondition, typename ReadHandler, typename Executor>
  1629. struct associated_executor<
  1630. detail::read_until_match_op_v1<AsyncReadStream,
  1631. DynamicBuffer_v1, MatchCondition, ReadHandler>,
  1632. Executor>
  1633. {
  1634. typedef typename associated_executor<ReadHandler, Executor>::type type;
  1635. static type get(
  1636. const detail::read_until_match_op_v1<AsyncReadStream,
  1637. DynamicBuffer_v1, MatchCondition, ReadHandler>& h,
  1638. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  1639. {
  1640. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  1641. }
  1642. };
  1643. #endif // !defined(GENERATING_DOCUMENTATION)
  1644. template <typename AsyncReadStream, typename DynamicBuffer_v1,
  1645. typename MatchCondition, typename ReadHandler>
  1646. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1647. void (boost::system::error_code, std::size_t))
  1648. async_read_until(AsyncReadStream& s,
  1649. BOOST_ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
  1650. MatchCondition match_condition, BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1651. typename enable_if<
  1652. is_match_condition<MatchCondition>::value
  1653. && is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
  1654. && !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
  1655. >::type*)
  1656. {
  1657. return async_initiate<ReadHandler,
  1658. void (boost::system::error_code, std::size_t)>(
  1659. detail::initiate_async_read_until_match_v1(), handler,
  1660. &s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers), match_condition);
  1661. }
  1662. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  1663. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1664. inline BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1665. void (boost::system::error_code, std::size_t))
  1666. async_read_until(AsyncReadStream& s,
  1667. boost::asio::basic_streambuf<Allocator>& b,
  1668. char delim, BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  1669. {
  1670. return async_read_until(s, basic_streambuf_ref<Allocator>(b),
  1671. delim, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  1672. }
  1673. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1674. inline BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1675. void (boost::system::error_code, std::size_t))
  1676. async_read_until(AsyncReadStream& s,
  1677. boost::asio::basic_streambuf<Allocator>& b,
  1678. BOOST_ASIO_STRING_VIEW_PARAM delim,
  1679. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  1680. {
  1681. return async_read_until(s, basic_streambuf_ref<Allocator>(b),
  1682. delim, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  1683. }
  1684. #if defined(BOOST_ASIO_HAS_BOOST_REGEX)
  1685. template <typename AsyncReadStream, typename Allocator, typename ReadHandler>
  1686. inline BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1687. void (boost::system::error_code, std::size_t))
  1688. async_read_until(AsyncReadStream& s,
  1689. boost::asio::basic_streambuf<Allocator>& b, const boost::regex& expr,
  1690. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  1691. {
  1692. return async_read_until(s, basic_streambuf_ref<Allocator>(b),
  1693. expr, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  1694. }
  1695. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  1696. template <typename AsyncReadStream, typename Allocator,
  1697. typename MatchCondition, typename ReadHandler>
  1698. inline BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1699. void (boost::system::error_code, std::size_t))
  1700. async_read_until(AsyncReadStream& s,
  1701. boost::asio::basic_streambuf<Allocator>& b,
  1702. MatchCondition match_condition, BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1703. typename enable_if<is_match_condition<MatchCondition>::value>::type*)
  1704. {
  1705. return async_read_until(s, basic_streambuf_ref<Allocator>(b),
  1706. match_condition, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
  1707. }
  1708. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  1709. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  1710. #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
  1711. namespace detail
  1712. {
  1713. template <typename AsyncReadStream,
  1714. typename DynamicBuffer_v2, typename ReadHandler>
  1715. class read_until_delim_op_v2
  1716. {
  1717. public:
  1718. template <typename BufferSequence>
  1719. read_until_delim_op_v2(AsyncReadStream& stream,
  1720. BOOST_ASIO_MOVE_ARG(BufferSequence) buffers,
  1721. char delim, ReadHandler& handler)
  1722. : stream_(stream),
  1723. buffers_(BOOST_ASIO_MOVE_CAST(BufferSequence)(buffers)),
  1724. delim_(delim),
  1725. start_(0),
  1726. search_position_(0),
  1727. bytes_to_read_(0),
  1728. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  1729. {
  1730. }
  1731. #if defined(BOOST_ASIO_HAS_MOVE)
  1732. read_until_delim_op_v2(const read_until_delim_op_v2& other)
  1733. : stream_(other.stream_),
  1734. buffers_(other.buffers_),
  1735. delim_(other.delim_),
  1736. start_(other.start_),
  1737. search_position_(other.search_position_),
  1738. bytes_to_read_(other.bytes_to_read_),
  1739. handler_(other.handler_)
  1740. {
  1741. }
  1742. read_until_delim_op_v2(read_until_delim_op_v2&& other)
  1743. : stream_(other.stream_),
  1744. buffers_(BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(other.buffers_)),
  1745. delim_(other.delim_),
  1746. start_(other.start_),
  1747. search_position_(other.search_position_),
  1748. bytes_to_read_(other.bytes_to_read_),
  1749. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  1750. {
  1751. }
  1752. #endif // defined(BOOST_ASIO_HAS_MOVE)
  1753. void operator()(const boost::system::error_code& ec,
  1754. std::size_t bytes_transferred, int start = 0)
  1755. {
  1756. const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();
  1757. std::size_t pos;
  1758. switch (start_ = start)
  1759. {
  1760. case 1:
  1761. for (;;)
  1762. {
  1763. {
  1764. // Determine the range of the data to be searched.
  1765. typedef typename DynamicBuffer_v2::const_buffers_type
  1766. buffers_type;
  1767. typedef buffers_iterator<buffers_type> iterator;
  1768. buffers_type data_buffers =
  1769. const_cast<const DynamicBuffer_v2&>(buffers_).data(
  1770. 0, buffers_.size());
  1771. iterator begin = iterator::begin(data_buffers);
  1772. iterator start_pos = begin + search_position_;
  1773. iterator end = iterator::end(data_buffers);
  1774. // Look for a match.
  1775. iterator iter = std::find(start_pos, end, delim_);
  1776. if (iter != end)
  1777. {
  1778. // Found a match. We're done.
  1779. search_position_ = iter - begin + 1;
  1780. bytes_to_read_ = 0;
  1781. }
  1782. // No match yet. Check if buffer is full.
  1783. else if (buffers_.size() == buffers_.max_size())
  1784. {
  1785. search_position_ = not_found;
  1786. bytes_to_read_ = 0;
  1787. }
  1788. // Need to read some more data.
  1789. else
  1790. {
  1791. // Next search can start with the new data.
  1792. search_position_ = end - begin;
  1793. bytes_to_read_ = std::min<std::size_t>(
  1794. std::max<std::size_t>(512,
  1795. buffers_.capacity() - buffers_.size()),
  1796. std::min<std::size_t>(65536,
  1797. buffers_.max_size() - buffers_.size()));
  1798. }
  1799. }
  1800. // Check if we're done.
  1801. if (!start && bytes_to_read_ == 0)
  1802. break;
  1803. // Start a new asynchronous read op_v2eration to obtain more data.
  1804. pos = buffers_.size();
  1805. buffers_.grow(bytes_to_read_);
  1806. stream_.async_read_some(buffers_.data(pos, bytes_to_read_),
  1807. BOOST_ASIO_MOVE_CAST(read_until_delim_op_v2)(*this));
  1808. return; default:
  1809. buffers_.shrink(bytes_to_read_ - bytes_transferred);
  1810. if (ec || bytes_transferred == 0)
  1811. break;
  1812. }
  1813. const boost::system::error_code result_ec =
  1814. (search_position_ == not_found)
  1815. ? error::not_found : ec;
  1816. const std::size_t result_n =
  1817. (ec || search_position_ == not_found)
  1818. ? 0 : search_position_;
  1819. handler_(result_ec, result_n);
  1820. }
  1821. }
  1822. //private:
  1823. AsyncReadStream& stream_;
  1824. DynamicBuffer_v2 buffers_;
  1825. char delim_;
  1826. int start_;
  1827. std::size_t search_position_;
  1828. std::size_t bytes_to_read_;
  1829. ReadHandler handler_;
  1830. };
  1831. template <typename AsyncReadStream,
  1832. typename DynamicBuffer_v2, typename ReadHandler>
  1833. inline void* asio_handler_allocate(std::size_t size,
  1834. read_until_delim_op_v2<AsyncReadStream,
  1835. DynamicBuffer_v2, ReadHandler>* this_handler)
  1836. {
  1837. return boost_asio_handler_alloc_helpers::allocate(
  1838. size, this_handler->handler_);
  1839. }
  1840. template <typename AsyncReadStream,
  1841. typename DynamicBuffer_v2, typename ReadHandler>
  1842. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  1843. read_until_delim_op_v2<AsyncReadStream,
  1844. DynamicBuffer_v2, ReadHandler>* this_handler)
  1845. {
  1846. boost_asio_handler_alloc_helpers::deallocate(
  1847. pointer, size, this_handler->handler_);
  1848. }
  1849. template <typename AsyncReadStream,
  1850. typename DynamicBuffer_v2, typename ReadHandler>
  1851. inline bool asio_handler_is_continuation(
  1852. read_until_delim_op_v2<AsyncReadStream,
  1853. DynamicBuffer_v2, ReadHandler>* this_handler)
  1854. {
  1855. return this_handler->start_ == 0 ? true
  1856. : boost_asio_handler_cont_helpers::is_continuation(
  1857. this_handler->handler_);
  1858. }
  1859. template <typename Function, typename AsyncReadStream,
  1860. typename DynamicBuffer_v2, typename ReadHandler>
  1861. inline void asio_handler_invoke(Function& function,
  1862. read_until_delim_op_v2<AsyncReadStream,
  1863. DynamicBuffer_v2, ReadHandler>* this_handler)
  1864. {
  1865. boost_asio_handler_invoke_helpers::invoke(
  1866. function, this_handler->handler_);
  1867. }
  1868. template <typename Function, typename AsyncReadStream,
  1869. typename DynamicBuffer_v2, typename ReadHandler>
  1870. inline void asio_handler_invoke(const Function& function,
  1871. read_until_delim_op_v2<AsyncReadStream,
  1872. DynamicBuffer_v2, ReadHandler>* this_handler)
  1873. {
  1874. boost_asio_handler_invoke_helpers::invoke(
  1875. function, this_handler->handler_);
  1876. }
  1877. struct initiate_async_read_until_delim_v2
  1878. {
  1879. template <typename ReadHandler, typename AsyncReadStream,
  1880. typename DynamicBuffer_v2>
  1881. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1882. AsyncReadStream* s, BOOST_ASIO_MOVE_ARG(DynamicBuffer_v2) buffers,
  1883. char delim) const
  1884. {
  1885. // If you get an error on the following line it means that your handler
  1886. // does not meet the documented type requirements for a ReadHandler.
  1887. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  1888. non_const_lvalue<ReadHandler> handler2(handler);
  1889. read_until_delim_op_v2<AsyncReadStream,
  1890. typename decay<DynamicBuffer_v2>::type,
  1891. typename decay<ReadHandler>::type>(
  1892. *s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
  1893. delim, handler2.value)(boost::system::error_code(), 0, 1);
  1894. }
  1895. };
  1896. } // namespace detail
  1897. #if !defined(GENERATING_DOCUMENTATION)
  1898. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  1899. typename ReadHandler, typename Allocator>
  1900. struct associated_allocator<
  1901. detail::read_until_delim_op_v2<AsyncReadStream,
  1902. DynamicBuffer_v2, ReadHandler>,
  1903. Allocator>
  1904. {
  1905. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  1906. static type get(
  1907. const detail::read_until_delim_op_v2<AsyncReadStream,
  1908. DynamicBuffer_v2, ReadHandler>& h,
  1909. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  1910. {
  1911. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  1912. }
  1913. };
  1914. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  1915. typename ReadHandler, typename Executor>
  1916. struct associated_executor<
  1917. detail::read_until_delim_op_v2<AsyncReadStream,
  1918. DynamicBuffer_v2, ReadHandler>,
  1919. Executor>
  1920. {
  1921. typedef typename associated_executor<ReadHandler, Executor>::type type;
  1922. static type get(
  1923. const detail::read_until_delim_op_v2<AsyncReadStream,
  1924. DynamicBuffer_v2, ReadHandler>& h,
  1925. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  1926. {
  1927. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  1928. }
  1929. };
  1930. #endif // !defined(GENERATING_DOCUMENTATION)
  1931. template <typename AsyncReadStream,
  1932. typename DynamicBuffer_v2, typename ReadHandler>
  1933. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  1934. void (boost::system::error_code, std::size_t))
  1935. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  1936. char delim, BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  1937. typename enable_if<
  1938. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  1939. >::type*)
  1940. {
  1941. return async_initiate<ReadHandler,
  1942. void (boost::system::error_code, std::size_t)>(
  1943. detail::initiate_async_read_until_delim_v2(), handler,
  1944. &s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers), delim);
  1945. }
  1946. namespace detail
  1947. {
  1948. template <typename AsyncReadStream,
  1949. typename DynamicBuffer_v2, typename ReadHandler>
  1950. class read_until_delim_string_op_v2
  1951. {
  1952. public:
  1953. template <typename BufferSequence>
  1954. read_until_delim_string_op_v2(AsyncReadStream& stream,
  1955. BOOST_ASIO_MOVE_ARG(BufferSequence) buffers,
  1956. const std::string& delim, ReadHandler& handler)
  1957. : stream_(stream),
  1958. buffers_(BOOST_ASIO_MOVE_CAST(BufferSequence)(buffers)),
  1959. delim_(delim),
  1960. start_(0),
  1961. search_position_(0),
  1962. bytes_to_read_(0),
  1963. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  1964. {
  1965. }
  1966. #if defined(BOOST_ASIO_HAS_MOVE)
  1967. read_until_delim_string_op_v2(const read_until_delim_string_op_v2& other)
  1968. : stream_(other.stream_),
  1969. buffers_(other.buffers_),
  1970. delim_(other.delim_),
  1971. start_(other.start_),
  1972. search_position_(other.search_position_),
  1973. bytes_to_read_(other.bytes_to_read_),
  1974. handler_(other.handler_)
  1975. {
  1976. }
  1977. read_until_delim_string_op_v2(read_until_delim_string_op_v2&& other)
  1978. : stream_(other.stream_),
  1979. buffers_(BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(other.buffers_)),
  1980. delim_(BOOST_ASIO_MOVE_CAST(std::string)(other.delim_)),
  1981. start_(other.start_),
  1982. search_position_(other.search_position_),
  1983. bytes_to_read_(other.bytes_to_read_),
  1984. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  1985. {
  1986. }
  1987. #endif // defined(BOOST_ASIO_HAS_MOVE)
  1988. void operator()(const boost::system::error_code& ec,
  1989. std::size_t bytes_transferred, int start = 0)
  1990. {
  1991. const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();
  1992. std::size_t pos;
  1993. switch (start_ = start)
  1994. {
  1995. case 1:
  1996. for (;;)
  1997. {
  1998. {
  1999. // Determine the range of the data to be searched.
  2000. typedef typename DynamicBuffer_v2::const_buffers_type
  2001. buffers_type;
  2002. typedef buffers_iterator<buffers_type> iterator;
  2003. buffers_type data_buffers =
  2004. const_cast<const DynamicBuffer_v2&>(buffers_).data(
  2005. 0, buffers_.size());
  2006. iterator begin = iterator::begin(data_buffers);
  2007. iterator start_pos = begin + search_position_;
  2008. iterator end = iterator::end(data_buffers);
  2009. // Look for a match.
  2010. std::pair<iterator, bool> result = detail::partial_search(
  2011. start_pos, end, delim_.begin(), delim_.end());
  2012. if (result.first != end && result.second)
  2013. {
  2014. // Full match. We're done.
  2015. search_position_ = result.first - begin + delim_.length();
  2016. bytes_to_read_ = 0;
  2017. }
  2018. // No match yet. Check if buffer is full.
  2019. else if (buffers_.size() == buffers_.max_size())
  2020. {
  2021. search_position_ = not_found;
  2022. bytes_to_read_ = 0;
  2023. }
  2024. // Need to read some more data.
  2025. else
  2026. {
  2027. if (result.first != end)
  2028. {
  2029. // Partial match. Next search needs to start from beginning of
  2030. // match.
  2031. search_position_ = result.first - begin;
  2032. }
  2033. else
  2034. {
  2035. // Next search can start with the new data.
  2036. search_position_ = end - begin;
  2037. }
  2038. bytes_to_read_ = std::min<std::size_t>(
  2039. std::max<std::size_t>(512,
  2040. buffers_.capacity() - buffers_.size()),
  2041. std::min<std::size_t>(65536,
  2042. buffers_.max_size() - buffers_.size()));
  2043. }
  2044. }
  2045. // Check if we're done.
  2046. if (!start && bytes_to_read_ == 0)
  2047. break;
  2048. // Start a new asynchronous read op_v2eration to obtain more data.
  2049. pos = buffers_.size();
  2050. buffers_.grow(bytes_to_read_);
  2051. stream_.async_read_some(buffers_.data(pos, bytes_to_read_),
  2052. BOOST_ASIO_MOVE_CAST(read_until_delim_string_op_v2)(*this));
  2053. return; default:
  2054. buffers_.shrink(bytes_to_read_ - bytes_transferred);
  2055. if (ec || bytes_transferred == 0)
  2056. break;
  2057. }
  2058. const boost::system::error_code result_ec =
  2059. (search_position_ == not_found)
  2060. ? error::not_found : ec;
  2061. const std::size_t result_n =
  2062. (ec || search_position_ == not_found)
  2063. ? 0 : search_position_;
  2064. handler_(result_ec, result_n);
  2065. }
  2066. }
  2067. //private:
  2068. AsyncReadStream& stream_;
  2069. DynamicBuffer_v2 buffers_;
  2070. std::string delim_;
  2071. int start_;
  2072. std::size_t search_position_;
  2073. std::size_t bytes_to_read_;
  2074. ReadHandler handler_;
  2075. };
  2076. template <typename AsyncReadStream,
  2077. typename DynamicBuffer_v2, typename ReadHandler>
  2078. inline void* asio_handler_allocate(std::size_t size,
  2079. read_until_delim_string_op_v2<AsyncReadStream,
  2080. DynamicBuffer_v2, ReadHandler>* this_handler)
  2081. {
  2082. return boost_asio_handler_alloc_helpers::allocate(
  2083. size, this_handler->handler_);
  2084. }
  2085. template <typename AsyncReadStream,
  2086. typename DynamicBuffer_v2, typename ReadHandler>
  2087. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  2088. read_until_delim_string_op_v2<AsyncReadStream,
  2089. DynamicBuffer_v2, ReadHandler>* this_handler)
  2090. {
  2091. boost_asio_handler_alloc_helpers::deallocate(
  2092. pointer, size, this_handler->handler_);
  2093. }
  2094. template <typename AsyncReadStream,
  2095. typename DynamicBuffer_v2, typename ReadHandler>
  2096. inline bool asio_handler_is_continuation(
  2097. read_until_delim_string_op_v2<AsyncReadStream,
  2098. DynamicBuffer_v2, ReadHandler>* this_handler)
  2099. {
  2100. return this_handler->start_ == 0 ? true
  2101. : boost_asio_handler_cont_helpers::is_continuation(
  2102. this_handler->handler_);
  2103. }
  2104. template <typename Function, typename AsyncReadStream,
  2105. typename DynamicBuffer_v2, typename ReadHandler>
  2106. inline void asio_handler_invoke(Function& function,
  2107. read_until_delim_string_op_v2<AsyncReadStream,
  2108. DynamicBuffer_v2, ReadHandler>* this_handler)
  2109. {
  2110. boost_asio_handler_invoke_helpers::invoke(
  2111. function, this_handler->handler_);
  2112. }
  2113. template <typename Function, typename AsyncReadStream,
  2114. typename DynamicBuffer_v2, typename ReadHandler>
  2115. inline void asio_handler_invoke(const Function& function,
  2116. read_until_delim_string_op_v2<AsyncReadStream,
  2117. DynamicBuffer_v2, ReadHandler>* this_handler)
  2118. {
  2119. boost_asio_handler_invoke_helpers::invoke(
  2120. function, this_handler->handler_);
  2121. }
  2122. struct initiate_async_read_until_delim_string_v2
  2123. {
  2124. template <typename ReadHandler, typename AsyncReadStream,
  2125. typename DynamicBuffer_v2>
  2126. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  2127. AsyncReadStream* s, BOOST_ASIO_MOVE_ARG(DynamicBuffer_v2) buffers,
  2128. const std::string& delim) const
  2129. {
  2130. // If you get an error on the following line it means that your handler
  2131. // does not meet the documented type requirements for a ReadHandler.
  2132. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  2133. non_const_lvalue<ReadHandler> handler2(handler);
  2134. read_until_delim_string_op_v2<AsyncReadStream,
  2135. typename decay<DynamicBuffer_v2>::type,
  2136. typename decay<ReadHandler>::type>(
  2137. *s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
  2138. delim, handler2.value)(boost::system::error_code(), 0, 1);
  2139. }
  2140. };
  2141. } // namespace detail
  2142. #if !defined(GENERATING_DOCUMENTATION)
  2143. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2144. typename ReadHandler, typename Allocator>
  2145. struct associated_allocator<
  2146. detail::read_until_delim_string_op_v2<AsyncReadStream,
  2147. DynamicBuffer_v2, ReadHandler>,
  2148. Allocator>
  2149. {
  2150. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  2151. static type get(
  2152. const detail::read_until_delim_string_op_v2<AsyncReadStream,
  2153. DynamicBuffer_v2, ReadHandler>& h,
  2154. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  2155. {
  2156. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  2157. }
  2158. };
  2159. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2160. typename ReadHandler, typename Executor>
  2161. struct associated_executor<
  2162. detail::read_until_delim_string_op_v2<AsyncReadStream,
  2163. DynamicBuffer_v2, ReadHandler>,
  2164. Executor>
  2165. {
  2166. typedef typename associated_executor<ReadHandler, Executor>::type type;
  2167. static type get(
  2168. const detail::read_until_delim_string_op_v2<AsyncReadStream,
  2169. DynamicBuffer_v2, ReadHandler>& h,
  2170. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  2171. {
  2172. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  2173. }
  2174. };
  2175. #endif // !defined(GENERATING_DOCUMENTATION)
  2176. template <typename AsyncReadStream,
  2177. typename DynamicBuffer_v2, typename ReadHandler>
  2178. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2179. void (boost::system::error_code, std::size_t))
  2180. async_read_until(AsyncReadStream& s,
  2181. DynamicBuffer_v2 buffers, BOOST_ASIO_STRING_VIEW_PARAM delim,
  2182. BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  2183. typename enable_if<
  2184. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2185. >::type*)
  2186. {
  2187. return async_initiate<ReadHandler,
  2188. void (boost::system::error_code, std::size_t)>(
  2189. detail::initiate_async_read_until_delim_string_v2(),
  2190. handler, &s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
  2191. static_cast<std::string>(delim));
  2192. }
  2193. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  2194. #if defined(BOOST_ASIO_HAS_BOOST_REGEX)
  2195. namespace detail
  2196. {
  2197. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2198. typename RegEx, typename ReadHandler>
  2199. class read_until_expr_op_v2
  2200. {
  2201. public:
  2202. template <typename BufferSequence>
  2203. read_until_expr_op_v2(AsyncReadStream& stream,
  2204. BOOST_ASIO_MOVE_ARG(BufferSequence) buffers,
  2205. const boost::regex& expr, ReadHandler& handler)
  2206. : stream_(stream),
  2207. buffers_(BOOST_ASIO_MOVE_CAST(BufferSequence)(buffers)),
  2208. expr_(expr),
  2209. start_(0),
  2210. search_position_(0),
  2211. bytes_to_read_(0),
  2212. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  2213. {
  2214. }
  2215. #if defined(BOOST_ASIO_HAS_MOVE)
  2216. read_until_expr_op_v2(const read_until_expr_op_v2& other)
  2217. : stream_(other.stream_),
  2218. buffers_(other.buffers_),
  2219. expr_(other.expr_),
  2220. start_(other.start_),
  2221. search_position_(other.search_position_),
  2222. bytes_to_read_(other.bytes_to_read_),
  2223. handler_(other.handler_)
  2224. {
  2225. }
  2226. read_until_expr_op_v2(read_until_expr_op_v2&& other)
  2227. : stream_(other.stream_),
  2228. buffers_(BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(other.buffers_)),
  2229. expr_(other.expr_),
  2230. start_(other.start_),
  2231. search_position_(other.search_position_),
  2232. bytes_to_read_(other.bytes_to_read_),
  2233. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  2234. {
  2235. }
  2236. #endif // defined(BOOST_ASIO_HAS_MOVE)
  2237. void operator()(const boost::system::error_code& ec,
  2238. std::size_t bytes_transferred, int start = 0)
  2239. {
  2240. const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();
  2241. std::size_t pos;
  2242. switch (start_ = start)
  2243. {
  2244. case 1:
  2245. for (;;)
  2246. {
  2247. {
  2248. // Determine the range of the data to be searched.
  2249. typedef typename DynamicBuffer_v2::const_buffers_type
  2250. buffers_type;
  2251. typedef buffers_iterator<buffers_type> iterator;
  2252. buffers_type data_buffers =
  2253. const_cast<const DynamicBuffer_v2&>(buffers_).data(
  2254. 0, buffers_.size());
  2255. iterator begin = iterator::begin(data_buffers);
  2256. iterator start_pos = begin + search_position_;
  2257. iterator end = iterator::end(data_buffers);
  2258. // Look for a match.
  2259. boost::match_results<iterator,
  2260. typename std::vector<boost::sub_match<iterator> >::allocator_type>
  2261. match_results;
  2262. bool match = regex_search(start_pos, end, match_results, expr_,
  2263. boost::match_default | boost::match_partial);
  2264. if (match && match_results[0].matched)
  2265. {
  2266. // Full match. We're done.
  2267. search_position_ = match_results[0].second - begin;
  2268. bytes_to_read_ = 0;
  2269. }
  2270. // No match yet. Check if buffer is full.
  2271. else if (buffers_.size() == buffers_.max_size())
  2272. {
  2273. search_position_ = not_found;
  2274. bytes_to_read_ = 0;
  2275. }
  2276. // Need to read some more data.
  2277. else
  2278. {
  2279. if (match)
  2280. {
  2281. // Partial match. Next search needs to start from beginning of
  2282. // match.
  2283. search_position_ = match_results[0].first - begin;
  2284. }
  2285. else
  2286. {
  2287. // Next search can start with the new data.
  2288. search_position_ = end - begin;
  2289. }
  2290. bytes_to_read_ = std::min<std::size_t>(
  2291. std::max<std::size_t>(512,
  2292. buffers_.capacity() - buffers_.size()),
  2293. std::min<std::size_t>(65536,
  2294. buffers_.max_size() - buffers_.size()));
  2295. }
  2296. }
  2297. // Check if we're done.
  2298. if (!start && bytes_to_read_ == 0)
  2299. break;
  2300. // Start a new asynchronous read op_v2eration to obtain more data.
  2301. pos = buffers_.size();
  2302. buffers_.grow(bytes_to_read_);
  2303. stream_.async_read_some(buffers_.data(pos, bytes_to_read_),
  2304. BOOST_ASIO_MOVE_CAST(read_until_expr_op_v2)(*this));
  2305. return; default:
  2306. buffers_.shrink(bytes_to_read_ - bytes_transferred);
  2307. if (ec || bytes_transferred == 0)
  2308. break;
  2309. }
  2310. const boost::system::error_code result_ec =
  2311. (search_position_ == not_found)
  2312. ? error::not_found : ec;
  2313. const std::size_t result_n =
  2314. (ec || search_position_ == not_found)
  2315. ? 0 : search_position_;
  2316. handler_(result_ec, result_n);
  2317. }
  2318. }
  2319. //private:
  2320. AsyncReadStream& stream_;
  2321. DynamicBuffer_v2 buffers_;
  2322. RegEx expr_;
  2323. int start_;
  2324. std::size_t search_position_;
  2325. std::size_t bytes_to_read_;
  2326. ReadHandler handler_;
  2327. };
  2328. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2329. typename RegEx, typename ReadHandler>
  2330. inline void* asio_handler_allocate(std::size_t size,
  2331. read_until_expr_op_v2<AsyncReadStream,
  2332. DynamicBuffer_v2, RegEx, ReadHandler>* this_handler)
  2333. {
  2334. return boost_asio_handler_alloc_helpers::allocate(
  2335. size, this_handler->handler_);
  2336. }
  2337. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2338. typename RegEx, typename ReadHandler>
  2339. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  2340. read_until_expr_op_v2<AsyncReadStream,
  2341. DynamicBuffer_v2, RegEx, ReadHandler>* this_handler)
  2342. {
  2343. boost_asio_handler_alloc_helpers::deallocate(
  2344. pointer, size, this_handler->handler_);
  2345. }
  2346. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2347. typename RegEx, typename ReadHandler>
  2348. inline bool asio_handler_is_continuation(
  2349. read_until_expr_op_v2<AsyncReadStream,
  2350. DynamicBuffer_v2, RegEx, ReadHandler>* this_handler)
  2351. {
  2352. return this_handler->start_ == 0 ? true
  2353. : boost_asio_handler_cont_helpers::is_continuation(
  2354. this_handler->handler_);
  2355. }
  2356. template <typename Function, typename AsyncReadStream,
  2357. typename DynamicBuffer_v2, typename RegEx, typename ReadHandler>
  2358. inline void asio_handler_invoke(Function& function,
  2359. read_until_expr_op_v2<AsyncReadStream,
  2360. DynamicBuffer_v2, RegEx, ReadHandler>* this_handler)
  2361. {
  2362. boost_asio_handler_invoke_helpers::invoke(
  2363. function, this_handler->handler_);
  2364. }
  2365. template <typename Function, typename AsyncReadStream,
  2366. typename DynamicBuffer_v2, typename RegEx, typename ReadHandler>
  2367. inline void asio_handler_invoke(const Function& function,
  2368. read_until_expr_op_v2<AsyncReadStream,
  2369. DynamicBuffer_v2, RegEx, ReadHandler>* this_handler)
  2370. {
  2371. boost_asio_handler_invoke_helpers::invoke(
  2372. function, this_handler->handler_);
  2373. }
  2374. struct initiate_async_read_until_expr_v2
  2375. {
  2376. template <typename ReadHandler, typename AsyncReadStream,
  2377. typename DynamicBuffer_v2, typename RegEx>
  2378. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  2379. AsyncReadStream* s, BOOST_ASIO_MOVE_ARG(DynamicBuffer_v2) buffers,
  2380. const RegEx& expr) const
  2381. {
  2382. // If you get an error on the following line it means that your handler
  2383. // does not meet the documented type requirements for a ReadHandler.
  2384. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  2385. non_const_lvalue<ReadHandler> handler2(handler);
  2386. read_until_expr_op_v2<AsyncReadStream,
  2387. typename decay<DynamicBuffer_v2>::type,
  2388. RegEx, typename decay<ReadHandler>::type>(
  2389. *s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
  2390. expr, handler2.value)(boost::system::error_code(), 0, 1);
  2391. }
  2392. };
  2393. } // namespace detail
  2394. #if !defined(GENERATING_DOCUMENTATION)
  2395. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2396. typename RegEx, typename ReadHandler, typename Allocator>
  2397. struct associated_allocator<
  2398. detail::read_until_expr_op_v2<AsyncReadStream,
  2399. DynamicBuffer_v2, RegEx, ReadHandler>,
  2400. Allocator>
  2401. {
  2402. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  2403. static type get(
  2404. const detail::read_until_expr_op_v2<AsyncReadStream,
  2405. DynamicBuffer_v2, RegEx, ReadHandler>& h,
  2406. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  2407. {
  2408. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  2409. }
  2410. };
  2411. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2412. typename RegEx, typename ReadHandler, typename Executor>
  2413. struct associated_executor<
  2414. detail::read_until_expr_op_v2<AsyncReadStream,
  2415. DynamicBuffer_v2, RegEx, ReadHandler>,
  2416. Executor>
  2417. {
  2418. typedef typename associated_executor<ReadHandler, Executor>::type type;
  2419. static type get(
  2420. const detail::read_until_expr_op_v2<AsyncReadStream,
  2421. DynamicBuffer_v2, RegEx, ReadHandler>& h,
  2422. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  2423. {
  2424. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  2425. }
  2426. };
  2427. #endif // !defined(GENERATING_DOCUMENTATION)
  2428. template <typename AsyncReadStream,
  2429. typename DynamicBuffer_v2, typename ReadHandler>
  2430. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2431. void (boost::system::error_code, std::size_t))
  2432. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2433. const boost::regex& expr, BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  2434. typename enable_if<
  2435. is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2436. >::type*)
  2437. {
  2438. return async_initiate<ReadHandler,
  2439. void (boost::system::error_code, std::size_t)>(
  2440. detail::initiate_async_read_until_expr_v2(), handler,
  2441. &s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers), expr);
  2442. }
  2443. #endif // defined(BOOST_ASIO_HAS_BOOST_REGEX)
  2444. namespace detail
  2445. {
  2446. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2447. typename MatchCondition, typename ReadHandler>
  2448. class read_until_match_op_v2
  2449. {
  2450. public:
  2451. template <typename BufferSequence>
  2452. read_until_match_op_v2(AsyncReadStream& stream,
  2453. BOOST_ASIO_MOVE_ARG(BufferSequence) buffers,
  2454. MatchCondition match_condition, ReadHandler& handler)
  2455. : stream_(stream),
  2456. buffers_(BOOST_ASIO_MOVE_CAST(BufferSequence)(buffers)),
  2457. match_condition_(match_condition),
  2458. start_(0),
  2459. search_position_(0),
  2460. bytes_to_read_(0),
  2461. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  2462. {
  2463. }
  2464. #if defined(BOOST_ASIO_HAS_MOVE)
  2465. read_until_match_op_v2(const read_until_match_op_v2& other)
  2466. : stream_(other.stream_),
  2467. buffers_(other.buffers_),
  2468. match_condition_(other.match_condition_),
  2469. start_(other.start_),
  2470. search_position_(other.search_position_),
  2471. bytes_to_read_(other.bytes_to_read_),
  2472. handler_(other.handler_)
  2473. {
  2474. }
  2475. read_until_match_op_v2(read_until_match_op_v2&& other)
  2476. : stream_(other.stream_),
  2477. buffers_(BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(other.buffers_)),
  2478. match_condition_(other.match_condition_),
  2479. start_(other.start_),
  2480. search_position_(other.search_position_),
  2481. bytes_to_read_(other.bytes_to_read_),
  2482. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  2483. {
  2484. }
  2485. #endif // defined(BOOST_ASIO_HAS_MOVE)
  2486. void operator()(const boost::system::error_code& ec,
  2487. std::size_t bytes_transferred, int start = 0)
  2488. {
  2489. const std::size_t not_found = (std::numeric_limits<std::size_t>::max)();
  2490. std::size_t pos;
  2491. switch (start_ = start)
  2492. {
  2493. case 1:
  2494. for (;;)
  2495. {
  2496. {
  2497. // Determine the range of the data to be searched.
  2498. typedef typename DynamicBuffer_v2::const_buffers_type
  2499. buffers_type;
  2500. typedef buffers_iterator<buffers_type> iterator;
  2501. buffers_type data_buffers =
  2502. const_cast<const DynamicBuffer_v2&>(buffers_).data(
  2503. 0, buffers_.size());
  2504. iterator begin = iterator::begin(data_buffers);
  2505. iterator start_pos = begin + search_position_;
  2506. iterator end = iterator::end(data_buffers);
  2507. // Look for a match.
  2508. std::pair<iterator, bool> result = match_condition_(start_pos, end);
  2509. if (result.second)
  2510. {
  2511. // Full match. We're done.
  2512. search_position_ = result.first - begin;
  2513. bytes_to_read_ = 0;
  2514. }
  2515. // No match yet. Check if buffer is full.
  2516. else if (buffers_.size() == buffers_.max_size())
  2517. {
  2518. search_position_ = not_found;
  2519. bytes_to_read_ = 0;
  2520. }
  2521. // Need to read some more data.
  2522. else
  2523. {
  2524. if (result.first != end)
  2525. {
  2526. // Partial match. Next search needs to start from beginning of
  2527. // match.
  2528. search_position_ = result.first - begin;
  2529. }
  2530. else
  2531. {
  2532. // Next search can start with the new data.
  2533. search_position_ = end - begin;
  2534. }
  2535. bytes_to_read_ = std::min<std::size_t>(
  2536. std::max<std::size_t>(512,
  2537. buffers_.capacity() - buffers_.size()),
  2538. std::min<std::size_t>(65536,
  2539. buffers_.max_size() - buffers_.size()));
  2540. }
  2541. }
  2542. // Check if we're done.
  2543. if (!start && bytes_to_read_ == 0)
  2544. break;
  2545. // Start a new asynchronous read op_v2eration to obtain more data.
  2546. pos = buffers_.size();
  2547. buffers_.grow(bytes_to_read_);
  2548. stream_.async_read_some(buffers_.data(pos, bytes_to_read_),
  2549. BOOST_ASIO_MOVE_CAST(read_until_match_op_v2)(*this));
  2550. return; default:
  2551. buffers_.shrink(bytes_to_read_ - bytes_transferred);
  2552. if (ec || bytes_transferred == 0)
  2553. break;
  2554. }
  2555. const boost::system::error_code result_ec =
  2556. (search_position_ == not_found)
  2557. ? error::not_found : ec;
  2558. const std::size_t result_n =
  2559. (ec || search_position_ == not_found)
  2560. ? 0 : search_position_;
  2561. handler_(result_ec, result_n);
  2562. }
  2563. }
  2564. //private:
  2565. AsyncReadStream& stream_;
  2566. DynamicBuffer_v2 buffers_;
  2567. MatchCondition match_condition_;
  2568. int start_;
  2569. std::size_t search_position_;
  2570. std::size_t bytes_to_read_;
  2571. ReadHandler handler_;
  2572. };
  2573. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2574. typename MatchCondition, typename ReadHandler>
  2575. inline void* asio_handler_allocate(std::size_t size,
  2576. read_until_match_op_v2<AsyncReadStream, DynamicBuffer_v2,
  2577. MatchCondition, ReadHandler>* this_handler)
  2578. {
  2579. return boost_asio_handler_alloc_helpers::allocate(
  2580. size, this_handler->handler_);
  2581. }
  2582. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2583. typename MatchCondition, typename ReadHandler>
  2584. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  2585. read_until_match_op_v2<AsyncReadStream, DynamicBuffer_v2,
  2586. MatchCondition, ReadHandler>* this_handler)
  2587. {
  2588. boost_asio_handler_alloc_helpers::deallocate(
  2589. pointer, size, this_handler->handler_);
  2590. }
  2591. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2592. typename MatchCondition, typename ReadHandler>
  2593. inline bool asio_handler_is_continuation(
  2594. read_until_match_op_v2<AsyncReadStream, DynamicBuffer_v2,
  2595. MatchCondition, ReadHandler>* this_handler)
  2596. {
  2597. return this_handler->start_ == 0 ? true
  2598. : boost_asio_handler_cont_helpers::is_continuation(
  2599. this_handler->handler_);
  2600. }
  2601. template <typename Function, typename AsyncReadStream,
  2602. typename DynamicBuffer_v2, typename MatchCondition,
  2603. typename ReadHandler>
  2604. inline void asio_handler_invoke(Function& function,
  2605. read_until_match_op_v2<AsyncReadStream, DynamicBuffer_v2,
  2606. MatchCondition, ReadHandler>* this_handler)
  2607. {
  2608. boost_asio_handler_invoke_helpers::invoke(
  2609. function, this_handler->handler_);
  2610. }
  2611. template <typename Function, typename AsyncReadStream,
  2612. typename DynamicBuffer_v2, typename MatchCondition,
  2613. typename ReadHandler>
  2614. inline void asio_handler_invoke(const Function& function,
  2615. read_until_match_op_v2<AsyncReadStream, DynamicBuffer_v2,
  2616. MatchCondition, ReadHandler>* this_handler)
  2617. {
  2618. boost_asio_handler_invoke_helpers::invoke(
  2619. function, this_handler->handler_);
  2620. }
  2621. struct initiate_async_read_until_match_v2
  2622. {
  2623. template <typename ReadHandler, typename AsyncReadStream,
  2624. typename DynamicBuffer_v2, typename MatchCondition>
  2625. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  2626. AsyncReadStream* s, BOOST_ASIO_MOVE_ARG(DynamicBuffer_v2) buffers,
  2627. MatchCondition match_condition) const
  2628. {
  2629. // If you get an error on the following line it means that your handler
  2630. // does not meet the documented type requirements for a ReadHandler.
  2631. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  2632. non_const_lvalue<ReadHandler> handler2(handler);
  2633. read_until_match_op_v2<AsyncReadStream,
  2634. typename decay<DynamicBuffer_v2>::type,
  2635. MatchCondition, typename decay<ReadHandler>::type>(
  2636. *s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
  2637. match_condition, handler2.value)(boost::system::error_code(), 0, 1);
  2638. }
  2639. };
  2640. } // namespace detail
  2641. #if !defined(GENERATING_DOCUMENTATION)
  2642. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2643. typename MatchCondition, typename ReadHandler, typename Allocator>
  2644. struct associated_allocator<
  2645. detail::read_until_match_op_v2<AsyncReadStream,
  2646. DynamicBuffer_v2, MatchCondition, ReadHandler>,
  2647. Allocator>
  2648. {
  2649. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  2650. static type get(
  2651. const detail::read_until_match_op_v2<AsyncReadStream,
  2652. DynamicBuffer_v2, MatchCondition, ReadHandler>& h,
  2653. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  2654. {
  2655. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  2656. }
  2657. };
  2658. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2659. typename MatchCondition, typename ReadHandler, typename Executor>
  2660. struct associated_executor<
  2661. detail::read_until_match_op_v2<AsyncReadStream,
  2662. DynamicBuffer_v2, MatchCondition, ReadHandler>,
  2663. Executor>
  2664. {
  2665. typedef typename associated_executor<ReadHandler, Executor>::type type;
  2666. static type get(
  2667. const detail::read_until_match_op_v2<AsyncReadStream,
  2668. DynamicBuffer_v2, MatchCondition, ReadHandler>& h,
  2669. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  2670. {
  2671. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  2672. }
  2673. };
  2674. #endif // !defined(GENERATING_DOCUMENTATION)
  2675. template <typename AsyncReadStream, typename DynamicBuffer_v2,
  2676. typename MatchCondition, typename ReadHandler>
  2677. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  2678. void (boost::system::error_code, std::size_t))
  2679. async_read_until(AsyncReadStream& s, DynamicBuffer_v2 buffers,
  2680. MatchCondition match_condition, BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  2681. typename enable_if<
  2682. is_match_condition<MatchCondition>::value
  2683. && is_dynamic_buffer_v2<DynamicBuffer_v2>::value
  2684. >::type*)
  2685. {
  2686. return async_initiate<ReadHandler,
  2687. void (boost::system::error_code, std::size_t)>(
  2688. detail::initiate_async_read_until_match_v2(), handler,
  2689. &s, BOOST_ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers), match_condition);
  2690. }
  2691. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  2692. } // namespace asio
  2693. } // namespace boost
  2694. #include <boost/asio/detail/pop_options.hpp>
  2695. #endif // BOOST_ASIO_IMPL_READ_UNTIL_HPP