src/urlencoded_form.cpp

100.0% Lines (27/27) 100.0% List of functions (7/7) 100.0% Branches (11/11)
urlencoded_form.cpp
f(x) Functions (7)
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/urlencoded_form.hpp>
11
12 #include <boost/capy/buffers/make_buffer.hpp>
13 #include <boost/capy/io/any_buffer_sink.hpp>
14 #include <boost/url/encode.hpp>
15 #include <boost/url/encoding_opts.hpp>
16 #include <boost/url/rfc/unreserved_chars.hpp>
17
18 #include <cstdint>
19 #include <optional>
20 #include <string>
21 #include <string_view>
22 #include <utility>
23
24 namespace boost
25 {
26 namespace burl
27 {
28
29 5x urlencoded_form::urlencoded_form(
30 5x std::initializer_list<std::pair<std::string_view, std::string_view>> fields)
31 {
32
2/2
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 5 times.
12x for(auto const& [name, value] : fields)
33
1/1
✓ Branch 1 taken 7 times.
7x append(name, value);
34 5x }
35
36 urlencoded_form&
37 17x urlencoded_form::append(std::string_view name, std::string_view value)
38 {
39 17x urls::encoding_opts opt;
40 17x opt.space_as_plus = true;
41
42
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 9 times.
17x if(!body_.empty())
43
1/1
✓ Branch 1 taken 8 times.
8x body_ += '&';
44
1/1
✓ Branch 2 taken 17 times.
17x urls::encode(
45 name,
46 urls::unreserved_chars,
47 opt,
48 34x urls::string_token::append_to(body_));
49
1/1
✓ Branch 1 taken 17 times.
17x body_ += '=';
50
1/1
✓ Branch 2 taken 17 times.
17x urls::encode(
51 value,
52 urls::unreserved_chars,
53 opt,
54 34x urls::string_token::append_to(body_));
55 17x return *this;
56 }
57
58 class urlencoded_form::body
59 {
60 std::string body_;
61
62 public:
63 10x explicit body(std::string body)
64 10x : body_(std::move(body))
65 {
66 10x }
67
68 std::optional<std::string>
69 10x content_type() const
70 {
71 10x return "application/x-www-form-urlencoded";
72 }
73
74 std::optional<std::uint64_t>
75 10x content_length() const noexcept
76 {
77 10x return body_.size();
78 }
79
80 capy::io_task<>
81
1/1
✓ Branch 1 taken 56 times.
56x write(capy::any_buffer_sink& sink) const
82 {
83 auto [ec, n] =
84 co_await sink.write(capy::make_buffer(std::string_view(body_)));
85 if(ec)
86 co_return { ec };
87 co_return {};
88 112x }
89 };
90
91 any_request_body
92 10x tag_invoke(body_from_tag<urlencoded_form>, urlencoded_form form)
93 {
94
1/1
✓ Branch 4 taken 10 times.
10x return urlencoded_form::body{ std::move(form.body_) };
95 }
96
97 } // namespace burl
98 } // namespace boost
99