rippled
rfc2616.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2014, Vinnie Falco <vinnie.falco@gmail.com>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef BEAST_RFC2616_HPP
21 #define BEAST_RFC2616_HPP
22 
23 #include <boost/beast/http/message.hpp>
24 #include <boost/beast/http/rfc7230.hpp>
25 #include <boost/range/algorithm/equal.hpp>
26 #include <boost/range/iterator_range.hpp>
27 #include <boost/utility/string_ref.hpp>
28 #include <algorithm>
29 #include <cctype>
30 #include <iterator>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace beast {
36 namespace rfc2616 {
37 
38 namespace detail {
39 
44 inline bool
45 is_lws(char c)
46 {
47  return c == ' ' || c == '\t';
48 }
49 
51 inline bool
52 is_white(char c)
53 {
54  switch (c)
55  {
56  case ' ':
57  case '\f':
58  case '\n':
59  case '\r':
60  case '\t':
61  case '\v':
62  return true;
63  };
64  return false;
65 }
66 
67 template <class FwdIter>
68 FwdIter
69 trim_right(FwdIter first, FwdIter last)
70 {
71  if (first == last)
72  return last;
73  do
74  {
75  --last;
76  if (!is_white(*last))
77  return ++last;
78  } while (last != first);
79  return first;
80 }
81 
82 template <class String>
83 String
84 trim_right(String const& s)
85 {
86  using std::begin;
87  using std::end;
88  auto first(begin(s));
89  auto last(end(s));
90  last = trim_right(first, last);
91  return {first, last};
92 }
93 
94 } // namespace detail
95 
104 template <
105  class FwdIt,
106  class Result = std::vector<
108  class Char>
109 Result
110 split(FwdIt first, FwdIt last, Char delim)
111 {
112  using namespace detail;
113  using string = typename Result::value_type;
114 
115  Result result;
116 
117  FwdIt iter = first;
118  string e;
119  while (iter != last)
120  {
121  if (*iter == '"')
122  {
123  // quoted-string
124  ++iter;
125  while (iter != last)
126  {
127  if (*iter == '"')
128  {
129  ++iter;
130  break;
131  }
132 
133  if (*iter == '\\')
134  {
135  // quoted-pair
136  ++iter;
137  if (iter != last)
138  e.append(1, *iter++);
139  }
140  else
141  {
142  // qdtext
143  e.append(1, *iter++);
144  }
145  }
146  if (!e.empty())
147  {
148  result.emplace_back(std::move(e));
149  e.clear();
150  }
151  }
152  else if (*iter == delim)
153  {
154  e = trim_right(e);
155  if (!e.empty())
156  {
157  result.emplace_back(std::move(e));
158  e.clear();
159  }
160  ++iter;
161  }
162  else if (is_lws(*iter))
163  {
164  ++iter;
165  }
166  else
167  {
168  e.append(1, *iter++);
169  }
170  }
171 
172  if (!e.empty())
173  {
174  e = trim_right(e);
175  if (!e.empty())
176  result.emplace_back(std::move(e));
177  }
178  return result;
179 }
180 
181 template <
182  class FwdIt,
183  class Result = std::vector<
185 Result
186 split_commas(FwdIt first, FwdIt last)
187 {
188  return split(first, last, ',');
189 }
190 
191 template <class Result = std::vector<std::string>>
192 Result
193 split_commas(boost::beast::string_view const& s)
194 {
195  return split_commas(s.begin(), s.end());
196 }
197 
198 template <bool isRequest, class Body, class Fields>
199 bool
200 is_keep_alive(boost::beast::http::message<isRequest, Body, Fields> const& m)
201 {
202  if (m.version() <= 10)
203  return boost::beast::http::token_list{
204  m[boost::beast::http::field::connection]}
205  .exists("keep-alive");
206  return !boost::beast::http::token_list{
207  m[boost::beast::http::field::connection]}
208  .exists("close");
209 }
210 
211 } // namespace rfc2616
212 } // namespace beast
213 
214 #endif
std::basic_string
STL class.
utility
vector
beast::rfc2616::split
Result split(FwdIt first, FwdIt last, Char delim)
Parse a character sequence of values separated by commas.
Definition: rfc2616.h:110
iterator
algorithm
beast::rfc2616::is_keep_alive
bool is_keep_alive(boost::beast::http::message< isRequest, Body, Fields > const &m)
Definition: rfc2616.h:200
beast::rfc2616::detail::is_lws
bool is_lws(char c)
Returns true if c is linear white space.
Definition: rfc2616.h:45
beast::rfc2616::split_commas
Result split_commas(FwdIt first, FwdIt last)
Definition: rfc2616.h:186
beast::rfc2616::detail::trim_right
FwdIter trim_right(FwdIter first, FwdIter last)
Definition: rfc2616.h:69
std::begin
T begin(T... args)
cctype
std::end
T end(T... args)
beast::rfc2616::detail::is_white
bool is_white(char c)
Returns true if c is any whitespace character.
Definition: rfc2616.h:52
beast
Definition: base_uint.h:585
string