/** * Parse the parameters from an OAuth Authorization or WWW-Authenticate * header. The realm is included as a parameter. If the given header doesn't * start with "OAuth ", return an empty list. */ public static List <OAuth.Parameter> decodeAuthorization(String authorization) { List <OAuth.Parameter> into = new List <OAuth.Parameter>(); if (authorization != null) { Match m = AUTHORIZATION.Match(authorization); if (m.Success) { if (m.Groups[1].Value.Equals(AUTH_SCHEME)) { foreach (String nvp in Regex.Split(m.Groups[2].Value, "\\s*,\\s*")) { m = NVP.Match(nvp); if (m.Success) { String name = Rfc3986.Decode(m.Groups[1].Value); String value = Rfc3986.Decode(m.Groups[2].Value); into.Add(new OAuth.Parameter(name, value)); } } } } } return(into); }
/// <summary> /// Splits a ampersand-separated list of key-value pairs, decodes the keys and /// values, and adds them to a NameValueCollection. Keys and values are separated /// by equals signs. /// </summary> /// <param name="input">The key-value pair list</param> /// <returns>A name value collection, which may be empty.</returns> /// <exception cref="System.FormatException"> /// If the string is not a series of key-value pairs separated by ampersands, /// or if one of the keys is null or empty, or if one of the keys or values is /// not properly encoded. /// </exception> public static NameValueCollection SplitAndDecode(string input) { NameValueCollection parameters = new NameValueCollection(); if (string.IsNullOrEmpty(input)) { return(parameters); } foreach (string pair in input.Split('&')) { string[] parts = pair.Split('='); if (parts.Length != 2) { throw new FormatException("Pair is not a key-value pair"); } string key = Rfc3986.Decode(parts[0]); if (string.IsNullOrEmpty(key)) { throw new FormatException("Key cannot be null or empty"); } string value = Rfc3986.Decode(parts[1]); parameters.Add(key, value); } return(parameters); }
/// <summary> /// Join the name-value pairs into a string seperated with ampersands. /// Each name and value is first RFC 3986 encoded and values are separated /// from names with equal signs. /// </summary> /// <param name="values">The name value collection to encode and join</param> /// <returns>An RFC 3986 compliant string</returns> public static string EncodeAndJoin(NameValueCollection values) { if (values == null) { return(string.Empty); } StringBuilder enc = new StringBuilder(); bool first = true; foreach (string key in values.Keys) { string encKey = Rfc3986.Encode(key); foreach (string value in values.GetValues(key)) { if (!first) { enc.Append("&"); } else { first = false; } enc.Append(encKey).Append("=").Append(Rfc3986.Encode(value)); } } return(enc.ToString()); }
/** Parse a form-urlencoded document. */ public static List <Parameter> decodeForm(String form) { List <Parameter> list = new List <Parameter>(); if (!isEmpty(form)) { foreach (String nvp in form.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries)) { int equals = nvp.IndexOf('='); String name; String value; if (equals < 0) { name = Rfc3986.Decode(nvp); value = null; } else { name = Rfc3986.Decode(nvp.Substring(0, equals)); value = Rfc3986.Decode(nvp.Substring(equals + 1)); } list.Add(new Parameter(name, value)); } } return(list); }
/** Construct a &-separated list of the given values, percentEncoded. */ public static String percentEncode(List <string> values) { StringBuilder p = new StringBuilder(); foreach (string v in values) { if (p.Length > 0) { p.Append("&"); } p.Append(Rfc3986.Encode(ToString(v))); } return(p.ToString()); }
/** * Construct a WWW-Authenticate or Authentication header value, containing * the given realm plus all the parameters whose names begin with "oauth_". */ public String getAuthorizationHeader(String realm) { StringBuilder into = new StringBuilder(AUTH_SCHEME); into.Append(" realm=\"").Append(Rfc3986.Encode(realm)).Append('"'); beforeGetParameter(); if (parameters != null) { foreach (var parameter in parameters) { String name = ToString(parameter.Key); if (name.StartsWith("oauth_")) { into.Append(", "); into.Append(Rfc3986.Encode(name)).Append("=\"") .Append( Rfc3986.Encode(ToString(parameter .Value))).Append('"'); } } } return(into.ToString()); }
/** * Write a form-urlencoded document into the given stream, containing the * given sequence of name/value pairs. */ public static void formEncode(List <Parameter> parameters, Stream into) { if (parameters != null) { bool first = true; byte[] towrite; foreach (Parameter parameter in parameters) { if (first) { first = false; } else { towrite = Encoding.UTF8.GetBytes("&"); into.Write(towrite, 0, towrite.Length); } towrite = Encoding.UTF8.GetBytes(Rfc3986.Encode(ToString(parameter.Key)) + "=" + Rfc3986.Encode(ToString(parameter.Value))); into.Write(towrite, 0, towrite.Length); } } }
public override String ToString() { return(Rfc3986.Encode(Key) + '=' + Rfc3986.Encode(Value)); }