예제 #1
0
        public static void ParseObsWebServiceResponse(HttpResponse httpResponse, ObsWebServiceResponse response, IHeaders iheaders)
        {
            response.StatusCode = httpResponse.StatusCode;
            if (httpResponse.Headers.ContainsKey(iheaders.RequestIdHeader()))
            {
                response.RequestId = httpResponse.Headers[iheaders.RequestIdHeader()];
            }
            if (httpResponse.Headers.ContainsKey(Constants.CommonHeaders.ContentLength))
            {
                response.ContentLength = Convert.ToInt64(httpResponse.Headers[Constants.CommonHeaders.ContentLength]);
            }

            foreach (KeyValuePair <string, string> header in httpResponse.Headers)
            {
                string key = header.Key;
                if (key.StartsWith(iheaders.HeaderMetaPrefix()))
                {
                    key = key.Substring(iheaders.HeaderMetaPrefix().Length);
                }
                else if (key.StartsWith(iheaders.HeaderPrefix()))
                {
                    key = key.Substring(iheaders.HeaderPrefix().Length);
                }
                else if (key.StartsWith(Constants.ObsHeaderMetaPrefix))
                {
                    key = key.Substring(Constants.ObsHeaderMetaPrefix.Length);
                }
                else if (key.StartsWith(Constants.ObsHeaderPrefix))
                {
                    key = key.Substring(Constants.ObsHeaderPrefix.Length);
                }
                response.Headers.Add(key, header.Value);
            }
        }
예제 #2
0
        internal void PrepareRequestAndContext(HttpRequest request, HttpContext context)
        {
            IHeaders iheaders = this.GetIHeaders(context);

            CommonUtil.RenameHeaders(request, iheaders.HeaderPrefix(), iheaders.HeaderMetaPrefix());

            if (LoggerMgr.IsDebugEnabled)
            {
                LoggerMgr.Debug(string.Format("Perform {0} request for {1}", request.Method, request.GetUrl()));
                LoggerMgr.Debug("Perform http request with headers:" + CommonUtil.ConvertHeadersToString(request.Headers));
            }
        }
예제 #3
0
        /// <summary>
        /// Generate parameters for a temporary authentication request.
        /// </summary>
        /// <param name="request">Request parameters</param>
        /// <returns>Response</returns>
        public CreateTemporarySignatureResponse CreateTemporarySignature(CreateTemporarySignatureRequest request)
        {
            HttpRequest httpRequest = new HttpRequest();

            httpRequest.PathStyle  = this.ObsConfig.PathStyle;
            httpRequest.BucketName = request.BucketName;
            httpRequest.ObjectKey  = request.ObjectKey;
            httpRequest.Method     = request.Method;

            IHeaders iheaders = this.httpClient.GetIHeaders(new HttpContext(this.sp, this.ObsConfig));

            if (!string.IsNullOrEmpty(this.sp.Token) && !request.Parameters.ContainsKey(iheaders.SecurityTokenHeader()))
            {
                request.Parameters.Add(iheaders.SecurityTokenHeader(), this.sp.Token.Trim());
            }

            foreach (KeyValuePair <string, string> entry in request.Headers)
            {
                CommonUtil.AddHeader(httpRequest, entry.Key, entry.Value);
            }

            foreach (KeyValuePair <string, string> entry in request.Parameters)
            {
                CommonUtil.AddParam(httpRequest, entry.Key, entry.Value);
            }

            if (request.SubResource.HasValue)
            {
                SubResourceEnum value = request.SubResource.Value;
                if (value == SubResourceEnum.StoragePolicy && this.ObsConfig.AuthType == AuthTypeEnum.OBS)
                {
                    value = SubResourceEnum.StorageClass;
                }
                else if (value == SubResourceEnum.StorageClass && this.ObsConfig.AuthType != AuthTypeEnum.OBS)
                {
                    value = SubResourceEnum.StoragePolicy;
                }

                CommonUtil.AddParam(httpRequest, EnumAdaptor.GetStringValue(value), null);
            }

            foreach (KeyValuePair <string, string> entry in request.Metadata.KeyValuePairs)
            {
                if (string.IsNullOrEmpty(entry.Key))
                {
                    continue;
                }
                string _key = entry.Key;
                if (!entry.Key.StartsWith(iheaders.HeaderMetaPrefix(), StringComparison.OrdinalIgnoreCase) && !entry.Key.StartsWith(Constants.ObsHeaderMetaPrefix, StringComparison.OrdinalIgnoreCase))
                {
                    _key = iheaders.HeaderMetaPrefix() + _key;
                }
                CommonUtil.AddHeader(httpRequest, _key, entry.Value);
            }

            long expires = 300;

            if (request.Expires.HasValue && request.Expires.Value > 0)
            {
                expires = request.Expires.Value;
            }

            return(this.ObsConfig.AuthType == AuthTypeEnum.V4 ? this.CreateV4TemporarySignature(httpRequest, expires, iheaders) : this.CreateTemporarySignature(httpRequest, expires, iheaders));
        }
        internal override IDictionary <string, string> GetSignature(HttpRequest request, HttpContext context, IHeaders iheaders)
        {
            StringBuilder stringToSign = new StringBuilder();

            stringToSign.Append(request.Method.ToString()).Append("\n");

            string dateHeader        = Constants.CommonHeaders.Date.ToLower();
            string contentTypeHeader = Constants.CommonHeaders.ContentType.ToLower();
            string contentMd5Header  = Constants.CommonHeaders.ContentMd5.ToLower();
            string headerPrefix      = iheaders.HeaderPrefix();
            string headerMetaPrefix  = iheaders.HeaderMetaPrefix();

            IDictionary <string, string> tempDict = new Dictionary <string, string>();

            if (request.Headers.Count > 0)
            {
                foreach (KeyValuePair <string, string> entry in request.Headers)
                {
                    if (string.IsNullOrEmpty(entry.Key))
                    {
                        continue;
                    }

                    string key = entry.Key.Trim().ToLower();
                    if (key.StartsWith(headerPrefix) || key.Equals(contentTypeHeader) || key.Equals(contentMd5Header))
                    {
                        tempDict.Add(key, entry.Value);
                    }
                }
            }

            if (request.Headers.ContainsKey(dateHeader))
            {
                tempDict.Add(dateHeader, request.Headers[dateHeader]);
            }
            else
            {
                tempDict.Add(dateHeader, "");
            }

            if (!tempDict.ContainsKey(contentMd5Header))
            {
                tempDict.Add(contentMd5Header, "");
            }

            if (!tempDict.ContainsKey(contentTypeHeader))
            {
                tempDict.Add(contentTypeHeader, "");
            }

            List <KeyValuePair <string, string> > kvlist = new List <KeyValuePair <string, string> >(tempDict);

            tempDict.Clear();

            kvlist.Sort(delegate(KeyValuePair <string, string> x, KeyValuePair <string, string> y)
            {
                return(string.Compare(x.Key, y.Key, StringComparison.Ordinal));
            });

            foreach (KeyValuePair <string, string> kv in kvlist)
            {
                if (kv.Key.StartsWith(headerMetaPrefix))
                {
                    stringToSign.Append(kv.Key).Append(":").Append(kv.Value.Trim());
                }
                else if (kv.Key.StartsWith(headerPrefix))
                {
                    stringToSign.Append(kv.Key).Append(":").Append(kv.Value);
                }
                else
                {
                    stringToSign.Append(kv.Value);
                }
                stringToSign.Append("\n");
            }

            kvlist.Clear();

            stringToSign.Append("/");
            if (!string.IsNullOrEmpty(request.BucketName))
            {
                stringToSign.Append(CommonUtil.UrlEncode(request.BucketName));

                if (!request.PathStyle)
                {
                    stringToSign.Append("/");
                }

                if (!string.IsNullOrEmpty(request.ObjectKey))
                {
                    if (request.PathStyle)
                    {
                        stringToSign.Append("/");
                    }
                    stringToSign.Append(CommonUtil.UrlEncode(request.ObjectKey, null, "/"));
                }
            }

            if (request.Params.Count > 0)
            {
                foreach (KeyValuePair <string, string> entry in request.Params)
                {
                    if (string.IsNullOrEmpty(entry.Key))
                    {
                        continue;
                    }
                    if (Constants.AllowedResourceParameters.Contains(entry.Key.ToLower()) || entry.Key.ToLower().StartsWith(iheaders.HeaderPrefix()))
                    {
                        tempDict.Add(entry.Key, entry.Value);
                    }
                }
            }

            kvlist = new List <KeyValuePair <string, string> >(tempDict);

            tempDict.Clear();

            kvlist.Sort(delegate(KeyValuePair <string, string> x, KeyValuePair <string, string> y)
            {
                return(string.Compare(x.Key, y.Key, StringComparison.Ordinal));
            });
            if (kvlist.Count > 0)
            {
                bool isFirst = true;
                foreach (KeyValuePair <string, string> kv in kvlist)
                {
                    if (isFirst)
                    {
                        stringToSign.Append("?");
                        isFirst = false;
                    }
                    else
                    {
                        stringToSign.Append("&");
                    }
                    stringToSign.Append(kv.Key);
                    if (kv.Value != null)
                    {
                        stringToSign.Append("=").Append(kv.Value);
                    }
                }
            }

            if (LoggerMgr.IsDebugEnabled)
            {
                LoggerMgr.Debug("StringToSign: ******");
            }

            IDictionary <string, string> ret = new Dictionary <string, string>();

            ret.Add("Signature", Convert.ToBase64String(CommonUtil.HmacSha1(context.SecurityProvider.Sk, stringToSign.ToString())));

            return(ret);
        }