mirror of https://github.com/aria2/aria2
HttpServer: Return bool for receiveHeader, use std::unique_ptr for headers
parent
6a3e26a34d
commit
d128a39fb6
|
@ -126,7 +126,7 @@ const char* getStatusString(int status)
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
std::shared_ptr<HttpHeader> HttpServer::receiveRequest()
|
bool HttpServer::receiveRequest()
|
||||||
{
|
{
|
||||||
if(socketRecvBuffer_->bufferEmpty()) {
|
if(socketRecvBuffer_->bufferEmpty()) {
|
||||||
if(socketRecvBuffer_->recv() == 0 &&
|
if(socketRecvBuffer_->recv() == 0 &&
|
||||||
|
@ -134,14 +134,12 @@ std::shared_ptr<HttpHeader> HttpServer::receiveRequest()
|
||||||
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
throw DL_ABORT_EX(EX_EOF_FROM_PEER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::shared_ptr<HttpHeader> header;
|
|
||||||
if(headerProcessor_->parse(socketRecvBuffer_->getBuffer(),
|
if(headerProcessor_->parse(socketRecvBuffer_->getBuffer(),
|
||||||
socketRecvBuffer_->getBufferLength())) {
|
socketRecvBuffer_->getBufferLength())) {
|
||||||
header = headerProcessor_->getResult();
|
lastRequestHeader_ = headerProcessor_->getResult();
|
||||||
A2_LOG_INFO(fmt("HTTP Server received request\n%s",
|
A2_LOG_INFO(fmt("HTTP Server received request\n%s",
|
||||||
headerProcessor_->getHeaderString().c_str()));
|
headerProcessor_->getHeaderString().c_str()));
|
||||||
socketRecvBuffer_->shiftBuffer(headerProcessor_->getLastBytesProcessed());
|
socketRecvBuffer_->shiftBuffer(headerProcessor_->getLastBytesProcessed());
|
||||||
lastRequestHeader_ = header;
|
|
||||||
bodyConsumed_ = 0;
|
bodyConsumed_ = 0;
|
||||||
if(setupResponseRecv() < 0) {
|
if(setupResponseRecv() < 0) {
|
||||||
A2_LOG_INFO("Request path is invaild. Ignore the request body.");
|
A2_LOG_INFO("Request path is invaild. Ignore the request body.");
|
||||||
|
@ -172,10 +170,11 @@ std::shared_ptr<HttpHeader> HttpServer::receiveRequest()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
socketRecvBuffer_->shiftBuffer(headerProcessor_->getLastBytesProcessed());
|
socketRecvBuffer_->shiftBuffer(headerProcessor_->getLastBytesProcessed());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return header;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HttpServer::receiveBody()
|
bool HttpServer::receiveBody()
|
||||||
|
|
|
@ -68,7 +68,7 @@ private:
|
||||||
std::shared_ptr<SocketRecvBuffer> socketRecvBuffer_;
|
std::shared_ptr<SocketRecvBuffer> socketRecvBuffer_;
|
||||||
SocketBuffer socketBuffer_;
|
SocketBuffer socketBuffer_;
|
||||||
std::unique_ptr<HttpHeaderProcessor> headerProcessor_;
|
std::unique_ptr<HttpHeaderProcessor> headerProcessor_;
|
||||||
std::shared_ptr<HttpHeader> lastRequestHeader_;
|
std::unique_ptr<HttpHeader> lastRequestHeader_;
|
||||||
int64_t lastContentLength_;
|
int64_t lastContentLength_;
|
||||||
// How many bytes are consumed. The total number of bytes is
|
// How many bytes are consumed. The total number of bytes is
|
||||||
// lastContentLength_.
|
// lastContentLength_.
|
||||||
|
@ -87,7 +87,7 @@ public:
|
||||||
|
|
||||||
~HttpServer();
|
~HttpServer();
|
||||||
|
|
||||||
std::shared_ptr<HttpHeader> receiveRequest();
|
bool receiveRequest();
|
||||||
|
|
||||||
bool receiveBody();
|
bool receiveBody();
|
||||||
|
|
||||||
|
@ -171,7 +171,7 @@ public:
|
||||||
return socket_;
|
return socket_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::shared_ptr<HttpHeader>& getRequestHeader() const
|
const std::unique_ptr<HttpHeader>& getRequestHeader() const
|
||||||
{
|
{
|
||||||
return lastRequestHeader_;
|
return lastRequestHeader_;
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,8 +183,7 @@ bool HttpServerBodyCommand::execute()
|
||||||
if(httpServer_->getMethod() == "OPTIONS") {
|
if(httpServer_->getMethod() == "OPTIONS") {
|
||||||
// Response to Preflight Request.
|
// Response to Preflight Request.
|
||||||
// See http://www.w3.org/TR/cors/
|
// See http://www.w3.org/TR/cors/
|
||||||
const std::shared_ptr<HttpHeader>& header =
|
auto& header = httpServer_->getRequestHeader();
|
||||||
httpServer_->getRequestHeader();
|
|
||||||
std::string accessControlHeaders;
|
std::string accessControlHeaders;
|
||||||
if(!header->find(HttpHeader::ORIGIN).empty() &&
|
if(!header->find(HttpHeader::ORIGIN).empty() &&
|
||||||
!header->find(HttpHeader::ACCESS_CONTROL_REQUEST_METHOD).empty()
|
!header->find(HttpHeader::ACCESS_CONTROL_REQUEST_METHOD).empty()
|
||||||
|
|
|
@ -137,7 +137,7 @@ std::string createWebSocketServerKey(const std::string& clientKey)
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
int websocketHandshake(const std::shared_ptr<HttpHeader>& header)
|
int websocketHandshake(const HttpHeader* header)
|
||||||
{
|
{
|
||||||
if(header->getMethod() != "GET" ||
|
if(header->getMethod() != "GET" ||
|
||||||
header->find(HttpHeader::SEC_WEBSOCKET_KEY).empty()) {
|
header->find(HttpHeader::SEC_WEBSOCKET_KEY).empty()) {
|
||||||
|
@ -190,9 +190,7 @@ bool HttpServerCommand::execute()
|
||||||
}
|
}
|
||||||
#endif // ENABLE_SSL
|
#endif // ENABLE_SSL
|
||||||
|
|
||||||
std::shared_ptr<HttpHeader> header;
|
if(!httpServer_->receiveRequest()) {
|
||||||
header = httpServer_->receiveRequest();
|
|
||||||
if(!header) {
|
|
||||||
updateWriteCheck();
|
updateWriteCheck();
|
||||||
e_->addCommand(std::unique_ptr<Command>(this));
|
e_->addCommand(std::unique_ptr<Command>(this));
|
||||||
return false;
|
return false;
|
||||||
|
@ -209,10 +207,11 @@ bool HttpServerCommand::execute()
|
||||||
e_->setNoWait(true);
|
e_->setNoWait(true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
auto& header = httpServer_->getRequestHeader();
|
||||||
if(header->fieldContains(HttpHeader::UPGRADE, "websocket") &&
|
if(header->fieldContains(HttpHeader::UPGRADE, "websocket") &&
|
||||||
header->fieldContains(HttpHeader::CONNECTION, "upgrade")) {
|
header->fieldContains(HttpHeader::CONNECTION, "upgrade")) {
|
||||||
#ifdef ENABLE_WEBSOCKET
|
#ifdef ENABLE_WEBSOCKET
|
||||||
int status = websocketHandshake(header);
|
int status = websocketHandshake(header.get());
|
||||||
if(status == 101) {
|
if(status == 101) {
|
||||||
std::string serverKey =
|
std::string serverKey =
|
||||||
createWebSocketServerKey
|
createWebSocketServerKey
|
||||||
|
|
Loading…
Reference in New Issue