-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfile_body.hpp
More file actions
300 lines (257 loc) · 8.12 KB
/
file_body.hpp
File metadata and controls
300 lines (257 loc) · 8.12 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
#ifndef BEAST_EXAMPLE_COMMON_FILE_BODY_HPP
#define BEAST_EXAMPLE_COMMON_FILE_BODY_HPP
#include <boost/beast/core/error.hpp>
#include <boost/beast/http/message.hpp>
#include <boost/filesystem.hpp>
#include <boost/assert.hpp>
#include <boost/optional.hpp>
#include <algorithm>
#include <cstdio>
#include <cstdint>
#include <utility>
namespace beast = boost::beast;
//[example_http_FileBody_1
/** A message body represented by a file on the filesystem.
Messages with this type have bodies represented by a
file on the file system. When parsing a message using
this body type, the data is stored in the file pointed
to by the path, which must be writable. When serializing,
the implementation will read the file and present those
octets as the body content. This may be used to serve
content from a directory as part of a web service.
*/
struct FileBody
{
/** The type of the @ref message::body member.
Messages declared using `FileBody` will have this
type for the body member. We use a path indicating
the location on the file system for which the data
will be read or written.
*/
using value_type = boost::filesystem::path;
/** Returns the content length of the body in a message.
This optional static function returns the size of the
body in bytes. It is called from @ref message::size to
return the payload size, and from @ref message::prepare
to automatically set the Content-Length field. If this
function is omitted from a body type, calls to
@ref message::prepare will set the chunked transfer
encoding.
@param m The message containing a file body to check.
@return The size of the file in bytes.
*/
static
std::uint64_t
size(value_type const& v);
/** Algorithm for retrieving buffers when serializing.
Objects of this type are created during serialization
to extract the buffers representing the body.
*/
class reader
{
std::streamsize remain_; // The number of unread bytes
const value_type& path_;
std::ifstream stream_;
char buf_[4096];
public:
// The type of buffer sequence returned by `get`.
//
using const_buffers_type =
boost::asio::const_buffer;
// Constructor.
//
// `m` holds the message we are sending, which will
// always have the `FileBody` as the body type.
//
template<bool isRequest, class Fields>
reader(beast::http::message<isRequest, FileBody, Fields> const& m,
beast::error_code& ec)
: path_(m.body)
{
stream_.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
stream_.open(path_.string(), std::ifstream::in | std::ifstream::binary);
remain_ = boost::filesystem::file_size(path_, ec);
}
catch (std::system_error& e)
{
ec = boost::system::error_code(e.code().value(), boost::system::system_category());
}
}
// This function is called zero or more times to
// retrieve buffers. A return value of `boost::none`
// means there are no more buffers. Otherwise,
// the contained pair will have the next buffer
// to serialize, and a `bool` indicating whether
// or not there may be additional buffers.
boost::optional<std::pair<const_buffers_type, bool>>
get(beast::error_code& ec)
{
const std::streamsize amount = std::min(remain_, static_cast<std::streamsize>(sizeof(buf_)));
// Check for an empty file
if (amount == 0)
{
ec = {};
return boost::none;
}
try
{
stream_.read(buf_, amount);
assert(stream_.gcount() == amount);
}
catch (std::system_error& e)
{
ec = boost::system::error_code(e.code().value(), boost::system::system_category());
return boost::none;
}
// Update the amount remaining based on what we got
remain_ -= amount;
// Return the buffer to the caller.
//
// The second element of the pair indicates whether or
// not there is more data. As long as there is some
// unread bytes, there will be more data. Otherwise,
// we set this bool to `false` so we will not be called
// again.
//
ec = {};
return {{
const_buffers_type{buf_, static_cast<std::size_t>(amount)}, // buffer to return.
remain_ > 0 // `true` if there are more buffers.
}};
}
};
/** Algorithm for storing buffers when parsing.
Objects of this type are created during parsing
to store incoming buffers representing the body.
*/
class writer;
};
//]
//[example_http_FileBody_2
inline
std::uint64_t
FileBody::
size(value_type const& v)
{
return boost::filesystem::file_size(v);
}
class FileBody::writer
{
value_type const& path_; // A path to the file
FILE* file_ = nullptr; // The file handle
public:
// Constructor.
//
// This is called after the header is parsed and
// indicates that a non-zero sized body may be present.
// `m` holds the message we are receiving, which will
// always have the `FileBody` as the body type.
//
template<bool isRequest, class Fields>
explicit
writer(beast::http::message<isRequest, FileBody, Fields>& m,
boost::optional<std::uint64_t> const& content_length,
beast::error_code& ec);
// This function is called one or more times to store
// buffer sequences corresponding to the incoming body.
//
template<class ConstBufferSequence>
void
put(ConstBufferSequence const& buffers, beast::error_code& ec);
// This function is called when writing is complete.
// It is an opportunity to perform any final actions
// which might fail, in order to return an error code.
// Operations that might fail should not be attemped in
// destructors, since an exception thrown from there
// would terminate the program.
//
void
finish(beast::error_code& ec);
// Destructor.
//
// Avoid calling anything that might fail here.
//
~writer();
};
//]
//[example_http_FileBody_6
// Just stash a reference to the path so we can open the file later.
template<bool isRequest, class Fields>
FileBody::writer::
writer(beast::http::message<isRequest, FileBody, Fields>& m,
boost::optional<std::uint64_t> const& content_length,
beast::error_code& ec)
: path_(m.body)
{
boost::ignore_unused(content_length);
// Attempt to open the file for writing
file_ = fopen(path_.string().c_str(), "wb");
if(! file_)
{
// Convert the old-school `errno` into
// an error code using the generic category.
ec = beast::error_code{errno, beast::generic_category()};
return;
}
// This is required by the error_code specification
ec = {};
}
// This will get called one or more times with body buffers
//
template<class ConstBufferSequence>
void
FileBody::writer::
put(ConstBufferSequence const& buffers, beast::error_code& ec)
{
// Loop over all the buffers in the sequence,
// and write each one to the file.
for(boost::asio::const_buffer buffer : buffers)
{
// Write this buffer to the file
fwrite(
boost::asio::buffer_cast<void const*>(buffer), 1,
boost::asio::buffer_size(buffer),
file_);
// Handle any errors
if(ferror(file_))
{
// Convert the old-school `errno` into
// an error code using the generic category.
ec = beast::error_code{errno, beast::generic_category()};
return;
}
}
// Indicate success
ec = {};
}
// Called after writing is done when there's no error.
inline
void
FileBody::writer::
finish(beast::error_code& ec)
{
// This has to be cleared before returning, to
// indicate no error. The specification requires it.
ec = {};
}
// The destructor is always invoked if construction succeeds
//
inline
FileBody::writer::
~writer()
{
// Just close the file if its open
if(file_)
fclose(file_);
// In theory fclose() can fail but how would we handle it?
}
//]
#endif