summaryrefslogtreecommitdiff
path: root/src/HttpServerBodyCommand.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2015-12-27 18:39:47 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2015-12-27 18:40:08 +0900
commitb1132d6b10cd3791b9529d7031d723fd0e2d8215 (patch)
tree9ce465b42bf09372118335c7c619bb2b41869eaf /src/HttpServerBodyCommand.cc
parent4abad2f64c9ff80144a61c5a9c2bea144852b883 (diff)
make clang-format using clang-format-3.6
Diffstat (limited to 'src/HttpServerBodyCommand.cc')
-rw-r--r--src/HttpServerBodyCommand.cc184
1 files changed, 91 insertions, 93 deletions
diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc
index 2f7bc7da..2bb9d6d2 100644
--- a/src/HttpServerBodyCommand.cc
+++ b/src/HttpServerBodyCommand.cc
@@ -61,27 +61,25 @@
#include "JsonDiskWriter.h"
#include "ValueBaseJsonParser.h"
#ifdef ENABLE_XML_RPC
-# include "XmlRpcRequestParserStateMachine.h"
-# include "XmlRpcDiskWriter.h"
+#include "XmlRpcRequestParserStateMachine.h"
+#include "XmlRpcDiskWriter.h"
#endif // ENABLE_XML_RPC
namespace aria2 {
-HttpServerBodyCommand::HttpServerBodyCommand
-(cuid_t cuid,
- const std::shared_ptr<HttpServer>& httpServer,
- DownloadEngine* e,
- const std::shared_ptr<SocketCore>& socket)
- : Command(cuid),
- e_(e),
- socket_(socket),
- httpServer_(httpServer),
- writeCheck_(false)
+HttpServerBodyCommand::HttpServerBodyCommand(
+ cuid_t cuid, const std::shared_ptr<HttpServer>& httpServer,
+ DownloadEngine* e, const std::shared_ptr<SocketCore>& socket)
+ : Command(cuid),
+ e_(e),
+ socket_(socket),
+ httpServer_(httpServer),
+ writeCheck_(false)
{
// To handle Content-Length == 0 case
setStatus(Command::STATUS_ONESHOT_REALTIME);
e_->addSocketForReadCheck(socket_, this);
- if(!httpServer_->getSocketRecvBuffer()->bufferEmpty()) {
+ if (!httpServer_->getSocketRecvBuffer()->bufferEmpty()) {
e_->setNoWait(true);
}
}
@@ -89,7 +87,7 @@ HttpServerBodyCommand::HttpServerBodyCommand
HttpServerBodyCommand::~HttpServerBodyCommand()
{
e_->deleteSocketForReadCheck(socket_, this);
- if(writeCheck_) {
+ if (writeCheck_) {
e_->deleteSocketForWriteCheck(socket_, this);
}
}
@@ -101,20 +99,20 @@ std::string getJsonRpcContentType(bool script)
}
} // namespace
-void HttpServerBodyCommand::sendJsonRpcResponse
-(const rpc::RpcResponse& res,
- const std::string& callback)
+void HttpServerBodyCommand::sendJsonRpcResponse(const rpc::RpcResponse& res,
+ const std::string& callback)
{
bool notauthorized = rpc::not_authorized(res);
bool gzip = httpServer_->supportsGZip();
std::string responseData = rpc::toJson(res, callback, gzip);
- if(res.code == 0) {
+ if (res.code == 0) {
httpServer_->feedResponse(std::move(responseData),
getJsonRpcContentType(!callback.empty()));
- } else {
+ }
+ else {
httpServer_->disableKeepAlive();
int httpCode;
- switch(res.code) {
+ switch (res.code) {
case 1:
// error caught while executing RpcMethod
httpCode = 400;
@@ -128,16 +126,14 @@ void HttpServerBodyCommand::sendJsonRpcResponse
default:
httpCode = 500;
};
- httpServer_->feedResponse(httpCode, A2STR::NIL,
- std::move(responseData),
+ httpServer_->feedResponse(httpCode, A2STR::NIL, std::move(responseData),
getJsonRpcContentType(!callback.empty()));
}
addHttpServerResponseCommand(notauthorized);
}
-void HttpServerBodyCommand::sendJsonRpcBatchResponse
-(const std::vector<rpc::RpcResponse>& results,
- const std::string& callback)
+void HttpServerBodyCommand::sendJsonRpcBatchResponse(
+ const std::vector<rpc::RpcResponse>& results, const std::string& callback)
{
bool notauthorized = rpc::any_not_authorized(results.begin(), results.end());
bool gzip = httpServer_->supportsGZip();
@@ -149,11 +145,11 @@ void HttpServerBodyCommand::sendJsonRpcBatchResponse
void HttpServerBodyCommand::addHttpServerResponseCommand(bool delayed)
{
- auto resp =
- make_unique<HttpServerResponseCommand>(getCuid(), httpServer_, e_, socket_);
+ auto resp = make_unique<HttpServerResponseCommand>(getCuid(), httpServer_, e_,
+ socket_);
if (delayed) {
- e_->addCommand(make_unique<DelayedCommand>(
- getCuid(), e_, 1_s, std::move(resp), true));
+ e_->addCommand(
+ make_unique<DelayedCommand>(getCuid(), e_, 1_s, std::move(resp), true));
return;
}
@@ -163,12 +159,13 @@ void HttpServerBodyCommand::addHttpServerResponseCommand(bool delayed)
void HttpServerBodyCommand::updateWriteCheck()
{
- if(httpServer_->wantWrite()) {
- if(!writeCheck_) {
+ if (httpServer_->wantWrite()) {
+ if (!writeCheck_) {
writeCheck_ = true;
e_->addSocketForWriteCheck(socket_, this);
}
- } else if(writeCheck_) {
+ }
+ else if (writeCheck_) {
writeCheck_ = false;
e_->deleteSocketForWriteCheck(socket_, this);
}
@@ -176,38 +173,38 @@ void HttpServerBodyCommand::updateWriteCheck()
bool HttpServerBodyCommand::execute()
{
- if(e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) {
+ if (e_->getRequestGroupMan()->downloadFinished() || e_->isHaltRequested()) {
return true;
}
try {
- if(socket_->isReadable(0) ||
- (writeCheck_ && socket_->isWritable(0)) ||
- !httpServer_->getSocketRecvBuffer()->bufferEmpty() ||
- httpServer_->getContentLength() == 0) {
+ if (socket_->isReadable(0) || (writeCheck_ && socket_->isWritable(0)) ||
+ !httpServer_->getSocketRecvBuffer()->bufferEmpty() ||
+ httpServer_->getContentLength() == 0) {
timeoutTimer_ = global::wallclock();
- if(httpServer_->receiveBody()) {
+ if (httpServer_->receiveBody()) {
std::string reqPath = httpServer_->getRequestPath();
reqPath.erase(std::find(reqPath.begin(), reqPath.end(), '#'),
reqPath.end());
std::string query(std::find(reqPath.begin(), reqPath.end(), '?'),
reqPath.end());
- reqPath.erase(reqPath.size()-query.size(), query.size());
+ reqPath.erase(reqPath.size() - query.size(), query.size());
- if(httpServer_->getMethod() == "OPTIONS") {
+ if (httpServer_->getMethod() == "OPTIONS") {
// Response to Preflight Request.
// See http://www.w3.org/TR/cors/
auto& header = httpServer_->getRequestHeader();
std::string accessControlHeaders;
- if(!header->find(HttpHeader::ORIGIN).empty() &&
- !header->find(HttpHeader::ACCESS_CONTROL_REQUEST_METHOD).empty()
- && !httpServer_->getAllowOrigin().empty()) {
+ if (!header->find(HttpHeader::ORIGIN).empty() &&
+ !header->find(HttpHeader::ACCESS_CONTROL_REQUEST_METHOD)
+ .empty() &&
+ !httpServer_->getAllowOrigin().empty()) {
accessControlHeaders +=
- "Access-Control-Allow-Methods: POST, GET, OPTIONS\r\n"
- "Access-Control-Max-Age: 1728000\r\n";
+ "Access-Control-Allow-Methods: POST, GET, OPTIONS\r\n"
+ "Access-Control-Max-Age: 1728000\r\n";
const std::string& accReqHeaders =
- header->find(HttpHeader::ACCESS_CONTROL_REQUEST_HEADERS);
- if(!accReqHeaders.empty()) {
+ header->find(HttpHeader::ACCESS_CONTROL_REQUEST_HEADERS);
+ if (!accReqHeaders.empty()) {
// We allow all headers requested.
accessControlHeaders += "Access-Control-Allow-Headers: ";
accessControlHeaders += accReqHeaders;
@@ -220,22 +217,21 @@ bool HttpServerBodyCommand::execute()
}
// Do something for requestpath and body
- switch(httpServer_->getRequestType()) {
+ switch (httpServer_->getRequestType()) {
case RPC_TYPE_XML: {
#ifdef ENABLE_XML_RPC
- auto dw = static_cast<rpc::XmlRpcDiskWriter*>
- (httpServer_->getBody());
+ auto dw = static_cast<rpc::XmlRpcDiskWriter*>(httpServer_->getBody());
int error;
error = dw->finalize();
rpc::RpcRequest req;
- if(error == 0) {
+ if (error == 0) {
req = dw->getResult();
}
dw->reset();
- if(error < 0) {
- A2_LOG_INFO
- (fmt("CUID#%" PRId64 " - Failed to parse XML-RPC request",
- getCuid()));
+ if (error < 0) {
+ A2_LOG_INFO(fmt("CUID#%" PRId64
+ " - Failed to parse XML-RPC request",
+ getCuid()));
httpServer_->feedResponse(400);
addHttpServerResponseCommand(false);
return true;
@@ -247,7 +243,7 @@ bool HttpServerBodyCommand::execute()
std::string responseData = rpc::toXml(res, gzip);
httpServer_->feedResponse(std::move(responseData), "text/xml");
addHttpServerResponseCommand(false);
-#else // !ENABLE_XML_RPC
+#else // !ENABLE_XML_RPC
httpServer_->feedResponse(404);
addHttpServerResponseCommand(false);
#endif // !ENABLE_XML_RPC
@@ -258,56 +254,55 @@ bool HttpServerBodyCommand::execute()
std::string callback;
std::unique_ptr<ValueBase> json;
ssize_t error = 0;
- if(httpServer_->getRequestType() == RPC_TYPE_JSONP) {
+ if (httpServer_->getRequestType() == RPC_TYPE_JSONP) {
json::JsonGetParam param = json::decodeGetParams(query);
callback = param.callback;
ssize_t error = 0;
- json = json::ValueBaseJsonParser().parseFinal
- (param.request.c_str(),
- param.request.size(),
- error);
- } else {
- auto dw = static_cast<json::JsonDiskWriter*>
- (httpServer_->getBody());
+ json = json::ValueBaseJsonParser().parseFinal(
+ param.request.c_str(), param.request.size(), error);
+ }
+ else {
+ auto dw =
+ static_cast<json::JsonDiskWriter*>(httpServer_->getBody());
error = dw->finalize();
- if(error == 0) {
+ if (error == 0) {
json = dw->getResult();
}
dw->reset();
}
- if(error < 0) {
- A2_LOG_INFO
- (fmt("CUID#%" PRId64 " - Failed to parse JSON-RPC request",
- getCuid()));
- rpc::RpcResponse res
- (rpc::createJsonRpcErrorResponse(-32700, "Parse error.",
- Null::g()));
+ if (error < 0) {
+ A2_LOG_INFO(fmt("CUID#%" PRId64
+ " - Failed to parse JSON-RPC request",
+ getCuid()));
+ rpc::RpcResponse res(rpc::createJsonRpcErrorResponse(
+ -32700, "Parse error.", Null::g()));
sendJsonRpcResponse(res, callback);
return true;
}
Dict* jsondict = downcast<Dict>(json);
- if(jsondict) {
+ if (jsondict) {
auto res = rpc::processJsonRpcRequest(jsondict, e_);
sendJsonRpcResponse(res, callback);
- } else {
+ }
+ else {
List* jsonlist = downcast<List>(json);
- if(jsonlist) {
+ if (jsonlist) {
// This is batch call
std::vector<rpc::RpcResponse> results;
- for(List::ValueType::const_iterator i = jsonlist->begin(),
- eoi = jsonlist->end(); i != eoi; ++i) {
+ for (List::ValueType::const_iterator i = jsonlist->begin(),
+ eoi = jsonlist->end();
+ i != eoi; ++i) {
Dict* jsondict = downcast<Dict>(*i);
if (jsondict) {
- auto resp =
- rpc::processJsonRpcRequest(jsondict, e_);
+ auto resp = rpc::processJsonRpcRequest(jsondict, e_);
results.push_back(std::move(resp));
}
}
sendJsonRpcBatchResponse(results, callback);
- } else {
- rpc::RpcResponse res
- (rpc::createJsonRpcErrorResponse
- (-32600, "Invalid Request.", Null::g()));
+ }
+ else {
+ rpc::RpcResponse res(rpc::createJsonRpcErrorResponse(
+ -32600, "Invalid Request.", Null::g()));
sendJsonRpcResponse(res, callback);
}
}
@@ -318,28 +313,31 @@ bool HttpServerBodyCommand::execute()
addHttpServerResponseCommand(false);
return true;
}
- } else {
+ }
+ else {
updateWriteCheck();
e_->addCommand(std::unique_ptr<Command>(this));
return false;
}
- } else {
- if(timeoutTimer_.difference(global::wallclock()) >= 30_s) {
+ }
+ else {
+ if (timeoutTimer_.difference(global::wallclock()) >= 30_s) {
A2_LOG_INFO("HTTP request body timeout.");
return true;
- } else {
+ }
+ else {
e_->addCommand(std::unique_ptr<Command>(this));
return false;
}
}
- } catch(RecoverableException& e) {
- A2_LOG_INFO_EX
- (fmt("CUID#%" PRId64 " - Error occurred while reading HTTP request body",
- getCuid()),
- e);
+ }
+ catch (RecoverableException& e) {
+ A2_LOG_INFO_EX(fmt("CUID#%" PRId64
+ " - Error occurred while reading HTTP request body",
+ getCuid()),
+ e);
return true;
}
-
}
} // namespace aria2