private static string ProcessRequestPath(string requestPath)
        {
            var path       = requestPath.Replace("+", " ");
            var decodePath = JDCloudSignV3Util.UnescapeDataStringRfc3986(path);
            var pathArray  = decodePath.Split('/');

#if !NET30 && !NET20
            var pathList = pathArray.Select(p => JDCloudSignV3Util.EscapeUriDataStringRfc3986(p)).ToList();
            pathList.RemoveAll(p => p.Equals(string.Empty));
            path = string.Join("/", pathList.ToArray());
#else
            var pathList = new List <string>();
            foreach (var item in pathArray)
            {
                if (item != null && item != string.Empty)
                {
                    pathList.Add(JDCloudSignV3Util.EscapeUriDataStringRfc3986(item));
                }
            }
            if (pathList.Count > 0)
            {
                string[] pathArrays = new string[pathList.Count];
                int      i          = 0;
                foreach (var item in pathList)
                {
                    pathArrays[i] = item;
                    i            += 1;
                }
                path = string.Join("/", pathArrays);
            }
#endif
            pathList.RemoveAll(p => p.Equals(string.Empty));

            path = string.Join("/", pathList.ToArray());
            if (!path.StartsWith("/"))
            {
                path = $"/{path}";
            }
            if (requestPath.TrimEnd().EndsWith("/") || requestPath.TrimEnd().EndsWith("%2F"))
            {
                path = $"{path}/";
            }
            return(path);
        }
Exemple #2
0
        public void URITest()
        {
            // /v1/regions/cn-north-1/instances/%20/%60!@
            var path = "/v1/regions/cn-north-1/instances/ /`!@#$%^&*()=+/0123456789/[]\\;',<>?:\"{}|///abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/-_.~:GET";
            var host = @"http://apigw-internal-dev.cn-north-1.jcloudcs.com:8000";

            if (!path.StartsWith("/", StringComparison.CurrentCulture))
            {
                path = "/" + path;
            }

            path = path.Replace("+", " ");
            var pathArray       = path.Split('/');
            var pathDecodeArray = pathArray.Select(p => JDCloudSignV3Util.UnescapeDataStringRfc3986(p)).ToList();
            var pathList        = pathDecodeArray.Select(p => JDCloudSignV3Util.EscapeUriDataStringRfc3986(p)).ToList();

            pathList.RemoveAll(p => p.Equals(string.Empty));
            path = string.Join("/", pathList.ToArray());
            if (!path.StartsWith("/"))
            {
                path = $"/{path}";
            }
            _output.WriteLine(path);
        }
        /// <summary>
        /// process requst url query string value
        /// </summary>
        /// <param name="queryString">request query string </param>
        /// <returns></returns>
        public static string ProcessQueryString(string queryString)
        {
            if (queryString.IsNullOrWhiteSpace())
            {
                return(string.Empty);
            }
            if (queryString.StartsWith("?"))
            {
                queryString = queryString.Remove(0, 1);
            }

            var queryStringArray             = queryString.Split('&');
            List <QueryParam> queryParamList = new List <QueryParam>();

            if (queryStringArray != null && queryStringArray.Length > 0)
            {
                foreach (var item in queryStringArray)
                {
                    if (!item.IsNullOrWhiteSpace())
                    {
                        var paramKV = item.Split('=');
                        if (paramKV != null && paramKV.Length > 0)
                        {
                            if (paramKV.Length == 1 && !paramKV[0].IsNullOrWhiteSpace())
                            {
                                // queryString = queryString.Replace("+", " ");
                                string keyValue = paramKV[0].Replace("+", " ");
                                keyValue = JDCloudSignV3Util.UnescapeDataStringRfc3986(keyValue);
                                QueryParam queryParam = new QueryParam {
                                    Key = JDCloudSignV3Util.EscapeUriDataStringRfc3986(keyValue), Value = string.Empty
                                };
                                queryParamList.Add(queryParam);
                            }
                            else if (paramKV.Length == 2 && !paramKV[0].IsNullOrWhiteSpace())
                            {
                                string keyValue = paramKV[0].Replace("+", " ");
                                keyValue = JDCloudSignV3Util.UnescapeDataStringRfc3986(keyValue);
                                string value = paramKV[1].Replace("+", " ");
                                value = JDCloudSignV3Util.UnescapeDataStringRfc3986(value);
                                QueryParam queryParam = new QueryParam {
                                    Key   = JDCloudSignV3Util.EscapeUriDataStringRfc3986(keyValue),
                                    Value = JDCloudSignV3Util.EscapeUriDataStringRfc3986(value)
                                };
                                queryParamList.Add(queryParam);
                            }
                            else
                            {
                                if (paramKV[0] != null && paramKV[0] != string.Empty)
                                {
                                    string key = JDCloudSignV3Util.UnescapeDataStringRfc3986(paramKV[0]);

#if !NET20 && !NET30
                                    string queryParamValue = string.Join("=", paramKV.Skip(1).Take(paramKV.Length - 1).ToArray());
#else
                                    string[] queryParamVlueArray = new string[paramKV.Length - 1];
                                    for (int i = 1; i < paramKV.Length; i++)
                                    {
                                        queryParamVlueArray[i - 1] = paramKV[i];
                                    }
                                    string queryParamValue = string.Join("=", queryParamVlueArray);
#endif
                                    string     value      = JDCloudSignV3Util.UnescapeDataStringRfc3986(queryParamValue);
                                    QueryParam queryParam = new QueryParam
                                    {
                                        Key   = JDCloudSignV3Util.EscapeUriDataStringRfc3986(key),
                                        Value = JDCloudSignV3Util.EscapeUriDataStringRfc3986(value)
                                    };
                                    queryParamList.Add(queryParam);
                                }
                            }
                        }
                    }
                }
            }

            if (queryParamList.Count > 0)
            {
#if !NET20 && !NET30
                queryParamList = queryParamList.OrderBy(p => p.Key).ThenBy(p => p.Value).ToList();
#else
                queryParamList.Sort();
#endif
                StringBuilder stringBuilder = new StringBuilder();
                foreach (var item in queryParamList)
                {
                    stringBuilder.Append(item.Key).Append("=");
                    if (item.Value.IsNullOrWhiteSpace())
                    {
                        stringBuilder.Append(string.Empty).Append("&");
                    }
                    else
                    {
                        stringBuilder.Append(item.Value).Append("&");
                    }
                }
                var queryStringEncode = stringBuilder.ToString();
                if (!queryStringEncode.IsNullOrWhiteSpace())
                {
                    if (queryStringEncode.EndsWith("&"))
                    {
                        queryStringEncode = queryStringEncode.TrimEnd('&');
                    }
                }
                else
                {
                    queryStringEncode = string.Empty;
                }

                return(queryStringEncode);
            }
            return(string.Empty);
        }