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
7fd744bb
Commit
7fd744bb
authored
Mar 21, 2016
by
titanium007
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix issue #47
Allow developers to modify request/response headers
parent
bcd0747a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
141 additions
and
91 deletions
+141
-91
ProxyTestController.cs
Titanium.Web.Proxy.Test/ProxyTestController.cs
+33
-33
HttpWebResponseExtensions.cs
Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs
+3
-3
HttpWebClient.cs
Titanium.Web.Proxy/Network/HttpWebClient.cs
+104
-12
ResponseHandler.cs
Titanium.Web.Proxy/ResponseHandler.cs
+1
-43
No files found.
Titanium.Web.Proxy.Test/ProxyTestController.cs
View file @
7fd744bb
...
...
@@ -20,7 +20,7 @@ namespace Titanium.Web.Proxy.Test
//Usefull for clients that use certificate pinning
//for example dropbox.com
var
explicitEndPoint
=
new
ExplicitProxyEndPoint
(
IPAddress
.
Any
,
8000
,
true
){
ExcludedHttpsHostNameRegex
=
new
List
<
string
>()
{
"dropbox.com"
}
// ExcludedHttpsHostNameRegex = new List<string>() { "google.com",
"dropbox.com" }
};
//An explicit endpoint is where the client knows about the existance of a proxy
...
...
@@ -68,49 +68,49 @@ namespace Titanium.Web.Proxy.Test
{
Console
.
WriteLine
(
e
.
ProxySession
.
Request
.
Url
);
//
//
read request headers
//
var requestHeaders = e.ProxySession.Request.RequestHeaders;
//read request headers
var
requestHeaders
=
e
.
ProxySession
.
Request
.
RequestHeaders
;
//
if ((e.RequestMethod.ToUpper() == "POST" || e.RequestMethod.ToUpper() == "PUT"))
//
{
//
//Get/Set request body bytes
//
byte[] bodyBytes = e.GetRequestBody();
//
e.SetRequestBody(bodyBytes);
if
((
e
.
RequestMethod
.
ToUpper
()
==
"POST"
||
e
.
RequestMethod
.
ToUpper
()
==
"PUT"
))
{
//Get/Set request body bytes
byte
[]
bodyBytes
=
e
.
GetRequestBody
();
e
.
SetRequestBody
(
bodyBytes
);
//
//Get/Set request body as string
//
string bodyString = e.GetRequestBodyAsString();
//
e.SetRequestBodyString(bodyString);
//Get/Set request body as string
string
bodyString
=
e
.
GetRequestBodyAsString
();
e
.
SetRequestBodyString
(
bodyString
);
//
}
}
//
//
To cancel a request with a custom HTML content
//
//
Filter URL
//To cancel a request with a custom HTML content
//Filter URL
//if (e.ProxySession.Request.RequestUrl
.Contains("google.com"))
//
{
//
e.Ok("<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>");
//
}
if
(
e
.
ProxySession
.
Request
.
RequestUri
.
AbsoluteUri
.
Contains
(
"google.com"
))
{
e
.
Ok
(
"<!DOCTYPE html><html><body><h1>Website Blocked</h1><p>Blocked by titanium web proxy.</p></body></html>"
);
}
}
//Test script injection
//Insert script to read the Browser URL and send it back to proxy
public
void
OnResponse
(
object
sender
,
SessionEventArgs
e
)
{
//
//
read response headers
//
var responseHeaders = e.ProxySession.Response.ResponseHeaders;
//if (!e.ProxySession.Request.Host
name
.Equals("medeczane.sgk.gov.tr")) return;
//
if (e.RequestMethod == "GET" || e.RequestMethod == "POST")
//
{
//
if (e.ProxySession.Response.ResponseStatusCode == "200")
//
{
//
if (e.ProxySession.Response.ContentType.Trim().ToLower().Contains("text/html"))
//
{
// string body = e.GetResponseBodyAsString();
//
}
//
}
//
}
//read response headers
var
responseHeaders
=
e
.
ProxySession
.
Response
.
ResponseHeaders
;
//if (!e.ProxySession.Request.Host.Equals("medeczane.sgk.gov.tr")) return;
if
(
e
.
RequestMethod
==
"GET"
||
e
.
RequestMethod
==
"POST"
)
{
if
(
e
.
ProxySession
.
Response
.
ResponseStatusCode
==
"200"
)
{
if
(
e
.
ProxySession
.
Response
.
ContentType
.
Trim
().
ToLower
().
Contains
(
"text/html"
))
{
string
body
=
e
.
GetResponseBodyAsString
();
}
}
}
}
}
}
\ No newline at end of file
Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs
View file @
7fd744bb
...
...
@@ -6,14 +6,14 @@ namespace Titanium.Web.Proxy.Extensions
{
public
static
class
HttpWebResponseExtensions
{
public
static
Encoding
GetResponseEncoding
(
this
HttpWebSession
response
)
public
static
Encoding
GetResponseEncoding
(
this
Response
response
)
{
if
(
string
.
IsNullOrEmpty
(
response
.
Response
.
CharacterSet
))
if
(
string
.
IsNullOrEmpty
(
response
.
CharacterSet
))
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
try
{
return
Encoding
.
GetEncoding
(
response
.
Response
.
CharacterSet
.
Replace
(
@""""
,
string
.
Empty
));
return
Encoding
.
GetEncoding
(
response
.
CharacterSet
.
Replace
(
@""""
,
string
.
Empty
));
}
catch
{
return
Encoding
.
GetEncoding
(
"ISO-8859-1"
);
}
}
...
...
Titanium.Web.Proxy/Network/HttpWebClient.cs
View file @
7fd744bb
...
...
@@ -139,28 +139,120 @@ namespace Titanium.Web.Proxy.Network
public
string
ResponseStatusCode
{
get
;
set
;
}
public
string
ResponseStatusDescription
{
get
;
set
;
}
internal
Encoding
Encoding
{
get
;
set
;
}
internal
Encoding
Encoding
{
get
{
return
this
.
GetResponseEncoding
();
}
}
internal
string
CharacterSet
{
get
{
if
(
this
.
ContentType
.
Contains
(
";"
))
{
return
this
.
ContentType
.
Split
(
';'
)[
1
].
Substring
(
9
).
Trim
();
}
return
null
;
}
}
internal
string
ContentEncoding
{
get
{
var
header
=
this
.
ResponseHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
().
Equals
(
"content-encoding"
));
if
(
header
!=
null
)
{
return
header
.
Value
.
Trim
().
ToLower
();
}
return
null
;
}
}
internal
string
HttpVersion
{
get
;
set
;
}
internal
bool
ResponseKeepAlive
{
get
{
var
header
=
this
.
ResponseHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
().
Equals
(
"connection"
));
if
(
header
!=
null
&&
header
.
Value
.
ToLower
().
Contains
(
"close"
))
{
return
false
;
}
return
true
;
}
}
public
string
ContentType
{
get
{
var
header
=
this
.
ResponseHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
().
Equals
(
"content-type"
));
if
(
header
!=
null
)
{
if
(
header
.
Value
.
Contains
(
";"
))
{
return
header
.
Value
.
Split
(
';'
)[
0
].
Trim
();
}
else
return
header
.
Value
.
ToLower
().
Trim
();
}
return
null
;
}
}
internal
int
ContentLength
{
get
{
var
header
=
this
.
ResponseHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
().
Equals
(
"content-length"
));
if
(
header
!=
null
)
{
return
int
.
Parse
(
header
.
Value
.
Trim
());
}
return
-
1
;
}
}
internal
bool
IsChunked
{
get
{
var
header
=
this
.
ResponseHeaders
.
FirstOrDefault
(
x
=>
x
.
Name
.
ToLower
().
Equals
(
"transfer-encoding"
));
if
(
header
!=
null
&&
header
.
Value
.
ToLower
().
Contains
(
"chunked"
))
{
return
true
;
}
return
false
;
}
}
public
List
<
HttpHeader
>
ResponseHeaders
{
get
;
set
;
}
internal
Stream
ResponseStream
{
get
;
set
;
}
internal
byte
[]
ResponseBody
{
get
;
set
;
}
internal
string
ResponseBodyString
{
get
;
set
;
}
internal
bool
ResponseBodyRead
{
get
;
set
;
}
internal
bool
ResponseLocked
{
get
;
set
;
}
internal
string
CharacterSet
{
get
;
set
;
}
internal
string
ContentEncoding
{
get
;
set
;
}
internal
string
HttpVersion
{
get
;
set
;
}
internal
bool
ResponseKeepAlive
{
get
;
set
;
}
public
string
ContentType
{
get
;
internal
set
;
}
internal
int
ContentLength
{
get
;
set
;
}
internal
bool
IsChunked
{
get
;
set
;
}
public
List
<
HttpHeader
>
ResponseHeaders
{
get
;
internal
set
;
}
public
Response
()
{
this
.
ResponseHeaders
=
new
List
<
HttpHeader
>();
this
.
ResponseKeepAlive
=
true
;
}
}
...
...
Titanium.Web.Proxy/ResponseHandler.cs
View file @
7fd744bb
...
...
@@ -25,13 +25,11 @@ namespace Titanium.Web.Proxy
try
{
args
.
ProxySession
.
Response
.
ResponseHeaders
=
ReadResponseHeaders
(
args
.
ProxySession
);
args
.
ProxySession
.
Response
.
ResponseStream
=
args
.
ProxySession
.
ProxyClient
.
ServerStreamReader
.
BaseStream
;
if
(
BeforeResponse
!=
null
)
{
args
.
ProxySession
.
Response
.
Encoding
=
args
.
ProxySession
.
GetResponseEncoding
();
{
BeforeResponse
(
null
,
args
);
}
...
...
@@ -85,47 +83,7 @@ namespace Titanium.Web.Proxy
}
}
private
static
List
<
HttpHeader
>
ReadResponseHeaders
(
HttpWebSession
response
)
{
for
(
var
i
=
0
;
i
<
response
.
Response
.
ResponseHeaders
.
Count
;
i
++)
{
switch
(
response
.
Response
.
ResponseHeaders
[
i
].
Name
.
ToLower
())
{
case
"content-length"
:
response
.
Response
.
ContentLength
=
int
.
Parse
(
response
.
Response
.
ResponseHeaders
[
i
].
Value
.
Trim
());
break
;
case
"content-encoding"
:
response
.
Response
.
ContentEncoding
=
response
.
Response
.
ResponseHeaders
[
i
].
Value
.
Trim
().
ToLower
();
break
;
case
"content-type"
:
if
(
response
.
Response
.
ResponseHeaders
[
i
].
Value
.
Contains
(
";"
))
{
response
.
Response
.
ContentType
=
response
.
Response
.
ResponseHeaders
[
i
].
Value
.
Split
(
';'
)[
0
].
Trim
();
response
.
Response
.
CharacterSet
=
response
.
Response
.
ResponseHeaders
[
i
].
Value
.
Split
(
';'
)[
1
].
Substring
(
9
).
Trim
();
}
else
response
.
Response
.
ContentType
=
response
.
Response
.
ResponseHeaders
[
i
].
Value
.
ToLower
().
Trim
();
break
;
case
"transfer-encoding"
:
if
(
response
.
Response
.
ResponseHeaders
[
i
].
Value
.
ToLower
().
Contains
(
"chunked"
))
response
.
Response
.
IsChunked
=
true
;
break
;
case
"connection"
:
if
(
response
.
Response
.
ResponseHeaders
[
i
].
Value
.
ToLower
().
Contains
(
"close"
))
response
.
Response
.
ResponseKeepAlive
=
false
;
break
;
default
:
break
;
}
}
return
response
.
Response
.
ResponseHeaders
;
}
private
static
void
WriteResponseStatus
(
string
version
,
string
code
,
string
description
,
...
...
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