forked from facebook/proxygen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseBuilder.h
More file actions
197 lines (172 loc) · 4.88 KB
/
ResponseBuilder.h
File metadata and controls
197 lines (172 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#pragma once
#include <folly/ScopeGuard.h>
#include <proxygen/httpserver/ResponseHandler.h>
namespace proxygen {
/**
* 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();
*
* 2. 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();
*
* 3. Accept or reject Upgrade Requests
*
* ResponseBuilder(handler)
* .acceptUpgradeRequest() // send '200 OK' without EOM
*
* or
*
* ResponseBuilder(handler)
* .rejectUpgradeRequest() // send '400 Bad Request'
*
*/
class ResponseBuilder {
public:
explicit ResponseBuilder(ResponseHandler* txn): txn_(txn) {
}
ResponseBuilder& status(uint16_t code, std::string message) {
headers_ = folly::make_unique<HTTPMessage>();
headers_->setHTTPVersion(1, 1);
headers_->setStatusCode(code);
headers_->setStatusMessage(message);
return *this;
}
template <typename T>
ResponseBuilder& header(const std::string& headerIn, const T& value) {
CHECK(headers_) << "You need to call `status` before adding headers";
headers_->getHeaders().add(headerIn, value);
return *this;
}
template <typename T>
ResponseBuilder& header(HTTPHeaderCode code, const T& value) {
CHECK(headers_) << "You need to call `status` before adding headers";
headers_->getHeaders().add(code, value);
return *this;
}
ResponseBuilder& body(std::unique_ptr<folly::IOBuf> bodyIn) {
if (bodyIn) {
if (body_) {
body_->prependChain(std::move(bodyIn));
} else {
body_ = std::move(bodyIn);
}
}
return *this;
}
template <typename T>
ResponseBuilder& body(T&& t) {
return body(folly::IOBuf::maybeCopyBuffer(
folly::to<std::string>(std::forward<T>(t))));
}
ResponseBuilder& closeConnection() {
return header(HTTP_HEADER_CONNECTION, "close");
}
void sendWithEOM() {
sendEOM_ = true;
send();
}
void send() {
// Once we send them, we don't want to send them again
SCOPE_EXIT { headers_.reset(); };
// By default, chunked
bool chunked = true;
// If we have complete response, we can use Content-Length and get done
if (headers_ && sendEOM_) {
chunked = false;
}
if (headers_) {
// We don't need to add Content-Length or Encoding for 1xx responses
if (headers_->getStatusCode() >= 200) {
if (chunked) {
headers_->setIsChunked(true);
} else {
const auto len = body_ ? body_->computeChainDataLength() : 0;
headers_->getHeaders().add(
HTTP_HEADER_CONTENT_LENGTH,
folly::to<std::string>(len));
}
}
txn_->sendHeaders(*headers_);
}
if (body_) {
if (chunked) {
txn_->sendChunkHeader(body_->computeChainDataLength());
txn_->sendBody(std::move(body_));
txn_->sendChunkTerminator();
} else {
txn_->sendBody(std::move(body_));
}
}
if (sendEOM_) {
txn_->sendEOM();
}
}
enum class UpgradeType {
CONNECT_REQUEST = 0,
HTTP_UPGRADE,
};
void acceptUpgradeRequest(UpgradeType upgradeType,
const std::string upgradeProtocol = "") {
headers_ = folly::make_unique<HTTPMessage>();
if (upgradeType == UpgradeType::CONNECT_REQUEST) {
headers_->constructDirectResponse({1, 1}, 200, "OK");
} else {
CHECK(!upgradeProtocol.empty());
headers_->constructDirectResponse({1, 1}, 101, "Switching Protocols");
headers_->getHeaders().add(HTTP_HEADER_UPGRADE, upgradeProtocol);
headers_->getHeaders().add(HTTP_HEADER_CONNECTION, "Upgrade");
}
txn_->sendHeaders(*headers_);
}
void rejectUpgradeRequest() {
headers_ = folly::make_unique<HTTPMessage>();
headers_->constructDirectResponse({1, 1}, 400, "Bad Request");
txn_->sendHeaders(*headers_);
txn_->sendEOM();
}
private:
ResponseHandler* const txn_{nullptr};
std::unique_ptr<HTTPMessage> headers_;
std::unique_ptr<folly::IOBuf> body_;
// If true, sends EOM.
bool sendEOM_{false};
};
}