From 2bfdc0928b350d560355d05185fd042368dfc17c Mon Sep 17 00:00:00 2001 From: Kris Moore Date: Tue, 5 Jan 2016 17:55:22 -0500 Subject: [PATCH] Fixed up the way REST / HTTP1.1 messages are formatted when passed back to the client. Fixes an issue with bad header formatting, and changed JSON to be more human-readable. Also, changed the test script to only display the verbose error output if it fails, otherwise it'll just return the JSON response --- src/server/RestStructs.h | 18 +++++++++++------- tests/rest-api-test.sh | 7 +++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/server/RestStructs.h b/src/server/RestStructs.h index 0013eee..b7636b9 100644 --- a/src/server/RestStructs.h +++ b/src/server/RestStructs.h @@ -104,12 +104,12 @@ public: if( !in_struct.VERB.isEmpty() ){ //REST output syntax QStringList headers; - QString firstline = in_struct.HTTPVERSION; - if(firstline.isEmpty()){ firstline = CurHttpVersion; }//default value + QString firstline = in_struct.HTTPVERSION.simplified(); + if(firstline.isEmpty()){ firstline = CurHttpVersion.simplified(); }//default value QString Body; if(!out_args.isNull()){ QJsonObject obj; obj.insert("args", out_args); - Body = QJsonDocument(obj).toJson(QJsonDocument::Compact); + Body = QJsonDocument(obj).toJson(); } switch(CODE){ case PROCESSING: @@ -136,13 +136,17 @@ public: firstline.append(" 404 Not Found"); break; } headers << firstline; - headers << "Date: "+QDateTime::currentDateTime().toString(Qt::ISODate); - if(!Header.isEmpty()){ headers << Header; } + headers << "Server: SysAdm/1.0"; + headers << "Date: "+QDateTime::currentDateTime().toString(Qt::ISODate).simplified(); + if(!Header.isEmpty()) { + for (int i = 0; i < Header.size(); ++i) + headers << Header.at(i).simplified(); + } //Now add the body of the return - qDebug() << "Return JSON:" << Body; if(!Body.isEmpty()){ headers << "Content-Length: "+QString::number(Body.toUtf8().size()); } //number of bytes for the body + headers << ""; headers << Body; - return headers.join("\n"); + return headers.join("\r\n"); }else{ //JSON output (load all the input fields for the moment) diff --git a/tests/rest-api-test.sh b/tests/rest-api-test.sh index 2559de1..038ed21 100755 --- a/tests/rest-api-test.sh +++ b/tests/rest-api-test.sh @@ -19,5 +19,8 @@ echo "" . ./utils/resty -W "http://127.0.0.1:12151" -H "Accept: application/json" -H "Content-Type: application/json" -u ${fuser}:${fpass} # Check the reply of this REST query -POST /sysadm/lifepreserver '{ "action":"listcron" }' -v - +PUT /sysadm/lifepreserver '{ "action":"listcron" }' -v 2>/tmp/.rstErr +if [ $? -ne 0 ] ; then + echo "Failed.. Error output:" + cat /tmp/.rstErr +fi