Simplified proxy example, no more proxy.conf file.
Added forwarding case based on server name.
This commit is contained in:
@@ -2,5 +2,17 @@ Proxy example
|
||||
=============
|
||||
|
||||
Via the `wsf_proxy` library, it is possible to implement a simple reverse proxy service.
|
||||
Note: you need to edit the `proxy.conf` file to use proper remote service.
|
||||
(You can use for instance any of the EWF examples as remote server, or also existing public server).
|
||||
|
||||
This example forwards request based on server name, or location as described below:
|
||||
- if server name is `foo`, forwards to `http://localhost:8080/foo`
|
||||
- if server name is `bar`, forwards to `http://localhost:8080/bar`
|
||||
- if path starts with "/search/", forwards to `http://www.google.com/search?q=`
|
||||
|
||||
For instance:
|
||||
- http://foo/a/test -> http://localhost:8080/foo/a/test
|
||||
- http://localhost:9090/search/eiffel -> http://www.google.com/search?q=eiffel
|
||||
|
||||
Notes:
|
||||
- look at `{APPLICATION_EXECUTION}.execute` for the implementation.
|
||||
- the `send_proxy_response` implements a general forwarding, but for more advanced need, this could be customized per forwarding. For instance, a specific forwarding, may need the X-Forwarded headers, or to keep the `Host` from the client, ...
|
||||
|
||||
|
||||
@@ -18,56 +18,21 @@ feature -- Basic operations
|
||||
local
|
||||
l_forwarded: BOOLEAN
|
||||
do
|
||||
-- NOTE: please edit the proxy.conf file
|
||||
across
|
||||
proxy_map as ic
|
||||
until
|
||||
l_forwarded
|
||||
loop
|
||||
if request.path_info.starts_with_general (ic.key) then
|
||||
l_forwarded := True
|
||||
send_proxy_response (ic.key, ic.item, agent proxy_uri (ic.key, ?))
|
||||
end
|
||||
end
|
||||
if not l_forwarded then
|
||||
-- Hardocoded for the example
|
||||
if request.server_name.same_string ("foo") then
|
||||
send_proxy_response ("http://localhost:8080/foo", Void)
|
||||
l_forwarded := True
|
||||
elseif request.server_name.same_string ("bar") then
|
||||
send_proxy_response ("http://localhost:8080/bar", Void)
|
||||
l_forwarded := True
|
||||
elseif request.path_info.starts_with_general ("/search/") then
|
||||
send_proxy_response ("http://www.google.com/search?q=", agent uri_for_location_based_proxy ("/search/", ?))
|
||||
else
|
||||
response.send (create {WSF_PAGE_RESPONSE}.make_with_body ("EiffelWeb proxy: not forwarded!"))
|
||||
end
|
||||
end
|
||||
|
||||
proxy_map: HASH_TABLE [STRING, STRING]
|
||||
-- location => target
|
||||
local
|
||||
f: PLAIN_TEXT_FILE
|
||||
l_line: STRING
|
||||
p: INTEGER
|
||||
once ("thread")
|
||||
create Result.make (1)
|
||||
-- Load proxy.conf
|
||||
create f.make_with_name ("proxy.conf")
|
||||
if f.exists and then f.is_access_readable then
|
||||
f.open_read
|
||||
from
|
||||
until
|
||||
f.end_of_file or f.exhausted
|
||||
loop
|
||||
f.read_line
|
||||
l_line := f.last_string
|
||||
if l_line.starts_with ("#") then
|
||||
-- ignore
|
||||
else
|
||||
-- Format:
|
||||
-- path%Tserver
|
||||
p := l_line.index_of ('%T', 1)
|
||||
if p > 0 then
|
||||
Result.force (l_line.substring (p + 1, l_line.count), l_line.head (p - 1))
|
||||
end
|
||||
end
|
||||
end
|
||||
f.close
|
||||
end
|
||||
end
|
||||
|
||||
send_proxy_response (a_location, a_remote: READABLE_STRING_8; a_rewriter: detachable FUNCTION [WSF_REQUEST, STRING])
|
||||
send_proxy_response (a_remote: READABLE_STRING_8; a_rewriter: detachable FUNCTION [WSF_REQUEST, STRING])
|
||||
local
|
||||
h: WSF_SIMPLE_REVERSE_PROXY_HANDLER
|
||||
do
|
||||
@@ -85,13 +50,16 @@ feature -- Basic operations
|
||||
-- Uncomment following line to keep the original Host value.
|
||||
-- h.keep_proxy_host (True)
|
||||
|
||||
-- For debug information, uncomment next line
|
||||
-- response.put_error ("Forwarding to " + h.proxy_url (request))
|
||||
|
||||
h.execute (request, response)
|
||||
end
|
||||
|
||||
feature -- Helpers
|
||||
|
||||
proxy_uri (a_location: READABLE_STRING_8; a_request: WSF_REQUEST): STRING
|
||||
-- Request uri rewriten as url.
|
||||
uri_for_location_based_proxy (a_location: READABLE_STRING_8; a_request: WSF_REQUEST): STRING
|
||||
-- Request uri rewritten as url.
|
||||
do
|
||||
Result := a_request.request_uri
|
||||
-- If related proxy setting is
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
/google/ http://www.google.com/search?q=eiffel
|
||||
/ http://localhost:8080/testproxy
|
||||
Reference in New Issue
Block a user