forked from cppalliance/beast2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.cpp
More file actions
88 lines (76 loc) · 2.03 KB
/
message.cpp
File metadata and controls
88 lines (76 loc) · 2.03 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
//
// Copyright (c) 2024 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/beast2
//
#include "message.hpp"
#include <boost/http/field.hpp>
namespace capy = boost::capy;
string_body::string_body(std::string body, std::string content_type)
: body_{ std::move(body) }
, content_type_{ std::move(content_type) }
{
}
http::method
string_body::method() const noexcept
{
return http::method::post;
}
core::string_view
string_body::content_type() const noexcept
{
return content_type_;
}
std::size_t
string_body::content_length() const noexcept
{
return body_.size();
}
capy::const_buffer
string_body::body() const noexcept
{
return { body_.data(), body_.size() };
}
// -----------------------------------------------------------------------------
void
message::set_headers(http::request& request) const
{
std::visit(
[&](auto& f)
{
using field = http::field;
if constexpr(!std::is_same_v<decltype(f), const std::monostate&>)
{
request.set_method(f.method());
request.set(field::content_type, f.content_type());
std::size_t content_length = f.content_length();
request.set_content_length(content_length);
if(content_length >= 1024 * 1024 &&
request.version() == http::version::http_1_1)
request.set(field::expect, "100-continue");
}
},
body_);
}
void
message::start_serializer(
http::serializer& serializer,
http::request& request) const
{
std::visit(
[&](auto& f)
{
if constexpr(!std::is_same_v<decltype(f), const std::monostate&>)
{
serializer.start(request, f.body());
}
else
{
serializer.start(request);
}
},
body_);
}