Skip to content

Commit

Permalink
Added ZnProxyServerDelegate[Test]
Browse files Browse the repository at this point in the history
This is an example of a simple proxy server
Thanks Nobert Hartl for the idea
  • Loading branch information
svenvc committed Jul 28, 2024
1 parent 51ed0ec commit 87efcbe
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
I am a ZnDelegate that acts as a proxy.

I handle requests by passing them to a proxyBlock which can change the request before I execute it.
The idea is that the changed request is to a different server, the one that we proxy.

Usage example

| proxiedServer proxyServer proxyDelegate client |
proxiedServer := ZnServer startOn: 8080.
proxyServer := ZnServer on: 9090.
proxyDelegate := ZnProxyServerDelegate new
server: proxyServer;
proxyBlock: [ :request |
request url: (request url port: 8080) ].
proxyServer
delegate: proxyDelegate;
start.
client := ZnClient new.
client get: proxyServer url / #echo.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
private
augmentUrl: aUrl
"The URL in a server request has no scheme, host or port set.
Augment aUrl by taking those elements from #serverUrl"

self server ifNil: [ ^ aUrl ].
^ aUrl inContextOf: self server url
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
private
copyAndPrepareRequest: aRequest
"We make a copy of the request and augment its URL"

| copiedRequest |
copiedRequest := aRequest copy.
copiedRequest url: (self augmentUrl: copiedRequest url).
^ copiedRequest
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public
handleRequest: incomingRequest
"Ask our proxy block to transform a incoming ZnRequest copy into an outgoing ZnRequest"

| outgoingRequest |
outgoingRequest := self proxyBlock
cull: (self copyAndPrepareRequest: incomingRequest)
cull: self server.
^ ZnClient new
request: outgoingRequest;
beOneShot;
execute;
response
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
accessing
proxyBlock: aBlock
"Set the block that transforms a copy of the incoming ZnRequest to the outgoing ZnRequest.
Two parameters are passed to block: the request and an optional server reference.
See #copyAndPrepareRequest: for how the request got changed."

proxyBlock := aBlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
proxyBlock
^ proxyBlock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
accessing
server: aZnServer
"Set areference to the server we're in (optional)"

server := aZnServer
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
accessing
server
^ server
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"commentStamp" : "<historical>",
"super" : "Object",
"category" : "Zinc-HTTP-Examples",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"proxyBlock",
"server"
],
"name" : "ZnProxyServerDelegate",
"type" : "normal"
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
testing
testEcho
| proxiedServer proxyServer proxyDelegate client |
proxiedServer := ZnServer startOn: 8080.
proxyServer := ZnServer on: 9090.
proxyDelegate := ZnProxyServerDelegate new
server: proxyServer;
proxyBlock: [ :request |
request url: (request url port: 8080) ].
proxyServer
delegate: proxyDelegate;
start.
client := ZnClient new.
client get: proxyServer url / #echo.
self assert: client isSuccess.
self assert: (client contents includesSubstring: 'echoing').
self assert: (client contents includesSubstring: 'running 8080').
proxiedServer stop.
proxyServer stop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"commentStamp" : "",
"super" : "TestCase",
"category" : "Zinc-HTTP-Examples",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [ ],
"name" : "ZnProxyServerDelegateTest",
"type" : "normal"
}

0 comments on commit 87efcbe

Please sign in to comment.