@@ -446,7 +446,7 @@ void HTTPConnection::loop() {
446446 }
447447
448448 // Create request context
449- HTTPRequest req = HTTPRequest (this , _httpHeaders, resolvedResource.getParams (), _httpResource, _httpMethod, resolvedResource.getMatchingNode ()-> _tag );
449+ HTTPRequest req = HTTPRequest (this , _httpHeaders, resolvedResource.getMatchingNode (), resolvedResource.getParams (), _httpResource );
450450 HTTPResponse res = HTTPResponse (this );
451451
452452 // Add default headers to the response
@@ -477,6 +477,10 @@ void HTTPConnection::loop() {
477477 next = std::function<void ()>(std::bind ((*itMw), &req, &res, next));
478478 itMw++;
479479 }
480+
481+ // We insert the internal validation middleware at the start of the chain:
482+ next = std::function<void ()>(std::bind (&validationMiddleware, &req, &res, next));
483+
480484 // Call the whole chain
481485 next ();
482486
@@ -581,6 +585,32 @@ bool HTTPConnection::checkWebsocket() {
581585 return false ;
582586}
583587
588+ /* *
589+ * Middleware function that handles the validation of parameters
590+ */
591+ void validationMiddleware (HTTPRequest * req, HTTPResponse * res, std::function<void ()> next) {
592+ bool valid = true ;
593+ // Get the matched node
594+ HTTPNode * node = req->getResolvedNode ();
595+ // Get the parameters
596+ ResourceParameters * params = req->getParams ();
597+
598+ // Iterate over the validators and run them
599+ std::vector<HTTPValidator*> * validators = node->getValidators ();
600+ for (std::vector<HTTPValidator*>::iterator validator = validators->begin (); valid && validator != validators->end (); ++validator) {
601+ std::string param = params->getUrlParameter ((*validator)->_idx );
602+ valid = ((*validator)->_validatorFunction )(param);
603+ }
604+
605+ if (valid) {
606+ next ();
607+ } else {
608+ res->setStatusCode (400 );
609+ res->setStatusText (" Bad Request" );
610+ res->print (" 400 Bad Request" );
611+ }
612+ }
613+
584614/* *
585615 * Handler function for the websocket handshake. Will be used by HTTPConnection if a websocket is detected
586616 */
0 commit comments