|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
Parses and validates incoming requests against an XML REST API definition. More...
#include <RESTAPI.h>
Public Member Functions | |
| RESTParser () | |
| Create an uninitialised parser; call init() before use. | |
| virtual | ~RESTParser () |
| Destructor; deletes the owned RESTHTTPCall objects. | |
| bool | init (const char *restDefinition) |
| Load and validate the XML REST API definition. | |
| RESTRequest * | parseRequest (HTTPRequest *req) |
| Parse an incoming HTTP request against the API definition. | |
| RESTRequest * | parseRequest (DataMessage *msg) |
| Parse a request carried in a DataMessage against the API definition. | |
| const char * | getLastErrorString () |
Static Public Member Functions | |
| static bool | UnitTest () |
| Run the RESTParser unit tests. | |
Protected Member Functions | |
| XMLNode | findTopNodeForRequest (const char *requestString) |
| std::map< std::string, RESTParam > | parseParamSet (XMLNode objectNode) |
| XMLNode | parseRequest (const char *requestString, std::map< std::string, RESTParam > ¶mSet, RESTRequest *restReq, std::string &signature) |
| bool | extractParameters (HTTPRequest *req, std::map< std::string, RESTParam > ¶mSet, RESTRequest *restReq) |
| bool | extractParameters (DataMessage *msg, std::map< std::string, RESTParam > ¶mSet, RESTRequest *restReq) |
| bool | extractArrayParameters (HTTPRequest *req, std::map< std::string, RESTParam > ¶mSet, RESTRequest *restReq, int entryNum, const char *content, uint32 contentSize, const char *contentType) |
| bool | extractArrayParameters (DataMessage *msg, std::map< std::string, RESTParam > ¶mSet, RESTRequest *restReq, int entryNum, const char *content, uint32 contentSize, const char *contentType) |
| bool | setParameterValue (HTTPRequest *req, RESTParam ¶m, std::string value, RESTRequest *restReq, uint32 entryNum=0) |
| bool | setParameterValue (DataMessage *msg, RESTParam ¶m, std::string value, RESTRequest *restReq, uint32 entryNum=0) |
Protected Attributes | |
| XMLNode | mainNode |
| std::string | lastErrorString |
| std::map< std::string, std::map< std::string, RESTParam > > | paramSets |
| std::map< std::string, RESTHTTPCall * > | httpCalls |
Parses and validates incoming requests against an XML REST API definition.
Initialise once with init() (the <restapi> XML), then call parseRequest() per incoming HTTPRequest or DataMessage. Returns a new RESTRequest per call (caller takes ownership); check RESTRequest::getStatus(). Owns the RESTHTTPCall objects defined in the API. init()/parseRequest() are not synchronised — initialise before serving and treat the parser as read-only afterwards.
| cmlabs::RESTParser::RESTParser | ( | ) |
Create an uninitialised parser; call init() before use.
Definition at line 13 of file RESTAPI.cpp.
References mainNode.
Referenced by UnitTest().
|
virtual |
Destructor; deletes the owned RESTHTTPCall objects.
Definition at line 16 of file RESTAPI.cpp.
References httpCalls.
|
protected |
Definition at line 1030 of file RESTAPI.cpp.
References cmlabs::RESTRequest::addToErrorString(), cmlabs::RESTParam::ARRAY, cmlabs::RESTParam::BINARY, cmlabs::XMLResults::error, cmlabs::eXMLErrorNone, cmlabs::DataMessage::getData(), GetJSONValue(), cmlabs::DataMessage::getString(), cmlabs::RESTParam::IMAGE, cmlabs::XMLNode::isEmpty(), jsmn_init(), jsmn_parse(), JSMN_UNDEFINED, cmlabs::RESTRequest::setBinary(), setParameterValue(), cmlabs::utils::StringFormat(), cmlabs::utils::stristr(), and cmlabs::RESTParam::VIDEO.
|
protected |
Definition at line 899 of file RESTAPI.cpp.
References cmlabs::RESTRequest::addToErrorString(), cmlabs::RESTParam::ARRAY, cmlabs::RESTParam::BINARY, cmlabs::XMLResults::error, cmlabs::eXMLErrorNone, GetJSONValue(), cmlabs::HTTPRequest::getPostData(), cmlabs::HTTPRequest::getPostDataType(), cmlabs::RESTParam::IMAGE, cmlabs::XMLNode::isEmpty(), jsmn_init(), jsmn_parse(), JSMN_UNDEFINED, cmlabs::RESTRequest::setBinary(), setParameterValue(), cmlabs::utils::StringFormat(), cmlabs::utils::stristr(), and cmlabs::RESTParam::VIDEO.
Referenced by extractParameters(), and extractParameters().
|
protected |
Definition at line 699 of file RESTAPI.cpp.
References cmlabs::RESTRequest::addToErrorString(), cmlabs::RESTParam::ARRAY, cmlabs::RESTParam::BINARY, cmlabs::RESTRequest::clearErrorState(), cmlabs::XMLResults::error, cmlabs::eXMLErrorNone, extractArrayParameters(), cmlabs::DataMessage::getData(), GetJSONArrayIndexes(), GetJSONValue(), cmlabs::DataMessage::getString(), cmlabs::RESTParam::IMAGE, cmlabs::XMLNode::isEmpty(), jsmn_init(), jsmn_parse(), JSMN_UNDEFINED, paramSets, cmlabs::RESTRequest::setBinary(), setParameterValue(), cmlabs::utils::StringFormat(), cmlabs::utils::stristr(), and cmlabs::RESTParam::VIDEO.
|
protected |
Definition at line 497 of file RESTAPI.cpp.
References cmlabs::RESTRequest::addToErrorString(), cmlabs::RESTParam::ARRAY, cmlabs::RESTParam::BINARY, cmlabs::RESTRequest::clearErrorState(), cmlabs::XMLResults::error, cmlabs::eXMLErrorNone, extractArrayParameters(), cmlabs::HTTPRequest::getHeaderEntry(), GetJSONArrayIndexes(), GetJSONValue(), cmlabs::HTTPRequest::getPostData(), cmlabs::HTTPRequest::getPostDataType(), cmlabs::RESTParam::IMAGE, cmlabs::XMLNode::isEmpty(), jsmn_init(), jsmn_parse(), JSMN_UNDEFINED, paramSets, cmlabs::RESTRequest::setBinary(), setParameterValue(), cmlabs::utils::StringFormat(), cmlabs::utils::stristr(), and cmlabs::RESTParam::VIDEO.
Referenced by parseRequest(), and parseRequest().
|
protected |
Definition at line 1339 of file RESTAPI.cpp.
References cmlabs::XMLNode::getAttribute(), and mainNode.
Referenced by parseRequest().
|
inline |
Definition at line 181 of file RESTAPI.h.
References lastErrorString.
Referenced by UnitTest().
| bool cmlabs::RESTParser::init | ( | const char * | restDefinition | ) |
Load and validate the XML REST API definition.
| restDefinition | XML text with <restapi> as the main tag. |
Definition at line 26 of file RESTAPI.cpp.
References cmlabs::XMLResults::error, cmlabs::eXMLErrorFirstTagNotFound, cmlabs::eXMLErrorNone, cmlabs::XMLNode::getAttribute(), httpCalls, cmlabs::RESTHTTPCall::init(), lastErrorString, mainNode, cmlabs::XMLResults::nColumn, cmlabs::XMLResults::nLine, paramSets, parseParamSet(), and cmlabs::utils::StringFormat().
Referenced by UnitTest().
|
protected |
Definition at line 85 of file RESTAPI.cpp.
References cmlabs::RESTParam::allowEmpty, cmlabs::RESTParam::ARRAY, cmlabs::RESTParam::BINARY, cmlabs::RESTParam::FLOAT, cmlabs::XMLNode::getAttribute(), cmlabs::XMLNode::getChildNode(), cmlabs::RESTParam::IMAGE, cmlabs::RESTParam::INTEGER, lastErrorString, cmlabs::RESTParam::name, cmlabs::XMLNode::nChildNode(), cmlabs::RESTParam::objects, cmlabs::RESTParam::optional, cmlabs::utils::StringFormat(), cmlabs::RESTParam::TEXT, cmlabs::RESTParam::type, and cmlabs::RESTParam::VIDEO.
Referenced by init().
|
protected |
Definition at line 298 of file RESTAPI.cpp.
References cmlabs::RESTRequest::addToErrorString(), cmlabs::utils::Ascii2Int64(), cmlabs::RESTRequest::clearErrorState(), cmlabs::html::DecodeHTML(), cmlabs::RESTRequest::FAILED_DESCRIPTION, cmlabs::RESTRequest::FAILED_EMPTY, cmlabs::RESTRequest::FAILED_FORMAT, cmlabs::RESTRequest::FAILED_UNKNOWN_REQUEST, findTopNodeForRequest(), cmlabs::XMLNode::getAttribute(), cmlabs::XMLNode::getChildNode(), cmlabs::XMLNode::getName(), cmlabs::XMLNode::isEmpty(), cmlabs::utils::IsTextNumeric(), mainNode, cmlabs::XMLNode::nChildNode(), paramSets, parseRequest(), cmlabs::RESTRequest::setFilter(), cmlabs::RESTRequest::setInt(), cmlabs::RESTRequest::setRequireAuthentication(), cmlabs::RESTRequest::setStatus(), cmlabs::RESTRequest::setString(), stricmp, cmlabs::utils::StringFormat(), cmlabs::utils::TextListSplit(), cmlabs::utils::TextMultiMapSplit(), and cmlabs::utils::TextTrimQuotes().
| RESTRequest * cmlabs::RESTParser::parseRequest | ( | DataMessage * | msg | ) |
Parse a request carried in a DataMessage against the API definition.
| msg | The message to parse (not consumed). |
Definition at line 209 of file RESTAPI.cpp.
References cmlabs::RESTRequest::addToErrorString(), extractParameters(), cmlabs::RESTRequest::FAILED_AUTH, cmlabs::RESTRequest::FAILED_FORMAT, cmlabs::RESTRequest::FAILED_PARAM, cmlabs::XMLNode::getAttribute(), cmlabs::DataMessage::getInt(), cmlabs::RESTRequest::getRequireAuthentication(), cmlabs::DataMessage::getString(), HTTP_DELETE, HTTP_GET, HTTP_OPTIONS, HTTP_POST, HTTP_PUT, cmlabs::HTTP_Type, cmlabs::RESTRequest::httpCall, httpCalls, cmlabs::XMLNode::isEmpty(), parseRequest(), cmlabs::RESTRequest::setRequestSignature(), cmlabs::RESTRequest::setStatus(), cmlabs::RESTRequest::setString(), cmlabs::utils::StringFormat(), cmlabs::utils::stristr(), cmlabs::RESTRequest::SUCCESS, and cmlabs::RESTRequest::SUCCESS_OPTIONS.
| RESTRequest * cmlabs::RESTParser::parseRequest | ( | HTTPRequest * | req | ) |
Parse an incoming HTTP request against the API definition.
| req | The HTTP request to parse (not consumed). |
Definition at line 127 of file RESTAPI.cpp.
References cmlabs::RESTRequest::addToErrorString(), extractParameters(), cmlabs::RESTRequest::FAILED_AUTH, cmlabs::RESTRequest::FAILED_FORMAT, cmlabs::RESTRequest::FAILED_PARAM, cmlabs::XMLNode::getAttribute(), cmlabs::HTTPRequest::getHeaderEntry(), cmlabs::RESTRequest::getRequireAuthentication(), cmlabs::HTTPRequest::getURI(), HTTP_DELETE, HTTP_GET, HTTP_OPTIONS, HTTP_POST, HTTP_PUT, cmlabs::HTTP_Type, cmlabs::RESTRequest::httpCall, httpCalls, cmlabs::XMLNode::isEmpty(), parseRequest(), cmlabs::RESTRequest::setRequestSignature(), cmlabs::RESTRequest::setStatus(), cmlabs::RESTRequest::setString(), cmlabs::utils::StringFormat(), cmlabs::utils::stristr(), cmlabs::RESTRequest::SUCCESS, cmlabs::RESTRequest::SUCCESS_OPTIONS, and cmlabs::HTTPRequest::type.
Referenced by parseRequest(), parseRequest(), parseRequest(), and UnitTest().
|
protected |
Definition at line 1262 of file RESTAPI.cpp.
References cmlabs::RESTRequest::addToErrorString(), cmlabs::utils::Ascii2Float64(), cmlabs::utils::Ascii2Int64(), cmlabs::RESTParam::BINARY, cmlabs::RESTParam::FLOAT, cmlabs::DataMessage::getData(), cmlabs::DataMessage::getString(), cmlabs::RESTParam::IMAGE, cmlabs::RESTParam::INTEGER, cmlabs::utils::IsTextNumeric(), cmlabs::RESTParam::name, cmlabs::RESTRequest::setBinary(), cmlabs::RESTRequest::setDouble(), cmlabs::RESTRequest::setInt(), cmlabs::RESTRequest::setString(), cmlabs::utils::StringFormat(), cmlabs::utils::StringSingleReplace(), cmlabs::RESTParam::TEXT, cmlabs::RESTParam::type, and cmlabs::RESTParam::VIDEO.
|
protected |
Definition at line 1201 of file RESTAPI.cpp.
References cmlabs::RESTRequest::addToErrorString(), cmlabs::utils::Ascii2Float64(), cmlabs::utils::Ascii2Int64(), cmlabs::RESTParam::BINARY, cmlabs::RESTParam::FLOAT, cmlabs::HTTPRequest::getPostData(), cmlabs::HTTPRequest::getPostDataType(), cmlabs::RESTParam::IMAGE, cmlabs::RESTParam::INTEGER, cmlabs::utils::IsTextNumeric(), cmlabs::RESTParam::name, cmlabs::RESTRequest::setBinary(), cmlabs::RESTRequest::setDouble(), cmlabs::RESTRequest::setInt(), cmlabs::RESTRequest::setString(), cmlabs::utils::StringFormat(), cmlabs::utils::StringSingleReplace(), cmlabs::RESTParam::TEXT, cmlabs::RESTParam::type, and cmlabs::RESTParam::VIDEO.
Referenced by extractArrayParameters(), extractArrayParameters(), extractParameters(), and extractParameters().
|
static |
Run the RESTParser unit tests.
Definition at line 1426 of file RESTAPI.cpp.
References cmlabs::CTRL_TEST, cmlabs::unittest::detail(), cmlabs::unittest::fail(), cmlabs::RESTRequest::getErrorString(), getLastErrorString(), cmlabs::RESTRequest::getStatus(), cmlabs::GetTimeNow(), HTTP_GET, init(), cmlabs::unittest::metric(), parseRequest(), cmlabs::HTTPRequest::processHeader(), cmlabs::unittest::progress(), RESTParser(), cmlabs::DataMessage::setInt(), cmlabs::DataMessage::setString(), cmlabs::RESTRequest::SUCCESS, and cmlabs::HTTPRequest::type.
Referenced by cmlabs::Register_RESTParser_Tests().
|
protected |
Definition at line 187 of file RESTAPI.h.
Referenced by init(), parseRequest(), parseRequest(), and ~RESTParser().
|
protected |
Definition at line 185 of file RESTAPI.h.
Referenced by getLastErrorString(), init(), and parseParamSet().
|
protected |
Definition at line 184 of file RESTAPI.h.
Referenced by findTopNodeForRequest(), init(), parseRequest(), and RESTParser().
|
protected |
Definition at line 186 of file RESTAPI.h.
Referenced by extractParameters(), extractParameters(), init(), and parseRequest().