libs/http/src/response_base.cpp

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