From 79598509706b20a8e130a0b8b5ef9aa797ef3054 Mon Sep 17 00:00:00 2001 From: Philippe G Date: Tue, 4 Jan 2022 17:32:28 -0800 Subject: [PATCH] and more crap again... --- .../spotify/cspot/bell/include/HTTPClient.h | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 components/spotify/cspot/bell/include/HTTPClient.h diff --git a/components/spotify/cspot/bell/include/HTTPClient.h b/components/spotify/cspot/bell/include/HTTPClient.h new file mode 100644 index 00000000..eef7a615 --- /dev/null +++ b/components/spotify/cspot/bell/include/HTTPClient.h @@ -0,0 +1,76 @@ +#ifndef BELL_HTTP_CLIENT +#define BELL_HTTP_CLIENT + +#include "BellSocket.h" +#include "TCPSocket.h" +#include "platform/TLSSocket.h" +#include +#include +#include + +#define BUF_SIZE 128 + +namespace bell { +class HTTPClient { + public: + enum HTTPMethod { + GET, + POST, + }; + + struct HTTPRequest { + HTTPMethod method = HTTPMethod::GET; + std::string url; + std::string body; + std::map headers; + std::string contentType; + int maxRedirects = -1; + }; + + struct HTTPResponse { + std::shared_ptr socket; + + std::map headers; + + uint16_t statusCode; + size_t contentLength; + std::string contentType; + std::string location; + bool isChunked = false; + bool isGzip = false; + bool isComplete = false; + bool isRedirect = false; + size_t redirectCount = 0; + + void close() { + socket->close(); + free(buf); + buf = nullptr; + bufPtr = nullptr; + } + + void readHeaders(); + size_t read(char *dst, size_t len); + std::string readToString(); + + private: + char *buf = nullptr; // allocated buffer + char *bufPtr = nullptr; // reading pointer within buf + size_t bodyRead = 0; + size_t bufRemaining = 0; + size_t chunkRemaining = 0; + bool isStreaming = false; + size_t readRaw(char *dst); + bool skip(size_t len, bool dontRead = false); + }; + + private: + static void executeImpl(const struct HTTPRequest &request, const char *url, struct HTTPResponse *&response); + static bool readHeader(const char *&header, const char *name); + + public: + static struct HTTPResponse *execute(const struct HTTPRequest &request); +}; +} // namespace bell + +#endif