本文整理汇总了C++中ApiRequest::setResponseErrorStr方法的典型用法代码示例。如果您正苦于以下问题:C++ ApiRequest::setResponseErrorStr方法的具体用法?C++ ApiRequest::setResponseErrorStr怎么用?C++ ApiRequest::setResponseErrorStr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ApiRequest
的用法示例。
在下文中一共展示了ApiRequest::setResponseErrorStr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: handleRequest
api_return ApiRouter::handleRequest(ApiRequest& aRequest, bool aIsSecure, const WebSocketPtr& aSocket, const string& aIp) noexcept {
int code;
try {
// Special case because we may not have the session yet
if (aRequest.getApiModule() == "session") {
return handleSessionRequest(aRequest, aIsSecure, aSocket, aIp);
}
// Require auth for all other modules
if (!aRequest.getSession()) {
aRequest.setResponseErrorStr("Not authorized");
return websocketpp::http::status_code::unauthorized;
}
// Require using the same protocol that was used for logging in
if (aRequest.getSession()->isSecure() != aIsSecure) {
aRequest.setResponseErrorStr("Protocol mismatch");
return websocketpp::http::status_code::not_acceptable;
}
aRequest.getSession()->updateActivity();
code = aRequest.getSession()->handleRequest(aRequest);
} catch (const ArgumentException& e) {
aRequest.setResponseErrorJson(e.getErrorJson());
code = CODE_UNPROCESSABLE_ENTITY;
} catch (const std::exception& e) {
aRequest.setResponseErrorStr(e.what());
code = websocketpp::http::status_code::bad_request;
}
return static_cast<api_return>(code);
}
示例2: handleRequest
api_return ApiModule::handleRequest(ApiRequest& aRequest) {
// Find section
auto i = requestHandlers.find(aRequest.getStringParam(0));
if (i == requestHandlers.end()) {
aRequest.setResponseErrorStr("Invalid API section");
return websocketpp::http::status_code::bad_request;
}
aRequest.popParam();
const auto& sectionHandlers = i->second;
bool hasParamMatch = false; // for better error reporting
// Match parameters
auto handler = boost::find_if(sectionHandlers, [&](const RequestHandler& aHandler) {
// Regular matching
auto matchesParams = aHandler.matchParams(aRequest.getParameters());
if (!matchesParams) {
return false;
}
if (aHandler.method == aRequest.getMethod() || aHandler.isModuleHandler()) {
return true;
}
hasParamMatch = true;
return false;
});
if (handler == sectionHandlers.end()) {
if (hasParamMatch) {
aRequest.setResponseErrorStr("Method not supported for this command");
} else {
aRequest.setResponseErrorStr("Invalid parameters for this API section");
}
return websocketpp::http::status_code::bad_request;
}
// Check JSON payload
if (handler->requireJson && !aRequest.hasRequestBody()) {
aRequest.setResponseErrorStr("JSON body required");
return websocketpp::http::status_code::bad_request;
}
// Check permission
if (!session->getUser()->hasPermission(handler->access)) {
aRequest.setResponseErrorStr("Permission denied");
return websocketpp::http::status_code::forbidden;
}
// Exact params could be removed from the request...
return handler->f(aRequest);
}
示例3:
websocketpp::http::status_code::value Session::handleRequest(ApiRequest& aRequest) {
auto h = apiHandlers.find(aRequest.getApiModule());
if (h != apiHandlers.end()) {
if (aRequest.getApiVersion() != h->second->getVersion()) {
aRequest.setResponseErrorStr("Invalid API version");
return websocketpp::http::status_code::precondition_failed;
}
return h->second->handleRequest(aRequest);
}
aRequest.setResponseErrorStr("Section not found");
return websocketpp::http::status_code::not_found;
}
示例4: handleActivity
api_return SessionApi::handleActivity(ApiRequest& aRequest) {
auto s = aRequest.getSession();
if (!s) {
aRequest.setResponseErrorStr("Not authorized");
return websocketpp::http::status_code::unauthorized;
}
if (!s->isUserSession()) {
aRequest.setResponseErrorStr("Activity can only be updated for user sessions");
return websocketpp::http::status_code::bad_request;
}
ActivityManager::getInstance()->updateActivity();
return websocketpp::http::status_code::ok;
}
示例5: handleSubscribe
api_return ApiModule::handleSubscribe(ApiRequest& aRequest) {
if (!socket) {
aRequest.setResponseErrorStr("Socket required");
return websocketpp::http::status_code::precondition_required;
}
const auto& subscription = aRequest.getStringParam(0);
if (!subscriptionExists(subscription)) {
aRequest.setResponseErrorStr("No such subscription: " + subscription);
return websocketpp::http::status_code::not_found;
}
setSubscriptionState(subscription, true);
return websocketpp::http::status_code::ok;
}
示例6: handleAddFileBundle
api_return QueueApi::handleAddFileBundle(ApiRequest& aRequest) {
const auto& reqJson = aRequest.getRequestBody();
string targetDirectory, targetFileName;
TargetUtil::TargetType targetType;
QueueItemBase::Priority prio;
Deserializer::deserializeDownloadParams(aRequest.getRequestBody(), targetDirectory, targetFileName, targetType, prio);
BundlePtr b = nullptr;
try {
b = QueueManager::getInstance()->createFileBundle(
targetDirectory + targetFileName,
JsonUtil::getField<int64_t>("size", reqJson, false),
Deserializer::deserializeTTH(reqJson),
Deserializer::deserializeHintedUser(reqJson),
JsonUtil::getField<time_t>("time", reqJson, false),
0,
prio
);
}
catch (const Exception& e) {
aRequest.setResponseErrorStr(e.getError());
return websocketpp::http::status_code::internal_server_error;
}
if (b) {
json retJson = {
{ "id", b->getToken() }
};
aRequest.setResponseBody(retJson);
}
return websocketpp::http::status_code::ok;
}
示例7: WEBCFG
websocketpp::http::status_code::value SessionApi::handleLogin(ApiRequest& aRequest, bool aIsSecure, const WebSocketPtr& aSocket, const string& aIP) {
const auto& reqJson = aRequest.getRequestBody();
auto username = JsonUtil::getField<string>("username", reqJson, false);
auto password = JsonUtil::getField<string>("password", reqJson, false);
auto inactivityMinutes = JsonUtil::getOptionalFieldDefault<uint64_t>("max_inactivity", reqJson, WEBCFG(DEFAULT_SESSION_IDLE_TIMEOUT).uint64());
auto userSession = JsonUtil::getOptionalFieldDefault<bool>("user_session", reqJson, false);
auto session = WebServerManager::getInstance()->getUserManager().authenticate(username, password,
aIsSecure, inactivityMinutes, userSession, aIP);
if (!session) {
aRequest.setResponseErrorStr("Invalid username or password");
return websocketpp::http::status_code::unauthorized;
}
json retJson = {
{ "permissions", session->getUser()->getPermissions() },
{ "token", session->getAuthToken() },
{ "user", session->getUser()->getUserName() },
{ "system", getSystemInfo(aIP) },
{ "run_wizard", SETTING(WIZARD_RUN) },
{ "cid", ClientManager::getInstance()->getMyCID().toBase32() },
};
if (aSocket) {
session->onSocketConnected(aSocket);
aSocket->setSession(session);
}
aRequest.setResponseBody(retJson);
return websocketpp::http::status_code::ok;
}
示例8: handleAddFileBundle
api_return QueueApi::handleAddFileBundle(ApiRequest& aRequest) {
const auto& reqJson = aRequest.getRequestBody();
string targetDirectory, targetFileName;
Priority prio;
Deserializer::deserializeDownloadParams(aRequest.getRequestBody(), aRequest.getSession(), targetDirectory, targetFileName, prio);
BundleAddInfo bundleAddInfo;
try {
bundleAddInfo = QueueManager::getInstance()->createFileBundle(
targetDirectory + targetFileName,
JsonUtil::getField<int64_t>("size", reqJson, false),
Deserializer::deserializeTTH(reqJson),
Deserializer::deserializeHintedUser(reqJson),
JsonUtil::getField<time_t>("time", reqJson, false),
0,
prio
);
} catch (const Exception& e) {
aRequest.setResponseErrorStr(e.getError());
return websocketpp::http::status_code::internal_server_error;
}
aRequest.setResponseBody(Serializer::serializeBundleAddInfo(bundleAddInfo));
return websocketpp::http::status_code::ok;
}
示例9: handlePostDirectory
api_return FilesystemApi::handlePostDirectory(ApiRequest& aRequest) {
const auto& reqJson = aRequest.getRequestBody();
auto path = JsonUtil::getField<string>("path", reqJson, false);
try {
if (!File::createDirectory(path)) {
aRequest.setResponseErrorStr("Directory exists");
return websocketpp::http::status_code::bad_request;
}
} catch (const FileException& e) {
aRequest.setResponseErrorStr("Failed to create directory: " + e.getError());
return websocketpp::http::status_code::internal_server_error;
}
return websocketpp::http::status_code::ok;
}
示例10: handleFavorite
api_return HubInfo::handleFavorite(ApiRequest& aRequest) {
if (!client->saveFavorite()) {
aRequest.setResponseErrorStr(STRING(FAVORITE_HUB_ALREADY_EXISTS));
return websocketpp::http::status_code::bad_request;
}
return websocketpp::http::status_code::ok;
}
示例11: handleRemoveFile
api_return QueueApi::handleRemoveFile(ApiRequest& aRequest) {
if (QueueManager::getInstance()->removeFile(aRequest.getTokenParam(0), false)) {
aRequest.setResponseErrorStr("File not found");
return websocketpp::http::status_code::bad_request;
}
return websocketpp::http::status_code::ok;
}
示例12: handleRemoveFile
api_return ViewFileApi::handleRemoveFile(ApiRequest& aRequest) {
auto success = ViewFileManager::getInstance()->removeFile(Deserializer::parseTTH(aRequest.getStringParam(0)));
if (!success) {
aRequest.setResponseErrorStr("File not found");
return websocketpp::http::status_code::not_found;
}
return websocketpp::http::status_code::ok;
}
示例13: handleRemoveFile
api_return QueueApi::handleRemoveFile(ApiRequest& aRequest) {
auto path = JsonUtil::getField<string>("target", aRequest.getRequestBody(), false);
if (!QueueManager::getInstance()->removeFile(path, false)) {
aRequest.setResponseErrorStr("File not found");
return websocketpp::http::status_code::bad_request;
}
return websocketpp::http::status_code::ok;
}
示例14: handleUpdateProfile
api_return ShareProfileApi::handleUpdateProfile(ApiRequest& aRequest) {
const auto& reqJson = aRequest.getRequestBody();
auto token = aRequest.getTokenParam(0);
if (token == SP_HIDDEN) {
aRequest.setResponseErrorStr("Hidden profile can't be edited");
return websocketpp::http::status_code::not_found;
}
auto profile = ShareManager::getInstance()->getShareProfile(token);
if (!profile) {
aRequest.setResponseErrorStr("Profile not found");
return websocketpp::http::status_code::not_found;
}
parseProfile(profile, reqJson);
ShareManager::getInstance()->updateProfile(profile);
return websocketpp::http::status_code::ok;
}
示例15: handleSocketConnect
api_return SessionApi::handleSocketConnect(ApiRequest& aRequest, bool aIsSecure, const WebSocketPtr& aSocket) {
auto sessionToken = JsonUtil::getField<string>("authorization", aRequest.getRequestBody(), false);
auto session = WebServerManager::getInstance()->getUserManager().getSession(sessionToken);
if (!session) {
aRequest.setResponseErrorStr("Invalid session token");
return websocketpp::http::status_code::bad_request;
}
if (session->isSecure() != aIsSecure) {
aRequest.setResponseErrorStr("Invalid protocol");
return websocketpp::http::status_code::bad_request;
}
session->onSocketConnected(aSocket);
aSocket->setSession(session);
return websocketpp::http::status_code::ok;
}