Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
6393b39a
Commit
6393b39a
authored
Dec 29, 2017
by
Honfika
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Chunked response simplified
parent
3c44891d
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
41 additions
and
40 deletions
+41
-40
HttpResponseWriter.cs
Titanium.Web.Proxy/Helpers/HttpResponseWriter.cs
+28
-33
HttpWriter.cs
Titanium.Web.Proxy/Helpers/HttpWriter.cs
+11
-0
HttpWebClient.cs
Titanium.Web.Proxy/Http/HttpWebClient.cs
+1
-2
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+1
-1
ProxyConstants.cs
Titanium.Web.Proxy/Shared/ProxyConstants.cs
+0
-4
No files found.
Titanium.Web.Proxy/Helpers/HttpResponseWriter.cs
View file @
6393b39a
...
...
@@ -6,7 +6,6 @@ using System.Threading.Tasks;
using
StreamExtended.Network
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Shared
;
namespace
Titanium.Web.Proxy.Helpers
{
...
...
@@ -64,13 +63,11 @@ namespace Titanium.Web.Proxy.Helpers
/// Copies the specified content length number of bytes to the output stream from the given inputs stream
/// optionally chunked
/// </summary>
/// <param name="bufferSize"></param>
/// <param name="inStreamReader"></param>
/// <param name="isChunked"></param>
/// <param name="contentLength"></param>
/// <returns></returns>
internal
async
Task
WriteResponseBodyAsync
(
int
bufferSize
,
CustomBinaryReader
inStreamReader
,
bool
isChunked
,
long
contentLength
)
internal
async
Task
CopyBodyAsync
(
CustomBinaryReader
inStreamReader
,
bool
isChunked
,
long
contentLength
)
{
if
(!
isChunked
)
{
...
...
@@ -84,57 +81,55 @@ namespace Titanium.Web.Proxy.Helpers
}
else
{
await
WriteResponse
BodyChunkedAsync
(
inStreamReader
);
await
Copy
BodyChunkedAsync
(
inStreamReader
);
}
}
/// <summary>
/// Copies the given input bytes to output stream chunked
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private
async
Task
WriteResponseBodyChunkedAsync
(
byte
[]
data
)
{
var
chunkHead
=
Encoding
.
ASCII
.
GetBytes
(
data
.
Length
.
ToString
(
"x2"
));
await
WriteAsync
(
chunkHead
);
await
WriteLineAsync
();
await
WriteAsync
(
data
);
await
WriteLineAsync
();
await
WriteLineAsync
(
"0"
);
await
WriteLineAsync
();
}
/// <summary>
/// Copies the streams chunked
/// </summary>
/// <param name="inStreamReader"></param>
/// <returns></returns>
internal
async
Task
WriteResponse
BodyChunkedAsync
(
CustomBinaryReader
inStreamReader
)
private
async
Task
Copy
BodyChunkedAsync
(
CustomBinaryReader
inStreamReader
)
{
while
(
true
)
{
string
chunkHead
=
await
inStreamReader
.
ReadLineAsync
();
int
chunkSize
=
int
.
Parse
(
chunkHead
,
NumberStyles
.
HexNumber
);
await
WriteLineAsync
(
chunkHead
);
if
(
chunkSize
!=
0
)
{
var
chunkHeadBytes
=
Encoding
.
ASCII
.
GetBytes
(
chunkSize
.
ToString
(
"x2"
));
await
BaseStream
.
WriteAsync
(
chunkHeadBytes
,
0
,
chunkHeadBytes
.
Length
);
await
BaseStream
.
WriteAsync
(
ProxyConstants
.
NewLineBytes
,
0
,
ProxyConstants
.
NewLineBytes
.
Length
);
await
inStreamReader
.
CopyBytesToStream
(
BaseStream
,
chunkSize
);
await
BaseStream
.
WriteAsync
(
ProxyConstants
.
NewLineBytes
,
0
,
ProxyConstants
.
NewLineBytes
.
Length
);
await
inStreamReader
.
ReadLineAsync
();
}
else
await
WriteLineAsync
();
await
inStreamReader
.
ReadLineAsync
();
if
(
chunkSize
==
0
)
{
await
inStreamReader
.
ReadLineAsync
();
await
BaseStream
.
WriteAsync
(
ProxyConstants
.
ChunkEnd
,
0
,
ProxyConstants
.
ChunkEnd
.
Length
);
break
;
}
}
}
/// <summary>
/// Copies the given input bytes to output stream chunked
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
internal
async
Task
WriteResponseBodyChunkedAsync
(
byte
[]
data
)
{
var
chunkHead
=
Encoding
.
ASCII
.
GetBytes
(
data
.
Length
.
ToString
(
"x2"
));
await
BaseStream
.
WriteAsync
(
chunkHead
,
0
,
chunkHead
.
Length
);
await
BaseStream
.
WriteAsync
(
ProxyConstants
.
NewLineBytes
,
0
,
ProxyConstants
.
NewLineBytes
.
Length
);
await
BaseStream
.
WriteAsync
(
data
,
0
,
data
.
Length
);
await
BaseStream
.
WriteAsync
(
ProxyConstants
.
NewLineBytes
,
0
,
ProxyConstants
.
NewLineBytes
.
Length
);
await
BaseStream
.
WriteAsync
(
ProxyConstants
.
ChunkEnd
,
0
,
ProxyConstants
.
ChunkEnd
.
Length
);
}
}
}
Titanium.Web.Proxy/Helpers/HttpWriter.cs
View file @
6393b39a
...
...
@@ -53,5 +53,16 @@ namespace Titanium.Web.Proxy.Helpers
await
FlushAsync
();
}
}
public
async
Task
WriteAsync
(
byte
[]
data
,
bool
flush
=
false
)
{
await
FlushAsync
();
await
BaseStream
.
WriteAsync
(
data
,
0
,
data
.
Length
);
if
(
flush
)
{
// flush the stream and the encoder, too
await
FlushAsync
();
}
}
}
}
Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
6393b39a
...
...
@@ -81,8 +81,6 @@ namespace Titanium.Web.Proxy.Http
/// <returns></returns>
internal
async
Task
SendRequest
(
bool
enable100ContinueBehaviour
)
{
var
stream
=
ServerConnection
.
Stream
;
byte
[]
requestBytes
;
using
(
var
ms
=
new
MemoryStream
())
using
(
var
writer
=
new
HttpRequestWriter
(
ms
,
bufferSize
))
...
...
@@ -127,6 +125,7 @@ namespace Titanium.Web.Proxy.Http
requestBytes
=
ms
.
ToArray
();
}
var
stream
=
ServerConnection
.
Stream
;
await
stream
.
WriteAsync
(
requestBytes
,
0
,
requestBytes
.
Length
);
await
stream
.
FlushAsync
();
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
6393b39a
...
...
@@ -105,7 +105,7 @@ namespace Titanium.Web.Proxy
//Write body if exists
if
(
response
.
HasBody
)
{
await
clientStreamWriter
.
WriteResponseBodyAsync
(
BufferSize
,
args
.
WebSession
.
ServerConnection
.
StreamReader
,
await
clientStreamWriter
.
CopyBodyAsync
(
args
.
WebSession
.
ServerConnection
.
StreamReader
,
response
.
IsChunked
,
response
.
ContentLength
);
}
}
...
...
Titanium.Web.Proxy/Shared/ProxyConstants.cs
View file @
6393b39a
...
...
@@ -14,10 +14,6 @@ namespace Titanium.Web.Proxy.Shared
internal
static
readonly
char
[]
SemiColonSplit
=
{
';'
};
internal
static
readonly
char
[]
EqualSplit
=
{
'='
};
internal
static
readonly
byte
[]
NewLineBytes
=
Encoding
.
ASCII
.
GetBytes
(
NewLine
);
internal
static
readonly
byte
[]
ChunkEnd
=
Encoding
.
ASCII
.
GetBytes
(
0.
ToString
(
"x2"
)
+
NewLine
+
NewLine
);
internal
const
string
NewLine
=
"\r\n"
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment