private static byte[] ProcessPostData(IDictionary <string, string> postData) { if (postData == null || postData.Count == 0) { return(null); } var sb = new StringBuilder(); foreach (var pair in postData) { if (sb.Length > 0) { sb.Append("&"); } if (String.IsNullOrEmpty(pair.Value)) { continue; } sb.AppendFormat("{0}={1}", pair.Key, UriQueryUtility.UrlEncode(pair.Value)); } return(Encoding.UTF8.GetBytes(sb.ToString())); }
private static bool AppendNameValuePair(StringBuilder builder, bool first, bool urlEncode, string name, string value) { string effectiveName = name ?? String.Empty; string encodedName = urlEncode ? UriQueryUtility.UrlEncode(effectiveName) : effectiveName; string effectiveValue = value ?? String.Empty; string encodedValue = urlEncode ? UriQueryUtility.UrlEncode(effectiveValue) : effectiveValue; if (first) { first = false; } else { builder.Append("&"); } builder.Append(encodedName); if (!String.IsNullOrEmpty(encodedValue)) { builder.Append("="); builder.Append(encodedValue); } return(first); }
public static NameValueCollection ParseDelimited(string s) { var nvc = new NameValueCollection(); if (s == null) { return(nvc); } foreach (var pair in s.Split('&')) { var kvp = pair.Split(new[] { "=" }, StringSplitOptions.RemoveEmptyEntries); if (kvp.Length == 0) { continue; } string key = kvp[0].Trim(); if (String.IsNullOrEmpty(key)) { continue; } string value = kvp.Length > 1 ? kvp[1].Trim() : null; nvc[key] = UriQueryUtility.UrlDecode(value); } return(nvc); }
/// <summary> /// Copies current name-only to the provided collection instance. /// </summary> /// <param name="nameValuePairs">The collection to copy into.</param> public void CopyNameOnlyTo(ICollection <KeyValuePair <string, string> > nameValuePairs) { string unescapedName = UriQueryUtility.UrlDecode(_name.ToString()); string value = String.Empty; nameValuePairs.Add(new KeyValuePair <string, string>(unescapedName, value)); Clear(); }
private static void BuildParams(string prefix, JToken jsonValue, List <string> results) { if (jsonValue is JValue) { JValue jsonPrimitive = jsonValue as JValue; if (jsonPrimitive != null) { if (jsonPrimitive.Type == JTokenType.String && String.IsNullOrEmpty(jsonPrimitive.Value.ToString())) { results.Add(prefix + "=" + String.Empty); } else { if (jsonPrimitive.Value is DateTime || jsonPrimitive.Value is DateTimeOffset) { string dateStr = jsonPrimitive.ToString(); if (!String.IsNullOrEmpty(dateStr) && dateStr.StartsWith("\"")) { dateStr = dateStr.Substring(1, dateStr.Length - 2); } results.Add(prefix + "=" + UriQueryUtility.UrlEncode(dateStr)); } else { results.Add(prefix + "=" + UriQueryUtility.UrlEncode(jsonPrimitive.Value.ToString())); } } } else { results.Add(prefix + "=" + String.Empty); } } else if (jsonValue is JArray) { for (int i = 0; i < ((JArray)jsonValue).Count; i++) { if (jsonValue[i] is JArray || jsonValue[i] is JObject) { BuildParams(prefix + "[" + i + "]", jsonValue[i], results); } else { BuildParams(prefix + "[]", jsonValue[i], results); } } } else { //jsonValue is JObject foreach (KeyValuePair <string, JToken> item in (JObject)jsonValue) { BuildParams(prefix + "[" + item.Key + "]", item.Value, results); } } }
/// <summary> /// Copies current name value pair field to the provided collection instance. /// </summary> /// <param name="nameValuePairs">The collection to copy into.</param> public void CopyTo(ICollection <KeyValuePair <string, string> > nameValuePairs) { string unescapedName = UriQueryUtility.UrlDecode(this._name.ToString()); string escapedValue = this._value.ToString(); string value = UriQueryUtility.UrlDecode(escapedValue); nameValuePairs.Add(new KeyValuePair <string, string>(unescapedName, value)); this.Clear(); }
public void UrlEncodeToBytesThrowsOnInvalidArgs() { Assert.Null(UriQueryUtility.UrlEncodeToBytes(null, 0, 0)); Assert.ThrowsArgumentNull(() => UriQueryUtility.UrlEncodeToBytes(null, 0, 2), "bytes"); Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlEncodeToBytes(new byte[0], -1, 0), "offset", null); Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlEncodeToBytes(new byte[0], 2, 0), "offset", null); Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlEncodeToBytes(new byte[0], 0, -1), "count", null); Assert.ThrowsArgumentOutOfRange(() => UriQueryUtility.UrlEncodeToBytes(new byte[0], 0, 2), "count", null); }
public virtual IHttpRouteData GetRouteData(string virtualPathRoot, HttpRequestMessage request) { if (virtualPathRoot == null) { throw Error.ArgumentNull("virtualPathRoot"); } if (request == null) { throw Error.ArgumentNull("request"); } // Note: we don't validate host/port as this is expected to be done at the host level string requestPath = request.RequestUri.AbsolutePath; if (!requestPath.StartsWith(virtualPathRoot, StringComparison.OrdinalIgnoreCase)) { return(null); } string relativeRequestPath = null; int virtualPathLength = virtualPathRoot.Length; if (requestPath.Length > virtualPathLength && requestPath[virtualPathLength] == '/') { relativeRequestPath = requestPath.Substring(virtualPathLength + 1); } else { relativeRequestPath = requestPath.Substring(virtualPathLength); } string decodedRelativeRequestPath = UriQueryUtility.UrlDecode(relativeRequestPath); HttpRouteValueDictionary values = _parsedRoute.Match(decodedRelativeRequestPath, _defaults); if (values == null) { // If we got back a null value set, that means the URI did not match return(null); } // Validate the values if (!ProcessConstraints(request, values, HttpRouteDirection.UriResolution)) { return(null); } return(new HttpRouteData(this, values)); }
internal static ServiceQuery GetServiceQuery(Uri uri) { if (uri == null) { throw Error.ArgumentNull("uri"); } NameValueCollection queryPartCollection = UriQueryUtility.ParseQueryString(uri.Query); List <ServiceQueryPart> serviceQueryParts = new List <ServiceQueryPart>(); foreach (string queryPart in queryPartCollection) { if (queryPart == null || !queryPart.StartsWith("$", StringComparison.Ordinal)) { // not a special query string continue; } foreach (string value in queryPartCollection.GetValues(queryPart)) { string queryOperator = queryPart.Substring(1); if (!ServiceQuery.IsSupportedQueryOperator(queryOperator)) { // skip any operators we don't support continue; } ServiceQueryPart serviceQueryPart = new ServiceQueryPart(queryOperator, value); serviceQueryParts.Add(serviceQueryPart); } } // Query parts for OData need to be ordered $filter, $orderby, $skip, $top. For this // set of query operators, they are already in alphabetical order, so it suffices to // order by operator name. In the future if we support other operators, this may need // to be reexamined. serviceQueryParts = serviceQueryParts.OrderBy(p => p.QueryOperator).ToList(); ServiceQuery serviceQuery = new ServiceQuery() { QueryParts = serviceQueryParts, }; return(serviceQuery); }
private static string FormUrlEncoding(JToken jsonValue) { List <string> results = new List <string>(); if (jsonValue is JValue) { return(UriQueryUtility.UrlEncode(((JValue)jsonValue).Value.ToString())); } BuildParams("JV", jsonValue, results); StringBuilder strResult = new StringBuilder(); foreach (var result in results) { strResult.Append("&" + result); } if (strResult.Length > 0) { return(strResult.Remove(0, 1).ToString()); } return(strResult.ToString()); }
public static bool Compare(JToken initValue, JToken newValue) { if (initValue == null && newValue == null) { return(true); } if (initValue == null || newValue == null) { return(false); } if (initValue is JValue) { string initStr; if (initValue.Type == JTokenType.String) { initStr = initValue.ToString(); } else { initStr = ((JValue)initValue).Value.ToString(); } string newStr; if (newValue is JValue) { newStr = newValue.ToString(); initStr = UriQueryUtility.UrlDecode(UriQueryUtility.UrlEncode(initStr)); return(initStr.Equals(newStr)); } else if (newValue is JObject && ((JObject)newValue).Count == 1) { initStr = String.Format("{0}", initValue.ToString()); return(((IDictionary <string, JToken>)newValue).ContainsKey(initStr)); } return(false); } if (((JContainer)initValue).Count != ((JContainer)newValue).Count) { return(false); } if (initValue is JObject && newValue is JObject) { foreach (KeyValuePair <string, JToken> item in (JObject)initValue) { if (!Compare(item.Value, newValue[item.Key])) { return(false); } } return(true); } if (initValue is JArray && newValue is JArray) { for (int i = 0; i < ((JArray)initValue).Count; i++) { if (!Compare(initValue[i], newValue[i])) { return(false); } } return(true); } return(false); }
public void UrlDecode_ReturnsNull() { Assert.Null(UriQueryUtility.UrlDecode(null)); }
private string ToString(bool urlencoded) { int n = Count; if (n == 0) { return(String.Empty); } StringBuilder s = new StringBuilder(); string key, keyPrefix, item; for (int i = 0; i < n; i++) { key = GetKey(i); if (urlencoded) { key = UriQueryUtility.UrlEncode(key); } keyPrefix = (!String.IsNullOrEmpty(key)) ? (key + "=") : String.Empty; ArrayList values = (ArrayList)BaseGet(i); int numValues = (values != null) ? values.Count : 0; if (s.Length > 0) { s.Append('&'); } if (numValues == 1) { s.Append(keyPrefix); item = (string)values[0]; if (urlencoded) { item = UriQueryUtility.UrlEncode(item); } s.Append(item); } else if (numValues == 0) { s.Append(keyPrefix); } else { for (int j = 0; j < numValues; j++) { if (j > 0) { s.Append('&'); } s.Append(keyPrefix); item = (string)values[j]; if (urlencoded) { item = UriQueryUtility.UrlEncode(item); } s.Append(item); } } } return(s.ToString()); }