1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 #include <proxygen/lib/http/session/HTTPTransaction.h>
12 
13 namespace proxygen {
14 
15 class HTTPErrorPage;
16 
17 class HTTPDirectResponseHandler : public HTTPTransaction::Handler {
18  public:
19   HTTPDirectResponseHandler(unsigned statusCode,
20                             const std::string& statusMsg,
21                             const HTTPErrorPage* errorPage = nullptr);
22 
forceConnectionClose(bool close)23   void forceConnectionClose(bool close) {
24     forceConnectionClose_ = close;
25   }
26   // HTTPTransaction::Handler methods
27   void setTransaction(HTTPTransaction* txn) noexcept override;
28   void detachTransaction() noexcept override;
29   void onHeadersComplete(std::unique_ptr<HTTPMessage> msg) noexcept override;
30   void onBody(std::unique_ptr<folly::IOBuf> chain) noexcept override;
31   void onTrailers(std::unique_ptr<HTTPHeaders> trailers) noexcept override;
32   void onEOM() noexcept override;
33   void onUpgrade(UpgradeProtocol protocol) noexcept override;
34   void onError(const HTTPException& error) noexcept override;
35   // These are no-ops since the direct response is already in memory
onEgressPaused()36   void onEgressPaused() noexcept override {
37   }
onEgressResumed()38   void onEgressResumed() noexcept override {
39   }
40 
41  private:
42   ~HTTPDirectResponseHandler() override;
43 
44   HTTPTransaction* txn_;
45   const HTTPErrorPage* errorPage_;
46   std::string statusMessage_;
47   unsigned statusCode_;
48   bool headersSent_ : 1;
49   bool eomSent_ : 1;
50   bool forceConnectionClose_ : 1;
51 };
52 
53 } // namespace proxygen
54