Commit 0c88f3f1 authored by Honfika's avatar Honfika

HTTP/2 fix, pushpromise commented out (not wokring yet)

parent 8bbfa308
...@@ -152,6 +152,11 @@ namespace Titanium.Web.Proxy.Examples.Wpf ...@@ -152,6 +152,11 @@ namespace Titanium.Web.Proxy.Examples.Wpf
private async Task ProxyServer_BeforeRequest(object sender, SessionEventArgs e) private async Task ProxyServer_BeforeRequest(object sender, SessionEventArgs e)
{ {
if (e.HttpClient.ConnectRequest?.TunnelType != TunnelType.Http2)
{
return;
}
SessionListItem item = null; SessionListItem item = null;
await Dispatcher.InvokeAsync(() => { item = addSession(e); }); await Dispatcher.InvokeAsync(() => { item = addSession(e); });
...@@ -171,6 +176,11 @@ namespace Titanium.Web.Proxy.Examples.Wpf ...@@ -171,6 +176,11 @@ namespace Titanium.Web.Proxy.Examples.Wpf
private async Task ProxyServer_BeforeResponse(object sender, SessionEventArgs e) private async Task ProxyServer_BeforeResponse(object sender, SessionEventArgs e)
{ {
if (e.HttpClient.ConnectRequest?.TunnelType != TunnelType.Http2)
{
return;
}
SessionListItem item = null; SessionListItem item = null;
await Dispatcher.InvokeAsync(() => await Dispatcher.InvokeAsync(() =>
{ {
...@@ -180,14 +190,9 @@ namespace Titanium.Web.Proxy.Examples.Wpf ...@@ -180,14 +190,9 @@ namespace Titanium.Web.Proxy.Examples.Wpf
} }
}); });
//e.SetResponseBody(Encoding.ASCII.GetBytes("TITANIUMMMM!!!!"));
//if (!e.HttpClient.Request.RequestUri.ToString().Contains("/mail/u/"))
// return;
//e.HttpClient.Response.Headers.AddHeader("X-Titanium-Header", "HTTP/2 works"); //e.HttpClient.Response.Headers.AddHeader("X-Titanium-Header", "HTTP/2 works");
//if (e.HttpClient.ConnectRequest?.TunnelType == TunnelType.Http2) //e.SetResponseBody(Encoding.ASCII.GetBytes("TITANIUMMMM!!!!"));
//{
//}
if (item != null) if (item != null)
{ {
......
...@@ -15,12 +15,17 @@ ...@@ -15,12 +15,17 @@
public byte[] CopyToBuffer() public byte[] CopyToBuffer()
{ {
int length = Length; int length = Length;
var buf = Buffer; var buf = /*new byte[9];*/Buffer;
buf[0] = (byte)((length >> 16) & 0xff); buf[0] = (byte)((length >> 16) & 0xff);
buf[1] = (byte)((length >> 8) & 0xff); buf[1] = (byte)((length >> 8) & 0xff);
buf[2] = (byte)(length & 0xff); buf[2] = (byte)(length & 0xff);
buf[3] = (byte)Type; buf[3] = (byte)Type;
buf[4] = (byte)Flags; buf[4] = (byte)Flags;
int streamId = StreamId;
//buf[5] = (byte)((streamId >> 24) & 0xff);
//buf[6] = (byte)((streamId >> 16) & 0xff);
//buf[7] = (byte)((streamId >> 8) & 0xff);
//buf[8] = (byte)(streamId & 0xff);
return buf; return buf;
} }
} }
......
...@@ -62,7 +62,7 @@ namespace Titanium.Web.Proxy.Http2 ...@@ -62,7 +62,7 @@ namespace Titanium.Web.Proxy.Http2
int headerTableSize = 0; int headerTableSize = 0;
Decoder decoder = null; Decoder decoder = null;
Http2FrameHeader frameHeader = new Http2FrameHeader(); var frameHeader = new Http2FrameHeader();
frameHeader.Buffer = new byte[9]; frameHeader.Buffer = new byte[9];
byte[] buffer = null; byte[] buffer = null;
while (true) while (true)
...@@ -102,20 +102,20 @@ namespace Titanium.Web.Proxy.Http2 ...@@ -102,20 +102,20 @@ namespace Titanium.Web.Proxy.Http2
bool endStream = false; bool endStream = false;
SessionEventArgs args = null; SessionEventArgs args = null;
RequestResponseBase rr; RequestResponseBase rr = null;
if (type == Http2FrameType.Data || type == Http2FrameType.Headers || type == Http2FrameType.PushPromise) if (type == Http2FrameType.Data || type == Http2FrameType.Headers/* || type == Http2FrameType.PushPromise*/)
{ {
if (!sessions.TryGetValue(streamId, out args)) if (!sessions.TryGetValue(streamId, out args))
{ {
if (type == Http2FrameType.Data) //if (type == Http2FrameType.Data)
{ //{
throw new ProxyHttpException("HTTP Body data received before any header frame.", null, args); // throw new ProxyHttpException("HTTP Body data received before any header frame.", null, args);
} //}
if (type == Http2FrameType.Headers && !isClient) //if (type == Http2FrameType.Headers && !isClient)
{ //{
throw new ProxyHttpException("HTTP Response received before any Request header frame.", null, args); // throw new ProxyHttpException("HTTP Response received before any Request header frame.", null, args);
} //}
if (type == Http2FrameType.PushPromise && isClient) if (type == Http2FrameType.PushPromise && isClient)
{ {
...@@ -125,7 +125,7 @@ namespace Titanium.Web.Proxy.Http2 ...@@ -125,7 +125,7 @@ namespace Titanium.Web.Proxy.Http2
} }
//System.Diagnostics.Debug.WriteLine("CONN: " + connectionId + ", CLIENT: " + isClient + ", STREAM: " + streamId + ", TYPE: " + type); //System.Diagnostics.Debug.WriteLine("CONN: " + connectionId + ", CLIENT: " + isClient + ", STREAM: " + streamId + ", TYPE: " + type);
if (type == Http2FrameType.Data) if (type == Http2FrameType.Data && args != null)
{ {
rr = isClient ? (RequestResponseBase)args.HttpClient.Request : args.HttpClient.Response; rr = isClient ? (RequestResponseBase)args.HttpClient.Request : args.HttpClient.Response;
...@@ -155,54 +155,6 @@ namespace Titanium.Web.Proxy.Http2 ...@@ -155,54 +155,6 @@ namespace Titanium.Web.Proxy.Http2
} }
data.Write(buffer, offset, length); data.Write(buffer, offset, length);
if (endStream)
{
var body = data.ToArray();
if (rr.ContentEncoding != null)
{
using (var ms = new MemoryStream())
{
using (var zip =
DecompressionFactory.Create(rr.ContentEncoding, new MemoryStream(body)))
{
zip.CopyTo(ms);
}
body = ms.ToArray();
}
}
if (!rr.BodyAvailable)
{
rr.Body = body;
}
rr.IsBodyRead = true;
var tcs = rr.ReadHttp2BodyTaskCompletionSource;
rr.ReadHttp2BodyTaskCompletionSource = null;
if (!tcs.Task.IsCompleted)
{
tcs.SetResult(true);
}
rr.Http2BodyData = null;
if (rr.Http2BeforeHandlerTask != null)
{
await rr.Http2BeforeHandlerTask;
}
if (args.IsPromise)
{
breakpoint();
}
await sendBody(remoteSettings, rr, frameHeader, buffer, output);
}
} }
} }
else if (type == Http2FrameType.Headers/* || type == Http2FrameType.PushPromise*/) else if (type == Http2FrameType.Headers/* || type == Http2FrameType.PushPromise*/)
...@@ -391,6 +343,55 @@ namespace Titanium.Web.Proxy.Http2 ...@@ -391,6 +343,55 @@ namespace Titanium.Web.Proxy.Http2
} }
} }
if (endStream && rr.ReadHttp2BodyTaskCompletionSource != null)
{
if (!rr.BodyAvailable)
{
var data = rr.Http2BodyData;
var body = data.ToArray();
if (rr.ContentEncoding != null)
{
using (var ms = new MemoryStream())
{
using (var zip =
DecompressionFactory.Create(rr.ContentEncoding, new MemoryStream(body)))
{
zip.CopyTo(ms);
}
body = ms.ToArray();
}
}
rr.Body = body;
}
rr.IsBodyRead = true;
var tcs = rr.ReadHttp2BodyTaskCompletionSource;
rr.ReadHttp2BodyTaskCompletionSource = null;
if (!tcs.Task.IsCompleted)
{
tcs.SetResult(true);
}
rr.Http2BodyData = null;
if (rr.Http2BeforeHandlerTask != null)
{
await rr.Http2BeforeHandlerTask;
}
if (args.IsPromise)
{
breakpoint();
}
await sendBody(remoteSettings, rr, frameHeader, buffer, output);
}
if (!isClient && endStream) if (!isClient && endStream)
{ {
sessions.TryRemove(streamId, out _); sessions.TryRemove(streamId, out _);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment