From cf1c49aa3aa064de5b1759efe2890ea80cfb6a55 Mon Sep 17 00:00:00 2001 From: Gio Date: Thu, 22 Sep 2016 12:48:08 +0200 Subject: [PATCH] Advances on trusted node addings ApiServerLocal trim method/path line to avoid white spaces parsing ApiServerLocal add support for passing METHOD in request AddTrustedNode.qml add ability to copy and paste keys --- libresapi/src/api/ApiServerLocal.cpp | 27 +++++++---- libresapi/src/api/ApiServerLocal.h | 2 + libresapi/src/api/ApiServerMHD.cpp | 25 +--------- libresapi/src/api/ApiTypes.h | 29 ++++-------- retroshare-qml-app/src/qml/AddTrustedNode.qml | 47 +++++++++++++++++-- 5 files changed, 76 insertions(+), 54 deletions(-) diff --git a/libresapi/src/api/ApiServerLocal.cpp b/libresapi/src/api/ApiServerLocal.cpp index 1755a2d9c..d77cd7633 100644 --- a/libresapi/src/api/ApiServerLocal.cpp +++ b/libresapi/src/api/ApiServerLocal.cpp @@ -74,15 +74,25 @@ void ApiLocalConnectionHandler::handlePendingRequests() if(mLocalSocket->canReadLine()) { readPath: - reqPath = mLocalSocket->readLine().constData(); - mState = WAITING_DATA; + QString rString(mLocalSocket->readLine()); + rString = rString.simplified(); + if (!rString.isEmpty()) + { + if(rString.startsWith("PUT", Qt::CaseInsensitive)) reqMeth = resource_api::Request::PUT; + else if (rString.startsWith("DELETE", Qt::CaseInsensitive)) reqMeth = resource_api::Request::DELETE_AA; + if(rString.contains(' ')) rString = rString.split(' ')[1]; - /* Because QLocalSocket is SOCK_STREAM some clients implementations - * like the one based on QLocalSocket feel free to send the whole - * request (PATH + DATA) in a single write(), causing readyRead() - * signal being emitted only once, in that case we should continue - * processing without waiting for readyRead() being fired again, so - * we don't break here as there may be more lines to read */ + reqPath = rString.toStdString(); + mState = WAITING_DATA; + + /* Because QLocalSocket is SOCK_STREAM some clients implementations + * like the one based on QLocalSocket feel free to send the whole + * request (PATH + DATA) in a single write(), causing readyRead() + * signal being emitted only once, in that case we should continue + * processing without waiting for readyRead() being fired again, so + * we don't break here as there may be more lines to read */ + } + else break; } } case WAITING_DATA: @@ -92,6 +102,7 @@ void ApiLocalConnectionHandler::handlePendingRequests() resource_api::JsonStream reqJson; reqJson.setJsonString(std::string(mLocalSocket->readLine().constData())); resource_api::Request req(reqJson); + req.mMethod = reqMeth; req.setPath(reqPath); std::string resultString = mApiServer->handleRequest(req); mLocalSocket->write(resultString.c_str(), resultString.length()); diff --git a/libresapi/src/api/ApiServerLocal.h b/libresapi/src/api/ApiServerLocal.h index 576190a71..1d6c87ae5 100644 --- a/libresapi/src/api/ApiServerLocal.h +++ b/libresapi/src/api/ApiServerLocal.h @@ -24,6 +24,7 @@ #include #include +#include "ApiTypes.h" #include "ApiServer.h" namespace resource_api @@ -89,6 +90,7 @@ private: QLocalSocket* mLocalSocket; State mState; std::string reqPath; + resource_api::Request::Method reqMeth; }; } // namespace resource_api diff --git a/libresapi/src/api/ApiServerMHD.cpp b/libresapi/src/api/ApiServerMHD.cpp index 9522c66be..273010d8b 100644 --- a/libresapi/src/api/ApiServerMHD.cpp +++ b/libresapi/src/api/ApiServerMHD.cpp @@ -211,30 +211,7 @@ public: req.mMethod = resource_api::Request::DELETE_AA; } - std::stack stack; - std::string str; - for(std::string::reverse_iterator sit = path2.rbegin(); sit != path2.rend(); sit++) - { - if((*sit) != '/') - { - // add to front because we are traveling in reverse order - str = *sit + str; - } - else - { - if(str != "") - { - stack.push(str); - str.clear(); - } - } - } - if(str != "") - { - stack.push(str); - } - req.mPath = stack; - req.mFullPath = path2; + req.setPath(path2); std::string result = mApiServer->handleRequest(req); diff --git a/libresapi/src/api/ApiTypes.h b/libresapi/src/api/ApiTypes.h index f5ab6094b..98ad44cd9 100644 --- a/libresapi/src/api/ApiTypes.h +++ b/libresapi/src/api/ApiTypes.h @@ -193,29 +193,21 @@ public: // then each handler should pop the top element std::stack mPath; std::string mFullPath; - bool setPath(std::string reqPath) + bool setPath(const std::string &reqPath) { std::string str; - for(std::string::reverse_iterator sit = reqPath.rbegin(); sit != reqPath.rend(); sit++) + std::string::const_reverse_iterator sit; + for( sit = reqPath.rbegin(); sit != reqPath.rend(); ++sit ) { - if((*sit) != '/') + // add to front because we are traveling in reverse order + if((*sit) != '/') str = *sit + str; + else if(!str.empty()) { - // add to front because we are traveling in reverse order - str = *sit + str; - } - else - { - if(str != "") - { - mPath.push(str); - str.clear(); - } + mPath.push(str); + str.clear(); } } - if(str != "") - { - mPath.push(str); - } + if(!str.empty()) mPath.push(str); mFullPath = reqPath; return true; @@ -231,8 +223,7 @@ public: // contains data for new resources StreamBase& mStream; - // use the is*() methods to query the method type -//private: + // use the is*() methods to query the method type: enum Method { GET, PUT, DELETE_AA, EXEC};// something is wrong with DELETE, it won't compile with it Method mMethod; }; diff --git a/retroshare-qml-app/src/qml/AddTrustedNode.qml b/retroshare-qml-app/src/qml/AddTrustedNode.qml index 37b8efc9c..0d44ab46d 100644 --- a/retroshare-qml-app/src/qml/AddTrustedNode.qml +++ b/retroshare-qml-app/src/qml/AddTrustedNode.qml @@ -27,13 +27,41 @@ Item ColumnLayout { + id: colLayout anchors.top: parent.top - anchors.bottom: bottomButton.top + anchors.bottom: rowLayout.top - Text { id: myKeyField } + + TextField { id: myKeyField } TextField { id: otherKeyField } } + RowLayout + { + id: rowLayout + anchors.top: colLayout.bottom + + Button + { + text: "Copy" + onClicked: + { + myKeyField.selectAll() + myKeyField.copy() + } + } + + Button + { + text: "Paste" + onClicked: + { + otherKeyField.selectAll() + otherKeyField.paste() + } + } + } + Button { id: bottomButton @@ -41,7 +69,20 @@ Item anchors.bottom: parent.bottom onClicked: { - rsApi.request("/peers/examine_cert/", JSON.stringify({ cert_string: otherKeyField.text })) + console.log("retroshare addtrusted: ", otherKeyField.text) + var jsonData = + { + cert_string: otherKeyField.text, + flags: + { + allow_direct_download: true, + allow_push: false, + require_whitelist: false, + } + } + console.log("retroshare addtrusted jsonData: ", JSON.stringify(jsonData)) + //rsApi.request("/peers/examine_cert/", JSON.stringify({ cert_string: otherKeyField.text })) + rsApi.request("POST /peers", JSON.stringify(jsonData)) } } }