-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is an example of a simple proxy server Thanks Nobert Hartl for the idea
- Loading branch information
Showing
12 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
7 changes: 7 additions & 0 deletions
7
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/augmentUrl..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
8 changes: 8 additions & 0 deletions
8
...Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/copyAndPrepareRequest..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
13 changes: 13 additions & 0 deletions
13
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/handleRequest..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 7 additions & 0 deletions
7
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/proxyBlock.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
accessing | ||
proxyBlock | ||
^ proxyBlock |
5 changes: 5 additions & 0 deletions
5
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/instance/server.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
accessing | ||
server | ||
^ server |
14 changes: 14 additions & 0 deletions
14
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegate.class/properties.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
19 changes: 19 additions & 0 deletions
19
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/instance/testEcho.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
11 changes: 11 additions & 0 deletions
11
repository/Zinc-HTTP-Examples.package/ZnProxyServerDelegateTest.class/properties.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |