Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:30

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_HTTP_IMPL_FIELDS_IPP
0011 #define BOOST_BEAST_HTTP_IMPL_FIELDS_IPP
0012 
0013 #include <boost/beast/http/fields.hpp>
0014 
0015 namespace boost {
0016 namespace beast {
0017 namespace http {
0018 namespace detail {
0019 
0020 // `basic_fields` assumes that `std::size_t` is larger than `uint16_t`, so we
0021 // verify it explicitly here, so that users that use split compilation don't
0022 // need to pay the (fairly small) price for this sanity check
0023 BOOST_STATIC_ASSERT((std::numeric_limits<std::size_t>::max)() >=
0024     (std::numeric_limits<std::uint32_t>::max)());
0025 
0026 // Filter a token list
0027 //
0028 inline
0029 void
0030 filter_token_list(
0031     beast::detail::temporary_buffer& s,
0032     string_view value,
0033     iequals_predicate const& pred)
0034 {
0035     token_list te{value};
0036     auto it = te.begin();
0037     auto last = te.end();
0038     if(it == last)
0039         return;
0040     while(pred(*it))
0041         if(++it == last)
0042             return;
0043     s.append(*it);
0044     while(++it != last)
0045     {
0046         if(! pred(*it))
0047         {
0048             s.append(", ", *it);
0049         }
0050     }
0051 }
0052 
0053 void
0054 filter_token_list_last(
0055     beast::detail::temporary_buffer& s,
0056     string_view value,
0057     iequals_predicate const& pred)
0058 {
0059     token_list te{value};
0060     if(te.begin() != te.end())
0061     {
0062         auto it = te.begin();
0063         auto next = std::next(it);
0064         if(next == te.end())
0065         {
0066             if(! pred(*it))
0067                 s.append(*it);
0068             return;
0069         }
0070         s.append(*it);
0071         for(;;)
0072         {
0073             it = next;
0074             next = std::next(it);
0075             if(next == te.end())
0076             {
0077                 if(! pred(*it))
0078                 {
0079                     s.append(", ", *it);
0080                 }
0081                 return;
0082             }
0083             s.append(", ", *it);
0084         }
0085     }
0086 }
0087 
0088 void
0089 keep_alive_impl(
0090     beast::detail::temporary_buffer& s, string_view value,
0091     unsigned version, bool keep_alive)
0092 {
0093     if(version < 11)
0094     {
0095         if(keep_alive)
0096         {
0097             // remove close
0098             filter_token_list(s, value, iequals_predicate{"close", {}});
0099             // add keep-alive
0100             if(s.empty())
0101                 s.append("keep-alive");
0102             else if(! token_list{value}.exists("keep-alive"))
0103                 s.append(", keep-alive");
0104         }
0105         else
0106         {
0107             // remove close and keep-alive
0108             filter_token_list(s, value,
0109                 iequals_predicate{"close", "keep-alive"});
0110         }
0111     }
0112     else
0113     {
0114         if(keep_alive)
0115         {
0116             // remove close and keep-alive
0117             filter_token_list(s, value,
0118                 iequals_predicate{"close", "keep-alive"});
0119         }
0120         else
0121         {
0122             // remove keep-alive
0123             filter_token_list(s, value, iequals_predicate{"keep-alive", {}});
0124             // add close
0125             if(s.empty())
0126                 s.append("close");
0127             else if(! token_list{value}.exists("close"))
0128                 s.append(", close");
0129         }
0130     }
0131 }
0132 
0133 } // detail
0134 } // http
0135 } // beast
0136 } // boost
0137 
0138 #endif // BOOST_BEAST_HTTP_IMPL_FIELDS_IPP