Beispiel #1
0
        /// <summary>
        /// Resolve the current CRI relative to a provided CRI value
        /// </summary>
        /// <param name="baseRef">CRI to resolve relative to</param>
        /// <returns></returns>
        public Cori ResolveTo(Cori baseRef)
        {
            CBORObject o = Resolve(baseRef.Data, Data);

            if (o == null)
            {
                throw new CoAPException("Unable to resolve the two URIs.");
            }
            return(new Cori(o));
        }
Beispiel #2
0
        /// <inheritdoc />
        public override bool Equals(object obj)
        {
            if (!(obj is Cori))
            {
                return(false);
            }

            Cori right = (Cori)obj;

            if (this == right)
            {
                return(true);
            }

            if (right == null)
            {
                return(false);
            }

            if (!IsAbsolute() || !right.IsAbsolute())
            {
                throw new ArgumentException("Can only compare absolute CoRI values");
            }

            if (Data.Count != right.Data.Count)
            {
                return(false);
            }

            for (int i = 0; i < Data.Count; i++)
            {
                if (!Data[i].Equals(right.Data[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #3
0
        public Cori MakeRelative(Cori baseHref)
        {
            if (!baseHref.IsAbsolute() || !IsAbsolute())
            {
                throw new ArgumentException("Must be absolute URIs");
            }

            if (this.Equals(baseHref))
            {
                return(new Cori(CBORObject.NewArray()));
            }

            List <CBORObject> baseUri = new List <CBORObject>(baseHref.Data.Values);
            List <CBORObject> href    = new List <CBORObject>(Data.Values);

            //  Strip leading matches

            int lastOption = OptionBegin;

            while (baseUri.Count >= 2 && href.Count >= 2 && baseUri[0].Equals(href[0]) && baseUri[1].Equals(href[1]))
            {
                lastOption = baseUri[0].AsInt32();
                baseUri.RemoveRange(0, 2);
                href.RemoveRange(0, 2);
            }

            bool removedTail = false;

            //  Strip trailing matches
            if (baseUri.Count > 0)
            {
                int opt = OptionEnd;
                if (lastOption >= OptionPort)
                {
                    opt = lastOption;
                }

                if (opt > OptionPath && href.Count > 0)
                {
                    int t = href[href.Count - 2].AsInt32();
                    if (t < opt && t >= OptionPath)
                    {
                        opt = t;
                    }
                }

                if (opt > OptionPath && href.Count > 2)
                {
                    int t = href[href.Count - 4].AsInt32();
                    if (t < opt && t >= OptionPath)
                    {
                        opt = t;
                    }
                }

                if (opt <= OptionPath)
                {
                    opt = OptionQuery;
                }

                while (baseUri.Count > 0 && baseUri[baseUri.Count - 2].AsInt32() >= opt)
                {
                    baseUri.RemoveRange(baseUri.Count - 2, 2);
                    removedTail = true;
                }
            }

            if (removedTail && lastOption == OptionPort)
            {
                if (baseUri.Count == 0)
                {
                    href.Insert(0, CBORObject.FromObject(PathTypeAbsolutePath));
                    href.Insert(0, CBORObject.FromObject(OptionPathType));
                }
                else if (baseUri.Count == 2)
                {
                    href.Insert(0, CBORObject.FromObject(PathTypeRelativePath));
                    href.Insert(0, CBORObject.FromObject(OptionPathType));
                }
                else
                {
                    href.Insert(0, CBORObject.FromObject(PathTypeRelativePath1Up + baseUri.Count - 1));
                    href.Insert(0, CBORObject.FromObject(OptionPathType));
                }
                return(new Cori(ArrayToCbor(href)));
            }


            if (removedTail && lastOption == OptionPath)
            {
                if (baseUri.Count == 0)
                {
                    href.Insert(0, CBORObject.FromObject(PathTypeAppendPath));
                    href.Insert(0, CBORObject.FromObject(OptionPathType));
                }
                else if (baseUri.Count == 2)
                {
                    href.Insert(0, CBORObject.FromObject(PathTypeRelativePath));
                    href.Insert(0, CBORObject.FromObject(OptionPathType));
                }
                else
                {
                    href.Insert(0, CBORObject.FromObject(PathTypeRelativePath1Up + baseUri.Count - 1));
                    href.Insert(0, CBORObject.FromObject(OptionPathType));
                }
                return(new Cori(ArrayToCbor(href)));
            }

            if (baseUri.Count == 0 && href.Count == 0)
            {
                // Not sure how we got here as this should have been dealt with already.
                return(new Cori(CBORObject.NewArray()));
            }

            if (baseUri.Count == 0 || baseUri[0].AsInt32() <= OptionPort)
            {
                //  Ran the entire base set - what is left is the relative URI
                //  If we did not get down to the port option, then what is left is the relative URI
                if (lastOption == OptionPath)
                {
                    href.Insert(0, CBORObject.FromObject(PathTypeAppendPath));
                    href.Insert(0, CBORObject.FromObject(OptionPathType));
                }
                return(new Cori(ArrayToCbor(href)));
            }

            if (href.Count == 0)
            {
                if (lastOption == OptionPort)
                {
                    href.Add(CBORObject.FromObject(OptionPathType));
                    href.Add(CBORObject.FromObject(PathTypeAbsolutePath));
                    return(new Cori(ArrayToCbor(href)));
                }
                else if (baseUri.Count == 2)
                {
                    href.Add(CBORObject.FromObject(OptionPathType));
                    href.Add(CBORObject.FromObject(PathTypeRelativePath));
                    return(new Cori(ArrayToCbor(href)));
                }
                else
                {
                    href.Add(CBORObject.FromObject(OptionPathType));
                    href.Add(CBORObject.FromObject(PathTypeRelativePath + baseUri.Count / 2 - 1));
                    return(new Cori(ArrayToCbor(href)));
                }
            }

            if (baseUri.Count > 0 && lastOption >= OptionPath)
            {
                href.Insert(0, CBORObject.FromObject(PathTypeRelativePath + baseUri.Count / 2 - 1));
                href.Insert(0, CBORObject.FromObject(OptionPathType));
                return(new Cori(ArrayToCbor(href)));
            }

            if (lastOption == OptionPort)
            {
                href.Insert(0, CBORObject.FromObject(PathTypeAbsolutePath));
                href.Insert(0, CBORObject.FromObject(OptionPathType));
                return(new Cori(ArrayToCbor(href)));
            }

            return(new Cori(ArrayToCbor(href)));
        }