summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-06-29 23:20:43 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2013-06-29 23:20:43 +0900
commitd128a39fb6d9ecf4f26af9858852bcd2ab8935dd (patch)
treea8685ca346e869e13e1ee8e6859fd59dd52c8fec /src
parent6a3e26a34d4e1c878e2d7d4ba2f72c362c5f8a3a (diff)
HttpServer: Return bool for receiveHeader, use std::unique_ptr for headers
Diffstat (limited to 'src')
-rw-r--r--src/HttpServer.cc9
-rw-r--r--src/HttpServer.h6
-rw-r--r--src/HttpServerBodyCommand.cc3
-rw-r--r--src/HttpServerCommand.cc9
4 files changed, 12 insertions, 15 deletions
diff --git a/src/HttpServer.cc b/src/HttpServer.cc
index 1532466a..5da8ec2d 100644
--- a/src/HttpServer.cc
+++ b/src/HttpServer.cc
@@ -126,7 +126,7 @@ const char* getStatusString(int status)
}
} // namespace
-std::shared_ptr<HttpHeader> HttpServer::receiveRequest()
+bool HttpServer::receiveRequest()
{
if(socketRecvBuffer_->bufferEmpty()) {
if(socketRecvBuffer_->recv() == 0 &&
@@ -134,14 +134,12 @@ std::shared_ptr<HttpHeader> HttpServer::receiveRequest()
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
}
}
- std::shared_ptr<HttpHeader> header;
if(headerProcessor_->parse(socketRecvBuffer_->getBuffer(),
socketRecvBuffer_->getBufferLength())) {
- header = headerProcessor_->getResult();
+ lastRequestHeader_ = headerProcessor_->getResult();
A2_LOG_INFO(fmt("HTTP Server received request\n%s",
headerProcessor_->getHeaderString().c_str()));
socketRecvBuffer_->shiftBuffer(headerProcessor_->getLastBytesProcessed());
- lastRequestHeader_ = header;
bodyConsumed_ = 0;
if(setupResponseRecv() < 0) {
A2_LOG_INFO("Request path is invaild. Ignore the request body.");
@@ -172,10 +170,11 @@ std::shared_ptr<HttpHeader> HttpServer::receiveRequest()
break;
}
}
+ return true;
} else {
socketRecvBuffer_->shiftBuffer(headerProcessor_->getLastBytesProcessed());
+ return false;
}
- return header;
}
bool HttpServer::receiveBody()
diff --git a/src/HttpServer.h b/src/HttpServer.h
index c0c8069d..0d1435aa 100644
--- a/src/HttpServer.h
+++ b/src/HttpServer.h
@@ -68,7 +68,7 @@ private:
std::shared_ptr<SocketRecvBuffer> socketRecvBuffer_;
SocketBuffer socketBuffer_;
std::unique_ptr<HttpHeaderProcessor> headerProcessor_;
- std::shared_ptr<HttpHeader> lastRequestHeader_;
+ std::unique_ptr<HttpHeader> lastRequestHeader_;
int64_t lastContentLength_;
// How many bytes are consumed. The total number of bytes is
// lastContentLength_.
@@ -87,7 +87,7 @@ public:
~HttpServer();
- std::shared_ptr<HttpHeader> receiveRequest();
+ bool receiveRequest();
bool receiveBody();
@@ -171,7 +171,7 @@ public:
return socket_;
}
- const std::shared_ptr<HttpHeader>& getRequestHeader() const
+ const std::unique_ptr<HttpHeader>& getRequestHeader() const
{
return lastRequestHeader_;
}
diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc
index dca204ec..13e327be 100644
--- a/src/HttpServerBodyCommand.cc
+++ b/src/HttpServerBodyCommand.cc
@@ -183,8 +183,7 @@ bool HttpServerBodyCommand::execute()
if(httpServer_->getMethod() == "OPTIONS") {
// Response to Preflight Request.
// See http://www.w3.org/TR/cors/
- const std::shared_ptr<HttpHeader>& header =
- httpServer_->getRequestHeader();
+ auto& header = httpServer_->getRequestHeader();
std::string accessControlHeaders;
if(!header->find(HttpHeader::ORIGIN).empty() &&
!header->find(HttpHeader::ACCESS_CONTROL_REQUEST_METHOD).empty()
diff --git a/src/HttpServerCommand.cc b/src/HttpServerCommand.cc
index 1784611b..106d1b97 100644
--- a/src/HttpServerCommand.cc
+++ b/src/HttpServerCommand.cc
@@ -137,7 +137,7 @@ std::string createWebSocketServerKey(const std::string& clientKey)
} // namespace
namespace {
-int websocketHandshake(const std::shared_ptr<HttpHeader>& header)
+int websocketHandshake(const HttpHeader* header)
{
if(header->getMethod() != "GET" ||
header->find(HttpHeader::SEC_WEBSOCKET_KEY).empty()) {
@@ -190,9 +190,7 @@ bool HttpServerCommand::execute()
}
#endif // ENABLE_SSL
- std::shared_ptr<HttpHeader> header;
- header = httpServer_->receiveRequest();
- if(!header) {
+ if(!httpServer_->receiveRequest()) {
updateWriteCheck();
e_->addCommand(std::unique_ptr<Command>(this));
return false;
@@ -209,10 +207,11 @@ bool HttpServerCommand::execute()
e_->setNoWait(true);
return true;
}
+ auto& header = httpServer_->getRequestHeader();
if(header->fieldContains(HttpHeader::UPGRADE, "websocket") &&
header->fieldContains(HttpHeader::CONNECTION, "upgrade")) {
#ifdef ENABLE_WEBSOCKET
- int status = websocketHandshake(header);
+ int status = websocketHandshake(header.get());
if(status == 101) {
std::string serverKey =
createWebSocketServerKey