private void ExecuteFileTests(ICompressor compressor, string extension, List<string> exclusions) { // Bung the folder name and extension onto each element - makes it easier in testing membership // Since we don't have linq in .net 2 land :} for (var i = 0; i < exclusions.Count; i++) { exclusions[i] = "Compatability Test Files\\" + exclusions[i] + extension; } var sourceFiles = Directory.GetFiles("Compatability Test Files", "*" + extension); Assume.That(sourceFiles.Length, Is.GreaterThan(0), "No matching source files found, nothing to test"); foreach (var file in Directory.GetFiles("Compatability Test Files", "*" + extension)) { if (exclusions.Contains(file)) { continue; } var source = File.ReadAllText(file); var expected = File.ReadAllText(file + ".min"); // Some of the test files have a load of carriage returns at the end, so we should strip those out while (expected.EndsWith("\n")) { expected = expected.Substring(0, expected.Length - 1); } // Act var actual = compressor.Compress(source); // Assert Assert.That(actual, Is.EqualTo(expected), file); } }
static ZlibCompression() { if (IntPtr.Size == 8) Compressor = new Compressor64(); else Compressor = new Compressor32(); }
public ZOutputStream(Stream output) : this() { this._output = output; this._compressor = new Inflate(); this._compressionMode = CompressionMode.Decompress; }
public ZOutputStream(Stream output, CompressionLevel level, bool nowrap) : this() { this._output = output; this._compressor = new Deflate(level, nowrap); this._compressionMode = CompressionMode.Compress; }
public void Performance(ICompressor compressor) { results.Add("---------" + compressor.GetType() + "---------"); byte[] indata = Encoding.UTF8.GetBytes(testData); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); byte[] result = null; foreach (var i in Enumerable.Range(0,100)) { result = compressor.Compress(indata); } stopwatch.Stop(); long ticks = stopwatch.ElapsedTicks; results.Add("Compress: " + ticks); stopwatch.Reset(); stopwatch.Start(); byte[] resurrected; foreach (var i in Enumerable.Range(0, 100)) { resurrected = compressor.Decompress(result); } stopwatch.Stop(); ticks = stopwatch.ElapsedTicks; results.Add("Decompress: " + ticks); results.Add("Compression: " + result.Length / (1.0 * indata.Length)); }
public static string GetCompressedContents(IEnumerable<string> bundle, string basePath, ICompressor compressor) { var fileList = bundle.ToList(); var tempLookup = new ConcurrentDictionary<int, string>(); Parallel.For(0, fileList.Count(), index => { var file = fileList[index]; var filePathName = Path.Combine(basePath, file); var fileContents = File.ReadAllText(filePathName, Encoding.UTF8); try { fileContents = compressor.Compress(fileContents); } catch (Exception) { Log.Warn("Error during compression (using uncompressed file) - " + file); } tempLookup[index] = fileContents; }); var contents = new StringBuilder(); for (var index = 0; index < fileList.Count; index++) { contents.Append(tempLookup[index]); } return contents.ToString(); }
public CompressorTaskEngine(ILog log, ICompressor compressor) { Log = log; this.compressor = compressor; this.Encoding = Encoding.Default; DeleteSourceFiles = false; LineBreakPosition = -1; }
protected CompressorTask(ICompressor compressor) { this.logger = new NantLogAdapter(); this.TaskEngine = new CompressorTaskEngine(logger, compressor) { SetBuildParameters = this.SetBuildParameters }; this.SourceFiles = new FileSet(); this.DeleteSourceFiles = false; this.LineBreakPosition = -1; }
public CompressorTaskEngine(ILog log, ICompressor compressor) { Log = log; this.compressor = compressor; this.Encoding = Encoding.Default; DeleteSourceFiles = false; LineBreakPosition = -1; EcmaExceptions = new List<EcmaScriptException>(); }
private static async Task CompressResponseNormal(IOwinContext context, ICompressor tmpCompressor, Stream responseStream, CancellationToken cancellationToken) { using (var tmpBufferStream = new MemoryStream()) { await tmpCompressor.Compress(context.Response.Body, tmpBufferStream, cancellationToken); context.Response.ContentLength = tmpBufferStream.Length; await BaseCompressor.Pump(tmpBufferStream, responseStream, cancellationToken); } }
public static MongoGridFSFileInfo UploadFileWithCompress(this IMongoRepository repository, ICompressor compressor, byte[] data, string remoteFilename) { compressor.ShouldNotBeNull("compressor"); var entry = new CompressedFileItem(compressor.GetType(), compressor.Compress(data)); using(var stream = new MemoryStream(FileSerializer.Serialize(entry))) { return repository.UploadFile(remoteFilename, stream); } }
public CompressedContent(HttpContent content, ICompressor compressor) { //Ensure.Argument.NotNull(content, "content"); //Ensure.Argument.NotNull(compressor, "compressor"); this.content = content; this.compressor = compressor; AddHeaders(); }
public CompressedContainerImpl(SerializationInfo info, StreamingContext ctxt) : base(info, ctxt) { // Nasty hack - The content excryption tests use the serialization constructors // which is only used for testing. When the object is serialized the compressor/decompressor are // never set. m_decompressor = new ZipDecompressor(); m_compressor = new ZipCompressor(); }
public void Compress_decompression_yields_original_data(ICompressor compressor) { var randomBytes = new byte[2000]; new Randomizer().NextBytes(randomBytes); var textBytes = Encoding.UTF8.GetBytes(testData); foreach (var array in new[]{randomBytes,textBytes}) { var resurrected = compressor.Decompress(compressor.Compress(array)); CollectionAssert.AreEqual(array, resurrected); } }
public CompressedContainerImpl(FileData fileData, bool expanded, ICompressor compressor, IDecompressor decompressor) : base(fileData) { m_decompressor = decompressor; m_compressor = compressor; m_fileData.FileType = Workshare.Policy.FileType.ZIP; m_expanded = expanded; // We want to know when someone adds to the collection so that we can mark it as expanded. this.Files.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(FilesCollectionChanged); }
public ZInputStream(Stream input, bool nowrap) : this() { Debug.Assert(input.CanRead); this._input = input; this._compressor = new Inflate(nowrap); this._compressionMode = CompressionMode.Decompress; this._compressor.next_in = _buffer; this._compressor.next_in_index = 0; this._compressor.avail_in = 0; }
private static async Task<HttpContent> DecompressContent(HttpContent compressedContent, ICompressor compressor) { using (compressedContent) { MemoryStream decompressed = new MemoryStream(); await compressor.Decompress(await compressedContent.ReadAsStreamAsync(), decompressed); var newContent = new StreamContent(decompressed); // copy content type so we know how to load correct formatter newContent.Headers.ContentType = compressedContent.Headers.ContentType; return newContent; } }
private static async Task<HttpContent> DecompressContentAsync(HttpContent compressedContent, ICompressor compressor) { using (compressedContent) { var decompressed = new MemoryStream(); await compressor.Decompress(await compressedContent.ReadAsStreamAsync(), decompressed).ConfigureAwait(true); decompressed.Position = 0; var newContent = new StreamContent(decompressed); newContent.Headers.ContentType = compressedContent.Headers.ContentType; return newContent; } }
private static async Task CompressToHttpOutputStream(OwinContext context, ICompressor compressor, Stream httpOutputStream, MemoryStream uncompressedStream) { using (var compressedStream = new MemoryStream()) { using (var compressionStream = compressor.CreateStream(compressedStream)) { await uncompressedStream.CopyToAsync(compressionStream, BufferSize); } compressedStream.Position = 0; SetResponseHeaders(context, compressor, compressedStream); await compressedStream.CopyToAsync(httpOutputStream, BufferSize); } }
public virtual void Compression(string sourcePath, string destinationPath, CompressionMode mode, ICompressor compressor = null) { compressor = compressor ?? new GzipCompressor(); switch (mode) { case CompressionMode.Compress: Compress(sourcePath, destinationPath, compressor); return; case CompressionMode.Decompress: Decompress(sourcePath, destinationPath, compressor); return; default: throw new ArgumentException("The compression mode is not valid.", "mode"); } }
private void Decompress(string sourcePath, string destinationPath, ICompressor compressor) { using (var originalStream = OpenRead(sourcePath)) { using (var destinationStream = OpenWrite(destinationPath)) { using (compressor) { using (var compression = compressor.Decompress(originalStream)) { compression.CopyTo(destinationStream); } } } } }
private static async Task<HttpContent> DecompressContentAsync(HttpContent compressedContent, ICompressor compressor) { using (compressedContent) { MemoryStream decompressed = new MemoryStream(); await compressor.Decompress(await compressedContent.ReadAsStreamAsync(), decompressed).ConfigureAwait(false); // set position back to 0 so it can be read again decompressed.Position = 0; var newContent = new StreamContent(decompressed); // copy content type so we know how to load correct formatter newContent.Headers.ContentType = compressedContent.Headers.ContentType; return newContent; } }
/// <summary> /// Initializes a new instance of the <see cref="CompressedContent"/> class. /// </summary> /// <param name="content">The content.</param> /// <param name="compressor">The compressor.</param> public CompressedContent(HttpContent content, ICompressor compressor) { if (content == null) { throw new ArgumentNullException("content"); } if (compressor == null) { throw new ArgumentNullException("compressor"); } this.originalContent = content; this.compressor = compressor; this.CopyHeaders(); }
public BrokeredMessageFactory(MaxLargeMessageSizeSetting maxLargeMessageSize, MaxSmallMessageSizeSetting maxSmallMessageSize, IClock clock, ICompressor compressor, IDispatchContextManager dispatchContextManager, ILargeMessageBodyStore largeMessageBodyStore, ISerializer serializer, ITypeProvider typeProvider) { _serializer = serializer; _maxLargeMessageSize = maxLargeMessageSize; _maxSmallMessageSize = maxSmallMessageSize; _compressor = compressor; _dispatchContextManager = dispatchContextManager; _largeMessageBodyStore = largeMessageBodyStore; _typeProvider = typeProvider; _clock = clock; }
internal override async Task Compress(Func<IDictionary<string, object>, Task> next, OwinContext context, ICompressor compressor, Stream httpOutputStream) { using (var memoryStream = new MemoryStream()) { using (var compressedStream = compressor.CreateStream(memoryStream)) { context.Response.Body = compressedStream; await next.Invoke(context.Environment); } if (memoryStream.Length > 0) { SetResponseHeaders(context, compressor, memoryStream); memoryStream.Position = 0; await memoryStream.CopyToAsync(httpOutputStream, BufferSize); } } }
/// <summary> /// <paramref name="pageState"/> 정보를 직렬화해서, <see cref="IPageStateEntity"/> 객체로 빌드합니다. /// </summary> /// <param name="id"></param> /// <param name="pageState"></param> /// <param name="compressor"></param> /// <param name="compressThreshold"></param> /// <returns></returns> public static IPageStateEntity CreateStateEntity(string id, object pageState, ICompressor compressor, int compressThreshold = 40960) { compressor.ShouldNotBeNull("compressor"); compressThreshold.ShouldBePositive("compressThreshold"); if(IsDebugEnabled) log.Debug("상태 정보를 가지는 인스턴스를 생성합니다. pageState=[{0}], compressor=[{1}], compressThreshold=[{2}]", pageState, compressor, compressThreshold); if(pageState == null) return new PageStateEntity(id, null, false); var serializedValue = Serializer.Serialize(pageState); if(serializedValue.Length > compressThreshold) return new PageStateEntity(id, compressor.Compress(serializedValue), true); return new PageStateEntity(id, serializedValue, false); }
/// <summary> /// Decompresses the compressed HTTP content. /// </summary> /// <param name="compressedContent">The compressed HTTP content.</param> /// <param name="compressor">The compressor.</param> /// <returns>The decompressed content.</returns> public async Task<HttpContent> DecompressContent(HttpContent compressedContent, ICompressor compressor) { var decompressedContentStream = new MemoryStream(); // Decompress buffered content using (var ms = new MemoryStream(await compressedContent.ReadAsByteArrayAsync())) { await compressor.Decompress(ms, decompressedContentStream).ConfigureAwait(false); } // Set position back to 0 so it can be read again decompressedContentStream.Position = 0; var decompressedContent = new StreamContent(decompressedContentStream); // Copy content headers so we know what got sent back this.CopyHeaders(compressedContent, decompressedContent); return decompressedContent; }
public BrokeredMessageFactory(DefaultMessageTimeToLiveSetting timeToLive, MaxLargeMessageSizeSetting maxLargeMessageSize, MaxSmallMessageSizeSetting maxSmallMessageSize, ReplyQueueNameSetting replyQueueName, IClock clock, ICompressor compressor, IDispatchContextManager dispatchContextManager, ILargeMessageBodyStore largeMessageBodyStore, ISerializer serializer, ITypeProvider typeProvider) { _timeToLive = timeToLive; _maxLargeMessageSize = maxLargeMessageSize; _maxSmallMessageSize = maxSmallMessageSize; _replyQueueName = replyQueueName; _clock = clock; _compressor = compressor; _dispatchContextManager = dispatchContextManager; _largeMessageBodyStore = largeMessageBodyStore; _serializer = serializer; _typeProvider = typeProvider; }
internal override async Task Compress(Func<IDictionary<string, object>, Task> next, OwinContext context, ICompressor compressor, Stream httpOutputStream) { using (var uncompressedStream = new MemoryStream()) { context.Response.Body = uncompressedStream; await next.Invoke(context.Environment); await uncompressedStream.FlushAsync(); if (uncompressedStream.Length > 0) { uncompressedStream.Position = 0; if (ShouldCompress(context.Response.ContentType)) { await CompressToHttpOutputStream(context, compressor, httpOutputStream, uncompressedStream); } else { await uncompressedStream.CopyToAsync(httpOutputStream, BufferSize); } } } }
private void ExecuteFileTests(ICompressor compressor, string extension, List <string> exclusions) { // Bung the folder name and extension onto each element - makes it easier in testing membership // Since we don't have linq in .net 2 land :} for (var i = 0; i < exclusions.Count; i++) { exclusions[i] = "Compatability Test Files\\" + exclusions[i] + extension; } var sourceFiles = Directory.GetFiles("Compatability Test Files", "*" + extension); Assume.That(sourceFiles.Length, Is.GreaterThan(0), "No matching source files found, nothing to test"); foreach (var file in Directory.GetFiles("Compatability Test Files", "*" + extension)) { Debug.WriteLine("File: " + file); if (exclusions.Contains(file)) { continue; } var source = File.ReadAllText(file); var expected = File.ReadAllText(file + ".min"); // Some of the test files have a load of carriage returns at the end, so we should strip those out while (expected.EndsWith("\n")) { expected = expected.Substring(0, expected.Length - 1); } // Act var actual = compressor.Compress(source); // Assert Assert.That(actual, Is.EqualTo(expected), file); } }
internal ZLibStatus InflateTreesBits(int[] c, // 19 code lengths int[] bb, // bits tree desired/actual depth int[] tb, // bits tree result int[] hp, // space for trees ICompressor z // for messages ) { ZLibStatus result; InitWorkArea(19); hn[0] = 0; result = HuftBuild(c, 0, 19, 19, null, null, tb, bb, hp, hn, v); if (result == ZLibStatus.Z_DATA_ERROR) { z.msg = "oversubscribed dynamic bit lengths tree"; } else if (result == ZLibStatus.Z_BUF_ERROR || bb[0] == 0) { z.msg = "incomplete dynamic bit lengths tree"; result = ZLibStatus.Z_DATA_ERROR; } return(result); }
public ICompressor Create(WebPageContext context) { var value = context.GetConfigValue <string>("Page", "compressor", string.Empty).ToLower(); if (!string.IsNullOrEmpty(value)) { switch (value) { case "gzip": return(HttpCompressor.Instance); default: return(NonCompressor.Instance); } } //如果没有页面级配置,那么根据扩展名查看配置 ICompressor compressor = null; if (_compressors.TryGetValue(context.PathExtension, out compressor)) { return(compressor); } return(NonCompressor.Instance); }
/// <summary> /// set some default settings for the desired behaviour. /// </summary> /// <param name="comp"></param> private void ConfigureCompressor(ICompressor comp) { if (comp is IJavaScriptCompressor) { IJavaScriptCompressor jsComp = comp as IJavaScriptCompressor; jsComp.CompressionType = CompressionType.Standard; jsComp.DisableOptimizations = true; //TODO output errors - jsComp.ErrorReporter jsComp.IgnoreEval = false; //allow eval to pass through jsComp.ObfuscateJavascript = false; jsComp.PreserveAllSemicolons = true; //TODO would be nice to keep comments! } else if (comp is ICssCompressor) { CssCompressor cssComp = comp as CssCompressor; cssComp.CompressionType = CompressionType.Standard; cssComp.RemoveComments = false; } else { throw new ArgumentException("Unrecognised Compressor"); } }
/// <summary> /// 压缩代码 /// </summary> /// <param name="code"></param> /// <returns></returns> protected virtual Stream CompressCode(string code) { if (code == null) { return(null); } //压缩 byte[] codeBytes = Encoding.UTF8.GetBytes(code); MemoryStream source = new MemoryStream(codeBytes); var context = this.PageContext; ICompressor cpor = CompressorFactory.Create(context); if (cpor.IsAccepted(context)) { //支持压缩 MemoryStream target = new MemoryStream(codeBytes.Length); cpor.Compress(context, source, target); target.Position = 0; source = target; } return(source); }
public MessageServerFacade( IComponentryContainer container, IMessageBusAdapter messagingAdapter, ICompressor compressor, EncryptionSettings encryption, ZmqNetworkAddress requestAddress, ZmqNetworkAddress responseAddress) : base( container, messagingAdapter, new MsgPackDictionarySerializer(), new MsgPackRequestSerializer(), new MsgPackResponseSerializer(), compressor, encryption, new Label("test-server"), requestAddress, responseAddress) { this.ReceivedMessages = new List <DataRequest>(); this.ReceivedStrings = new List <string>(); this.RegisterHandler <DataRequest>(this.OnMessage); }
/// <summary> /// <paramref name="stateEntity"/> 정보를 파싱하여 원본 Page 상태정보를 빌드합니다. /// </summary> /// <param name="stateEntity"></param> /// <param name="compressor"></param> /// <param name="pageState"></param> /// <returns></returns> public static bool TryParseStateEntity(IPageStateEntity stateEntity, ICompressor compressor, out object pageState) { if(IsDebugEnabled) log.Debug("상태 정보를 파싱합니다..."); pageState = null; if(stateEntity == null) return false; bool result; try { if(stateEntity.Value != null) { if(IsDebugEnabled) log.Debug("저장된 상태 정보(stateEntity)의 값이 존재합니다. 값을 로드합니다... entity Id=[{0}]", stateEntity.Id); var bytes = (stateEntity.IsCompressed) ? compressor.Decompress(stateEntity.Value) : stateEntity.Value; pageState = Serializer.Deserialize(bytes); } result = (pageState != null); } catch(Exception ex) { if(log.IsWarnEnabled) { log.Warn("State 정보를 파싱하는데 실패했습니다. stateEntity=[{0}]", stateEntity); log.Warn(ex); } result = false; } if(IsDebugEnabled) log.Debug("저장된 상태 정보를 로드했습니다. 결과=[{0}], StateEntity=[{1}]", result, stateEntity); return result; }
/// <summary> /// Initializes a new instance of the <see cref="TickPublisher"/> class. /// </summary> /// <param name="container">The componentry container.</param> /// <param name="dataBusAdapter">The data bus adapter.</param> /// <param name="quoteSerializer">The quote tick serializer.</param> /// <param name="tradeSerializer">The trade tick serializer.</param> /// <param name="compressor">The data compressor.</param> /// <param name="encryption">The encryption configuration.</param> /// <param name="port">The publisher port.</param> public TickPublisher( IComponentryContainer container, IDataBusAdapter dataBusAdapter, IDataSerializer <QuoteTick> quoteSerializer, IDataSerializer <TradeTick> tradeSerializer, ICompressor compressor, EncryptionSettings encryption, Port port) : base( container, dataBusAdapter, compressor, encryption, ZmqNetworkAddress.AllInterfaces(port)) { this.quoteSerializer = quoteSerializer; this.tradeSerializer = tradeSerializer; this.RegisterHandler <QuoteTick>(this.OnMessage); this.RegisterHandler <TradeTick>(this.OnMessage); this.Subscribe <QuoteTick>(); this.Subscribe <TradeTick>(); }
/** * <summary>Get the array of bytes in any compression format.</summary> * * <param name="compressor">The compression format.</param> * <returns>The array of bytes.</returns> */ public byte[] Export(ICompressor compressor) { return(odsInternal.Export(compressor)); }
public async Task Invoke(HttpContext context) { bool useMinification = _options.IsMinificationEnabled() && _minificationManagers.Count > 0; bool useCompression = _options.IsCompressionEnabled() && _compressionManager != null; if (!useMinification && !useCompression) { await _next.Invoke(context); return; } HttpRequest request = context.Request; HttpResponse response = context.Response; using (var cachedStream = new MemoryStream()) { Stream originalStream = response.Body; response.Body = cachedStream; try { await _next.Invoke(context); } catch (Exception) { response.Body = originalStream; cachedStream.SetLength(0); throw; } byte[] cachedBytes = cachedStream.ToArray(); int cachedByteCount = cachedBytes.Length; bool isProcessed = false; response.Body = originalStream; cachedStream.SetLength(0); if (request.Method == "GET" && response.StatusCode == 200 && _options.IsAllowableResponseSize(cachedByteCount)) { string contentType = response.ContentType; string mediaType = null; Encoding encoding = null; if (contentType != null) { MediaTypeHeaderValue mediaTypeHeader; if (MediaTypeHeaderValue.TryParse(contentType, out mediaTypeHeader)) { mediaType = mediaTypeHeader.MediaType.ToLowerInvariant(); encoding = mediaTypeHeader.Encoding; } } encoding = encoding ?? Encoding.GetEncoding(0); string currentUrl = request.Path.Value; QueryString queryString = request.QueryString; if (queryString.HasValue) { currentUrl += queryString.Value; } string content = encoding.GetString(cachedBytes); string processedContent = content; IHeaderDictionary responseHeaders = response.Headers; bool isEncodedContent = responseHeaders.IsEncodedContent(); Action <string, string> appendHttpHeader = (key, value) => { responseHeaders.Append(key, new StringValues(value)); }; if (useMinification) { foreach (IMarkupMinificationManager minificationManager in _minificationManagers) { if (mediaType != null && minificationManager.IsSupportedMediaType(mediaType) && minificationManager.IsProcessablePage(currentUrl)) { if (isEncodedContent) { throw new InvalidOperationException( string.Format( AspNetCommonStrings.MarkupMinificationIsNotApplicableToEncodedContent, responseHeaders["Content-Encoding"] ) ); } IMarkupMinifier minifier = minificationManager.CreateMinifier(); MarkupMinificationResult minificationResult = minifier.Minify(processedContent, currentUrl, encoding, false); if (minificationResult.Errors.Count == 0) { processedContent = minificationResult.MinifiedContent; if (_options.IsPoweredByHttpHeadersEnabled()) { minificationManager.AppendPoweredByHttpHeader(appendHttpHeader); } isProcessed = true; } } if (isProcessed) { break; } } } if (useCompression && !isEncodedContent && _compressionManager.IsSupportedMediaType(mediaType)) { byte[] processedBytes = encoding.GetBytes(processedContent); using (var inputStream = new MemoryStream(processedBytes)) using (var outputStream = new MemoryStream()) { string acceptEncoding = request.Headers["Accept-Encoding"]; ICompressor compressor = _compressionManager.CreateCompressor(acceptEncoding); using (Stream compressedStream = compressor.Compress(outputStream)) { await inputStream.CopyToAsync(compressedStream); } byte[] compressedBytes = outputStream.ToArray(); int compressedByteCount = compressedBytes.Length; if (outputStream.CanWrite) { outputStream.SetLength(0); } inputStream.SetLength(0); responseHeaders["Content-Length"] = compressedByteCount.ToString(); compressor.AppendHttpHeaders(appendHttpHeader); await originalStream.WriteAsync(compressedBytes, 0, compressedByteCount); } isProcessed = true; } else { if (isProcessed) { byte[] processedBytes = encoding.GetBytes(processedContent); int processedByteCount = processedBytes.Length; responseHeaders["Content-Length"] = processedByteCount.ToString(); await originalStream.WriteAsync(processedBytes, 0, processedByteCount); } } } if (!isProcessed) { await originalStream.WriteAsync(cachedBytes, 0, cachedByteCount); } } }
protected override void Encode(IChannelHandlerContext context, object msg, IByteBuffer output) { try { if (msg is RpcMessage rpcMessage) { int fullLength = ProtocolConstants.V1_HEAD_LENGTH; int headLength = ProtocolConstants.V1_HEAD_LENGTH; byte messageType = rpcMessage.MessageType; output.WriteBytes(ProtocolConstants.MAGIC_CODE_BYTES); output.WriteByte(ProtocolConstants.VERSION); // full Length(4B) and head length(2B) will fix in the end. output.SetWriterIndex(output.WriterIndex + 6); output.WriteByte(messageType); output.WriteByte(rpcMessage.Codec); output.WriteByte(rpcMessage.Compressor); output.WriteInt(rpcMessage.Id); // direct write head with zero-copy IDictionary <string, string> headMap = rpcMessage.HeadMap; if (headMap?.Count > 0) { int headMapBytesLength = HeadMapSerializer.getInstance().Encode(headMap, output); headLength += headMapBytesLength; fullLength += headMapBytesLength; } byte[] bodyBytes = null; if (messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST && messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE) { ISerializer serializer = EnhancedServiceLoader.Load <ISerializer>(((SerializerType)rpcMessage.Codec).ToString()); bodyBytes = serializer.Serialize(rpcMessage.Body as AbstractMessage); ICompressor compressor = CompressorFactory.GetCompressor(rpcMessage.Compressor); bodyBytes = compressor.Compress(bodyBytes); fullLength += bodyBytes.Length; } if (bodyBytes != null) { output.WriteBytes(bodyBytes); } // fix fullLength and headLength int writeIndex = output.WriterIndex; // skip magic code(2B) + version(1B) output.SetWriterIndex(writeIndex - fullLength + 3); output.WriteInt(fullLength); output.WriteShort(headLength); output.SetWriterIndex(writeIndex); } else { throw new NotSupportedException("Not support this class:" + msg.GetType()); } } catch (Exception e) { Logger().LogError(e, "Encode request error!"); } }
/// <summary> /// Repetitively executes the specified action while it satisfies the current retry policy. /// </summary> /// <typeparam name="TResult">The type of result expected from the executable action.</typeparam> /// <param name="func">A delegate representing the executable action which returns the result of type R.</param> /// <returns>The result from the action.</returns> private TResult ExecuteAction <TResult>(Func <TResult> func) { IntuitRetryHelper.IsArgumentNull(func, "func"); int retryCount = 1; TimeSpan delay = TimeSpan.Zero; Exception lastError; while (true) { lastError = null; try { return(func()); } catch (RetryExceededException retryExceededException) { // The user code can throw a RetryLimitExceededException to force the exit from the retry loop. // The RetryLimitExceeded exception can have an inner exception attached to it. This is the exception // which we will have to throw up the stack so that callers can handle it. if (retryExceededException.InnerException != null) { throw retryExceededException.InnerException; } else { return(default(TResult)); } } catch (Exception ex) { lastError = ex; if (!IsTransient(lastError) || ((this.ExtendedRetryException != null) && this.ExtendedRetryException.IsRetryException(ex))) { throw; } if (!this.shouldRetry(retryCount++, lastError, out delay)) { WebException webException = ex as WebException; string errorString = string.Empty; if (webException != null) { // If not null then check the response property of the webException object. if (webException.Response != null) { // There is a response from the Ids server. Cast it to HttpWebResponse. HttpWebResponse errorResponse = (HttpWebResponse)webException.Response; // Get the status code description of the error response. string statusCodeDescription = errorResponse.StatusCode.ToString(); // Get the status code of the error response. int statusCode = (int)errorResponse.StatusCode; ICompressor responseCompressor = CoreHelper.GetCompressor(this.context, false); if (!string.IsNullOrWhiteSpace(errorResponse.ContentEncoding) && responseCompressor != null) { using (var responseStream = errorResponse.GetResponseStream()) //Check for decompressing { using (var decompressedStream = responseCompressor.Decompress(responseStream)) { // Get the response stream. StreamReader reader = new StreamReader(decompressedStream); //StreamReader reader = new StreamReader(responseStream); // Read the Stream errorString = reader.ReadToEnd(); // Close reader reader.Close(); } } } else { using (Stream responseStream = errorResponse.GetResponseStream()) { // Get the response stream. StreamReader reader = new StreamReader(responseStream); // Read the Stream errorString = reader.ReadToEnd(); // Close reader reader.Close(); } } // Log the error string to disk. CoreHelper.GetRequestLogging(this.context).LogPlatformRequests(errorString, false); } } Core.Rest.FaultHandler fault = new Core.Rest.FaultHandler(this.context); IdsException idsException = fault.ParseErrorResponseAndPrepareException(errorString); if (idsException != null) { throw new RetryExceededException(webException.Message, webException.Status.ToString(), webException.Source, idsException); } else if (webException != null) { throw new RetryExceededException(webException.Message, webException.Status.ToString(), webException.Source, webException); } throw new RetryExceededException(ex.Message, ex); } } // Perform an extra check in the delay interval. Should prevent from accidentally ending up with the value of -1 that will block a thread indefinitely. // In addition, any other negative numbers will cause an ArgumentOutOfRangeException fault that will be thrown by Thread.Sleep. if (delay.TotalMilliseconds < 0) { delay = TimeSpan.Zero; } this.OnRetrying(retryCount - 1, lastError, delay); if (retryCount > 2 && delay > TimeSpan.Zero) { Thread.Sleep(delay); } } }
//////////////////////////////// Implementation /////////////////////////////// /// <summary>Create a new UDP socket and start listening for packets.</summary> /// <param name="config">Host configuration values. If null, defaults are used.</param> /// <param name="listener">Host listener to use. If null, event based listener is created.</param> public Host(HostConfig config, IHostListener listener) { // Initialize Config = config ?? new HostConfig(); Listener = listener ?? new HostEvents(); Statistics = new HostStatistics(); Disposed = false; Allocator = new Allocator(Config); Random = new CryptoRandom(); Authenticator = new CryptoRSA(Allocator); Compressor = new CompressorDeflate(Allocator); Stopwatch = Stopwatch.StartNew(); Peers = new Dictionary <IPEndPoint, Peer>(new IPComparer()); PeersLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion); ReceiveSemaphore = new SemaphoreSlim(Config.ReceiveCount, Config.ReceiveCount); ReceiveCancel = new CancellationTokenSource(); // Import private key if (Config.PrivateKey != null) { Authenticator.ImportPrivateKey(Config.PrivateKey); } // Create socket if (Config.DualMode && SupportsIPv6) { Socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); } else { Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } // If true, socket is a dual-mode socket used for both IPv4 and IPv6 if (Socket.AddressFamily == AddressFamily.InterNetworkV6) { Socket.DualMode = true; } // If false and packet size + header is larger than MTU, fragment it // MTU on ethernet is 1500 bytes - 20 bytes for IP header - 8 bytes for UDP header if (Socket.AddressFamily == AddressFamily.InterNetwork && SocketSetDontFragment) { Socket.DontFragment = true; } // Disable SIO_UDP_CONNRESET // If enabled, and this socket sends a packet to a destination without a socket it returns ICMP port unreachable // When this is received, the next operation on the socket will throw a SocketException try { if (SocketSetSioUdpConnreset) { Socket.IOControl(-1744830452, new byte[] { 0 }, null); } } catch { } // If true, any call that doesnt complete immediately will wait until it finishes Socket.Blocking = false; // Allow broadcasts to 255.255.255.255 Socket.EnableBroadcast = Config.Broadcast; if (Socket.AddressFamily == AddressFamily.InterNetworkV6) { BroadcastAddress = Config.Broadcast ? IPAddress.Parse("FF02:0:0:0:0:0:0:1") : IPAddress.IPv6None; if (Config.Broadcast) { IPv6MulticastOption option = new IPv6MulticastOption(BroadcastAddress); Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, option); } } else { BroadcastAddress = Config.Broadcast ? IPAddress.Broadcast : IPAddress.None; } // If exclusive, Bind will fail if address is already in use try { Socket.ExclusiveAddressUse = true; Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false); } catch { if (SocketReuseAddressThrowException) { throw; } } // If true, this socket receives outgoing multicast packets Socket.MulticastLoopback = false; // Maximum number of router hops Socket.Ttl = Config.TTL; // Buffer sizes Socket.SendBufferSize = Config.SendBufferSize; Socket.ReceiveBufferSize = Config.ReceiveBufferSize; // The amount of time in milliseconds after which a synchronous Send/Receive call will time out, 0 for infinite Socket.ReceiveTimeout = 0; Socket.SendTimeout = 0; // Set to true if you intend to call DuplicateAndClose Socket.UseOnlyOverlappedIO = false; // Bind socket if (Socket.AddressFamily == AddressFamily.InterNetwork) { IPAddress bind = Config.BindAddress?.MapToIPv4() ?? IPAddress.Any; Socket.Bind(new IPEndPoint(bind, Config.Port)); } else { IPAddress bind = Config.BindAddress?.MapToIPv6() ?? IPAddress.IPv6Any; try { Socket.Bind(new IPEndPoint(bind, Config.Port)); } catch (SocketException exception) { switch (exception.SocketErrorCode) { case SocketError.AddressAlreadyInUse: try { Socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, true); Socket.Bind(new IPEndPoint(bind, Config.Port)); } catch { if (SocketBindThrowIpv6Rebind) { throw; } } break; case SocketError.AddressFamilyNotSupported: if (SocketBindThrowIpv6NotSupported) { throw; } break; default: throw; } } } // Start receive worker ReceiveWorker = Task.Factory.StartNew( ReceiveWorkerAsync, ReceiveCancel.Token, TaskCreationOptions.PreferFairness | TaskCreationOptions.LongRunning, TaskScheduler.Default ); }
/// <summary> /// Initializes a new instance of the <see cref="SqlServerCache"/> class with given /// settings and specified connection factory. /// </summary> /// <param name="settings">Cache settings.</param> /// <param name="connectionFactory">Cache connection factory.</param> /// <param name="serializer">The serializer.</param> /// <param name="compressor">The compressor.</param> /// <param name="clock">The clock.</param> /// <param name="random">The random number generator.</param> public SqlServerCache(SqlServerCacheSettings settings, SqlServerCacheConnectionFactory connectionFactory, ISerializer serializer = null, ICompressor compressor = null, IClock clock = null, IRandom random = null) : base(settings, connectionFactory, serializer, compressor, clock, random) { }
public StreamAsyncTests(ICompressor compressor) : base(compressor) { }
internal override async Task Compress(Func <IDictionary <string, object>, Task> next, OwinContext context, ICompressor compressor, Stream httpOutputStream) { using (var memoryStream = new MemoryStream()) { using (var compressedStream = compressor.CreateStream(memoryStream)) { context.Response.Body = compressedStream; await next.Invoke(context.Environment); } if (memoryStream.Length > 0) { SetResponseHeaders(context, compressor, memoryStream); memoryStream.Position = 0; await memoryStream.CopyToAsync(httpOutputStream, BufferSize); } } }
public BinaryTests(ICompressor compressor) : base(compressor) { }
public void Store(string fileName, ICompressor compressor, IFilter filter) { compressor.Compress(); filter.Apply(); }
public ImageStorage(IFilter filter, ICompressor compressor) { _filter = filter; _compressor = compressor; }
#pragma warning restore CC0022 // Should dispose object #endregion Default Instance /// <summary> /// Initializes a new instance of the <see cref="SqlServerCache"/> class with given settings. /// </summary> /// <param name="settings">Cache settings.</param> /// <param name="serializer">The serializer.</param> /// <param name="compressor">The compressor.</param> /// <param name="clock">The clock.</param> /// <param name="random">The random number generator.</param> public SqlServerCache(SqlServerCacheSettings settings, ISerializer serializer = null, ICompressor compressor = null, IClock clock = null, IRandom random = null) : this(settings, new SqlServerCacheConnectionFactory(settings), serializer, compressor, clock, random) { }
/** * <summary> * <para>Import the data from an ODS file.</para> * <para>See <see cref="ODSMem.ImportFile(FileInfo, ICompressor)"/> and <see cref="ODSFile.ImportFile(FileInfo, ICompressor)"/> * for the specifics.</para> * </summary> * * <param name="file">The file to import from.</param> * <param name="compressor">The compressor.</param> */ public void ImportFile(FileInfo file, ICompressor compressor) { odsInternal.ImportFile(file, compressor); }
public async Task Invoke(HttpContext context) { bool useMinification = _options.IsMinificationEnabled() && _minificationManagers.Count > 0; bool useCompression = _options.IsCompressionEnabled() && _compressionManager != null; if (!useMinification && !useCompression) { await _next.Invoke(context); return; } HttpRequest request = context.Request; HttpResponse response = context.Response; using (var cacheStream = new MemoryStream()) { Stream originalStream = response.Body; response.Body = cacheStream; await _next.Invoke(context); byte[] cacheBytes = cacheStream.ToArray(); int cacheSize = cacheBytes.Length; bool isProcessed = false; response.Body = originalStream; if (request.Method == "GET" && response.StatusCode == 200 && _options.IsAllowableResponseSize(cacheSize)) { string contentType = response.ContentType; string mediaType = null; Encoding encoding = Encoding.GetEncoding(0); if (contentType != null) { MediaTypeHeaderValue mediaTypeHeader; if (MediaTypeHeaderValue.TryParse(contentType, out mediaTypeHeader)) { mediaType = mediaTypeHeader.MediaType.ToLowerInvariant(); encoding = mediaTypeHeader.Encoding; } } string currentUrl = request.Path.Value; QueryString queryString = request.QueryString; if (queryString.HasValue) { currentUrl += queryString.Value; } string content = encoding.GetString(cacheBytes); string processedContent = content; Action <string, string> appendHttpHeader = (key, value) => { response.Headers.Append(key, new StringValues(value)); }; if (useMinification) { foreach (IMarkupMinificationManager minificationManager in _minificationManagers) { if (mediaType != null && minificationManager.IsSupportedMediaType(mediaType) && minificationManager.IsProcessablePage(currentUrl)) { IMarkupMinifier minifier = minificationManager.CreateMinifier(); MarkupMinificationResult minificationResult = minifier.Minify(processedContent, currentUrl, encoding, false); if (minificationResult.Errors.Count == 0) { processedContent = minificationResult.MinifiedContent; if (_options.IsPoweredByHttpHeadersEnabled()) { minificationManager.AppendPoweredByHttpHeader(appendHttpHeader); } isProcessed = true; } } if (isProcessed) { break; } } } if (useCompression && _compressionManager.IsSupportedMediaType(mediaType)) { string acceptEncoding = request.Headers["Accept-Encoding"]; ICompressor compressor = _compressionManager.CreateCompressor(acceptEncoding); Stream compressedStream = compressor.Compress(originalStream); compressor.AppendHttpHeaders(appendHttpHeader); using (var writer = new StreamWriter(compressedStream, encoding)) { await writer.WriteAsync(processedContent); } isProcessed = true; } else { if (isProcessed) { using (var writer = new StreamWriter(originalStream, encoding)) { await writer.WriteAsync(processedContent); } } } } if (!isProcessed) { cacheStream.Seek(0, SeekOrigin.Begin); await cacheStream.CopyToAsync(originalStream); } cacheStream.SetLength(0); } }
/** * <summary> * <para>Save the data to a file.</para> * <para>See <see cref="ODSMem.SaveToFile(FileInfo, ICompressor)"/> and <see cref="ODSFile.SaveToFile(FileInfo, ICompressor)"/> for * the specifics.</para> * </summary> * * <param name="file">The file.</param> * <param name="compressor">The compressor.</param> */ public void SaveToFile(FileInfo file, ICompressor compressor) { odsInternal.SaveToFile(file, compressor); }
public void ExecuteAction <TResult>(Action <AsyncCallback> beginAction, Func <IAsyncResult, TResult> endAction, Action <TResult> successHandler, Action <Exception> faultHandler) { IntuitRetryHelper.IsArgumentNull(beginAction, "beginAction"); IntuitRetryHelper.IsArgumentNull(endAction, "endAction"); IntuitRetryHelper.IsArgumentNull(successHandler, "successHandler"); IntuitRetryHelper.IsArgumentNull(faultHandler, "faultHandler"); int retryCount = 0; AsyncCallback endInvoke = null; Func <Action, bool> executeWithRetry = null; // Configure a custom callback delegate that invokes the end operation and the success handler if the operation succeedes endInvoke = ar => { var result = default(TResult); if (executeWithRetry(() => result = endAction(ar))) { successHandler(result); } }; // Utility delegate to invoke an action and implement the core retry logic // If the action succeeds (i.e. does not throw an exception) it returns true. // If the action throws, it analizes it for retries. If a retry is required, it restarts the async operation; otherwise, it invokes the fault handler. executeWithRetry = a => { try { // Invoke the callback delegate which can throw an exception if the main async operation has completed with a fault. a(); return(true); } catch (Exception ex) { // Capture the original exception for analysis. var lastError = ex; // Handling of RetryLimitExceededException needs to be done separately. This exception type indicates the application's intent to exit from the retry loop. if (lastError is RetryExceededException) { if (lastError.InnerException != null) { faultHandler(lastError.InnerException); return(false); } else { faultHandler(lastError); return(false); } } else { var delay = TimeSpan.Zero; // Check if we should continue retrying on this exception. If not, invoke the fault handler so that user code can take control. if (!IsTransient(lastError) || ((this.ExtendedRetryException != null) && this.ExtendedRetryException.IsRetryException(ex))) { faultHandler(lastError); return(false); } else { if (delay.TotalMilliseconds < 0) { delay = TimeSpan.Zero; } retryCount = retryCount + 1; if (!this.shouldRetry(retryCount, lastError, out delay)) { WebException webException = ex as WebException; string errorString = string.Empty; if (webException != null) { // If not null then check the response property of the webException object. if (webException.Response != null) { // There is a response from the Ids server. Cast it to HttpWebResponse. HttpWebResponse errorResponse = (HttpWebResponse)webException.Response; // Get the status code description of the error response. string statusCodeDescription = errorResponse.StatusCode.ToString(); // Get the status code of the error response. int statusCode = (int)errorResponse.StatusCode; ICompressor responseCompressor = CoreHelper.GetCompressor(this.context, false); if (!string.IsNullOrWhiteSpace(errorResponse.ContentEncoding) && responseCompressor != null) { using (var responseStream = errorResponse.GetResponseStream()) //Check for decompressing { using (var decompressedStream = responseCompressor.Decompress(responseStream)) { // Get the response stream. StreamReader reader = new StreamReader(decompressedStream); //StreamReader reader = new StreamReader(responseStream); // Read the Stream errorString = reader.ReadToEnd(); // Close reader reader.Close(); } } } else { using (Stream responseStream = errorResponse.GetResponseStream()) { // Get the response stream. StreamReader reader = new StreamReader(responseStream); // Read the Stream errorString = reader.ReadToEnd(); // Close reader reader.Close(); } } // Log the error string to disk. CoreHelper.GetRequestLogging(this.context).LogPlatformRequests(errorString, false); } } Core.Rest.FaultHandler fault = new Core.Rest.FaultHandler(this.context); IdsException idsException = fault.ParseErrorResponseAndPrepareException(errorString); if (idsException != null) { faultHandler(new RetryExceededException(webException.Message, webException.Status.ToString(), webException.Source, idsException)); return(false); } else if (webException != null) { faultHandler(new RetryExceededException(webException.Message, webException.Status.ToString(), webException.Source, webException)); return(false); } faultHandler(new RetryExceededException(ex.Message, ex)); return(false); } // Notify the respective subscribers about this exception. this.OnRetrying(retryCount, lastError, delay); // Sleep for the defined interval before repetitively executing the main async operation. if (retryCount > 2 && delay > TimeSpan.Zero) { Thread.Sleep(delay); } executeWithRetry(() => beginAction(endInvoke)); } } return(false); } }; // Invoke the the main async operation for the first time which should return control to the caller immediately. executeWithRetry(() => beginAction(endInvoke)); }
/** * <summary>Create ODS using the file storage type. Data will be written to and read from a file.</summary> * <param name="file">The file to use.</param> * <param name="compression">The compression that should be used.</param> */ public ObjectDataStructure(FileInfo file, ICompressor compression) { this.odsInternal = new ODSFile(file, compression); }
/// <summary> /// <see cref="CompressionContent(HttpContent, ICompressor)"/> /// </summary> public CompressedContent(HttpContent content, ICompressor compressor) : base(content, compressor) { }
/** * <summary>Create ODS using the memory storage type. Data will be written to and read from a memory stream.</summary> * <param name="data">Pre-existing data that should be inserted into the memory stream.</param> * <param name="compressor">The compression format the data uses.</param> */ public ObjectDataStructure(byte[] data, ICompressor compressor) { this.odsInternal = new ODSMem(data, compressor); }
public TestBase(ICompressor compressor) { Compressor = compressor; }
public static void CompressFile(string sourcePath, string destinationPath, ICompressor compressor = null) { singletonInstance.Instance.CompressFile(sourcePath, destinationPath); }
private void Initialize() { if (_wrapperInitializedFlag.Set()) { HttpRequest request = _context.Request; HttpResponse response = _context.Response; if (response.StatusCode == 200) { string httpMethod = request.Method; string contentType = response.ContentType; string mediaType = null; Encoding encoding = null; if (contentType != null) { MediaTypeHeaderValue mediaTypeHeader; if (MediaTypeHeaderValue.TryParse(contentType, out mediaTypeHeader)) { mediaType = mediaTypeHeader.MediaType #if ASPNETCORE2 .Value #endif .ToLowerInvariant() ; encoding = mediaTypeHeader.Encoding; } } string currentUrl = request.Path.Value; QueryString queryString = request.QueryString; if (queryString.HasValue) { currentUrl += queryString.Value; } IHeaderDictionary responseHeaders = response.Headers; bool isEncodedContent = responseHeaders.IsEncodedContent(); if (_minificationManagers.Count > 0) { foreach (IMarkupMinificationManager minificationManager in _minificationManagers) { if (minificationManager.IsSupportedHttpMethod(httpMethod) && mediaType != null && minificationManager.IsSupportedMediaType(mediaType) && minificationManager.IsProcessablePage(currentUrl)) { if (isEncodedContent) { throw new InvalidOperationException( string.Format( AspNetCommonStrings.MarkupMinificationIsNotApplicableToEncodedContent, responseHeaders[HeaderNames.ContentEncoding] ) ); } _currentMinificationManager = minificationManager; _cachedStream = new MemoryStream(); _minificationEnabled = true; break; } } } if (_compressionManager != null && !isEncodedContent && _compressionManager.IsSupportedHttpMethod(httpMethod) && _compressionManager.IsSupportedMediaType(mediaType) && _compressionManager.IsProcessablePage(currentUrl)) { string acceptEncoding = request.Headers[HeaderNames.AcceptEncoding]; ICompressor compressor = InitializeCurrentCompressor(acceptEncoding); if (compressor != null) { _compressionStream = compressor.Compress(_originalStream); _compressionEnabled = true; } } _currentUrl = currentUrl; _encoding = encoding; } } }
/// <summary> /// Parses the Response and throws appropriate exceptions. /// </summary> /// <param name="webException">Web Exception.</param> /// <param name="isIps">Specifies whether the exception is generated by an IPS call.</param> /// <returns>Ids Exception.</returns> internal IdsException ParseResponseAndThrowException(WebException webException, bool isIps = false) { IdsException idsException = null; // Checks whether the webException is null or not. if (webException != null) { // If not null then check the response property of the webException object. if (webException.Response != null) { // There is a response from the Ids server. Cast it to HttpWebResponse. HttpWebResponse errorResponse = (HttpWebResponse)webException.Response; // Get the status code description of the error response. string statusCodeDescription = errorResponse.StatusCode.ToString(); // Get the status code of the error response. int statusCode = (int)errorResponse.StatusCode; string errorString = string.Empty; ICompressor responseCompressor = CoreHelper.GetCompressor(this.context, false); if (!string.IsNullOrWhiteSpace(errorResponse.ContentEncoding) && responseCompressor != null) { using (var responseStream = errorResponse.GetResponseStream()) { using (var decompressedStream = responseCompressor.Decompress(responseStream)) { // Get the response stream. StreamReader reader = new StreamReader(decompressedStream); // Read the Stream errorString = reader.ReadToEnd(); // Close reader reader.Close(); } } } else { using (Stream responseStream = errorResponse.GetResponseStream()) { // Get the response stream. StreamReader reader = new StreamReader(responseStream); // Read the Stream errorString = reader.ReadToEnd(); // Close reader reader.Close(); } } string response_intuit_tid_header = ""; //get intuit_tid header for (int i = 0; i < errorResponse.Headers.Count; ++i) { if (errorResponse.Headers.Keys[i] == "intuit_tid") { response_intuit_tid_header = errorResponse.Headers[i]; } } //Log errorstring to disk CoreHelper.GetRequestLogging(this.context).LogPlatformRequests(" Response Intuit_Tid header: " + response_intuit_tid_header + ", Response Payload: " + errorString, false); if (isIps) { IdsException exception = new IdsException(errorString, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source); this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Error, exception.ToString()); return(exception); } // Use the above idsException to set to innerException of the specific exception which will be created below. // Ids will set the following error codes. Depending on that we will be throwing specific exceptions. switch (errorResponse.StatusCode) { // Bad Request: 400 case HttpStatusCode.BadRequest: // Parse the error response and create the aggregate exception. idsException = this.ParseErrorResponseAndPrepareException(errorString); idsException = new IdsException(statusCodeDescription, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source, idsException); break; // Unauthorized: 401 case HttpStatusCode.Unauthorized: // Create Invalid Token Exception. idsException = this.ParseErrorResponseAndPrepareException(errorString); InvalidTokenException invalidTokenException = new InvalidTokenException(string.Format(CultureInfo.InvariantCulture, "{0}-{1}", statusCodeDescription, statusCode), idsException); idsException = invalidTokenException; break; // ServiceUnavailable: 503 case HttpStatusCode.ServiceUnavailable: // InternalServerError: 500 case HttpStatusCode.InternalServerError: // Forbidden: 403 case HttpStatusCode.Forbidden: // NotFound: 404 case HttpStatusCode.NotFound: idsException = new IdsException(statusCodeDescription, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source, new EndpointNotFoundException()); break; // Throttle Exceeded: 429 case (HttpStatusCode)429: idsException = new IdsException(statusCodeDescription, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source, new ThrottleExceededException()); break; // Default. Throw generic exception i.e. IdsException. default: // Parse the error response and create the aggregate exception. // TODO: Do we need to give error string in exception also. If so then uncomemnt the below line. // idsException = new IdsException(errorString, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source); idsException = new IdsException(statusCodeDescription, statusCode.ToString(CultureInfo.InvariantCulture), webException.Source); break; } } } // Return the Ids Exception. return(idsException); }