95.12% Lines (39/41) 100.00% Functions (2/2)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Mohammad Nejati 2   // Copyright (c) 2025 Mohammad Nejati
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/http 7   // Official repository: https://github.com/cppalliance/http
8   // 8   //
9   9  
10   #include <boost/http/response_base.hpp> 10   #include <boost/http/response_base.hpp>
11   11  
12   #include <cstring> 12   #include <cstring>
13   13  
14   namespace boost { 14   namespace boost {
15   namespace http { 15   namespace http {
16   16  
17   void 17   void
HITCBC 18   26 response_base:: 18   26 response_base::
19   set_start_line_impl( 19   set_start_line_impl(
20   http::status sc, 20   http::status sc,
21   unsigned short si, 21   unsigned short si,
22   core::string_view rs, 22   core::string_view rs,
23   http::version v) 23   http::version v)
24   { 24   {
25   // TODO: check validity 25   // TODO: check validity
HITCBC 26   26 auto const vs = to_string(v); 26   26 auto const vs = to_string(v);
27   auto const new_prefix = 27   auto const new_prefix =
HITCBC 28   26 vs.size() + 1 + // HTTP-version SP 28   26 vs.size() + 1 + // HTTP-version SP
HITCBC 29   26 3 + 1 + // status-code SP 29   26 3 + 1 + // status-code SP
HITCBC 30   26 rs.size() + 2; // reason-phrase CRLF 30   26 rs.size() + 2; // reason-phrase CRLF
31   31  
32   // Introduce a new scope so that prefix_op's 32   // Introduce a new scope so that prefix_op's
33   // destructor runs before h_.on_start_line(). 33   // destructor runs before h_.on_start_line().
34   { 34   {
HITCBC 35   26 auto op = prefix_op_t(*this, new_prefix, &rs); 35   26 auto op = prefix_op_t(*this, new_prefix, &rs);
HITCBC 36   25 char* dest = h_.buf; 36   25 char* dest = h_.buf;
37   37  
HITCBC 38   25 h_.version = v; 38   25 h_.version = v;
HITCBC 39   25 vs.copy(dest, vs.size()); 39   25 vs.copy(dest, vs.size());
HITCBC 40   25 dest += vs.size(); 40   25 dest += vs.size();
HITCBC 41   25 *dest++ = ' '; 41   25 *dest++ = ' ';
42   42  
HITCBC 43   25 h_.res.status = sc; 43   25 h_.res.status = sc;
HITCBC 44   25 h_.res.status_int = si; 44   25 h_.res.status_int = si;
HITCBC 45   25 dest[0] = '0' + ((h_.res.status_int / 100) % 10); 45   25 dest[0] = '0' + ((h_.res.status_int / 100) % 10);
HITCBC 46   25 dest[1] = '0' + ((h_.res.status_int / 10) % 10); 46   25 dest[1] = '0' + ((h_.res.status_int / 10) % 10);
HITCBC 47   25 dest[2] = '0' + ((h_.res.status_int / 1) % 10); 47   25 dest[2] = '0' + ((h_.res.status_int / 1) % 10);
HITCBC 48   25 dest[3] = ' '; 48   25 dest[3] = ' ';
HITCBC 49   25 dest += 4; 49   25 dest += 4;
50   50  
HITCBC 51   25 std::memmove(dest, rs.data(), rs.size()); 51   25 std::memmove(dest, rs.data(), rs.size());
HITCBC 52   25 dest += rs.size(); 52   25 dest += rs.size();
HITCBC 53   25 dest[0] = '\r'; 53   25 dest[0] = '\r';
HITCBC 54   25 dest[1] = '\n'; 54   25 dest[1] = '\n';
HITCBC 55   25 } 55   25 }
56   56  
HITCBC 57   25 h_.on_start_line(); 57   25 h_.on_start_line();
HITCBC 58   25 } 58   25 }
59   59  
60   void 60   void
HITCBC 61   3 response_base:: 61   3 response_base::
62   set_version( 62   set_version(
63   http::version v) 63   http::version v)
64   { 64   {
HITCBC 65   3 if(v == h_.version) 65   3 if(v == h_.version)
HITCBC 66   1 return; 66   1 return;
HITCBC 67   2 if(h_.is_default()) 67   2 if(h_.is_default())
68   { 68   {
HITCBC 69   1 auto def = h_.get_default(detail::kind::response); 69   1 auto def = h_.get_default(detail::kind::response);
MISUBC 70   return set_start_line_impl( 70   return set_start_line_impl(
HITCBC 71   1 def->res.status, def->res.status_int, 71   1 def->res.status, def->res.status_int,
72   core::string_view( 72   core::string_view(
HITCBC 73   2 def->cbuf + 13, def->prefix - 15), v); 73   2 def->cbuf + 13, def->prefix - 15), v);
74   } 74   }
75   75  
76   // Introduce a new scope so that prefix_op's 76   // Introduce a new scope so that prefix_op's
77   // destructor runs before h_.on_start_line(). 77   // destructor runs before h_.on_start_line().
78   { 78   {
79   auto op = prefix_op_t( 79   auto op = prefix_op_t(
HITCBC 80   1 *this, h_.prefix, nullptr); 80   1 *this, h_.prefix, nullptr);
HITCBC 81   1 char* dest = h_.buf; 81   1 char* dest = h_.buf;
HITCBC 82   1 if(v == http::version::http_1_1) 82   1 if(v == http::version::http_1_1)
HITCBC 83   1 dest[7] = '1'; 83   1 dest[7] = '1';
84   else 84   else
MISUBC 85   dest[7] = '0'; 85   dest[7] = '0';
HITCBC 86   1 h_.version = v; 86   1 h_.version = v;
HITCBC 87   1 } 87   1 }
88   88  
HITCBC 89   1 h_.on_start_line(); 89   1 h_.on_start_line();
90   } 90   }
91   91  
92   } // http 92   } // http
93   } // boost 93   } // boost