rfc7230.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_HTTP_IMPL_RFC7230_HPP
  10. #define BOOST_BEAST_HTTP_IMPL_RFC7230_HPP
  11. #include <boost/beast/http/detail/rfc7230.hpp>
  12. #include <iterator>
  13. namespace boost {
  14. namespace beast {
  15. namespace http {
  16. class param_list::const_iterator
  17. {
  18. using iter_type = string_view::const_iterator;
  19. std::string s_;
  20. detail::param_iter pi_;
  21. public:
  22. using value_type = param_list::value_type;
  23. using pointer = value_type const*;
  24. using reference = value_type const&;
  25. using difference_type = std::ptrdiff_t;
  26. using iterator_category = std::input_iterator_tag;
  27. const_iterator() = default;
  28. bool
  29. operator==(const_iterator const& other) const
  30. {
  31. return
  32. other.pi_.it == pi_.it &&
  33. other.pi_.last == pi_.last &&
  34. other.pi_.first == pi_.first;
  35. }
  36. bool
  37. operator!=(const_iterator const& other) const
  38. {
  39. return !(*this == other);
  40. }
  41. reference
  42. operator*() const
  43. {
  44. return pi_.v;
  45. }
  46. pointer
  47. operator->() const
  48. {
  49. return &*(*this);
  50. }
  51. const_iterator&
  52. operator++()
  53. {
  54. increment();
  55. return *this;
  56. }
  57. const_iterator
  58. operator++(int)
  59. {
  60. auto temp = *this;
  61. ++(*this);
  62. return temp;
  63. }
  64. private:
  65. friend class param_list;
  66. const_iterator(iter_type first, iter_type last)
  67. {
  68. pi_.it = first;
  69. pi_.first = first;
  70. pi_.last = last;
  71. increment();
  72. }
  73. BOOST_BEAST_DECL
  74. static
  75. std::string
  76. unquote(string_view sr);
  77. BOOST_BEAST_DECL
  78. void
  79. increment();
  80. };
  81. inline
  82. auto
  83. param_list::
  84. begin() const ->
  85. const_iterator
  86. {
  87. return const_iterator{s_.begin(), s_.end()};
  88. }
  89. inline
  90. auto
  91. param_list::
  92. end() const ->
  93. const_iterator
  94. {
  95. return const_iterator{s_.end(), s_.end()};
  96. }
  97. inline
  98. auto
  99. param_list::
  100. cbegin() const ->
  101. const_iterator
  102. {
  103. return const_iterator{s_.begin(), s_.end()};
  104. }
  105. inline
  106. auto
  107. param_list::
  108. cend() const ->
  109. const_iterator
  110. {
  111. return const_iterator{s_.end(), s_.end()};
  112. }
  113. //------------------------------------------------------------------------------
  114. class ext_list::const_iterator
  115. {
  116. ext_list::value_type v_;
  117. iter_type it_;
  118. iter_type first_;
  119. iter_type last_;
  120. public:
  121. using value_type = ext_list::value_type;
  122. using pointer = value_type const*;
  123. using reference = value_type const&;
  124. using difference_type = std::ptrdiff_t;
  125. using iterator_category = std::forward_iterator_tag;
  126. const_iterator() = default;
  127. bool
  128. operator==(const_iterator const& other) const
  129. {
  130. return
  131. other.it_ == it_ &&
  132. other.first_ == first_ &&
  133. other.last_ == last_;
  134. }
  135. bool
  136. operator!=(const_iterator const& other) const
  137. {
  138. return !(*this == other);
  139. }
  140. reference
  141. operator*() const
  142. {
  143. return v_;
  144. }
  145. pointer
  146. operator->() const
  147. {
  148. return &*(*this);
  149. }
  150. const_iterator&
  151. operator++()
  152. {
  153. increment();
  154. return *this;
  155. }
  156. const_iterator
  157. operator++(int)
  158. {
  159. auto temp = *this;
  160. ++(*this);
  161. return temp;
  162. }
  163. private:
  164. friend class ext_list;
  165. const_iterator(iter_type begin, iter_type end)
  166. {
  167. it_ = begin;
  168. first_ = begin;
  169. last_ = end;
  170. increment();
  171. }
  172. BOOST_BEAST_DECL
  173. void
  174. increment();
  175. };
  176. inline
  177. auto
  178. ext_list::
  179. begin() const ->
  180. const_iterator
  181. {
  182. return const_iterator{s_.begin(), s_.end()};
  183. }
  184. inline
  185. auto
  186. ext_list::
  187. end() const ->
  188. const_iterator
  189. {
  190. return const_iterator{s_.end(), s_.end()};
  191. }
  192. inline
  193. auto
  194. ext_list::
  195. cbegin() const ->
  196. const_iterator
  197. {
  198. return const_iterator{s_.begin(), s_.end()};
  199. }
  200. inline
  201. auto
  202. ext_list::
  203. cend() const ->
  204. const_iterator
  205. {
  206. return const_iterator{s_.end(), s_.end()};
  207. }
  208. template<class T>
  209. auto
  210. ext_list::
  211. find(T const& s) ->
  212. const_iterator
  213. {
  214. return std::find_if(begin(), end(),
  215. [&s](value_type const& v)
  216. {
  217. return iequals(s, v.first);
  218. });
  219. }
  220. template<class T>
  221. bool
  222. ext_list::
  223. exists(T const& s)
  224. {
  225. return find(s) != end();
  226. }
  227. //------------------------------------------------------------------------------
  228. class token_list::const_iterator
  229. {
  230. token_list::value_type v_;
  231. iter_type it_;
  232. iter_type first_;
  233. iter_type last_;
  234. public:
  235. using value_type = token_list::value_type;
  236. using pointer = value_type const*;
  237. using reference = value_type const&;
  238. using difference_type = std::ptrdiff_t;
  239. using iterator_category = std::forward_iterator_tag;
  240. const_iterator() = default;
  241. bool
  242. operator==(const_iterator const& other) const
  243. {
  244. return
  245. other.it_ == it_ &&
  246. other.first_ == first_ &&
  247. other.last_ == last_;
  248. }
  249. bool
  250. operator!=(const_iterator const& other) const
  251. {
  252. return !(*this == other);
  253. }
  254. reference
  255. operator*() const
  256. {
  257. return v_;
  258. }
  259. pointer
  260. operator->() const
  261. {
  262. return &*(*this);
  263. }
  264. const_iterator&
  265. operator++()
  266. {
  267. increment();
  268. return *this;
  269. }
  270. const_iterator
  271. operator++(int)
  272. {
  273. auto temp = *this;
  274. ++(*this);
  275. return temp;
  276. }
  277. private:
  278. friend class token_list;
  279. const_iterator(iter_type begin, iter_type end)
  280. {
  281. it_ = begin;
  282. first_ = begin;
  283. last_ = end;
  284. increment();
  285. }
  286. BOOST_BEAST_DECL
  287. void
  288. increment();
  289. };
  290. inline
  291. auto
  292. token_list::
  293. begin() const ->
  294. const_iterator
  295. {
  296. return const_iterator{s_.begin(), s_.end()};
  297. }
  298. inline
  299. auto
  300. token_list::
  301. end() const ->
  302. const_iterator
  303. {
  304. return const_iterator{s_.end(), s_.end()};
  305. }
  306. inline
  307. auto
  308. token_list::
  309. cbegin() const ->
  310. const_iterator
  311. {
  312. return const_iterator{s_.begin(), s_.end()};
  313. }
  314. inline
  315. auto
  316. token_list::
  317. cend() const ->
  318. const_iterator
  319. {
  320. return const_iterator{s_.end(), s_.end()};
  321. }
  322. template<class T>
  323. bool
  324. token_list::
  325. exists(T const& s)
  326. {
  327. return std::find_if(begin(), end(),
  328. [&s](value_type const& v)
  329. {
  330. return iequals(s, v);
  331. }
  332. ) != end();
  333. }
  334. template<class Policy>
  335. bool
  336. validate_list(detail::basic_parsed_list<
  337. Policy> const& list)
  338. {
  339. auto const last = list.end();
  340. auto it = list.begin();
  341. if(it.error())
  342. return false;
  343. while(it != last)
  344. {
  345. ++it;
  346. if(it.error())
  347. return false;
  348. if(it == last)
  349. break;
  350. }
  351. return true;
  352. }
  353. } // http
  354. } // beast
  355. } // boost
  356. #ifdef BOOST_BEAST_HEADER_ONLY
  357. #include <boost/beast/http/impl/rfc7230.ipp>
  358. #endif
  359. #endif