-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from mathieucarbou/response-override
Response override
- Loading branch information
Showing
14 changed files
with
107 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,6 @@ | ||
#include "PsychicMiddleware.h" | ||
#include "PsychicMiddlewareChain.h" | ||
#include "PsychicRequest.h" | ||
#include "PsychicResponse.h" | ||
|
||
PsychicMiddlewareClosure::PsychicMiddlewareClosure(PsychicMiddlewareFunction fn) : _fn(fn) | ||
esp_err_t PsychicMiddlewareFunction::run(PsychicRequest* request, PsychicResponse* response, PsychicMiddlewareNext next) | ||
{ | ||
assert(_fn); | ||
return _fn(request, request->response(), next); | ||
} | ||
esp_err_t PsychicMiddlewareClosure::run(PsychicRequest* request, PsychicResponse* response, PsychicMiddlewareCallback next) | ||
{ | ||
return _fn(request, response, next); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,47 @@ | ||
#include "PsychicMiddlewareChain.h" | ||
|
||
PsychicMiddlewareChain::PsychicMiddlewareChain() {} | ||
|
||
PsychicMiddlewareChain::~PsychicMiddlewareChain() | ||
{ | ||
for (auto middleware : _middleware) { | ||
if (middleware->_managed) | ||
for (auto middleware : _middleware) | ||
if (middleware->_freeOnRemoval) | ||
delete middleware; | ||
} | ||
_middleware.clear(); | ||
} | ||
|
||
void PsychicMiddlewareChain::add(PsychicMiddleware* middleware) | ||
void PsychicMiddlewareChain::addMiddleware(PsychicMiddleware* middleware) | ||
{ | ||
_middleware.push_back(middleware); | ||
} | ||
|
||
void PsychicMiddlewareChain::add(PsychicMiddlewareFunction fn) | ||
void PsychicMiddlewareChain::addMiddleware(PsychicMiddlewareCallback fn) | ||
{ | ||
PsychicMiddlewareClosure* closure = new PsychicMiddlewareClosure(fn); | ||
closure->_managed = true; | ||
PsychicMiddlewareFunction* closure = new PsychicMiddlewareFunction(fn); | ||
closure->_freeOnRemoval = true; | ||
_middleware.push_back(closure); | ||
} | ||
|
||
void PsychicMiddlewareChain::remove(PsychicMiddleware* middleware) | ||
void PsychicMiddlewareChain::removeMiddleware(PsychicMiddleware* middleware) | ||
{ | ||
_middleware.remove(middleware); | ||
if (middleware->_freeOnRemoval) | ||
delete middleware; | ||
} | ||
|
||
esp_err_t PsychicMiddlewareChain::run(PsychicRequest* request, PsychicResponse* response, PsychicMiddlewareCallback finalizer) | ||
esp_err_t PsychicMiddlewareChain::runChain(PsychicRequest* request, PsychicMiddlewareNext finalizer) | ||
{ | ||
if (_middleware.size() == 0) { | ||
return finalizer(request, response); | ||
} | ||
if (_middleware.size() == 0) | ||
return finalizer(); | ||
|
||
PsychicMiddlewareCallback next; | ||
PsychicMiddlewareNext next; | ||
std::list<PsychicMiddleware*>::iterator it = _middleware.begin(); | ||
|
||
next = [this, &next, &it, finalizer](PsychicRequest* request, PsychicResponse* response) { | ||
if (it != _middleware.end()) { | ||
PsychicMiddleware* m = *it; | ||
it++; | ||
return m->run(request, response, next); | ||
} else { | ||
return finalizer(request, response); | ||
} | ||
next = [this, &next, &it, request, finalizer]() { | ||
if (it == _middleware.end()) | ||
return finalizer(); | ||
PsychicMiddleware* m = *it; | ||
it++; | ||
return m->run(request, request->response(), next); | ||
}; | ||
|
||
return next(request, response); | ||
return next(); | ||
} |
Oops, something went wrong.