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
a9c29243
Commit
a9c29243
authored
Mar 25, 2018
by
Honfika
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
content length limit fix
parent
72cd50d5
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
57 additions
and
100 deletions
+57
-100
CompressionFactory.cs
Titanium.Web.Proxy/Compression/CompressionFactory.cs
+2
-5
DefaultCompression.cs
Titanium.Web.Proxy/Compression/DefaultCompression.cs
+0
-16
DecompressionFactory.cs
Titanium.Web.Proxy/Decompression/DecompressionFactory.cs
+2
-5
DefaultDecompression.cs
Titanium.Web.Proxy/Decompression/DefaultDecompression.cs
+0
-16
LimitedStream.cs
Titanium.Web.Proxy/EventArguments/LimitedStream.cs
+22
-12
SessionEventArgs.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+30
-45
HttpWriter.cs
Titanium.Web.Proxy/Helpers/HttpWriter.cs
+1
-1
No files found.
Titanium.Web.Proxy/Compression/CompressionFactory.cs
View file @
a9c29243
...
@@ -6,14 +6,11 @@ namespace Titanium.Web.Proxy.Compression
...
@@ -6,14 +6,11 @@ namespace Titanium.Web.Proxy.Compression
/// <summary>
/// <summary>
/// A factory to generate the compression methods based on the type of compression
/// A factory to generate the compression methods based on the type of compression
/// </summary>
/// </summary>
internal
class
CompressionFactory
internal
static
class
CompressionFactory
{
{
public
static
readonly
CompressionFactory
Instance
=
new
CompressionFactory
();
//cache
//cache
private
static
readonly
Lazy
<
ICompression
>
gzip
=
new
Lazy
<
ICompression
>(()
=>
new
GZipCompression
());
private
static
readonly
Lazy
<
ICompression
>
gzip
=
new
Lazy
<
ICompression
>(()
=>
new
GZipCompression
());
private
static
readonly
Lazy
<
ICompression
>
deflate
=
new
Lazy
<
ICompression
>(()
=>
new
DeflateCompression
());
private
static
readonly
Lazy
<
ICompression
>
deflate
=
new
Lazy
<
ICompression
>(()
=>
new
DeflateCompression
());
private
static
readonly
Lazy
<
ICompression
>
def
=
new
Lazy
<
ICompression
>(()
=>
new
DefaultCompression
());
public
static
ICompression
GetCompression
(
string
type
)
public
static
ICompression
GetCompression
(
string
type
)
{
{
...
@@ -24,7 +21,7 @@ namespace Titanium.Web.Proxy.Compression
...
@@ -24,7 +21,7 @@ namespace Titanium.Web.Proxy.Compression
case
KnownHeaders
.
ContentEncodingDeflate
:
case
KnownHeaders
.
ContentEncodingDeflate
:
return
deflate
.
Value
;
return
deflate
.
Value
;
default
:
default
:
return
def
.
Value
;
throw
new
Exception
(
$"Unsupported compression mode:
{
type
}
"
)
;
}
}
}
}
}
}
...
...
Titanium.Web.Proxy/Compression/DefaultCompression.cs
deleted
100644 → 0
View file @
72cd50d5
using
System.IO
;
using
System.Threading.Tasks
;
namespace
Titanium.Web.Proxy.Compression
{
/// <summary>
/// When no compression is specified just return the stream
/// </summary>
internal
class
DefaultCompression
:
ICompression
{
public
Stream
GetStream
(
Stream
stream
)
{
return
stream
;
}
}
}
Titanium.Web.Proxy/Decompression/DecompressionFactory.cs
View file @
a9c29243
...
@@ -8,14 +8,11 @@ namespace Titanium.Web.Proxy.Decompression
...
@@ -8,14 +8,11 @@ namespace Titanium.Web.Proxy.Decompression
/// </summary>
/// </summary>
internal
class
DecompressionFactory
internal
class
DecompressionFactory
{
{
public
static
readonly
DecompressionFactory
Instance
=
new
DecompressionFactory
();
//cache
//cache
private
static
readonly
Lazy
<
IDecompression
>
gzip
=
new
Lazy
<
IDecompression
>(()
=>
new
GZipDecompression
());
private
static
readonly
Lazy
<
IDecompression
>
gzip
=
new
Lazy
<
IDecompression
>(()
=>
new
GZipDecompression
());
private
static
readonly
Lazy
<
IDecompression
>
deflate
=
new
Lazy
<
IDecompression
>(()
=>
new
DeflateDecompression
());
private
static
readonly
Lazy
<
IDecompression
>
deflate
=
new
Lazy
<
IDecompression
>(()
=>
new
DeflateDecompression
());
private
static
readonly
Lazy
<
IDecompression
>
def
=
new
Lazy
<
IDecompression
>(()
=>
new
DefaultDecompression
());
internal
IDecompression
Create
(
string
type
)
public
static
IDecompression
Create
(
string
type
)
{
{
switch
(
type
)
switch
(
type
)
{
{
...
@@ -24,7 +21,7 @@ namespace Titanium.Web.Proxy.Decompression
...
@@ -24,7 +21,7 @@ namespace Titanium.Web.Proxy.Decompression
case
KnownHeaders
.
ContentEncodingDeflate
:
case
KnownHeaders
.
ContentEncodingDeflate
:
return
deflate
.
Value
;
return
deflate
.
Value
;
default
:
default
:
return
def
.
Value
;
throw
new
Exception
(
$"Unsupported decompression mode:
{
type
}
"
)
;
}
}
}
}
}
}
...
...
Titanium.Web.Proxy/Decompression/DefaultDecompression.cs
deleted
100644 → 0
View file @
72cd50d5
using
System.IO
;
using
System.Threading.Tasks
;
namespace
Titanium.Web.Proxy.Decompression
{
/// <summary>
/// When no compression is specified just return the stream
/// </summary>
internal
class
DefaultDecompression
:
IDecompression
{
public
Stream
GetStream
(
Stream
stream
)
{
return
stream
;
}
}
}
Titanium.Web.Proxy/EventArguments/
Chunk
edStream.cs
→
Titanium.Web.Proxy/EventArguments/
Limit
edStream.cs
View file @
a9c29243
...
@@ -8,18 +8,21 @@ using StreamExtended.Network;
...
@@ -8,18 +8,21 @@ using StreamExtended.Network;
namespace
Titanium.Web.Proxy.EventArguments
namespace
Titanium.Web.Proxy.EventArguments
{
{
internal
class
Chunk
edStream
:
Stream
internal
class
Limit
edStream
:
Stream
{
{
private
readonly
CustomBufferedStream
baseStream
;
private
readonly
CustomBufferedStream
baseStream
;
private
readonly
CustomBinaryReader
baseReader
;
private
readonly
CustomBinaryReader
baseReader
;
private
readonly
bool
isChunked
;
private
bool
readChunkTrail
;
private
bool
readChunkTrail
;
private
int
chunkB
ytesRemaining
;
private
long
b
ytesRemaining
;
public
ChunkedStream
(
CustomBufferedStream
baseStream
,
CustomBinaryReader
baseReader
)
public
LimitedStream
(
CustomBufferedStream
baseStream
,
CustomBinaryReader
baseReader
,
bool
isChunked
,
long
contentLength
)
{
{
this
.
baseStream
=
baseStream
;
this
.
baseStream
=
baseStream
;
this
.
baseReader
=
baseReader
;
this
.
baseReader
=
baseReader
;
this
.
isChunked
=
isChunked
;
bytesRemaining
=
isChunked
?
0
:
contentLength
;
}
}
private
void
GetNextChunk
()
private
void
GetNextChunk
()
...
@@ -41,11 +44,11 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -41,11 +44,11 @@ namespace Titanium.Web.Proxy.EventArguments
}
}
int
chunkSize
=
int
.
Parse
(
chunkHead
,
NumberStyles
.
HexNumber
);
int
chunkSize
=
int
.
Parse
(
chunkHead
,
NumberStyles
.
HexNumber
);
chunkB
ytesRemaining
=
chunkSize
;
b
ytesRemaining
=
chunkSize
;
if
(
chunkSize
==
0
)
if
(
chunkSize
==
0
)
{
{
chunkB
ytesRemaining
=
-
1
;
b
ytesRemaining
=
-
1
;
//chunk trail
//chunk trail
string
s
=
baseReader
.
ReadLineAsync
().
Result
;
string
s
=
baseReader
.
ReadLineAsync
().
Result
;
...
@@ -69,24 +72,31 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -69,24 +72,31 @@ namespace Titanium.Web.Proxy.EventArguments
public
override
int
Read
(
byte
[]
buffer
,
int
offset
,
int
count
)
public
override
int
Read
(
byte
[]
buffer
,
int
offset
,
int
count
)
{
{
if
(
chunkB
ytesRemaining
==
-
1
)
if
(
b
ytesRemaining
==
-
1
)
{
{
return
0
;
return
0
;
}
}
if
(
chunkB
ytesRemaining
==
0
)
if
(
b
ytesRemaining
==
0
)
{
{
GetNextChunk
();
if
(
isChunked
)
{
GetNextChunk
();
}
else
{
bytesRemaining
=
-
1
;
}
}
}
if
(
chunkB
ytesRemaining
==
-
1
)
if
(
b
ytesRemaining
==
-
1
)
{
{
return
0
;
return
0
;
}
}
int
toRead
=
Math
.
Min
(
count
,
chunkB
ytesRemaining
);
int
toRead
=
(
int
)
Math
.
Min
(
count
,
b
ytesRemaining
);
int
res
=
baseStream
.
Read
(
buffer
,
offset
,
toRead
);
int
res
=
baseStream
.
Read
(
buffer
,
offset
,
toRead
);
chunkB
ytesRemaining
-=
res
;
b
ytesRemaining
-=
res
;
return
res
;
return
res
;
}
}
...
@@ -96,7 +106,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -96,7 +106,7 @@ namespace Titanium.Web.Proxy.EventArguments
var
buffer
=
BufferPool
.
GetBuffer
(
baseReader
.
Buffer
.
Length
);
var
buffer
=
BufferPool
.
GetBuffer
(
baseReader
.
Buffer
.
Length
);
try
try
{
{
while
(
chunkB
ytesRemaining
!=
-
1
)
while
(
b
ytesRemaining
!=
-
1
)
{
{
await
ReadAsync
(
buffer
,
0
,
buffer
.
Length
);
await
ReadAsync
(
buffer
,
0
,
buffer
.
Length
);
}
}
...
...
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
a9c29243
...
@@ -294,73 +294,58 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -294,73 +294,58 @@ namespace Titanium.Web.Proxy.EventArguments
}
}
else
else
{
{
await
CopyBodyAsync
(
ProxyClient
.
ClientStream
,
reader
,
writer
,
await
CopyBodyAsync
(
ProxyClient
.
ClientStream
,
reader
,
writer
,
request
,
transformation
,
OnDataSent
);
request
.
IsChunked
,
transformation
,
request
.
ContentEncoding
,
contentLength
,
OnDataSent
);
}
}
}
}
private
async
Task
CopyBodyAsync
(
CustomBufferedStream
stream
,
CustomBinaryReader
reader
,
HttpWriter
writer
,
internal
async
Task
CopyResponseBodyAsync
(
HttpWriter
writer
,
TransformationMode
transformation
)
bool
isChunked
,
TransformationMode
transformation
,
string
contentEncoding
,
long
contentLength
,
Action
<
byte
[],
int
,
int
>
onCopy
)
{
{
bool
newReader
=
false
;
var
response
=
WebSession
.
Response
;
var
reader
=
WebSession
.
ServerConnection
.
StreamReader
;
Stream
s
=
stream
;
await
CopyBodyAsync
(
WebSession
.
ServerConnection
.
Stream
,
reader
,
writer
,
response
,
transformation
,
OnDataReceived
);
ChunkedStream
chunkedStream
=
null
;
}
Stream
decompressStream
=
null
;
if
(
isChunked
&&
transformation
!=
TransformationMode
.
None
)
private
async
Task
CopyBodyAsync
(
CustomBufferedStream
stream
,
CustomBinaryReader
reader
,
HttpWriter
writer
,
RequestResponseBase
requestResponse
,
TransformationMode
transformation
,
Action
<
byte
[],
int
,
int
>
onCopy
)
{
bool
isChunked
=
requestResponse
.
IsChunked
;
long
contentLength
=
requestResponse
.
ContentLength
;
if
(
transformation
==
TransformationMode
.
None
)
{
{
s
=
chunkedStream
=
new
ChunkedStream
(
stream
,
reader
);
await
writer
.
CopyBodyAsync
(
reader
,
isChunked
,
contentLength
,
onCopy
);
isChunked
=
false
;
return
;
newReader
=
true
;
}
}
if
(
transformation
==
TransformationMode
.
Uncompress
)
LimitedStream
limitedStream
;
Stream
decompressStream
=
null
;
string
contentEncoding
=
requestResponse
.
ContentEncoding
;
Stream
s
=
limitedStream
=
new
LimitedStream
(
stream
,
reader
,
isChunked
,
contentLength
);
if
(
transformation
==
TransformationMode
.
Uncompress
&&
contentEncoding
!=
null
)
{
{
s
=
decompressStream
=
DecompressionFactory
.
Instance
.
Create
(
contentEncoding
).
GetStream
(
s
);
s
=
decompressStream
=
DecompressionFactory
.
Create
(
contentEncoding
).
GetStream
(
s
);
newReader
=
true
;
}
}
try
try
{
{
if
(
newReader
)
var
bufStream
=
new
CustomBufferedStream
(
s
,
bufferSize
,
true
);
{
reader
=
new
CustomBinaryReader
(
bufStream
,
bufferSize
);
var
bufStream
=
new
CustomBufferedStream
(
s
,
bufferSize
,
true
);
s
=
bufStream
;
reader
=
new
CustomBinaryReader
(
bufStream
,
bufferSize
);
}
await
writer
.
CopyBodyAsync
(
reader
,
isChunked
,
contentLength
,
onCopy
);
await
writer
.
CopyBodyAsync
(
reader
,
false
,
-
1
,
onCopy
);
}
}
finally
finally
{
{
if
(
newReader
)
reader
?.
Dispose
();
{
decompressStream
?.
Dispose
();
reader
?.
Dispose
();
if
(
decompressStream
!=
null
&&
decompressStream
!=
stream
)
{
decompressStream
.
Dispose
();
}
if
(
chunkedStream
!=
null
)
await
limitedStream
.
Finish
();
{
limitedStream
.
Dispose
();
await
chunkedStream
.
Finish
();
chunkedStream
.
Dispose
();
}
}
}
}
}
}
internal
async
Task
CopyResponseBodyAsync
(
HttpWriter
writer
,
TransformationMode
transformation
)
{
var
response
=
WebSession
.
Response
;
var
reader
=
WebSession
.
ServerConnection
.
StreamReader
;
await
CopyBodyAsync
(
WebSession
.
ServerConnection
.
Stream
,
reader
,
writer
,
response
.
IsChunked
,
transformation
,
response
.
ContentEncoding
,
response
.
ContentLength
,
OnDataReceived
);
}
/// <summary>
/// <summary>
/// Read a line from the byte stream
/// Read a line from the byte stream
/// </summary>
/// </summary>
...
...
Titanium.Web.Proxy/Helpers/HttpWriter.cs
View file @
a9c29243
...
@@ -165,7 +165,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -165,7 +165,7 @@ namespace Titanium.Web.Proxy.Helpers
return
CopyBodyChunkedAsync
(
streamReader
,
onCopy
);
return
CopyBodyChunkedAsync
(
streamReader
,
onCopy
);
}
}
//http 1.0
//http 1.0
or the stream reader limits the stream
if
(
contentLength
==
-
1
)
if
(
contentLength
==
-
1
)
{
{
contentLength
=
long
.
MaxValue
;
contentLength
=
long
.
MaxValue
;
...
...
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