Re-sync with internal repository
[hiphop-php.git] / third-party / proxygen / src / proxygen / lib / http / session / HTTPDirectResponseHandler.cpp
blobe69e2a5e6636f0d83860a9ab4cf10eb723bb558d
1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
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 */
9 #include <proxygen/lib/http/session/HTTPDirectResponseHandler.h>
11 #include <folly/Conv.h>
12 #include <proxygen/lib/http/session/HTTPErrorPage.h>
14 using folly::IOBuf;
15 using std::string;
16 using std::unique_ptr;
18 namespace proxygen {
20 HTTPDirectResponseHandler::HTTPDirectResponseHandler(
21 unsigned statusCode,
22 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) {
33 HTTPDirectResponseHandler::~HTTPDirectResponseHandler() {
36 void HTTPDirectResponseHandler::setTransaction(HTTPTransaction* txn) noexcept {
37 txn_ = txn;
40 void HTTPDirectResponseHandler::detachTransaction() noexcept {
41 delete this;
44 void HTTPDirectResponseHandler::onHeadersComplete(
45 std::unique_ptr<HTTPMessage> /*msg*/) noexcept {
46 VLOG(4) << "processing request";
47 headersSent_ = true;
48 HTTPMessage response;
49 std::unique_ptr<folly::IOBuf> responseBody;
50 response.setHTTPVersion(1, 1);
51 response.setStatusCode(statusCode_);
52 if (!statusMessage_.empty()) {
53 response.setStatusMessage(statusMessage_);
54 } else {
55 response.setStatusMessage(HTTPMessage::getDefaultReason(statusCode_));
57 if (forceConnectionClose_) {
58 response.getHeaders().add(HTTP_HEADER_CONNECTION, "close");
60 if (errorPage_) {
61 HTTPErrorPage::Page page = errorPage_->generate(
62 0, statusCode_, statusMessage_, nullptr, empty_string);
63 VLOG(4) << "sending error page with type " << page.contentType;
64 response.getHeaders().add(HTTP_HEADER_CONTENT_TYPE, page.contentType);
65 responseBody = std::move(page.content);
67 response.getHeaders().add(
68 HTTP_HEADER_CONTENT_LENGTH,
69 folly::to<string>(responseBody ? responseBody->computeChainDataLength()
70 : 0));
71 txn_->sendHeaders(response);
72 if (responseBody) {
73 txn_->sendBody(std::move(responseBody));
77 void HTTPDirectResponseHandler::onBody(unique_ptr<IOBuf> /*chain*/) noexcept {
78 VLOG(4) << "discarding request body";
81 void HTTPDirectResponseHandler::onTrailers(
82 unique_ptr<HTTPHeaders> /*trailers*/) noexcept {
83 VLOG(4) << "discarding request trailers";
86 void HTTPDirectResponseHandler::onEOM() noexcept {
87 eomSent_ = true;
88 txn_->sendEOM();
91 void HTTPDirectResponseHandler::onUpgrade(
92 UpgradeProtocol /*protocol*/) noexcept {
95 void HTTPDirectResponseHandler::onError(const HTTPException& error) noexcept {
96 if (error.getDirection() == HTTPException::Direction::INGRESS) {
97 if (error.getProxygenError() == kErrorTimeout) {
98 VLOG(4) << "processing ingress timeout";
99 if (!headersSent_) {
100 onHeadersComplete(nullptr);
102 if (!eomSent_) {
103 onEOM();
105 } else {
106 VLOG(4) << "processing ingress error";
107 if (!headersSent_) {
108 onHeadersComplete(nullptr);
110 if (!eomSent_) {
111 onEOM();
117 } // namespace proxygen