Initial commit

This commit is contained in:
hiimgoodpack 2021-03-24 23:06:13 -04:00
commit fff4e5b3f3
Signed by untrusted user: hiimgoodpack
GPG key ID: 4E0E62733C14AE69
5 changed files with 297 additions and 0 deletions

16
LICENSE Normal file
View file

@ -0,0 +1,16 @@
Copyright (c) 2021 hiimgoodpack
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

31
Makefile Normal file
View file

@ -0,0 +1,31 @@
# Copyright (c) 2021 hiimgoodpack
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
CPPFLAGS=-lstdc++ -lm -lpthread -lcurl -std=c++11 -ggdb -Wall
CXX=ccache clang
all: main
websocket.o: websocket.h
$(CXX) $(CPPFLAGS) -x c++ -DWEBSOCKET_IMPLEMENTATION -c -o websocket.o websocket.h
http.o: http.h
$(CXX) $(CPPFLAGS) -x c++ -DHTTP_IMPLEMENTATION -c -o http.o http.h
main.o: main.cpp
$(CXX) $(CPPFLAGS) -c -o main.o main.cpp
main: websocket.o http.o main.o
$(CXX) $(CPPFLAGS) websocket.o http.o main.o

84
http.h Normal file
View file

@ -0,0 +1,84 @@
// Copyright (c) 2021 hiimgoodpack
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef HTTP_H
#define HTTP_H
#include <string>
#include <curl/curl.h>
#include <stdexcept>
#include <cstdint>
namespace Http {
class Error : public std::runtime_error {
public:
using runtime_error::runtime_error;
};
class Request {
private:
CURL* handle;
std::string postFields;
static std::size_t write(void* buffer, std::size_t blocks, std::size_t blockSize, std::string* result);
public:
std::string buffer;
long int status;
Request(const char* const url);
void setPostFields(std::string fields);
void perform();
~Request();
};
}
#endif
#if defined(HTTP_IMPLEMENTATION) && !defined(HTTP_IMPLEMENTED)
#define HTTP_IMPLEMENTED
namespace Http {
Request::Request(const char* const url) {
handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url);
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status);
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &buffer);
}
std::size_t Request::write(void* buffer, std::size_t blocks, std::size_t blockSize, std::string* result) {
const std::size_t size = blocks * blockSize;
result->append((char*)buffer, size);
return size;
}
void Request::setPostFields(std::string newFields) {
postFields = newFields;
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, postFields.c_str());
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, postFields.length()+1);
}
void Request::perform() {
CURLcode code = curl_easy_perform(handle);
if (code != CURLE_OK)
throw Error(curl_easy_strerror(code));
}
Request::~Request() {
curl_easy_cleanup(handle);
}
}
#endif

66
main.cpp Normal file
View file

@ -0,0 +1,66 @@
// Copyright (c) 2021 hiimgoodpack
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "http.h"
#include "websocket.h"
#include <string>
#include <cstring> // std::strncmp
#include <cstdint>
#include <stdexcept>
#include <iostream>
enum State {
WAIT_HELLO,
WAIT_ACK,
READY
};
int main(int argc, char* argv[]) {
curl_global_init(CURL_GLOBAL_ALL);
Http::Request tokenRequest("http://localhost:3005/api/v1/users/token/create");
tokenRequest.setPostFields("username=TestUser&password=12345678&alsoSetCookie=true}");
tokenRequest.perform();
std::cout << tokenRequest.status << " " << tokenRequest.buffer;
WebSocket socket("ws://localhost:3005/gateway");
State state = WAIT_HELLO;
socket.onReceive = [&](std::string message) {
std::cout << "Received: " << message << "\n";
switch (state) {
case WAIT_HELLO: {
if (std::strncmp(message.c_str(), "0@", 2) == 0) {
state = WAIT_ACK;
socket.send("1@{\"token\":\"my totally real token\"");
} else {
std::cerr << "Failed authentication: Expected packet type 0 (HELLO), received buffer: " << message << "\n";
socket.close();
}
break;
}
case WAIT_ACK: {
if (std::strncmp(message.c_str(), "2@", 2) == 0) {
state = READY;
} else {
std::cerr << "Failed authentication: Expected packet type 2 (YOO_ACK), received buffer: " << message << "\n";
socket.close();
}
}
case READY: break;
}
};
socket.run();
}

100
websocket.h Normal file
View file

@ -0,0 +1,100 @@
// Copyright (c) 2021 hiimgoodpack
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef WEBSOCKET_H
#define WEBSOCKET_H
#include <string>
#include <functional>
#include <stdexcept>
// We're doing some declarations instead of just including all of websocketpp since
// websocketpp takes a long time to compile
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
class WebSocketError : public std::runtime_error {
public:
using runtime_error::runtime_error;
};
class WebSocket {
private:
websocketpp::client<websocketpp::config::asio_client> client;
websocketpp::connection_hdl hdl;
static void onConnectHandler(WebSocket*, websocketpp::connection_hdl);
static void onReceiveHandler(WebSocket*, websocketpp::config::asio_client::message_type::ptr);
static void onCloseHandler(WebSocket*);
public:
WebSocket(const char* const url);
void run();
void send(std::string buffer);
void close();
std::function<void()> onConnect = nullptr;
std::function<void(std::string)> onReceive = nullptr;
std::function<void()> onClose = nullptr;
};
#endif
#if defined(WEBSOCKET_IMPLEMENTATION) && !defined(WEBSOCKET_IMPLEMENTED)
#define WEBSOCKET_IMPLEMENTED
#include <websocketpp/client.hpp>
WebSocket::WebSocket(const char* const url) {
client.init_asio();
client.set_open_handler(std::bind(onConnectHandler, this, std::placeholders::_1));
client.set_message_handler(std::bind(onReceiveHandler, this, std::placeholders::_2));
client.set_close_handler(std::bind(onCloseHandler, this));
websocketpp::lib::error_code error;
decltype(client)::connection_ptr connection = client.get_connection(url, error);
if (error)
throw WebSocketError(error.message());
client.connect(connection);
}
void WebSocket::onConnectHandler(WebSocket* socket, websocketpp::connection_hdl hdl) {
socket->hdl = hdl;
if (socket->onConnect)
socket->onConnect();
}
void WebSocket::onReceiveHandler(WebSocket* socket, websocketpp::config::asio_client::message_type::ptr message) {
if (socket->onReceive)
socket->onReceive(message->get_payload());
}
void WebSocket::onCloseHandler(WebSocket* socket) {
if (socket->onClose)
socket->onClose();
}
void WebSocket::run() {
client.run();
}
void WebSocket::send(std::string buffer) {
client.send(hdl, buffer, websocketpp::frame::opcode::text);
}
void WebSocket::close() {
client.close(hdl, websocketpp::close::status::going_away, "");
}
#endif