From 87efcbe2b1bfc2d83ada0ab2ee3db93038aa40bc Mon Sep 17 00:00:00 2001 From: Sven Van Caekenberghe Date: Sun, 28 Jul 2024 19:41:50 +0200 Subject: [PATCH] Added ZnProxyServerDelegate[Test] This is an example of a simple proxy server Thanks Nobert Hartl for the idea --- .../ZnProxyServerDelegate.class/README.md | 19 +++++++++++++++++++ .../instance/augmentUrl..st | 7 +++++++ .../instance/copyAndPrepareRequest..st | 8 ++++++++ .../instance/handleRequest..st | 13 +++++++++++++ .../instance/proxyBlock..st | 7 +++++++ .../instance/proxyBlock.st | 3 +++ .../instance/server..st | 5 +++++ .../instance/server.st | 3 +++ .../properties.json | 14 ++++++++++++++ .../ZnProxyServerDelegateTest.class/README.md | 0 .../instance/testEcho.st | 19 +++++++++++++++++++ .../properties.json | 11 +++++++++++ 12 files changed, 109 insertions(+) create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/README.md create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/augmentUrl..st create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/copyAndPrepareRequest..st create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/handleRequest..st create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock..st create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock.st create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server..st create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server.st create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/properties.json create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/README.md create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/instance/testEcho.st create mode 100644 repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/properties.json diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/README.md b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/README.md new file mode 100644 index 00000000..ba9a7b37 --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/README.md @@ -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. diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/augmentUrl..st b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/augmentUrl..st new file mode 100644 index 00000000..26001bb3 --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/augmentUrl..st @@ -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 \ No newline at end of file diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/copyAndPrepareRequest..st b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/copyAndPrepareRequest..st new file mode 100644 index 00000000..1e9ff441 --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/copyAndPrepareRequest..st @@ -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 \ No newline at end of file diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/handleRequest..st b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/handleRequest..st new file mode 100644 index 00000000..ba4df3df --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/handleRequest..st @@ -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 \ No newline at end of file diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock..st b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock..st new file mode 100644 index 00000000..9d22ce89 --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock..st @@ -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 \ No newline at end of file diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock.st b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock.st new file mode 100644 index 00000000..5403ee42 --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock.st @@ -0,0 +1,3 @@ +accessing +proxyBlock + ^ proxyBlock \ No newline at end of file diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server..st b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server..st new file mode 100644 index 00000000..86c15f1b --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server..st @@ -0,0 +1,5 @@ +accessing +server: aZnServer + "Set areference to the server we're in (optional)" + + server := aZnServer \ No newline at end of file diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server.st b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server.st new file mode 100644 index 00000000..30bc977b --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server.st @@ -0,0 +1,3 @@ +accessing +server + ^ server \ No newline at end of file diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/properties.json b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/properties.json new file mode 100644 index 00000000..df9aafaf --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/properties.json @@ -0,0 +1,14 @@ +{ + "commentStamp" : "", + "super" : "Object", + "category" : "Zinc-HTTP-Examples", + "classinstvars" : [ ], + "pools" : [ ], + "classvars" : [ ], + "instvars" : [ + "proxyBlock", + "server" + ], + "name" : "ZnProxyServerDelegate", + "type" : "normal" +} \ No newline at end of file diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/README.md b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/README.md new file mode 100644 index 00000000..e69de29b diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/instance/testEcho.st b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/instance/testEcho.st new file mode 100644 index 00000000..cab65bf2 --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/instance/testEcho.st @@ -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 \ No newline at end of file diff --git a/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/properties.json b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/properties.json new file mode 100644 index 00000000..ae431a6c --- /dev/null +++ b/repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/properties.json @@ -0,0 +1,11 @@ +{ + "commentStamp" : "", + "super" : "TestCase", + "category" : "Zinc-HTTP-Examples", + "classinstvars" : [ ], + "pools" : [ ], + "classvars" : [ ], + "instvars" : [ ], + "name" : "ZnProxyServerDelegateTest", + "type" : "normal" +} \ No newline at end of file