Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 0e8e25c

Browse files
authored
Merge pull request #616 from justcoding121/master
do not throw exception in fillbuffer (only when it called on an alrea…
2 parents e4d96d3 + c9bbbd1 commit 0e8e25c

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

examples/Titanium.Web.Proxy.Examples.Wpf/MainWindow.xaml.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ private async Task ProxyServer_BeforeRequest(object sender, SessionEventArgs e)
159159
{
160160
e.HttpClient.Request.KeepBody = true;
161161
await e.GetRequestBody();
162+
163+
if (item == SelectedSession)
164+
{
165+
await Dispatcher.InvokeAsync(selectedSessionChanged);
166+
}
162167
}
163168
}
164169

@@ -185,6 +190,10 @@ await Dispatcher.InvokeAsync(() =>
185190
await e.GetResponseBody();
186191

187192
await Dispatcher.InvokeAsync(() => { item.Update(); });
193+
if (item == SelectedSession)
194+
{
195+
await Dispatcher.InvokeAsync(selectedSessionChanged);
196+
}
188197
}
189198
}
190199
}

src/Titanium.Web.Proxy/StreamExtended/Network/CustomBufferedStream.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Net.Sockets;
45
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
@@ -450,7 +451,7 @@ public bool FillBuffer()
450451
{
451452
if (closed)
452453
{
453-
return false;
454+
throw new Exception("Stream is already closed");
454455
}
455456

456457
if (bufferLength > 0)
@@ -462,17 +463,23 @@ public bool FillBuffer()
462463

463464
bufferPos = 0;
464465

465-
int readBytes = baseStream.Read(streamBuffer, bufferLength, streamBuffer.Length - bufferLength);
466-
bool result = readBytes > 0;
467-
if (result)
466+
bool result = false;
467+
try
468468
{
469-
OnDataRead(streamBuffer, bufferLength, readBytes);
470-
bufferLength += readBytes;
469+
int readBytes = baseStream.Read(streamBuffer, bufferLength, streamBuffer.Length - bufferLength);
470+
result = readBytes > 0;
471+
if (result)
472+
{
473+
OnDataRead(streamBuffer, bufferLength, readBytes);
474+
bufferLength += readBytes;
475+
}
471476
}
472-
else
477+
finally
473478
{
474-
closed = true;
475-
throw new EndOfStreamException();
479+
if (!result)
480+
{
481+
closed = true;
482+
}
476483
}
477484

478485
return result;
@@ -488,7 +495,7 @@ public async Task<bool> FillBufferAsync(CancellationToken cancellationToken = de
488495
{
489496
if (closed)
490497
{
491-
return false;
498+
throw new Exception("Stream is already closed");
492499
}
493500

494501
if (bufferLength > 0)
@@ -506,18 +513,23 @@ public async Task<bool> FillBufferAsync(CancellationToken cancellationToken = de
506513

507514
bufferPos = 0;
508515

509-
int readBytes = await baseStream.ReadAsync(streamBuffer, bufferLength, bytesToRead, cancellationToken);
510-
bool result = readBytes > 0;
511-
if (result)
516+
bool result = false;
517+
try
512518
{
513-
OnDataRead(streamBuffer, bufferLength, readBytes);
514-
bufferLength += readBytes;
519+
int readBytes = await baseStream.ReadAsync(streamBuffer, bufferLength, bytesToRead, cancellationToken);
520+
result = readBytes > 0;
521+
if (result)
522+
{
523+
OnDataRead(streamBuffer, bufferLength, readBytes);
524+
bufferLength += readBytes;
525+
}
515526
}
516-
else
527+
finally
517528
{
518-
closed = true;
519-
520-
// do not throw exception here
529+
if (!result)
530+
{
531+
closed = true;
532+
}
521533
}
522534

523535
return result;

0 commit comments

Comments
 (0)