Added html encoding facility to WSF_STRING

Added WSF_STRING.is_empty
Improved HTML_ENCODER to be able to decode a STRING_8 or STRING_32 using general_decoded_string (s)
Improved tutorial example
Added precompilation for WSF library
Cosmetic (removed unused locals)
This commit is contained in:
Jocelyn Fiat
2012-05-30 09:36:55 +02:00
parent 8dd0cb29fa
commit 0d5011b03e
11 changed files with 280 additions and 47 deletions

View File

@@ -10,8 +10,11 @@
<option warning="true" is_attached_by_default="true" void_safety="all" syntax="transitional">
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
</option>
<setting name="concurrency" value="thread"/>
<precompile name="precomp_wsf-mt" location="..\..\..\..\precomp\wsf-mt-safe.ecf"/>
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
<library name="default_nino" location="..\..\..\..\library\server\wsf\default\nino-safe.ecf"/>
<library name="encoder" location="..\..\..\..\library\text\encoder\encoder-safe.ecf" readonly="false"/>
<library name="wsf" location="..\..\..\..\library\server\wsf\wsf-safe.ecf" readonly="false"/>
<library name="http" location="..\..\..\..\library\protocol\http\http-safe.ecf" readonly="false"/>
<cluster name="src" location=".\src\" recursive="true"/>

View File

@@ -1,4 +1,4 @@
note
note
description: "[
This class implements the `Hello World' service.
@@ -29,7 +29,12 @@ feature {NONE} -- Initialization
setup_router
do
router.map_agent ("/hello", agent execute_hello)
router.map_agent_response_with_request_methods ("/{user}/bye", agent response_bye, <<"GET">>)
router.map_with_request_methods ("/users/{user}/message/{mesgid}", create {USER_MESSAGE_HANDLER}, <<"GET", "POST">>)
router.map_with_request_methods ("/users/{user}/message/", create {USER_MESSAGE_HANDLER}, <<"GET", "POST">>)
router.map_agent_response_with_request_methods ("/users/{user}/{?op}", agent response_user, <<"GET">>)
end
feature -- Execution
@@ -44,6 +49,7 @@ feature -- Execution
-- Computed response message.
local
mesg: WSF_HTML_PAGE_RESPONSE
s: STRING_8
do
--| It is now returning a WSF_HTML_PAGE_RESPONSE
--| Since it is easier for building html page
@@ -51,21 +57,26 @@ feature -- Execution
mesg.set_title ("EWF tutorial / Hello World!")
--| Check if the request contains a parameter named "user"
--| this could be a query, or a form parameter
if attached req.string_item ("user") as u then
if attached {WSF_STRING} req.item ("user") as u then
--| If yes, say hello world #name
mesg.set_body ("Hello " + u + "!<br/>Click <a href=%"/" + u + "/bye%">here</a> to quit.")
s := "<p>Hello " + u.html_encoded_string + "!</p>"
s.append ("Display a <a href=%"/users/" + u.url_encoded_string + "/message/%">message</a></p>")
s.append ("<p>Click <a href=%"/users/" + u.url_encoded_string + "/?op=quit%">here</a> to quit.</p>")
mesg.set_body (s)
--| We should html encode this name
--| but to keep the example simple, we don't do that for now.
else
--| Otherwise, ask for name
mesg.set_body ("[
s := (create {HTML_ENCODER}).encoded_string ({STRING_32} "Hello / ahoj / नमस्ते / Ciào / مرحبا / Hola / 你好 / Hallo / Selam / Bonjour ")
s.append ("[
<form action="/hello" method="POST">
<p>Hello, what is your name?</p>
What is your name?</p>
<input type="text" name="user"/>
<input type="submit" value="Validate"/>
</form>
]"
)
mesg.set_body (s)
end
--| note:
@@ -79,17 +90,39 @@ feature -- Execution
res.send (mesg)
end
response_bye (ctx: WSF_URI_TEMPLATE_HANDLER_CONTEXT; req: WSF_REQUEST): WSF_RESPONSE_MESSAGE
response_user (ctx: WSF_URI_TEMPLATE_HANDLER_CONTEXT; req: WSF_REQUEST): WSF_RESPONSE_MESSAGE
-- Computed response message.
local
html: WSF_HTML_PAGE_RESPONSE
redir: WSF_HTML_DELAYED_REDIRECTION_RESPONSE
s: STRING_8
do
if attached ctx.string_path_parameter ("user") as u then
create redir.make (req.script_url ("/hello"), 5)
redir.set_title ("Bye " + u)
redir.set_body ("Bye " + u + ",<br/> see you soon.<p>You will be redirected to " + redir.url_location + " in " + redir.delay.out + " second(s) ...</p>")
Result := redir
if attached {WSF_STRING} ctx.path_parameter ("user") as u then
if
attached {WSF_STRING} req.query_parameter ("op") as l_op
then
if l_op.is_case_insensitive_equal ("quit") then
create redir.make (req.script_url ("/hello"), 5)
redir.set_title ("Bye " + u.url_encoded_string)
redir.set_body ("Bye " + u.url_encoded_string + ",<br/> see you soon.<p>You will be redirected to " +
redir.url_location + " in " + redir.delay.out + " second(s) ...</p>"
)
Result := redir
else
create html.make
html.set_title ("Bad request")
html.set_body ("Bad request: unknown operation '" + l_op.url_encoded_string + "'.")
Result := html
end
else
s := "<p>User <em>'" + u.url_encoded_string + "'</em>!</p>"
s.append ("Display a <a href=%"/users/" + u.url_encoded_string + "/message/%">message</a></p>")
s.append ("<p>Click <a href=%"/users/" + u.url_encoded_string + "/?op=quit%">here</a> to quit.</p>")
create html.make
html.set_title ("User '" + u.url_encoded_string + "'")
html.set_body (s)
Result := html
end
else
create html.make
html.set_title ("Bad request")

View File

@@ -0,0 +1,73 @@
note
description: "[
Handler to process /user/{user}/message/ requests
]"
date: "$Date$"
revision: "$Revision$"
class
USER_MESSAGE_HANDLER
inherit
WSF_RESPONSE_HANDLER [WSF_URI_TEMPLATE_HANDLER_CONTEXT]
feature -- Access
response (ctx: WSF_URI_TEMPLATE_HANDLER_CONTEXT; req: WSF_REQUEST): WSF_RESPONSE_MESSAGE
do
if attached {WSF_STRING} ctx.path_parameter ("user") as u then
if req.is_request_method ("GET") then
Result := user_message_get (u, ctx, req)
elseif req.is_request_method ("POST") then
Result := user_message_response_post (u, ctx, req)
else
Result := unsupported_method_response (req)
end
else
Result := missing_argument_response ("Missing parameter 'user'.", req)
end
end
missing_argument_response (m: READABLE_STRING_8; req: WSF_REQUEST): WSF_PAGE_RESPONSE
do
create Result.make
Result.set_status_code ({HTTP_STATUS_CODE}.bad_request)
Result.put_string (req.request_uri + ": " + m)
end
unsupported_method_response (req: WSF_REQUEST): WSF_PAGE_RESPONSE
do
create Result.make
Result.set_status_code ({HTTP_STATUS_CODE}.bad_request)
Result.put_string (req.request_uri + " only support: GET and POST; " + req.request_method + " is not supported.")
end
user_message_get (u: WSF_STRING; ctx: WSF_URI_TEMPLATE_HANDLER_CONTEXT; req: WSF_REQUEST): WSF_HTML_PAGE_RESPONSE
local
s: STRING_8
do
create Result.make
s := "<p>No message from user '" + u.html_encoded_string + "'.</p>"
s.append ("<form action=%""+ req.request_uri +"%" method=%"POST%">")
s.append ("<textarea name=%"message%" rows=%"10%" cols=%"70%" ></textarea>")
s.append ("<input type=%"submit%" value=%"Ok%" />")
s.append ("</form>")
Result.set_body (s)
end
user_message_response_post (u: WSF_STRING; ctx: WSF_URI_TEMPLATE_HANDLER_CONTEXT; req: WSF_REQUEST): WSF_HTML_PAGE_RESPONSE
local
s: STRING_8
do
create Result.make
s := "<p>Message from user '<a href=%"/users/" + u.url_encoded_string + "/%">" + u.html_encoded_string + "</a>'.</p>"
if attached {WSF_STRING} req.form_parameter ("message") as m and then not m.is_empty then
s.append ("<textarea>"+ m.string +"</textarea>")
else
s.append ("<strong>No or empty message!</strong>")
end
Result.set_body (s)
end
end