-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathresponse.cpp
More file actions
113 lines (95 loc) · 2.36 KB
/
Copy pathresponse.cpp
File metadata and controls
113 lines (95 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// Copyright (c) 2026 Mohammad Nejati
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/burl
//
#include <boost/burl/response.hpp>
#include "detail/can_reuse_conn.hpp"
#include <boost/capy/error.hpp>
#include <boost/capy/timeout.hpp>
#include <chrono>
#include <utility>
namespace boost
{
namespace burl
{
response::response(
urls::url url,
detail::pooled_connection conn,
http::response_parser parser,
std::optional<clock::time_point> deadline)
: url_(std::move(url))
, conn_(std::move(conn))
, parser_(std::move(parser))
, deadline_(deadline)
{
}
response::response(response&& other) noexcept
: url_(std::move(other.url_))
, conn_(std::move(other.conn_))
, parser_(std::move(other.parser_))
, deadline_(other.deadline_)
{
}
response&
response::operator=(response&& other) noexcept
{
if(this != &other)
{
if(conn_ && detail::can_reuse_conn(parser_))
conn_.return_to_pool();
url_ = std::move(other.url_);
conn_ = std::move(other.conn_);
parser_ = std::move(other.parser_);
deadline_ = other.deadline_;
}
return *this;
}
response::~response()
{
if(conn_ && detail::can_reuse_conn(parser_))
conn_.return_to_pool();
}
capy::io_task<std::string_view>
response::try_as_view() &
{
if(parser_.is_complete())
co_return { {}, parser_.body() };
if(deadline_)
{
auto dur = *deadline_ - clock::now();
if(dur <= clock::duration::zero())
co_return { capy::error::timeout, {} };
auto [rec] = co_await capy::timeout(parser_.read(conn_), dur);
if(rec)
co_return { rec, {} };
}
else if(auto [rec] = co_await parser_.read(conn_); rec)
{
co_return { rec, {} };
}
co_return { {}, parser_.body() };
}
capy::task<std::string_view>
response::as_view() &
{
auto [ec, body] = co_await try_as_view();
if(ec)
throw std::system_error(ec);
co_return std::move(body);
}
capy::any_buffer_source
response::as_buffer_source() &
{
return parser_.source_for(conn_);
}
capy::any_read_source
response::as_read_source() &
{
return as_buffer_source(); // TODO
}
} // namespace burl
} // namespace boost