read_until.hpp 105 KB

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