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
fd5244d6
Commit
fd5244d6
authored
Mar 02, 2016
by
justcoding121
Committed by
justcoding121
Mar 02, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add comments
parent
3dbea1e3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
35 deletions
+106
-35
SessionEventArgs.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+106
-33
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+0
-2
No files found.
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
fd5244d6
...
...
@@ -13,35 +13,48 @@ using Titanium.Web.Proxy.Models;
namespace
Titanium.Web.Proxy.EventArguments
{
public
class
Client
{
internal
TcpClient
TcpClient
{
get
;
set
;
}
internal
Stream
ClientStream
{
get
;
set
;
}
internal
CustomBinaryReader
ClientStreamReader
{
get
;
set
;
}
internal
StreamWriter
ClientStreamWriter
{
get
;
set
;
}
public
int
ClientPort
{
get
;
internal
set
;
}
public
IPAddress
ClientIpAddress
{
get
;
internal
set
;
}
}
/// <summary>
/// Holds info related to a single proxy session (single request/response sequence)
/// A proxy session is bounded to a single connection from client
/// A proxy session ends when client terminates connection to proxy
/// or when server terminates connection from proxy
/// </summary>
public
class
SessionEventArgs
:
EventArgs
,
IDisposable
{
readonly
int
_bufferSize
;
/// <summary>
/// Constructor to initialize the proxy
/// </summary>
internal
SessionEventArgs
(
int
bufferSize
)
{
_bufferSize
=
bufferSize
;
Client
=
new
Client
();
Client
=
new
Proxy
Client
();
ProxySession
=
new
HttpWebSession
();
}
internal
Client
Client
{
get
;
set
;
}
/// <summary>
/// Holds a reference to server connection
/// </summary>
internal
ProxyClient
Client
{
get
;
set
;
}
/// <summary>
/// Does this session uses SSL
/// </summary>
public
bool
IsHttps
{
get
;
internal
set
;
}
/// <summary>
/// A web session corresponding to a single request/response sequence
/// within a proxy connection
/// </summary>
public
HttpWebSession
ProxySession
{
get
;
set
;
}
/// <summary>
/// A shortcut to get the request content length
/// </summary>
public
int
RequestContentLength
{
get
...
...
@@ -50,17 +63,25 @@ namespace Titanium.Web.Proxy.EventArguments
}
}
/// <summary>
/// A shortcut to get the request Method (GET/POST/PUT etc)
/// </summary>
public
string
RequestMethod
{
get
{
return
ProxySession
.
Request
.
Method
;
}
}
/// <summary>
/// A shortcut to get the response status code (200 OK, 404 etc)
/// </summary>
public
string
ResponseStatusCode
{
get
{
return
ProxySession
.
Response
.
ResponseStatusCode
;
}
}
/// <summary>
/// A shortcut to get the response content type
/// </summary>
public
string
ResponseContentType
{
get
...
...
@@ -69,30 +90,39 @@ namespace Titanium.Web.Proxy.EventArguments
}
}
/// <summary>
/// implement any cleanup here
/// </summary>
public
void
Dispose
()
{
}
/// <summary>
/// Read request body content as bytes[] for current session
/// </summary>
private
void
ReadRequestBody
()
{
//GET request don't have a request body to read
if
((
ProxySession
.
Request
.
Method
.
ToUpper
()
!=
"POST"
&&
ProxySession
.
Request
.
Method
.
ToUpper
()
!=
"PUT"
))
{
throw
new
BodyNotFoundException
(
"Request don't have a body."
+
"Please verify that this request is a Http POST/PUT and request content length is greater than zero before accessing the body."
);
}
//Caching check
if
(
ProxySession
.
Request
.
RequestBody
==
null
)
{
var
isChunked
=
false
;
string
requestContentEncoding
=
null
;
//get compression method (gzip, zlib etc)
if
(
ProxySession
.
Request
.
RequestHeaders
.
Any
(
x
=>
x
.
Name
.
ToLower
()
==
"content-encoding"
))
{
requestContentEncoding
=
ProxySession
.
Request
.
RequestHeaders
.
First
(
x
=>
x
.
Name
.
ToLower
()
==
"content-encoding"
).
Value
;
}
//check if the request have chunked body (body send chunck by chunck without a fixed length)
if
(
ProxySession
.
Request
.
RequestHeaders
.
Any
(
x
=>
x
.
Name
.
ToLower
()
==
"transfer-encoding"
))
{
var
transferEncoding
=
...
...
@@ -103,13 +133,14 @@ namespace Titanium.Web.Proxy.EventArguments
}
}
//If not chunked then its easy just read the whole body with the content length mentioned in the request header
if
(
requestContentEncoding
==
null
&&
!
isChunked
)
ProxySession
.
Request
.
RequestBody
=
this
.
Client
.
ClientStreamReader
.
ReadBytes
(
RequestContentLength
);
else
{
using
(
var
requestBodyStream
=
new
MemoryStream
())
{
//For chunked request we need to read data as they arrive, until we reach a chunk end symbol
if
(
isChunked
)
{
while
(
true
)
...
...
@@ -126,6 +157,7 @@ namespace Titanium.Web.Proxy.EventArguments
}
else
{
//chunk end
this
.
Client
.
ClientStreamReader
.
ReadLine
();
break
;
}
...
...
@@ -134,6 +166,7 @@ namespace Titanium.Web.Proxy.EventArguments
try
{
//decompress
switch
(
requestContentEncoding
)
{
case
"gzip"
:
...
...
@@ -152,20 +185,29 @@ namespace Titanium.Web.Proxy.EventArguments
}
catch
{
//if decompression fails, just assign the body stream as it it
//Not a safe option
ProxySession
.
Request
.
RequestBody
=
requestBodyStream
.
ToArray
();
}
}
}
}
//Now set the flag to true
//So that next time we can deliver body from cache
ProxySession
.
Request
.
RequestBodyRead
=
true
;
}
/// <summary>
/// Read response body as byte[] for current response
/// </summary>
private
void
ReadResponseBody
()
{
//If not already read (not cached yet)
if
(
ProxySession
.
Response
.
ResponseBody
==
null
)
{
using
(
var
responseBodyStream
=
new
MemoryStream
())
{
//If chuncked the read chunk by chunk until we hit chunk end symbol
if
(
ProxySession
.
Response
.
IsChunked
)
{
while
(
true
)
...
...
@@ -182,6 +224,7 @@ namespace Titanium.Web.Proxy.EventArguments
}
else
{
//chuck end
ProxySession
.
ProxyClient
.
ServerStreamReader
.
ReadLine
();
break
;
}
...
...
@@ -189,10 +232,11 @@ namespace Titanium.Web.Proxy.EventArguments
}
else
{
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
var
buffer
=
ProxySession
.
ProxyClient
.
ServerStreamReader
.
ReadBytes
(
ProxySession
.
Response
.
ContentLength
);
responseBodyStream
.
Write
(
buffer
,
0
,
buffer
.
Length
);
}
//decompress
switch
(
ProxySession
.
Response
.
ContentEncoding
)
{
case
"gzip"
:
...
...
@@ -209,19 +253,15 @@ namespace Titanium.Web.Proxy.EventArguments
break
;
}
}
//set this to true for caching
ProxySession
.
Response
.
ResponseBodyRead
=
true
;
}
}
public
Encoding
GetRequestBodyEncoding
()
{
if
(
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
return
ProxySession
.
Request
.
Encoding
;
}
/// <summary>
/// Gets the request body as bytes
/// </summary>
/// <returns></returns>
public
byte
[]
GetRequestBody
()
{
if
(
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
...
...
@@ -229,7 +269,10 @@ namespace Titanium.Web.Proxy.EventArguments
ReadRequestBody
();
return
ProxySession
.
Request
.
RequestBody
;
}
/// <summary>
/// Gets the request body as string
/// </summary>
/// <returns></returns>
public
string
GetRequestBodyAsString
()
{
if
(
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
...
...
@@ -237,13 +280,19 @@ namespace Titanium.Web.Proxy.EventArguments
ReadRequestBody
();
//Use the encoding specified in request to decode the byte[] data to string
return
ProxySession
.
Request
.
RequestBodyString
??
(
ProxySession
.
Request
.
RequestBodyString
=
ProxySession
.
Request
.
Encoding
.
GetString
(
ProxySession
.
Request
.
RequestBody
));
}
/// <summary>
/// Sets the request body
/// </summary>
/// <param name="body"></param>
public
void
SetRequestBody
(
byte
[]
body
)
{
if
(
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
//syphon out the request body from client before setting the new body
if
(!
ProxySession
.
Request
.
RequestBodyRead
)
{
ReadRequestBody
();
...
...
@@ -253,10 +302,15 @@ namespace Titanium.Web.Proxy.EventArguments
ProxySession
.
Request
.
RequestBodyRead
=
true
;
}
/// <summary>
/// Sets the body with the specified string
/// </summary>
/// <param name="body"></param>
public
void
SetRequestBodyString
(
string
body
)
{
if
(
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
//syphon out the request body from client before setting the new body
if
(!
ProxySession
.
Request
.
RequestBodyRead
)
{
ReadRequestBody
();
...
...
@@ -266,13 +320,10 @@ namespace Titanium.Web.Proxy.EventArguments
ProxySession
.
Request
.
RequestBodyRead
=
true
;
}
public
Encoding
GetResponseBodyEncoding
()
{
if
(!
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function before request is made to server."
);
return
ProxySession
.
Response
.
Encoding
;
}
/// <summary>
/// Gets the response body as byte array
/// </summary>
/// <returns></returns>
public
byte
[]
GetResponseBody
()
{
if
(!
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function before request is made to server."
);
...
...
@@ -281,6 +332,10 @@ namespace Titanium.Web.Proxy.EventArguments
return
ProxySession
.
Response
.
ResponseBody
;
}
/// <summary>
/// Gets the response body as string
/// </summary>
/// <returns></returns>
public
string
GetResponseBodyAsString
()
{
if
(!
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function before request is made to server."
);
...
...
@@ -290,10 +345,15 @@ namespace Titanium.Web.Proxy.EventArguments
return
ProxySession
.
Response
.
ResponseBodyString
??
(
ProxySession
.
Response
.
ResponseBodyString
=
ProxySession
.
Response
.
Encoding
.
GetString
(
ProxySession
.
Response
.
ResponseBody
));
}
/// <summary>
/// Set the response body bytes
/// </summary>
/// <param name="body"></param>
public
void
SetResponseBody
(
byte
[]
body
)
{
if
(!
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function before request is made to server."
);
//syphon out the response body from server before setting the new body
if
(
ProxySession
.
Response
.
ResponseBody
==
null
)
{
GetResponseBody
();
...
...
@@ -302,10 +362,15 @@ namespace Titanium.Web.Proxy.EventArguments
ProxySession
.
Response
.
ResponseBody
=
body
;
}
/// <summary>
/// Replace the response body with the specified string
/// </summary>
/// <param name="body"></param>
public
void
SetResponseBodyString
(
string
body
)
{
if
(!
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function before request is made to server."
);
//syphon out the response body from server before setting the new body
if
(
ProxySession
.
Response
.
ResponseBody
==
null
)
{
GetResponseBody
();
...
...
@@ -316,6 +381,14 @@ namespace Titanium.Web.Proxy.EventArguments
}
/// <summary>
/// Before request is made to server
/// Respond with the specified HTML string to client
/// and ignore the request
/// Marking as obsolete, need to comeup with a generic responder method in future
/// </summary>
/// <param name="html"></param>
[
Obsolete
]
public
void
Ok
(
string
html
)
{
if
(
ProxySession
.
Request
.
RequestLocked
)
throw
new
Exception
(
"You cannot call this function after request is made to server."
);
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
fd5244d6
...
...
@@ -239,8 +239,6 @@ namespace Titanium.Web.Proxy
args
.
Client
.
ClientStreamWriter
=
clientStreamWriter
;
args
.
ProxySession
.
Request
.
Hostname
=
args
.
ProxySession
.
Request
.
RequestUri
.
Host
;
args
.
ProxySession
.
Request
.
Url
=
args
.
ProxySession
.
Request
.
RequestUri
.
OriginalString
;
args
.
Client
.
ClientPort
=
((
IPEndPoint
)
client
.
Client
.
RemoteEndPoint
).
Port
;
args
.
Client
.
ClientIpAddress
=
((
IPEndPoint
)
client
.
Client
.
RemoteEndPoint
).
Address
;
//If requested interception
...
...
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