13 #ifndef __ENCODER_HPP__
14 #define __ENCODER_HPP__
56 virtual void backup(
size_t length) = 0;
66 : data(_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"
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;
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)
261 *length = size -
temp;
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__
virtual ~Encoder()
Definition: encoder.hpp:52
FileEncoder(int_fd _fd, size_t _size)
Definition: encoder.hpp:236
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...
virtual void backup(size_t length)
Definition: encoder.hpp:83
bool acceptsEncoding(const std::string &encoding) const
Returns whether the encoding is considered acceptable in the response.
std::string status
Definition: http.hpp:621
Definition: message.hpp:22
virtual Kind kind() const
Definition: encoder.hpp:70
DataEncoder(const std::string &_data)
Definition: encoder.hpp:65
virtual Kind kind() const =0
virtual int_fd next(off_t *offset, size_t *length)
Definition: encoder.hpp:256
Definition: result.hpp:40
Kind
Definition: encoder.hpp:44
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
UPID from
Definition: message.hpp:25
virtual ~DataEncoder()
Definition: encoder.hpp:68
#define CHECK_SOME(expression)
Definition: check.hpp:44
std::string body
Definition: http.hpp:654
Try< Nothing > close(int fd)
Definition: close.hpp:24
virtual void backup(size_t length)
Definition: encoder.hpp:265
Option< T > max(const Option< T > &left, const Option< T > &right)
Definition: option.hpp:199
virtual Kind kind() const
Definition: encoder.hpp:251
Definition: encoder.hpp:41
virtual const char * next(size_t *length)
Definition: encoder.hpp:75
#define foreachpair(KEY, VALUE, ELEMS)
Definition: foreach.hpp:51
virtual size_t remaining() const
Definition: encoder.hpp:90
const T & get() const
Definition: result.hpp:115
static Try error(const E &e)
Definition: try.hpp:42
Definition: encoder.hpp:46
virtual size_t remaining() const =0
Definition: encoder.hpp:233
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
bool isError() const
Definition: try.hpp:71
static std::string encode(const Message &message)
Definition: encoder.hpp:107
HttpResponseEncoder(const http::Response &response, const http::Request &request)
Definition: encoder.hpp:146
Definition: encoder.hpp:62
virtual size_t remaining() const
Definition: encoder.hpp:272
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:111
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:623
enum process::http::Response::@4 type
std::string name
Definition: message.hpp:24
const T & get() const
Definition: try.hpp:73
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:47
tm * gmtime_r(const time_t *timep, tm *result)
Definition: os.hpp:389
virtual ~FileEncoder()
Definition: encoder.hpp:246