|
@@ -1,6 +1,8 @@
|
|
#include "../include/Server.h"
|
|
#include "../include/Server.h"
|
|
|
|
+#include "../include/base64.h"
|
|
|
|
+
|
|
#include <iostream>
|
|
#include <iostream>
|
|
-#include <jsoncpp/json/json.h>
|
|
|
|
|
|
+#include <json/json.h>
|
|
|
|
|
|
using namespace boost::asio;
|
|
using namespace boost::asio;
|
|
using ip::tcp;
|
|
using ip::tcp;
|
|
@@ -67,17 +69,17 @@ void con_handler::handle_read_version(const boost::system::error_code &err,
|
|
answer["accept"] = true;
|
|
answer["accept"] = true;
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
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));
|
|
|
|
-
|
|
|
|
// read next data
|
|
// read next data
|
|
sock.async_read_some(buffer(data, max_length),
|
|
sock.async_read_some(buffer(data, max_length),
|
|
boost::bind(&con_handler::handle_read_login,
|
|
boost::bind(&con_handler::handle_read_login,
|
|
shared_from_this(), placeholders::error,
|
|
shared_from_this(), placeholders::error,
|
|
placeholders::bytes_transferred));
|
|
placeholders::bytes_transferred));
|
|
|
|
+
|
|
|
|
+ // 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 {
|
|
} else {
|
|
answer["accept"] = false;
|
|
answer["accept"] = false;
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
@@ -123,17 +125,17 @@ void con_handler::handle_read_login(const boost::system::error_code &err,
|
|
answer["accept"] = true;
|
|
answer["accept"] = true;
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
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));
|
|
|
|
-
|
|
|
|
// read next data
|
|
// read next data
|
|
sock.async_read_some(buffer(data, max_length),
|
|
sock.async_read_some(buffer(data, max_length),
|
|
boost::bind(&con_handler::handle_read_command,
|
|
boost::bind(&con_handler::handle_read_command,
|
|
shared_from_this(), placeholders::error,
|
|
shared_from_this(), placeholders::error,
|
|
placeholders::bytes_transferred));
|
|
placeholders::bytes_transferred));
|
|
|
|
+
|
|
|
|
+ // 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 {
|
|
} else {
|
|
answer["accept"] = false;
|
|
answer["accept"] = false;
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
@@ -174,10 +176,27 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
|
|
|
|
// check command
|
|
// check command
|
|
if (root["command"].compare("status") == 0) {
|
|
if (root["command"].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));
|
|
|
|
+
|
|
answer["command"] = "status";
|
|
answer["command"] = "status";
|
|
|
|
|
|
// TODO answer a real status message
|
|
// TODO answer a real status message
|
|
- answer["response"] = "This is a status message :)";
|
|
|
|
|
|
+ std::string response;
|
|
|
|
+ if (this->getFile.is_open() && this->putFile.is_open()) {
|
|
|
|
+ response = "download and upload running";
|
|
|
|
+ } else if (this->putFile.is_open()) {
|
|
|
|
+ response = "upload running";
|
|
|
|
+ } else if (this->getFile.is_open()) {
|
|
|
|
+ response = "download running";
|
|
|
|
+ } else {
|
|
|
|
+ response = "ok";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ answer["response"] = response;
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
const std::string answerString = Json::writeString(stringBuilder, answer);
|
|
|
|
|
|
// send answer
|
|
// send answer
|
|
@@ -185,13 +204,13 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
boost::bind(&con_handler::handle_write,
|
|
boost::bind(&con_handler::handle_write,
|
|
shared_from_this(), placeholders::error,
|
|
shared_from_this(), placeholders::error,
|
|
placeholders::bytes_transferred));
|
|
placeholders::bytes_transferred));
|
|
-
|
|
|
|
|
|
+ } else if (root["command"].compare("list") == 0) {
|
|
// read next data
|
|
// read next data
|
|
sock.async_read_some(buffer(data, max_length),
|
|
sock.async_read_some(buffer(data, max_length),
|
|
boost::bind(&con_handler::handle_read_command,
|
|
boost::bind(&con_handler::handle_read_command,
|
|
shared_from_this(), placeholders::error,
|
|
shared_from_this(), placeholders::error,
|
|
placeholders::bytes_transferred));
|
|
placeholders::bytes_transferred));
|
|
- } else if (root["command"].compare("list") == 0) {
|
|
|
|
|
|
+
|
|
answer["command"] = "list";
|
|
answer["command"] = "list";
|
|
|
|
|
|
// TODO look for real data
|
|
// TODO look for real data
|
|
@@ -209,47 +228,181 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
|
|
shared_from_this(), placeholders::error,
|
|
shared_from_this(), placeholders::error,
|
|
placeholders::bytes_transferred));
|
|
placeholders::bytes_transferred));
|
|
|
|
|
|
|
|
+ } else if (root["command"].compare("put") == 0) {
|
|
// read next data
|
|
// read next data
|
|
sock.async_read_some(buffer(data, max_length),
|
|
sock.async_read_some(buffer(data, max_length),
|
|
boost::bind(&con_handler::handle_read_command,
|
|
boost::bind(&con_handler::handle_read_command,
|
|
shared_from_this(), placeholders::error,
|
|
shared_from_this(), placeholders::error,
|
|
placeholders::bytes_transferred));
|
|
placeholders::bytes_transferred));
|
|
- } else if (root["command"].compare("put") == 0) {
|
|
|
|
- answer["command"] = "put";
|
|
|
|
-
|
|
|
|
- // TODO establish real connection and receive file
|
|
|
|
- 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));
|
|
|
|
|
|
+ answer["command"] = "put";
|
|
|
|
|
|
|
|
+ if (this->putFile.is_open() && root["cancel"].asBool() == true) {
|
|
|
|
+ // cancel upload
|
|
|
|
+ this->putFile.close();
|
|
|
|
+ std::remove(this->putFileName.c_str());
|
|
|
|
+
|
|
|
|
+ } else if (this->putFile.is_open()) {
|
|
|
|
+ // upload chunks
|
|
|
|
+
|
|
|
|
+ // decode base64 string
|
|
|
|
+ const std::string data = base64::decode(root["data"].asString());
|
|
|
|
+
|
|
|
|
+ this->putFile << data;
|
|
|
|
+
|
|
|
|
+ // close file if put sends remaining = 0
|
|
|
|
+ if (root["remaining"].asInt() == 0) {
|
|
|
|
+ this->putFile.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ // start upload
|
|
|
|
+ const std::string filename = root["file"].asString();
|
|
|
|
+ if (filename.find("/") != std::string::npos) {
|
|
|
|
+ // 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));
|
|
|
|
+ } else {
|
|
|
|
+ // build file path
|
|
|
|
+ this->putFileName = this->fileDirectory;
|
|
|
|
+ this->putFileName.append(filename);
|
|
|
|
+
|
|
|
|
+ // open file and test if it already exists
|
|
|
|
+ this->putFile.open(this->putFileName,
|
|
|
|
+ std::ios::app | std::ios::binary);
|
|
|
|
+ if (this->putFile.tellp() != std::ios::beg) {
|
|
|
|
+ // file already exists
|
|
|
|
+ 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));
|
|
|
|
+ } 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));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else if (root["command"].compare("get") == 0) {
|
|
// read next data
|
|
// read next data
|
|
sock.async_read_some(buffer(data, max_length),
|
|
sock.async_read_some(buffer(data, max_length),
|
|
boost::bind(&con_handler::handle_read_command,
|
|
boost::bind(&con_handler::handle_read_command,
|
|
shared_from_this(), placeholders::error,
|
|
shared_from_this(), placeholders::error,
|
|
placeholders::bytes_transferred));
|
|
placeholders::bytes_transferred));
|
|
- } else if (root["command"].compare("get") == 0) {
|
|
|
|
- answer["command"] = "get";
|
|
|
|
-
|
|
|
|
- // TODO establish real connection and send file
|
|
|
|
- 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));
|
|
|
|
|
|
+ answer["command"] = "get";
|
|
|
|
|
|
- // 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));
|
|
|
|
|
|
+ // a get request is already being progressed
|
|
|
|
+ if (this->getFile.is_open()) {
|
|
|
|
+ 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));
|
|
|
|
+ } else {
|
|
|
|
+ // open file
|
|
|
|
+ const std::string filename = root["file"].asString();
|
|
|
|
+ if (filename.find("/") != std::string::npos) {
|
|
|
|
+ // 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));
|
|
|
|
+ } else {
|
|
|
|
+ std::string file = this->fileDirectory;
|
|
|
|
+ file.append(filename);
|
|
|
|
+
|
|
|
|
+ this->getFile = std::ifstream(file, std::ios::ate | std::ios::binary);
|
|
|
|
+
|
|
|
|
+ if (this->getFile.is_open() == 0) {
|
|
|
|
+ // 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));
|
|
|
|
+ } else {
|
|
|
|
+ answer["accept"] = true;
|
|
|
|
+
|
|
|
|
+ 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));
|
|
|
|
+
|
|
|
|
+ answer = Json::Value();
|
|
|
|
+ answer["command"] = "get";
|
|
|
|
+
|
|
|
|
+ // get size of file
|
|
|
|
+ size_t size = this->getFile.tellg();
|
|
|
|
+ this->getFile.seekg(std::ios::beg);
|
|
|
|
+
|
|
|
|
+ char fileBuffer[max_data_length + 1];
|
|
|
|
+ while (size_t read =
|
|
|
|
+ this->getFile.readsome(fileBuffer, max_data_length)) {
|
|
|
|
+ fileBuffer[read] = 0;
|
|
|
|
+ size -= read;
|
|
|
|
+ int remaining = size / max_data_length +
|
|
|
|
+ (size % max_data_length == 0 ? 0 : 1);
|
|
|
|
+ answer["remaining"] = remaining;
|
|
|
|
+ answer["cancel"] = false;
|
|
|
|
+ answer["data"] = base64::encode(fileBuffer);
|
|
|
|
+
|
|
|
|
+ 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));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this->getFile.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
} else if (root["command"].compare("close") == 0) {
|
|
} else if (root["command"].compare("close") == 0) {
|
|
answer["command"] = "close";
|
|
answer["command"] = "close";
|
|
answer["response"] = "bye";
|
|
answer["response"] = "bye";
|