Make JsonStore path parsing more robust. Should fix the

invalid path problem.
user_profiles
Mic Bowman 2013-02-08 22:43:15 -08:00
parent 6d825d7ea2
commit 7bb82c8f2e
2 changed files with 59 additions and 21 deletions

View File

@ -81,7 +81,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
protected static Regex m_ParsePassFour = new Regex("\\.+"); protected static Regex m_ParsePassFour = new Regex("\\.+");
// expression used to validate the full path, this is canonical representation // expression used to validate the full path, this is canonical representation
protected static Regex m_ValidatePath = new Regex("^\\.(({[^}]+}|\\[[0-9]+\\]|\\[\\+\\])\\.)+$"); protected static Regex m_ValidatePath = new Regex("^\\.(({[^}]+}|\\[[0-9]+\\]|\\[\\+\\])\\.)*$");
// expression used to match path components // expression used to match path components
protected static Regex m_PathComponent = new Regex("\\.({[^}]+}|\\[[0-9]+\\]|\\[\\+\\]+)"); protected static Regex m_PathComponent = new Regex("\\.({[^}]+}|\\[[0-9]+\\]|\\[\\+\\]+)");
@ -107,9 +107,17 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
/// ///
/// </summary> /// </summary>
// ----------------------------------------------------------------- // -----------------------------------------------------------------
public static string CanonicalPathExpression(string path) public static bool CanonicalPathExpression(string ipath, out string opath)
{ {
return PathExpressionToKey(ParsePathExpression(path)); Stack<string> path;
if (! ParsePathExpression(ipath,out path))
{
opath = "";
return false;
}
opath = PathExpressionToKey(path);
return true;
} }
// ----------------------------------------------------------------- // -----------------------------------------------------------------
@ -139,7 +147,10 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
// ----------------------------------------------------------------- // -----------------------------------------------------------------
public bool TestPath(string expr, bool useJson) public bool TestPath(string expr, bool useJson)
{ {
Stack<string> path = ParsePathExpression(expr); Stack<string> path;
if (! ParsePathExpression(expr,out path))
return false;
OSD result = ProcessPathExpression(ValueStore,path); OSD result = ProcessPathExpression(ValueStore,path);
if (result == null) if (result == null)
@ -158,7 +169,13 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
// ----------------------------------------------------------------- // -----------------------------------------------------------------
public bool GetValue(string expr, out string value, bool useJson) public bool GetValue(string expr, out string value, bool useJson)
{ {
Stack<string> path = ParsePathExpression(expr); Stack<string> path;
if (! ParsePathExpression(expr,out path))
{
value = "";
return false;
}
OSD result = ProcessPathExpression(ValueStore,path); OSD result = ProcessPathExpression(ValueStore,path);
return ConvertOutputValue(result,out value,useJson); return ConvertOutputValue(result,out value,useJson);
} }
@ -192,7 +209,10 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
// ----------------------------------------------------------------- // -----------------------------------------------------------------
public bool TakeValue(string expr, bool useJson, TakeValueCallback cback) public bool TakeValue(string expr, bool useJson, TakeValueCallback cback)
{ {
Stack<string> path = ParsePathExpression(expr); Stack<string> path;
if (! ParsePathExpression(expr,out path))
return false;
string pexpr = PathExpressionToKey(path); string pexpr = PathExpressionToKey(path);
OSD result = ProcessPathExpression(ValueStore,path); OSD result = ProcessPathExpression(ValueStore,path);
@ -223,7 +243,10 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
// ----------------------------------------------------------------- // -----------------------------------------------------------------
public bool ReadValue(string expr, bool useJson, TakeValueCallback cback) public bool ReadValue(string expr, bool useJson, TakeValueCallback cback)
{ {
Stack<string> path = ParsePathExpression(expr); Stack<string> path;
if (! ParsePathExpression(expr,out path))
return false;
string pexpr = PathExpressionToKey(path); string pexpr = PathExpressionToKey(path);
OSD result = ProcessPathExpression(ValueStore,path); OSD result = ProcessPathExpression(ValueStore,path);
@ -253,7 +276,10 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
// ----------------------------------------------------------------- // -----------------------------------------------------------------
protected bool SetValueFromExpression(string expr, OSD ovalue) protected bool SetValueFromExpression(string expr, OSD ovalue)
{ {
Stack<string> path = ParsePathExpression(expr); Stack<string> path;
if (! ParsePathExpression(expr,out path))
return false;
if (path.Count == 0) if (path.Count == 0)
{ {
ValueStore = ovalue; ValueStore = ovalue;
@ -399,34 +425,36 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
/// use a stack because we process the path in inverse order later /// use a stack because we process the path in inverse order later
/// </summary> /// </summary>
// ----------------------------------------------------------------- // -----------------------------------------------------------------
protected static Stack<string> ParsePathExpression(string path) protected static bool ParsePathExpression(string expr, out Stack<string> path)
{ {
Stack<string> m_path = new Stack<string>(); path = new Stack<string>();
// add front and rear separators // add front and rear separators
path = "." + path + "."; expr = "." + expr + ".";
// add separators for quoted paths // add separators for quoted exprs
path = m_ParsePassOne.Replace(path,".$0.",-1,0); expr = m_ParsePassOne.Replace(expr,".$0.",-1,0);
// add separators for array references // add separators for array references
path = m_ParsePassTwo.Replace(path,".$0.",-1,0); expr = m_ParsePassTwo.Replace(expr,".$0.",-1,0);
// add quotes to bare identifier // add quotes to bare identifier
path = m_ParsePassThree.Replace(path,".{$1}",-1,0); expr = m_ParsePassThree.Replace(expr,".{$1}",-1,0);
// remove extra separators // remove extra separators
path = m_ParsePassFour.Replace(path,".",-1,0); expr = m_ParsePassFour.Replace(expr,".",-1,0);
// validate the results (catches extra quote characters for example) // validate the results (catches extra quote characters for example)
if (m_ValidatePath.IsMatch(path)) if (m_ValidatePath.IsMatch(expr))
{ {
MatchCollection matches = m_PathComponent.Matches(path,0); MatchCollection matches = m_PathComponent.Matches(expr,0);
foreach (Match match in matches) foreach (Match match in matches)
m_path.Push(match.Groups[1].Value); path.Push(match.Groups[1].Value);
return true;
} }
return m_path; return false;
} }
// ----------------------------------------------------------------- // -----------------------------------------------------------------

View File

@ -301,7 +301,16 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
[ScriptInvocation] [ScriptInvocation]
public string JsonList2Path(UUID hostID, UUID scriptID, object[] pathlist) public string JsonList2Path(UUID hostID, UUID scriptID, object[] pathlist)
{ {
return JsonStore.CanonicalPathExpression(ConvertList2Path(pathlist)); string ipath = ConvertList2Path(pathlist);
string opath;
if (JsonStore.CanonicalPathExpression(ipath,out opath))
return opath;
// This won't parse if passed to the other routines as opposed to
// returning an empty string which is a valid path and would overwrite
// the entire store
return "**INVALID**";
} }
// ----------------------------------------------------------------- // -----------------------------------------------------------------
@ -421,6 +430,7 @@ namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
// ----------------------------------------------------------------- // -----------------------------------------------------------------
protected void GenerateRuntimeError(string msg) protected void GenerateRuntimeError(string msg)
{ {
m_log.InfoFormat("[JsonStore] runtime error: {0}",msg);
throw new Exception("JsonStore Runtime Error: " + msg); throw new Exception("JsonStore Runtime Error: " + msg);
} }