mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-11 22:17:17 +03:00
update cspot
This commit is contained in:
@@ -6,7 +6,14 @@
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
namespace bell {
|
||||
|
||||
@@ -20,6 +27,40 @@ class ResponseReader {
|
||||
virtual void close() = 0;
|
||||
};
|
||||
|
||||
class RequestBodyReader : public ResponseReader {
|
||||
public:
|
||||
std::vector<uint8_t> partialBuffer;
|
||||
int fd = 0;
|
||||
size_t contentLength = 0;
|
||||
size_t sizeRead = 0;
|
||||
|
||||
RequestBodyReader(size_t contentLength, int fd, std::vector<uint8_t> &partialBuffer) {
|
||||
this->contentLength = contentLength;
|
||||
this->partialBuffer = partialBuffer;
|
||||
this->fd = fd;
|
||||
};
|
||||
|
||||
|
||||
size_t read(char *buffer, size_t size) {
|
||||
if (sizeRead < partialBuffer.size()) {
|
||||
size_t toRead = std::min(size, partialBuffer.size() - sizeRead);
|
||||
memcpy(buffer, &partialBuffer[sizeRead], toRead);
|
||||
sizeRead += toRead;
|
||||
return toRead;
|
||||
} else {
|
||||
size_t toRead = std::min(size, contentLength - sizeRead);
|
||||
size_t read = recv(fd, buffer, toRead, 0);
|
||||
sizeRead += read;
|
||||
return read;
|
||||
}
|
||||
}
|
||||
|
||||
void close() {
|
||||
}
|
||||
|
||||
size_t getTotalSize() { return contentLength; }
|
||||
};
|
||||
|
||||
class FileResponseReader : public ResponseReader {
|
||||
public:
|
||||
FILE *file;
|
||||
@@ -30,7 +71,7 @@ class FileResponseReader : public ResponseReader {
|
||||
fileSize = ftell(file); // get current file pointer
|
||||
fseek(file, 0, SEEK_SET); // seek back to beginning of file
|
||||
};
|
||||
~FileResponseReader() { fclose(file); };
|
||||
|
||||
|
||||
size_t read(char *buffer, size_t size) {
|
||||
return fread(buffer, 1, size, file);
|
||||
@@ -48,10 +89,13 @@ enum class RequestType { GET, POST };
|
||||
struct HTTPRequest {
|
||||
std::map<std::string, std::string> urlParams;
|
||||
std::map<std::string, std::string> queryParams;
|
||||
std::unique_ptr<ResponseReader> responseReader = std::unique_ptr<RequestBodyReader>(nullptr);
|
||||
|
||||
std::string body;
|
||||
std::string url;
|
||||
int handlerId;
|
||||
int connection;
|
||||
int contentLength;
|
||||
};
|
||||
|
||||
struct HTTPResponse {
|
||||
@@ -64,19 +108,22 @@ struct HTTPResponse {
|
||||
std::unique_ptr<ResponseReader> responseReader;
|
||||
};
|
||||
|
||||
typedef std::function<void(HTTPRequest &)> httpHandler;
|
||||
typedef std::function<void(std::unique_ptr<bell::HTTPRequest>)> httpHandler;
|
||||
|
||||
struct HTTPRoute {
|
||||
RequestType requestType;
|
||||
httpHandler handler;
|
||||
bool readBodyToStr;
|
||||
};
|
||||
|
||||
struct HTTPConnection {
|
||||
int fd = 0;
|
||||
std::vector<uint8_t> buffer;
|
||||
std::string currentLine = "";
|
||||
std::vector<uint8_t> partialBuffer = std::vector<uint8_t>();
|
||||
int contentLength = 0;
|
||||
bool isReadingBody = false;
|
||||
std::string httpMethod;
|
||||
bool toBeClosed = false;
|
||||
bool headersRead = false;
|
||||
bool isEventConnection = false;
|
||||
bool isCaptivePortal = false;
|
||||
};
|
||||
@@ -96,10 +143,11 @@ public:
|
||||
*
|
||||
* @param requestType GET or POST
|
||||
* @param endpoint registering under
|
||||
* @param readResponseToStr if true, response will be read to string, otherwise it will return a reader object
|
||||
* httpHandler lambda to be called when given endpoint gets executed
|
||||
*/
|
||||
virtual void registerHandler(RequestType requestType, const std::string & endpoint,
|
||||
httpHandler) = 0;
|
||||
httpHandler, bool readResponseToStr = true) = 0;
|
||||
|
||||
/**
|
||||
* Writes given response to a fd
|
||||
|
||||
Reference in New Issue
Block a user