|
@@ -2,7 +2,6 @@
|
|
|
#include "../include/base64.h"
|
|
|
|
|
|
#include <iostream>
|
|
|
-#include <json/json.h>
|
|
|
|
|
|
using namespace boost::asio;
|
|
|
using ip::tcp;
|
|
@@ -13,7 +12,14 @@ using ip::tcp;
|
|
|
|
|
|
con_handler::con_handler(
|
|
|
basic_socket_acceptor<ip::tcp>::executor_type &io_service)
|
|
|
- : sock(io_service) {}
|
|
|
+ : sock(io_service) {
|
|
|
+ // disable indentation for json
|
|
|
+ this->jsonStringBuilder.settings_["indentation"] = "";
|
|
|
+
|
|
|
+ // init reader to parse json
|
|
|
+ Json::CharReaderBuilder builder;
|
|
|
+ jsonReader = std::unique_ptr<Json::CharReader>(builder.newCharReader());
|
|
|
+}
|
|
|
|
|
|
con_handler::~con_handler() {}
|
|
|
|
|
@@ -24,23 +30,7 @@ con_handler::create(basic_socket_acceptor<ip::tcp>::executor_type &io_service) {
|
|
|
|
|
|
tcp::socket &con_handler::socket() { return sock; }
|
|
|
|
|
|
-void con_handler::start() {
|
|
|
-
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_version,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
-}
|
|
|
-
|
|
|
-void con_handler::handle_read(const boost::system::error_code &err,
|
|
|
- size_t bytes_transferred) {
|
|
|
- if (!err) {
|
|
|
- std::cout << this->data << std::endl;
|
|
|
- } else {
|
|
|
- std::cerr << "error: " << err.message() << std::endl;
|
|
|
- sock.close();
|
|
|
- }
|
|
|
-}
|
|
|
+void con_handler::start() { read(&con_handler::handle_read_version); }
|
|
|
|
|
|
void con_handler::handle_read_version(const boost::system::error_code &err,
|
|
|
size_t bytes_transferred) {
|
|
@@ -48,47 +38,33 @@ void con_handler::handle_read_version(const boost::system::error_code &err,
|
|
|
// set up json stuff
|
|
|
JSONCPP_STRING err;
|
|
|
Json::Value root;
|
|
|
- Json::CharReaderBuilder builder;
|
|
|
- const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
|
|
|
|
|
|
// parse data
|
|
|
- if (!reader->parse(this->data, this->data + bytes_transferred, &root,
|
|
|
- &err)) {
|
|
|
+ if (!this->jsonReader->parse(this->data, this->data + bytes_transferred,
|
|
|
+ &root, &err)) {
|
|
|
std::cerr << "Json error: " << err << std::endl << "data: " << this->data;
|
|
|
sock.close();
|
|
|
}
|
|
|
|
|
|
// create answer
|
|
|
Json::Value answer;
|
|
|
- Json::StreamWriterBuilder stringBuilder;
|
|
|
|
|
|
answer["version"] = this->protocolVersion;
|
|
|
|
|
|
// check version string
|
|
|
if (root["version"].asString().compare(this->protocolVersion) == 0) {
|
|
|
answer["accept"] = true;
|
|
|
- const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
|
|
|
- // read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_login,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_login);
|
|
|
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
} else {
|
|
|
answer["accept"] = false;
|
|
|
- const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
+ ;
|
|
|
// close connection
|
|
|
sock.close();
|
|
|
}
|
|
@@ -105,46 +81,32 @@ void con_handler::handle_read_login(const boost::system::error_code &err,
|
|
|
// set up json stuff
|
|
|
JSONCPP_STRING err;
|
|
|
Json::Value root;
|
|
|
- Json::CharReaderBuilder builder;
|
|
|
- const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
|
|
|
|
|
|
// parse data
|
|
|
- if (!reader->parse(this->data, this->data + bytes_transferred, &root,
|
|
|
- &err)) {
|
|
|
+ if (!this->jsonReader->parse(this->data, this->data + bytes_transferred,
|
|
|
+ &root, &err)) {
|
|
|
std::cerr << "Json error: " << err << std::endl << "data: " << this->data;
|
|
|
sock.close();
|
|
|
}
|
|
|
|
|
|
Json::Value answer;
|
|
|
- Json::StreamWriterBuilder stringBuilder;
|
|
|
|
|
|
// user credentials
|
|
|
// TODO check user credentials!!!
|
|
|
if (root["user"].asString().compare("user") == 0 &&
|
|
|
root["pass"].asString().compare("pass") == 0) {
|
|
|
answer["accept"] = true;
|
|
|
- const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
} else {
|
|
|
answer["accept"] = false;
|
|
|
- const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
// close connection
|
|
|
sock.close();
|
|
|
}
|
|
@@ -161,26 +123,20 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
// set up json stuff
|
|
|
JSONCPP_STRING err;
|
|
|
Json::Value root;
|
|
|
- Json::CharReaderBuilder builder;
|
|
|
- const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
|
|
|
|
|
|
// parse data
|
|
|
- if (!reader->parse(this->data, this->data + bytes_transferred, &root,
|
|
|
- &err)) {
|
|
|
+ if (!this->jsonReader->parse(this->data, this->data + bytes_transferred,
|
|
|
+ &root, &err)) {
|
|
|
std::cerr << "Json error: " << err << std::endl << "data: " << this->data;
|
|
|
sock.close();
|
|
|
}
|
|
|
|
|
|
Json::Value answer;
|
|
|
- Json::StreamWriterBuilder stringBuilder;
|
|
|
|
|
|
// check command
|
|
|
if (root["command"].asString().compare("status") == 0) {
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
answer["command"] = "status";
|
|
|
|
|
@@ -197,19 +153,13 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
}
|
|
|
|
|
|
answer["response"] = response;
|
|
|
- const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
- } else if (root["command"].asString().compare("list") == 0) {
|
|
|
+ sendJson(answer);
|
|
|
+ } else if (root["command"].compare("list") == 0) {
|
|
|
+
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
answer["command"] = "list";
|
|
|
|
|
@@ -221,19 +171,12 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
answer["names"] = array;
|
|
|
answer["remaining"] = 0;
|
|
|
|
|
|
- const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
|
|
|
} else if (root["command"].asString().compare("put") == 0) {
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
answer["command"] = "put";
|
|
|
|
|
@@ -263,13 +206,8 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
answer["cancel"] = false;
|
|
|
answer["received"] = this->putFileReceived;
|
|
|
|
|
|
- const std::string answerString =
|
|
|
- Json::writeString(stringBuilder, answer);
|
|
|
// send answer
|
|
|
- sock.async_write_some(
|
|
|
- buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write, shared_from_this(),
|
|
|
- placeholders::error, placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
|
|
|
} else {
|
|
|
// start upload
|
|
@@ -278,14 +216,8 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
// slashes in file names are illegal
|
|
|
answer["accept"] = false;
|
|
|
|
|
|
- const std::string answerString =
|
|
|
- Json::writeString(stringBuilder, answer);
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(),
|
|
|
- placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
} else {
|
|
|
// build file path
|
|
|
this->putFileName = this->fileDirectory;
|
|
@@ -300,35 +232,20 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
this->putFile.close();
|
|
|
answer["accept"] = false;
|
|
|
|
|
|
- const std::string answerString =
|
|
|
- Json::writeString(stringBuilder, answer);
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(),
|
|
|
- placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
} else {
|
|
|
// accept file
|
|
|
answer["accept"] = true;
|
|
|
|
|
|
- const std::string answerString =
|
|
|
- Json::writeString(stringBuilder, answer);
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(),
|
|
|
- placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else if (root["command"].asString().compare("get") == 0) {
|
|
|
// read next data
|
|
|
- sock.async_read_some(buffer(data, max_length),
|
|
|
- boost::bind(&con_handler::handle_read_command,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ read(&con_handler::handle_read_command);
|
|
|
|
|
|
answer["command"] = "get";
|
|
|
|
|
@@ -352,14 +269,7 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
answer["cancel"] = false;
|
|
|
answer["data"] = base64::encodeVector(data);
|
|
|
|
|
|
- const std::string answerString =
|
|
|
- Json::writeString(stringBuilder, answer);
|
|
|
-
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(),
|
|
|
- placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
} else {
|
|
|
// remaining 0 and received by client so you can close the file
|
|
|
this->getFile.close();
|
|
@@ -367,14 +277,8 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
} else {
|
|
|
answer["accept"] = false;
|
|
|
|
|
|
- const std::string answerString =
|
|
|
- Json::writeString(stringBuilder, answer);
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(),
|
|
|
- placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
}
|
|
|
} else {
|
|
|
// open file
|
|
@@ -383,14 +287,8 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
// slashes in file names are illegal
|
|
|
answer["accept"] = false;
|
|
|
|
|
|
- const std::string answerString =
|
|
|
- Json::writeString(stringBuilder, answer);
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(),
|
|
|
- placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
} else {
|
|
|
std::string file = this->fileDirectory;
|
|
|
file.append(filename);
|
|
@@ -401,24 +299,12 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
// file does not exist or cannot be opened
|
|
|
answer["accept"] = false;
|
|
|
|
|
|
- const std::string answerString =
|
|
|
- Json::writeString(stringBuilder, answer);
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(),
|
|
|
- placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
} else {
|
|
|
answer["accept"] = true;
|
|
|
|
|
|
- std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
-
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(),
|
|
|
- placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
|
|
|
answer = Json::Value();
|
|
|
answer["command"] = "get";
|
|
@@ -442,26 +328,16 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
answer["cancel"] = false;
|
|
|
answer["data"] = base64::encodeVector(data);
|
|
|
|
|
|
- answerString = Json::writeString(stringBuilder, answer);
|
|
|
-
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(),
|
|
|
- placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else if (root["command"].asString().compare("close") == 0) {
|
|
|
answer["command"] = "close";
|
|
|
answer["response"] = "bye";
|
|
|
- const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
|
|
|
// send answer
|
|
|
- sock.async_write_some(buffer(answerString, max_length),
|
|
|
- boost::bind(&con_handler::handle_write,
|
|
|
- shared_from_this(), placeholders::error,
|
|
|
- placeholders::bytes_transferred));
|
|
|
+ sendJson(answer);
|
|
|
|
|
|
// close connection
|
|
|
sock.close();
|
|
@@ -488,6 +364,24 @@ void con_handler::handle_write(const boost::system::error_code &err,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void con_handler::read(void (con_handler::*handler)(
|
|
|
+ const boost::system::error_code &err, size_t bytes_transferred)) {
|
|
|
+ sock.async_read_some(buffer(data, max_length),
|
|
|
+ boost::bind(handler, shared_from_this(),
|
|
|
+ placeholders::error,
|
|
|
+ placeholders::bytes_transferred));
|
|
|
+}
|
|
|
+
|
|
|
+void con_handler::sendJson(const Json::Value &json) {
|
|
|
+ std::string jsonString = Json::writeString(jsonStringBuilder, json);
|
|
|
+ jsonString.append("\n");
|
|
|
+
|
|
|
+ sock.async_write_some(buffer(jsonString, max_length),
|
|
|
+ boost::bind(&con_handler::handle_write,
|
|
|
+ shared_from_this(), placeholders::error,
|
|
|
+ placeholders::bytes_transferred));
|
|
|
+}
|
|
|
+
|
|
|
/**********
|
|
|
* Server *
|
|
|
**********/
|