catching up (trying to) wiht CSpot

This commit is contained in:
Philippe G
2022-01-06 18:46:57 -08:00
parent 491d0d260d
commit 9af4cd5b23
57 changed files with 2165 additions and 343 deletions

View File

@@ -2,6 +2,7 @@
#define BELL_HTTP_CLIENT
#include "BellSocket.h"
#include "ByteStream.h"
#include "TCPSocket.h"
#include "platform/TLSSocket.h"
#include <map>
@@ -21,13 +22,15 @@ class HTTPClient {
struct HTTPRequest {
HTTPMethod method = HTTPMethod::GET;
std::string url;
std::string body;
const char *body = nullptr;
const char *contentType = nullptr;
std::map<std::string, std::string> headers;
std::string contentType;
int maxRedirects = -1;
std::ostream *dumpFs = nullptr;
std::ostream *dumpRawFs = nullptr;
};
struct HTTPResponse {
struct HTTPResponse : public ByteStream {
std::shared_ptr<bell::Socket> socket;
std::map<std::string, std::string> headers;
@@ -41,18 +44,32 @@ class HTTPClient {
bool isComplete = false;
bool isRedirect = false;
size_t redirectCount = 0;
std::ostream *dumpFs = nullptr;
std::ostream *dumpRawFs = nullptr;
void close() {
socket->close();
free(buf);
buf = nullptr;
bufPtr = nullptr;
}
~HTTPResponse();
void close() override;
void readHeaders();
size_t read(char *dst, size_t len);
size_t read(char *dst, size_t len, bool wait = false);
std::string readToString();
inline size_t skip(size_t len) override {
return read((char *)nullptr, len);
}
inline size_t read(uint8_t *dst, size_t len) override {
return read((char *)dst, len);
}
inline size_t read(uint8_t *dst, size_t len, bool wait) {
return read((char *)dst, len, wait);
}
inline size_t size() override {
return contentLength;
}
inline size_t position() override {
return bodyRead;
}
private:
char *buf = nullptr; // allocated buffer
char *bufPtr = nullptr; // reading pointer within buf
@@ -61,16 +78,19 @@ class HTTPClient {
size_t chunkRemaining = 0;
bool isStreaming = false;
size_t readRaw(char *dst);
bool skip(size_t len, bool dontRead = false);
bool skipRaw(size_t len, bool dontRead = false);
};
typedef std::unique_ptr<struct HTTPClient::HTTPResponse> HTTPResponse_t;
private:
static void executeImpl(const struct HTTPRequest &request, const char *url, struct HTTPResponse *&response);
static HTTPResponse_t executeImpl(const struct HTTPRequest &request, HTTPResponse_t response);
static bool readHeader(const char *&header, const char *name);
public:
static struct HTTPResponse *execute(const struct HTTPRequest &request);
static HTTPResponse_t execute(const struct HTTPRequest &request);
};
typedef std::unique_ptr<struct HTTPClient::HTTPResponse> HTTPResponse_t;
} // namespace bell
#endif