PsychichHTTP v2-dev

This commit is contained in:
iranl
2024-12-30 14:37:09 +01:00
parent 2cf5201285
commit 78459c2d08
118 changed files with 5453 additions and 4972 deletions

View File

@@ -3,17 +3,8 @@
#define PH_TAG "psychic"
//version numbers
#define PSYCHIC_HTTP_VERSION_MAJOR 1
#define PSYCHIC_HTTP_VERSION_MINOR 1
#define PSYCHIC_HTTP_VERSION_PATCH 0
#ifndef MAX_COOKIE_SIZE
#define MAX_COOKIE_SIZE 512
#endif
#ifndef FILE_CHUNK_SIZE
#define FILE_CHUNK_SIZE 8*1024
#define FILE_CHUNK_SIZE 8 * 1024
#endif
#ifndef STREAM_CHUNK_SIZE
@@ -21,87 +12,93 @@
#endif
#ifndef MAX_UPLOAD_SIZE
#define MAX_UPLOAD_SIZE (2048*1024) // 2MB
#define MAX_UPLOAD_SIZE (2048 * 1024) // 2MB
#endif
#ifndef MAX_REQUEST_BODY_SIZE
#define MAX_REQUEST_BODY_SIZE (16*1024) //16K
#define MAX_REQUEST_BODY_SIZE (16 * 1024) // 16K
#endif
#ifdef ARDUINO
#include <Arduino.h>
#endif
#include <esp_http_server.h>
#include <map>
#include <list>
#include <libb64/cencode.h>
#include "esp_random.h"
#include "MD5Builder.h"
#include <UrlEncode.h>
#include "FS.h"
#include "MD5Builder.h"
#include "esp_random.h"
#include <ArduinoJson.h>
#include <UrlEncode.h>
#include <esp_http_server.h>
#include <libb64/cencode.h>
#include <list>
#include <map>
enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
#ifdef PSY_DEVMODE
#include "ArduinoTrace.h"
#endif
enum HTTPAuthMethod {
BASIC_AUTH,
DIGEST_AUTH
};
String urlDecode(const char* encoded);
class PsychicHttpServer;
class PsychicRequest;
class PsychicResponse;
class PsychicWebSocketRequest;
class PsychicClient;
//filter function definition
typedef std::function<bool(PsychicRequest *request)> PsychicRequestFilterFunction;
// filter function definition
typedef std::function<bool(PsychicRequest* request)> PsychicRequestFilterFunction;
//client connect callback
typedef std::function<void(PsychicClient *client)> PsychicClientCallback;
// middleware function definition
typedef std::function<esp_err_t()> PsychicMiddlewareNext;
typedef std::function<esp_err_t(PsychicRequest* request, PsychicResponse* response, PsychicMiddlewareNext next)> PsychicMiddlewareCallback;
//callback definitions
typedef std::function<esp_err_t(PsychicRequest *request)> PsychicHttpRequestCallback;
typedef std::function<esp_err_t(PsychicRequest *request, JsonVariant &json)> PsychicJsonRequestCallback;
// client connect callback
typedef std::function<void(PsychicClient* client)> PsychicClientCallback;
// callback definitions
typedef std::function<esp_err_t(PsychicRequest* request, PsychicResponse* response)> PsychicHttpRequestCallback;
typedef std::function<esp_err_t(PsychicRequest* request, PsychicResponse* response, JsonVariant& json)> PsychicJsonRequestCallback;
typedef std::function<esp_err_t(PsychicRequest* request, const String& filename, uint64_t index, uint8_t* data, size_t len, bool final)> PsychicUploadCallback;
struct HTTPHeader {
char * field;
char * value;
String field;
String value;
};
class DefaultHeaders {
std::list<HTTPHeader> _headers;
class DefaultHeaders
{
std::list<HTTPHeader> _headers;
public:
DefaultHeaders() {}
public:
DefaultHeaders() {}
void addHeader(const String& field, const String& value)
{
addHeader(field.c_str(), value.c_str());
}
void addHeader(const String& field, const String& value)
{
_headers.push_back({field, value});
}
void addHeader(const char * field, const char * value)
{
HTTPHeader header;
void addHeader(const char* field, const char* value)
{
_headers.push_back({field, value});
}
//these are just going to stick around forever.
header.field =(char *)malloc(strlen(field)+1);
header.value = (char *)malloc(strlen(value)+1);
const std::list<HTTPHeader>& getHeaders() { return _headers; }
strlcpy(header.field, field, strlen(field)+1);
strlcpy(header.value, value, strlen(value)+1);
// delete the copy constructor, singleton class
DefaultHeaders(DefaultHeaders const&) = delete;
DefaultHeaders& operator=(DefaultHeaders const&) = delete;
_headers.push_back(header);
}
const std::list<HTTPHeader>& getHeaders() { return _headers; }
//delete the copy constructor, singleton class
DefaultHeaders(DefaultHeaders const &) = delete;
DefaultHeaders &operator=(DefaultHeaders const &) = delete;
//single static class interface
static DefaultHeaders &Instance() {
static DefaultHeaders instance;
return instance;
}
// single static class interface
static DefaultHeaders& Instance()
{
static DefaultHeaders instance;
return instance;
}
};
#endif //PsychicCore_h
#endif // PsychicCore_h