Skip to content

Commit

Permalink
big progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoeken committed Aug 13, 2024
1 parent 873ff6e commit b50ae9c
Show file tree
Hide file tree
Showing 32 changed files with 294 additions and 203 deletions.
12 changes: 6 additions & 6 deletions examples/platformio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ bool setupSDCard()
}
#endif

bool PERMISSIVE_CORS(PsychicRequest* request)
bool PERMISSIVE_CORS(PsychicRequest* request, PsychicResponse* response)
{
if (request->hasHeader("Origin")) {
request->addResponseHeader("Access-Control-Allow-Origin", "*");
request->addResponseHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
request->addResponseHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept");
request->addResponseHeader("Access-Control-Max-Age", "86400");
response->addHeader("Access-Control-Allow-Origin", "*");
response->addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
response->addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept");
response->addHeader("Access-Control-Max-Age", "86400");
}

return true;
Expand Down Expand Up @@ -693,7 +693,7 @@ void setup()
});

// this will send CORS headers on every request that contains the Origin: header
server.setFilter(PERMISSIVE_CORS);
server.addMiddleware(PERMISSIVE_CORS);

// this will respond to CORS requests (note: the global server filter will automatically add the CORS headers)
server.on("*", HTTP_OPTIONS, [](PsychicRequest* request) { return request->reply(200); });
Expand Down
56 changes: 28 additions & 28 deletions middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,49 @@

# PsychicHandler

- [ ] create addMiddleware()
- [x] _filters -> _middleware
- [x] create addMiddleware()
- [ ] create removeMiddleware(name)
- [ ] create runMiddleware()
- [ ] convert handler.setFilter() to call addMiddleware with a wrapper
- [ ] depreciate handler.filter(), change to runMiddleware()
- [ ] create removeMiddleware(pointer)
- [x] create runMiddleware()
- [x] convert handler.setFilter() to call addMiddleware with a wrapper
- [ ] depreciate handler.filter() - preprocessor? how?
- [ ] what about canHandle()?
- [ ] depreciate and convert to middleware that gets added internally on construction.
- [ ] remove canHandle() calls
- [ ] handleRequest() -> handleRequest(*request, *response);
- [ ] depreciate and add new style (*request, *response) call
- [ ] _filters -> _middleware
* we should probably keep this - middleware is not context aware, and there are probably situations where we want to check handler variables
- [x] handleRequest() -> handleRequest(*request, *response);
- [ ] depreciate old style
- [x] add new style (*request, *response) call
- [x] destructor memory management
- [ ] convert auth to middleware, remove handler->needsAuthentication calls

# PsychicEndpoint

- [ ] depreciate endpoint.setFilter(), change to addMiddleware()
- [x] convert endpoint.setFilter(), change to addMiddleware()
- [ ] convert setAuthentication() to add AuthMiddleware instead.
- [ ] create addMiddleware()
- [ ] pass to PsychicHandler
- [ ] create runMiddleware()
- [ ] pass to PsychicHandler
- [x] create addMiddleware()
- [x] pass to PsychicHandler
- [x] create runMiddleware()
- [x] pass to PsychicHandler

## PsychicHttpServer

- [ ] convert server.setFilter()
- [ ] change to addMiddleware() with wrapper
- [x] _filters -> _middleware
- [x] convert server.setFilter()
- [x] change to addMiddleware() with wrapper
- [ ] create additional calls to server.on() with new callback format
- [ ] create addMiddleware()
- [ ] create runMiddleware()
- [x] create addMiddleware()
- [x] create runMiddleware()
- [ ] create removeMiddleware(name)
- [ ] _filters -> _middleware

# PsychicRequest

- [ ] add _response pointer to PsychicRequest
- [ ] request->beginReply() should return existing _response pointer
- [ ] depreciate authenticate() -> middleware function
- [x] add _response pointer to PsychicRequest
- [x] request->beginReply() should return existing _response pointer
- [ ] requestAuthentication() -> should add middleware?

# PsychicResponse

- [ ] add new constructor request(*response)
- [ ] refactor existing custom Response objects to Delegation style
- [ ] store root response pointer internally
- [ ] pass on 'standard' calls to root object
- [ ] code, contentType, headers, cookies, content, send, etc.
- [ ] existing constructors that pass in *request pointer can instead call constructor with new *request->_response style
- [x] make class final
- [x] refactor existing custom Response objects to Delegation style
- [x] store root response pointer internally
- [x] pass on all calls to root object
2 changes: 1 addition & 1 deletion src/ChunkPrinter.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include "ChunkPrinter.h"

ChunkPrinter::ChunkPrinter(PsychicResponse* response, uint8_t* buffer, size_t len) : _response(response),
ChunkPrinter::ChunkPrinter(PsychicResponseDelegate* response, uint8_t* buffer, size_t len) : _response(response),
_buffer(buffer),
_length(len),
_pos(0)
Expand Down
5 changes: 3 additions & 2 deletions src/ChunkPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
#define ChunkPrinter_h

#include "PsychicResponse.h"
#include "PsychicResponseDelegate.h"
#include <Print.h>

class ChunkPrinter : public Print
{
private:
PsychicResponse* _response;
PsychicResponseDelegate* _response;
uint8_t* _buffer;
size_t _length;
size_t _pos;

public:
ChunkPrinter(PsychicResponse* response, uint8_t* buffer, size_t len);
ChunkPrinter(PsychicResponseDelegate* response, uint8_t* buffer, size_t len);
~ChunkPrinter();

size_t write(uint8_t c) override;
Expand Down
33 changes: 25 additions & 8 deletions src/PsychicEndpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,27 @@ esp_err_t PsychicEndpoint::requestCallback(httpd_req_t* req)
PsychicEndpoint* self = (PsychicEndpoint*)req->user_ctx;
PsychicHandler* handler = self->handler();
PsychicRequest request(self->_server, req);
PsychicResponse response(&request);

// make sure we have a handler
if (handler != NULL) {
if (handler->filter(&request) && handler->canHandle(&request)) {
// check our credentials
if (handler->needsAuthentication(&request))
return handler->authenticate(&request);

// pass it to our handler
return handler->handleRequest(&request);
if (handler->canHandle(&request)) {
if (handler->runMiddleware(&request, &response)) {
// check our credentials
if (handler->needsAuthentication(&request))
return handler->authenticate(&request);

// pass it to our handler
return handler->handleRequest(&request, &response);
}
}
// pass it to our generic handlers
else
return PsychicHttpServer::requestHandler(req);
} else
return request.reply(500, "text/html", "No handler registered.");

return ESP_ERR_HTTPD_INVALID_REQ;
}

bool PsychicEndpoint::matches(const char* uri)
Expand Down Expand Up @@ -124,4 +129,16 @@ PsychicEndpoint* PsychicEndpoint::setAuthentication(const char* username, const
{
_handler->setAuthentication(username, password, method, realm, authFailMsg);
return this;
};
};

PsychicEndpoint* PsychicEndpoint::addMiddleware(PsychicMiddleware *middleware)
{
_handler->addMiddleware(middleware);
return this;
}

PsychicEndpoint* PsychicEndpoint::addMiddleware(PsychicMiddlewareFunction fn)
{
_handler->addMiddleware(fn);
return this;
}
3 changes: 3 additions & 0 deletions src/PsychicEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "PsychicCore.h"

class PsychicHandler;
class PsychicMiddleware;

#ifdef ENABLE_ASYNC
#include "async_worker.h"
Expand Down Expand Up @@ -34,6 +35,8 @@ class PsychicEndpoint

PsychicEndpoint* setFilter(PsychicRequestFilterFunction fn);
PsychicEndpoint* setAuthentication(const char* username, const char* password, HTTPAuthMethod method = BASIC_AUTH, const char* realm = "", const char* authFailMsg = "");
PsychicEndpoint* addMiddleware(PsychicMiddleware *middleware);
PsychicEndpoint* addMiddleware(PsychicMiddlewareFunction fn);

String uri();

Expand Down
8 changes: 4 additions & 4 deletions src/PsychicEventSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ PsychicEventSourceClient* PsychicEventSource::getClient(PsychicClient* client)
return getClient(client->socket());
}

esp_err_t PsychicEventSource::handleRequest(PsychicRequest* request)
esp_err_t PsychicEventSource::handleRequest(PsychicRequest* request, PsychicResponse* resp)
{
// start our open ended HTTP response
PsychicEventSourceResponse response(request);
PsychicEventSourceResponse response(response);
esp_err_t err = response.send();

// lookup our client
Expand Down Expand Up @@ -235,7 +235,7 @@ void PsychicEventSourceClient::_sendEventSentCallback(esp_err_t err, int socket,
/*****************************************/

PsychicEventSourceResponse::PsychicEventSourceResponse(PsychicRequest* request)
: PsychicResponse(request)
: PsychicResponseDelegate(request)
{
}

Expand All @@ -258,7 +258,7 @@ esp_err_t PsychicEventSourceResponse::send()

int result;
do {
result = httpd_send(_request->request(), out.c_str(), out.length());
result = httpd_send(request()->request(), out.c_str(), out.length());
} while (result == HTTPD_SOCK_ERR_TIMEOUT);

if (result < 0)
Expand Down
5 changes: 3 additions & 2 deletions src/PsychicEventSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "PsychicCore.h"
#include "PsychicHandler.h"
#include "PsychicResponse.h"
#include "PsychicResponseDelegate.h"

class PsychicEventSource;
class PsychicEventSourceResponse;
Expand Down Expand Up @@ -80,12 +81,12 @@ class PsychicEventSource : public PsychicHandler
PsychicEventSource* onOpen(PsychicEventSourceClientCallback fn);
PsychicEventSource* onClose(PsychicEventSourceClientCallback fn);

esp_err_t handleRequest(PsychicRequest* request) override final;
esp_err_t handleRequest(PsychicRequest* request, PsychicResponse* response) override final;

void send(const char* message, const char* event = NULL, uint32_t id = 0, uint32_t reconnect = 0);
};

class PsychicEventSourceResponse : public PsychicResponse
class PsychicEventSourceResponse : public PsychicResponseDelegate
{
public:
PsychicEventSourceResponse(PsychicRequest* request);
Expand Down
14 changes: 7 additions & 7 deletions src/PsychicFileResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "PsychicResponse.h"

PsychicFileResponse::PsychicFileResponse(PsychicRequest* request, FS& fs, const String& path, const String& contentType, bool download)
: PsychicResponse(request)
: PsychicResponseDelegate(request)
{
//_code = 200;
String _path(path);
Expand All @@ -15,7 +15,7 @@ PsychicFileResponse::PsychicFileResponse(PsychicRequest* request, FS& fs, const
}

_content = fs.open(_path, "r");
_contentLength = _content.size();
setContentLength(_content.size());

if (contentType == "")
_setContentType(path);
Expand All @@ -40,7 +40,7 @@ PsychicFileResponse::PsychicFileResponse(PsychicRequest* request, FS& fs, const
}

PsychicFileResponse::PsychicFileResponse(PsychicRequest* request, File content, const String& path, const String& contentType, bool download)
: PsychicResponse(request)
: PsychicResponseDelegate(request)
{
String _path(path);

Expand All @@ -50,7 +50,7 @@ PsychicFileResponse::PsychicFileResponse(PsychicRequest* request, File content,
}

_content = content;
_contentLength = _content.size();
setContentLength(_content.size());

if (contentType == "")
_setContentType(path);
Expand Down Expand Up @@ -136,14 +136,14 @@ esp_err_t PsychicFileResponse::send()
if (buffer == NULL)
{
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(this->_request->request(), HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to allocate memory.");
httpd_resp_send_err(request()->request(), HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to allocate memory.");
return ESP_FAIL;
}

size_t readSize = _content.readBytes((char*)buffer, size);

this->setContent(buffer, readSize);
err = PsychicResponse::send();
err = PsychicResponseDelegate::send();

free(buffer);
}
Expand All @@ -154,7 +154,7 @@ esp_err_t PsychicFileResponse::send()
if (chunk == NULL)
{
/* Respond with 500 Internal Server Error */
httpd_resp_send_err(this->_request->request(), HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to allocate memory.");
httpd_resp_send_err(request()->request(), HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to allocate memory.");
return ESP_FAIL;
}

Expand Down
3 changes: 2 additions & 1 deletion src/PsychicFileResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

#include "PsychicCore.h"
#include "PsychicResponse.h"
#include "PsychicResponseDelegate.h"

class PsychicRequest;

class PsychicFileResponse : public PsychicResponse
class PsychicFileResponse : public PsychicResponseDelegate
{
using File = fs::File;
using FS = fs::FS;
Expand Down
Loading

0 comments on commit b50ae9c

Please sign in to comment.