public override void Claim(FileSystemCommandConnection connection) { if (!connection.Socket.RemoteEndPoint.Equals(this.OwnerEndPoint)) { connection.WriteError(ErrorCodes.INVALID_PARAM, "ticket"); connection.RunLevel = DisconnectedRunLevel.Default; return; } var src = this.file.GetContent().GetInputStream(this.fileShare); if (this.offset > 0 || this.length != -1) { src = new PartialStream(src, this.offset, this.length); } connection.WriteOk("length", src.Length.ToString(CultureInfo.InvariantCulture)); connection.Flush(); var des = connection.WriteStream; var pump = new StreamCopier(src, des); pump.Run(); connection.RunLevel = DisconnectedRunLevel.Default; }
public override void Claim(FileSystemCommandConnection connection) { if (!connection.Socket.RemoteEndPoint.Equals(this.OwnerEndPoint)) { connection.WriteError(ErrorCodes.INVALID_PARAM, "ticket"); connection.RunLevel = DisconnectedRunLevel.Default; return; } var src = this.file.GetContent().GetInputStream(this.fileShare); if (this.offset > 0 || this.length != -1) { src = new PartialStream(src, this.offset, this.length); } connection.WriteOk("length", src.Length.ToString(CultureInfo.InvariantCulture)); connection.Flush(); var des = connection.WriteStream; var pump = new StreamCopier(src, des); pump.Run(); connection.RunLevel = DisconnectedRunLevel.Default; }
public async Task SlowStreams_TelemetryReportsCorrectTime(bool isRequest) { var events = TestEventListener.Collect(); const int SourceSize = 3; var sourceBytes = new byte[SourceSize]; var source = new MemoryStream(sourceBytes); var destination = new MemoryStream(); var clock = new ManualClock(); var sourceWaitTime = TimeSpan.FromMilliseconds(12345); var destinationWaitTime = TimeSpan.FromMilliseconds(42); await StreamCopier.CopyAsync( isRequest, new SlowStream(source, clock, sourceWaitTime), new SlowStream(destination, clock, destinationWaitTime), clock, CancellationToken.None); Assert.Equal(sourceBytes, destination.ToArray()); AssertContentTransferred(events, isRequest, SourceSize, iops: SourceSize + 1, firstReadTime: sourceWaitTime, readTime: (SourceSize + 1) * sourceWaitTime, writeTime: SourceSize * destinationWaitTime); }
public async Task DestinationThrows_Reported(bool isRequest) { var events = TestEventListener.Collect(); const int SourceSize = 10; const int BytesPerRead = 3; var clock = new ManualClock(); var sourceWaitTime = TimeSpan.FromMilliseconds(12345); var destinationWaitTime = TimeSpan.FromMilliseconds(42); var source = new SlowStream(new MemoryStream(new byte[SourceSize]), clock, sourceWaitTime) { MaxBytesPerRead = BytesPerRead }; var destination = new SlowStream(new ThrowStream(), clock, destinationWaitTime); var(result, error) = await StreamCopier.CopyAsync(isRequest, source, destination, clock, CancellationToken.None); Assert.Equal(StreamCopyResult.OutputError, result); Assert.IsAssignableFrom <IOException>(error); AssertContentTransferred(events, isRequest, contentLength: BytesPerRead, iops: 1, firstReadTime: sourceWaitTime, readTime: sourceWaitTime, writeTime: destinationWaitTime); }
public async Task Cancelled_Reported() { var source = new MemoryStream(new byte[10]); var destination = new MemoryStream(); var(result, error) = await StreamCopier.CopyAsync(source, destination, new CancellationToken(canceled : true)); Assert.Equal(StreamCopyResult.Canceled, result); Assert.IsAssignableFrom <OperationCanceledException>(error); }
public async Task DestinationThrows_Reported() { var source = new MemoryStream(new byte[10]); var destination = new ThrowStream(); var(result, error) = await StreamCopier.CopyAsync(source, destination, CancellationToken.None); Assert.Equal(StreamCopyResult.OutputError, result); Assert.IsAssignableFrom <IOException>(error); }
public async Task CopyAsync_Works() { const int SourceSize = (128 * 1024) - 3; var sourceBytes = Enumerable.Range(0, SourceSize).Select(i => (byte)(i % 256)).ToArray(); var source = new MemoryStream(sourceBytes); var destination = new MemoryStream(); await StreamCopier.CopyAsync(source, destination, CancellationToken.None); Assert.Equal(sourceBytes, destination.ToArray()); }
public void Download(Stream destination) { using (var response = Request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { var streamCopier = new StreamCopier(_bufferSize); streamCopier.Length = response.ContentLength; streamCopier.StartPosition = SourceStartPosition.Current; streamCopier.Copy(responseStream, destination); } } }
public async Task CopyAsync_Works(bool isRequest) { var events = TestEventListener.Collect(); const int SourceSize = (128 * 1024) - 3; var sourceBytes = Enumerable.Range(0, SourceSize).Select(i => (byte)(i % 256)).ToArray(); var source = new MemoryStream(sourceBytes); var destination = new MemoryStream(); await StreamCopier.CopyAsync(isRequest, source, destination, new Clock(), CancellationToken.None); Assert.Equal(sourceBytes, destination.ToArray()); AssertContentTransferred(events, isRequest, SourceSize); }
private async ValueTask <(StreamCopyResult, Exception?)> CopyResponseBodyAsync(HttpContent destinationResponseContent, Stream clientResponseStream, ActivityCancellationTokenSource activityCancellationSource) { // SocketHttpHandler and similar transports always provide an HttpContent object, even if it's empty. // In 3.1 this is only likely to return null in tests. // As of 5.0 HttpResponse.Content never returns null. // https://github.com/dotnet/runtime/blame/8fc68f626a11d646109a758cb0fc70a0aa7826f1/src/libraries/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs#L46 if (destinationResponseContent != null) { using var destinationResponseStream = await destinationResponseContent.ReadAsStreamAsync(); // The response content-length is enforced by the server. return(await StreamCopier.CopyAsync(isRequest : false, destinationResponseStream, clientResponseStream, StreamCopier.UnknownLength, _clock, activityCancellationSource, activityCancellationSource.Token)); } return(StreamCopyResult.Success, null); }
public async Task CopyAsync_Works(bool isRequest) { var events = TestEventListener.Collect(); const int SourceSize = (128 * 1024) - 3; var sourceBytes = Enumerable.Range(0, SourceSize).Select(i => (byte)(i % 256)).ToArray(); var source = new MemoryStream(sourceBytes); var destination = new MemoryStream(); using var cts = ActivityCancellationTokenSource.Rent(TimeSpan.FromSeconds(10), CancellationToken.None); await StreamCopier.CopyAsync(isRequest, source, destination, SourceSize, new Clock(), cts, cts.Token); Assert.False(cts.Token.IsCancellationRequested); Assert.Equal(sourceBytes, destination.ToArray()); AssertContentTransferred(events, isRequest, SourceSize); }
public async Task Cancelled_Reported(bool isRequest) { var events = TestEventListener.Collect(); var source = new MemoryStream(new byte[10]); var destination = new MemoryStream(); var(result, error) = await StreamCopier.CopyAsync(isRequest, source, destination, new Clock(), new CancellationToken(canceled : true)); Assert.Equal(StreamCopyResult.Canceled, result); Assert.IsAssignableFrom <OperationCanceledException>(error); AssertContentTransferred(events, isRequest, contentLength: 0, iops: 0, firstReadTime: TimeSpan.Zero, readTime: TimeSpan.Zero, writeTime: TimeSpan.Zero); }
public async Task Cancelled_Reported(bool isRequest) { var events = TestEventListener.Collect(); var source = new MemoryStream(new byte[10]); var destination = new MemoryStream(); using var cts = ActivityCancellationTokenSource.Rent(TimeSpan.FromSeconds(10), CancellationToken.None); cts.Cancel(); var(result, error) = await StreamCopier.CopyAsync(isRequest, source, destination, StreamCopier.UnknownLength, new ManualClock(), cts, cts.Token); Assert.Equal(StreamCopyResult.Canceled, result); Assert.IsAssignableFrom <OperationCanceledException>(error); AssertContentTransferred(events, isRequest, contentLength: 0, iops: 1, firstReadTime: TimeSpan.Zero, readTime: TimeSpan.Zero, writeTime: TimeSpan.Zero); }
public async Task SourceThrows_Reported(bool isRequest) { var events = TestEventListener.Collect(); var clock = new ManualClock(); var sourceWaitTime = TimeSpan.FromMilliseconds(12345); var source = new SlowStream(new ThrowStream(), clock, sourceWaitTime); var destination = new MemoryStream(); using var cts = new CancellationTokenSource(); var(result, error) = await StreamCopier.CopyAsync(isRequest, source, destination, clock, cts, TimeSpan.FromSeconds(10)); Assert.Equal(StreamCopyResult.InputError, result); Assert.IsAssignableFrom <IOException>(error); AssertContentTransferred(events, isRequest, contentLength: 0, iops: 1, firstReadTime: sourceWaitTime, readTime: sourceWaitTime, writeTime: TimeSpan.Zero); }
public static void extract(string dirName, string outPath) { if (CommandFindRomsInGame == null) { CommandFindRomsInGame = new SQLiteCommand( @"SELECT ROM.RomId, ROM.name, FILES.size, FILES.compressedsize, FILES.crc, FILES.sha1 FROM ROM,FILES WHERE ROM.FileId=FILES.FileId AND ROM.GameId=@GameId AND ROM.PutInZip ORDER BY ROM.RomId", DBSqlite.db.Connection); } CommandFindRomsInGame.Parameters.Add(new SQLiteParameter("GameId")); Debug.WriteLine(dirName); SQLiteCommand getfiles = new SQLiteCommand(@"SELECT dir.FullName,GameId,game.Name FROM dir,dat,game where dat.dirid=dir.dirid and game.datid=dat.datid and dir.fullname like '" + dirName + "%'", DBSqlite.db.Connection); DbDataReader reader = getfiles.ExecuteReader(); while (reader.Read()) { string outputFile = reader["fullname"].ToString() + reader["Name"].ToString() + ".zip"; outputFile = outputFile.Substring(dirName.Length); outputFile = Path.Combine(outPath, outputFile).Replace(@"/", @"\"); Debug.WriteLine(outputFile); int GameId = Convert.ToInt32(reader["GameId"]); string GameName = reader["name"].ToString(); Debug.WriteLine("Game " + GameId + " Name: " + GameName); Zip memZip = new Zip(); memZip.ZipCreateFake(); ulong fileOffset = 0; Stream zipFs = null; int romCount = 0; using (DbDataReader drRom = ZipSetGetRomsInGame(GameId)) { while (drRom.Read()) { int RomId = Convert.ToInt32(drRom["RomId"]); string RomName = drRom["name"].ToString(); ulong size = Convert.ToUInt64(drRom["size"]); ulong compressedSize = Convert.ToUInt64(drRom["compressedsize"]); byte[] CRC = VarFix.CleanMD5SHA1(drRom["crc"].ToString(), 8); byte[] sha1 = VarFix.CleanMD5SHA1(drRom["sha1"].ToString(), 32); Debug.WriteLine(" Rom " + RomId + " Name: " + RomName + " Size: " + size + " Compressed: " + compressedSize + " CRC: " + VarFix.ToString(CRC)); byte[] localHeader; memZip.ZipFileAddFake(RomName, fileOffset, size, compressedSize, CRC, out localHeader); //ZipSetLocalFileHeader(RomId, localHeader, fileOffset); if (romCount == 0) { CompressUtils.CreateDirForFile(outputFile); int errorCode = FileStream.OpenFileWrite(outputFile, out zipFs); } zipFs.Write(localHeader, 0, localHeader.Length); gZip GZip = new gZip(); string strFilename = RomRootDir.Getfilename(sha1); GZip.ZipFileOpen(strFilename, -1, true); GZip.ZipFileOpenReadStream(0, true, out Stream oStr, out ulong _); StreamCopier.StreamCopy(oStr, zipFs, compressedSize); GZip.ZipFileCloseReadStream(); GZip.ZipFileClose(); fileOffset += (ulong)localHeader.Length + compressedSize; zipFs.Position = (long)fileOffset; romCount += 1; } } memZip.ZipFileCloseFake(fileOffset, out byte[] centeralDir); if (romCount > 0) { zipFs.Write(centeralDir, 0, centeralDir.Length); zipFs.Flush(); zipFs.Close(); zipFs.Dispose(); } } }
public async Task LongContentTransfer_TelemetryReportsTransferringEvents(bool isRequest) { var events = TestEventListener.Collect(); const int SourceSize = 123; var sourceBytes = new byte[SourceSize]; var source = new MemoryStream(sourceBytes); var destination = new MemoryStream(); var clock = new ManualClock(); var sourceWaitTime = TimeSpan.FromMilliseconds(789); // Every second read triggers ContentTransferring var destinationWaitTime = TimeSpan.FromMilliseconds(42); const int BytesPerRead = 3; var contentReads = (int)Math.Ceiling((double)SourceSize / BytesPerRead); await StreamCopier.CopyAsync( isRequest, new SlowStream(source, clock, sourceWaitTime) { MaxBytesPerRead = BytesPerRead }, new SlowStream(destination, clock, destinationWaitTime), clock, CancellationToken.None); Assert.Equal(sourceBytes, destination.ToArray()); AssertContentTransferred(events, isRequest, SourceSize, iops: contentReads + 1, firstReadTime: sourceWaitTime, readTime: (contentReads + 1) * sourceWaitTime, writeTime: contentReads * destinationWaitTime); var transferringEvents = events.Where(e => e.EventName == "ContentTransferring").ToArray(); Assert.Equal(contentReads / 2, transferringEvents.Length); for (var i = 0; i < transferringEvents.Length; i++) { var payload = transferringEvents[i].Payload; Assert.Equal(5, payload.Count); Assert.Equal(isRequest, (bool)payload[0]); var contentLength = (long)payload[1]; var iops = (long)payload[2]; Assert.Equal((i + 1) * 2, iops); if (contentLength % BytesPerRead == 0) { Assert.Equal(iops * BytesPerRead, contentLength); } else { Assert.Equal(transferringEvents.Length - 1, i); Assert.Equal(SourceSize, contentLength); } var readTime = new TimeSpan((long)payload[3]); Assert.Equal(iops * sourceWaitTime, readTime, new ApproximateTimeSpanComparer()); var writeTime = new TimeSpan((long)payload[4]); Assert.Equal(iops * destinationWaitTime, writeTime, new ApproximateTimeSpanComparer()); } }
public override void DoRun() { IFile destinationTemp; Stream destinationTempStream; string sourceHash; try { lock (this) { SetTransferState(TransferState.Preparing); } Action <IFile> transferAttributes = delegate(IFile dest) { using (dest.Attributes.AquireUpdateContext()) { foreach (string s in this.serviceType.AttributesToTransfer) { dest.Attributes[s] = this.source.Attributes[s]; } } }; Stream sourceStream = null; for (var i = 0; i < 4; i++) { try { sourceStream = this.OperatingNode.GetContent().GetInputStream(FileMode.Open, FileShare.Read); break; } catch (NodeNotFoundException) { throw; } catch (Exception) { if (i == 3) { throw; } } ProcessTaskStateRequest(); } using (sourceStream) { var sourceHashingService = (IHashingService)this.OperatingNode.GetService(new StreamHashingServiceType(sourceStream, this.HashAlgorithmName)); // Compute the hash of the source file SetTransferState(TransferState.Comparing); ProcessTaskStateRequest(); sourceHash = sourceHashingService.ComputeHash().TextValue; // Try to open the destination file ProcessTaskStateRequest(); var destinationHashingService = (IHashingService)this.TargetNode.GetService(new FileHashingServiceType(this.HashAlgorithmName)); string destinationHash; try { destinationHash = destinationHashingService.ComputeHash().TextValue; } catch (DirectoryNodeNotFoundException) { this.TargetNode.ParentDirectory.Create(true); try { destinationHash = destinationHashingService.ComputeHash().TextValue; } catch (NodeNotFoundException) { destinationHash = null; } } catch (NodeNotFoundException) { destinationHash = null; } ProcessTaskStateRequest(); // Source and destination are identical if (sourceHash == destinationHash) { SetTransferState(TransferState.Transferring); this.progress.RaiseValueChanged(0, GetBytesToTransfer()); SetTransferState(TransferState.Tidying); // Transfer attributes try { transferAttributes((IFile)this.TargetNode); } catch (FileNotFoundException) { } // Done SetTransferState(TransferState.Finished); ProcessTaskStateRequest(); return; } // Get a temp file for the destination based on the source's hash destinationTemp = ((ITempIdentityFileService)this.destination.GetService(new TempIdentityFileServiceType(sourceHash))).GetTempFile(); // Get the stream for the destination temp file try { if (!destinationTemp.ParentDirectory.Exists) { destinationTemp.ParentDirectory.Create(true); } } catch (IOException) { } using (destinationTempStream = destinationTemp.GetContent().OpenStream(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { Action finishUp = delegate { SetTransferState(TransferState.Tidying); destinationTempStream.Close(); for (int i = 0; i < 4; i++) { try { // Save hash value StandardFileHashingService.SaveHashToCache((IFile)destinationTemp, this.HashAlgorithmName, sourceHash, (IFile)this.TargetNode); try { // Transfer attributes transferAttributes(destinationTemp); } catch (FileNotFoundException e) { Console.WriteLine(e); } // Move destination temp to destination destinationTemp.MoveTo(this.TargetNode, true); break; } catch (Exception) { if (i == 3) { throw; } } ProcessTaskStateRequest(); } // Done SetTransferState(TransferState.Finished); ProcessTaskStateRequest(); }; // Get the hash for the destination temp file var destinationTempHashingService = (IHashingService)destinationTemp.GetService(new StreamHashingServiceType(destinationTempStream)); // If the destination temp and the source aren't the same // then complete the destination temp string destinationTempHash; if (destinationTempStream.Length >= sourceStream.Length) { // Destination is longer than source but starts source (unlikely) destinationTempHash = destinationTempHashingService.ComputeHash(0, sourceStream.Length).TextValue; if (destinationTempHash == sourceHash) { if (destinationTempStream.Length != sourceStream.Length) { destinationTempStream.SetLength(sourceStream.Length); } finishUp(); return; } destinationTempStream.SetLength(0); } if (destinationTempStream.Length > 0) { destinationTempHash = destinationTempHashingService.ComputeHash().TextValue; // Destination shorter than the source but is a partial copy of source sourceHash = sourceHashingService.ComputeHash(0, destinationTempStream.Length).TextValue; if (sourceHash == destinationTempHash) { this.offset = destinationTempStream.Length; } else { this.offset = 0; destinationTempStream.SetLength(0); } } else { this.offset = 0; } this.progress.RaiseValueChanged(0, this.offset); // Transfer over the remaining part needed (or everything if offset is 0) this.offset = destinationTempStream.Length; Stream sourcePartialStream = new PartialStream(sourceStream, destinationTempStream.Length); Stream destinationTempPartialStream = new PartialStream(destinationTempStream, destinationTempStream.Length); this.copier = new StreamCopier(new BufferedStream(sourcePartialStream, this.serviceType.BufferSize), destinationTempPartialStream, false, false, this.serviceType.ChunkSize); this.copier.TaskStateChanged += delegate(object sender, TaskEventArgs eventArgs) { if (eventArgs.TaskState == TaskState.Running || eventArgs.TaskState == TaskState.Paused || eventArgs.TaskState == TaskState.Stopped) { SetTaskState(eventArgs.TaskState); } }; SetTransferState(TransferState.Transferring); ProcessTaskStateRequest(); this.copier.Run(); if (this.copier.TaskState == TaskState.Stopped) { throw new StopRequestedException(); } finishUp(); } } } catch (StopRequestedException) { } finally { if (this.TransferState != TransferState.Finished) { SetTransferState(TransferState.Stopped); } } }
/// <summary> /// /// </summary> /// <param name="requestUri"></param> /// <param name="authParameters"></param> /// <param name="httpMethod"></param> /// <param name="contentType"></param> /// <param name="bodyStream"></param> /// <returns></returns> protected virtual HttpWebRequest CreateRequest(Uri requestUri, OAuthParameters authParameters, string httpMethod, string contentType, System.IO.Stream bodyStream) { NameValueCollection requestSpecificParameters = new NameValueCollection(authParameters.AdditionalParameters); if (!this.Service.UseAuthorizationHeader) { ////The OAuth params need to be added either into the querystring or into the post body. requestSpecificParameters.Add(authParameters.OAuthRequestParams()); } if (Constants.HttpPostUrlEncodedContentTypeRegex.IsMatch(contentType) && bodyStream == null) { ////All the requestSpecificParameters need to be encoded into the body bytes string body = Rfc3986.EncodeAndJoin(requestSpecificParameters); bodyStream = new MemoryStream(Encoding.ASCII.GetBytes(body)); } else { ////They go into the querystring. string query = Rfc3986.EncodeAndJoin(requestSpecificParameters); if (!string.IsNullOrEmpty(query)) { UriBuilder mutableRequestUri = new UriBuilder(requestUri); if (string.IsNullOrEmpty(mutableRequestUri.Query)) { mutableRequestUri.Query = query; } else { mutableRequestUri.Query = mutableRequestUri.Query.Substring(1) + "&" + query; } requestUri = mutableRequestUri.Uri; } } HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUri); request.Method = httpMethod; if (this.Service.UseAuthorizationHeader) { request.Headers.Add(HttpRequestHeader.Authorization, authParameters.ToHeaderFormat()); } if (!String.IsNullOrEmpty(contentType)) { request.ContentType = contentType; if (bodyStream != null) { if (bodyStream.CanSeek) { request.ContentLength = bodyStream.Length; } StreamCopier.CopyTo(bodyStream, request.GetRequestStream()); } } return(request); }
private async ValueTask <ForwarderError> HandleUpgradedResponse(HttpContext context, HttpResponseMessage destinationResponse, ActivityCancellationTokenSource activityCancellationSource) { ForwarderTelemetry.Log.ForwarderStage(ForwarderStage.ResponseUpgrade); // SocketHttpHandler and similar transports always provide an HttpContent object, even if it's empty. // Note as of 5.0 HttpResponse.Content never returns null. // https://github.com/dotnet/runtime/blame/8fc68f626a11d646109a758cb0fc70a0aa7826f1/src/libraries/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs#L46 if (destinationResponse.Content == null) { throw new InvalidOperationException("A response content is required for upgrades."); } // :: Step 7-A-1: Upgrade the client channel. This will also send response headers. var upgradeFeature = context.Features.Get <IHttpUpgradeFeature>(); if (upgradeFeature == null) { var ex = new InvalidOperationException("Invalid 101 response when upgrades aren't supported."); destinationResponse.Dispose(); context.Response.StatusCode = StatusCodes.Status502BadGateway; ReportProxyError(context, ForwarderError.UpgradeResponseDestination, ex); return(ForwarderError.UpgradeResponseDestination); } RestoreUpgradeHeaders(context, destinationResponse); Stream upgradeResult; try { upgradeResult = await upgradeFeature.UpgradeAsync(); } catch (Exception ex) { destinationResponse.Dispose(); ReportProxyError(context, ForwarderError.UpgradeResponseClient, ex); return(ForwarderError.UpgradeResponseClient); } using var clientStream = upgradeResult; // :: Step 7-A-2: Copy duplex streams using var destinationStream = await destinationResponse.Content.ReadAsStreamAsync(); var requestTask = StreamCopier.CopyAsync(isRequest: true, clientStream, destinationStream, StreamCopier.UnknownLength, _clock, activityCancellationSource, activityCancellationSource.Token).AsTask(); var responseTask = StreamCopier.CopyAsync(isRequest: false, destinationStream, clientStream, StreamCopier.UnknownLength, _clock, activityCancellationSource, activityCancellationSource.Token).AsTask(); // Make sure we report the first failure. var firstTask = await Task.WhenAny(requestTask, responseTask); var requestFinishedFirst = firstTask == requestTask; var secondTask = requestFinishedFirst ? responseTask : requestTask; ForwarderError error; var(firstResult, firstException) = await firstTask; if (firstResult != StreamCopyResult.Success) { error = ReportResult(context, requestFinishedFirst, firstResult, firstException); // Cancel the other direction activityCancellationSource.Cancel(); // Wait for this to finish before exiting so the resources get cleaned up properly. await secondTask; } else { var(secondResult, secondException) = await secondTask; if (secondResult != StreamCopyResult.Success) { error = ReportResult(context, !requestFinishedFirst, secondResult, secondException !); } else { error = ForwarderError.None; } } return(error); ForwarderError ReportResult(HttpContext context, bool reqeuest, StreamCopyResult result, Exception exception) { var error = result switch { StreamCopyResult.InputError => reqeuest ? ForwarderError.UpgradeRequestClient : ForwarderError.UpgradeResponseDestination, StreamCopyResult.OutputError => reqeuest ? ForwarderError.UpgradeRequestDestination : ForwarderError.UpgradeResponseClient, StreamCopyResult.Canceled => reqeuest ? ForwarderError.UpgradeRequestCanceled : ForwarderError.UpgradeResponseCanceled, _ => throw new NotImplementedException(result.ToString()), }; ReportProxyError(context, error, exception); return(error); } }
protected Meter(StreamCopier copier) { this.copier = copier; }
public BytesWrittenMeter(StreamCopier copier) : base(copier) { }
public BytesReadMeter(StreamCopier copier) : base(copier) { }
/// <summary> /// Read the attachment's content from the input stream /// </summary> /// <remarks>Set the ContentType and Filename properties before calling this method</remarks> /// <param name="input">Stream to read the content from</param> public void ReadFrom(Stream input) { using (Stream output = GetWriteStream()) StreamCopier.CopyStream(input, output); CommitWriteStream(ContentType); }
/// <summary> /// Write this attachment's content to the output stream /// </summary> /// <param name="output">Stream to write the content to</param> public void WriteTo(Stream output) { using (Stream input = GetReadStream()) StreamCopier.CopyStream(input, output); }
public override void DoRun() { IFile destinationTemp; Stream destinationTempStream; string sourceHash; try { lock (this) { SetTransferState(TransferState.Preparing); } Action<IFile> transferAttributes = delegate(IFile dest) { using (dest.Attributes.AquireUpdateContext()) { foreach (string s in this.serviceType.AttributesToTransfer) { dest.Attributes[s] = this.source.Attributes[s]; } } }; Stream sourceStream = null; for (var i = 0; i < 4; i++) { try { sourceStream = this.OperatingNode.GetContent().GetInputStream(FileMode.Open, FileShare.Read); break; } catch (NodeNotFoundException) { throw; } catch (Exception) { if (i == 3) { throw; } } ProcessTaskStateRequest(); } using (sourceStream) { var sourceHashingService = (IHashingService)this.OperatingNode.GetService(new StreamHashingServiceType(sourceStream, this.HashAlgorithmName)); // Compute the hash of the source file SetTransferState(TransferState.Comparing); ProcessTaskStateRequest(); sourceHash = sourceHashingService.ComputeHash().TextValue; // Try to open the destination file ProcessTaskStateRequest(); var destinationHashingService = (IHashingService)this.TargetNode.GetService(new FileHashingServiceType(this.HashAlgorithmName)); string destinationHash; try { destinationHash = destinationHashingService.ComputeHash().TextValue; } catch (DirectoryNodeNotFoundException) { this.TargetNode.ParentDirectory.Create(true); try { destinationHash = destinationHashingService.ComputeHash().TextValue; } catch (NodeNotFoundException) { destinationHash = null; } } catch (NodeNotFoundException) { destinationHash = null; } ProcessTaskStateRequest(); // Source and destination are identical if (sourceHash == destinationHash) { SetTransferState(TransferState.Transferring); this.progress.RaiseValueChanged(0, GetBytesToTransfer()); SetTransferState(TransferState.Tidying); // Transfer attributes try { transferAttributes((IFile) this.TargetNode); } catch (FileNotFoundException) { } // Done SetTransferState(TransferState.Finished); ProcessTaskStateRequest(); return; } // Get a temp file for the destination based on the source's hash destinationTemp = ((ITempIdentityFileService)this.destination.GetService(new TempIdentityFileServiceType(sourceHash))).GetTempFile(); // Get the stream for the destination temp file try { if (!destinationTemp.ParentDirectory.Exists) { destinationTemp.ParentDirectory.Create(true); } } catch (IOException) { } using (destinationTempStream = destinationTemp.GetContent().OpenStream(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)) { Action finishUp = delegate { SetTransferState(TransferState.Tidying); destinationTempStream.Close(); for (int i = 0; i < 4; i++) { try { // Save hash value StandardFileHashingService.SaveHashToCache((IFile) destinationTemp, this.HashAlgorithmName, sourceHash, (IFile) this.TargetNode); try { // Transfer attributes transferAttributes(destinationTemp); } catch (FileNotFoundException e) { Console.WriteLine(e); } // Move destination temp to destination destinationTemp.MoveTo(this.TargetNode, true); break; } catch (Exception) { if (i == 3) { throw; } } ProcessTaskStateRequest(); } // Done SetTransferState(TransferState.Finished); ProcessTaskStateRequest(); }; // Get the hash for the destination temp file var destinationTempHashingService = (IHashingService) destinationTemp.GetService(new StreamHashingServiceType(destinationTempStream)); // If the destination temp and the source aren't the same // then complete the destination temp string destinationTempHash; if (destinationTempStream.Length >= sourceStream.Length) { // Destination is longer than source but starts source (unlikely) destinationTempHash = destinationTempHashingService.ComputeHash(0, sourceStream.Length).TextValue; if (destinationTempHash == sourceHash) { if (destinationTempStream.Length != sourceStream.Length) { destinationTempStream.SetLength(sourceStream.Length); } finishUp(); return; } destinationTempStream.SetLength(0); } if (destinationTempStream.Length > 0) { destinationTempHash = destinationTempHashingService.ComputeHash().TextValue; // Destination shorter than the source but is a partial copy of source sourceHash = sourceHashingService.ComputeHash(0, destinationTempStream.Length).TextValue; if (sourceHash == destinationTempHash) { this.offset = destinationTempStream.Length; } else { this.offset = 0; destinationTempStream.SetLength(0); } } else { this.offset = 0; } this.progress.RaiseValueChanged(0, this.offset); // Transfer over the remaining part needed (or everything if offset is 0) this.offset = destinationTempStream.Length; Stream sourcePartialStream = new PartialStream(sourceStream, destinationTempStream.Length); Stream destinationTempPartialStream = new PartialStream(destinationTempStream, destinationTempStream.Length); this.copier = new StreamCopier(new BufferedStream(sourcePartialStream, this.serviceType.BufferSize), destinationTempPartialStream, false, false, this.serviceType.ChunkSize); this.copier.TaskStateChanged += delegate(object sender, TaskEventArgs eventArgs) { if (eventArgs.TaskState == TaskState.Running || eventArgs.TaskState == TaskState.Paused || eventArgs.TaskState == TaskState.Stopped) { SetTaskState(eventArgs.TaskState); } }; SetTransferState(TransferState.Transferring); ProcessTaskStateRequest(); this.copier.Run(); if (this.copier.TaskState == TaskState.Stopped) { throw new StopRequestedException(); } finishUp(); } } } catch (StopRequestedException) { } finally { if (this.TransferState != TransferState.Finished) { SetTransferState(TransferState.Stopped); } } }
private static bool ScanAFile(string realFilename, Stream memzip, string displayFilename) { Compress.File.File fStream = new Compress.File.File(); if (string.IsNullOrEmpty(realFilename) && memzip != null) { fStream.ZipFileOpen(memzip); } else { ZipReturn zRet = fStream.ZipFileOpen(realFilename, -1, true); if (zRet != ZipReturn.ZipGood) { return(false); } } bool ret = false; FileScan fScan = new FileScan(); List <FileScan.FileResults> resScan = fScan.Scan(fStream, true, true); HeaderFileType foundFileType = resScan[0].HeaderFileType; if (foundFileType == HeaderFileType.CHD) { // read altheader values from CHD file. } RvFile tFile = new RvFile { Size = resScan[0].Size, CRC = resScan[0].CRC, MD5 = resScan[0].MD5, SHA1 = resScan[0].SHA1, AltType = resScan[0].HeaderFileType, AltSize = resScan[0].AltSize, AltCRC = resScan[0].AltCRC, AltMD5 = resScan[0].AltMD5, AltSHA1 = resScan[0].AltSHA1 }; // test if needed. FindStatus res = RvRomFileMatchup.FileneededTest(tFile); if (res == FindStatus.FileNeededInArchive) { _bgw?.ReportProgress(0, new bgwShowError(displayFilename, "found")); Debug.WriteLine("Reading file as " + tFile.SHA1); string outfile = RomRootDir.Getfilename(tFile.SHA1); gZip gz1 = new gZip(); gz1.ZipFileCreate(outfile); gz1.ExtraData = tFile.SetExtraData(); gz1.ZipFileOpenWriteStream(false, true, "", tFile.Size, 8, out Stream write, null); fStream.ZipFileOpenReadStream(0, out Stream s, out ulong _); // do copy StreamCopier.StreamCopy(s, write, tFile.Size); fStream.ZipFileCloseReadStream(); fStream.ZipFileClose(); gz1.ZipFileCloseWriteStream(tFile.CRC); tFile.CompressedSize = gz1.CompressedSize; gz1.ZipFileClose(); tFile.DBWrite(); ret = true; } else if (res == FindStatus.FoundFileInArchive) { ret = true; } fStream.ZipFileClose(); if (foundFileType == HeaderFileType.ZIP || foundFileType == HeaderFileType.SevenZip || foundFileType == HeaderFileType.GZ) { ICompress fz; switch (foundFileType) { case HeaderFileType.SevenZip: fz = new SevenZ(); break; case HeaderFileType.GZ: fz = new gZip(); break; //case HeaderFileType.ZIP: default: fz = new Zip(); break; } ZipReturn zp; if (string.IsNullOrEmpty(realFilename) && memzip != null) { memzip.Position = 0; zp = fz.ZipFileOpen(memzip); } else { zp = fz.ZipFileOpen(realFilename); } if (zp == ZipReturn.ZipGood) { bool allZipFound = true; for (int i = 0; i < fz.LocalFilesCount(); i++) { LocalFile lf = fz.GetLocalFile(i); ZipReturn openFile = fz.ZipFileOpenReadStream(i, out Stream stream, out ulong streamSize); if (streamSize <= _inMemorySize) { if (openFile == ZipReturn.ZipTryingToAccessADirectory) { continue; } byte[] tmpFile = new byte[streamSize]; stream.Read(tmpFile, 0, (int)streamSize); using (Stream memStream = new MemoryStream(tmpFile, false)) { allZipFound &= ScanAFile(null, memStream, lf.Filename); } } else { string file = Path.Combine(_tmpDir, Guid.NewGuid().ToString()); FileStream.OpenFileWrite(file, out Stream fs); ulong sizetogo = streamSize; while (sizetogo > 0) { int sizenow = sizetogo > (ulong)Buffersize ? Buffersize : (int)sizetogo; stream.Read(Buffer, 0, sizenow); fs.Write(Buffer, 0, sizenow); sizetogo -= (ulong)sizenow; } fs.Close(); allZipFound &= ScanAFile(file, null, lf.Filename); File.Delete(file); } //fz.ZipFileCloseReadStream(); } fz.ZipFileClose(); ret |= allZipFound; } else { ret = false; } } return(ret); }
private void SaveContentToFile(String fileName) { var streamCopier = new StreamCopier { DisposeDest = true, DisposeSource = true, Length = _handlerParams.Context.Request.ContentLength64, StartPosition = SourceStartPosition.Current }; streamCopier.Copy( _handlerParams.Context.Request.InputStream, new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)); }