Skip to content

Commit 249160a

Browse files
committed
Remove serializer Source API
1 parent fc56858 commit 249160a

File tree

7 files changed

+15
-638
lines changed

7 files changed

+15
-638
lines changed

example/client/burl/main.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -376,26 +376,7 @@ perform_request(
376376

377377
if(!request_opt.input.empty())
378378
{
379-
msg = [&]() -> message
380-
{
381-
if(request_opt.input == "-")
382-
return stdin_body{};
383-
384-
auto path = request_opt.input;
385-
386-
// Append filename to URL if missing
387-
auto segs = url.encoded_segments();
388-
if(segs.empty())
389-
{
390-
segs.push_back(path.filename().string());
391-
}
392-
else if(auto back = --segs.end(); back->empty())
393-
{
394-
segs.replace(back, path.filename().string());
395-
}
396-
397-
return file_body{ path.string() };
398-
}();
379+
throw std::runtime_error{ "File upload is not available" };
399380
}
400381

401382
fs::path output_path = [&]()

example/client/burl/message.cpp

Lines changed: 6 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,10 @@
88
//
99

1010
#include "message.hpp"
11-
#include "mime_type.hpp"
1211

13-
#include <boost/capy/file.hpp>
1412
#include <boost/http/field.hpp>
15-
#include <boost/system/system_error.hpp>
1613

17-
#include <filesystem>
18-
#include <iostream>
19-
20-
namespace capy = boost::capy;
21-
namespace fs = std::filesystem;
22-
using system_error = boost::system::system_error;
14+
namespace capy = boost::capy;
2315

2416
string_body::string_body(std::string body, std::string content_type)
2517
: body_{ std::move(body) }
@@ -53,79 +45,6 @@ string_body::body() const noexcept
5345

5446
// -----------------------------------------------------------------------------
5547

56-
file_body::file_body(std::string path)
57-
: path_{ std::move(path) }
58-
{
59-
}
60-
61-
http::method
62-
file_body::method() const noexcept
63-
{
64-
return http::method::put;
65-
}
66-
67-
core::string_view
68-
file_body::content_type() const noexcept
69-
{
70-
return mime_type(path_);
71-
}
72-
73-
std::uint64_t
74-
file_body::content_length() const
75-
{
76-
return fs::file_size(path_);
77-
}
78-
79-
http::file_source
80-
file_body::body() const
81-
{
82-
boost::capy::file file;
83-
error_code ec;
84-
file.open(path_.c_str(), boost::capy::file_mode::read, ec);
85-
if(ec)
86-
throw system_error{ ec };
87-
88-
return http::file_source{ std::move(file), content_length() };
89-
}
90-
91-
// -----------------------------------------------------------------------------
92-
93-
boost::http::source::results
94-
stdin_body::source::on_read(capy::mutable_buffer mb)
95-
{
96-
std::cin.read(static_cast<char*>(mb.data()), mb.size());
97-
98-
return { .ec = {},
99-
.bytes = static_cast<std::size_t>(std::cin.gcount()),
100-
.finished = std::cin.eof() };
101-
}
102-
103-
http::method
104-
stdin_body::method() const noexcept
105-
{
106-
return http::method::put;
107-
}
108-
109-
core::string_view
110-
stdin_body::content_type() const noexcept
111-
{
112-
return "application/octet-stream";
113-
}
114-
115-
boost::optional<std::size_t>
116-
stdin_body::content_length() const noexcept
117-
{
118-
return boost::none;
119-
}
120-
121-
stdin_body::source
122-
stdin_body::body() const
123-
{
124-
return {};
125-
}
126-
127-
// -----------------------------------------------------------------------------
128-
12948
void
13049
message::set_headers(http::request& request) const
13150
{
@@ -138,20 +57,11 @@ message::set_headers(http::request& request) const
13857
request.set_method(f.method());
13958
request.set(field::content_type, f.content_type());
14059

141-
boost::optional<std::size_t> content_length =
142-
f.content_length();
143-
if(content_length.has_value())
144-
{
145-
request.set_content_length(content_length.value());
146-
if(content_length.value() >= 1024 * 1024 &&
147-
request.version() == http::version::http_1_1)
148-
request.set(field::expect, "100-continue");
149-
}
150-
else
151-
{
152-
request.set_chunked(true);
60+
std::size_t content_length = f.content_length();
61+
request.set_content_length(content_length);
62+
if(content_length >= 1024 * 1024 &&
63+
request.version() == http::version::http_1_1)
15364
request.set(field::expect, "100-continue");
154-
}
15565
}
15666
},
15767
body_);
@@ -167,8 +77,7 @@ message::start_serializer(
16777
{
16878
if constexpr(!std::is_same_v<decltype(f), const std::monostate&>)
16979
{
170-
serializer.start<std::decay_t<decltype(f.body())>>(
171-
request, f.body());
80+
serializer.start(request, f.body());
17281
}
17382
else
17483
{

example/client/burl/message.hpp

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#ifndef BURL_MESSAGE_HPP
1111
#define BURL_MESSAGE_HPP
1212

13-
#include "multipart_form.hpp"
14-
15-
#include <boost/http/file_source.hpp>
1613
#include <boost/http/request.hpp>
1714
#include <boost/http/serializer.hpp>
1815

@@ -41,57 +38,11 @@ class string_body
4138
body() const noexcept;
4239
};
4340

44-
class file_body
45-
{
46-
std::string path_;
47-
48-
public:
49-
file_body(std::string path);
50-
51-
http::method
52-
method() const noexcept;
53-
54-
core::string_view
55-
content_type() const noexcept;
56-
57-
std::uint64_t
58-
content_length() const;
59-
60-
http::file_source
61-
body() const;
62-
};
63-
64-
class stdin_body
65-
{
66-
public:
67-
class source : public http::source
68-
{
69-
public:
70-
results
71-
on_read(capy::mutable_buffer mb) override;
72-
};
73-
74-
http::method
75-
method() const noexcept;
76-
77-
core::string_view
78-
content_type() const noexcept;
79-
80-
boost::optional<std::size_t>
81-
content_length() const noexcept;
82-
83-
source
84-
body() const;
85-
};
86-
8741
class message
8842
{
8943
std::variant<
9044
std::monostate,
91-
string_body,
92-
multipart_form,
93-
file_body,
94-
stdin_body>
45+
string_body>
9546
body_;
9647

9748
public:

0 commit comments

Comments
 (0)