Support 100-continue expact(rfc2616-8.2.3)

This commit is contained in:
antao 2018-09-07 16:07:41 +08:00
parent 7e3cac5c35
commit 26773ff81f
5 changed files with 46 additions and 6 deletions

View File

@ -32,7 +32,9 @@ namespace drogon
{
public:
enum Version {
kUnknown, kHttp10, kHttp11
kUnknown=0,
kHttp10=1,
kHttp11=2
};
enum Method {
kInvalid, kGet, kPost, kHead, kPut, kDelete

View File

@ -45,7 +45,7 @@ void HttpClientImpl::sendRequestInLoop(const drogon::HttpRequestPtr &req, const
LOG_TRACE<<"connection callback";
if(connPtr->connected())
{
connPtr->setContext(HttpContext());
connPtr->setContext(HttpContext(connPtr));
//send request;
LOG_TRACE<<"Connection established!";
auto req=thisPtr->_reqAndCallbacks.front().first;

View File

@ -30,11 +30,12 @@
#include <iostream>
using namespace trantor;
using namespace drogon;
HttpContext::HttpContext()
HttpContext::HttpContext(const trantor::TcpConnectionPtr &connPtr)
: state_(kExpectRequestLine),
request_(new HttpRequestImpl),
res_state_(HttpResponseParseState::kExpectResponseLine),
_pipeLineMutex(std::make_shared<std::mutex>())
_pipeLineMutex(std::make_shared<std::mutex>()),
_conn(connPtr)
{
}
bool HttpContext::processRequestLine(const char *begin, const char *end)
@ -129,6 +130,39 @@ bool HttpContext::parseRequest(MsgBuffer *buf)
{
request_->contentLen = atoi(len.c_str());
state_ = kExpectBody;
auto expect=request_->getHeader("Expect");
if(expect=="100-continue"&&
request_->getVersion()>=HttpRequest::kHttp11)
{
//rfc2616-8.2.3
//TODO:here we can add content-length limitation
auto connPtr=_conn.lock();
if(connPtr)
{
auto resp=HttpResponse::newHttpResponse();
resp->setStatusCode(HttpResponse::k100Continue);
MsgBuffer buffer;
std::dynamic_pointer_cast<HttpResponseImpl>(resp)
->appendToBuffer(&buffer);
connPtr->send(std::move(buffer));
}
}
else if(!expect.empty())
{
LOG_WARN<<"417ExpectationFailed for \""<<expect<<"\"";
auto connPtr=_conn.lock();
if(connPtr)
{
auto resp=HttpResponse::newHttpResponse();
resp->setStatusCode(HttpResponse::k417ExpectationFailed);
MsgBuffer buffer;
std::dynamic_pointer_cast<HttpResponseImpl>(resp)
->appendToBuffer(&buffer);
connPtr->send(std::move(buffer));
connPtr->shutdown();
}
}
}
else
{

View File

@ -32,6 +32,8 @@
#include <drogon/WebSocketConnection.h>
#include <list>
#include <mutex>
#include <trantor/net/TcpConnection.h>
using namespace trantor;
namespace drogon
{
@ -58,7 +60,7 @@ namespace drogon
kGotAll,
};
HttpContext();
HttpContext(const trantor::TcpConnectionPtr &connPtr);
// default copy-ctor, dtor and assignment are fine
@ -163,6 +165,8 @@ namespace drogon
std::list<std::pair<HttpRequestPtr,HttpResponsePtr>> _requestPipeLine;
std::shared_ptr<std::mutex> _pipeLineMutex;
std::weak_ptr<trantor::TcpConnection> _conn;
};
}

View File

@ -82,7 +82,7 @@ void HttpServer::start()
void HttpServer::onConnection(const TcpConnectionPtr& conn)
{
if (conn->connected()) {
conn->setContext(HttpContext());
conn->setContext(HttpContext(conn));
}
else if(conn->disconnected())
{