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
This commit is contained in:
Gio 2016-09-22 12:48:08 +02:00
parent 48a9be0ccc
commit cf1c49aa3a
5 changed files with 76 additions and 54 deletions

View File

@ -74,15 +74,25 @@ void ApiLocalConnectionHandler::handlePendingRequests()
if(mLocalSocket->canReadLine()) if(mLocalSocket->canReadLine())
{ {
readPath: readPath:
reqPath = mLocalSocket->readLine().constData(); QString rString(mLocalSocket->readLine());
mState = WAITING_DATA; 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 reqPath = rString.toStdString();
* like the one based on QLocalSocket feel free to send the whole mState = WAITING_DATA;
* request (PATH + DATA) in a single write(), causing readyRead()
* signal being emitted only once, in that case we should continue /* Because QLocalSocket is SOCK_STREAM some clients implementations
* processing without waiting for readyRead() being fired again, so * like the one based on QLocalSocket feel free to send the whole
* we don't break here as there may be more lines to read */ * 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: case WAITING_DATA:
@ -92,6 +102,7 @@ void ApiLocalConnectionHandler::handlePendingRequests()
resource_api::JsonStream reqJson; resource_api::JsonStream reqJson;
reqJson.setJsonString(std::string(mLocalSocket->readLine().constData())); reqJson.setJsonString(std::string(mLocalSocket->readLine().constData()));
resource_api::Request req(reqJson); resource_api::Request req(reqJson);
req.mMethod = reqMeth;
req.setPath(reqPath); req.setPath(reqPath);
std::string resultString = mApiServer->handleRequest(req); std::string resultString = mApiServer->handleRequest(req);
mLocalSocket->write(resultString.c_str(), resultString.length()); mLocalSocket->write(resultString.c_str(), resultString.length());

View File

@ -24,6 +24,7 @@
#include <retroshare/rsinit.h> #include <retroshare/rsinit.h>
#include <string> #include <string>
#include "ApiTypes.h"
#include "ApiServer.h" #include "ApiServer.h"
namespace resource_api namespace resource_api
@ -89,6 +90,7 @@ private:
QLocalSocket* mLocalSocket; QLocalSocket* mLocalSocket;
State mState; State mState;
std::string reqPath; std::string reqPath;
resource_api::Request::Method reqMeth;
}; };
} // namespace resource_api } // namespace resource_api

View File

@ -211,30 +211,7 @@ public:
req.mMethod = resource_api::Request::DELETE_AA; req.mMethod = resource_api::Request::DELETE_AA;
} }
std::stack<std::string> stack; req.setPath(path2);
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;
std::string result = mApiServer->handleRequest(req); std::string result = mApiServer->handleRequest(req);

View File

@ -193,29 +193,21 @@ public:
// then each handler should pop the top element // then each handler should pop the top element
std::stack<std::string> mPath; std::stack<std::string> mPath;
std::string mFullPath; std::string mFullPath;
bool setPath(std::string reqPath) bool setPath(const std::string &reqPath)
{ {
std::string str; 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 mPath.push(str);
str = *sit + str; str.clear();
}
else
{
if(str != "")
{
mPath.push(str);
str.clear();
}
} }
} }
if(str != "") if(!str.empty()) mPath.push(str);
{
mPath.push(str);
}
mFullPath = reqPath; mFullPath = reqPath;
return true; return true;
@ -231,8 +223,7 @@ public:
// contains data for new resources // contains data for new resources
StreamBase& mStream; StreamBase& mStream;
// use the is*() methods to query the method type // use the is*() methods to query the method type:
//private:
enum Method { GET, PUT, DELETE_AA, EXEC};// something is wrong with DELETE, it won't compile with it enum Method { GET, PUT, DELETE_AA, EXEC};// something is wrong with DELETE, it won't compile with it
Method mMethod; Method mMethod;
}; };

View File

@ -27,13 +27,41 @@ Item
ColumnLayout ColumnLayout
{ {
id: colLayout
anchors.top: parent.top anchors.top: parent.top
anchors.bottom: bottomButton.top anchors.bottom: rowLayout.top
Text { id: myKeyField }
TextField { id: myKeyField }
TextField { id: otherKeyField } 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 Button
{ {
id: bottomButton id: bottomButton
@ -41,7 +69,20 @@ Item
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
onClicked: 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))
} }
} }
} }