proxygen
proxygen::ResponseBuilder Class Reference

#include <ResponseBuilder.h>

Public Types

enum  UpgradeType { UpgradeType::CONNECT_REQUEST = 0, UpgradeType::HTTP_UPGRADE }
 

Public Member Functions

 ResponseBuilder (ResponseHandler *txn)
 
ResponseBuilderpromise (const std::string &url, const std::string &host)
 
ResponseBuilderstatus (uint16_t code, const std::string &message)
 
template<typename T >
ResponseBuilderheader (const std::string &headerIn, const T &value)
 
template<typename T >
ResponseBuilderheader (HTTPHeaderCode code, const T &value)
 
ResponseBuilderbody (std::unique_ptr< folly::IOBuf > bodyIn)
 
template<typename T >
ResponseBuilderbody (T &&t)
 
ResponseBuildercloseConnection ()
 
ResponseBuildertrailers (const HTTPHeaders &trailers)
 
void sendWithEOM ()
 
void send ()
 
void acceptUpgradeRequest (UpgradeType upgradeType, const std::string upgradeProtocol="")
 
void rejectUpgradeRequest ()
 
ResponseBuildersetEgressWebsocketHeaders ()
 

Private Attributes

ResponseHandler *const txn_ {nullptr}
 
std::unique_ptr< HTTPMessageheaders_
 
std::unique_ptr< folly::IOBufbody_
 
std::unique_ptr< HTTPHeaderstrailers_
 
bool sendEOM_ {false}
 

Detailed Description

Helps you make responses and send them on demand.

NOTE: We don't do any correctness checks here, we depend on state machine in HTTPTransaction to tell us when an error occurs

Three expected use cases are

  1. Send all response at once. If this is an error response, most probably you also want 'closeConnection'.

ResponseBuilder(handler) .status(200, "OK") .body(...) .sendWithEOM();

  1. Sending back response in chunks.

ResponseBuilder(handler) .status(200, "OK") .body(...) .send(); // Without WithEOM we make it chunked

1 or more time

ResponseBuilder(handler) .body(...) .send();

At last

ResponseBuilder(handler) .body(...) .sendWithEOM();

  1. Accept or reject Upgrade Requests

ResponseBuilder(handler) .acceptUpgradeRequest() // send '200 OK' without EOM

or

ResponseBuilder(handler) .rejectUpgradeRequest() // send '400 Bad Request'

Definition at line 64 of file ResponseBuilder.h.

Member Enumeration Documentation

Enumerator
CONNECT_REQUEST 
HTTP_UPGRADE 

Definition at line 181 of file ResponseBuilder.h.

181  {
182  CONNECT_REQUEST = 0,
183  HTTP_UPGRADE,
184  };
#define CONNECT_REQUEST

Constructor & Destructor Documentation

proxygen::ResponseBuilder::ResponseBuilder ( ResponseHandler txn)
inlineexplicit

Definition at line 66 of file ResponseBuilder.h.

66  : txn_(txn) {
67  }
ResponseHandler *const txn_

Member Function Documentation

void proxygen::ResponseBuilder::acceptUpgradeRequest ( UpgradeType  upgradeType,
const std::string  upgradeProtocol = "" 
)
inline

Definition at line 186 of file ResponseBuilder.h.

References CONNECT_REQUEST, headers_, proxygen::HTTP_HEADER_CONNECTION, proxygen::HTTP_HEADER_UPGRADE, proxygen::ResponseHandler::sendHeaders(), and txn_.

187  {
188  headers_ = std::make_unique<HTTPMessage>();
189  if (upgradeType == UpgradeType::CONNECT_REQUEST) {
190  headers_->constructDirectResponse({1, 1}, 200, "OK");
191  } else {
192  CHECK(!upgradeProtocol.empty());
193  headers_->constructDirectResponse({1, 1}, 101, "Switching Protocols");
194  headers_->getHeaders().add(HTTP_HEADER_UPGRADE, upgradeProtocol);
195  headers_->getHeaders().add(HTTP_HEADER_CONNECTION, "Upgrade");
196  }
198  }
ResponseHandler *const txn_
virtual void sendHeaders(HTTPMessage &msg) noexcept=0
std::unique_ptr< HTTPMessage > headers_
ResponseBuilder& proxygen::ResponseBuilder::body ( std::unique_ptr< folly::IOBuf bodyIn)
inline
template<typename T >
ResponseBuilder& proxygen::ResponseBuilder::body ( T &&  t)
inline

Definition at line 112 of file ResponseBuilder.h.

References body(), folly::IOBuf::maybeCopyBuffer(), and folly::pushmi::detail::t.

112  {
114  folly::to<std::string>(std::forward<T>(t))));
115  }
ResponseBuilder & body(std::unique_ptr< folly::IOBuf > bodyIn)
static std::unique_ptr< IOBuf > maybeCopyBuffer(const std::string &buf, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1609
ResponseBuilder& proxygen::ResponseBuilder::closeConnection ( )
inline

Definition at line 117 of file ResponseBuilder.h.

References header(), and proxygen::HTTP_HEADER_CONNECTION.

Referenced by proxygen::RequestHandlerAdaptor::onError(), and proxygen::RequestHandlerAdaptor::onHeadersComplete().

117  {
118  return header(HTTP_HEADER_CONNECTION, "close");
119  }
ResponseBuilder & header(const std::string &headerIn, const T &value)
template<typename T >
ResponseBuilder& proxygen::ResponseBuilder::header ( const std::string headerIn,
const T value 
)
inline

Definition at line 86 of file ResponseBuilder.h.

References headers_.

Referenced by closeConnection(), ZlibServerFilterTest::exercise_compression(), PushService::PushRequestHandler::onEOM(), EchoService::EchoHandler::onEOM(), TestHandlerFactory::TestHandler::onEOM(), and TEST_F().

86  {
87  CHECK(headers_) << "You need to call `status` before adding headers";
88  headers_->getHeaders().add(headerIn, value);
89  return *this;
90  }
static const char *const value
Definition: Conv.cpp:50
std::unique_ptr< HTTPMessage > headers_
template<typename T >
ResponseBuilder& proxygen::ResponseBuilder::header ( HTTPHeaderCode  code,
const T value 
)
inline

Definition at line 93 of file ResponseBuilder.h.

References headers_.

93  {
94  CHECK(headers_) << "You need to call `status` before adding headers";
95  headers_->getHeaders().add(code, value);
96  return *this;
97  }
static const char *const value
Definition: Conv.cpp:50
std::unique_ptr< HTTPMessage > headers_
ResponseBuilder& proxygen::ResponseBuilder::promise ( const std::string url,
const std::string host 
)
inline

Definition at line 69 of file ResponseBuilder.h.

References headers_, and proxygen::HTTP_HEADER_HOST.

Referenced by PushService::PushRequestHandler::onRequest().

69  {
70  headers_ = std::make_unique<HTTPMessage>();
71  headers_->setHTTPVersion(1, 1);
72  headers_->setURL(url);
73  headers_->getHeaders().set(HTTP_HEADER_HOST, host);
74  return *this;
75  }
std::unique_ptr< HTTPMessage > headers_
void proxygen::ResponseBuilder::rejectUpgradeRequest ( )
inline

Definition at line 200 of file ResponseBuilder.h.

References headers_, proxygen::ResponseHandler::sendEOM(), proxygen::ResponseHandler::sendHeaders(), and txn_.

Referenced by proxygen::RejectConnectFilter::onRequest().

200  {
201  headers_ = std::make_unique<HTTPMessage>();
202  headers_->constructDirectResponse({1, 1}, 400, "Bad Request");
204  txn_->sendEOM();
205  }
virtual void sendEOM() noexcept=0
ResponseHandler *const txn_
virtual void sendHeaders(HTTPMessage &msg) noexcept=0
std::unique_ptr< HTTPMessage > headers_
void proxygen::ResponseBuilder::send ( )
inline

Definition at line 131 of file ResponseBuilder.h.

References body_, folly::gen::chunked(), proxygen::ResponseHandler::getTransaction(), headers_, proxygen::HTTP_HEADER_CONTENT_LENGTH, folly::gen::move, SCOPE_EXIT, proxygen::ResponseHandler::sendBody(), proxygen::ResponseHandler::sendChunkHeader(), proxygen::ResponseHandler::sendChunkTerminator(), proxygen::ResponseHandler::sendEOM(), sendEOM_, proxygen::ResponseHandler::sendHeaders(), proxygen::HTTPTransaction::sendTrailers(), trailers_, and txn_.

Referenced by ProxyService::ProxyHandler::connectSuccess(), ZlibServerFilterTest::exercise_compression(), proxygen::RequestHandlerAdaptor::onHeadersComplete(), StaticService::StaticHandler::onRequest(), StaticService::StaticHandler::readFile(), sendWithEOM(), and TEST_F().

131  {
132  // Once we send them, we don't want to send them again
133  SCOPE_EXIT { headers_.reset(); };
134 
135  // By default, chunked
136  bool chunked = true;
137 
138  // If we have complete response, we can use Content-Length and get done
139  if (headers_ && sendEOM_) {
140  chunked = false;
141  }
142 
143  if (headers_) {
144  // We don't need to add Content-Length or Encoding for 1xx responses
145  if (headers_->isResponse() && headers_->getStatusCode() >= 200) {
146  if (chunked) {
147  headers_->setIsChunked(true);
148  } else {
149  const auto len = body_ ? body_->computeChainDataLength() : 0;
150  headers_->getHeaders().add(
152  folly::to<std::string>(len));
153  }
154  }
155 
157  }
158 
159  if (body_) {
160  if (chunked) {
161  txn_->sendChunkHeader(body_->computeChainDataLength());
164  } else {
166  }
167  }
168 
169  if (sendEOM_) {
170  if (trailers_) {
171  auto txn = txn_->getTransaction();
172  if (txn) {
173  txn->sendTrailers(*trailers_);
174  }
175  trailers_.reset();
176  }
177  txn_->sendEOM();
178  }
179  }
virtual void sendChunkTerminator() noexcept=0
virtual void sendEOM() noexcept=0
virtual void sendTrailers(const HTTPHeaders &trailers)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
HTTPTransaction * getTransaction() const noexcept
std::unique_ptr< folly::IOBuf > body_
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
virtual void sendChunkHeader(size_t len) noexcept=0
ResponseHandler *const txn_
virtual void sendHeaders(HTTPMessage &msg) noexcept=0
virtual void sendBody(std::unique_ptr< folly::IOBuf > body) noexcept=0
std::unique_ptr< HTTPMessage > headers_
std::unique_ptr< HTTPHeaders > trailers_
Chunked chunked(const Container &container, int chunkSize=256)
Definition: Parallel.h:49
ResponseBuilder& proxygen::ResponseBuilder::setEgressWebsocketHeaders ( )
inline

Definition at line 207 of file ResponseBuilder.h.

References headers_.

207  {
208  headers_->setEgressWebsocketUpgrade();
209  return *this;
210  }
std::unique_ptr< HTTPMessage > headers_
ResponseBuilder& proxygen::ResponseBuilder::trailers ( const HTTPHeaders trailers)
inline

Definition at line 121 of file ResponseBuilder.h.

References trailers_.

121  {
122  trailers_.reset(new HTTPHeaders(trailers));
123  return *this;
124  }
ResponseBuilder & trailers(const HTTPHeaders &trailers)
std::unique_ptr< HTTPHeaders > trailers_

Member Data Documentation

std::unique_ptr<folly::IOBuf> proxygen::ResponseBuilder::body_
private

Definition at line 216 of file ResponseBuilder.h.

Referenced by body(), and send().

std::unique_ptr<HTTPMessage> proxygen::ResponseBuilder::headers_
private
bool proxygen::ResponseBuilder::sendEOM_ {false}
private

Definition at line 220 of file ResponseBuilder.h.

Referenced by send(), and sendWithEOM().

std::unique_ptr<HTTPHeaders> proxygen::ResponseBuilder::trailers_
private

Definition at line 217 of file ResponseBuilder.h.

Referenced by send(), and trailers().

ResponseHandler* const proxygen::ResponseBuilder::txn_ {nullptr}
private

Definition at line 213 of file ResponseBuilder.h.

Referenced by acceptUpgradeRequest(), rejectUpgradeRequest(), and send().


The documentation for this class was generated from the following file: