예제 #1
0
        private ObsException ParseObsException(HttpResponse response, string message, Exception ex, HttpContext context)
        {
            ObsException exception = new ObsException(message, ex);

            if (response != null)
            {
                exception.StatusCode = response.StatusCode;
                try
                {
                    CommonParser.ParseErrorResponse(response.Content, exception);
                }
                catch (Exception ee)
                {
                    if (LoggerMgr.IsErrorEnabled)
                    {
                        LoggerMgr.Error(ee.Message, ee);
                    }
                }
                string temp;
                if (response.Headers.TryGetValue(this.GetIHeaders(context).RequestId2Header(), out temp))
                {
                    exception.ObsId2 = temp;
                }
                if (string.IsNullOrEmpty(exception.RequestId) && response.Headers.TryGetValue(this.GetIHeaders(context).RequestIdHeader(), out temp))
                {
                    exception.RequestId = temp;
                }
                response.Abort();
            }
            exception.ErrorType = ErrorType.Receiver;
            return(exception);
        }
예제 #2
0
 public static void ParseErrorResponse(Stream stream, ObsException exception)
 {
     if (stream != null)
     {
         using (XmlReader reader = XmlReader.Create(stream))
         {
             while (reader.Read())
             {
                 if ("Code".Equals(reader.Name))
                 {
                     exception.ErrorCode = reader.ReadString();
                 }
                 else if ("Message".Equals(reader.Name))
                 {
                     exception.ErrorMessage = reader.ReadString();
                 }
                 else if ("RequestId".Equals(reader.Name))
                 {
                     exception.RequestId = reader.ReadString();
                 }
                 else if ("HostId".Equals(reader.Name))
                 {
                     exception.HostId = reader.ReadString();
                 }
             }
         }
     }
 }
        /// <summary>
        /// Write the object content to a file.
        /// </summary>
        /// <param name="filePath">File path</param>
        /// <param name="append">Write mode</param>
        public void WriteResponseStreamToFile(string filePath, bool append)
        {
            try
            {
                filePath = System.IO.Path.GetFullPath(filePath);
                FileInfo fi = new FileInfo(filePath);
                Directory.CreateDirectory(fi.DirectoryName);

                FileMode fm = FileMode.Create;
                if (append && File.Exists(filePath))
                {
                    fm = FileMode.Append;
                }
                using (Stream downloadStream = new FileStream(filePath, fm, FileAccess.Write, FileShare.Read, Constants.DefaultBufferSize))
                {
                    long   current   = 0;
                    byte[] buffer    = new byte[Constants.DefaultBufferSize];
                    int    bytesRead = 0;
                    while ((bytesRead = this.OutputStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        downloadStream.Write(buffer, 0, bytesRead);
                        current += bytesRead;
                    }
                    if (current != this.ContentLength)
                    {
                        throw new ObsException(string.Format("The total bytes read {0} from response stream is not equal to the Content-Length {1}", current, this.ContentLength), ErrorType.Receiver, null);
                    }
                }
            }
            catch (ObsException ex)
            {
                if (LoggerMgr.IsErrorEnabled)
                {
                    LoggerMgr.Error(ex.Message, ex);
                }
                throw ex;
            }
            catch (Exception ex)
            {
                if (LoggerMgr.IsErrorEnabled)
                {
                    LoggerMgr.Error(ex.Message, ex);
                }
                ObsException exception = new ObsException(ex.Message, ex);
                exception.ErrorType = ErrorType.Receiver;
                throw exception;
            }
            finally
            {
                if (OutputStream != null)
                {
                    OutputStream.Close();
                    OutputStream = null;
                }
            }
        }
        /// <summary>
        /// 序列化记录文件的数据一致性校验
        /// Md5值;文件的名字、大小、最后修改时间;CheckSum值
        /// </summary>
        /// <param name="uploadFile"></param>
        /// <param name="uploadStream"></param>
        /// <param name="enableCheckSum"></param>
        /// <returns></returns>
        public bool IsValid(string uploadFile, Stream uploadStream, bool enableCheckSum, ResumableUploadTypeEnum uploadType)
        {
            if (this.Md5 != ComputeHash.HashCode <UploadCheckPoint>(this))
            {
                return(false);
            }

            if (uploadType == ResumableUploadTypeEnum.UploadFile)
            {
                FileInfo upload = new FileInfo(uploadFile);
                if (this.FileStatus.Size != upload.Length || this.FileStatus.LastModified != upload.LastWriteTime)
                {
                    return(false);
                }
            }
            else if (this.FileStatus.Size != uploadStream.Length - uploadStream.Position)
            {
                return(false);
            }

            if (enableCheckSum)
            {
                if (this.FileStatus.CheckSum != null)
                {
                    try
                    {
                        if (uploadType == ResumableUploadTypeEnum.UploadFile)
                        {
                            using (FileStream fileStream = new FileStream(uploadFile, FileMode.Open))
                            {
                                //校验CheckSum值--UploadFile文件的一致性
                                return(this.FileStatus.CheckSum.Equals(CommonUtil.Base64Md5(fileStream)));
                            }
                        }
                        else
                        {
                            //校验CheckSum值--UploadStream流的一致性
                            long originPosition = uploadStream.Position;
                            bool flag           = this.FileStatus.CheckSum.Equals(CommonUtil.Base64Md5(uploadStream));
                            uploadStream.Seek(originPosition, SeekOrigin.Begin);
                            return(flag);
                        }
                    }
                    catch (Exception ex)
                    {
                        ObsException e = new ObsException(ex.Message, ex.InnerException);
                        e.ErrorType = ErrorType.Sender;
                        throw e;
                    }
                }
            }
            return(true);
        }
예제 #5
0
        private ObsException ParseObsException(HttpResponse response, string message, Exception ex, HttpContext context)
        {
            ObsException exception = new ObsException(message, ex);

            if (response != null)
            {
                exception.StatusCode = response.StatusCode;
                string temp;
                try
                {
                    if (response.Content.Length > 0)
                    {
                        CommonParser.ParseErrorResponse(response.Content, exception);
                    }
                    else if (response.Headers.ContainsKey(Constants.ObsHeadErrorCode) && response.Headers.ContainsKey(Constants.ObsHeadErrorMessage))
                    {
                        response.Headers.TryGetValue(Constants.ObsHeadErrorCode, out temp);
                        exception.ErrorCode = temp;
                        response.Headers.TryGetValue(Constants.ObsHeadErrorMessage, out temp);
                        exception.ErrorMessage = temp;
                    }
                    else
                    {
                        exception.ErrorCode    = response.StatusCode.ToString();
                        exception.ErrorMessage = response.Failure.Message;
                    }
                }
                catch (Exception ee)
                {
                    exception.ErrorMessage = ee.Message;
                    if (LoggerMgr.IsErrorEnabled)
                    {
                        LoggerMgr.Error(ee.Message, ee);
                    }
                }

                if (response.Headers.TryGetValue(this.GetIHeaders(context).RequestId2Header(), out temp))
                {
                    exception.ObsId2 = temp;
                }
                if (string.IsNullOrEmpty(exception.RequestId) && response.Headers.TryGetValue(this.GetIHeaders(context).RequestIdHeader(), out temp))
                {
                    exception.RequestId = temp;
                }
                response.Abort();
            }
            exception.ErrorType = ErrorType.Receiver;
            return(exception);
        }
        /// <summary>
        /// 获取待上传文件/数据流的状态信息
        /// </summary>
        /// <param name="uploadFile"></param>
        /// <param name="uploadStream"></param>
        /// <param name="checkSum"></param>
        /// <returns></returns>
        public static FileStatus getFileStatus(string uploadFile, Stream uploadStream, bool checkSum, ResumableUploadTypeEnum uploadType)
        {
            FileStatus fileStatus = new FileStatus();

            if (uploadType == ResumableUploadTypeEnum.UploadFile)
            {
                FileInfo fileInfo = new FileInfo(uploadFile);
                fileStatus.Size         = fileInfo.Length;
                fileStatus.LastModified = fileInfo.LastWriteTime;
            }
            else
            {
                fileStatus.Size = uploadStream.Length - uploadStream.Position;
                //数据流方式LastModified置为空
                fileStatus.LastModified = null;
            }

            //若开启文件内容校验
            if (checkSum)
            {
                try
                {
                    if (uploadType == ResumableUploadTypeEnum.UploadFile)
                    {
                        using (FileStream fileStream = new FileStream(uploadFile, FileMode.Open))
                        {
                            //计算UploadFile的hash值
                            fileStatus.CheckSum = CommonUtil.Base64Md5(fileStream);
                        }
                    }
                    else
                    {
                        //计算UploadStream的hash值
                        long originPosition = uploadStream.Position;
                        fileStatus.CheckSum = CommonUtil.Base64Md5(uploadStream);
                        uploadStream.Seek(originPosition, SeekOrigin.Begin);
                    }
                }
                catch (Exception ex)
                {
                    ObsException e = new ObsException(ex.Message, ex.InnerException);
                    e.ErrorType = ErrorType.Sender;
                    throw e;
                }
            }

            return(fileStatus);
        }
예제 #7
0
        internal HttpResponse PerformRequest(HttpRequest request, HttpContext context, int retryCount)
        {
            long         originPos = -1;
            HttpResponse response  = null;

            try
            {
                if (request.Content != null && request.Content.CanSeek)
                {
                    originPos = request.Content.Position;
                }
                response = this.DoRequest(request, context);

                new MergeResponseHeaderHandler(this.GetIHeaders(context)).Handle(response);

                int statusCode = Convert.ToInt32(response.StatusCode);

                if (LoggerMgr.IsDebugEnabled)
                {
                    LoggerMgr.Debug(string.Format("Response with statusCode {0} and headers {1}", statusCode, CommonUtil.ConvertHeadersToString(response.Headers)));
                }

                int maxErrorRetry = context.ObsConfig.MaxErrorRetry;

                if (statusCode >= 300 && statusCode < 400 && statusCode != 304)
                {
                    if (response.Headers.ContainsKey(Constants.CommonHeaders.Location))
                    {
                        string location = response.Headers[Constants.CommonHeaders.Location];
                        if (!string.IsNullOrEmpty(location))
                        {
                            if (location.IndexOf("?") < 0)
                            {
                                location += "?" + CommonUtil.ConvertParamsToString(request.Params);
                            }
                            if (LoggerMgr.IsWarnEnabled)
                            {
                                LoggerMgr.Warn(string.Format("Redirect to {0}", location));
                            }
                            context.RedirectLocation = location;
                            retryCount--;
                            if (ShouldRetry(request, null, retryCount, maxErrorRetry))
                            {
                                PrepareRetry(request, response, retryCount, originPos, false);
                                return(PerformRequest(request, context, ++retryCount));
                            }
                            else if (retryCount > maxErrorRetry)
                            {
                                throw ParseObsException(response, "Exceeded 3xx redirect limit", context);
                            }
                        }
                    }
                    throw ParseObsException(response, "Try to redirect, but location is null!", context);
                }
                else if ((statusCode >= 400 && statusCode < 500) || statusCode == 304)
                {
                    ObsException exception = ParseObsException(response, "Request error", context);
                    if (Constants.RequestTimeout.Equals(exception.ErrorCode))
                    {
                        if (ShouldRetry(request, null, retryCount, maxErrorRetry))
                        {
                            if (LoggerMgr.IsWarnEnabled)
                            {
                                LoggerMgr.Warn("Retrying connection that failed with RequestTimeout error");
                            }
                            PrepareRetry(request, response, retryCount, originPos, false);
                            return(PerformRequest(request, context, ++retryCount));
                        }
                        else if (retryCount > maxErrorRetry && LoggerMgr.IsErrorEnabled)
                        {
                            LoggerMgr.Error("Exceeded maximum number of retries for RequestTimeout errors");
                        }
                    }
                    throw exception;
                }
                else if (statusCode >= 500)
                {
                    if (ShouldRetry(request, null, retryCount, maxErrorRetry))
                    {
                        PrepareRetry(request, response, retryCount, originPos, true);
                        return(PerformRequest(request, context, ++retryCount));
                    }
                    else if (retryCount > maxErrorRetry && LoggerMgr.IsErrorEnabled)
                    {
                        LoggerMgr.Error("Encountered too many 5xx errors");
                    }
                    throw ParseObsException(response, "Request error", context);
                }
                return(response);
            }
            catch (Exception ex)
            {
                try
                {
                    if (ex is ObsException)
                    {
                        if (LoggerMgr.IsErrorEnabled)
                        {
                            LoggerMgr.Error("Rethrowing as a ObsException error in PerformRequest", ex);
                        }
                        throw ex;
                    }
                    else
                    {
                        if (ShouldRetry(request, ex, retryCount, context.ObsConfig.MaxErrorRetry))
                        {
                            PrepareRetry(request, response, retryCount, originPos, true);
                            return(PerformRequest(request, context, ++retryCount));
                        }
                        else if (retryCount > context.ObsConfig.MaxErrorRetry && LoggerMgr.IsWarnEnabled)
                        {
                            LoggerMgr.Warn("Too many errors excced the max error retry count", ex);
                        }
                        if (LoggerMgr.IsErrorEnabled)
                        {
                            LoggerMgr.Error("Rethrowing as a ObsException error in PerformRequest", ex);
                        }
                        throw ParseObsException(response, ex.Message, ex, context);
                    }
                }
                finally
                {
                    CommonUtil.CloseIDisposable(response);
                }
            }
        }