Browse Source

fuck up some shit

master
Jørgen Sverre Lien Sellæg 4 years ago
parent
commit
f16d8d3931
  1. 3
      .gitignore
  2. 9
      .gitmodules
  3. 13
      lay/.eslintignore
  4. 15
      lay/.eslintrc.cjs
  5. 8
      lay/.gitignore
  6. 1
      lay/.npmrc
  7. 13
      lay/.prettierignore
  8. 8
      lay/.prettierrc
  9. 38
      lay/README.md
  10. 24
      lay/package.json
  11. 12
      lay/src/app.html
  12. 2
      lay/src/routes/+page.svelte
  13. BIN
      lay/static/favicon.png
  14. 10
      lay/svelte.config.js
  15. 7
      lay/vite.config.js
  16. 1704
      lay/yarn.lock
  17. 30
      toREST/CMakeLists.txt
  18. 2320
      toREST/docs/Doxyfile.in
  19. 11475
      toREST/lib/Catch/catch.hpp
  20. 1
      toREST/lib/Simple-Web-Server
  21. 1
      toREST/lib/Simple-WebSocket-Server
  22. 220
      toREST/lib/crypto.hpp
  23. 1
      toREST/lib/json
  24. 10435
      toREST/lib/json.hpp
  25. 414
      toREST/lib/server_http.hpp
  26. 774
      toREST/lib/server_ws.hpp
  27. 12
      toREST/tests/config_test.cpp
  28. 65
      toREST/tests/http_test.cpp
  29. 7
      toREST/tests/include/ConfigContext.hpp
  30. 37
      toREST/tests/include/ServerContext.hpp
  31. 66
      toREST/tests/include/SessionContext.hpp
  32. 56
      toREST/tests/include/TorrentContext.hpp
  33. 80
      toREST/tests/session_test.cpp
  34. 110
      toREST/tests/stubs/ServerContext.cpp
  35. 109
      toREST/tests/stubs/SessionContext.cpp
  36. 47
      toREST/tests/stubs/TorrentContext.cpp
  37. 3
      toREST/tests/test.cpp
  38. 91
      toREST/tests/torrent_test.cpp
  39. 243
      toREST/tests/torrents_test.cpp
  40. 25
      toREST/tests/util_test.cpp
  41. 88
      toREST/tests/ws_test.cpp

3
.gitignore vendored

@ -1,3 +1,2 @@
lay
scripts
*~

9
.gitmodules vendored

@ -0,0 +1,9 @@
[submodule "toREST/lib/Simple-WebSocket-Server"]
path = toREST/lib/Simple-WebSocket-Server
url = https://gitlab.com/eidheim/Simple-WebSocket-Server.git
[submodule "toREST/lib/Simple-Web-Server"]
path = toREST/lib/Simple-Web-Server
url = https://gitlab.com/eidheim/Simple-Web-Server.git
[submodule "toREST/lib/json"]
path = toREST/lib/json
url = https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent

13
lay/.eslintignore

@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock

15
lay/.eslintrc.cjs

@ -0,0 +1,15 @@
module.exports = {
root: true,
extends: ['eslint:recommended', 'prettier'],
plugins: ['svelte3'],
overrides: [{ files: ['*.svelte'], processor: 'svelte3/svelte3' }],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020
},
env: {
browser: true,
es2017: true,
node: true
}
};

8
lay/.gitignore vendored

@ -0,0 +1,8 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example

1
lay/.npmrc

@ -0,0 +1 @@
engine-strict=true

13
lay/.prettierignore

@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock

8
lay/.prettierrc

@ -0,0 +1,8 @@
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"pluginSearchDirs": ["."],
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}

38
lay/README.md

@ -0,0 +1,38 @@
# create-svelte
Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).
## Creating a project
If you're seeing this, you've probably already done this step. Congrats!
```bash
# create a new project in the current directory
npm create svelte@latest
# create a new project in my-app
npm create svelte@latest my-app
```
## Developing
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
```bash
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
```
## Building
To create a production version of your app:
```bash
npm run build
```
You can preview the production build with `npm run preview`.
> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.

24
lay/package.json

@ -0,0 +1,24 @@
{
"name": "lay",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"lint": "prettier --check . && eslint .",
"format": "prettier --write ."
},
"devDependencies": {
"@sveltejs/adapter-auto": "next",
"@sveltejs/kit": "next",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-svelte3": "^4.0.0",
"prettier": "^2.6.2",
"prettier-plugin-svelte": "^2.7.0",
"svelte": "^3.44.0",
"vite": "^3.1.0-beta.1"
},
"type": "module"
}

12
lay/src/app.html

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width" />
%sveltekit.head%
</head>
<body>
<div>%sveltekit.body%</div>
</body>
</html>

2
lay/src/routes/+page.svelte

@ -0,0 +1,2 @@
<h1>Lay</h1>
<p>Hei</p>

BIN
lay/static/favicon.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

10
lay/svelte.config.js

@ -0,0 +1,10 @@
import adapter from '@sveltejs/adapter-auto';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter()
}
};
export default config;

7
lay/vite.config.js

@ -0,0 +1,7 @@
import { sveltekit } from '@sveltejs/kit/vite';
const config = {
plugins: [sveltekit()]
};
export default config;

1704
lay/yarn.lock

File diff suppressed because it is too large Load Diff

30
toREST/CMakeLists.txt

@ -10,7 +10,6 @@ find_package(LibTorrent REQUIRED)
find_package(OpenSSL REQUIRED)
set(BT_INCLUDE_DIR ./include)
set(LIB_INCLUDE_DIR ./lib)
file(GLOB source_files "./src/*.cpp")
@ -19,7 +18,6 @@ include_directories(
${OPENSSL_INCLUDE_DIR}
${LIBTORRENT_INCLUDE_DIR}
${BT_INCLUDE_DIR}
${LIB_INCLUDE_DIR}
)
set(global_libraries
@ -30,34 +28,6 @@ set(global_libraries
)
add_library(project_shared OBJECT ${source_files})
enable_testing()
file(GLOB test_files "./tests/*.cpp")
file(GLOB test_stubs "./tests/stubs/*.cpp")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -Wall -fprofile-arcs -ftest-coverage ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -Wall -fprofile-arcs -ftest-coverage ")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage ")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage ")
set(test test_${project_name})
add_test(${test} ${test})
add_executable(${test} ${test_files} ${test_stubs} $<TARGET_OBJECTS:project_shared>)
target_include_directories(${test} PUBLIC ../lib/Catch ./tests/include)
target_link_libraries(${test} ${global_libraries})
add_executable(${project_name} ./src/main.cxx $<TARGET_OBJECTS:project_shared>)
target_link_libraries(${project_name} ${global_libraries})
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
add_custom_target(doc
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../include
COMMENT "Generating API documentation with Doxygen to ${CMAKE_CURRENT_BINARY_DIR}" VERBATIM
)
endif(DOXYGEN_FOUND)

2320
toREST/docs/Doxyfile.in

File diff suppressed because it is too large Load Diff

11475
toREST/lib/Catch/catch.hpp

File diff suppressed because it is too large Load Diff

1
toREST/lib/Simple-Web-Server

@ -0,0 +1 @@
Subproject commit 632ee8d88c8b9886c6187a67e81a2ffd2e8ff733

1
toREST/lib/Simple-WebSocket-Server

@ -0,0 +1 @@
Subproject commit 293a407f39fe50e285c8599b3d98a7da25371d20

220
toREST/lib/crypto.hpp

@ -1,220 +0,0 @@
#ifndef CRYPTO_HPP
#define CRYPTO_HPP
#include <string>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <istream>
#include <vector>
//Moving these to a seperate namespace for minimal global namespace cluttering does not work with clang++
#include <openssl/evp.h>
#include <openssl/buffer.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
namespace SimpleWeb {
//TODO 2017: remove workaround for MSVS 2012
#if _MSC_VER == 1700 //MSVS 2012 has no definition for round()
inline double round(double x) { //custom definition of round() for positive numbers
return floor(x + 0.5);
}
#endif
class Crypto {
const static size_t buffer_size=131072;
public:
class Base64 {
public:
static std::string encode(const std::string &ascii) {
std::string base64;
BIO *bio, *b64;
BUF_MEM *bptr=BUF_MEM_new();
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bio = BIO_new(BIO_s_mem());
BIO_push(b64, bio);
BIO_set_mem_buf(b64, bptr, BIO_CLOSE);
//Write directly to base64-buffer to avoid copy
int base64_length=static_cast<int>(round(4*ceil((double)ascii.size()/3.0)));
base64.resize(base64_length);
bptr->length=0;
bptr->max=base64_length+1;
bptr->data=(char*)&base64[0];
BIO_write(b64, &ascii[0], static_cast<int>(ascii.size()));
BIO_flush(b64);
//To keep &base64[0] through BIO_free_all(b64)
bptr->length=0;
bptr->max=0;
bptr->data=nullptr;
BIO_free_all(b64);
return base64;
}
static std::string decode(const std::string &base64) {
std::string ascii;
//Resize ascii, however, the size is a up to two bytes too large.
ascii.resize((6*base64.size())/8);
BIO *b64, *bio;
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bio = BIO_new_mem_buf((char*)&base64[0], static_cast<int>(base64.size()));
bio = BIO_push(b64, bio);
int decoded_length = BIO_read(bio, &ascii[0], static_cast<int>(ascii.size()));
ascii.resize(decoded_length);
BIO_free_all(b64);
return ascii;
}
};
/// Return hex string from bytes in input string.
static std::string to_hex_string(const std::string &input) {
std::stringstream hex_stream;
hex_stream << std::hex << std::internal << std::setfill('0');
for (auto &byte : input)
hex_stream << std::setw(2) << static_cast<int>(static_cast<unsigned char>(byte));
return hex_stream.str();
}
static std::string md5(const std::string &input, size_t iterations=1) {
std::string hash;
hash.resize(128 / 8);
MD5(reinterpret_cast<const unsigned char*>(&input[0]), input.size(), reinterpret_cast<unsigned char*>(&hash[0]));
for (size_t c = 1; c < iterations; ++c)
MD5(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));
return hash;
}
static std::string md5(std::istream &stream, size_t iterations=1) {
MD5_CTX context;
MD5_Init(&context);
std::streamsize read_length;
std::vector<char> buffer(buffer_size);
while((read_length=stream.read(&buffer[0], buffer_size).gcount())>0)
MD5_Update(&context, buffer.data(), read_length);
std::string hash;
hash.resize(128 / 8);
MD5_Final(reinterpret_cast<unsigned char*>(&hash[0]), &context);
for (size_t c = 1; c < iterations; ++c)
MD5(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));
return hash;
}
static std::string sha1(const std::string &input, size_t iterations=1) {
std::string hash;
hash.resize(160 / 8);
SHA1(reinterpret_cast<const unsigned char*>(&input[0]), input.size(), reinterpret_cast<unsigned char*>(&hash[0]));
for (size_t c = 1; c < iterations; ++c)
SHA1(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));
return hash;
}
static std::string sha1(std::istream &stream, size_t iterations=1) {
SHA_CTX context;
SHA1_Init(&context);
std::streamsize read_length;
std::vector<char> buffer(buffer_size);
while((read_length=stream.read(&buffer[0], buffer_size).gcount())>0)
SHA1_Update(&context, buffer.data(), read_length);
std::string hash;
hash.resize(160 / 8);
SHA1_Final(reinterpret_cast<unsigned char*>(&hash[0]), &context);
for (size_t c = 1; c < iterations; ++c)
SHA1(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));
return hash;
}
static std::string sha256(const std::string &input, size_t iterations=1) {
std::string hash;
hash.resize(256 / 8);
SHA256(reinterpret_cast<const unsigned char*>(&input[0]), input.size(), reinterpret_cast<unsigned char*>(&hash[0]));
for (size_t c = 1; c < iterations; ++c)
SHA256(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));
return hash;
}
static std::string sha256(std::istream &stream, size_t iterations=1) {
SHA256_CTX context;
SHA256_Init(&context);
std::streamsize read_length;
std::vector<char> buffer(buffer_size);
while((read_length=stream.read(&buffer[0], buffer_size).gcount())>0)
SHA256_Update(&context, buffer.data(), read_length);
std::string hash;
hash.resize(256 / 8);
SHA256_Final(reinterpret_cast<unsigned char*>(&hash[0]), &context);
for (size_t c = 1; c < iterations; ++c)
SHA256(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));
return hash;
}
static std::string sha512(const std::string &input, size_t iterations=1) {
std::string hash;
hash.resize(512 / 8);
SHA512(reinterpret_cast<const unsigned char*>(&input[0]), input.size(), reinterpret_cast<unsigned char*>(&hash[0]));
for (size_t c = 1; c < iterations; ++c)
SHA512(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));
return hash;
}
static std::string sha512(std::istream &stream, size_t iterations=1) {
SHA512_CTX context;
SHA512_Init(&context);
std::streamsize read_length;
std::vector<char> buffer(buffer_size);
while((read_length=stream.read(&buffer[0], buffer_size).gcount())>0)
SHA512_Update(&context, buffer.data(), read_length);
std::string hash;
hash.resize(512 / 8);
SHA512_Final(reinterpret_cast<unsigned char*>(&hash[0]), &context);
for (size_t c = 1; c < iterations; ++c)
SHA512(reinterpret_cast<const unsigned char*>(&hash[0]), hash.size(), reinterpret_cast<unsigned char*>(&hash[0]));
return hash;
}
/// key_size is number of bytes of the returned key.
static std::string pbkdf2(const std::string &password, const std::string &salt, int iterations, int key_size) {
std::string key;
key.resize(key_size);
PKCS5_PBKDF2_HMAC_SHA1(password.c_str(), password.size(),
reinterpret_cast<const unsigned char*>(salt.c_str()), salt.size(), iterations,
key_size, reinterpret_cast<unsigned char*>(&key[0]));
return key;
}
};
}
#endif /* CRYPTO_HPP */

1
toREST/lib/json

@ -0,0 +1 @@
Subproject commit 2cdf3757e097af6728cd4e1ffd8c34d3215d9811

10435
toREST/lib/json.hpp

File diff suppressed because it is too large Load Diff

414
toREST/lib/server_http.hpp

@ -1,414 +0,0 @@
#ifndef SERVER_HTTP_HPP
#define SERVER_HTTP_HPP
#include <boost/asio.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp>
#include <unordered_map>
#include <thread>
#include <functional>
#include <iostream>
#include <sstream>
namespace SimpleWeb {
template <class socket_type>
class ServerBase {
public:
virtual ~ServerBase() {}
class Response : public std::ostream {
friend class ServerBase<socket_type>;
boost::asio::streambuf streambuf;
std::shared_ptr<socket_type> socket;
Response(std::shared_ptr<socket_type> socket): std::ostream(&streambuf), socket(socket) {}
public:
size_t size() {
return streambuf.size();
}
};
class Content : public std::istream {
friend class ServerBase<socket_type>;
public:
size_t size() {
return streambuf.size();
}
std::string string() {
std::stringstream ss;
ss << rdbuf();
return ss.str();
}
private:
boost::asio::streambuf &streambuf;
Content(boost::asio::streambuf &streambuf): std::istream(&streambuf), streambuf(streambuf) {}
};
class Request {
friend class ServerBase<socket_type>;
//Based on http://www.boost.org/doc/libs/1_60_0/doc/html/unordered/hash_equality.html
class iequal_to {
public:
bool operator()(const std::string &key1, const std::string &key2) const {
return boost::algorithm::iequals(key1, key2);
}
};
class ihash {
public:
size_t operator()(const std::string &key) const {
std::size_t seed=0;
for(auto &c: key)
boost::hash_combine(seed, std::tolower(c));
return seed;
}
};
public:
std::string method, path, http_version;
Content content;
std::unordered_multimap<std::string, std::string, ihash, iequal_to> header;
boost::smatch path_match;
std::string remote_endpoint_address;
unsigned short remote_endpoint_port;
private:
Request(): content(streambuf) {}
boost::asio::streambuf streambuf;
void read_remote_endpoint_data(socket_type& socket) {
try {
remote_endpoint_address=socket.lowest_layer().remote_endpoint().address().to_string();
remote_endpoint_port=socket.lowest_layer().remote_endpoint().port();
}
catch(const std::exception&) {}
}
};
class Config {
friend class ServerBase<socket_type>;
Config(unsigned short port, size_t num_threads): num_threads(num_threads), port(port), reuse_address(true) {}
size_t num_threads;
public:
unsigned short port;
///IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation.
///If empty, the address will be any address.
std::string address;
///Set to false to avoid binding the socket to an address that is already in use.
bool reuse_address;
};
///Set before calling start().
Config config;
std::unordered_map<std::string, std::unordered_map<std::string,
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)> > > resource;
std::unordered_map<std::string,
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)> > default_resource;
private:
std::vector<std::pair<std::string, std::vector<std::pair<boost::regex,
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)> > > > > opt_resource;
public:
void start() {
//Copy the resources to opt_resource for more efficient request processing
opt_resource.clear();
for(auto& res: resource) {
for(auto& res_method: res.second) {
auto it=opt_resource.end();
for(auto opt_it=opt_resource.begin();opt_it!=opt_resource.end();opt_it++) {
if(res_method.first==opt_it->first) {
it=opt_it;
break;
}
}
if(it==opt_resource.end()) {
opt_resource.emplace_back();
it=opt_resource.begin()+(opt_resource.size()-1);
it->first=res_method.first;
}
it->second.emplace_back(boost::regex(res.first), res_method.second);
}
}
if(io_service.stopped())
io_service.reset();
boost::asio::ip::tcp::endpoint endpoint;
if(config.address.size()>0)
endpoint=boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(config.address), config.port);
else
endpoint=boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), config.port);
acceptor.open(endpoint.protocol());
acceptor.set_option(boost::asio::socket_base::reuse_address(config.reuse_address));
acceptor.bind(endpoint);
acceptor.listen();
accept();
//If num_threads>1, start m_io_service.run() in (num_threads-1) threads for thread-pooling
threads.clear();
for(size_t c=1;c<config.num_threads;c++) {
threads.emplace_back([this](){
io_service.run();
});
}
//Main thread
io_service.run();
//Wait for the rest of the threads, if any, to finish as well
for(auto& t: threads) {
t.join();
}
}
void stop() {
acceptor.close();
io_service.stop();
}
///Use this function if you need to recursively send parts of a longer message
void send(std::shared_ptr<Response> response, const std::function<void(const boost::system::error_code&)>& callback=nullptr) const {
boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(callback)
callback(ec);
});
}
protected:
boost::asio::io_service io_service;
boost::asio::ip::tcp::acceptor acceptor;
std::vector<std::thread> threads;
long timeout_request;
long timeout_content;
ServerBase(unsigned short port, size_t num_threads, long timeout_request, long timeout_send_or_receive) :
config(port, num_threads), acceptor(io_service),
timeout_request(timeout_request), timeout_content(timeout_send_or_receive) {}
virtual void accept()=0;
std::shared_ptr<boost::asio::deadline_timer> set_timeout_on_socket(std::shared_ptr<socket_type> socket, long seconds) {
std::shared_ptr<boost::asio::deadline_timer> timer(new boost::asio::deadline_timer(io_service));
timer->expires_from_now(boost::posix_time::seconds(seconds));
timer->async_wait([socket](const boost::system::error_code& ec){
if(!ec) {
boost::system::error_code ec;
socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
socket->lowest_layer().close();
}
});
return timer;
}
void read_request_and_content(std::shared_ptr<socket_type> socket) {
//Create new streambuf (Request::streambuf) for async_read_until()
//shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<Request> request(new Request());
request->read_remote_endpoint_data(*socket);
//Set timeout on the following boost::asio::async-read or write function
std::shared_ptr<boost::asio::deadline_timer> timer;
if(timeout_request>0)
timer=set_timeout_on_socket(socket, timeout_request);
boost::asio::async_read_until(*socket, request->streambuf, "\r\n\r\n",
[this, socket, request, timer](const boost::system::error_code& ec, size_t bytes_transferred) {
if(timeout_request>0)
timer->cancel();
if(!ec) {
//request->streambuf.size() is not necessarily the same as bytes_transferred, from Boost-docs:
//"After a successful async_read_until operation, the streambuf may contain additional data beyond the delimiter"
//The chosen solution is to extract lines from the stream directly when parsing the header. What is left of the
//streambuf (maybe some bytes of the content) is appended to in the async_read-function below (for retrieving content).
size_t num_additional_bytes=request->streambuf.size()-bytes_transferred;
if(!parse_request(request, request->content))
return;
//If content, read that as well
auto it=request->header.find("Content-Length");
if(it!=request->header.end()) {
//Set timeout on the following boost::asio::async-read or write function
std::shared_ptr<boost::asio::deadline_timer> timer;
if(timeout_content>0)
timer=set_timeout_on_socket(socket, timeout_content);
unsigned long long content_length;
try {
content_length=stoull(it->second);
}
catch(const std::exception &) {
return;
}
if(content_length>num_additional_bytes) {
boost::asio::async_read(*socket, request->streambuf,
boost::asio::transfer_exactly(content_length-num_additional_bytes),
[this, socket, request, timer]
(const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(timeout_content>0)
timer->cancel();
if(!ec)
find_resource(socket, request);
});
}
else {
if(timeout_content>0)
timer->cancel();
find_resource(socket, request);
}
}
else {
find_resource(socket, request);
}
}
});
}
bool parse_request(std::shared_ptr<Request> request, std::istream& stream) const {
std::string line;
getline(stream, line);
size_t method_end;
if((method_end=line.find(' '))!=std::string::npos) {
size_t path_end;
if((path_end=line.find(' ', method_end+1))!=std::string::npos) {
request->method=line.substr(0, method_end);
request->path=line.substr(method_end+1, path_end-method_end-1);
size_t protocol_end;
if((protocol_end=line.find('/', path_end+1))!=std::string::npos) {
if(line.substr(path_end+1, protocol_end-path_end-1)!="HTTP")
return false;
request->http_version=line.substr(protocol_end+1, line.size()-protocol_end-2);
}
else
return false;
getline(stream, line);
size_t param_end;
while((param_end=line.find(':'))!=std::string::npos) {
size_t value_start=param_end+1;
if((value_start)<line.size()) {
if(line[value_start]==' ')
value_start++;
if(value_start<line.size())
request->header.insert(std::make_pair(line.substr(0, param_end), line.substr(value_start, line.size()-value_start-1)));
}
getline(stream, line);
}
}
else
return false;
}
else
return false;
return true;
}
void find_resource(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request) {
//Find path- and method-match, and call write_response
for(auto& res: opt_resource) {
if(request->method==res.first) {
for(auto& res_path: res.second) {
boost::smatch sm_res;
if(boost::regex_match(request->path, sm_res, res_path.first)) {
request->path_match=std::move(sm_res);
write_response(socket, request, res_path.second);
return;
}
}
}
}
auto it_method=default_resource.find(request->method);
if(it_method!=default_resource.end()) {
write_response(socket, request, it_method->second);
}
}
void write_response(std::shared_ptr<socket_type> socket, std::shared_ptr<Request> request,
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>,
std::shared_ptr<typename ServerBase<socket_type>::Request>)>& resource_function) {
//Set timeout on the following boost::asio::async-read or write function
std::shared_ptr<boost::asio::deadline_timer> timer;
if(timeout_content>0)
timer=set_timeout_on_socket(socket, timeout_content);
auto response=std::shared_ptr<Response>(new Response(socket), [this, request, timer](Response *response_ptr) {
auto response=std::shared_ptr<Response>(response_ptr);
send(response, [this, response, request, timer](const boost::system::error_code& ec) {
if(!ec) {
if(timeout_content>0)
timer->cancel();
float http_version;
try {
http_version=stof(request->http_version);
}
catch(const std::exception &) {
return;
}
auto range=request->header.equal_range("Connection");
for(auto it=range.first;it!=range.second;it++) {
if(boost::iequals(it->second, "close"))
return;
}
if(http_version>1.05)
read_request_and_content(response->socket);
}
});
});
try {
resource_function(response, request);
}
catch(const std::exception&) {
return;
}
}
};
template<class socket_type>
class Server : public ServerBase<socket_type> {};
typedef boost::asio::ip::tcp::socket HTTP;
template<>
class Server<HTTP> : public ServerBase<HTTP> {
public:
Server(unsigned short port, size_t num_threads=1, long timeout_request=5, long timeout_content=300) :
ServerBase<HTTP>::ServerBase(port, num_threads, timeout_request, timeout_content) {}
protected:
void accept() {
//Create new socket for this connection
//Shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<HTTP> socket(new HTTP(io_service));
acceptor.async_accept(*socket, [this, socket](const boost::system::error_code& ec){
//Immediately start accepting a new connection
accept();
if(!ec) {
boost::asio::ip::tcp::no_delay option(true);
socket->set_option(option);
read_request_and_content(socket);
}
});
}
};
}
#endif /* SERVER_HTTP_HPP */

774
toREST/lib/server_ws.hpp

@ -1,774 +0,0 @@
#ifndef SERVER_WS_HPP
#define SERVER_WS_HPP
#include "crypto.hpp"
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/functional/hash.hpp>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <unordered_set>
#include <list>
#include <memory>
#include <atomic>
#include <iostream>
#ifndef CASE_INSENSITIVE_EQUALS_AND_HASH
#define CASE_INSENSITIVE_EQUALS_AND_HASH
//Based on http://www.boost.org/doc/libs/1_60_0/doc/html/unordered/hash_equality.html
class case_insensitive_equals {
public:
bool operator()(const std::string &key1, const std::string &key2) const {
return boost::algorithm::iequals(key1, key2);
}
};
class case_insensitive_hash {
public:
size_t operator()(const std::string &key) const {
std::size_t seed=0;
for(auto &c: key)
boost::hash_combine(seed, std::tolower(c));
return seed;
}
};
#endif
// Late 2017 TODO: remove the following checks and always use std::regex
#ifdef USE_BOOST_REGEX
#include <boost/regex.hpp>
#define REGEX_NS boost
#else
#include <regex>
#define REGEX_NS std
#endif
// TODO when switching to c++14, use [[deprecated]] instead
#ifndef DEPRECATED
#ifdef __GNUC__
#define DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define DEPRECATED __declspec(deprecated)
#else
#define DEPRECATED
#endif
#endif
namespace SimpleWeb {
// TODO: remove when onopen, onmessage, etc is removed:
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4996)
#endif
template <class socket_type>
class SocketServer;
template <class socket_type>
class SocketServerBase {
public:
virtual ~SocketServerBase() {}
class SendStream : public std::ostream {
friend class SocketServerBase<socket_type>;
private:
boost::asio::streambuf streambuf;
public:
SendStream(): std::ostream(&streambuf) {}
size_t size() {
return streambuf.size();
}
};
class Connection {
friend class SocketServerBase<socket_type>;
friend class SocketServer<socket_type>;
public:
Connection(const std::shared_ptr<socket_type> &socket): socket(socket), strand(socket->get_io_service()), closed(false) {}
std::string method, path, http_version;
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header;
REGEX_NS::smatch path_match;
std::string remote_endpoint_address;
unsigned short remote_endpoint_port;
private:
Connection(socket_type *socket): socket(socket), strand(socket->get_io_service()), closed(false) {}
class SendData {
public:
SendData(const std::shared_ptr<SendStream> &header_stream, const std::shared_ptr<SendStream> &message_stream,
const std::function<void(const boost::system::error_code)> &callback) :
header_stream(header_stream), message_stream(message_stream), callback(callback) {}
std::shared_ptr<SendStream> header_stream;
std::shared_ptr<SendStream> message_stream;
std::function<void(const boost::system::error_code)> callback;
};
std::shared_ptr<socket_type> socket;
boost::asio::strand strand;
std::list<SendData> send_queue;
void send_from_queue(const std::shared_ptr<Connection> &connection) {
strand.post([this, connection]() {
boost::asio::async_write(*socket, send_queue.begin()->header_stream->streambuf,
strand.wrap([this, connection](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(!ec) {
boost::asio::async_write(*socket, send_queue.begin()->message_stream->streambuf,
strand.wrap([this, connection]
(const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
auto send_queued=send_queue.begin();
if(send_queued->callback)
send_queued->callback(ec);
if(!ec) {
send_queue.erase(send_queued);
if(send_queue.size()>0)
send_from_queue(connection);
}
else
send_queue.clear();
}));
}
else {
auto send_queued=send_queue.begin();
if(send_queued->callback)
send_queued->callback(ec);
send_queue.clear();
}
}));
});
}
std::atomic<bool> closed;
std::unique_ptr<boost::asio::deadline_timer> timer_idle;
void read_remote_endpoint_data() {
try {
remote_endpoint_address=socket->lowest_layer().remote_endpoint().address().to_string();
remote_endpoint_port=socket->lowest_layer().remote_endpoint().port();
}
catch(...) {}
}
};
class Message : public std::istream {
friend class SocketServerBase<socket_type>;
public:
unsigned char fin_rsv_opcode;
size_t size() {
return length;
}
std::string string() {
std::stringstream ss;
ss << rdbuf();
return ss.str();
}
private:
Message(): std::istream(&streambuf) {}
size_t length;
boost::asio::streambuf streambuf;
};
class Endpoint {
friend class SocketServerBase<socket_type>;
private:
std::unordered_set<std::shared_ptr<Connection> > connections;
std::mutex connections_mutex;
public:
DEPRECATED std::function<void(std::shared_ptr<Connection>)> onopen;
std::function<void(std::shared_ptr<Connection>)> on_open;
DEPRECATED std::function<void(std::shared_ptr<Connection>, std::shared_ptr<Message>)> onmessage;
std::function<void(std::shared_ptr<Connection>, std::shared_ptr<Message>)> on_message;
DEPRECATED std::function<void(std::shared_ptr<Connection>, int, const std::string&)> onclose;
std::function<void(std::shared_ptr<Connection>, int, const std::string&)> on_close;
DEPRECATED std::function<void(std::shared_ptr<Connection>, const boost::system::error_code&)> onerror;
std::function<void(std::shared_ptr<Connection>, const boost::system::error_code&)> on_error;
std::unordered_set<std::shared_ptr<Connection> > get_connections() {
std::lock_guard<std::mutex> lock(connections_mutex);
auto copy=connections;
return copy;
}
};
class Config {
friend class SocketServerBase<socket_type>;
private:
Config(unsigned short port): port(port) {}
public:
/// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS.
unsigned short port;
/// Number of threads that the server will use when start() is called. Defaults to 1 thread.
size_t thread_pool_size=1;
/// Timeout on request handling. Defaults to 5 seconds.
size_t timeout_request=5;
/// Idle timeout. Defaults to no timeout.
size_t timeout_idle=0;
/// IPv4 address in dotted decimal form or IPv6 address in hexadecimal notation.
/// If empty, the address will be any address.
std::string address;
/// Set to false to avoid binding the socket to an address that is already in use. Defaults to true.
bool reuse_address=true;
};
///Set before calling start().
Config config;
private:
class regex_orderable : public REGEX_NS::regex {
std::string str;
public:
regex_orderable(const char *regex_cstr) : REGEX_NS::regex(regex_cstr), str(regex_cstr) {}
regex_orderable(const std::string &regex_str) : REGEX_NS::regex(regex_str), str(regex_str) {}
bool operator<(const regex_orderable &rhs) const {
return str<rhs.str;
}
};
public:
/// Warning: do not add or remove endpoints after start() is called
std::map<regex_orderable, Endpoint> endpoint;
virtual void start() {
for(auto &endp: endpoint) {
// TODO: remove when onopen, onmessage, etc is removed:
if(endp.second.onopen)
endp.second.on_open=endp.second.onopen;
if(endp.second.onmessage)
endp.second.on_message=endp.second.onmessage;
if(endp.second.onclose)
endp.second.on_close=endp.second.onclose;
if(endp.second.onerror)
endp.second.on_error=endp.second.onerror;
}
if(!io_service)
io_service=std::make_shared<boost::asio::io_service>();
if(io_service->stopped())
io_service->reset();
boost::asio::ip::tcp::endpoint endpoint;
if(config.address.size()>0)
endpoint=boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(config.address), config.port);
else
endpoint=boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), config.port);
if(!acceptor)
acceptor=std::unique_ptr<boost::asio::ip::tcp::acceptor>(new boost::asio::ip::tcp::acceptor(*io_service));
acceptor->open(endpoint.protocol());
acceptor->set_option(boost::asio::socket_base::reuse_address(config.reuse_address));
acceptor->bind(endpoint);
acceptor->listen();
accept();
//If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling
threads.clear();
for(size_t c=1;c<config.thread_pool_size;c++) {
threads.emplace_back([this](){
io_service->run();
});
}
//Main thread
if(config.thread_pool_size>0)
io_service->run();
//Wait for the rest of the threads, if any, to finish as well
for(auto& t: threads) {
t.join();
}
}
void stop() {
acceptor->close();
if(config.thread_pool_size>0)
io_service->stop();
for(auto &pair: endpoint) {
std::lock_guard<std::mutex> lock(pair.second.connections_mutex);
for(auto &connection: pair.second.connections) {
connection->socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both);
connection->socket->lowest_layer().close();
}
pair.second.connections.clear();
}
}
///fin_rsv_opcode: 129=one fragment, text, 130=one fragment, binary, 136=close connection.
///See http://tools.ietf.org/html/rfc6455#section-5.2 for more information
void send(const std::shared_ptr<Connection> &connection, const std::shared_ptr<SendStream> &message_stream,
const std::function<void(const boost::system::error_code&)>& callback=nullptr,
unsigned char fin_rsv_opcode=129) const {
if(fin_rsv_opcode!=136)
timer_idle_reset(connection);
auto header_stream=std::make_shared<SendStream>();
size_t length=message_stream->size();
header_stream->put(fin_rsv_opcode);
//unmasked (first length byte<128)
if(length>=126) {
int num_bytes;
if(length>0xffff) {
num_bytes=8;
header_stream->put(127);
}
else {
num_bytes=2;
header_stream->put(126);
}
for(int c=num_bytes-1;c>=0;c--) {
header_stream->put((static_cast<unsigned long long>(length) >> (8 * c)) % 256);
}
}
else
header_stream->put(static_cast<unsigned char>(length));
connection->strand.post([this, connection, header_stream, message_stream, callback]() {
connection->send_queue.emplace_back(header_stream, message_stream, callback);
if(connection->send_queue.size()==1)
connection->send_from_queue(connection);
});
}
void send_close(const std::shared_ptr<Connection> &connection, int status, const std::string& reason="",
const std::function<void(const boost::system::error_code&)>& callback=nullptr) const {
//Send close only once (in case close is initiated by server)
if(connection->closed)
return;
connection->closed=true;
auto send_stream=std::make_shared<SendStream>();
send_stream->put(status>>8);
send_stream->put(status%256);
*send_stream << reason;
//fin_rsv_opcode=136: message close
send(connection, send_stream, callback, 136);
}
std::unordered_set<std::shared_ptr<Connection> > get_connections() {
std::unordered_set<std::shared_ptr<Connection> > all_connections;
for(auto& e: endpoint) {
std::lock_guard<std::mutex> lock(e.second.connections_mutex);
all_connections.insert(e.second.connections.begin(), e.second.connections.end());
}
return all_connections;
}
/**
* Upgrades a request, from for instance Simple-Web-Server, to a WebSocket connection.
* The parameters are moved to the Connection object.
* See also Server::on_upgrade in the Simple-Web-Server project.
* The socket's io_service is used, thus running start() is not needed.
*
* Example use:
* server.on_upgrade=[&socket_server] (auto socket, auto request) {
* auto connection=std::make_shared<SimpleWeb::SocketServer<SimpleWeb::WS>::Connection>(socket);
* connection->method=std::move(request->method);
* connection->path=std::move(request->path);
* connection->http_version=std::move(request->http_version);
* connection->header=std::move(request->header);
* connection->remote_endpoint_address=std::move(request->remote_endpoint_address);
* connection->remote_endpoint_port=request->remote_endpoint_port;
* socket_server.upgrade(connection);
* }
*/
void upgrade(const std::shared_ptr<Connection> &connection) {
auto read_buffer=std::make_shared<boost::asio::streambuf>();
write_handshake(connection, read_buffer);
}
/// If you have your own boost::asio::io_service, store its pointer here before running start().
/// You might also want to set config.thread_pool_size to 0.
std::shared_ptr<boost::asio::io_service> io_service;
protected:
const std::string ws_magic_string="258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor;
std::vector<std::thread> threads;
SocketServerBase(unsigned short port) : config(port) {}
virtual void accept()=0;
std::shared_ptr<boost::asio::deadline_timer> get_timeout_timer(const std::shared_ptr<Connection> &connection, size_t seconds) {
if(seconds==0)
return nullptr;
auto timer=std::make_shared<boost::asio::deadline_timer>(connection->socket->get_io_service());
timer->expires_from_now(boost::posix_time::seconds(static_cast<long>(seconds)));
timer->async_wait([connection](const boost::system::error_code& ec){
if(!ec) {
connection->socket->lowest_layer().shutdown(boost::asio::ip::tcp::socket::shutdown_both);
connection->socket->lowest_layer().close();
}
});
return timer;
}
void read_handshake(const std::shared_ptr<Connection> &connection) {
connection->read_remote_endpoint_data();
//Create new read_buffer for async_read_until()
//Shared_ptr is used to pass temporary objects to the asynchronous functions
auto read_buffer=std::make_shared<boost::asio::streambuf>();
//Set timeout on the following boost::asio::async-read or write function
auto timer=get_timeout_timer(connection, config.timeout_request);
boost::asio::async_read_until(*connection->socket, *read_buffer, "\r\n\r\n",
[this, connection, read_buffer, timer]
(const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(timer)
timer->cancel();
if(!ec) {
//Convert to istream to extract string-lines
std::istream stream(read_buffer.get());
parse_handshake(connection, stream);
write_handshake(connection, read_buffer);
}
});
}
void parse_handshake(const std::shared_ptr<Connection> &connection, std::istream& stream) const {
std::string line;
getline(stream, line);
size_t method_end;
if((method_end=line.find(' '))!=std::string::npos) {
size_t path_end;
if((path_end=line.find(' ', method_end+1))!=std::string::npos) {
connection->method=line.substr(0, method_end);
connection->path=line.substr(method_end+1, path_end-method_end-1);
if((path_end+6)<line.size())
connection->http_version=line.substr(path_end+6, line.size()-(path_end+6)-1);
else
connection->http_version="1.1";
getline(stream, line);
size_t param_end;
while((param_end=line.find(':'))!=std::string::npos) {
size_t value_start=param_end+1;
if((value_start)<line.size()) {
if(line[value_start]==' ')
value_start++;
if(value_start<line.size())
connection->header.emplace(line.substr(0, param_end), line.substr(value_start, line.size()-value_start-1));
}
getline(stream, line);
}
}
}
}
void write_handshake(const std::shared_ptr<Connection> &connection, const std::shared_ptr<boost::asio::streambuf> &read_buffer) {
//Find path- and method-match, and generate response
for(auto &regex_endpoint: endpoint) {
REGEX_NS::smatch path_match;
if(REGEX_NS::regex_match(connection->path, path_match, regex_endpoint.first)) {
auto write_buffer=std::make_shared<boost::asio::streambuf>();
std::ostream handshake(write_buffer.get());
if(generate_handshake(connection, handshake)) {
connection->path_match=std::move(path_match);
//Capture write_buffer in lambda so it is not destroyed before async_write is finished
boost::asio::async_write(*connection->socket, *write_buffer,
[this, connection, write_buffer, read_buffer, &regex_endpoint]
(const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(!ec) {
connection_open(connection, regex_endpoint.second);
read_message(connection, read_buffer, regex_endpoint.second);
}
else
connection_error(connection, regex_endpoint.second, ec);
});
}
return;
}
}
}
bool generate_handshake(const std::shared_ptr<Connection> &connection, std::ostream& handshake) const {
auto header_it=connection->header.find("Sec-WebSocket-Key");
if(header_it==connection->header.end())
return false;
auto sha1=Crypto::sha1(header_it->second+ws_magic_string);
handshake << "HTTP/1.1 101 Web Socket Protocol Handshake\r\n";
handshake << "Upgrade: websocket\r\n";
handshake << "Connection: Upgrade\r\n";
handshake << "Sec-WebSocket-Accept: " << Crypto::Base64::encode(sha1) << "\r\n";
handshake << "\r\n";
return true;
}
void read_message(const std::shared_ptr<Connection> &connection,
const std::shared_ptr<boost::asio::streambuf> &read_buffer, Endpoint& endpoint) const {
boost::asio::async_read(*connection->socket, *read_buffer, boost::asio::transfer_exactly(2),
[this, connection, read_buffer, &endpoint]
(const boost::system::error_code& ec, size_t bytes_transferred) {
if(!ec) {
if(bytes_transferred==0) { //TODO: why does this happen sometimes?
read_message(connection, read_buffer, endpoint);
return;
}
std::istream stream(read_buffer.get());
std::vector<unsigned char> first_bytes;
first_bytes.resize(2);
stream.read((char*)&first_bytes[0], 2);
unsigned char fin_rsv_opcode=first_bytes[0];
//Close connection if unmasked message from client (protocol error)
if(first_bytes[1]<128) {
const std::string reason("message from client not masked");
send_close(connection, 1002, reason, [this, connection](const boost::system::error_code& /*ec*/) {});
connection_close(connection, endpoint, 1002, reason);
return;
}
size_t length=(first_bytes[1]&127);
if(length==126) {
//2 next bytes is the size of content
boost::asio::async_read(*connection->socket, *read_buffer, boost::asio::transfer_exactly(2),
[this, connection, read_buffer, &endpoint, fin_rsv_opcode]
(const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(!ec) {
std::istream stream(read_buffer.get());
std::vector<unsigned char> length_bytes;
length_bytes.resize(2);
stream.read((char*)&length_bytes[0], 2);
size_t length=0;
int num_bytes=2;
for(int c=0;c<num_bytes;c++)
length+=length_bytes[c]<<(8*(num_bytes-1-c));
read_message_content(connection, read_buffer, length, endpoint, fin_rsv_opcode);
}
else
connection_error(connection, endpoint, ec);
});
}
else if(length==127) {
//8 next bytes is the size of content
boost::asio::async_read(*connection->socket, *read_buffer, boost::asio::transfer_exactly(8),
[this, connection, read_buffer, &endpoint, fin_rsv_opcode]
(const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(!ec) {
std::istream stream(read_buffer.get());
std::vector<unsigned char> length_bytes;
length_bytes.resize(8);
stream.read((char*)&length_bytes[0], 8);
size_t length=0;
int num_bytes=8;
for(int c=0;c<num_bytes;c++)
length+=length_bytes[c]<<(8*(num_bytes-1-c));
read_message_content(connection, read_buffer, length, endpoint, fin_rsv_opcode);
}
else
connection_error(connection, endpoint, ec);
});
}
else
read_message_content(connection, read_buffer, length, endpoint, fin_rsv_opcode);
}
else
connection_error(connection, endpoint, ec);
});
}
void read_message_content(const std::shared_ptr<Connection> &connection, const std::shared_ptr<boost::asio::streambuf> &read_buffer,
size_t length, Endpoint& endpoint, unsigned char fin_rsv_opcode) const {
boost::asio::async_read(*connection->socket, *read_buffer, boost::asio::transfer_exactly(4+length),
[this, connection, read_buffer, length, &endpoint, fin_rsv_opcode]
(const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
if(!ec) {
std::istream raw_message_data(read_buffer.get());
//Read mask
std::vector<unsigned char> mask;
mask.resize(4);
raw_message_data.read((char*)&mask[0], 4);
std::shared_ptr<Message> message(new Message());
message->length=length;
message->fin_rsv_opcode=fin_rsv_opcode;
std::ostream message_data_out_stream(&message->streambuf);
for(size_t c=0;c<length;c++) {
message_data_out_stream.put(raw_message_data.get()^mask[c%4]);
}
//If connection close
if((fin_rsv_opcode&0x0f)==8) {
int status=0;
if(length>=2) {
unsigned char byte1=message->get();
unsigned char byte2=message->get();
status=(byte1<<8)+byte2;
}
auto reason=message->string();
send_close(connection, status, reason, [this, connection](const boost::system::error_code& /*ec*/) {});
connection_close(connection, endpoint, status, reason);
return;
}
else {
//If ping
if((fin_rsv_opcode&0x0f)==9) {
//send pong
auto empty_send_stream=std::make_shared<SendStream>();
send(connection, empty_send_stream, nullptr, fin_rsv_opcode+1);
}
else if(endpoint.on_message) {
timer_idle_reset(connection);
endpoint.on_message(connection, message);
}
//Next message
read_message(connection, read_buffer, endpoint);
}
}
else
connection_error(connection, endpoint, ec);
});
}
void connection_open(const std::shared_ptr<Connection> &connection, Endpoint& endpoint) {
timer_idle_init(connection);
{
std::lock_guard<std::mutex> lock(endpoint.connections_mutex);
endpoint.connections.insert(connection);
}
if(endpoint.on_open)
endpoint.on_open(connection);
}
void connection_close(const std::shared_ptr<Connection> &connection, Endpoint& endpoint, int status, const std::string& reason) const {
timer_idle_cancel(connection);
{
std::lock_guard<std::mutex> lock(endpoint.connections_mutex);
endpoint.connections.erase(connection);
}
if(endpoint.on_close)
endpoint.on_close(connection, status, reason);
}
void connection_error(const std::shared_ptr<Connection> &connection, Endpoint& endpoint, const boost::system::error_code& ec) const {
timer_idle_cancel(connection);
{
std::lock_guard<std::mutex> lock(endpoint.connections_mutex);
endpoint.connections.erase(connection);
}
if(endpoint.on_error)
endpoint.on_error(connection, ec);
}
void timer_idle_init(const std::shared_ptr<Connection> &connection) {
if(config.timeout_idle>0) {
connection->timer_idle=std::unique_ptr<boost::asio::deadline_timer>(new boost::asio::deadline_timer(connection->socket->get_io_service()));
connection->timer_idle->expires_from_now(boost::posix_time::seconds(static_cast<unsigned long>(config.timeout_idle)));
timer_idle_expired_function(connection);
}
}
void timer_idle_reset(const std::shared_ptr<Connection> &connection) const {
if(config.timeout_idle>0 && connection->timer_idle->expires_from_now(boost::posix_time::seconds(static_cast<unsigned long>(config.timeout_idle)))>0)
timer_idle_expired_function(connection);
}
void timer_idle_cancel(const std::shared_ptr<Connection> &connection) const {
if(config.timeout_idle>0)
connection->timer_idle->cancel();
}
void timer_idle_expired_function(const std::shared_ptr<Connection> &connection) const {
connection->timer_idle->async_wait([this, connection](const boost::system::error_code& ec){
if(!ec)
send_close(connection, 1000, "idle timeout"); //1000=normal closure
});
}
};
template<class socket_type>
class SocketServer : public SocketServerBase<socket_type> {};
typedef boost::asio::ip::tcp::socket WS;
template<>
class SocketServer<WS> : public SocketServerBase<WS> {
public:
DEPRECATED SocketServer(unsigned short port, size_t thread_pool_size=1, size_t timeout_request=5, size_t timeout_idle=0) :
SocketServer() {
config.port=port;
config.thread_pool_size=thread_pool_size;
config.timeout_request=timeout_request;
config.timeout_idle=timeout_idle;
};
SocketServer() : SocketServerBase<WS>(80) {}
protected:
void accept() {
//Create new socket for this connection (stored in Connection::socket)
//Shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<Connection> connection(new Connection(new WS(*io_service)));
acceptor->async_accept(*connection->socket, [this, connection](const boost::system::error_code& ec) {
//Immediately start accepting a new connection (if io_service hasn't been stopped)
if (ec != boost::asio::error::operation_aborted)
accept();
if(!ec) {
boost::asio::ip::tcp::no_delay option(true);
connection->socket->set_option(option);
read_handshake(connection);
}
});
}
};
// TODO: remove when onopen, onmessage, etc is removed:
#ifdef __GNUC__
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif
}
#endif /* SERVER_WS_HPP */

12
toREST/tests/config_test.cpp

@ -1,12 +0,0 @@
#include <Catch/catch.hpp>
#include <config.hpp>
SCENARIO("The configuration have default properties") {
tr::config config;
REQUIRE_FALSE(config.default_download_dir.empty());
REQUIRE_FALSE(config.root_dir.empty());
REQUIRE(config.default_download_dir == "toREST");
REQUIRE(config.http_port == 8080);
REQUIRE(config.http_threads == 1);
REQUIRE(config.ws_port == 3000);
}

65
toREST/tests/http_test.cpp

@ -1,65 +0,0 @@
#include <Catch/catch.hpp>
#include <http.hpp>
TEST_CASE("Check http status") {
auto out = http::code(http::ok);
REQUIRE(out.first == 200);
REQUIRE(out.second == "OK");
}
SCENARIO("http response") {
auto response = http::response();
std::string body;
std::stringstream ss;
GIVEN("we stream our response without setting given any data") {
ss << response;
THEN("the response will default to 200")
REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\n200 OK");
}
GIVEN("we stream our data with the response status set to 404") {
response.set_status(http::not_found);
ss << response;
THEN("the default 404 response will be sent instead") {
REQUIRE(ss.str() == "HTTP/1.1 404 Not Found\r\nContent-Length: 13\r\n\r\n404 Not Found");
}
}
GIVEN("we stream our response with the status set to 204 No Content") {
response.set_status(http::no_content);
ss << response;
THEN("the response has no content header set")
REQUIRE(ss.str() == "HTTP/1.1 204 No Content\r\n\r\n");
}
GIVEN("we stream our response with the body set") {
body = "Easter eggs, everywhere! <a href=\"http://stream1.gifsoup.com/view4/4086928/x-x-everywhere-o.gif\">ye</a>";
response.set_body(body);
ss << response;
THEN("our response contains the appropriate headers")
REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nContent-Length: 103\r\n\r\nEaster eggs, everywhere! <a href=\"http://stream1.gifsoup.com/view4/4086928/x-x-everywhere-o.gif\">ye</a>");
}
GIVEN("we add a header to the resource") {
body = "Nothing";
response.set_body(body);
response.add_header(http::header("Strict-Error", "on"));
ss << response;
THEN("our response contains the appropriate headers")
REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nStrict-Error: on\r\nContent-Length: 7\r\n\r\nNothing");
}
}
SCENARIO("Stream http responses with a json response") {
auto response = http::response();
std::stringstream ss;
GIVEN("we set the response code to 404 and stream our response") {
response.set_status(http::not_found);
response.set_body({{"code", 404}, {"status", "Not Found"}});
ss << response;
THEN("our response is valid json and the resoinse code is 404")
REQUIRE(ss.str() == "HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\nContent-Length: 33\r\n\r\n{\"code\":404,\"status\":\"Not Found\"}");
}
GIVEN("we stream our response with the body set") {
response.set_body(nlohmann::json::object());
ss << response;
THEN("our response contains the appropriate headers")
REQUIRE(ss.str() == "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\n\r\n{}");
}
}

7
toREST/tests/include/ConfigContext.hpp

@ -1,7 +0,0 @@
#define _TR_TESTING_
#ifndef _TR_TEST_CONFIG_CONTEXT_HPP_
#define _TR_TEST_CONFIG_CONTEXT_HPP_
#include <config.hpp>
#endif

37
toREST/tests/include/ServerContext.hpp

@ -1,37 +0,0 @@
#define _TR_TESTING_
#ifndef _TR_TEST_SERVER_CONTEXT_HPP_
#define _TR_TEST_SERVER_CONTEXT_HPP_
#include <Catch/catch.hpp>
#include <json.hpp>
#include <sstream>
#include <unordered_map>
class TestRequest {
public:
std::stringstream content;
std::string path;
};
class TestResponse : public std::stringstream {
public:
std::string buffer;
std::unordered_map<std::string, std::string> headers_;
std::string string();
std::string message();
std::string code();
std::string content();
const std::unordered_map<std::string, std::string> &headers();
};
class CommonResponse {
public:
static void service_unavailable(std::shared_ptr<TestResponse> response);
static void ok(std::shared_ptr<TestResponse> response, const nlohmann::json &data);
static void bad_request(std::shared_ptr<TestResponse> response);
static void accepted(std::shared_ptr<TestResponse> response);
static void no_content(std::shared_ptr<TestResponse> response);
static void created(std::shared_ptr<TestResponse> response, std::shared_ptr<TestRequest> request, const std::string &location);
};
#endif

66
toREST/tests/include/SessionContext.hpp

@ -1,66 +0,0 @@
#ifndef _TR_TEST_SESSION_CONTEXT_HPP_
#define _TR_TEST_SESSION_CONTEXT_HPP_
#include <TorrentContext.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <sstream>
#include <vector>
class TestSessionSettings {
public:
int download_rate_limit = 1;
int upload_rate_limit = 2;
int listen_interfaces = 3;
int enable_dht = 4;
bool enable_dht_ = true;
int download_rate_limit_ = 0;
int upload_rate_limit_ = 0;
std::string listen_interfaces_ = "0.0.0.0:0";
void set_bool(int type, int value);
void set_int(int type, int value);
void set_str(int type, std::string value);
int get_int(int type) const;
bool get_bool(int type);
std::string get_str(int type);
};
class TestSession {
public:
TestSession() {
invalid_.valid = false;
}
bool valid = false;
bool paused = false;
std::vector<TestTorrent> torrents_;
TestSessionSettings settings_;
bool is_valid();
TestSessionSettings get_settings();
bool is_paused() const;
void pause();
void resume();
int listen_port();
bool is_dht_running() const;
std::vector<TestTorrent> &get_torrents();
TestTorrent &find_torrent(const libtorrent::sha1_hash &hash);
void remove_torrent(const TestTorrent &torrent, int options);
void apply_settings(TestSessionSettings settings);
void async_add_torrent(const libtorrent::add_torrent_params &params);
void post_torrent_updates(int flags = 0);
int post_torrent_updates_calls_ = 0;
void theTorrentExists();
TestTorrent invalid_;
};
const auto magnet_uri = "magnet:?xt=urn:btih:" + torrent_id + "&dn=Taylor Swift&tr=http://tracker.sout.no";
const nlohmann::json session_json = {
{"dht_enabled", true},
{"down_limit", 0},
{"paused", false},
{"port", 0},
{"torrents", 0},
{"up_limit", 0}};
#endif

56
toREST/tests/include/TorrentContext.hpp

@ -1,56 +0,0 @@
#define _TR_TESTING_
#ifndef _TR_TEST_TORRENT_CONTEXT_HPP_
#define _TR_TEST_TORRENT_CONTEXT_HPP_
#include <json.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <string>
const auto torrent_url = "http://sout.no/torrent.torrent";
const std::string torrent_id = "c0b0a90089710812fe8c37385a4cc2978eabf3e8";
class TorrentStatus {
public:
bool paused = false;
bool is_seeding = false;
bool announcing_to_dht = true;
int state = 0;
int priority = 0;
std::string name = "Arch";
std::string save_path = "";
};
class TestTorrent {
public:
TestTorrent(const libtorrent::add_torrent_params &params);
TestTorrent() {}
operator bool() const { return is_valid(); };
bool is_valid() const;
libtorrent::sha1_hash info_hash() const;
TorrentStatus &status(int type = 0);
int query_name = 1;
int query_save_path = 2;
bool valid = true;
int upload_limit_ = 0;
int upload_limit() const;
void set_download_limit(int limit);
void set_upload_limit(int limit);
int download_limit_ = 0;
int download_limit() const;
void pause(int flags = 0);
void resume();
libtorrent::sha1_hash hash_;
TorrentStatus status_;
};
const nlohmann::json torrent_json = {
{"info_hash", torrent_id},
{"paused", false},
{"seeding", false},
{"state", 0},
{"priority", 0},
{"up_limit", 0},
{"down_limit", 0},
{"name", "Arch"}};
#endif

80
toREST/tests/session_test.cpp

@ -1,80 +0,0 @@
#include <Catch/catch.hpp>
#include <ServerContext.hpp>
#include <SessionContext.hpp>
#include <session.hpp>
SCENARIO("We are running a GET /session resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
AND_WHEN("we recive a request") {
tr::session::get(torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
}
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
auto data = session_json;
WHEN("the server is paused") {
torrent_session.paused = true;
data.at("paused") = true;
THEN("the server should reply with the paused field set to true") {
tr::session::get(torrent_session, response, request);
CommonResponse::ok(response, data);
}
}
WHEN("the server is not paused") {
tr::session::get(torrent_session, response, request);
THEN("the server should reply with the paused field set to true") {
CommonResponse::ok(response, session_json);
}
}
}
}
SCENARIO("We recive a PATCH request on the /session resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
tr::session::patch(torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
GIVEN("the server recives invalid request") {
request->content << "Not valid json";
tr::session::patch(torrent_session, response, request);
THEN("the server should respond with bad request") {
CommonResponse::bad_request(response);
}
}
GIVEN("the server recives a valid request") {
const nlohmann::json obj({
{"dht_enabled", false},
{"paused", true},
{"port", 1},
{"down_limit", 100},
{"up_limit", 100},
});
request->content << obj;
REQUIRE_FALSE(torrent_session.paused);
REQUIRE(torrent_session.settings_.enable_dht_);
REQUIRE(torrent_session.listen_port() == 0);
REQUIRE(torrent_session.settings_.download_rate_limit_ == 0);
REQUIRE(torrent_session.settings_.upload_rate_limit_ == 0);
tr::session::patch(torrent_session, response, request);
CommonResponse::accepted(response);
REQUIRE(torrent_session.paused);
REQUIRE_FALSE(torrent_session.settings_.enable_dht_);
REQUIRE(torrent_session.listen_port() == 1);
REQUIRE(torrent_session.settings_.download_rate_limit_ == 100);
REQUIRE(torrent_session.settings_.upload_rate_limit_ == 100);
}
}
}

110
toREST/tests/stubs/ServerContext.cpp

@ -1,110 +0,0 @@
#include <ServerContext.hpp>
std::string TestResponse::string() {
buffer = str();
return buffer;
}
std::string TestResponse::message() {
if (buffer.empty())
string();
auto msg = buffer.substr(13, buffer.find('\r') - 13);
return msg;
}
std::string TestResponse::code() {
if (buffer.empty())
string();
return buffer.substr(9, 3);
}
std::string TestResponse::content() {
if (buffer.empty())
string();
auto start = buffer.find("\r\n\r\n") + 4;
return buffer.substr(buffer.find("\r\n\r\n") + 4, buffer.size() - start);
}
const std::unordered_map<std::string, std::string> &TestResponse::headers() {
size_t end = buffer.find("\r\n\r\n") + 4;
size_t start = buffer.find("\r\n") + 2;
std::string header;
std::string header_content;
for (size_t c = start; c < end; c++) {
if (buffer[c] == ':') {
header = buffer.substr(start, c - start);
c += 2;
start = c;
}
if (buffer[c] == '\r') {
header_content = buffer.substr(start, c - start);
c += 2;
start = c;
headers_.emplace(header, header_content);
header = "";
header_content = "";
}
}
return headers_;
}
void header_present(std::shared_ptr<TestResponse> response, const std::string &header, const std::string &content) {
const auto loc_itr = response->headers().find(header);
const auto end = response->headers().end();
const auto header_found = loc_itr != end;
REQUIRE(header_found);
REQUIRE(loc_itr->first == header);
REQUIRE(loc_itr->second == content);
}
void header_present(std::shared_ptr<TestResponse> response, const std::string &header) {
const auto loc_itr = response->headers().find(header);
const auto end = response->headers().end();
const auto header_found = loc_itr != end;
REQUIRE(header_found);
REQUIRE(loc_itr->first == header);
}
void is_json_response(std::shared_ptr<TestResponse> response, nlohmann::json data = nullptr) {
const auto content = response->content();
nlohmann::json obj = nlohmann::json::parse(response->content());
if (data != nullptr) {
REQUIRE(data == obj);
}
REQUIRE(obj.is_object());
header_present(response, "Content-Type", "application/json");
header_present(response, "Content-Length", std::to_string(content.length()));
}
void CommonResponse::service_unavailable(std::shared_ptr<TestResponse> response) {
REQUIRE(response->code() == "503");
REQUIRE(response->message() == "Service Unavailable");
is_json_response(response);
};
void CommonResponse::ok(std::shared_ptr<TestResponse> response, const nlohmann::json &data) {
REQUIRE(response->code() == "200");
REQUIRE(response->message() == "OK");
is_json_response(response, data);
};
void CommonResponse::no_content(std::shared_ptr<TestResponse> response) {
REQUIRE(response->code() == "204");
REQUIRE(response->message() == "No Content");
REQUIRE(response->content() == "");
};
void CommonResponse::bad_request(std::shared_ptr<TestResponse> response) {
REQUIRE(response->code() == "400");
REQUIRE(response->message() == "Bad Request");
is_json_response(response);
};
void CommonResponse::accepted(std::shared_ptr<TestResponse> response) {
REQUIRE(response->code() == "202");
REQUIRE(response->message() == "Accepted");
is_json_response(response);
};
void CommonResponse::created(std::shared_ptr<TestResponse> response, std::shared_ptr<TestRequest> request, const std::string &location) {
REQUIRE(response->code() == "201");
REQUIRE(response->message() == "Created");
is_json_response(response);
header_present(response, "Location", location);
};

109
toREST/tests/stubs/SessionContext.cpp

@ -1,109 +0,0 @@
#include <Catch/catch.hpp>
#include <ServerContext.hpp>
#include <SessionContext.hpp>
#include <vector>
void TestSession::theTorrentExists() {
torrents_.emplace_back();
auto &torrent = torrents_.back();
libtorrent::sha1_hash sha1_hash;
std::stringstream ss;
ss << torrent_id;
ss >> sha1_hash;
torrent.hash_ = sha1_hash;
torrent.valid = true;
REQUIRE(get_torrents().size() == 1);
}
void TestSessionSettings::set_int(int type, int value) {
if (type == download_rate_limit) {
download_rate_limit_ = value;
}
if (type == upload_rate_limit) {
upload_rate_limit_ = value;
}
if (type == enable_dht) {
enable_dht_ = value;
}
}
void TestSessionSettings::set_bool(int type, int value) {
set_int(type, value);
}
int TestSessionSettings::get_int(int type) const {
if (type == download_rate_limit) {
return download_rate_limit_;
}
if (type == upload_rate_limit) {
return upload_rate_limit_;
}
if (type == enable_dht) {
return enable_dht_;
}
return 0;
};
bool TestSessionSettings::get_bool(int type) { return get_int(type); }
std::string TestSessionSettings::get_str(int type) {
if (type == listen_interfaces) {
return listen_interfaces_;
}
return "";
}
void TestSessionSettings::set_str(int type, std::string value) {
if (type == listen_interfaces) {
listen_interfaces_ = value;
}
}
bool TestSession::is_valid() {
return valid;
}
TestSessionSettings TestSession::get_settings() {
return settings_;
}
bool TestSession::is_paused() const {
return paused;
}
void TestSession::pause() {
paused = true;
}
void TestSession::resume() {
paused = false;
}
int TestSession::listen_port() {
return std::stoi(settings_.listen_interfaces_.substr(8, settings_.listen_interfaces_.size() - 8));
}
bool TestSession::is_dht_running() const {
return settings_.enable_dht_;
}
std::vector<TestTorrent> &TestSession::get_torrents() {
return torrents_;
}
TestTorrent &TestSession::find_torrent(const libtorrent::sha1_hash &hash) {
auto itr = std::find_if(torrents_.begin(), torrents_.end(), [&](TestTorrent torrent) {
return torrent.info_hash() == hash;
});
if (itr != torrents_.end()) {
return *itr;
}
return invalid_;
}
void TestSession::remove_torrent(const TestTorrent &torrent, int options = -1) {
if (options > -1) {
torrents_.erase(std::find_if(torrents_.begin(), torrents_.end(), [&](TestTorrent a) {
return a.info_hash() == torrent.info_hash();
}));
}
}
void TestSession::apply_settings(TestSessionSettings settings) {
settings_ = settings;
};
void TestSession::async_add_torrent(const libtorrent::add_torrent_params &settings) {
torrents_.emplace_back(settings);
};
void TestSession::post_torrent_updates(int flags) {
post_torrent_updates_calls_++;
}

47
toREST/tests/stubs/TorrentContext.cpp

@ -1,47 +0,0 @@
#include <TorrentContext.hpp>
TorrentStatus &TestTorrent::status(int type) {
return status_;
}
bool TestTorrent::is_valid() const {
return valid;
}
int TestTorrent::upload_limit() const {
return upload_limit_;
}
int TestTorrent::download_limit() const {
return download_limit_;
}
TestTorrent::TestTorrent(const libtorrent::add_torrent_params &params) {
const auto paused = (params.flags & libtorrent::add_torrent_params::flag_paused) == libtorrent::add_torrent_params::flag_paused;
status_.paused = paused;
status_.save_path = params.save_path;
status_.name = params.name;
upload_limit_ = params.upload_limit;
download_limit_ = params.download_limit;
hash_ = params.info_hash;
}
libtorrent::sha1_hash TestTorrent::info_hash() const {
return hash_;
}
void TestTorrent::pause(int flags) {
status_.paused = true;
}
void TestTorrent::resume() {
status_.paused = false;
}
void TestTorrent::set_download_limit(int limit) {
download_limit_ = limit;
}
void TestTorrent::set_upload_limit(int limit) {
upload_limit_ = limit;
}

3
toREST/tests/test.cpp

@ -1,3 +0,0 @@
#define _TR_TESTING_
#define CATCH_CONFIG_MAIN
#include <Catch/catch.hpp>

91
toREST/tests/torrent_test.cpp

@ -1,91 +0,0 @@
#include <ServerContext.hpp>
#include <SessionContext.hpp>
#include <TorrentContext.hpp>
#include <torrent.hpp>
SCENARIO("We are running a GET /session/torrents/id resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly and we recive a reqest") {
THEN("the server should reply with service unavailable") {
tr::session::torrents::id::get(torrent_session, response, request);
CommonResponse::service_unavailable(response);
}
}
auto torrent = TestTorrent();
torrent_session.valid = true;
GIVEN("we have the server is working properly and we recive a request") {
WHEN("the request path is a proper id") {
request->path = "/session/torrents/" + torrent_id;
GIVEN("we have the torrent") {
torrent_session.theTorrentExists();
THEN("the response should be a json representation of the torrent") {
tr::session::torrents::id::get(torrent_session, response, request);
CommonResponse::ok(response, {torrent_json});
}
WHEN("The torrent isn't valid") {
torrent_session.torrents_.back().valid = false;
THEN("the server should reply with bad request") {
tr::session::torrents::id::get(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
}
}
}
}
SCENARIO("We are running a PATCH /session/torrents/id resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly and we recive a reqest") {
THEN("the server should reply with service unavailable") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::service_unavailable(response);
}
}
torrent_session.valid = true;
GIVEN("we have the server is working properly and we recive a request") {
WHEN("the request path is a proper id") {
request->path = "/session/torrents/" + torrent_id;
GIVEN("the request is valid and we have the torrent") {
request->content << nlohmann::json({
{"paused", "true"},
{"up_limit", "109"},
{"down_limit", 100},
});
torrent_session.theTorrentExists();
WHEN("The torrent isn't valid") {
torrent_session.torrents_.back().valid = false;
THEN("the server should reply with bad request") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
THEN("the server should reply with accepted") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::accepted(response);
REQUIRE(torrent_session.get_torrents().back().status().paused);
REQUIRE(torrent_session.get_torrents().back().download_limit() == 100);
REQUIRE(torrent_session.get_torrents().back().upload_limit() == 109);
}
}
THEN("the request is valid json, but doesn't have any of the required fields") {
request->content << "{}";
THEN("the server should reply with bad request") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
request->content << "Not valid";
THEN("the server should reply with bad request") {
tr::session::torrents::id::patch(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
}
}

243
toREST/tests/torrents_test.cpp

@ -1,243 +0,0 @@
#include <Catch/catch.hpp>
#include <ConfigContext.hpp>
#include <ServerContext.hpp>
#include <SessionContext.hpp>
#include <torrents.hpp>
SCENARIO("We are running a GET /session/torrents resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
WHEN("we recive a request") {
tr::session::torrents::get(torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
}
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
WHEN("we recive a valid request") {
GIVEN("we have no torrents") {
THEN("the server should reply with an empty array") {
tr::session::torrents::get(torrent_session, response, request);
CommonResponse::ok(response, {{"torrents", nlohmann::json::array()}});
}
}
GIVEN("we have at least one torrent") {
torrent_session.theTorrentExists();
THEN("the server should reply with an array of torrents") {
tr::session::torrents::get(torrent_session, response, request);
CommonResponse::ok(response, {{"torrents", {torrent_json}}});
};
}
}
}
}
SCENARIO("We are running a POST /session/torrents resource") {
auto torrent_session = TestSession();
auto settings = tr::config();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
AND_WHEN("we recive a request") {
tr::session::torrents::post(settings, torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
}
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
WHEN("we recive a valid request") {
GIVEN("we use the default download directory") {
AND_WHEN("we fill the magnet uri field") {
THEN("the server should reply with created") {
request->content << nlohmann::json::object({{"magnet_uri", magnet_uri}});
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/toREST");
}
}
// AND_WHEN("we fill the magnet url field"){
// THEN("the server should reply with created") {
// request->content << nlohmann::json::object({{"url", torrent_url}});
// tr::session::torrents::post(settings, torrent_session, response, request);
// CommonResponse::created(response, request, "/session/torrents/" + std::string("0000000000000000000000000000000000000000"));
// REQUIRE(torrent_session.get_torrents().size() == 1);
// REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/toREST");
// }
// }
}
GIVEN("we specify a absolute save path") {
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "/music"}});
tr::session::torrents::post(settings, torrent_session, response, request);
THEN("the server should reply with bad request") {
CommonResponse::bad_request(response);
REQUIRE(torrent_session.get_torrents().size() == 0);
}
}
GIVEN("we specify a relative save path") {
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "music"}});
THEN("the server should reply created") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE_FALSE(torrent_session.get_torrents()[0].status().paused);
REQUIRE(torrent_session.get_torrents()[0].upload_limit() == -1);
REQUIRE(torrent_session.get_torrents()[0].download_limit() == -1);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music");
}
}
GIVEN("we specify the torrent field to be paused") {
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "music"},
{"paused", "true"}});
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE(torrent_session.get_torrents()[0].status().paused);
REQUIRE(torrent_session.get_torrents()[0].upload_limit() == -1);
REQUIRE(torrent_session.get_torrents()[0].download_limit() == -1);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music");
}
}
GIVEN("we specify up an down speed") {
const std::string name = "Dent";
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "music"},
{"up_speed", "100"},
{"down_speed", "100"},
{"name", name}});
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE_FALSE(torrent_session.get_torrents()[0].status().paused);
REQUIRE(torrent_session.get_torrents()[0].status().name == name);
REQUIRE(torrent_session.get_torrents()[0].upload_limit() == 100);
REQUIRE(torrent_session.get_torrents()[0].download_limit() == 100);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music");
}
}
GIVEN("we specify up an down speed") {
const std::string name = "Dent";
request->content << nlohmann::json::object(
{{"magnet_uri", magnet_uri},
{"save_path", "music"},
{"up_speed", 90},
{"down_speed", "100"},
{"name", name}});
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::created(response, request, "/session/torrents/" + torrent_id);
REQUIRE(torrent_session.get_torrents().size() == 1);
REQUIRE_FALSE(torrent_session.get_torrents()[0].status().paused);
REQUIRE(torrent_session.get_torrents()[0].status().name == name);
REQUIRE(torrent_session.get_torrents()[0].upload_limit() == 90);
REQUIRE(torrent_session.get_torrents()[0].download_limit() == 100);
REQUIRE((torrent_session.get_torrents()[0]).status().save_path == "/music");
}
}
}
WHEN("we recive an invalid request") {
GIVEN("the request doesn't contain valid json") {
request->content << "Not valid";
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
GIVEN("it is valid json but are missing requred field") {
request->content << nlohmann::json({{"up_speed", "100"}});
THEN("the server should reply with bad request") {
tr::session::torrents::post(settings, torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
}
}
}
SCENARIO("We are running a DELETE /session/torrents resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
AND_WHEN("we recive a request") {
tr::session::torrents::del(torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
}
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
WHEN("we recive an invalid request") {
GIVEN("the request doesn't contain valid json") {
request->content << "Not valid";
THEN("the server should reply with bad request") {
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
GIVEN("it is valid json but are missing requred field") {
request->content << nlohmann::json({{"up_speed", "100"}});
THEN("the server should reply with bad request") {
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
GIVEN("required field is wrong type") {
request->content << nlohmann::json({{"info_hash", 1}});
THEN("the server should reply with bad request") {
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
GIVEN("fields are correct") {
GIVEN("the torrent exists") {
torrent_session.theTorrentExists();
THEN("the server should reply no content") {
request->content << nlohmann::json({{"info_hash", torrent_id}});
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::no_content(response);
REQUIRE(torrent_session.get_torrents().size() == 0);
}
GIVEN("we want to remove files") {
THEN("the should reply with no content") {
request->content << nlohmann::json({{"info_hash", torrent_id},
{"remove_files", true}});
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::no_content(response);
REQUIRE(torrent_session.get_torrents().size() == 0);
}
THEN("we should reply with no content") {
request->content << nlohmann::json({{"info_hash", torrent_id},
{"remove_files", "true"}});
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::no_content(response);
REQUIRE(torrent_session.get_torrents().size() == 0);
}
}
}
GIVEN("the torrent doesn't exist") {
THEN("the server should reply with bad request") {
tr::session::torrents::del(torrent_session, response, request);
CommonResponse::bad_request(response);
}
}
}
}
}
}

25
toREST/tests/util_test.cpp

@ -1,25 +0,0 @@
#include <Catch/catch.hpp>
#include <util.hpp>
SCENARIO("we want to parse a request uri with one field") {
std::string request_uri = "/session?fields=id,trains";
WHEN("we parse the uri") {
auto result = util::uri::parse(request_uri);
THEN("the map should contain structured data based ont the uri") {
REQUIRE(result.size() == 1);
REQUIRE(result["fields"] == "id,trains");
}
}
}
SCENARIO("we want to parse a request uri with more field") {
std::string request_uri = "/session?fields=id,trains&sort=desc";
WHEN("we parse the uri") {
auto result = util::uri::parse(request_uri);
THEN("the map should contain structured data based ont the uri") {
REQUIRE(result.size() == 2);
REQUIRE(result["fields"] == "id,trains");
REQUIRE(result["sort"] == "desc");
}
}
}

88
toREST/tests/ws_test.cpp

@ -1,88 +0,0 @@
#include <Catch/catch.hpp>
#include <SessionContext.hpp>
#include <ws.hpp>
SCENARIO("the test suite is working") {
REQUIRE(true);
REQUIRE_FALSE(false);
}
struct TestConnection {
std::string reason_ = "";
int status_ = 0;
};
struct TestMessage : public std::stringstream {
std::string string() {
return str();
}
};
struct TestWsServer {
void send_close(std::shared_ptr<TestConnection> con, int status, const std::string &reason) {
send_close_calls_++;
con->reason_ = reason;
con->status_ = status;
}
int send_close_calls_ = 0;
};
SCENARIO("we have a websocket resource") {
TestSession session;
TestWsServer server;
auto connection = std::make_shared<TestConnection>();
GIVEN("the session is valid") {
session.valid = true;
WHEN("we open the connection") {
tr::ws::on_open(server, session, connection);
THEN("the session should not post torrent torrent updates") {
REQUIRE(server.send_close_calls_ == 0);
}
}
}
GIVEN("the session is invalid") {
WHEN("we open the connection") {
tr::ws::on_open(server, session, connection);
THEN("the server should close the connection") {
REQUIRE(server.send_close_calls_ == 1);
REQUIRE(connection->status_ == 1011);
REQUIRE(connection->reason_ == "Session not valid");
}
}
}
auto message = std::make_shared<TestMessage>();
GIVEN("we recive an json request") {
WHEN("the json isn't valid") {
*message << "Not valid";
THEN("the server should reject the action") {
tr::ws::on_message(session, connection, message);
REQUIRE(server.send_close_calls_ == 0);
REQUIRE(session.post_torrent_updates_calls_ == 0);
}
}
WHEN("the json is valid but doesn't have any of the required fields") {
*message << "{}";
THEN("the server should reject the action") {
tr::ws::on_message(session, connection, message);
REQUIRE(server.send_close_calls_ == 0);
REQUIRE(session.post_torrent_updates_calls_ == 0);
}
}
WHEN("the json is valid and have the required fields but not of valid type") {
*message << nlohmann::json({{"type", "N/A"}});
THEN("the server should reject the action") {
tr::ws::on_message(session, connection, message);
REQUIRE(server.send_close_calls_ == 0);
REQUIRE(session.post_torrent_updates_calls_ == 0);
}
}
WHEN("the json is valid and have the required fields") {
*message << nlohmann::json({{"type", "POST_TORRENT_UPDATES"}});
THEN("the server should post torrent updates") {
tr::ws::on_message(session, connection, message);
REQUIRE(server.send_close_calls_ == 0);
REQUIRE(session.post_torrent_updates_calls_ == 1);
}
}
}
}
Loading…
Cancel
Save