|
| 1 | +#include <mochios/client.h> |
| 2 | + |
| 3 | +void sendHelper(const std::string &verb, mochios::Request &request, |
| 4 | + mochios::Response &response, int socket, |
| 5 | + std::function<void(mochios::Response &)> callback = nullptr); |
| 6 | + |
| 7 | +mochios::Client::Client(const std::string &host, |
| 8 | + const unsigned short &port) noexcept(false) { |
| 9 | + signal(SIGPIPE, SIG_IGN); |
| 10 | + this->socket = brewtils::sys::socket(AF_INET, SOCK_STREAM, 0); |
| 11 | + if (this->socket < 0) { |
| 12 | + logger::error("Socket not created!", |
| 13 | + "mochios::Client::Client(const std::string &host, const " |
| 14 | + "unsigned short &port)"); |
| 15 | + } |
| 16 | + |
| 17 | + struct addrinfo hints, *result; |
| 18 | + memset(&hints, 0, sizeof(hints)); |
| 19 | + hints.ai_family = AF_INET; |
| 20 | + hints.ai_socktype = SOCK_STREAM; |
| 21 | + |
| 22 | + int err = getaddrinfo(host.c_str(), NULL, &hints, &result); |
| 23 | + if (err != 0) { |
| 24 | + logger::error("DNS lookup failed!", |
| 25 | + "mochios::Client::Client(const std::string &host, const " |
| 26 | + "unsigned short &port)"); |
| 27 | + } |
| 28 | + |
| 29 | + // struct sockaddr_in *address = (struct sockaddr_in *)result->ai_addr; |
| 30 | + // address->sin_port = htons(port); |
| 31 | + |
| 32 | + struct sockaddr_in *ipv4 = (struct sockaddr_in *)result->ai_addr; |
| 33 | + char ipStr[INET_ADDRSTRLEN] = {'\0'}; |
| 34 | + inet_ntop(result->ai_family, &(ipv4->sin_addr), ipStr, sizeof(ipStr)); |
| 35 | + |
| 36 | + logger::info("IP address is: " + std::string(ipStr), |
| 37 | + "mochios::Client::Client(const std::string &host, const " |
| 38 | + "unsigned short &port)"); |
| 39 | + |
| 40 | + this->address.sin_family = AF_INET; |
| 41 | + this->address.sin_port = htons(port); |
| 42 | + this->address.sin_addr = ipv4->sin_addr; |
| 43 | + return; |
| 44 | +} |
| 45 | + |
| 46 | +mochios::Client::~Client() { return; } |
| 47 | + |
| 48 | +void mochios::Client::get( |
| 49 | + mochios::Request &request, mochios::Response &response, |
| 50 | + std::function<void(mochios::Response &)> callback) noexcept(false) { |
| 51 | + return this->send(mochios::verbs::GET, request, response, callback); |
| 52 | +}; |
| 53 | + |
| 54 | +void mochios::Client::post( |
| 55 | + mochios::Request &request, mochios::Response &response, |
| 56 | + std::function<void(mochios::Response &)> callback) noexcept(false) { |
| 57 | + return this->send(mochios::verbs::GET, request, response, callback); |
| 58 | +}; |
| 59 | + |
| 60 | +void mochios::Client::put( |
| 61 | + mochios::Request &request, mochios::Response &response, |
| 62 | + std::function<void(mochios::Response &)> callback) noexcept(false) { |
| 63 | + return this->send(mochios::verbs::GET, request, response, callback); |
| 64 | +}; |
| 65 | + |
| 66 | +void mochios::Client::del( |
| 67 | + mochios::Request &request, mochios::Response &response, |
| 68 | + std::function<void(mochios::Response &)> callback) noexcept(false) { |
| 69 | + return this->send(mochios::verbs::GET, request, response, callback); |
| 70 | +}; |
| 71 | + |
| 72 | +void mochios::Client::patch( |
| 73 | + mochios::Request &request, mochios::Response &response, |
| 74 | + std::function<void(mochios::Response &)> callback) noexcept(false) { |
| 75 | + return this->send(mochios::verbs::GET, request, response, callback); |
| 76 | +}; |
| 77 | + |
| 78 | +void mochios::Client::options( |
| 79 | + mochios::Request &request, mochios::Response &response, |
| 80 | + std::function<void(mochios::Response &)> callback) noexcept(false) { |
| 81 | + return this->send(mochios::verbs::GET, request, response, callback); |
| 82 | +}; |
| 83 | + |
| 84 | +void mochios::Client::head( |
| 85 | + mochios::Request &request, mochios::Response &response, |
| 86 | + std::function<void(mochios::Response &)> callback) noexcept(false) { |
| 87 | + return this->send(mochios::verbs::GET, request, response, callback); |
| 88 | +}; |
| 89 | + |
| 90 | +void mochios::Client::send( |
| 91 | + const std::string &verb, mochios::Request &request, |
| 92 | + mochios::Response &response, |
| 93 | + std::function<void(mochios::Response &)> callback) noexcept(false) { |
| 94 | + if (::connect(this->socket, (struct sockaddr *)&this->address, |
| 95 | + sizeof(this->address)) < 0) { |
| 96 | + logger::error("Connection failed!", |
| 97 | + "mochios::Client::send(const std::string &verb, " |
| 98 | + "mochios::Request &request, mochios::Response &response, " |
| 99 | + "std::function<void(mochios::Response &)> callback)"); |
| 100 | + } |
| 101 | + |
| 102 | + if (callback == nullptr) { |
| 103 | + sendHelper(verb, request, response, this->socket); |
| 104 | + } else { |
| 105 | + nexus::async(sendHelper, verb, std::ref(request), std::ref(response), |
| 106 | + this->socket, callback); |
| 107 | + } |
| 108 | + return; |
| 109 | +} |
| 110 | + |
| 111 | +void sendHelper(const std::string &verb, mochios::Request &request, |
| 112 | + mochios::Response &response, int socket, |
| 113 | + std::function<void(mochios::Response &)> callback) { |
| 114 | + std::string headers = verb + " " + request.path + " HTTP/1.1\r\n"; |
| 115 | + headers += request.stringify(); |
| 116 | + std::string body = request.body.dumps(0); |
| 117 | + |
| 118 | + if (brewtils::sys::send(socket, headers.c_str(), headers.size(), 0) < 0) { |
| 119 | + logger::error( |
| 120 | + "Send failed!", |
| 121 | + "mochios::Client::sendHelper(const std::string &verb, mochios::Request " |
| 122 | + "&request, mochios::Response &response, int socket, " |
| 123 | + "std::function<void(mochios::Response " |
| 124 | + "&)> callback)"); |
| 125 | + } |
| 126 | + if (brewtils::sys::send(socket, body.c_str(), body.size(), 0) < 0) { |
| 127 | + logger::error( |
| 128 | + "Send failed!", |
| 129 | + "mochios::Client::sendHelper(const std::string &verb, mochios::Request " |
| 130 | + "&request, mochios::Response &response, int socket, " |
| 131 | + "std::function<void(mochios::Response " |
| 132 | + "&)> callback)"); |
| 133 | + } |
| 134 | + |
| 135 | + char buffer[4096] = {0}; |
| 136 | + if (brewtils::sys::recv(socket, buffer, 4096, 0) < 0) { |
| 137 | + logger::error( |
| 138 | + "Receive failed!", |
| 139 | + "mochios::Client::sendHelper(const std::string &verb, mochios::Request " |
| 140 | + "&request, mochios::Response &response, int socket, " |
| 141 | + "std::function<void(mochios::Response " |
| 142 | + "&)> callback)"); |
| 143 | + } |
| 144 | + |
| 145 | + ::close(socket); |
| 146 | + std::cout << buffer << std::endl; |
| 147 | + response = mochios::Response(buffer); |
| 148 | + if (callback != nullptr) { |
| 149 | + callback(response); |
| 150 | + } |
| 151 | + |
| 152 | + return; |
| 153 | +} |
0 commit comments