本文整理汇总了C++中HTTPMessage::setStatusCode方法的典型用法代码示例。如果您正苦于以下问题:C++ HTTPMessage::setStatusCode方法的具体用法?C++ HTTPMessage::setStatusCode怎么用?C++ HTTPMessage::setStatusCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTPMessage
的用法示例。
在下文中一共展示了HTTPMessage::setStatusCode方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: upstream
TEST(HTTP1xCodecTest, Test1xxConnectionHeader) {
HTTP1xCodec upstream(TransportDirection::UPSTREAM);
HTTP1xCodec downstream(TransportDirection::DOWNSTREAM);
HTTP1xCodecCallback callbacks;
upstream.setCallback(&callbacks);
HTTPMessage resp;
resp.setStatusCode(100);
resp.setHTTPVersion(1, 1);
resp.getHeaders().add(HTTP_HEADER_CONNECTION, "Upgrade");
folly::IOBufQueue writeBuf(folly::IOBufQueue::cacheChainLength());
auto streamID = downstream.createStream();
downstream.generateHeader(writeBuf, streamID, resp);
upstream.onIngress(*writeBuf.front());
EXPECT_EQ(callbacks.headersComplete, 1);
EXPECT_EQ(
callbacks.msg_->getHeaders().getSingleOrEmpty(HTTP_HEADER_CONNECTION),
"Upgrade");
resp.setStatusCode(200);
resp.getHeaders().remove(HTTP_HEADER_CONNECTION);
resp.getHeaders().add(HTTP_HEADER_CONTENT_LENGTH, "0");
writeBuf.move();
downstream.generateHeader(writeBuf, streamID, resp);
upstream.onIngress(*writeBuf.front());
EXPECT_EQ(callbacks.headersComplete, 2);
EXPECT_EQ(
callbacks.msg_->getHeaders().getSingleOrEmpty(HTTP_HEADER_CONNECTION),
"keep-alive");
}
示例2: parse
TEST_F(HTTP2CodecTest, BadSettingsTableSize) {
auto settings = upstreamCodec_.getEgressSettings();
settings->setSetting(SettingsId::HEADER_TABLE_SIZE, 8192);
// This sets the max decoder table size to 8k
upstreamCodec_.generateSettings(output_);
parse();
EXPECT_EQ(callbacks_.settings, 1);
EXPECT_EQ(callbacks_.streamErrors, 0);
EXPECT_EQ(callbacks_.sessionErrors, 0);
callbacks_.reset();
// Set max decoder table size back to 4k, but don't parse it. The
// upstream encoder will up the table size to 8k per the first settings frame
// and the HPACK codec will send a code to update the decoder.
settings->setSetting(SettingsId::HEADER_TABLE_SIZE, 4096);
upstreamCodec_.generateSettings(output_);
HTTPMessage resp;
resp.setStatusCode(200);
resp.setStatusMessage("nifty-nice");
resp.getHeaders().add("content-type", "x-coolio");
SetUpUpstreamTest();
downstreamCodec_.generateHeader(output_, 1, resp, 0);
parseUpstream();
EXPECT_EQ(callbacks_.streamErrors, 0);
EXPECT_EQ(callbacks_.sessionErrors, 1);
EXPECT_EQ(callbacks_.messageBegin, 0);
EXPECT_EQ(callbacks_.headersComplete, 0);
}
示例3: parse
TEST_F(HTTP2CodecTest, SettingsTableSize) {
auto settings = upstreamCodec_.getEgressSettings();
settings->setSetting(SettingsId::HEADER_TABLE_SIZE, 8192);
upstreamCodec_.generateSettings(output_);
parse();
EXPECT_EQ(callbacks_.settings, 1);
EXPECT_EQ(callbacks_.streamErrors, 0);
EXPECT_EQ(callbacks_.sessionErrors, 0);
callbacks_.reset();
HTTPMessage resp;
resp.setStatusCode(200);
resp.setStatusMessage("nifty-nice");
resp.getHeaders().add("content-type", "x-coolio");
output_.move();
downstreamCodec_.generateConnectionPreface(output_);
downstreamCodec_.generateHeader(output_, 1, resp, 0);
parseUpstream();
callbacks_.expectMessage(false, 1, 200);
const auto& headers = callbacks_.msg->getHeaders();
EXPECT_EQ("x-coolio", headers.getSingleOrEmpty("content-type"));
}
示例4: codec
TEST(HTTP1xCodecTest, TestBadPost100) {
HTTP1xCodec codec(TransportDirection::DOWNSTREAM);
MockHTTPCodecCallback callbacks;
codec.setCallback(&callbacks);
folly::IOBufQueue writeBuf(folly::IOBufQueue::cacheChainLength());
InSequence enforceOrder;
EXPECT_CALL(callbacks, onMessageBegin(1, _));
EXPECT_CALL(callbacks, onHeadersComplete(1, _))
.WillOnce(InvokeWithoutArgs([&] {
HTTPMessage cont;
cont.setStatusCode(100);
cont.setStatusMessage("Continue");
codec.generateHeader(writeBuf, 1, cont);
}));
EXPECT_CALL(callbacks, onBody(1, _, _));
EXPECT_CALL(callbacks, onMessageComplete(1, _));
EXPECT_CALL(callbacks, onMessageBegin(2, _))
.WillOnce(InvokeWithoutArgs([&] {
// simulate HTTPSession's aversion to pipelining
codec.setParserPaused(true);
// Trigger the response to the POST
HTTPMessage resp;
resp.setStatusCode(200);
resp.setStatusMessage("OK");
codec.generateHeader(writeBuf, 1, resp);
codec.generateEOM(writeBuf, 1);
codec.setParserPaused(false);
}));
EXPECT_CALL(callbacks, onError(2, _, _))
.WillOnce(InvokeWithoutArgs([&] {
HTTPMessage resp;
resp.setStatusCode(400);
resp.setStatusMessage("Bad");
codec.generateHeader(writeBuf, 2, resp);
codec.generateEOM(writeBuf, 2);
}));
// Generate a POST request with a bad content-length
auto reqBuf = folly::IOBuf::copyBuffer(
"POST /www.facebook.com HTTP/1.1\r\nHost: www.facebook.com\r\n"
"Expect: 100-Continue\r\nContent-Length: 5\r\n\r\nabcdefghij");
codec.onIngress(*reqBuf);
}
示例5: getResponse
HTTPMessage getResponse(uint32_t code, uint32_t bodyLen) {
HTTPMessage resp;
resp.setStatusCode(code);
if (bodyLen > 0) {
resp.getHeaders().set(HTTP_HEADER_CONTENT_LENGTH,
folly::to<string>(bodyLen));
}
return resp;
}
示例6: egressCodec
TEST(SPDYCodecTest, SegmentedHeaderBlock) {
SPDYCodec egressCodec(TransportDirection::UPSTREAM,
SPDYVersion::SPDY3_1_HPACK);
SPDYCodec ingressCodec(TransportDirection::DOWNSTREAM,
SPDYVersion::SPDY3_1_HPACK);
// generate huge string to use as a header value
string huge;
uint32_t size = 20000;
char ch = 'a';
for (uint32_t i = 0; i < size; i++) {
huge.push_back(ch);
if (ch == 'z') {
ch = 'a';
} else {
ch++;
}
}
HTTPMessage req;
req.setMethod("GET");
req.setURL("http://www.facebook.com");
auto& reqHeaders = req.getHeaders();
reqHeaders.set("HOST", "www.facebook.com");
// setting this huge header value will cause allocation of a separate IOBuf
reqHeaders.set("X-FB-Huge", huge);
auto buf = getSynStream(egressCodec, 1, req);
FakeHTTPCodecCallback callbacks;
ingressCodec.setCallback(&callbacks);
ingressCodec.onIngress(*buf);
EXPECT_EQ(callbacks.streamErrors, 0);
EXPECT_EQ(callbacks.sessionErrors, 0);
EXPECT_EQ(callbacks.headersComplete, 1);
EXPECT_EQ(callbacks.msg->getHeaders().getSingleOrEmpty("x-fb-huge").size(),
size);
// do it for responses
HTTPMessage resp;
resp.setStatusCode(200);
resp.setStatusMessage("OK");
auto& respHeaders = resp.getHeaders();
respHeaders.set("X-FB-Huge", huge);
auto buf2 = getSynStream(ingressCodec, 1, resp);
callbacks.reset();
egressCodec.setCallback(&callbacks);
egressCodec.onIngress(*buf2);
EXPECT_EQ(callbacks.streamErrors, 0);
EXPECT_EQ(callbacks.sessionErrors, 0);
EXPECT_EQ(callbacks.headersComplete, 1);
EXPECT_EQ(callbacks.msg->getHeaders().getSingleOrEmpty("x-fb-huge").size(),
size);
}
示例7: sendErrorResponse
void ProxygenTransport::sendErrorResponse(uint32_t code) noexcept {
HTTPMessage response;
response.setHTTPVersion(1, 1);
response.setStatusCode(code);
response.setStatusMessage(HTTPMessage::getDefaultReason(code));
response.getHeaders().add(HTTP_HEADER_CONNECTION, "close");
CHECK(!m_sendStarted);
m_sendStarted = true;
m_sendEnded = true;
m_responseCode = code;
m_responseCodeInfo = response.getStatusMessage();
m_server->onRequestError(this);
m_clientTxn->sendHeaders(response);
m_clientTxn->sendEOM();
}
示例8: parseUpstream
TEST_F(HTTP2CodecTest, BasicHeaderReply) {
HTTPMessage resp;
resp.setStatusCode(200);
resp.setStatusMessage("nifty-nice");
resp.getHeaders().add("content-type", "x-coolio");
downstreamCodec_.generateHeader(output_, 1, resp, 0);
downstreamCodec_.generateEOM(output_, 1);
parseUpstream();
callbacks_.expectMessage(true, 1, 200);
const auto& headers = callbacks_.msg->getHeaders();
// HTTP/2 doesnt support serialization - instead you get the default
EXPECT_EQ("OK", callbacks_.msg->getStatusMessage());
EXPECT_EQ("x-coolio", headers.getSingleOrEmpty("content-type"));
}
示例9: parse
TEST_F(HTTP2CodecTest, GoawayReply) {
upstreamCodec_.generateGoaway(output_, 0, ErrorCode::NO_ERROR);
parse();
EXPECT_EQ(callbacks_.goaways, 1);
EXPECT_EQ(callbacks_.streamErrors, 0);
EXPECT_EQ(callbacks_.sessionErrors, 0);
SetUpUpstreamTest();
HTTPMessage resp;
resp.setStatusCode(200);
resp.setStatusMessage("nifty-nice");
downstreamCodec_.generateHeader(output_, 1, resp, 0);
downstreamCodec_.generateEOM(output_, 1);
parseUpstream();
callbacks_.expectMessage(true, 0, 200);
}
示例10: VLOG
bool
HTTPDownstreamSession::onNativeProtocolUpgrade(
HTTPCodec::StreamID streamID, CodecProtocol protocol,
const std::string& protocolString,
HTTPMessage& msg) {
VLOG(4) << *this << " onNativeProtocolUpgrade streamID=" << streamID <<
" protocol=" << protocolString;
auto txn = findTransaction(streamID);
CHECK(txn);
if (txn->canSendHeaders()) {
// Create the new Codec
auto codec = HTTPCodecFactory::getCodec(protocol,
TransportDirection::DOWNSTREAM);
CHECK(codec);
if (!codec->onIngressUpgradeMessage(msg)) {
VLOG(4) << *this << " codec rejected upgrade";
return false;
}
// Send a 101 Switching Protocols message while we still have HTTP codec
// Note: it's possible that the client timed out waiting for a
// 100-continue and ended up here first. In this case the 100 may go
// out in the new protocol
HTTPMessage switchingProtos;
switchingProtos.setHTTPVersion(1, 1);
switchingProtos.setStatusCode(101);
switchingProtos.setStatusMessage("Switching Protocols");
switchingProtos.getHeaders().set(HTTP_HEADER_UPGRADE, protocolString);
txn->sendHeaders(switchingProtos);
// no sendEOM for 1xx
// This will actually switch the protocol
bool ret = HTTPSession::onNativeProtocolUpgradeImpl(
streamID, std::move(codec), protocolString);
if (ret) {
codec_->addPriorityNodes(txnEgressQueue_, writeBuf_, 0);
}
return ret;
} else {
VLOG(4) << *this << " plaintext upgrade failed due to early response";
return false;
}
}
示例11: onHeadersComplete
void ProxygenTransport::onHeadersComplete(
unique_ptr<HTTPMessage> msg) noexcept {
Timer::GetMonotonicTime(m_requestStart);
m_request = std::move(msg);
if (m_request->isSecure()) {
setSSL();
}
m_request->dumpMessage(4);
auto method = m_request->getMethod();
const auto& methodStr = m_request->getMethodString();
if (method == HTTPMethod::GET) {
m_method = Transport::Method::GET;
} else if (method == HTTPMethod::POST ||
s_post_methods.find(methodStr) != s_post_methods.end()) {
m_method = Transport::Method::POST;
} else if (method == HTTPMethod::HEAD) {
m_method = Transport::Method::HEAD;
} else if (method == HTTPMethod::CONNECT) {
sendErrorResponse(400 /* Bad Request */);
return;
} else {
// looks like proxygen HTTP parser understands a few more methods
// than libevent:
// TRACE, COPY, MOVE, MKACTIVITY, CHECKOUT, MERGE, MSEARCH, NOTIFY,
// SUBSCRIBE, UNSUBSCRIBE, PATCH
sendErrorResponse(400 /* Bad Request */);
return;
}
m_extended_method = methodStr.c_str();
const auto& headers = m_request->getHeaders();
headers.forEach([&] (const std::string &header, const std::string &val) {
m_requestHeaders[header.c_str()].push_back(val.c_str());
});
if (m_method == Transport::Method::POST && m_request->isHTTP1_1()) {
const std::string& expectation =
headers.getSingleOrEmpty(HTTP_HEADER_EXPECT);
if (!expectation.empty()) {
bool sendEom = false;
HTTPMessage response;
if (!boost::iequals(expectation, "100-continue")) {
response.setStatusCode(417);
response.setStatusMessage(HTTPMessage::getDefaultReason(417));
response.getHeaders().add(HTTP_HEADER_CONNECTION, "close");
sendEom = true;
} else {
response.setStatusCode(100);
response.setStatusMessage(HTTPMessage::getDefaultReason(100));
}
response.setHTTPVersion(1, 1);
response.dumpMessage(4);
m_clientTxn->sendHeaders(response);
if (sendEom) {
m_responseCode = response.getStatusCode();
m_responseCodeInfo = response.getStatusMessage();
m_server->onRequestError(this);
m_clientTxn->sendEOM();
// this object is no longer valid
return;
}
}
}
if (!bufferRequest()) {
m_enqueued = true;
m_server->onRequest(shared_from_this());
} // otherwise we wait for EOM
}