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
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
execute_application (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
@@ -50,8 +57,7 @@ feature -- Execution
create s.make_empty
if
attached {WSF_STRING} ctx.path_parameter ("resource") as l_resource_value and then
attached l_resource_value.string as l_resource
attached resource_value (ctx) as l_resource
then
from
hdl_cursor := router.new_cursor
@@ -309,7 +315,7 @@ feature -- Access
end
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)"
source: "[
Eiffel Software

View File

@@ -16,7 +16,7 @@ create
make
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)"
source: "[
Eiffel Software

View File

@@ -21,15 +21,13 @@ feature {NONE} -- Initialization
path := p
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
end
query_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
do
Result := request.query_parameter (a_name)
Result := request.item (a_name)
end
note

View File

@@ -9,6 +9,9 @@ class
inherit
WSF_HANDLER_CONTEXT
redefine
item
end
create
make
@@ -29,7 +32,20 @@ feature -- Access
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
do
@@ -38,13 +54,30 @@ feature -- Query
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
if attached uri_template_match.url_decoded_query_variable (a_name) as s then
create {WSF_STRING} Result.make (a_name, s)
else
Result := request.query_parameter (a_name)
end
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.
do
Result := string_array_for (a_name, agent string_path_parameter)
end
note

View File

@@ -62,23 +62,31 @@ feature -- Element change
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 request handler
local
h: HTTP_HEADER
s: STRING
uri: STRING
do
if attached {WSF_STRING} ctx.path_parameter ("path") as l_path then
uri := l_path.string
if attached requested_path (ctx) as uri then
process_uri (uri, ctx, req, res)
else
create h.make
h.put_content_type_text_html
s := "Hello " + ctx.path + "%N"
s.append ("root=" + document_root)
create h.make
h.put_content_type_text_html
h.put_content_length (s.count)
res.set_status_code ({HTTP_STATUS_CODE}.ok)
res.put_header_text (h.string)
res.put_string (s)

View File

@@ -28,6 +28,11 @@ feature -- Execution
execute (ctx: C; req: WSF_REQUEST; res: WSF_RESPONSE)
-- 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
is_valid_context: is_valid_context (req)
deferred

View File

@@ -106,27 +106,76 @@ feature -- Query
end
end
feature -- Query
feature -- Item
path_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Parameter value for path variable `a_name'
deferred
end
query_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Parameter value for query variable `a_name'
--| i.e after the ? character
item (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Variable value for parameter or variable `a_name'
-- See `{WSF_REQUEST}.item(s)'
deferred
end
parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Any parameter value for variable `a_name'
-- URI template parameter and query parameters
-- Variable value for parameter or variable `a_name'
-- See `{WSF_REQUEST}.item(s)'
obsolete "[2012-Mars-19] Use `item (a_name)' ."
do
Result := query_parameter (a_name)
if Result = Void then
Result := path_parameter (a_name)
end
Result := item (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
feature -- Convertion
@@ -149,75 +198,9 @@ feature -- Convertion
end
end
feature -- Path parameter
feature {NONE} -- Implementation
is_integer_path_parameter (a_name: READABLE_STRING_8): BOOLEAN
-- 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]
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]
-- Array of string values for query parameter `a_name' if relevant.
local
i: INTEGER
@@ -230,7 +213,7 @@ feature -- String parameter
until
i = 0
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)
n := n + 1
i := i + 1
@@ -241,14 +224,6 @@ feature -- String parameter
Result.keep_head (n - 1)
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
copyright: "2011-2012, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"