proxygen
HTTPDirectResponseHandler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
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. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
11 
12 #include <folly/Conv.h>
14 
15 using folly::IOBuf;
16 using std::string;
17 using std::unique_ptr;
18 
19 namespace proxygen {
20 
22  unsigned statusCode, const std::string& statusMsg,
23  const HTTPErrorPage* errorPage):
24  txn_(nullptr),
25  errorPage_(errorPage),
26  statusMessage_(statusMsg),
27  statusCode_(statusCode),
28  headersSent_(false),
29  eomSent_(false),
30  forceConnectionClose_(true) {
31 }
32 
34 }
35 
36 void
38  txn_ = txn;
39 }
40 
41 void
43  delete this;
44 }
45 
47  std::unique_ptr<HTTPMessage> /*msg*/) noexcept {
48  VLOG(4) << "processing request";
49  headersSent_ = true;
50  HTTPMessage response;
51  std::unique_ptr<folly::IOBuf> responseBody;
52  response.setHTTPVersion(1, 1);
53  response.setStatusCode(statusCode_);
54  if (!statusMessage_.empty()) {
56  } else {
58  }
60  response.getHeaders().add(HTTP_HEADER_CONNECTION, "close");
61  }
62  if (errorPage_) {
64  statusMessage_, nullptr, empty_string);
65  VLOG(4) << "sending error page with type " << page.contentType;
67  responseBody = std::move(page.content);
68  }
69  response.getHeaders().add(HTTP_HEADER_CONTENT_LENGTH, folly::to<string>(
70  responseBody ? responseBody->computeChainDataLength() : 0));
71  txn_->sendHeaders(response);
72  if (responseBody) {
73  txn_->sendBody(std::move(responseBody));
74  }
75 }
76 
77 void HTTPDirectResponseHandler::onBody(unique_ptr<IOBuf> /*chain*/) noexcept {
78  VLOG(4) << "discarding request body";
79 }
80 
82  unique_ptr<HTTPHeaders> /*trailers*/) noexcept {
83  VLOG(4) << "discarding request trailers";
84 }
85 
86 void
88  eomSent_ = true;
89  txn_->sendEOM();
90 }
91 
93  UpgradeProtocol /*protocol*/) noexcept {}
94 
96  if (error.getDirection() == HTTPException::Direction::INGRESS) {
97  if (error.getProxygenError() == kErrorTimeout) {
98  VLOG(4) << "processing ingress timeout";
99  if (!headersSent_) {
100  onHeadersComplete(nullptr);
101  }
102  if (!eomSent_) {
103  onEOM();
104  }
105  } else {
106  VLOG(4) << "processing ingress error";
107  if (!headersSent_) {
108  onHeadersComplete(nullptr);
109  }
110  if (!eomSent_) {
111  onEOM();
112  }
113  }
114  }
115 }
116 
117 } // proxygen
spdy::GoawayStatusCode statusCode
Definition: SPDYCodec.cpp:110
void setStatusMessage(T &&msg)
Definition: HTTPMessage.h:242
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const std::string contentType
Definition: HTTPErrorPage.h:37
requires E e noexcept(noexcept(s.error(std::move(e))))
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
#define nullptr
Definition: http_parser.c:41
HTTPDirectResponseHandler(unsigned statusCode, const std::string &statusMsg, const HTTPErrorPage *errorPage=nullptr)
virtual void sendBody(std::unique_ptr< folly::IOBuf > body)
void onBody(std::unique_ptr< folly::IOBuf > chain) noexceptoverride
virtual void sendHeaders(const HTTPMessage &headers)
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
void setTransaction(HTTPTransaction *txn) noexceptoverride
virtual Page generate(uint64_t requestID, unsigned httpStatusCode, const std::string &reason, std::unique_ptr< folly::IOBuf > body, const std::string &detailReason) const =0
std::size_t computeChainDataLength() const
Definition: IOBuf.cpp:501
void onHeadersComplete(std::unique_ptr< HTTPMessage > msg) noexceptoverride
const std::string empty_string
Definition: HTTPHeaders.cpp:23
const char * string
Definition: Conv.cpp:212
void onUpgrade(UpgradeProtocol protocol) noexceptoverride
void setHTTPVersion(uint8_t major, uint8_t minor)
void onError(const HTTPException &error) noexceptoverride
void add(folly::StringPiece name, folly::StringPiece value)
Definition: HTTPHeaders.cpp:52
static const char * getDefaultReason(uint16_t status)
std::unique_ptr< folly::IOBuf > content
Definition: HTTPErrorPage.h:38
void onTrailers(std::unique_ptr< HTTPHeaders > trailers) noexceptoverride
void setStatusCode(uint16_t status)