Added visitor patterns to WSF_VALUE

Handling UTF-8 unencoding for WSF_VALUE ...
Added WSF_TABLE_VALUE to handle list[]=a&list[]=b ...

Library encoder: added UTF8 facilities
This commit is contained in:
Jocelyn Fiat
2011-10-24 17:23:36 +02:00
parent 663a39d2ec
commit fb7854fbcc
27 changed files with 781 additions and 52 deletions

View File

@@ -0,0 +1,68 @@
note
description: "[
Iterator visitor on WSF_VALUE using agent for callback
]"
date: "$Date$"
revision: "$Revision$"
class
WSF_VALUE_AGENT_ITERATOR
inherit
WSF_VALUE_ITERATOR
redefine
process_table,
process_multiple_string,
process_string
end
create
make
feature {NONE} -- Initialization
make
do
create on_table_actions
create on_string_actions
create on_multiple_string_actions
end
feature -- Actions
on_table_actions: ACTION_SEQUENCE [TUPLE [WSF_TABLE_VALUE]]
on_string_actions: ACTION_SEQUENCE [TUPLE [WSF_STRING_VALUE]]
on_multiple_string_actions: ACTION_SEQUENCE [TUPLE [WSF_MULTIPLE_STRING_VALUE]]
feature -- Visitor
process_table (v: WSF_TABLE_VALUE)
do
on_table_actions.call ([v])
process_iterable_of_value (v)
end
process_string (v: WSF_STRING_VALUE)
do
on_string_actions.call ([v])
end
process_multiple_string (v: WSF_MULTIPLE_STRING_VALUE)
do
on_multiple_string_actions.call ([v])
process_iterable_of_value (v)
end
;note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end

View File

@@ -0,0 +1,51 @@
note
description: "[
Iterator visitor on WSF_VALUE
]"
date: "$Date$"
revision: "$Revision$"
class
WSF_VALUE_ITERATOR
inherit
WSF_VALUE_VISITOR
feature -- Helper
process_iterable_of_value (it: ITERABLE [WSF_VALUE])
do
across
it as c
loop
c.item.process (Current)
end
end
feature -- Visitor
process_table (v: WSF_TABLE_VALUE)
do
process_iterable_of_value (v)
end
process_string (v: WSF_STRING_VALUE)
do
end
process_multiple_string (v: WSF_MULTIPLE_STRING_VALUE)
do
process_iterable_of_value (v)
end
;note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end

View File

@@ -0,0 +1,38 @@
note
description: "[
NULL Visitor implementation
]"
date: "$Date$"
revision: "$Revision$"
class
WSF_VALUE_NULL_VISITOR
inherit
WSF_VALUE_VISITOR
feature -- Visitor
process_table (v: WSF_TABLE_VALUE)
do
end
process_string (v: WSF_STRING_VALUE)
do
end
process_multiple_string (v: WSF_MULTIPLE_STRING_VALUE)
do
end
;note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end

View File

@@ -0,0 +1,48 @@
note
description: "[
Component to visit WSF_VALUE
]"
date: "$Date$"
revision: "$Revision$"
deferred class
WSF_VALUE_VISITOR
feature -- Visitor
safe_process_value (v: detachable WSF_TABLE_VALUE)
do
if v /= Void then
v.process (Current)
end
end
process_table (v: WSF_TABLE_VALUE)
require
v_attached: v /= Void
deferred
end
process_string (v: WSF_STRING_VALUE)
require
v_attached: v /= Void
deferred
end
process_multiple_string (v: WSF_MULTIPLE_STRING_VALUE)
require
v_attached: v /= Void
deferred
end
;note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software
5949 Hollister Ave., Goleta, CA 93117 USA
Telephone 805-685-1006, Fax 805-685-6869
Website http://www.eiffel.com
Customer support http://support.eiffel.com
]"
end

View File

@@ -127,10 +127,17 @@ feature -- Element change
string_values.extend (s)
end
feature -- Visitor
process (vis: WSF_VALUE_VISITOR)
do
vis.process_multiple_string (Current)
end
invariant
string_values_not_empty: string_values.count >= 1
;note
note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[

View File

@@ -65,20 +65,14 @@ feature -- Conversion
create Result.make_from_string (string)
end
feature {NONE} -- Implementation
feature -- Visitor
url_decoded_string (s: READABLE_STRING_8): READABLE_STRING_32
-- Decoded url-encoded string `s'
process (vis: WSF_VALUE_VISITOR)
do
Result := url_encoder.decoded_string (s)
vis.process_string (Current)
end
url_encoder: URL_ENCODER
once
create Result
end
;note
note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[

View File

@@ -0,0 +1,131 @@
note
description: "[
Table value which can contain value indexed by a key
]"
date: "$Date$"
revision: "$Revision$"
class
WSF_TABLE_VALUE
inherit
WSF_VALUE
ITERABLE [WSF_VALUE]
create
make
feature {NONE} -- Initialization
make (a_name: READABLE_STRING_8)
do
name := url_decoded_string (a_name)
create values.make (5)
end
feature -- Access
name: READABLE_STRING_32
first_value: detachable WSF_VALUE
-- First value if any.
do
across
values as c
until
Result /= Void
loop
Result := c.item
end
end
first_key: detachable READABLE_STRING_32
do
across
values as c
until
Result /= Void
loop
Result := c.key
end
end
values: HASH_TABLE [WSF_VALUE, READABLE_STRING_32]
value (k: READABLE_STRING_32): detachable WSF_VALUE
do
Result := values.item (k)
end
count: INTEGER
do
Result := values.count
end
feature -- Element change
add_value (a_value: WSF_VALUE; k: READABLE_STRING_32)
require
same_name: a_value.name.same_string (name)
do
values.force (a_value, k)
end
feature -- Traversing
new_cursor: ITERATION_CURSOR [WSF_VALUE]
do
Result := values.new_cursor
end
feature -- Helper
same_string (a_other: READABLE_STRING_GENERAL): BOOLEAN
-- Does `a_other' represent the same string as `Current'?
do
if values.count = 1 and then attached first_value as f then
Result := f.same_string (a_other)
end
end
is_case_insensitive_equal (a_other: READABLE_STRING_8): BOOLEAN
-- Does `a_other' represent the same case insensitive string as `Current'?
do
if values.count = 1 and then attached first_value as f then
Result := f.is_case_insensitive_equal (a_other)
end
end
as_string: STRING_32
do
create Result.make_from_string ("{")
if values.count = 1 and then attached first_key as fk then
Result.append (fk + ": ")
if attached value (fk) as fv then
Result.append (fv.as_string)
else
Result.append ("???")
end
else
across
values as c
loop
if Result.count > 1 then
Result.append_character (',')
end
Result.append (c.key + ": ")
Result.append (c.item.as_string)
end
end
Result.append_character ('}')
end
feature -- Visitor
process (vis: WSF_VALUE_VISITOR)
do
vis.process_table (Current)
end
end

View File

@@ -1,6 +1,5 @@
note
description: "Summary description for {WSF_VALUE}."
author: ""
date: "$Date$"
revision: "$Revision$"
@@ -28,7 +27,7 @@ feature -- Helper
end
is_case_insensitive_equal (a_other: READABLE_STRING_8): BOOLEAN
-- Does `a_other' represent the same case insensitive string as `Current'?
-- Does `a_other' represent the same case insensitive string as `Current'?
deferred
end
@@ -46,6 +45,25 @@ feature -- Query
deferred
end
feature {NONE} -- Implementation
url_decoded_string (s: READABLE_STRING_8): READABLE_STRING_32
-- Decoded url-encoded string `s'
do
Result := url_encoder.decoded_string (s)
end
url_encoder: URL_ENCODER
once
create {UTF8_URL_ENCODER} Result --| Chrome is UTF-8 encoding the non ascii in query
end
feature -- Visitor
process (vis: WSF_VALUE_VISITOR)
deferred
end
note
copyright: "2011-2011, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

View File

@@ -133,7 +133,7 @@ feature {NONE} -- Access: global variable
end
across
form_data_parameters as vars
form_parameters as vars
loop
Result.force (vars.item, vars.item.name)
end
@@ -163,7 +163,7 @@ feature -- Access: global variable
if v = Void then
v := query_parameter (a_name)
if v = Void then
v := form_data_parameter (a_name)
v := form_parameter (a_name)
if v = Void then
v := cookie (a_name)
end
@@ -866,13 +866,83 @@ feature {NONE} -- Query parameters: implementation
end
end
add_value_to_table (a_name: READABLE_STRING_32; a_value: READABLE_STRING_32; a_table: HASH_TABLE [WSF_VALUE, READABLE_STRING_32])
add_value_to_table (a_name: READABLE_STRING_8; a_value: READABLE_STRING_8; a_table: HASH_TABLE [WSF_VALUE, READABLE_STRING_32])
local
v: WSF_VALUE
v: detachable WSF_VALUE
n,k,r: STRING_8
k32: STRING_32
p,q: INTEGER
tb,ptb: detachable WSF_TABLE_VALUE
do
v := new_string_value (a_name, a_value)
--| Check if this is a list format such as choice[] or choice[a] or even choice[a][] or choice[a][b][c]...
p := a_name.index_of ('[', 1)
if p > 0 then
q := a_name.index_of (']', p + 1)
if q > p then
n := a_name.substring (1, p - 1)
r := a_name.substring (q + 1, a_name.count)
r.left_adjust; r.right_adjust
create tb.make (n)
if a_table.has_key (tb.name) and then attached {WSF_TABLE_VALUE} a_table.found_item as l_existing_table then
tb := l_existing_table
end
k := a_name.substring (p + 1, q - 1)
k.left_adjust; k.right_adjust
if k.is_empty then
k.append_integer (tb.count)
end
v := tb
n.append_character ('[')
n.append (k)
n.append_character (']')
from
until
r.is_empty
loop
ptb := tb
p := r.index_of ({CHARACTER_8} '[', 1)
if p > 0 then
q := r.index_of ({CHARACTER_8} ']', p + 1)
if q > p then
k32 := url_encoder.decoded_string (k)
if attached {WSF_TABLE_VALUE} ptb.value (k32) as l_tb_value then
tb := l_tb_value
else
create tb.make (n)
ptb.add_value (tb, k32)
end
k := r.substring (p + 1, q - 1)
r := r.substring (q + 1, r.count)
r.left_adjust; r.right_adjust
if k.is_empty then
k.append_integer (tb.count)
end
n.append_character ('[')
n.append (k)
n.append_character (']')
end
else
r.wipe_out
--| Ignore bad value
end
end
tb.add_value (new_string_value (n, a_value), k)
else
--| Missing end bracket
end
end
if v = Void then
v := new_string_value (a_name, a_value)
end
if a_table.has_key (v.name) and then attached a_table.found_item as l_existing_value then
if attached {WSF_MULTIPLE_STRING_VALUE} l_existing_value as l_multi then
if tb /= Void then
--| Already done in previous part
elseif attached {WSF_MULTIPLE_STRING_VALUE} l_existing_value as l_multi then
l_multi.add_value (v)
else
a_table.force (create {WSF_MULTIPLE_STRING_VALUE}.make_with_array (<<l_existing_value, v>>), v.name)
@@ -883,17 +953,29 @@ feature {NONE} -- Query parameters: implementation
end
end
feature -- Form fields and related
feature -- Form fields and related
form_data_parameters: ITERABLE [WSF_VALUE]
form_data_parameters: like form_parameters
obsolete "[2011-oct-24] Use form_parameters"
do
Result := form_data_parameters_table
Result := form_parameters
end
form_data_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
form_data_parameter (a_name: READABLE_STRING_8): like form_parameter
obsolete "[2011-oct-24] Use form_parameter (a_name:...)"
do
Result := form_parameter (a_name)
end
form_parameters: ITERABLE [WSF_VALUE]
do
Result := form_parameters_table
end
form_parameter (a_name: READABLE_STRING_8): detachable WSF_VALUE
-- Field for name `a_name'.
do
Result := form_data_parameters_table.item (a_name)
Result := form_parameters_table.item (a_name)
end
uploaded_files: HASH_TABLE [WGI_UPLOADED_FILE_DATA, STRING]
@@ -907,7 +989,7 @@ feature -- Form fields and related
feature {NONE} -- Form fields and related
form_data_parameters_table: HASH_TABLE [WSF_VALUE, READABLE_STRING_32]
form_parameters_table: HASH_TABLE [WSF_VALUE, READABLE_STRING_32]
-- Variables sent by POST request
local
vars: like internal_form_data_parameters_table
@@ -1156,7 +1238,7 @@ feature {NONE} -- Temporary File handling
feature {NONE} -- Implementation: Form analyzer
analyze_multipart_form (t: STRING; s: STRING; vars: like form_data_parameters_table)
analyze_multipart_form (t: STRING; s: STRING; vars: like form_parameters_table)
-- Analyze multipart form content
--| FIXME[2011-06-21]: integrate eMIME parser library
require
@@ -1222,16 +1304,16 @@ feature {NONE} -- Implementation: Form analyzer
end
end
analyze_multipart_form_input (s: STRING; vars_post: like form_data_parameters_table)
analyze_multipart_form_input (s: STRING; vars_post: like form_parameters_table)
-- Analyze multipart entry
require
s_not_empty: s /= Void and then not s.is_empty
local
n, i,p, b,e: INTEGER
l_name, l_filename, l_content_type: detachable STRING
l_header: detachable STRING
l_content: detachable STRING
l_line: detachable STRING
l_name, l_filename, l_content_type: detachable STRING_8
l_header: detachable STRING_8
l_content: detachable STRING_8
l_line: detachable STRING_8
l_up_file_info: WGI_UPLOADED_FILE_DATA
do
from
@@ -1372,7 +1454,7 @@ feature {NONE} -- Internal value
internal_query_parameters_table: detachable like query_parameters_table
-- cached value for `query_parameters'
internal_form_data_parameters_table: detachable like form_data_parameters_table
internal_form_data_parameters_table: detachable like form_parameters_table
-- cached value for `form_fields'
internal_cookies_table: detachable like cookies_table
@@ -1487,7 +1569,7 @@ feature {NONE} -- Implementation: utilities
url_encoder: URL_ENCODER
once
create Result
create {UTF8_URL_ENCODER} Result
end
date_time_utilities: HTTP_DATE_TIME_UTILITIES