read_until.hpp 110 KB

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