commit fff4e5b3f356f111b610e0a92f85685ff1e23184 Author: hiimgoodpack Date: Wed Mar 24 23:06:13 2021 -0400 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6027b1a --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..84de24b --- /dev/null +++ b/Makefile @@ -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 diff --git a/http.h b/http.h new file mode 100644 index 0000000..a565953 --- /dev/null +++ b/http.h @@ -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 +#include +#include +#include + +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 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..6efb3ac --- /dev/null +++ b/main.cpp @@ -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 +#include // std::strncmp +#include +#include +#include + +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(); +} diff --git a/websocket.h b/websocket.h new file mode 100644 index 0000000..6c3be1e --- /dev/null +++ b/websocket.h @@ -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 +#include +#include + +// We're doing some declarations instead of just including all of websocketpp since +// websocketpp takes a long time to compile +#include +#include + +class WebSocketError : public std::runtime_error { + public: + using runtime_error::runtime_error; +}; + +class WebSocket { + private: + websocketpp::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 onConnect = nullptr; + std::function onReceive = nullptr; + std::function onClose = nullptr; +}; + +#endif + +#if defined(WEBSOCKET_IMPLEMENTATION) && !defined(WEBSOCKET_IMPLEMENTED) +#define WEBSOCKET_IMPLEMENTED + +#include + +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