new cspot/bell

This commit is contained in:
philippe44
2023-05-06 23:50:26 +02:00
parent e0e7e718ba
commit 8bad480112
163 changed files with 6611 additions and 6739 deletions

View File

@@ -1,22 +1,18 @@
#pragma once
#include <BellLogger.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <utility>
#include <optional>
#include <regex>
#include <sstream>
#include <string>
#include <mutex>
#include <unordered_map>
#include "CivetServer.h"
#include "civetweb.h"
#include <BellLogger.h> // for bell
#include <stdint.h> // for uint8_t
#include <stdlib.h> // for free, size_t
#include <functional> // for function
#include <map> // for map
#include <memory> // for unique_ptr
#include <mutex> // for mutex
#include <string> // for string, hash, operator==, operator<
#include <unordered_map> // for unordered_map
#include <utility> // for pair
#include <vector> // for vector
#include "CivetServer.h" // for CivetServer, CivetHandler
using namespace bell;
namespace bell {
@@ -46,7 +42,9 @@ class BellHTTPServer : public CivetHandler {
}
}
};
typedef std::function<std::unique_ptr<HTTPResponse>(struct mg_connection* conn)> HTTPHandler;
typedef std::function<std::unique_ptr<HTTPResponse>(
struct mg_connection* conn)>
HTTPHandler;
typedef std::function<void(struct mg_connection* conn, WSState)>
WSStateHandler;
typedef std::function<void(struct mg_connection* conn, char*, size_t)>
@@ -79,7 +77,8 @@ class BellHTTPServer : public CivetHandler {
std::vector<int> getListeningPorts() { return server->getListeningPorts(); };
void close() { server->close(); }
std::unique_ptr<HTTPResponse> makeJsonResponse(const std::string& json, int status = 200);
std::unique_ptr<HTTPResponse> makeJsonResponse(const std::string& json,
int status = 200);
std::unique_ptr<HTTPResponse> makeEmptyResponse();
void registerNotFound(HTTPHandler handler);
@@ -88,7 +87,8 @@ class BellHTTPServer : public CivetHandler {
void registerWS(const std::string&, WSDataHandler dataHandler,
WSStateHandler stateHandler);
static std::unordered_map<std::string, std::string> extractParams(struct mg_connection* conn);
static std::unordered_map<std::string, std::string> extractParams(
struct mg_connection* conn);
private:
std::unique_ptr<CivetServer> server;

View File

@@ -14,6 +14,6 @@ class Socket {
virtual size_t read(uint8_t* buf, size_t len) = 0;
virtual bool isOpen() = 0;
virtual void close() = 0;
virtual int getFd() = 0;
};
} // namespace bell

View File

@@ -1,8 +1,7 @@
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <iostream> // for istream, ostream
#include <string> // for string
namespace bell::BellTar {
typedef long long unsigned file_size_t;

View File

@@ -1,31 +1,28 @@
#pragma once
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <memory>
#include "ByteStream.h"
#include <stdint.h> // for uint8_t, int16_t, int32_t, uint32_t
#include <stdlib.h> // for size_t
#include <memory> // for shared_ptr
#include <vector> // for vector
namespace bell
{
class BinaryReader
{
std::shared_ptr<ByteStream> stream;
size_t currentPos = 0;
namespace bell {
class ByteStream;
public:
BinaryReader(std::shared_ptr<ByteStream> stream);
int32_t readInt();
int16_t readShort();
uint32_t readUInt();
long long readLong();
void close();
uint8_t readByte();
size_t size();
size_t position();
std::vector<uint8_t> readBytes(size_t);
void skip(size_t);
};
}
class BinaryReader {
std::shared_ptr<ByteStream> stream;
size_t currentPos = 0;
public:
BinaryReader(std::shared_ptr<ByteStream> stream);
int32_t readInt();
int16_t readShort();
uint32_t readUInt();
long long readLong();
void close();
uint8_t readByte();
size_t size();
size_t position();
std::vector<uint8_t> readBytes(size_t);
void skip(size_t);
};
} // namespace bell

View File

@@ -1,10 +1,11 @@
#pragma once
#ifndef ESP_PLATFORM
#include <bit>
#include <bit> // for endian
#endif
#include <iostream>
#include <vector>
#include <stdint.h> // for int16_t, int32_t, int64_t, uint16_t, uint32_t
#include <cstddef> // for byte
#include <iostream> // for istream, ostream
namespace bell {
class BinaryStream {

View File

@@ -1,12 +1,16 @@
#pragma once
#include "ByteStream.h"
#include "BellTask.h"
#include "WrappedSemaphore.h"
#include <atomic>
#include <functional>
#include <memory>
#include <mutex>
#include <stddef.h> // for size_t
#include <stdint.h> // for uint32_t, uint8_t
#include <atomic> // for atomic
#include <functional> // for function
#include <memory> // for shared_ptr
#include <mutex> // for mutex
#include <string> // for string
#include "BellTask.h" // for Task
#include "ByteStream.h" // for ByteStream
#include "WrappedSemaphore.h" // for WrappedSemaphore
/**
* This class implements a wrapper around an arbitrary bell::ByteStream,
@@ -26,12 +30,12 @@
* method correctly, such as that 0 is returned if, and only if the stream ends.
*/
class BufferedStream : public bell::ByteStream, bell::Task {
public:
typedef std::shared_ptr<bell::ByteStream> StreamPtr;
typedef std::function<StreamPtr(uint32_t rangeStart)> StreamReader;
public:
typedef std::shared_ptr<bell::ByteStream> StreamPtr;
typedef std::function<StreamPtr(uint32_t rangeStart)> StreamReader;
public:
/**
public:
/**
* @param taskName name to use for the reading task
* @param bufferSize total size of the reading buffer
* @param readThreshold how much can be read before refilling the buffer
@@ -41,22 +45,18 @@ class BufferedStream : public bell::ByteStream, bell::Task {
* @param waitForReady whether to wait for the buffer to be ready during reading
* @param endWithSource whether to end the streaming as soon as source returns 0 from read()
*/
BufferedStream(
const std::string &taskName,
uint32_t bufferSize,
uint32_t readThreshold,
uint32_t readSize,
uint32_t readyThreshold,
uint32_t notReadyThreshold,
bool waitForReady = false);
~BufferedStream() override;
bool open(const StreamPtr &stream);
bool open(const StreamReader &newReader, uint32_t initialOffset = 0);
void close() override;
BufferedStream(const std::string& taskName, uint32_t bufferSize,
uint32_t readThreshold, uint32_t readSize,
uint32_t readyThreshold, uint32_t notReadyThreshold,
bool waitForReady = false);
~BufferedStream() override;
bool open(const StreamPtr& stream);
bool open(const StreamReader& newReader, uint32_t initialOffset = 0);
void close() override;
// inherited methods
public:
/**
// inherited methods
public:
/**
* Read len bytes from the buffer to dst. If waitForReady is enabled
* and readAvailable is lower than notReadyThreshold, the function
* will block until readyThreshold bytes is available.
@@ -65,61 +65,63 @@ class BufferedStream : public bell::ByteStream, bell::Task {
* if the buffer does not contain len bytes available), or 0 if the source
* stream is already closed and there is no reader attached.
*/
size_t read(uint8_t *dst, size_t len) override;
size_t skip(size_t len) override;
size_t position() override;
size_t size() override;
size_t read(uint8_t* dst, size_t len) override;
size_t skip(size_t len) override;
size_t position() override;
size_t size() override;
// stream status
public:
/**
// stream status
public:
/**
* Total amount of bytes served to read().
*/
uint32_t readTotal;
/**
uint32_t readTotal;
/**
* Total amount of bytes read from source.
*/
uint32_t bufferTotal;
/**
uint32_t bufferTotal;
/**
* Amount of bytes available to read from the buffer.
*/
std::atomic<uint32_t> readAvailable;
/**
std::atomic<uint32_t> readAvailable;
/**
* Whether the caller should start reading the data. This indicates that a safe
* amount (determined by readyThreshold) of data is available in the buffer.
*/
bool isReady() const;
/**
bool isReady() const;
/**
* Whether the caller should stop reading the data. This indicates that the amount of data
* available for reading is decreasing to a non-safe value, as data is being read
* faster than it can be buffered.
*/
bool isNotReady() const;
/**
bool isNotReady() const;
/**
* Semaphore that is given when the buffer becomes ready (isReady() == true). Caller can
* wait for the semaphore instead of continuously querying isReady().
*/
bell::WrappedSemaphore readySem;
bell::WrappedSemaphore readySem;
private:
std::mutex runningMutex;
bool running = false;
bool terminate = false;
bell::WrappedSemaphore readSem; // signal to start writing to buffer after reading from it
std::mutex readMutex; // mutex for locking read operations during writing, and vice versa
uint32_t bufferSize;
uint32_t readAt;
uint32_t readSize;
uint32_t readyThreshold;
uint32_t notReadyThreshold;
bool waitForReady;
uint8_t *buf;
uint8_t *bufEnd;
uint8_t *bufReadPtr;
uint8_t *bufWritePtr;
StreamPtr source;
StreamReader reader;
void runTask() override;
void reset();
uint32_t lengthBetween(uint8_t *me, uint8_t *other);
private:
std::mutex runningMutex;
bool running = false;
bool terminate = false;
bell::WrappedSemaphore
readSem; // signal to start writing to buffer after reading from it
std::mutex
readMutex; // mutex for locking read operations during writing, and vice versa
uint32_t bufferSize;
uint32_t readAt;
uint32_t readSize;
uint32_t readyThreshold;
uint32_t notReadyThreshold;
bool waitForReady;
uint8_t* buf;
uint8_t* bufEnd;
uint8_t* bufReadPtr;
uint8_t* bufWritePtr;
StreamPtr source;
StreamReader reader;
void runTask() override;
void reset();
uint32_t lengthBetween(uint8_t* me, uint8_t* other);
};

View File

@@ -1,27 +1,25 @@
#ifndef BELL_BYTE_READER_H
#define BELL_BYTE_READER_H
#include <stdlib.h>
#include <stdint.h>
#include <stdlib.h>
/**
* A class for reading bytes from a stream. Further implemented in HTTPStream.h
*/
namespace bell
{
class ByteStream
{
public:
ByteStream(){};
virtual ~ByteStream() = default;
namespace bell {
class ByteStream {
public:
ByteStream(){};
virtual ~ByteStream() = default;
virtual size_t read(uint8_t *buf, size_t nbytes) = 0;
virtual size_t skip(size_t nbytes) = 0;
virtual size_t read(uint8_t* buf, size_t nbytes) = 0;
virtual size_t skip(size_t nbytes) = 0;
virtual size_t position() = 0;
virtual size_t size() = 0;
virtual void close() = 0;
};
}
virtual size_t position() = 0;
virtual size_t size() = 0;
virtual void close() = 0;
};
} // namespace bell
#endif

View File

@@ -1,39 +1,35 @@
#pragma once
#include <algorithm>
#include <memory>
#include <cstring>
#include <mutex>
#include <vector>
#include "WrappedSemaphore.h"
#include <cstdint> // for uint8_t
#include <cstring> // for size_t
#include <memory> // for unique_ptr
#include <mutex> // for mutex
#include <vector> // for vector
#include "WrappedSemaphore.h" // for WrappedSemaphore
namespace bell {
class CircularBuffer {
public:
CircularBuffer(size_t dataCapacity);
public:
CircularBuffer(size_t dataCapacity);
std::unique_ptr<bell::WrappedSemaphore> dataSemaphore;
std::unique_ptr<bell::WrappedSemaphore> dataSemaphore;
size_t size() const {
return dataSize;
}
size_t size() const { return dataSize; }
size_t capacity() const {
return dataCapacity;
}
size_t capacity() const { return dataCapacity; }
size_t write(const uint8_t *data, size_t bytes);
size_t read(uint8_t *data, size_t bytes);
void emptyBuffer();
void emptyExcept(size_t size);
size_t write(const uint8_t* data, size_t bytes);
size_t read(uint8_t* data, size_t bytes);
void emptyBuffer();
void emptyExcept(size_t size);
private:
std::mutex bufferMutex;
size_t begIndex = 0;
size_t endIndex = 0;
size_t dataSize = 0;
size_t dataCapacity = 0;
std::vector<uint8_t> buffer;
private:
std::mutex bufferMutex;
size_t begIndex = 0;
size_t endIndex = 0;
size_t dataSize = 0;
size_t dataCapacity = 0;
std::vector<uint8_t> buffer;
};
} // namespace bell
} // namespace bell

View File

@@ -1,15 +1,17 @@
#pragma once
#include <string>
#include <vector>
#include <stddef.h> // for size_t
#include <cstdint> // for uint8_t
#include <memory> // for shared_ptr, unique_ptr
#include <string> // for basic_string, string
#include <vector> // for vector
#include "BellLogger.h"
#include "ByteStream.h"
#include "DecoderGlobals.h"
#include "aacdec.h"
#include "mp3dec.h"
#include "aacdec.h" // for AACFrameInfo
#include "mp3dec.h" // for MP3FrameInfo
namespace bell {
class ByteStream;
class EncodedAudioStream {
public:
EncodedAudioStream();

View File

@@ -1,10 +1,9 @@
#pragma once
#include <string>
#include <stdexcept>
#include <BellLogger.h>
#include <ByteStream.h>
#include <stdio.h>
#include <ByteStream.h> // for ByteStream
#include <stdint.h> // for uint8_t
#include <stdio.h> // for size_t, FILE
#include <string> // for string
/*
* FileStream
@@ -12,17 +11,15 @@
* A class for reading and writing to files implementing the ByteStream interface.
*
*/
namespace bell
{
class FileStream : public ByteStream
{
public:
FileStream(const std::string& path, std::string mode);
~FileStream();
namespace bell {
class FileStream : public ByteStream {
public:
FileStream(const std::string& path, std::string mode);
~FileStream();
FILE* file;
FILE* file;
/*
/*
* Reads data from the stream.
*
* @param buf The buffer to read data into.
@@ -30,18 +27,18 @@ namespace bell
* @return The number of bytes read.
* @throws std::runtime_error if the stream is closed.
*/
size_t read(uint8_t *buf, size_t nbytes);
size_t read(uint8_t* buf, size_t nbytes);
/*
/*
* Skips nbytes bytes in the stream.
*/
size_t skip(size_t nbytes);
size_t skip(size_t nbytes);
size_t position();
size_t position();
size_t size();
size_t size();
// Closes the connection
void close();
};
}
// Closes the connection
void close();
};
} // namespace bell

View File

@@ -1,24 +1,20 @@
#pragma once
#include <memory>
#include <stddef.h> // for size_t
#include <cstdint> // for uint8_t, int32_t
#include <memory> // for make_unique, unique_ptr
#include <string> // for string
#include <string_view> // for string_view
#include <utility> // for pair
#include <vector> // for vector
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <variant>
#include <vector>
#include <cassert>
#include "BellSocket.h"
#include "ByteStream.h"
#include "SocketStream.h"
#include "URLParser.h"
#include "SocketStream.h" // for SocketStream
#include "URLParser.h" // for URLParser
#ifndef BELL_DISABLE_FMT
#include "fmt/core.h"
#include "fmt/core.h" // for format
#endif
#include "picohttpparser.h"
#include "picohttpparser.h" // for phr_header
namespace bell {
class HTTPClient {
@@ -31,19 +27,20 @@ class HTTPClient {
// Helper over ValueHeader, formatting a HTTP bytes range
struct RangeHeader {
static ValueHeader range(int32_t from, int32_t to) {
#ifndef BELL_DISABLE_FMT
#ifndef BELL_DISABLE_FMT
return ValueHeader{"Range", fmt::format("bytes={}-{}", from, to)};
#else
return ValueHeader{"Range", "bytes=" + std::to_string(from) + "-" + std::to_string(to)};
#endif
#else
return ValueHeader{
"Range", "bytes=" + std::to_string(from) + "-" + std::to_string(to)};
#endif
}
static ValueHeader last(int32_t nbytes) {
#ifndef BELL_DISABLE_FMT
#ifndef BELL_DISABLE_FMT
return ValueHeader{"Range", fmt::format("bytes=-{}", nbytes)};
#else
#else
return ValueHeader{"Range", "bytes=-" + std::to_string(nbytes)};
#endif
#endif
}
};

View File

@@ -1,8 +1,10 @@
#pragma once
#include <iostream>
#include "TCPSocket.h"
#include "TLSSocket.h"
#include <iostream> // for streamsize, basic_streambuf<>::int_type, ios...
#include <memory> // for unique_ptr, operator!=
#include <string> // for char_traits, string
#include "BellSocket.h" // for Socket
namespace bell {
class SocketBuffer : public std::streambuf {

View File

@@ -39,6 +39,8 @@ class TCPSocket : public bell::Socket {
TCPSocket(){};
~TCPSocket() { close(); };
int getFd() { return sockFd; }
void open(const std::string& host, uint16_t port) {
int err;
int domain = AF_INET;
@@ -101,7 +103,9 @@ class TCPSocket : public bell::Socket {
#endif
return value;
}
bool isOpen() { return !isClosed; }
bool isOpen() {
return !isClosed;
}
void close() {
if (!isClosed) {

View File

@@ -1,34 +1,21 @@
#ifndef BELL_TLS_SOCKET_H
#define BELL_TLS_SOCKET_H
#include <ctype.h>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include "BellLogger.h"
#include "BellSocket.h"
#include <stdint.h> // for uint8_t, uint16_t
#include "BellSocket.h" // for Socket
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
#include <stdlib.h>
#include <sys/types.h>
#include <sstream>
#include <string>
#include <vector>
#include <stdlib.h> // for size_t
#include <string> // for string
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/debug.h"
#include "mbedtls/entropy.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/ssl.h"
#include "mbedtls/ctr_drbg.h" // for mbedtls_ctr_drbg_context
#include "mbedtls/entropy.h" // for mbedtls_entropy_context
#include "mbedtls/net_sockets.h" // for mbedtls_net_context
#include "mbedtls/ssl.h" // for mbedtls_ssl_config, mbedtls_ssl_con...
namespace bell {
class TLSSocket : public bell::Socket {
@@ -53,6 +40,7 @@ class TLSSocket : public bell::Socket {
bool isOpen();
void close();
int getFd() { return server_fd.fd; }
};
} // namespace bell

View File

@@ -1,9 +1,9 @@
#pragma once
#include <iostream>
#include <regex>
#include <stdexcept>
#include <string>
#include <cstdlib> // for strtol, size_t
#include <regex> // for match_results, match_results<>::value_type, sub...
#include <stdexcept> // for invalid_argument
#include <string> // for string, allocator, operator+, char_traits, oper...
namespace bell {
class URLParser {
@@ -60,7 +60,7 @@ class URLParser {
std::string path;
#ifdef BELL_DISABLE_REGEX
void parse(const char* url, std::vector<std::string>& match);
#else
#else
static const std::regex urlParseRegex;
#endif
@@ -71,10 +71,10 @@ class URLParser {
#ifdef BELL_DISABLE_REGEX
std::vector<std::string> match(6);
parser.parse(url.c_str(), match);
#else
#else
std::cmatch match;
std::regex_match(url.c_str(), match, parser.urlParseRegex);
#endif
#endif
if (match.size() < 3) {
throw std::invalid_argument("Invalid URL");

View File

@@ -1,8 +1,11 @@
#pragma once
#include <stdexcept>
#include "BellLogger.h"
#include "mbedtls/ssl.h"
#include <mbedtls/x509_crt.h> // for mbedtls_x509_crt
#include <stddef.h> // for size_t
#include <cstdint> // for uint8_t, uint16_t, uint32_t
#include <vector> // for vector
#include "mbedtls/ssl.h" // for mbedtls_ssl_config
namespace bell::X509Bundle {
@@ -13,6 +16,7 @@ typedef struct crt_bundle_t {
} crt_bundle_t;
static crt_bundle_t s_crt_bundle;
static std::vector<uint8_t> bundleBytes;
static constexpr auto TAG = "X509Bundle";
static constexpr auto CRT_HEADER_OFFSET = 4;

View File

@@ -40,30 +40,35 @@ extern "C" {
/* contains name and value of a header (name == NULL if is a continuing line
* of a multiline header */
struct phr_header {
const char *name;
size_t name_len;
const char *value;
size_t value_len;
const char* name;
size_t name_len;
const char* value;
size_t value_len;
};
/* returns number of bytes consumed if successful, -2 if request is partial,
* -1 if failed */
int phr_parse_request(const char *buf, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len,
int *minor_version, struct phr_header *headers, size_t *num_headers, size_t last_len);
int phr_parse_request(const char* buf, size_t len, const char** method,
size_t* method_len, const char** path, size_t* path_len,
int* minor_version, struct phr_header* headers,
size_t* num_headers, size_t last_len);
/* ditto */
int phr_parse_response(const char *_buf, size_t len, int *minor_version, int *status, const char **msg, size_t *msg_len,
struct phr_header *headers, size_t *num_headers, size_t last_len);
int phr_parse_response(const char* _buf, size_t len, int* minor_version,
int* status, const char** msg, size_t* msg_len,
struct phr_header* headers, size_t* num_headers,
size_t last_len);
/* ditto */
int phr_parse_headers(const char *buf, size_t len, struct phr_header *headers, size_t *num_headers, size_t last_len);
int phr_parse_headers(const char* buf, size_t len, struct phr_header* headers,
size_t* num_headers, size_t last_len);
/* should be zero-filled before start */
struct phr_chunked_decoder {
size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
char consume_trailer; /* if trailing headers should be consumed */
char _hex_count;
char _state;
size_t bytes_left_in_chunk; /* number of bytes left in current chunk */
char consume_trailer; /* if trailing headers should be consumed */
char _hex_count;
char _state;
};
/* the function rewrites the buffer given as (buf, bufsz) removing the chunked-
@@ -75,10 +80,11 @@ struct phr_chunked_decoder {
* octets left undecoded, that starts from the offset returned by `*bufsz`.
* Returns -1 on error.
*/
ssize_t phr_decode_chunked(struct phr_chunked_decoder *decoder, char *buf, size_t *bufsz);
ssize_t phr_decode_chunked(struct phr_chunked_decoder* decoder, char* buf,
size_t* bufsz);
/* returns if the chunked decoder is in middle of chunked data */
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder *decoder);
int phr_decode_chunked_is_in_data(struct phr_chunked_decoder* decoder);
#ifdef __cplusplus
}