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

Commit a99ff10

Browse files
committed
Show response data as image in test app, try-catch added to http2 test
1 parent 3092024 commit a99ff10

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,20 @@
3636
</ListView>
3737
<TabControl Grid.Column="2" Grid.Row="0">
3838
<TabItem Header="Session">
39-
<Grid Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
39+
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
4040
<Grid.RowDefinitions>
4141
<RowDefinition />
4242
<RowDefinition />
4343
</Grid.RowDefinitions>
4444
<TextBox x:Name="TextBoxRequest" Grid.Row="0" />
45-
<TextBox x:Name="TextBoxResponse" Grid.Row="1" />
45+
<TabControl Grid.Row="1">
46+
<TabItem Header="Text">
47+
<TextBox x:Name="TextBoxResponse" />
48+
</TabItem>
49+
<TabItem Header="Image">
50+
<Image x:Name="ImageResponse" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None" />
51+
</TabItem>
52+
</TabControl>
4653
</Grid>
4754
</TabItem>
4855
</TabControl>

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
4+
using System.IO;
45
using System.Linq;
56
using System.Net;
67
using System.Text;
78
using System.Threading.Tasks;
89
using System.Windows;
910
using System.Windows.Controls;
1011
using System.Windows.Input;
12+
using System.Windows.Media.Imaging;
1113
using Titanium.Web.Proxy.EventArguments;
1214
using Titanium.Web.Proxy.Http;
1315
using Titanium.Web.Proxy.Models;
@@ -297,7 +299,8 @@ private void selectedSessionChanged()
297299

298300
var session = SelectedSession.HttpClient;
299301
var request = session.Request;
300-
var data = (request.IsBodyRead ? request.Body : null) ?? new byte[0];
302+
var fullData = (request.IsBodyRead ? request.Body : null) ?? new byte[0];
303+
var data = fullData;
301304
bool truncated = data.Length > truncateLimit;
302305
if (truncated)
303306
{
@@ -313,7 +316,8 @@ private void selectedSessionChanged()
313316
TextBoxRequest.Text = sb.ToString();
314317

315318
var response = session.Response;
316-
data = (response.IsBodyRead ? response.Body : null) ?? new byte[0];
319+
fullData = (response.IsBodyRead ? response.Body : null) ?? new byte[0];
320+
data = fullData;
317321
truncated = data.Length > truncateLimit;
318322
if (truncated)
319323
{
@@ -333,6 +337,19 @@ private void selectedSessionChanged()
333337
}
334338

335339
TextBoxResponse.Text = sb.ToString();
340+
341+
try
342+
{
343+
using (MemoryStream stream = new MemoryStream(fullData))
344+
{
345+
ImageResponse.Source =
346+
BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
347+
}
348+
}
349+
catch
350+
{
351+
ImageResponse.Source = null;
352+
}
336353
}
337354
}
338355
}

src/StreamExtended/Network/CustomBufferedStream.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ public override int ReadByte()
245245
{
246246
return -1;
247247
}
248-
249-
248+
250249
return streamBuffer[bufferPos + index];
251250
}
252251

@@ -491,7 +490,7 @@ public bool FillBuffer()
491490
if (bufferLength > 0)
492491
{
493492
//normally we fill the buffer only when it is empty, but sometimes we need more data
494-
//move the remanining data to the beginning of the buffer
493+
//move the remaining data to the beginning of the buffer
495494
Buffer.BlockCopy(streamBuffer, bufferPos, streamBuffer, 0, bufferLength);
496495
}
497496

@@ -516,7 +515,6 @@ public bool FillBuffer()
516515
}
517516

518517
return result;
519-
520518
}
521519

522520
/// <summary>

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
4747
{
4848
string connectHostname = null;
4949
TunnelConnectSessionEventArgs connectArgs = null;
50-
51-
50+
5251
// Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request)
5352
if (await HttpHelper.IsConnectMethod(clientStream) == 1)
5453
{
@@ -142,15 +141,22 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
142141
if (alpn != null && alpn.Contains(SslApplicationProtocol.Http2))
143142
{
144143
// test server HTTP/2 support
145-
// todo: this is a hack, because Titanium does not support HTTP protocol changing currently
146-
var connection = await tcpConnectionFactory.GetServerConnection(this, connectArgs,
147-
isConnect: true, applicationProtocols: SslExtensions.Http2ProtocolAsList,
148-
noCache: true, cancellationToken: cancellationToken);
149-
150-
http2Supported = connection.NegotiatedApplicationProtocol == SslApplicationProtocol.Http2;
151-
152-
//release connection back to pool instead of closing when connection pool is enabled.
153-
await tcpConnectionFactory.Release(connection, true);
144+
try
145+
{
146+
// todo: this is a hack, because Titanium does not support HTTP protocol changing currently
147+
var connection = await tcpConnectionFactory.GetServerConnection(this, connectArgs,
148+
isConnect: true, applicationProtocols: SslExtensions.Http2ProtocolAsList,
149+
noCache: true, cancellationToken: cancellationToken);
150+
151+
http2Supported = connection.NegotiatedApplicationProtocol ==
152+
SslApplicationProtocol.Http2;
153+
//release connection back to pool instead of closing when connection pool is enabled.
154+
await tcpConnectionFactory.Release(connection, true);
155+
}
156+
catch (Exception ex)
157+
{
158+
// ignore
159+
}
154160
}
155161

156162
if (EnableTcpServerConnectionPrefetch)

0 commit comments

Comments
 (0)