Rename some variables
This commit is contained in:
parent
f7b853e96c
commit
680fc28967
|
@ -173,7 +173,7 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const
|
|||
output->append("\r\n");
|
||||
|
||||
//LOG_INFO<<"request(no body):"<<output->peek();
|
||||
output->append(content_);
|
||||
output->append(_content);
|
||||
}
|
||||
|
||||
HttpRequestPtr HttpRequest::newHttpRequest()
|
||||
|
|
|
@ -37,7 +37,7 @@ class HttpRequestImpl : public HttpRequest
|
|||
HttpRequestImpl()
|
||||
: _method(Invalid),
|
||||
_version(kUnknown),
|
||||
contentLen(0)
|
||||
_contentLen(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,7 @@ class HttpRequestImpl : public HttpRequest
|
|||
}
|
||||
if (_method != Invalid)
|
||||
{
|
||||
content_ = "";
|
||||
_content = "";
|
||||
_query = "";
|
||||
_cookies.clear();
|
||||
_parameters.clear();
|
||||
|
@ -94,7 +94,7 @@ class HttpRequestImpl : public HttpRequest
|
|||
virtual void setMethod(const HttpMethod method) override
|
||||
{
|
||||
_method = method;
|
||||
content_ = "";
|
||||
_content = "";
|
||||
_query = "";
|
||||
_cookies.clear();
|
||||
_parameters.clear();
|
||||
|
@ -161,14 +161,14 @@ class HttpRequestImpl : public HttpRequest
|
|||
}
|
||||
// const string& content() const
|
||||
// {
|
||||
// return content_;
|
||||
// return _content;
|
||||
// }
|
||||
const std::string &query() const override
|
||||
{
|
||||
if (_query != "")
|
||||
return _query;
|
||||
if (_method == Post)
|
||||
return content_;
|
||||
return _content;
|
||||
return _query;
|
||||
}
|
||||
|
||||
|
@ -299,7 +299,7 @@ class HttpRequestImpl : public HttpRequest
|
|||
}
|
||||
const std::string &getContent() const
|
||||
{
|
||||
return content_;
|
||||
return _content;
|
||||
}
|
||||
void swap(HttpRequestImpl &that)
|
||||
{
|
||||
|
@ -317,13 +317,13 @@ class HttpRequestImpl : public HttpRequest
|
|||
std::swap(_peer, that._peer);
|
||||
std::swap(_local, that._local);
|
||||
_date.swap(that._date);
|
||||
content_.swap(that.content_);
|
||||
std::swap(contentLen, that.contentLen);
|
||||
_content.swap(that._content);
|
||||
std::swap(_contentLen, that._contentLen);
|
||||
}
|
||||
|
||||
void setContent(const std::string &content)
|
||||
{
|
||||
content_ = content;
|
||||
_content = content;
|
||||
}
|
||||
void addHeader(const std::string &key, const std::string &value)
|
||||
{
|
||||
|
@ -368,8 +368,8 @@ class HttpRequestImpl : public HttpRequest
|
|||
trantor::Date _date;
|
||||
|
||||
protected:
|
||||
std::string content_;
|
||||
size_t contentLen;
|
||||
std::string _content;
|
||||
size_t _contentLen;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<HttpRequestImpl> HttpRequestImplPtr;
|
||||
|
|
|
@ -50,14 +50,14 @@ static void defaultConnectionCallback(const trantor::TcpConnectionPtr &conn)
|
|||
HttpServer::HttpServer(EventLoop *loop,
|
||||
const InetAddress &listenAddr,
|
||||
const std::string &name)
|
||||
: server_(loop, listenAddr, name.c_str()),
|
||||
httpAsyncCallback_(defaultHttpAsyncCallback),
|
||||
newWebsocketCallback_(defaultWebSockAsyncCallback),
|
||||
: _server(loop, listenAddr, name.c_str()),
|
||||
_httpAsyncCallback(defaultHttpAsyncCallback),
|
||||
_newWebsocketCallback(defaultWebSockAsyncCallback),
|
||||
_connectionCallback(defaultConnectionCallback)
|
||||
{
|
||||
server_.setConnectionCallback(
|
||||
_server.setConnectionCallback(
|
||||
std::bind(&HttpServer::onConnection, this, _1));
|
||||
server_.setRecvMessageCallback(
|
||||
_server.setRecvMessageCallback(
|
||||
std::bind(&HttpServer::onMessage, this, _1, _2));
|
||||
}
|
||||
|
||||
|
@ -67,9 +67,9 @@ HttpServer::~HttpServer()
|
|||
|
||||
void HttpServer::start()
|
||||
{
|
||||
LOG_WARN << "HttpServer[" << server_.name()
|
||||
<< "] starts listenning on " << server_.ipPort();
|
||||
server_.start();
|
||||
LOG_WARN << "HttpServer[" << _server.name()
|
||||
<< "] starts listenning on " << _server.ipPort();
|
||||
_server.start();
|
||||
}
|
||||
|
||||
void HttpServer::onConnection(const TcpConnectionPtr &conn)
|
||||
|
@ -86,7 +86,7 @@ void HttpServer::onConnection(const TcpConnectionPtr &conn)
|
|||
// LOG_INFO << "###:" << string(buf->peek(), buf->readableBytes());
|
||||
if (context->webSocketConn())
|
||||
{
|
||||
disconnectWebsocketCallback_(context->webSocketConn());
|
||||
_disconnectWebsocketCallback(context->webSocketConn());
|
||||
}
|
||||
conn->setContext(std::string("None"));
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn,
|
|||
if (context->webSocketConn())
|
||||
{
|
||||
//websocket payload,we shouldn't parse it
|
||||
webSocketMessageCallback_(context->webSocketConn(), buf);
|
||||
_webSocketMessageCallback(context->webSocketConn(), buf);
|
||||
return;
|
||||
}
|
||||
if (!context->parseRequest(buf))
|
||||
|
@ -120,7 +120,7 @@ void HttpServer::onMessage(const TcpConnectionPtr &conn,
|
|||
if (context->firstReq() && isWebSocket(conn, context->request()))
|
||||
{
|
||||
auto wsConn = std::make_shared<WebSocketConnectionImpl>(conn);
|
||||
newWebsocketCallback_(context->request(),
|
||||
_newWebsocketCallback(context->request(),
|
||||
[=](const HttpResponsePtr &resp) mutable {
|
||||
if (resp->statusCode() == HttpResponse::k101SwitchingProtocols)
|
||||
{
|
||||
|
@ -163,7 +163,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r
|
|||
//std::lock_guard<std::mutex> guard(context->getPipeLineMutex());
|
||||
context->pushRquestToPipeLine(req);
|
||||
}
|
||||
httpAsyncCallback_(req, [=](const HttpResponsePtr &response) {
|
||||
_httpAsyncCallback(req, [=](const HttpResponsePtr &response) {
|
||||
if (!response)
|
||||
return;
|
||||
response->setCloseConnection(_close);
|
||||
|
|
|
@ -47,23 +47,23 @@ class HttpServer : trantor::NonCopyable
|
|||
|
||||
~HttpServer();
|
||||
|
||||
EventLoop *getLoop() const { return server_.getLoop(); }
|
||||
EventLoop *getLoop() const { return _server.getLoop(); }
|
||||
|
||||
void setHttpAsyncCallback(const HttpAsyncCallback &cb)
|
||||
{
|
||||
httpAsyncCallback_ = cb;
|
||||
_httpAsyncCallback = cb;
|
||||
}
|
||||
void setNewWebsocketCallback(const WebSocketNewAsyncCallback &cb)
|
||||
{
|
||||
newWebsocketCallback_ = cb;
|
||||
_newWebsocketCallback = cb;
|
||||
}
|
||||
void setDisconnectWebsocketCallback(const WebSocketDisconnetCallback &cb)
|
||||
{
|
||||
disconnectWebsocketCallback_ = cb;
|
||||
_disconnectWebsocketCallback = cb;
|
||||
}
|
||||
void setWebsocketMessageCallback(const WebSocketMessageCallback &cb)
|
||||
{
|
||||
webSocketMessageCallback_ = cb;
|
||||
_webSocketMessageCallback = cb;
|
||||
}
|
||||
void setConnectionCallback(const ConnectionCallback &cb)
|
||||
{
|
||||
|
@ -71,18 +71,18 @@ class HttpServer : trantor::NonCopyable
|
|||
}
|
||||
void setIoLoopNum(int numThreads)
|
||||
{
|
||||
server_.setIoLoopNum(numThreads);
|
||||
_server.setIoLoopNum(numThreads);
|
||||
}
|
||||
void kickoffIdleConnections(size_t timeout)
|
||||
{
|
||||
server_.kickoffIdleConnections(timeout);
|
||||
_server.kickoffIdleConnections(timeout);
|
||||
}
|
||||
void start();
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
void enableSSL(const std::string &certPath, const std::string &keyPath)
|
||||
{
|
||||
server_.enableSSL(certPath, keyPath);
|
||||
_server.enableSSL(certPath, keyPath);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -93,11 +93,11 @@ class HttpServer : trantor::NonCopyable
|
|||
void onRequest(const TcpConnectionPtr &, const HttpRequestPtr &);
|
||||
bool isWebSocket(const TcpConnectionPtr &conn, const HttpRequestPtr &req);
|
||||
void sendResponse(const TcpConnectionPtr &, const HttpResponsePtr &);
|
||||
trantor::TcpServer server_;
|
||||
HttpAsyncCallback httpAsyncCallback_;
|
||||
WebSocketNewAsyncCallback newWebsocketCallback_;
|
||||
WebSocketDisconnetCallback disconnectWebsocketCallback_;
|
||||
WebSocketMessageCallback webSocketMessageCallback_;
|
||||
trantor::TcpServer _server;
|
||||
HttpAsyncCallback _httpAsyncCallback;
|
||||
WebSocketNewAsyncCallback _newWebsocketCallback;
|
||||
WebSocketDisconnetCallback _disconnectWebsocketCallback;
|
||||
WebSocketMessageCallback _webSocketMessageCallback;
|
||||
trantor::ConnectionCallback _connectionCallback;
|
||||
};
|
||||
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
using namespace trantor;
|
||||
using namespace drogon;
|
||||
HttpServerContext::HttpServerContext(const trantor::TcpConnectionPtr &connPtr)
|
||||
: state_(kExpectRequestLine),
|
||||
request_(new HttpRequestImpl),
|
||||
: _state(kExpectRequestLine),
|
||||
_request(new HttpRequestImpl),
|
||||
_conn(connPtr)
|
||||
{
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ bool HttpServerContext::processRequestLine(const char *begin, const char *end)
|
|||
bool succeed = false;
|
||||
const char *start = begin;
|
||||
const char *space = std::find(start, end, ' ');
|
||||
if (space != end && request_->setMethod(start, space))
|
||||
if (space != end && _request->setMethod(start, space))
|
||||
{
|
||||
start = space + 1;
|
||||
space = std::find(start, end, ' ');
|
||||
|
@ -39,12 +39,12 @@ bool HttpServerContext::processRequestLine(const char *begin, const char *end)
|
|||
const char *question = std::find(start, space, '?');
|
||||
if (question != space)
|
||||
{
|
||||
request_->setPath(start, question);
|
||||
request_->setQuery(question + 1, space);
|
||||
_request->setPath(start, question);
|
||||
_request->setQuery(question + 1, space);
|
||||
}
|
||||
else
|
||||
{
|
||||
request_->setPath(start, space);
|
||||
_request->setPath(start, space);
|
||||
}
|
||||
start = space + 1;
|
||||
succeed = end - start == 8 && std::equal(start, end - 1, "HTTP/1.");
|
||||
|
@ -52,11 +52,11 @@ bool HttpServerContext::processRequestLine(const char *begin, const char *end)
|
|||
{
|
||||
if (*(end - 1) == '1')
|
||||
{
|
||||
request_->setVersion(HttpRequest::kHttp11);
|
||||
_request->setVersion(HttpRequest::kHttp11);
|
||||
}
|
||||
else if (*(end - 1) == '0')
|
||||
{
|
||||
request_->setVersion(HttpRequest::kHttp10);
|
||||
_request->setVersion(HttpRequest::kHttp10);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -76,7 +76,7 @@ bool HttpServerContext::parseRequest(MsgBuffer *buf)
|
|||
// std::cout<<std::string(buf->peek(),buf->readableBytes())<<std::endl;
|
||||
while (hasMore)
|
||||
{
|
||||
if (state_ == kExpectRequestLine)
|
||||
if (_state == kExpectRequestLine)
|
||||
{
|
||||
const char *crlf = buf->findCRLF();
|
||||
if (crlf)
|
||||
|
@ -84,9 +84,9 @@ bool HttpServerContext::parseRequest(MsgBuffer *buf)
|
|||
ok = processRequestLine(buf->peek(), crlf);
|
||||
if (ok)
|
||||
{
|
||||
//request_->setReceiveTime(receiveTime);
|
||||
//_request->setReceiveTime(receiveTime);
|
||||
buf->retrieveUntil(crlf + 2);
|
||||
state_ = kExpectHeaders;
|
||||
_state = kExpectHeaders;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -98,7 +98,7 @@ bool HttpServerContext::parseRequest(MsgBuffer *buf)
|
|||
hasMore = false;
|
||||
}
|
||||
}
|
||||
else if (state_ == kExpectHeaders)
|
||||
else if (_state == kExpectHeaders)
|
||||
{
|
||||
const char *crlf = buf->findCRLF();
|
||||
if (crlf)
|
||||
|
@ -106,20 +106,20 @@ bool HttpServerContext::parseRequest(MsgBuffer *buf)
|
|||
const char *colon = std::find(buf->peek(), crlf, ':');
|
||||
if (colon != crlf)
|
||||
{
|
||||
request_->addHeader(buf->peek(), colon, crlf);
|
||||
_request->addHeader(buf->peek(), colon, crlf);
|
||||
}
|
||||
else
|
||||
{
|
||||
// empty line, end of header
|
||||
std::string len = request_->getHeader("Content-Length");
|
||||
std::string len = _request->getHeader("Content-Length");
|
||||
LOG_TRACE << "content len=" << len;
|
||||
if (len != "")
|
||||
{
|
||||
request_->contentLen = atoi(len.c_str());
|
||||
state_ = kExpectBody;
|
||||
auto expect = request_->getHeader("Expect");
|
||||
_request->_contentLen = atoi(len.c_str());
|
||||
_state = kExpectBody;
|
||||
auto expect = _request->getHeader("Expect");
|
||||
if (expect == "100-continue" &&
|
||||
request_->getVersion() >= HttpRequest::kHttp11)
|
||||
_request->getVersion() >= HttpRequest::kHttp11)
|
||||
{
|
||||
//rfc2616-8.2.3
|
||||
//TODO:here we can add content-length limitation
|
||||
|
@ -152,7 +152,7 @@ bool HttpServerContext::parseRequest(MsgBuffer *buf)
|
|||
}
|
||||
else
|
||||
{
|
||||
state_ = kGotAll;
|
||||
_state = kGotAll;
|
||||
hasMore = false;
|
||||
}
|
||||
}
|
||||
|
@ -163,31 +163,31 @@ bool HttpServerContext::parseRequest(MsgBuffer *buf)
|
|||
hasMore = false;
|
||||
}
|
||||
}
|
||||
else if (state_ == kExpectBody)
|
||||
else if (_state == kExpectBody)
|
||||
{
|
||||
if (buf->readableBytes() == 0)
|
||||
{
|
||||
if (request_->contentLen == 0)
|
||||
if (_request->_contentLen == 0)
|
||||
{
|
||||
state_ = kGotAll;
|
||||
_state = kGotAll;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (request_->contentLen >= buf->readableBytes())
|
||||
if (_request->_contentLen >= buf->readableBytes())
|
||||
{
|
||||
request_->contentLen -= buf->readableBytes();
|
||||
request_->content_ += std::string(buf->peek(), buf->readableBytes());
|
||||
_request->_contentLen -= buf->readableBytes();
|
||||
_request->_content += std::string(buf->peek(), buf->readableBytes());
|
||||
buf->retrieveAll();
|
||||
}
|
||||
else
|
||||
{
|
||||
request_->content_ += std::string(buf->peek(), request_->contentLen);
|
||||
buf->retrieve(request_->contentLen);
|
||||
request_->contentLen = 0;
|
||||
_request->_content += std::string(buf->peek(), _request->_contentLen);
|
||||
buf->retrieve(_request->_contentLen);
|
||||
_request->_contentLen = 0;
|
||||
}
|
||||
if (request_->contentLen == 0)
|
||||
if (_request->_contentLen == 0)
|
||||
{
|
||||
state_ = kGotAll;
|
||||
_state = kGotAll;
|
||||
hasMore = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,28 +43,28 @@ class HttpServerContext
|
|||
|
||||
bool gotAll() const
|
||||
{
|
||||
return state_ == kGotAll;
|
||||
return _state == kGotAll;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
state_ = kExpectRequestLine;
|
||||
request_.reset(new HttpRequestImpl);
|
||||
_state = kExpectRequestLine;
|
||||
_request.reset(new HttpRequestImpl);
|
||||
}
|
||||
|
||||
const HttpRequestPtr request() const
|
||||
{
|
||||
return request_;
|
||||
return _request;
|
||||
}
|
||||
|
||||
HttpRequestPtr request()
|
||||
{
|
||||
return request_;
|
||||
return _request;
|
||||
}
|
||||
|
||||
HttpRequestImplPtr requestImpl()
|
||||
{
|
||||
return request_;
|
||||
return _request;
|
||||
}
|
||||
|
||||
bool firstReq()
|
||||
|
@ -95,8 +95,8 @@ class HttpServerContext
|
|||
private:
|
||||
bool processRequestLine(const char *begin, const char *end);
|
||||
|
||||
HttpRequestParseState state_;
|
||||
HttpRequestImplPtr request_;
|
||||
HttpRequestParseState _state;
|
||||
HttpRequestImplPtr _request;
|
||||
|
||||
bool _firstRequest = true;
|
||||
WebSocketConnectionPtr _websockConnPtr;
|
||||
|
|
Loading…
Reference in New Issue