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
a007c1c0
Unverified
Commit
a007c1c0
authored
Mar 28, 2018
by
honfika
Committed by
GitHub
Mar 28, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #395 from justcoding121/develop
merge to beta
parents
7eb8f297
ef38da77
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
273 additions
and
164 deletions
+273
-164
SessionEventArgs.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+77
-60
TunnelConnectEventArgs.cs
Titanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs
+1
-2
HttpResponseWriter.cs
Titanium.Web.Proxy/Helpers/HttpResponseWriter.cs
+1
-1
HttpWriter.cs
Titanium.Web.Proxy/Helpers/HttpWriter.cs
+20
-6
HttpWebClient.cs
Titanium.Web.Proxy/Http/HttpWebClient.cs
+4
-4
Request.cs
Titanium.Web.Proxy/Http/Request.cs
+6
-1
RequestResponseBase.cs
Titanium.Web.Proxy/Http/RequestResponseBase.cs
+75
-6
Response.cs
Titanium.Web.Proxy/Http/Response.cs
+23
-1
OkResponse.cs
Titanium.Web.Proxy/Http/Responses/OkResponse.cs
+8
-0
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+32
-37
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+26
-46
No files found.
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
a007c1c0
This diff is collapsed.
Click to expand it.
Titanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs
View file @
a007c1c0
...
@@ -11,9 +11,8 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -11,9 +11,8 @@ namespace Titanium.Web.Proxy.EventArguments
public
bool
IsHttpsConnect
{
get
;
internal
set
;
}
public
bool
IsHttpsConnect
{
get
;
internal
set
;
}
internal
TunnelConnectSessionEventArgs
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ConnectRequest
connectRequest
,
ExceptionHandler
exceptionFunc
)
internal
TunnelConnectSessionEventArgs
(
int
bufferSize
,
ProxyEndPoint
endPoint
,
ConnectRequest
connectRequest
,
ExceptionHandler
exceptionFunc
)
:
base
(
bufferSize
,
endPoint
,
exceptionFunc
)
:
base
(
bufferSize
,
endPoint
,
exceptionFunc
,
connectRequest
)
{
{
WebSession
.
Request
=
connectRequest
;
}
}
}
}
}
}
Titanium.Web.Proxy/Helpers/HttpResponseWriter.cs
View file @
a007c1c0
...
@@ -20,7 +20,7 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -20,7 +20,7 @@ namespace Titanium.Web.Proxy.Helpers
public
async
Task
WriteResponseAsync
(
Response
response
,
bool
flush
=
true
)
public
async
Task
WriteResponseAsync
(
Response
response
,
bool
flush
=
true
)
{
{
await
WriteResponseStatusAsync
(
response
.
HttpVersion
,
response
.
StatusCode
,
response
.
StatusDescription
);
await
WriteResponseStatusAsync
(
response
.
HttpVersion
,
response
.
StatusCode
,
response
.
StatusDescription
);
await
Write
HeadersAsync
(
response
.
Headers
,
flush
);
await
Write
Async
(
response
,
flush
);
}
}
/// <summary>
/// <summary>
...
...
Titanium.Web.Proxy/Helpers/HttpWriter.cs
View file @
a007c1c0
...
@@ -5,7 +5,7 @@ using System.Text;
...
@@ -5,7 +5,7 @@ using System.Text;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
StreamExtended.Helpers
;
using
StreamExtended.Helpers
;
using
StreamExtended.Network
;
using
StreamExtended.Network
;
using
Titanium.Web.Proxy.
EventArguments
;
using
Titanium.Web.Proxy.
Compression
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Http
;
using
Titanium.Web.Proxy.Shared
;
using
Titanium.Web.Proxy.Shared
;
...
@@ -93,14 +93,11 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -93,14 +93,11 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="flush"></param>
/// <param name="flush"></param>
/// <returns></returns>
/// <returns></returns>
public
async
Task
WriteHeadersAsync
(
HeaderCollection
headers
,
bool
flush
=
true
)
public
async
Task
WriteHeadersAsync
(
HeaderCollection
headers
,
bool
flush
=
true
)
{
if
(
headers
!=
null
)
{
{
foreach
(
var
header
in
headers
)
foreach
(
var
header
in
headers
)
{
{
await
header
.
WriteToStreamAsync
(
this
);
await
header
.
WriteToStreamAsync
(
this
);
}
}
}
await
WriteLineAsync
();
await
WriteLineAsync
();
if
(
flush
)
if
(
flush
)
...
@@ -265,5 +262,22 @@ namespace Titanium.Web.Proxy.Helpers
...
@@ -265,5 +262,22 @@ namespace Titanium.Web.Proxy.Helpers
onCopy
?.
Invoke
(
buffer
,
0
,
bytesRead
);
onCopy
?.
Invoke
(
buffer
,
0
,
bytesRead
);
}
}
}
}
/// <summary>
/// Writes the request/response headers and body.
/// </summary>
/// <param name="requestResponse"></param>
/// <param name="flush"></param>
/// <returns></returns>
protected
async
Task
WriteAsync
(
RequestResponseBase
requestResponse
,
bool
flush
=
true
)
{
var
body
=
requestResponse
.
CompressBodyAndUpdateContentLength
();
await
WriteHeadersAsync
(
requestResponse
.
Headers
,
flush
);
if
(
body
!=
null
)
{
await
WriteBodyAsync
(
body
,
requestResponse
.
IsChunked
);
}
}
}
}
}
}
Titanium.Web.Proxy/Http/HttpWebClient.cs
View file @
a007c1c0
...
@@ -38,7 +38,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -38,7 +38,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// <summary>
/// Web Request.
/// Web Request.
/// </summary>
/// </summary>
public
Request
Request
{
get
;
internal
set
;
}
public
Request
Request
{
get
;
}
/// <summary>
/// <summary>
/// Web Response.
/// Web Response.
...
@@ -56,13 +56,13 @@ namespace Titanium.Web.Proxy.Http
...
@@ -56,13 +56,13 @@ namespace Titanium.Web.Proxy.Http
/// </summary>
/// </summary>
public
bool
IsHttps
=>
Request
.
IsHttps
;
public
bool
IsHttps
=>
Request
.
IsHttps
;
internal
HttpWebClient
(
int
bufferSize
)
internal
HttpWebClient
(
int
bufferSize
,
Request
request
=
null
,
Response
response
=
null
)
{
{
this
.
bufferSize
=
bufferSize
;
this
.
bufferSize
=
bufferSize
;
RequestId
=
Guid
.
NewGuid
();
RequestId
=
Guid
.
NewGuid
();
Request
=
new
Request
();
Request
=
request
??
new
Request
();
Response
=
new
Response
();
Response
=
response
??
new
Response
();
}
}
/// <summary>
/// <summary>
...
...
Titanium.Web.Proxy/Http/Request.cs
View file @
a007c1c0
...
@@ -37,7 +37,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -37,7 +37,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// <summary>
/// Has request body?
/// Has request body?
/// </summary>
/// </summary>
public
bool
HasBody
public
override
bool
HasBody
{
{
get
get
{
{
...
@@ -102,6 +102,11 @@ namespace Titanium.Web.Proxy.Http
...
@@ -102,6 +102,11 @@ namespace Titanium.Web.Proxy.Http
internal
override
void
EnsureBodyAvailable
(
bool
throwWhenNotReadYet
=
true
)
internal
override
void
EnsureBodyAvailable
(
bool
throwWhenNotReadYet
=
true
)
{
{
if
(
BodyInternal
!=
null
)
{
return
;
}
//GET request don't have a request body to read
//GET request don't have a request body to read
if
(!
HasBody
)
if
(!
HasBody
)
{
{
...
...
Titanium.Web.Proxy/Http/RequestResponseBase.cs
View file @
a007c1c0
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.ComponentModel
;
using
System.ComponentModel
;
using
System.IO
;
using
System.Linq
;
using
System.Linq
;
using
System.Text
;
using
System.Text
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.Compression
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Extensions
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
...
@@ -15,7 +16,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -15,7 +16,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// <summary>
/// Cached body content as byte array
/// Cached body content as byte array
/// </summary>
/// </summary>
pr
ivate
byte
[]
body
;
pr
otected
byte
[]
BodyInternal
;
/// <summary>
/// <summary>
/// Cached body as string
/// Cached body as string
...
@@ -126,16 +127,84 @@ namespace Titanium.Web.Proxy.Http
...
@@ -126,16 +127,84 @@ namespace Titanium.Web.Proxy.Http
get
get
{
{
EnsureBodyAvailable
();
EnsureBodyAvailable
();
return
body
;
return
BodyInternal
;
}
}
internal
set
internal
set
{
{
body
=
value
;
BodyInternal
=
value
;
bodyString
=
null
;
bodyString
=
null
;
//If there is a content length header update it
UpdateContentLength
();
}
}
}
}
/// <summary>
/// Has the request/response body?
/// </summary>
public
abstract
bool
HasBody
{
get
;
}
/// <summary>
/// Store weather the original request/response has body or not, since the user may change the parameters
/// </summary>
internal
bool
OriginalHasBody
;
internal
abstract
void
EnsureBodyAvailable
(
bool
throwWhenNotReadYet
=
true
);
internal
abstract
void
EnsureBodyAvailable
(
bool
throwWhenNotReadYet
=
true
);
/// <summary>
/// get the compressed body from given bytes
/// </summary>
/// <param name="encodingType"></param>
/// <param name="body"></param>
/// <returns></returns>
internal
byte
[]
GetCompressedBody
(
string
encodingType
,
byte
[]
body
)
{
var
compressor
=
CompressionFactory
.
GetCompression
(
encodingType
);
using
(
var
ms
=
new
MemoryStream
())
{
using
(
var
zip
=
compressor
.
GetStream
(
ms
))
{
zip
.
Write
(
body
,
0
,
body
.
Length
);
}
return
ms
.
ToArray
();
}
}
internal
byte
[]
CompressBodyAndUpdateContentLength
()
{
if
(!
IsBodyRead
&&
BodyInternal
==
null
)
{
return
null
;
}
bool
isChunked
=
IsChunked
;
string
contentEncoding
=
ContentEncoding
;
if
(
HasBody
)
{
var
body
=
Body
;
if
(
contentEncoding
!=
null
&&
body
!=
null
)
{
body
=
GetCompressedBody
(
contentEncoding
,
body
);
if
(
isChunked
==
false
)
{
ContentLength
=
body
.
Length
;
}
else
{
ContentLength
=
-
1
;
}
}
return
body
;
}
ContentLength
=
0
;
return
null
;
}
/// <summary>
/// <summary>
/// Body as string
/// Body as string
/// Use the encoding specified to decode the byte[] data to string
/// Use the encoding specified to decode the byte[] data to string
...
@@ -155,7 +224,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -155,7 +224,7 @@ namespace Titanium.Web.Proxy.Http
internal
void
UpdateContentLength
()
internal
void
UpdateContentLength
()
{
{
ContentLength
=
IsChunked
?
-
1
:
body
.
Length
;
ContentLength
=
IsChunked
?
-
1
:
BodyInternal
?.
Length
??
0
;
}
}
/// <summary>
/// <summary>
...
@@ -165,7 +234,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -165,7 +234,7 @@ namespace Titanium.Web.Proxy.Http
{
{
if
(!
KeepBody
)
if
(!
KeepBody
)
{
{
body
=
null
;
BodyInternal
=
null
;
bodyString
=
null
;
bodyString
=
null
;
}
}
}
}
...
...
Titanium.Web.Proxy/Http/Response.cs
View file @
a007c1c0
...
@@ -26,7 +26,7 @@ namespace Titanium.Web.Proxy.Http
...
@@ -26,7 +26,7 @@ namespace Titanium.Web.Proxy.Http
/// <summary>
/// <summary>
/// Has response body?
/// Has response body?
/// </summary>
/// </summary>
public
bool
HasBody
public
override
bool
HasBody
{
{
get
get
{
{
...
@@ -77,8 +77,15 @@ namespace Titanium.Web.Proxy.Http
...
@@ -77,8 +77,15 @@ namespace Titanium.Web.Proxy.Http
}
}
}
}
internal
bool
TerminateResponse
{
get
;
set
;
}
internal
override
void
EnsureBodyAvailable
(
bool
throwWhenNotReadYet
=
true
)
internal
override
void
EnsureBodyAvailable
(
bool
throwWhenNotReadYet
=
true
)
{
{
if
(
BodyInternal
!=
null
)
{
return
;
}
if
(!
IsBodyRead
&&
throwWhenNotReadYet
)
if
(!
IsBodyRead
&&
throwWhenNotReadYet
)
{
{
throw
new
Exception
(
"Response body is not read yet. "
+
throw
new
Exception
(
"Response body is not read yet. "
+
...
@@ -121,6 +128,21 @@ namespace Titanium.Web.Proxy.Http
...
@@ -121,6 +128,21 @@ namespace Titanium.Web.Proxy.Http
}
}
}
}
/// <summary>
/// Constructor.
/// </summary>
public
Response
()
{
}
/// <summary>
/// Constructor.
/// </summary>
public
Response
(
byte
[]
body
)
{
Body
=
body
;
}
internal
static
string
CreateResponseLine
(
Version
version
,
int
statusCode
,
string
statusDescription
)
internal
static
string
CreateResponseLine
(
Version
version
,
int
statusCode
,
string
statusDescription
)
{
{
return
$"HTTP/
{
version
.
Major
}
.
{
version
.
Minor
}
{
statusCode
}
{
statusDescription
}
"
;
return
$"HTTP/
{
version
.
Major
}
.
{
version
.
Minor
}
{
statusCode
}
{
statusDescription
}
"
;
...
...
Titanium.Web.Proxy/Http/Responses/OkResponse.cs
View file @
a007c1c0
...
@@ -15,5 +15,13 @@ namespace Titanium.Web.Proxy.Http.Responses
...
@@ -15,5 +15,13 @@ namespace Titanium.Web.Proxy.Http.Responses
StatusCode
=
(
int
)
HttpStatusCode
.
OK
;
StatusCode
=
(
int
)
HttpStatusCode
.
OK
;
StatusDescription
=
"OK"
;
StatusDescription
=
"OK"
;
}
}
/// <summary>
/// Constructor.
/// </summary>
public
OkResponse
(
byte
[]
body
)
:
this
()
{
Body
=
body
;
}
}
}
}
}
Titanium.Web.Proxy/RequestHandler.cs
View file @
a007c1c0
...
@@ -46,7 +46,6 @@ namespace Titanium.Web.Proxy
...
@@ -46,7 +46,6 @@ namespace Titanium.Web.Proxy
try
try
{
{
string
connectHostname
=
null
;
string
connectHostname
=
null
;
ConnectRequest
connectRequest
=
null
;
ConnectRequest
connectRequest
=
null
;
//Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request)
//Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request)
...
@@ -297,7 +296,7 @@ namespace Titanium.Web.Proxy
...
@@ -297,7 +296,7 @@ namespace Titanium.Web.Proxy
string
httpCmd
=
await
clientStreamReader
.
ReadLineAsync
();
string
httpCmd
=
await
clientStreamReader
.
ReadLineAsync
();
if
(
string
.
IsNullOrEmpty
(
httpCmd
))
if
(
string
.
IsNullOrEmpty
(
httpCmd
))
{
{
break
;
return
;
}
}
var
args
=
new
SessionEventArgs
(
BufferSize
,
endPoint
,
ExceptionFunc
)
var
args
=
new
SessionEventArgs
(
BufferSize
,
endPoint
,
ExceptionFunc
)
...
@@ -347,11 +346,12 @@ namespace Titanium.Web.Proxy
...
@@ -347,11 +346,12 @@ namespace Titanium.Web.Proxy
}
}
}
}
args
.
WebSession
.
Request
.
RequestUri
=
httpRemoteUri
;
var
request
=
args
.
WebSession
.
Request
;
args
.
WebSession
.
Request
.
OriginalUrl
=
httpUrl
;
request
.
RequestUri
=
httpRemoteUri
;
request
.
OriginalUrl
=
httpUrl
;
args
.
WebSession
.
R
equest
.
Method
=
httpMethod
;
r
equest
.
Method
=
httpMethod
;
args
.
WebSession
.
R
equest
.
HttpVersion
=
version
;
r
equest
.
HttpVersion
=
version
;
args
.
ProxyClient
.
ClientStream
=
clientStream
;
args
.
ProxyClient
.
ClientStream
=
clientStream
;
args
.
ProxyClient
.
ClientStreamReader
=
clientStreamReader
;
args
.
ProxyClient
.
ClientStreamReader
=
clientStreamReader
;
args
.
ProxyClient
.
ClientStreamWriter
=
clientStreamWriter
;
args
.
ProxyClient
.
ClientStreamWriter
=
clientStreamWriter
;
...
@@ -363,35 +363,40 @@ namespace Titanium.Web.Proxy
...
@@ -363,35 +363,40 @@ namespace Titanium.Web.Proxy
//send the response
//send the response
await
clientStreamWriter
.
WriteResponseAsync
(
args
.
WebSession
.
Response
);
await
clientStreamWriter
.
WriteResponseAsync
(
args
.
WebSession
.
Response
);
break
;
return
;
}
}
if
(!
isTransparentEndPoint
)
if
(!
isTransparentEndPoint
)
{
{
PrepareRequestHeaders
(
args
.
WebSession
.
R
equest
.
Headers
);
PrepareRequestHeaders
(
r
equest
.
Headers
);
args
.
WebSession
.
Request
.
Host
=
args
.
WebSession
.
R
equest
.
RequestUri
.
Authority
;
request
.
Host
=
r
equest
.
RequestUri
.
Authority
;
}
}
//if win auth is enabled
//if win auth is enabled
//we need a cache of request body
//we need a cache of request body
//so that we can send it after authentication in WinAuthHandler.cs
//so that we can send it after authentication in WinAuthHandler.cs
if
(
isWindowsAuthenticationEnabledAndSupported
&&
args
.
WebSession
.
R
equest
.
HasBody
)
if
(
isWindowsAuthenticationEnabledAndSupported
&&
r
equest
.
HasBody
)
{
{
await
args
.
GetRequestBody
();
await
args
.
GetRequestBody
();
}
}
request
.
OriginalHasBody
=
request
.
HasBody
;
//If user requested interception do it
//If user requested interception do it
await
InvokeBeforeRequest
(
args
);
await
InvokeBeforeRequest
(
args
);
var
response
=
args
.
WebSession
.
Response
;
var
response
=
args
.
WebSession
.
Response
;
if
(
args
.
WebSession
.
R
equest
.
CancelRequest
)
if
(
r
equest
.
CancelRequest
)
{
{
//syphon out the request body from client before setting the new body
await
args
.
SyphonOutBodyAsync
(
true
);
await
HandleHttpSessionResponse
(
args
);
await
HandleHttpSessionResponse
(
args
);
if
(!
response
.
KeepAlive
)
if
(!
response
.
KeepAlive
)
{
{
break
;
return
;
}
}
continue
;
continue
;
...
@@ -399,7 +404,7 @@ namespace Titanium.Web.Proxy
...
@@ -399,7 +404,7 @@ namespace Titanium.Web.Proxy
//create a new connection if hostname/upstream end point changes
//create a new connection if hostname/upstream end point changes
if
(
connection
!=
null
if
(
connection
!=
null
&&
(!
connection
.
HostName
.
Equals
(
args
.
WebSession
.
R
equest
.
RequestUri
.
Host
,
StringComparison
.
OrdinalIgnoreCase
)
&&
(!
connection
.
HostName
.
Equals
(
r
equest
.
RequestUri
.
Host
,
StringComparison
.
OrdinalIgnoreCase
)
||
(
args
.
WebSession
.
UpStreamEndPoint
!=
null
||
(
args
.
WebSession
.
UpStreamEndPoint
!=
null
&&
!
args
.
WebSession
.
UpStreamEndPoint
.
Equals
(
connection
.
UpStreamEndPoint
))))
&&
!
args
.
WebSession
.
UpStreamEndPoint
.
Equals
(
connection
.
UpStreamEndPoint
))))
{
{
...
@@ -413,12 +418,11 @@ namespace Titanium.Web.Proxy
...
@@ -413,12 +418,11 @@ namespace Titanium.Web.Proxy
}
}
//if upgrading to websocket then relay the requet without reading the contents
//if upgrading to websocket then relay the requet without reading the contents
if
(
args
.
WebSession
.
R
equest
.
UpgradeToWebSocket
)
if
(
r
equest
.
UpgradeToWebSocket
)
{
{
//prepare the prefix content
//prepare the prefix content
var
requestHeaders
=
args
.
WebSession
.
Request
.
Headers
;
await
connection
.
StreamWriter
.
WriteLineAsync
(
httpCmd
);
await
connection
.
StreamWriter
.
WriteLineAsync
(
httpCmd
);
await
connection
.
StreamWriter
.
WriteHeadersAsync
(
requestHeaders
);
await
connection
.
StreamWriter
.
WriteHeadersAsync
(
request
.
Headers
);
string
httpStatus
=
await
connection
.
StreamReader
.
ReadLineAsync
();
string
httpStatus
=
await
connection
.
StreamReader
.
ReadLineAsync
();
Response
.
ParseResponseLine
(
httpStatus
,
out
var
responseVersion
,
out
int
responseStatusCode
,
Response
.
ParseResponseLine
(
httpStatus
,
out
var
responseVersion
,
out
int
responseStatusCode
,
...
@@ -445,16 +449,22 @@ namespace Titanium.Web.Proxy
...
@@ -445,16 +449,22 @@ namespace Titanium.Web.Proxy
(
buffer
,
offset
,
count
)
=>
{
args
.
OnDataReceived
(
buffer
,
offset
,
count
);
},
(
buffer
,
offset
,
count
)
=>
{
args
.
OnDataReceived
(
buffer
,
offset
,
count
);
},
ExceptionFunc
);
ExceptionFunc
);
break
;
return
;
}
}
//construct the web request that we are going to issue on behalf of the client.
//construct the web request that we are going to issue on behalf of the client.
await
HandleHttpSessionRequestInternal
(
connection
,
args
);
await
HandleHttpSessionRequestInternal
(
connection
,
args
);
if
(
args
.
WebSession
.
ServerConnection
==
null
)
{
//server connection was closed
return
;
}
//if connection is closing exit
//if connection is closing exit
if
(!
response
.
KeepAlive
)
if
(!
response
.
KeepAlive
)
{
{
break
;
return
;
}
}
}
}
catch
(
Exception
e
)
when
(!(
e
is
ProxyHttpException
))
catch
(
Exception
e
)
when
(!(
e
is
ProxyHttpException
))
...
@@ -493,6 +503,8 @@ namespace Titanium.Web.Proxy
...
@@ -493,6 +503,8 @@ namespace Titanium.Web.Proxy
var
request
=
args
.
WebSession
.
Request
;
var
request
=
args
.
WebSession
.
Request
;
request
.
Locked
=
true
;
request
.
Locked
=
true
;
var
body
=
request
.
CompressBodyAndUpdateContentLength
();
//if expect continue is enabled then send the headers first
//if expect continue is enabled then send the headers first
//and see if server would return 100 conitinue
//and see if server would return 100 conitinue
if
(
request
.
ExpectContinue
)
if
(
request
.
ExpectContinue
)
...
@@ -531,25 +543,8 @@ namespace Titanium.Web.Proxy
...
@@ -531,25 +543,8 @@ namespace Titanium.Web.Proxy
//If request was modified by user
//If request was modified by user
if
(
request
.
IsBodyRead
)
if
(
request
.
IsBodyRead
)
{
{
bool
isChunked
=
request
.
IsChunked
;
var
writer
=
args
.
WebSession
.
ServerConnection
.
StreamWriter
;
string
contentEncoding
=
request
.
ContentEncoding
;
await
writer
.
WriteBodyAsync
(
body
,
request
.
IsChunked
);
var
body
=
request
.
Body
;
if
(
contentEncoding
!=
null
&&
body
!=
null
)
{
body
=
GetCompressedBody
(
contentEncoding
,
body
);
if
(
isChunked
==
false
)
{
request
.
ContentLength
=
body
.
Length
;
}
else
{
request
.
ContentLength
=
-
1
;
}
}
await
args
.
WebSession
.
ServerConnection
.
StreamWriter
.
WriteBodyAsync
(
body
,
isChunked
);
}
}
else
else
{
{
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
a007c1c0
...
@@ -43,6 +43,29 @@ namespace Titanium.Web.Proxy
...
@@ -43,6 +43,29 @@ namespace Titanium.Web.Proxy
await
InvokeBeforeResponse
(
args
);
await
InvokeBeforeResponse
(
args
);
}
}
// it may changed in the user event
response
=
args
.
WebSession
.
Response
;
var
clientStreamWriter
=
args
.
ProxyClient
.
ClientStreamWriter
;
if
(
response
.
TerminateResponse
||
response
.
Locked
)
{
await
clientStreamWriter
.
WriteResponseAsync
(
response
);
if
(!
response
.
TerminateResponse
)
{
//syphon out the response body from server before setting the new body
await
args
.
SyphonOutBodyAsync
(
false
);
}
else
{
args
.
WebSession
.
ServerConnection
.
Dispose
();
args
.
WebSession
.
ServerConnection
=
null
;
}
return
;
}
//if user requested to send request again
//if user requested to send request again
//likely after making modifications from User Response Handler
//likely after making modifications from User Response Handler
if
(
args
.
ReRequest
)
if
(
args
.
ReRequest
)
...
@@ -55,8 +78,6 @@ namespace Titanium.Web.Proxy
...
@@ -55,8 +78,6 @@ namespace Titanium.Web.Proxy
response
.
Locked
=
true
;
response
.
Locked
=
true
;
var
clientStreamWriter
=
args
.
ProxyClient
.
ClientStreamWriter
;
//Write back to client 100-conitinue response if that's what server returned
//Write back to client 100-conitinue response if that's what server returned
if
(
response
.
Is100Continue
)
if
(
response
.
Is100Continue
)
{
{
...
@@ -69,9 +90,6 @@ namespace Titanium.Web.Proxy
...
@@ -69,9 +90,6 @@ namespace Titanium.Web.Proxy
await
clientStreamWriter
.
WriteLineAsync
();
await
clientStreamWriter
.
WriteLineAsync
();
}
}
//Write back response status to client
await
clientStreamWriter
.
WriteResponseStatusAsync
(
response
.
HttpVersion
,
response
.
StatusCode
,
response
.
StatusDescription
);
if
(!
args
.
IsTransparent
)
if
(!
args
.
IsTransparent
)
{
{
response
.
Headers
.
FixProxyHeaders
();
response
.
Headers
.
FixProxyHeaders
();
...
@@ -79,29 +97,12 @@ namespace Titanium.Web.Proxy
...
@@ -79,29 +97,12 @@ namespace Titanium.Web.Proxy
if
(
response
.
IsBodyRead
)
if
(
response
.
IsBodyRead
)
{
{
bool
isChunked
=
response
.
IsChunked
;
await
clientStreamWriter
.
WriteResponseAsync
(
response
);
string
contentEncoding
=
response
.
ContentEncoding
;
var
body
=
response
.
Body
;
if
(
contentEncoding
!=
null
&&
body
!=
null
)
{
body
=
GetCompressedBody
(
contentEncoding
,
body
);
if
(
isChunked
==
false
)
{
response
.
ContentLength
=
body
.
Length
;
}
else
{
response
.
ContentLength
=
-
1
;
}
}
await
clientStreamWriter
.
WriteHeadersAsync
(
response
.
Headers
);
await
clientStreamWriter
.
WriteBodyAsync
(
body
,
isChunked
);
}
}
else
else
{
{
//Write back response status to client
await
clientStreamWriter
.
WriteResponseStatusAsync
(
response
.
HttpVersion
,
response
.
StatusCode
,
response
.
StatusDescription
);
await
clientStreamWriter
.
WriteHeadersAsync
(
response
.
Headers
);
await
clientStreamWriter
.
WriteHeadersAsync
(
response
.
Headers
);
//Write body if exists
//Write body if exists
...
@@ -117,27 +118,6 @@ namespace Titanium.Web.Proxy
...
@@ -117,27 +118,6 @@ namespace Titanium.Web.Proxy
}
}
}
}
/// <summary>
/// get the compressed body from given bytes
/// </summary>
/// <param name="encodingType"></param>
/// <param name="body"></param>
/// <returns></returns>
private
byte
[]
GetCompressedBody
(
string
encodingType
,
byte
[]
body
)
{
var
compressor
=
CompressionFactory
.
GetCompression
(
encodingType
);
using
(
var
ms
=
new
MemoryStream
())
{
using
(
var
zip
=
compressor
.
GetStream
(
ms
))
{
zip
.
Write
(
body
,
0
,
body
.
Length
);
}
return
ms
.
ToArray
();
}
}
private
async
Task
InvokeBeforeResponse
(
SessionEventArgs
args
)
private
async
Task
InvokeBeforeResponse
(
SessionEventArgs
args
)
{
{
if
(
BeforeResponse
!=
null
)
if
(
BeforeResponse
!=
null
)
...
...
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