Beispiel #1
0
        /// <summary>
        /// Sorts a <see cref="WebParameterCollection"/> by name, and then value if equal.
        /// </summary>
        /// <param name="parameters">A collection of parameters to sort</param>
        /// <returns>A sorted parameter collection</returns>
        public static WebParameterCollection SortParametersExcludingSignature(WebParameterCollection parameters)
        {
            var copy       = new WebParameterCollection(parameters.Select(x => new WebParameter(x.Name, x.Value, x.Type)));
            var exclusions = copy.Where(n => string.Equals(n.Name, "oauth_signature", StringComparison.OrdinalIgnoreCase));

            copy.RemoveAll(exclusions);
            copy.ForEach(p =>
            {
                p.Name = UrlEncodeStrict(p.Name);
                if (p.Type == WebParameterType.Query)
                {
                    // Parameter provided by the user
                    p.Value = UrlEscapeUtility.Escape(p.Value, _encoding, UrlEscapeFlags.AllowLikeWebRequest);
                }
                else
                {
                    // Authorization or POST parameter
                    p.Value = UrlEncodeStrict(p.Value);
                }
            });
            copy.Sort(
                (x, y) =>
                string.CompareOrdinal(x.Name, y.Name) != 0
            ? string.CompareOrdinal(x.Name, y.Name)
            : string.CompareOrdinal(x.Value, y.Value));
            return(copy);
        }
Beispiel #2
0
        public void TestEscapeDataStringComplianceUmlaut()
        {
            const UrlEscapeFlags flags = UrlEscapeFlags.BuilderVariantListByteArray;
            const string         chars = "äöüßÄÖÜ\u007F";
            var expected = Uri.EscapeDataString(chars);
            var test     = _utility.Escape(chars, flags);

            Assert.Equal(expected, test);
            Assert.Equal(expected.Length, UrlEscapeUtility.ComputeLength(chars, flags));
        }
Beispiel #3
0
        public void TestEscapeDataStringComplianceASCII()
        {
            const UrlEscapeFlags flags = UrlEscapeFlags.BuilderVariantListByteArray;
            var chars    = new string(Enumerable.Range(32, 95).Select(x => (char)x).ToArray());
            var expected = Uri.EscapeDataString(chars);
            var test     = _utility.Escape(chars, flags);

            Assert.Equal(expected, test);
            Assert.Equal(expected.Length, UrlEscapeUtility.ComputeLength(chars, flags));
        }
Beispiel #4
0
        public void TestUrlEncodeComplianceUmlaut()
        {
            const UrlEscapeFlags flags = UrlEscapeFlags.LikeUrlEncode | UrlEscapeFlags.BuilderVariantListByteArray;
            const string         chars = "äöüßÄÖÜ\u007F";
            var expected = HttpUtility.UrlEncode(chars);

            Assert.NotNull(expected);
            var test = _utility.Escape(chars, flags);

            Assert.Equal(expected, test);
            Assert.Equal(expected.Length, UrlEscapeUtility.ComputeLength(chars, flags));
        }
Beispiel #5
0
        public void TestUrlEncodeComplianceASCII()
        {
            const UrlEscapeFlags flags = UrlEscapeFlags.LikeUrlEncode | UrlEscapeFlags.BuilderVariantListByteArray;
            var chars    = new string(Enumerable.Range(32, 95).Select(x => (char)x).ToArray());
            var expected = HttpUtility.UrlEncode(chars);

            Assert.NotNull(expected);
            var test = UrlEscapeUtility.Escape(chars, flags);

            Assert.Equal(expected, test);
            Assert.Equal(expected.Length, UrlEscapeUtility.ComputeLength(chars, flags));
        }
Beispiel #6
0
 /// <summary>
 /// URL encodes a string based on section 5.1 of the OAuth spec.
 /// Namely, percent encoding with [RFC3986], avoiding unreserved characters,
 /// upper-casing hexadecimal characters, and UTF-8 encoding for text value pairs.
 /// </summary>
 /// <param name="value"></param>
 /// <a href="http://oauth.net/core/1.0#encoding_parameters" />
 public static string UrlEncodeStrict(string value)
 {
     // [JD]: We need to escape the apostrophe as well or the signature will fail
     return(UrlEscapeUtility.Escape(value, _encoding, UrlEscapeFlags.Default));
 }