Added DEFAULT_SERVICE_LAUNCHER.make_and_launch_with_options

Added WSF_RESPONSE.redirect_now_with_content (...)
Updated hello_routed_world .. mainly example use to test/develop... not really a nice example
This commit is contained in:
Jocelyn Fiat
2011-12-14 14:44:06 +01:00
parent b60c41ee1b
commit c7366615cf
5 changed files with 113 additions and 18 deletions

View File

@@ -22,7 +22,7 @@ feature {NONE} -- Initialization
s: DEFAULT_SERVICE_LAUNCHER
do
initialize_router
create s.make_and_launch (agent execute)
create s.make_and_launch_with_options (agent execute, <<["port", 8099]>>)
end
create_router
@@ -36,6 +36,7 @@ feature {NONE} -- Initialization
hello: REQUEST_URI_TEMPLATE_ROUTING_HANDLER
www: REQUEST_FILE_SYSTEM_HANDLER [REQUEST_URI_TEMPLATE_HANDLER_CONTEXT]
do
router.map_agent ("/refresh", agent execute_refresh)
router.map_agent ("/home", agent execute_home)
create www.make (document_root)
www.set_directory_index (<<"index.html">>)
@@ -79,7 +80,16 @@ feature {NONE} -- Initialization
feature -- Execution
execute_default (req: WSF_REQUEST; res: WSF_RESPONSE)
local
l_url: STRING
do
l_url := req.absolute_script_url ("/home")
res.redirect_now_with_content (l_url, "You are now being redirected to " + l_url, {HTTP_MIME_TYPES}.text_html)
end
execute_refresh (ctx: REQUEST_URI_TEMPLATE_HANDLER_CONTEXT; req: WSF_REQUEST; res: WSF_RESPONSE)
local
h: HTTP_HEADER
l_url: STRING
@@ -87,20 +97,20 @@ feature -- Execution
n: INTEGER
i: INTEGER
s: STRING_8
df: WSF_FILE_RESPONSE
do
create df.make_html ("index.html")
df.set_no_cache
l_url := req.absolute_script_url ("/home")
l_url := req.script_url ("/home")
n := 3
create h.make
h.put_refresh (l_url, 5)
h.put_location (l_url)
h.put_content_type_text_plain
h.put_transfer_encoding_chunked
-- h.put_content_length (0)
res.set_status_code ({HTTP_STATUS_CODE}.moved_permanently)
-- res.set_status_code ({HTTP_STATUS_CODE}.moved_permanently)
res.set_status_code ({HTTP_STATUS_CODE}.ok)
res.write_header_text (h.string)
from
@@ -143,6 +153,7 @@ feature -- Execution
l_body.append ("<html><body>Hello World ?!%N")
l_body.append ("<h3>Please try the following links</h3><ul>%N")
l_body.append ("<li><a href=%""+ req.script_url ("/") + "%">default</a></li>%N")
l_body.append ("<li><a href=%""+ req.script_url ("/refresh") + "%">redirect using refresh and chunked encoding</a></li>%N")
l_body.append ("<li><a href=%""+ req.script_url ("/hello") + "%">/hello</a></li>%N")
l_body.append ("<li><a href=%""+ req.script_url ("/hello.html/Joce") + "%">/hello.html/Joce</a></li>%N")
l_body.append ("<li><a href=%""+ req.script_url ("/hello.json/Joce") + "%">/hello.json/Joce</a></li>%N")

View File

@@ -37,6 +37,11 @@ feature {NONE} -- Initialization
cgi.launch
end
make_and_launch_with_options (a_action: like action; a_options: ARRAY [detachable TUPLE [name: READABLE_STRING_GENERAL; value: detachable ANY]])
do
make_and_launch (a_action)
end
feature -- Execution
action: PROCEDURE [ANY, TUPLE [WSF_REQUEST, WSF_RESPONSE]]

View File

@@ -37,6 +37,11 @@ feature {NONE} -- Initialization
conn.launch
end
make_and_launch_with_options (a_action: like action; a_options: ARRAY [detachable TUPLE [name: READABLE_STRING_GENERAL; value: detachable ANY]])
do
make_and_launch (a_action)
end
feature -- Execution
action: PROCEDURE [ANY, TUPLE [WSF_REQUEST, WSF_RESPONSE]]

View File

@@ -13,6 +13,15 @@ note
do
-- ...
end
You can also provide specific options that might be relevant
only for specific connectors such as
create s.make_and_launch_and_options (agent execute, <<["port", 8099]>>)
The Nino default connector support:
port: numeric such as 8099 (or equivalent string as "8099")
base: base_url (very specific to standalone server)
verbose: to display verbose output, useful for Nino
]"
date: "$Date$"
revision: "$Revision$"
@@ -24,28 +33,73 @@ inherit
WSF_SERVICE
create
make_and_launch
make_and_launch,
make_and_launch_with_options
feature {NONE} -- Initialization
make_and_launch (a_action: like action)
local
app: NINO_SERVICE
port_number: INTEGER
base_url: STRING
do
action := a_action
port_number := 8087 --| Default, but quite often, this port is already used ...
launch (80, "", False)
end
make_and_launch_with_options (a_action: like action; a_options: ARRAY [detachable TUPLE [name: READABLE_STRING_GENERAL; value: detachable ANY]])
local
port_number: INTEGER
base_url: STRING
verbose: BOOLEAN
l_name: READABLE_STRING_GENERAL
do
action := a_action
port_number := 80 --| Default, but quite often, this port is already used ...
base_url := ""
debug ("nino")
print ("Example: start a Nino web server on port " + port_number.out +
", %Nand reply Hello World for any request such as http://localhost:" + port_number.out + "/" + base_url + "%N")
across
a_options as opt
loop
if attached opt.item as l_opt_item then
l_name := l_opt_item.name
if l_name.same_string ("port") then
if attached {INTEGER} l_opt_item.value as l_port then
port_number := l_port
elseif
attached {READABLE_STRING_GENERAL} l_opt_item.value as l_port_str and then
l_port_str.is_integer
then
port_number := l_port_str.as_string_8.to_integer
end
elseif l_name.same_string ("base") then
if attached {READABLE_STRING_GENERAL} l_opt_item.value as l_base_str then
base_url := l_base_str.as_string_8
end
elseif l_name.same_string ("verbose") then
if attached {BOOLEAN} l_opt_item.value as l_verbose then
verbose := l_verbose
elseif attached {READABLE_STRING_GENERAL} l_opt_item.value as l_verbose_str then
verbose := l_verbose_str.as_lower.same_string ("true")
end
end
end
end
create app.make_custom (agent wgi_execute, base_url)
launch (port_number, base_url, verbose)
end
launch (a_port: INTEGER; a_base: STRING; a_verbose: BOOLEAN)
-- Launch service with `a_port', `a_base' and `a_verbose' value
require
a_port_valid: a_port > 0
local
app: NINO_SERVICE
do
create app.make_custom (agent wgi_execute, a_base)
app.set_is_verbose (a_verbose)
debug ("nino")
app.set_is_verbose (True)
if a_verbose then
print ("Example: start a Nino web server on port " + a_port.out +
", %Nand reply Hello World for any request such as http://localhost:" + a_port.out + "/" + a_base + "%N")
end
end
app.listen (port_number)
app.listen (a_port)
end
feature -- Execution

View File

@@ -207,6 +207,7 @@ feature -- Redirect
h: HTTP_HEADER
do
if header_committed then
-- This might be a trouble about content-length
write_string ("Headers already sent.%NCannot redirect, for now please follow this <a %"href=%"" + a_url + "%">link</a> instead%N")
else
create h.make_with_count (1)
@@ -224,6 +225,25 @@ feature -- Redirect
redirect_now_with_custom_status_code (a_url, {HTTP_STATUS_CODE}.moved_permanently)
end
redirect_now_with_content (a_url: READABLE_STRING_8; a_content: READABLE_STRING_8; a_content_type: READABLE_STRING_8)
-- Redirect to the given url `a_url'
local
h: HTTP_HEADER
do
if header_committed then
-- This might be a trouble about content-length
write_string ("Headers already sent.%NCannot redirect, for now please follow this <a %"href=%"" + a_url + "%">link</a> instead%N")
else
create h.make_with_count (1)
h.put_location (a_url)
h.put_content_length (a_content.count)
h.put_content_type (a_content_type)
set_status_code ({HTTP_STATUS_CODE}.moved_permanently)
write_header_text (h.string)
write_string (a_content)
end
end
note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"