I want to write a proxy server that modifies a url then passes it onto another server and returns the response from that server.
I can receive the URL and modify it … I’m just not sure about the proxying part. Help?
Here is some super simple proxying code that I used in one of my projects. There are a lot of different ways you can go with this to fit your situation, but this could be a starting point for you. You can put this in the HandleURL event of a WebApplication. Hope it helps - let me know.
dim jjSocket as new HTTPSocket
dim myPage, myURL as string
myURL = "http://TheOtherServer.com/"+Request.Path
jjSocket.SetRequestHeader("User-Agent",Request.GetRequestHeader("User-Agent"))
jjSocket.SetRequestHeader("Cookie",Request.GetRequestHeader("Cookie"))
if Request.Method = "GET" then
if Request.QueryString.Len > 0 then
myURL = myURL + "?"+Request.QueryString
// Do any modifications you want to perform to the URL here
// This is the URL your proxy server is calling.
end
myPage = jjSocket.Get(myURL, 30)
elseif Request.Method = "POST" then
dim myPostData as new Dictionary
Dim myRawPost() as string
try
myRawPost() = split(Request.Entity, "&")
for i as integer = 0 to myRawPost.Ubound
dim myKeyValue() as string
myKeyValue() = split(myRawPost(i),"=")
myPostData.value(myKeyValue(0)) = DecodeURLComponent(myKeyValue(1))
next
catch
system.DebugLog("Form Parse Error")
end
jjSocket.SetFormData(myPostData)
myPage = jjSocket.Post(myURL, 30)
end
if jjSocket.HTTPStatusCode <> 200 then
system.DebugLog("URL " + myURL)
system.DebugLog("Request Entity " + Request.Entity)
system.DebugLog("Socket Status " + jjSocket.HTTPStatusCode.ToText)
end
// You can rewrite links in the page to come from your proxy server like this (oversimplified - you will need to customize to your content)
myPage = ReplaceAll(myPage,"TheOtherServer.com","yourServer.com")
myPage = Replace(myPage,"Anything else you want to change in the content","Whatever you want to change it to")
for i as integer = 0 to jjSocket.PageHeaders.Count -1
dim myCookieParts() as string
if jjSocket.PageHeaders.Name(i) = "Cookie" or jjSocket.PageHeaders.Name(i) = "Set-Cookie" then
myCookieParts() = split(jjSocket.PageHeaders.Value(i),"=")
Request.SetCookie(myCookieParts(0), myCookieParts(1))
else
request.Header(jjSocket.PageHeaders.Name(i)) = jjSocket.PageHeaders.Value(i)
end
next
request.status = jjSocket.HTTPStatusCode
Request.Print myPage
Return True