13 #ifndef __ENCODER_HPP__ 14 #define __ENCODER_HPP__ 53 virtual void backup(
size_t length) = 0;
63 : data(_data),
index(0) {}
66 : data(
std::move(_data)),
index(0) {}
75 virtual const char*
next(
size_t* length)
79 *length = data.size() -
temp;
80 return data.data() +
temp;
85 if (
index >= length) {
92 return data.size() -
index;
96 const std::string data;
109 std::ostringstream out;
117 if (message.
to.
id !=
"") {
118 out <<
"/" << message.
to.
id;
121 out <<
"/" << message.
name <<
" HTTP/1.1\r\n" 122 <<
"User-Agent: libprocess/" << message.
from <<
"\r\n" 123 <<
"Libprocess-From: " << message.
from <<
"\r\n" 124 <<
"Connection: Keep-Alive\r\n" 125 <<
"Host: " << message.
to.
address <<
"\r\n";
127 if (message.
body.size() > 0) {
128 out <<
"Transfer-Encoding: chunked\r\n\r\n" 129 << std::hex << message.
body.size() <<
"\r\n";
130 out.write(message.
body.data(), message.
body.size());
155 std::ostringstream out;
159 out <<
"HTTP/1.1 " << response.
status <<
"\r\n";
161 auto headers = response.
headers;
173 <<
"Failed to convert the current time to a tm struct " 174 <<
"using os::gmtime_r()";
177 strftime(date, 256,
"%a, %d %b %Y %H:%M:%S GMT", &tm_);
179 headers[
"Date"] = date;
182 std::string body = response.
body;
185 response.
body.length() >= GZIP_MINIMUM_BODY_LENGTH &&
186 !headers.contains(
"Content-Encoding") &&
190 LOG(WARNING) <<
"Failed to gzip response body: " << compressed.
error();
192 body = std::move(compressed.
get());
194 headers[
"Content-Length"] =
stringify(body.length());
195 headers[
"Content-Encoding"] =
"gzip";
199 foreachpair (
const std::string& key,
const std::string& value, headers) {
200 out << key <<
": " << value <<
"\r\n";
206 !headers.contains(
"Content-Length")) {
207 out <<
"Content-Length: 0\r\n";
209 !headers.contains(
"Content-Length")) {
210 out <<
"Content-Length: " << body.size() <<
"\r\n";
221 if (length.
isSome() && length.
get() <= body.length()) {
222 out.write(body.data(), length.
get());
224 out.write(body.data(), body.size());
237 : fd(_fd),
size(static_cast<off_t>(_size)),
index(0)
267 if (
index >= static_cast<off_t>(length)) {
268 index -=
static_cast<off_t
>(length);
274 return static_cast<size_t>(
size -
index);
285 #endif // __ENCODER_HPP__ size_t remaining() const override
Definition: encoder.hpp:272
virtual ~Encoder()
Definition: encoder.hpp:49
FileEncoder(int_fd _fd, size_t _size)
Definition: encoder.hpp:236
DataEncoder(std::string &&_data)
Definition: encoder.hpp:65
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
Kind kind() const override
Definition: encoder.hpp:251
Future< Response > request(const Request &request, bool streamedResponse=false)
Asynchronously sends an HTTP request to the process and returns the HTTP response once the entire res...
T & get()&
Definition: try.hpp:80
bool acceptsEncoding(const std::string &encoding) const
Returns whether the encoding is considered acceptable in the response.
std::string status
Definition: http.hpp:637
network::inet::Address address
Definition: pid.hpp:177
Definition: message.hpp:22
DataEncoder(const std::string &_data)
Definition: encoder.hpp:62
~DataEncoder() override
Definition: encoder.hpp:68
virtual Kind kind() const =0
virtual int_fd next(off_t *offset, size_t *length)
Definition: encoder.hpp:256
Definition: type_utils.hpp:619
void backup(size_t length) override
Definition: encoder.hpp:83
Kind
Definition: encoder.hpp:41
Try< std::string > compress(const std::string &decompressed, int level=Z_DEFAULT_COMPRESSION)
Definition: gzip.hpp:179
static std::string encode(const http::Response &response, const http::Request &request)
Definition: encoder.hpp:151
Definition: encoder.hpp:143
virtual void backup(size_t length)=0
UPID to
Definition: message.hpp:26
Try< Nothing > encode(const Netlink< struct rtnl_cls > &cls, const Classifier &classifier)
Result< int > index(const std::string &link)
UPID from
Definition: message.hpp:25
#define CHECK_SOME(expression)
Definition: check.hpp:50
std::string body
Definition: http.hpp:670
Try< Nothing > close(int fd)
Definition: close.hpp:24
Option< T > max(const Option< T > &left, const Option< T > &right)
Definition: option.hpp:214
Definition: encoder.hpp:38
virtual const char * next(size_t *length)
Definition: encoder.hpp:75
void backup(size_t length) override
Definition: encoder.hpp:265
size_t remaining() const override
Definition: encoder.hpp:90
#define foreachpair(KEY, VALUE, ELEMS)
Definition: foreach.hpp:51
~FileEncoder() override
Definition: encoder.hpp:246
static Try error(const E &e)
Definition: try.hpp:43
Definition: encoder.hpp:43
virtual size_t remaining() const =0
Definition: encoder.hpp:233
bool isError() const
Definition: try.hpp:78
static std::string encode(const Message &message)
Definition: encoder.hpp:107
T & get()&
Definition: result.hpp:116
Definition: executor.hpp:48
HttpResponseEncoder(const http::Response &response, const http::Request &request)
Definition: encoder.hpp:146
Definition: encoder.hpp:59
std::string body
Definition: message.hpp:27
MessageEncoder(const Message &message)
Definition: encoder.hpp:104
std::string temp()
Definition: temp.hpp:27
bool isSome() const
Definition: result.hpp:112
Definition: encoder.hpp:101
struct process::UPID::ID id
int int_fd
Definition: int_fd.hpp:35
std::string stringify(int flags)
Headers headers
Definition: http.hpp:639
enum process::http::Response::@4 type
std::string name
Definition: message.hpp:24
const uint32_t GZIP_MINIMUM_BODY_LENGTH
Definition: encoder.hpp:35
Try< std::vector< Value > > time(const std::string &hierarchy, const std::string &cgroup)
Definition: encoder.hpp:44
tm * gmtime_r(const time_t *timep, tm *result)
Definition: os.hpp:431
Kind kind() const override
Definition: encoder.hpp:70