Refactored WSF_HANDLER_CONTEXT

- removed path_parameter
  - added `item' to include WSF_REQUEST.item
  - marked obsolete `parameter'

The goal is to remove confusion, remove URI_TEMPLATE specific `path_parameter'
and provide a way to use ctx.item (..) to also include meta variable, query, form, ... items
This commit is contained in:
Jocelyn Fiat
2012-03-19 10:21:29 +01:00
parent b05ff01262
commit ef5ba19c46
7 changed files with 142 additions and 117 deletions

View File

@@ -34,6 +34,13 @@ feature -- Access
do do
end end
resource_value (ctx: C): detachable READABLE_STRING_32
do
if attached {WSF_STRING} ctx.item ("resource") as s then
Result := s.string
end
end
feature -- Execution feature -- Execution
execute_application (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE) execute_application (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
@@ -50,8 +57,7 @@ feature -- Execution
create s.make_empty create s.make_empty
if if
attached {WSF_STRING} ctx.path_parameter ("resource") as l_resource_value and then attached resource_value (ctx) as l_resource
attached l_resource_value.string as l_resource
then then
from from
hdl_cursor := router.new_cursor hdl_cursor := router.new_cursor
@@ -309,7 +315,7 @@ feature -- Access
end end
note note
copyright: "Copyright (c) 1984-2011, Eiffel Software and others" copyright: "Copyright (c) 1984-2012, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software

View File

@@ -16,7 +16,7 @@ create
make make
note note
copyright: "Copyright (c) 1984-2011, Eiffel Software and others" copyright: "Copyright (c) 1984-2012, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[ source: "[
Eiffel Software Eiffel Software

View File

@@ -21,15 +21,13 @@ feature {NONE} -- Initialization
path := p path := p
end end
feature -- Query feature -- Item
path_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE item (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Variable value for parameter or variable `a_name'
-- See `{WSF_REQUEST}.item(s)'
do do
end Result := request.item (a_name)
query_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
do
Result := request.query_parameter (a_name)
end end
note note

View File

@@ -9,6 +9,9 @@ class
inherit inherit
WSF_HANDLER_CONTEXT WSF_HANDLER_CONTEXT
redefine
item
end
create create
make make
@@ -29,7 +32,20 @@ feature -- Access
uri_template_match: URI_TEMPLATE_MATCH_RESULT uri_template_match: URI_TEMPLATE_MATCH_RESULT
feature -- Query
feature -- Item
item (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Variable value for parameter or variable `a_name'
-- See `{WSF_REQUEST}.item(s)'
do
Result := path_parameter (a_name)
if Result = Void then
Result := request.item (a_name)
end
end
feature -- Path parameter
path_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE path_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
do do
@@ -38,13 +54,30 @@ feature -- Query
end end
end end
query_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE is_integer_path_parameter (a_name: READABLE_STRING_8): BOOLEAN
-- Is path parameter related to `a_name' an integer value?
do do
if attached uri_template_match.url_decoded_query_variable (a_name) as s then Result := attached string_path_parameter (a_name) as s and then s.is_integer
create {WSF_STRING} Result.make (a_name, s) end
else
Result := request.query_parameter (a_name) integer_path_parameter (a_name: READABLE_STRING_8): INTEGER
end -- Integer value for path parameter `a_name' if relevant.
require
is_integer_path_parameter: is_integer_path_parameter (a_name)
do
Result := integer_from (path_parameter (a_name))
end
string_path_parameter (a_name: READABLE_STRING_8): detachable READABLE_STRING_32
-- String value for path parameter `a_name' if relevant.
do
Result := string_from (path_parameter (a_name))
end
string_array_path_parameter (a_name: READABLE_STRING_8): detachable ARRAY [READABLE_STRING_32]
-- Array of string values for path parameter `a_name' if relevant.
do
Result := string_array_for (a_name, agent string_path_parameter)
end end
note note

View File

@@ -62,23 +62,31 @@ feature -- Element change
feature -- Execution feature -- Execution
requested_path (ctx: C): detachable READABLE_STRING_8
-- Path associated with the request
-- i.e: path of the file system resource if any
do
if attached {WSF_STRING} ctx.item ("path") as v_path then
Result := v_path.string.as_string_8
end
end
execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE) execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request handler -- Execute request handler
local local
h: HTTP_HEADER h: HTTP_HEADER
s: STRING s: STRING
uri: STRING
do do
if attached {WSF_STRING} ctx.path_parameter ("path") as l_path then if attached requested_path (ctx) as uri then
uri := l_path.string
process_uri (uri, ctx, req, res) process_uri (uri, ctx, req, res)
else else
create h.make
h.put_content_type_text_html
s := "Hello " + ctx.path + "%N" s := "Hello " + ctx.path + "%N"
s.append ("root=" + document_root) s.append ("root=" + document_root)
create h.make
h.put_content_type_text_html
h.put_content_length (s.count) h.put_content_length (s.count)
res.set_status_code ({HTTP_STATUS_CODE}.ok) res.set_status_code ({HTTP_STATUS_CODE}.ok)
res.put_header_text (h.string) res.put_header_text (h.string)
res.put_string (s) res.put_string (s)

View File

@@ -27,7 +27,12 @@ feature -- Status report
feature -- Execution feature -- Execution
execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE) execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute request handler -- Execute request handler
-- `ctx': contains advanced data related to request_uri
-- in the case of URI_TEMPLATE, it add support for "path_parameter"
-- `req': request data
-- `res': reponse stream
--| note `ctx' can also provide data coming from `req'
require require
is_valid_context: is_valid_context (req) is_valid_context: is_valid_context (req)
deferred deferred

View File

@@ -1,6 +1,6 @@
note note
description: "[ description: "[
]" ]"
date: "$Date$" date: "$Date$"
revision: "$Revision$" revision: "$Revision$"
@@ -106,27 +106,76 @@ feature -- Query
end end
end end
feature -- Query feature -- Item
path_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE item (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Parameter value for path variable `a_name' -- Variable value for parameter or variable `a_name'
deferred -- See `{WSF_REQUEST}.item(s)'
end
query_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Parameter value for query variable `a_name'
--| i.e after the ? character
deferred deferred
end end
parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Any parameter value for variable `a_name' -- Variable value for parameter or variable `a_name'
-- URI template parameter and query parameters -- See `{WSF_REQUEST}.item(s)'
obsolete "[2012-Mars-19] Use `item (a_name)' ."
do do
Result := query_parameter (a_name) Result := item (a_name)
if Result = Void then end
Result := path_parameter (a_name)
end feature -- Parameter
string_item (a_name: READABLE_STRING_8): detachable READABLE_STRING_32
-- String value for any variable of parameter `a_name' if relevant.
do
Result := string_from (item (a_name))
end
string_parameter (a_name: READABLE_STRING_8): detachable READABLE_STRING_32
-- String value for any variable of parameter `a_name' if relevant.
obsolete "[2012-Mars-19] Use `string_item (a_name)' ."
do
Result := string_item (a_name)
end
string_array_item (a_name: READABLE_STRING_8): detachable ARRAY [READABLE_STRING_32]
-- Array of string values for query parameter `a_name' if relevant.
do
Result := string_array_for (a_name, agent string_item)
end
feature -- Query parameter
query_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Parameter value for query variable `a_name'
--| i.e after the ? character
do
Result := request.query_parameter (a_name)
end
string_query_parameter (a_name: READABLE_STRING_8): detachable READABLE_STRING_32
-- String value for query parameter `a_name' if relevant.
do
Result := string_from (query_parameter (a_name))
end
string_array_query_parameter (a_name: READABLE_STRING_8): detachable ARRAY [READABLE_STRING_32]
-- Array of string values for query parameter `a_name' if relevant.
do
Result := string_array_for (a_name, agent string_query_parameter)
end
is_integer_query_parameter (a_name: READABLE_STRING_8): BOOLEAN
-- Is query parameter related to `a_name' an integer value?
do
Result := attached string_query_parameter (a_name) as s and then s.is_integer
end
integer_query_parameter (a_name: READABLE_STRING_8): INTEGER
-- Integer value for query parameter `a_name' if relevant.
require
is_integer_query_parameter: is_integer_query_parameter (a_name)
do
Result := integer_from (query_parameter (a_name))
end end
feature -- Convertion feature -- Convertion
@@ -149,75 +198,9 @@ feature -- Convertion
end end
end end
feature -- Path parameter feature {NONE} -- Implementation
is_integer_path_parameter (a_name: READABLE_STRING_8): BOOLEAN string_array_for (a_name: READABLE_STRING_8; a_item_fct: FUNCTION [ANY, TUPLE [READABLE_STRING_8], detachable READABLE_STRING_32]): detachable ARRAY [READABLE_STRING_32]
-- Is path parameter related to `a_name' an integer value?
do
Result := attached string_path_parameter (a_name) as s and then s.is_integer
end
integer_path_parameter (a_name: READABLE_STRING_8): INTEGER
-- Integer value for path parameter `a_name' if relevant.
require
is_integer_path_parameter: is_integer_path_parameter (a_name)
do
Result := integer_from (path_parameter (a_name))
end
string_path_parameter (a_name: READABLE_STRING_8): detachable READABLE_STRING_32
-- String value for path parameter `a_name' if relevant.
do
Result := string_from (path_parameter (a_name))
end
string_array_path_parameter (a_name: READABLE_STRING_8): detachable ARRAY [READABLE_STRING_32]
-- Array of string values for path parameter `a_name' if relevant.
local
i: INTEGER
n: INTEGER
do
from
i := 1
n := 1
create Result.make_filled ("", 1, 5)
until
i = 0
loop
if attached string_path_parameter (a_name + "[" + i.out + "]") as v then
Result.force (v, n)
n := n + 1
i := i + 1
else
i := 0 -- Exit
end
end
Result.keep_head (n - 1)
end
feature -- String parameter
is_integer_query_parameter (a_name: READABLE_STRING_8): BOOLEAN
-- Is query parameter related to `a_name' an integer value?
do
Result := attached string_query_parameter (a_name) as s and then s.is_integer
end
integer_query_parameter (a_name: READABLE_STRING_8): INTEGER
-- Integer value for query parameter `a_name' if relevant.
require
is_integer_query_parameter: is_integer_query_parameter (a_name)
do
Result := integer_from (query_parameter (a_name))
end
string_query_parameter (a_name: READABLE_STRING_8): detachable READABLE_STRING_32
-- String value for query parameter `a_name' if relevant.
do
Result := string_from (query_parameter (a_name))
end
string_array_query_parameter (a_name: READABLE_STRING_8): detachable ARRAY [READABLE_STRING_32]
-- Array of string values for query parameter `a_name' if relevant. -- Array of string values for query parameter `a_name' if relevant.
local local
i: INTEGER i: INTEGER
@@ -230,7 +213,7 @@ feature -- String parameter
until until
i = 0 i = 0
loop loop
if attached string_query_parameter (a_name + "[" + i.out + "]") as v then if attached a_item_fct.item ([a_name + "[" + i.out + "]"]) as v then
Result.force (v, n) Result.force (v, n)
n := n + 1 n := n + 1
i := i + 1 i := i + 1
@@ -241,14 +224,6 @@ feature -- String parameter
Result.keep_head (n - 1) Result.keep_head (n - 1)
end end
feature -- Parameter
string_parameter (a_name: READABLE_STRING_8): detachable READABLE_STRING_32
-- String value for path or query parameter `a_name' if relevant.
do
Result := string_from (parameter (a_name))
end
;note ;note
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Eiffel Software and others" copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"