src/response.cpp

55.9% Lines (19/34) 50.0% List of functions (4/8) 38.9% Branches (7/18)
response.cpp
f(x) Functions (8)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 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/burl
8 //
9
10 #include <boost/burl/response.hpp>
11
12 #include "detail/can_reuse_conn.hpp"
13
14 #include <boost/capy/error.hpp>
15 #include <boost/capy/timeout.hpp>
16
17 #include <chrono>
18 #include <utility>
19
20 namespace boost
21 {
22 namespace burl
23 {
24
25 24x response::response(
26 urls::url url,
27 detail::pooled_connection conn,
28 http::response_parser parser,
29 24x std::optional<clock::time_point> deadline)
30 24x : url_(std::move(url))
31 24x , conn_(std::move(conn))
32 24x , parser_(std::move(parser))
33 24x , deadline_(deadline)
34 {
35 24x }
36
37 90x response::response(response&& other) noexcept
38 90x : url_(std::move(other.url_))
39 90x , conn_(std::move(other.conn_))
40 90x , parser_(std::move(other.parser_))
41 90x , deadline_(other.deadline_)
42 {
43 90x }
44
45 response&
46 response::operator=(response&& other) noexcept
47 {
48 if(this != &other)
49 {
50 if(conn_ && detail::can_reuse_conn(parser_))
51 conn_.return_to_pool();
52 url_ = std::move(other.url_);
53 conn_ = std::move(other.conn_);
54 parser_ = std::move(other.parser_);
55 deadline_ = other.deadline_;
56 }
57 return *this;
58 }
59
60 120x response::~response()
61 {
62
6/6
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 96 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 18 times.
✓ Branch 7 taken 102 times.
120x if(conn_ && detail::can_reuse_conn(parser_))
63 18x conn_.return_to_pool();
64 120x }
65
66 capy::io_task<std::string_view>
67
1/1
✓ Branch 1 taken 10 times.
10x response::try_as_view() &
68 {
69 if(parser_.is_complete())
70 co_return { {}, parser_.body() };
71
72 if(deadline_)
73 {
74 auto dur = *deadline_ - clock::now();
75 if(dur <= clock::duration::zero())
76 co_return { capy::error::timeout, {} };
77
78 auto [rec] = co_await capy::timeout(parser_.read(conn_), dur);
79 if(rec)
80 co_return { rec, {} };
81 }
82 else if(auto [rec] = co_await parser_.read(conn_); rec)
83 {
84 co_return { rec, {} };
85 }
86 co_return { {}, parser_.body() };
87 20x }
88
89 capy::task<std::string_view>
90 response::as_view() &
91 {
92 auto [ec, body] = co_await try_as_view();
93
94 if(ec)
95 throw std::system_error(ec);
96
97 co_return std::move(body);
98 }
99
100 capy::any_buffer_source
101 response::as_buffer_source() &
102 {
103 return parser_.source_for(conn_);
104 }
105
106 capy::any_read_source
107 response::as_read_source() &
108 {
109 return as_buffer_source(); // TODO
110 }
111
112 } // namespace burl
113 } // namespace boost
114