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
bcd0747a
Commit
bcd0747a
authored
Mar 07, 2016
by
titanium007
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow modifying request headers
parent
39bdfe5d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
125 additions
and
72 deletions
+125
-72
SessionEventArgs.cs
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
+3
-4
HttpWebRequestExtensions.cs
Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs
+3
-3
HttpWebClient.cs
Titanium.Web.Proxy/Network/HttpWebClient.cs
+112
-19
RequestHandler.cs
Titanium.Web.Proxy/RequestHandler.cs
+7
-46
No files found.
Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs
View file @
bcd0747a
...
@@ -22,14 +22,13 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -22,14 +22,13 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary>
/// </summary>
public
class
SessionEventArgs
:
EventArgs
,
IDisposable
public
class
SessionEventArgs
:
EventArgs
,
IDisposable
{
{
readonly
int
_bufferSize
;
/// <summary>
/// <summary>
/// Constructor to initialize the proxy
/// Constructor to initialize the proxy
/// </summary>
/// </summary>
internal
SessionEventArgs
(
int
bufferSize
)
internal
SessionEventArgs
()
{
{
_bufferSize
=
bufferSize
;
Client
=
new
ProxyClient
();
Client
=
new
ProxyClient
();
ProxySession
=
new
HttpWebSession
();
ProxySession
=
new
HttpWebSession
();
}
}
...
@@ -406,7 +405,7 @@ namespace Titanium.Web.Proxy.EventArguments
...
@@ -406,7 +405,7 @@ namespace Titanium.Web.Proxy.EventArguments
connectStreamWriter
.
WriteLine
(
"Pragma: no-cache"
);
connectStreamWriter
.
WriteLine
(
"Pragma: no-cache"
);
connectStreamWriter
.
WriteLine
(
"Expires: 0"
);
connectStreamWriter
.
WriteLine
(
"Expires: 0"
);
connectStreamWriter
.
WriteLine
(
ProxySession
.
Request
.
IsAlive
?
"Connection: Keep-Alive"
:
"Connection: close"
);
//
connectStreamWriter.WriteLine(ProxySession.Request.IsAlive ? "Connection: Keep-Alive" : "Connection: close");
connectStreamWriter
.
WriteLine
();
connectStreamWriter
.
WriteLine
();
connectStreamWriter
.
Flush
();
connectStreamWriter
.
Flush
();
...
...
Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs
View file @
bcd0747a
...
@@ -10,15 +10,15 @@ namespace Titanium.Web.Proxy.Extensions
...
@@ -10,15 +10,15 @@ namespace Titanium.Web.Proxy.Extensions
public
static
class
HttpWebRequestExtensions
public
static
class
HttpWebRequestExtensions
{
{
//Get encoding of the HTTP request
//Get encoding of the HTTP request
public
static
Encoding
GetEncoding
(
this
HttpWebSession
request
)
public
static
Encoding
GetEncoding
(
this
Request
request
)
{
{
try
try
{
{
//return default if not specified
//return default if not specified
if
(
request
.
Request
.
ContentType
==
null
)
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
if
(
request
.
ContentType
==
null
)
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
//extract the encoding by finding the charset
//extract the encoding by finding the charset
var
contentTypes
=
request
.
Request
.
ContentType
.
Split
(
';'
);
var
contentTypes
=
request
.
ContentType
.
Split
(
';'
);
foreach
(
var
contentType
in
contentTypes
)
foreach
(
var
contentType
in
contentTypes
)
{
{
var
encodingSplit
=
contentType
.
Split
(
'='
);
var
encodingSplit
=
contentType
.
Split
(
'='
);
...
...
Titanium.Web.Proxy/Network/HttpWebClient.cs
View file @
bcd0747a
...
@@ -9,34 +9,124 @@ using System.Text;
...
@@ -9,34 +9,124 @@ using System.Text;
using
System.Threading.Tasks
;
using
System.Threading.Tasks
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Helpers
;
using
Titanium.Web.Proxy.Models
;
using
Titanium.Web.Proxy.Models
;
using
System.Linq
;
using
Titanium.Web.Proxy.Extensions
;
namespace
Titanium.Web.Proxy.Network
namespace
Titanium.Web.Proxy.Network
{
{
public
class
Request
public
class
Request
{
{
public
string
Method
{
get
;
internal
set
;
}
public
string
Method
{
get
;
set
;
}
public
Uri
RequestUri
{
get
;
internal
set
;
}
public
Uri
RequestUri
{
get
;
set
;
}
public
string
HttpVersion
{
get
;
internal
set
;
}
public
string
HttpVersion
{
get
;
set
;
}
public
string
Status
{
get
;
internal
set
;
}
public
string
Host
public
int
ContentLength
{
get
;
internal
set
;
}
{
public
bool
SendChunked
{
get
;
internal
set
;
}
get
public
string
ContentType
{
get
;
internal
set
;
}
{
public
bool
KeepAlive
{
get
;
internal
set
;
}
var
host
=
RequestHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
()
==
"host"
);
public
string
Hostname
{
get
;
internal
set
;
}
if
(
host
!=
null
)
return
host
.
Value
;
return
null
;
}
set
{
var
host
=
RequestHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
()
==
"host"
);
if
(
host
!=
null
)
host
.
Value
=
value
;
else
RequestHeaders
.
Add
(
new
HttpHeader
(
"Host"
,
value
));
}
}
public
string
Url
{
get
;
internal
set
;
}
public
int
ContentLength
{
get
{
var
header
=
RequestHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
()
==
"content-length"
);
if
(
header
==
null
)
return
0
;
int
contentLen
;
int
.
TryParse
(
header
.
Value
,
out
contentLen
);
if
(
contentLen
!=
0
)
return
contentLen
;
return
0
;
}
set
{
var
header
=
RequestHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
()
==
"content-length"
);
if
(
header
!=
null
)
header
.
Value
=
value
.
ToString
();
else
RequestHeaders
.
Add
(
new
HttpHeader
(
"content-length"
,
value
.
ToString
()));
}
}
public
string
ContentType
{
get
{
var
header
=
RequestHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
()
==
"content-type"
);
if
(
header
!=
null
)
return
header
.
Value
;
return
null
;
}
set
{
var
header
=
RequestHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
()
==
"content-type"
);
if
(
header
!=
null
)
header
.
Value
=
value
.
ToString
();
else
RequestHeaders
.
Add
(
new
HttpHeader
(
"content-type"
,
value
.
ToString
()));
}
}
public
bool
SendChunked
{
get
{
var
header
=
RequestHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
()
==
"transfer-encoding"
);
if
(
header
!=
null
)
return
header
.
Value
.
ToLower
().
Contains
(
"chunked"
);
return
false
;
}
}
public
string
Url
{
get
{
return
RequestUri
.
OriginalString
;
}
}
internal
Encoding
Encoding
{
get
{
return
this
.
GetEncoding
();
}
}
internal
Encoding
Encoding
{
get
;
set
;
}
internal
bool
IsAlive
{
get
;
set
;
}
internal
bool
CancelRequest
{
get
;
set
;
}
internal
bool
CancelRequest
{
get
;
set
;
}
internal
byte
[]
RequestBody
{
get
;
set
;
}
internal
byte
[]
RequestBody
{
get
;
set
;
}
internal
string
RequestBodyString
{
get
;
set
;
}
internal
string
RequestBodyString
{
get
;
set
;
}
internal
bool
RequestBodyRead
{
get
;
set
;
}
internal
bool
RequestBodyRead
{
get
;
set
;
}
internal
bool
UpgradeToWebSocket
{
get
;
set
;
}
public
List
<
HttpHeader
>
RequestHeaders
{
get
;
internal
set
;
}
internal
bool
RequestLocked
{
get
;
set
;
}
internal
bool
RequestLocked
{
get
;
set
;
}
internal
bool
UpgradeToWebSocket
{
get
{
var
header
=
RequestHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
()
==
"upgrade"
);
if
(
header
==
null
)
return
false
;
if
(
header
.
Value
.
ToLower
()
==
"websocket"
)
return
true
;
return
false
;
}
}
public
List
<
HttpHeader
>
RequestHeaders
{
get
;
set
;
}
public
Request
()
public
Request
()
{
{
this
.
RequestHeaders
=
new
List
<
HttpHeader
>();
this
.
RequestHeaders
=
new
List
<
HttpHeader
>();
...
@@ -46,6 +136,8 @@ namespace Titanium.Web.Proxy.Network
...
@@ -46,6 +136,8 @@ namespace Titanium.Web.Proxy.Network
public
class
Response
public
class
Response
{
{
public
string
ResponseStatusCode
{
get
;
set
;
}
public
string
ResponseStatusDescription
{
get
;
set
;
}
internal
Encoding
Encoding
{
get
;
set
;
}
internal
Encoding
Encoding
{
get
;
set
;
}
internal
Stream
ResponseStream
{
get
;
set
;
}
internal
Stream
ResponseStream
{
get
;
set
;
}
...
@@ -53,22 +145,23 @@ namespace Titanium.Web.Proxy.Network
...
@@ -53,22 +145,23 @@ namespace Titanium.Web.Proxy.Network
internal
string
ResponseBodyString
{
get
;
set
;
}
internal
string
ResponseBodyString
{
get
;
set
;
}
internal
bool
ResponseBodyRead
{
get
;
set
;
}
internal
bool
ResponseBodyRead
{
get
;
set
;
}
internal
bool
ResponseLocked
{
get
;
set
;
}
internal
bool
ResponseLocked
{
get
;
set
;
}
public
List
<
HttpHeader
>
ResponseHeaders
{
get
;
internal
set
;
}
internal
string
CharacterSet
{
get
;
set
;
}
internal
string
CharacterSet
{
get
;
set
;
}
internal
string
ContentEncoding
{
get
;
set
;
}
internal
string
ContentEncoding
{
get
;
set
;
}
internal
string
HttpVersion
{
get
;
set
;
}
internal
string
HttpVersion
{
get
;
set
;
}
public
string
ResponseStatusCode
{
get
;
internal
set
;
}
public
string
ResponseStatusDescription
{
get
;
internal
set
;
}
internal
bool
ResponseKeepAlive
{
get
;
set
;
}
internal
bool
ResponseKeepAlive
{
get
;
set
;
}
public
string
ContentType
{
get
;
internal
set
;
}
public
string
ContentType
{
get
;
internal
set
;
}
internal
int
ContentLength
{
get
;
set
;
}
internal
int
ContentLength
{
get
;
set
;
}
internal
bool
IsChunked
{
get
;
set
;
}
internal
bool
IsChunked
{
get
;
set
;
}
public
List
<
HttpHeader
>
ResponseHeaders
{
get
;
internal
set
;
}
public
Response
()
public
Response
()
{
{
this
.
ResponseHeaders
=
new
List
<
HttpHeader
>();
this
.
ResponseHeaders
=
new
List
<
HttpHeader
>();
this
.
ResponseKeepAlive
=
true
;
this
.
ResponseKeepAlive
=
true
;
}
}
}
}
public
class
HttpWebSession
public
class
HttpWebSession
...
@@ -129,7 +222,7 @@ namespace Titanium.Web.Proxy.Network
...
@@ -129,7 +222,7 @@ namespace Titanium.Web.Proxy.Network
{
{
var
httpResult
=
ProxyClient
.
ServerStreamReader
.
ReadLine
().
Split
(
new
char
[]
{
' '
},
3
);
var
httpResult
=
ProxyClient
.
ServerStreamReader
.
ReadLine
().
Split
(
new
char
[]
{
' '
},
3
);
if
(
string
.
IsNullOrEmpty
(
httpResult
[
0
]))
if
(
string
.
IsNullOrEmpty
(
httpResult
[
0
]))
{
{
var
s
=
ProxyClient
.
ServerStreamReader
.
ReadLine
();
var
s
=
ProxyClient
.
ServerStreamReader
.
ReadLine
();
}
}
...
...
Titanium.Web.Proxy/RequestHandler.cs
View file @
bcd0747a
...
@@ -184,7 +184,7 @@ namespace Titanium.Web.Proxy
...
@@ -184,7 +184,7 @@ namespace Titanium.Web.Proxy
break
;
break
;
}
}
var
args
=
new
SessionEventArgs
(
BUFFER_SIZE
);
var
args
=
new
SessionEventArgs
();
args
.
Client
.
TcpClient
=
client
;
args
.
Client
.
TcpClient
=
client
;
try
try
...
@@ -219,7 +219,7 @@ namespace Titanium.Web.Proxy
...
@@ -219,7 +219,7 @@ namespace Titanium.Web.Proxy
SetRequestHeaders
(
args
.
ProxySession
.
Request
.
RequestHeaders
,
args
.
ProxySession
);
SetRequestHeaders
(
args
.
ProxySession
.
Request
.
RequestHeaders
,
args
.
ProxySession
);
var
httpRemoteUri
=
new
Uri
(!
IsHttps
?
httpCmdSplit
[
1
]
:
(
string
.
Concat
(
"https://"
,
args
.
ProxySession
.
Request
.
Host
name
,
httpCmdSplit
[
1
])));
var
httpRemoteUri
=
new
Uri
(!
IsHttps
?
httpCmdSplit
[
1
]
:
(
string
.
Concat
(
"https://"
,
args
.
ProxySession
.
Request
.
Host
,
httpCmdSplit
[
1
])));
args
.
IsHttps
=
IsHttps
;
args
.
IsHttps
=
IsHttps
;
if
(
args
.
ProxySession
.
Request
.
UpgradeToWebSocket
)
if
(
args
.
ProxySession
.
Request
.
UpgradeToWebSocket
)
...
@@ -237,14 +237,11 @@ namespace Titanium.Web.Proxy
...
@@ -237,14 +237,11 @@ namespace Titanium.Web.Proxy
args
.
Client
.
ClientStream
=
clientStream
;
args
.
Client
.
ClientStream
=
clientStream
;
args
.
Client
.
ClientStreamReader
=
clientStreamReader
;
args
.
Client
.
ClientStreamReader
=
clientStreamReader
;
args
.
Client
.
ClientStreamWriter
=
clientStreamWriter
;
args
.
Client
.
ClientStreamWriter
=
clientStreamWriter
;
args
.
ProxySession
.
Request
.
Hostname
=
args
.
ProxySession
.
Request
.
RequestUri
.
Host
;
args
.
ProxySession
.
Request
.
Host
=
args
.
ProxySession
.
Request
.
RequestUri
.
Host
;
args
.
ProxySession
.
Request
.
Url
=
args
.
ProxySession
.
Request
.
RequestUri
.
OriginalString
;
//If requested interception
//If requested interception
if
(
BeforeRequest
!=
null
)
if
(
BeforeRequest
!=
null
)
{
{
args
.
ProxySession
.
Request
.
Encoding
=
args
.
ProxySession
.
GetEncoding
();
BeforeRequest
(
null
,
args
);
BeforeRequest
(
null
,
args
);
}
}
...
@@ -260,10 +257,10 @@ namespace Titanium.Web.Proxy
...
@@ -260,10 +257,10 @@ namespace Titanium.Web.Proxy
//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.
connection
=
connection
==
null
?
connection
=
connection
==
null
?
TcpConnectionManager
.
GetClient
(
args
.
ProxySession
.
Request
.
RequestUri
.
Host
,
args
.
ProxySession
.
Request
.
RequestUri
.
Port
,
args
.
IsHttps
)
TcpConnectionManager
.
GetClient
(
args
.
ProxySession
.
Request
.
RequestUri
.
Host
,
args
.
ProxySession
.
Request
.
RequestUri
.
Port
,
args
.
IsHttps
)
:
lastRequestHostName
!=
args
.
ProxySession
.
Request
.
Host
name
?
TcpConnectionManager
.
GetClient
(
args
.
ProxySession
.
Request
.
RequestUri
.
Host
,
args
.
ProxySession
.
Request
.
RequestUri
.
Port
,
args
.
IsHttps
)
:
lastRequestHostName
!=
args
.
ProxySession
.
Request
.
Host
?
TcpConnectionManager
.
GetClient
(
args
.
ProxySession
.
Request
.
RequestUri
.
Host
,
args
.
ProxySession
.
Request
.
RequestUri
.
Port
,
args
.
IsHttps
)
:
connection
;
:
connection
;
lastRequestHostName
=
args
.
ProxySession
.
Request
.
Host
name
;
lastRequestHostName
=
args
.
ProxySession
.
Request
.
Host
;
args
.
ProxySession
.
SetConnection
(
connection
);
args
.
ProxySession
.
SetConnection
(
connection
);
args
.
ProxySession
.
SendRequest
();
args
.
ProxySession
.
SendRequest
();
...
@@ -326,44 +323,8 @@ namespace Titanium.Web.Proxy
...
@@ -326,44 +323,8 @@ namespace Titanium.Web.Proxy
{
{
case
"accept-encoding"
:
case
"accept-encoding"
:
requestHeaders
[
i
].
Value
=
"gzip,deflate,zlib"
;
requestHeaders
[
i
].
Value
=
"gzip,deflate,zlib"
;
break
;
break
;
case
"connection"
:
if
(
requestHeaders
[
i
].
Value
.
ToLower
()
==
"keep-alive"
)
webRequest
.
Request
.
KeepAlive
=
true
;
break
;
case
"content-length"
:
int
contentLen
;
int
.
TryParse
(
requestHeaders
[
i
].
Value
,
out
contentLen
);
if
(
contentLen
!=
0
)
webRequest
.
Request
.
ContentLength
=
contentLen
;
break
;
case
"content-type"
:
webRequest
.
Request
.
ContentType
=
requestHeaders
[
i
].
Value
;
break
;
case
"host"
:
webRequest
.
Request
.
Hostname
=
requestHeaders
[
i
].
Value
;
break
;
case
"proxy-connection"
:
if
(
requestHeaders
[
i
].
Value
.
ToLower
()
==
"keep-alive"
)
webRequest
.
Request
.
KeepAlive
=
true
;
else
if
(
requestHeaders
[
i
].
Value
.
ToLower
()
==
"close"
)
webRequest
.
Request
.
KeepAlive
=
false
;
break
;
case
"upgrade"
:
if
(
requestHeaders
[
i
].
Value
.
ToLower
()
==
"websocket"
)
webRequest
.
Request
.
UpgradeToWebSocket
=
true
;
break
;
//revisit this, transfer-encoding is not a request header according to spec
//But how to identify if client is sending chunked body for PUT/POST?
case
"transfer-encoding"
:
if
(
requestHeaders
[
i
].
Value
.
ToLower
().
Contains
(
"chunked"
))
webRequest
.
Request
.
SendChunked
=
true
;
else
webRequest
.
Request
.
SendChunked
=
false
;
break
;
default
:
default
:
break
;
break
;
}
}
...
...
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