Allow property set on the /json/ uri handler
This commit is contained in:
parent
bca8b75a81
commit
aeb1eedba8
1 changed files with 65 additions and 32 deletions
|
@ -33,55 +33,88 @@ bool JsonUriHandler::handleRequest( const HTTPRequest & request, HTTPResponse &
|
|||
{
|
||||
response.Header["Content-Type"] = "application/json; charset=UTF-8";
|
||||
response.Header["Access-Control-Allow-Origin"] = "*";
|
||||
response.Header["Access-Control-Allow-Methods"] = "OPTIONS, GET";
|
||||
response.Header["Access-Control-Allow-Methods"] = "OPTIONS, GET, POST";
|
||||
response.Header["Access-Control-Allow-Headers"] = "Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token";
|
||||
|
||||
if( request.Method == "OPTIONS" ){
|
||||
return true; // OPTIONS only needs the headers
|
||||
}
|
||||
|
||||
if( request.Method != "GET" ){
|
||||
response.Header["Allow"] = "OPTIONS, GET";
|
||||
response.StatusCode = 405;
|
||||
response.Content = "{}";
|
||||
return true;
|
||||
}
|
||||
if( request.Method == "GET" ){
|
||||
string propertyPath = request.Uri;
|
||||
|
||||
string propertyPath = request.Uri;
|
||||
// strip the uri prefix of our handler
|
||||
propertyPath = propertyPath.substr( getUri().size() );
|
||||
|
||||
// strip the uri prefix of our handler
|
||||
propertyPath = propertyPath.substr( getUri().size() );
|
||||
// strip the querystring
|
||||
size_t pos = propertyPath.find( '?' );
|
||||
if( pos != string::npos ) {
|
||||
propertyPath = propertyPath.substr( 0, pos-1 );
|
||||
}
|
||||
|
||||
// strip the querystring
|
||||
size_t pos = propertyPath.find( '?' );
|
||||
if( pos != string::npos ) {
|
||||
propertyPath = propertyPath.substr( 0, pos-1 );
|
||||
}
|
||||
// skip trailing '/' - not very efficient but shouldn't happen too often
|
||||
while( false == propertyPath.empty() && propertyPath[ propertyPath.length()-1 ] == '/' )
|
||||
propertyPath = propertyPath.substr(0,propertyPath.length()-1);
|
||||
|
||||
// skip trailing '/' - not very efficient but shouldn't happen too often
|
||||
while( false == propertyPath.empty() && propertyPath[ propertyPath.length()-1 ] == '/' )
|
||||
propertyPath = propertyPath.substr(0,propertyPath.length()-1);
|
||||
// max recursion depth
|
||||
int depth = atoi(request.RequestVariables.get("d").c_str());
|
||||
if( depth < 1 ) depth = 1; // at least one level
|
||||
|
||||
// max recursion depth
|
||||
int depth = atoi(request.RequestVariables.get("d").c_str());
|
||||
if( depth < 1 ) depth = 1; // at least one level
|
||||
// pretty print (y) or compact print (default)
|
||||
bool indent = request.RequestVariables.get("i") == "y";
|
||||
bool timestamp = request.RequestVariables.get("t") == "y";
|
||||
|
||||
// pretty print (y) or compact print (default)
|
||||
bool indent = request.RequestVariables.get("i") == "y";
|
||||
bool timestamp = request.RequestVariables.get("t") == "y";
|
||||
SGPropertyNode_ptr node = fgGetNode( string("/") + propertyPath );
|
||||
if( false == node.valid() ) {
|
||||
response.StatusCode = 404;
|
||||
response.Content = "{}";
|
||||
SG_LOG(SG_NETWORK,SG_WARN, "Node not found: '" << propertyPath << "'");
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
response.Content = JSON::toJsonString( indent, node, depth, timestamp ? fgGetDouble("/sim/time/elapsed-sec") : -1.0 );
|
||||
|
||||
SGPropertyNode_ptr node = fgGetNode( string("/") + propertyPath );
|
||||
if( false == node.valid() ) {
|
||||
response.StatusCode = 404;
|
||||
response.Content = "{}";
|
||||
SG_LOG(SG_NETWORK,SG_WARN, "Node not found: '" << propertyPath << "'");
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
if( request.Method == "POST" ) {
|
||||
SG_LOG(SG_NETWORK,SG_INFO, "Setting properties from JSON: " << request.Content );
|
||||
string propertyPath = request.Uri;
|
||||
propertyPath = propertyPath.substr( getUri().size() );
|
||||
|
||||
response.Content = JSON::toJsonString( indent, node, depth, timestamp ? fgGetDouble("/sim/time/elapsed-sec") : -1.0 );
|
||||
// strip the querystring
|
||||
size_t pos = propertyPath.find( '?' );
|
||||
if( pos != string::npos ) {
|
||||
propertyPath = propertyPath.substr( 0, pos-1 );
|
||||
}
|
||||
|
||||
return true;
|
||||
// skip trailing '/' - not very efficient but shouldn't happen too often
|
||||
while( false == propertyPath.empty() && propertyPath[ propertyPath.length()-1 ] == '/' )
|
||||
propertyPath = propertyPath.substr(0,propertyPath.length()-1);
|
||||
|
||||
SGPropertyNode_ptr node = fgGetNode( string("/") + propertyPath );
|
||||
if( false == node.valid() ) {
|
||||
response.StatusCode = 404;
|
||||
response.Content = "{}";
|
||||
SG_LOG(SG_NETWORK,SG_WARN, "Node not found: '" << propertyPath << "'");
|
||||
return true;
|
||||
}
|
||||
|
||||
cJSON * json = cJSON_Parse( request.Content.c_str() );
|
||||
if( NULL != json ) {
|
||||
JSON::toProp( json, node );
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
response.Content = "{}";
|
||||
return true;
|
||||
}
|
||||
|
||||
response.Header["Allow"] = "OPTIONS, GET";
|
||||
response.StatusCode = 405;
|
||||
response.Content = "{}";
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue