proxygen
proxygen::RequestHandlerAdaptor Class Reference

#include <RequestHandlerAdaptor.h>

Inheritance diagram for proxygen::RequestHandlerAdaptor:
proxygen::HTTPTransactionHandler proxygen::ResponseHandler StubRequestHandlerAdaptor

Public Member Functions

 RequestHandlerAdaptor (RequestHandler *requestHandler)
 
- Public Member Functions inherited from proxygen::HTTPTransactionHandler
virtual void onPushedTransaction (HTTPTransaction *) noexcept
 
virtual void onGoaway (ErrorCode) noexcept
 
virtual ~HTTPTransactionHandler ()
 
- Public Member Functions inherited from proxygen::ResponseHandler
 ResponseHandler (RequestHandler *upstream)
 
virtual ~ResponseHandler ()
 
HTTPTransactiongetTransaction () const noexcept
 

Private Member Functions

void setTransaction (HTTPTransaction *txn) noexceptoverride
 
void detachTransaction () noexceptoverride
 
void onHeadersComplete (std::unique_ptr< HTTPMessage > msg) noexceptoverride
 
void onBody (std::unique_ptr< folly::IOBuf > chain) noexceptoverride
 
void onChunkHeader (size_t length) noexceptoverride
 
void onChunkComplete () noexceptoverride
 
void onTrailers (std::unique_ptr< HTTPHeaders > trailers) noexceptoverride
 
void onEOM () noexceptoverride
 
void onUpgrade (UpgradeProtocol protocol) noexceptoverride
 
void onError (const HTTPException &error) noexceptoverride
 
void onEgressPaused () noexceptoverride
 
void onEgressResumed () noexceptoverride
 
void onExTransaction (HTTPTransaction *txn) noexceptoverride
 
void sendHeaders (HTTPMessage &msg) noexceptoverride
 
void sendChunkHeader (size_t len) noexceptoverride
 
void sendBody (std::unique_ptr< folly::IOBuf > body) noexceptoverride
 
void sendChunkTerminator () noexceptoverride
 
void sendEOM () noexceptoverride
 
void sendAbort () noexceptoverride
 
void refreshTimeout () noexceptoverride
 
void pauseIngress () noexceptoverride
 
void resumeIngress () noexceptoverride
 
ResponseHandlernewPushedResponse (PushHandler *pushHandler) noexceptoverride
 
ResponseHandlernewExMessage (ExMessageHandler *exHandler, bool unidirectional) noexceptoverride
 
const wangle::TransportInfogetSetupTransportInfo () const noexceptoverride
 
void getCurrentTransportInfo (wangle::TransportInfo *tinfo) const override
 
void setError (ProxygenError err) noexcept
 

Private Attributes

ProxygenError err_ {kErrorNone}
 

Additional Inherited Members

- Protected Attributes inherited from proxygen::ResponseHandler
RequestHandlerupstream_ {nullptr}
 
HTTPTransactiontxn_ {nullptr}
 

Detailed Description

An adaptor that converts HTTPTransactionHandler to RequestHandler. Apart from that it also -

  • Handles all the error cases itself as described below. It makes a terminal call onError(...) where you are expected to log something and stop processing the request if you have started so.

    onError - Send a direct response back if no response has started and writing is still possible. Otherwise sends an abort.

  • Handles the 100-continue case for you (by sending Continue response), if RequestHandler returns false for canHandleExpect(). Otherwise, RequestHandler is responsible for handling it.

Definition at line 35 of file RequestHandlerAdaptor.h.

Constructor & Destructor Documentation

proxygen::RequestHandlerAdaptor::RequestHandlerAdaptor ( RequestHandler requestHandler)
explicit

Definition at line 20 of file RequestHandlerAdaptor.cpp.

Referenced by newExMessage(), newPushedResponse(), and onExTransaction().

21  : ResponseHandler(requestHandler) {
22 }
ResponseHandler(RequestHandler *upstream)

Member Function Documentation

void proxygen::RequestHandlerAdaptor::detachTransaction ( )
overrideprivatevirtualnoexcept

Called once after a transaction successfully completes. It will be called even if a read or write error happened earlier. This is a terminal callback, which means that the HTTPTransaction object that gives this call will be invalid after this function completes.

Implements proxygen::HTTPTransactionHandler.

Definition at line 31 of file RequestHandlerAdaptor.cpp.

References err_, proxygen::kErrorNone, proxygen::RequestHandler::requestComplete(), and proxygen::ResponseHandler::upstream_.

31  {
32  if (err_ == kErrorNone) {
34  }
35 
36  // Otherwise we would have got some error call back and invoked onError
37  // on RequestHandler
38  delete this;
39 }
virtual void requestComplete() noexcept=0
RequestHandler * upstream_
void proxygen::RequestHandlerAdaptor::getCurrentTransportInfo ( wangle::TransportInfo tinfo) const
overrideprivatevirtual

Implements proxygen::ResponseHandler.

Definition at line 202 of file RequestHandlerAdaptor.cpp.

References proxygen::HTTPTransaction::getCurrentTransportInfo(), and proxygen::ResponseHandler::txn_.

203  {
205 }
HTTPTransaction * txn_
void getCurrentTransportInfo(wangle::TransportInfo *tinfo) const
const wangle::TransportInfo & proxygen::RequestHandlerAdaptor::getSetupTransportInfo ( ) const
overrideprivatevirtualnoexcept

Implements proxygen::ResponseHandler.

Definition at line 198 of file RequestHandlerAdaptor.cpp.

References proxygen::HTTPTransaction::getSetupTransportInfo(), and proxygen::ResponseHandler::txn_.

198  {
199  return txn_->getSetupTransportInfo();
200 }
const wangle::TransportInfo & getSetupTransportInfo() const noexcept
HTTPTransaction * txn_
ResponseHandler * proxygen::RequestHandlerAdaptor::newExMessage ( ExMessageHandler exHandler,
bool  unidirectional 
)
overrideprivatevirtualnoexcept

Reimplemented from proxygen::ResponseHandler.

Definition at line 189 of file RequestHandlerAdaptor.cpp.

References proxygen::ResponseHandler::getTransaction(), handler(), proxygen::HTTPTransaction::newExTransaction(), and RequestHandlerAdaptor().

191  {
193  getTransaction()->newExTransaction(handler, unidirectional);
194  return handler;
195 }
RequestHandlerAdaptor(RequestHandler *requestHandler)
HTTPTransaction * getTransaction() const noexcept
void handler(int, siginfo_t *, void *)
virtual HTTPTransaction * newExTransaction(HTTPTransactionHandler *handler, bool unidirectional=false)
ResponseHandler * proxygen::RequestHandlerAdaptor::newPushedResponse ( PushHandler pushHandler)
overrideprivatevirtualnoexcept

Implements proxygen::ResponseHandler.

Definition at line 177 of file RequestHandlerAdaptor.cpp.

References proxygen::HTTPTransaction::newPushedTransaction(), RequestHandlerAdaptor(), and proxygen::ResponseHandler::txn_.

178  {
179  auto pushTxn = txn_->newPushedTransaction(pushHandler->getHandler());
180  if (!pushTxn) {
181  // Codec doesn't support push
182  return nullptr;
183  }
184  auto pushHandlerAdaptor = new RequestHandlerAdaptor(pushHandler);
185  pushHandlerAdaptor->setTransaction(pushTxn);
186  return pushHandlerAdaptor;
187 }
RequestHandlerAdaptor(RequestHandler *requestHandler)
HTTPTransaction * txn_
virtual HTTPTransaction * newPushedTransaction(HTTPPushTransactionHandler *handler)
void proxygen::RequestHandlerAdaptor::onBody ( std::unique_ptr< folly::IOBuf chain)
overrideprivatevirtualnoexcept

Can be called multiple times per transaction. If you had previously called pauseIngress(), this callback will be delayed until you call resumeIngress().

Implements proxygen::HTTPTransactionHandler.

Definition at line 67 of file RequestHandlerAdaptor.cpp.

References c, folly::gen::move, proxygen::RequestHandler::onBody(), and proxygen::ResponseHandler::upstream_.

67  {
69 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
RequestHandler * upstream_
char c
virtual void onBody(std::unique_ptr< folly::IOBuf > body) noexcept=0
void proxygen::RequestHandlerAdaptor::onChunkComplete ( )
overrideprivatevirtualnoexcept

Can be called multiple times per transaction. If you had previously called pauseIngress(), this callback will be delayed until you call resumeIngress(). This signifies the end of a chunk.

Reimplemented from proxygen::HTTPTransactionHandler.

Definition at line 73 of file RequestHandlerAdaptor.cpp.

73  {
74 }
void proxygen::RequestHandlerAdaptor::onChunkHeader ( size_t  )
overrideprivatevirtualnoexcept

Can be called multiple times per transaction. If you had previously called pauseIngress(), this callback will be delayed until you call resumeIngress(). This signifies the beginning of a chunk of length 'length'. You will receive onBody() after this. Also, the length will be greater than zero.

Reimplemented from proxygen::HTTPTransactionHandler.

Definition at line 71 of file RequestHandlerAdaptor.cpp.

71 {}
void proxygen::RequestHandlerAdaptor::onEgressPaused ( )
overrideprivatevirtualnoexcept

If the remote side's receive buffer fills up, this callback will be invoked so you can attempt to stop sending to the remote side.

Implements proxygen::HTTPTransactionHandler.

Definition at line 127 of file RequestHandlerAdaptor.cpp.

References proxygen::RequestHandler::onEgressPaused(), and proxygen::ResponseHandler::upstream_.

127  {
129 }
virtual void onEgressPaused() noexcept
RequestHandler * upstream_
void proxygen::RequestHandlerAdaptor::onEgressResumed ( )
overrideprivatevirtualnoexcept

This callback lets you know that the remote side has resumed reading and you can now continue to send data.

Implements proxygen::HTTPTransactionHandler.

Definition at line 131 of file RequestHandlerAdaptor.cpp.

References proxygen::RequestHandler::onEgressResumed(), and proxygen::ResponseHandler::upstream_.

131  {
133 }
RequestHandler * upstream_
virtual void onEgressResumed() noexcept
void proxygen::RequestHandlerAdaptor::onEOM ( )
overrideprivatevirtualnoexcept

Can be called once per transaction. If you had previously called pauseIngress(), this callback will be delayed until you call resumeIngress(). After this callback is received, there will be no more normal ingress callbacks received (onEgress*() and onError() may still be invoked). The Handler should consider ingress complete after receiving this message. This Transaction is still valid, and work may still occur on it until detachTransaction is called.

Implements proxygen::HTTPTransactionHandler.

Definition at line 81 of file RequestHandlerAdaptor.cpp.

References err_, proxygen::kErrorNone, proxygen::RequestHandler::onEOM(), and proxygen::ResponseHandler::upstream_.

81  {
82  if (err_ == kErrorNone) {
83  upstream_->onEOM();
84  }
85 }
virtual void onEOM() noexcept=0
RequestHandler * upstream_
void proxygen::RequestHandlerAdaptor::onError ( const HTTPException error)
overrideprivatevirtualnoexcept

Can be called at any time before detachTransaction(). This callback implies that an error has occurred. To determine if ingress or egress is affected, check the direciont on the HTTPException. If the direction is INGRESS, it MAY still be possible to send egress.

Implements proxygen::HTTPTransactionHandler.

Definition at line 91 of file RequestHandlerAdaptor.cpp.

References proxygen::HTTPTransaction::canSendHeaders(), proxygen::ResponseBuilder::closeConnection(), err_, folly::pushmi::operators::error(), proxygen::HTTPException::INGRESS, proxygen::kErrorNone, proxygen::kErrorRead, proxygen::kErrorTimeout, proxygen::kErrorWrite, sendAbort(), proxygen::ResponseBuilder::sendWithEOM(), setError(), proxygen::ResponseBuilder::status(), and proxygen::ResponseHandler::txn_.

91  {
92  if (err_ != kErrorNone) {
93  // we have already handled an error and upstream would have been deleted
94  return;
95  }
96 
97  if (error.getProxygenError() == kErrorTimeout) {
99 
100  if (!txn_->canSendHeaders()) {
101  sendAbort();
102  } else {
103  ResponseBuilder(this)
104  .status(408, "Request Timeout")
105  .closeConnection()
106  .sendWithEOM();
107  }
108  } else if (error.getDirection() == HTTPException::Direction::INGRESS) {
110 
111  if (!txn_->canSendHeaders()) {
112  sendAbort();
113  } else {
114  ResponseBuilder(this)
115  .status(400, "Bad Request")
116  .closeConnection()
117  .sendWithEOM();
118  }
119 
120  } else {
122  }
123 
124  // Wait for detachTransaction to clean up
125 }
HTTPTransaction * txn_
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
virtual bool canSendHeaders() const
void setError(ProxygenError err) noexcept
void proxygen::RequestHandlerAdaptor::onExTransaction ( HTTPTransaction )
overrideprivatevirtualnoexcept

Ask the handler to construct a handler for a ExTransaction associated with its transaction.

Reimplemented from proxygen::HTTPTransactionHandler.

Definition at line 135 of file RequestHandlerAdaptor.cpp.

References proxygen::RequestHandler::getExHandler(), handler(), RequestHandlerAdaptor(), and proxygen::ResponseHandler::upstream_.

135  {
136  // Create handler for child EX transaction.
138  txn->setHandler(handler);
139 }
RequestHandlerAdaptor(RequestHandler *requestHandler)
virtual ExMessageHandler * getExHandler() noexcept
void handler(int, siginfo_t *, void *)
RequestHandler * upstream_
void proxygen::RequestHandlerAdaptor::onHeadersComplete ( std::unique_ptr< HTTPMessage msg)
overrideprivatevirtualnoexcept

Called at most once per transaction. This is usually the first ingress callback. It is possible to get a read error before this however. If you had previously called pauseIngress(), this callback will be delayed until you call resumeIngress().

Implements proxygen::HTTPTransactionHandler.

Definition at line 41 of file RequestHandlerAdaptor.cpp.

References proxygen::RequestHandler::canHandleExpect(), proxygen::ResponseBuilder::closeConnection(), err_, proxygen::HTTP_HEADER_EXPECT, proxygen::kErrorNone, proxygen::kErrorUnsupportedExpectation, folly::gen::move, proxygen::RequestHandler::onRequest(), proxygen::ResponseBuilder::send(), proxygen::ResponseBuilder::sendWithEOM(), setError(), proxygen::ResponseBuilder::status(), and proxygen::ResponseHandler::upstream_.

42  {
43  if (msg->getHeaders().exists(HTTP_HEADER_EXPECT) &&
45  auto expectation = msg->getHeaders().getSingleOrEmpty(HTTP_HEADER_EXPECT);
46  if (!boost::iequals(expectation, "100-continue")) {
48 
49  ResponseBuilder(this)
50  .status(417, "Expectation Failed")
51  .closeConnection()
52  .sendWithEOM();
53  } else {
54  ResponseBuilder(this)
55  .status(100, "Continue")
56  .send();
57 
58  }
59  }
60 
61  // Only in case of no error
62  if (err_ == kErrorNone) {
64  }
65 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
virtual bool canHandleExpect() noexcept
virtual void onRequest(std::unique_ptr< HTTPMessage > headers) noexcept=0
RequestHandler * upstream_
void setError(ProxygenError err) noexcept
void proxygen::RequestHandlerAdaptor::onTrailers ( std::unique_ptr< HTTPHeaders trailers)
overrideprivatevirtualnoexcept

Can be called any number of times per transaction. If you had previously called pauseIngress(), this callback will be delayed until you call resumeIngress(). Trailers can be received once right before the EOM of a chunked HTTP/1.1 reponse or multiple times per transaction from SPDY and HTTP/2.0 HEADERS frames.

Implements proxygen::HTTPTransactionHandler.

Definition at line 76 of file RequestHandlerAdaptor.cpp.

77  {
78  // XXX: Support trailers
79 }
void proxygen::RequestHandlerAdaptor::onUpgrade ( UpgradeProtocol  protocol)
overrideprivatevirtualnoexcept

Can be called once per transaction. If you had previously called pauseIngress(), this callback will be delayed until you call resumeIngress(). After this callback is invoked, further data will be forwarded using the onBody() callback. Once the data transfer is completed (EOF recevied in case of CONNECT), onEOM() callback will be invoked.

Implements proxygen::HTTPTransactionHandler.

Definition at line 87 of file RequestHandlerAdaptor.cpp.

References proxygen::RequestHandler::onUpgrade(), and proxygen::ResponseHandler::upstream_.

87  {
88  upstream_->onUpgrade(protocol);
89 }
RequestHandler * upstream_
virtual void onUpgrade(proxygen::UpgradeProtocol prot) noexcept=0
void proxygen::RequestHandlerAdaptor::pauseIngress ( )
overrideprivatevirtualnoexcept
void proxygen::RequestHandlerAdaptor::refreshTimeout ( )
overrideprivatevirtualnoexcept
void proxygen::RequestHandlerAdaptor::resumeIngress ( )
overrideprivatevirtualnoexcept
void proxygen::RequestHandlerAdaptor::sendAbort ( )
overrideprivatevirtualnoexcept

Implements proxygen::ResponseHandler.

Definition at line 161 of file RequestHandlerAdaptor.cpp.

References proxygen::HTTPTransaction::sendAbort(), and proxygen::ResponseHandler::txn_.

Referenced by onError().

161  {
162  txn_->sendAbort();
163 }
HTTPTransaction * txn_
void proxygen::RequestHandlerAdaptor::sendBody ( std::unique_ptr< folly::IOBuf body)
overrideprivatevirtualnoexcept

Implements proxygen::ResponseHandler.

Definition at line 149 of file RequestHandlerAdaptor.cpp.

References b, folly::gen::move, proxygen::HTTPTransaction::sendBody(), and proxygen::ResponseHandler::txn_.

149  {
151 }
char b
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
HTTPTransaction * txn_
virtual void sendBody(std::unique_ptr< folly::IOBuf > body)
void proxygen::RequestHandlerAdaptor::sendChunkHeader ( size_t  len)
overrideprivatevirtualnoexcept

Implements proxygen::ResponseHandler.

Definition at line 145 of file RequestHandlerAdaptor.cpp.

References proxygen::HTTPTransaction::sendChunkHeader(), and proxygen::ResponseHandler::txn_.

145  {
147 }
HTTPTransaction * txn_
virtual void sendChunkHeader(size_t length)
void proxygen::RequestHandlerAdaptor::sendChunkTerminator ( )
overrideprivatevirtualnoexcept
void proxygen::RequestHandlerAdaptor::sendEOM ( )
overrideprivatevirtualnoexcept

Implements proxygen::ResponseHandler.

Reimplemented in StubRequestHandlerAdaptor.

Definition at line 157 of file RequestHandlerAdaptor.cpp.

References proxygen::HTTPTransaction::sendEOM(), and proxygen::ResponseHandler::txn_.

157  {
158  txn_->sendEOM();
159 }
HTTPTransaction * txn_
void proxygen::RequestHandlerAdaptor::sendHeaders ( HTTPMessage msg)
overrideprivatevirtualnoexcept

NOTE: We take response message as non-const reference, to allow filters between your handler and client to be able to modify response if they want to.

eg. a compression filter might want to change the content-encoding

Implements proxygen::ResponseHandler.

Reimplemented in StubRequestHandlerAdaptor.

Definition at line 141 of file RequestHandlerAdaptor.cpp.

References proxygen::HTTPTransaction::sendHeaders(), and proxygen::ResponseHandler::txn_.

141  {
142  txn_->sendHeaders(msg);
143 }
HTTPTransaction * txn_
virtual void sendHeaders(const HTTPMessage &headers)
void proxygen::RequestHandlerAdaptor::setError ( ProxygenError  err)
privatenoexcept

Definition at line 207 of file RequestHandlerAdaptor.cpp.

References err_, proxygen::RequestHandler::onError(), and proxygen::ResponseHandler::upstream_.

Referenced by onError(), and onHeadersComplete().

207  {
208  err_ = err;
209  upstream_->onError(err);
210 }
RequestHandler * upstream_
virtual void onError(ProxygenError err) noexcept=0
void proxygen::RequestHandlerAdaptor::setTransaction ( HTTPTransaction txn)
overrideprivatevirtualnoexcept

Called once per transaction. This notifies the handler of which transaction it should talk to and will receive callbacks from.

Implements proxygen::HTTPTransactionHandler.

Definition at line 24 of file RequestHandlerAdaptor.cpp.

References proxygen::RequestHandler::setResponseHandler(), proxygen::ResponseHandler::txn_, and proxygen::ResponseHandler::upstream_.

24  {
25  txn_ = txn;
26 
27  // We become that transparent layer
29 }
virtual void setResponseHandler(ResponseHandler *handler) noexcept
HTTPTransaction * txn_
RequestHandler * upstream_

Member Data Documentation

ProxygenError proxygen::RequestHandlerAdaptor::err_ {kErrorNone}
private

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