From 2f3a462c42fef0cecbbb1cc02fb8f235c07d9670 Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Mon, 16 Sep 2013 18:04:43 +0200 Subject: [PATCH] Fixed type having a semicolon in a parameter value such as "text/plain; param1=%"something;foo=bar%"; param2=%"another-thing%" --- .../protocol/http/src/http_media_type.e | 81 +++++++++++++------ .../http/tests/http_content_type_set.e | 11 ++- .../protocol/http/tests/tests-safe.ecf | 11 ++- 3 files changed, 73 insertions(+), 30 deletions(-) diff --git a/library/network/protocol/http/src/http_media_type.e b/library/network/protocol/http/src/http_media_type.e index 0a2a11b6..940f3d84 100644 --- a/library/network/protocol/http/src/http_media_type.e +++ b/library/network/protocol/http/src/http_media_type.e @@ -63,6 +63,7 @@ feature {NONE} -- Initialization t: STRING_8 i,n: INTEGER p: INTEGER + cl: CELL [INTEGER] do -- Ignore starting space (should not be any) from @@ -79,18 +80,13 @@ feature {NONE} -- Initialization if p > 0 then t := s.substring (i, p - 1) from + create cl.put (p) + i := p + 1 until i >= n loop - i := p + 1 - p := s.index_of (';', i) - if p = 0 then - add_parameter_from_string (s, i, n) - i := n - else - add_parameter_from_string (s, i, p - 1) - i := p + 1 - end + add_parameter_from_string (s, i, cl) + i := cl.item end else t := s.substring (i, n) @@ -271,45 +267,80 @@ feature -- Element change feature {NONE} -- Implementation - add_parameter_from_string (s: READABLE_STRING_8; start_index, end_index: INTEGER) + add_parameter_from_string (s: READABLE_STRING_8; start_index: INTEGER; out_end_index: CELL [INTEGER]) -- Add parameter from string " attribute=value " + -- and put in `out_end_index' the index after found parameter. local + n: INTEGER pn,pv: STRING_8 i: INTEGER - p: INTEGER + p, q: INTEGER err: BOOLEAN do - -- Skip spaces + n := s.count + -- Skip spaces from i := start_index until - i > end_index or not s[i].is_space + i > n or not s[i].is_space loop i := i + 1 end - if i < end_index then + if s[i] = ';' then + -- empty parameter + out_end_index.replace (i + 1) + elseif i < n then p := s.index_of ('=', i) - if p > 0 and p < end_index then + if p > 0 then pn := s.substring (i, p - 1) - pv := s.substring (p + 1, end_index) - pv.right_adjust - if pv.count > 0 and pv [1] = '%"' then - if pv [pv.count] = '%"' then - pv := pv.substring (2, pv.count - 1) + if p >= n then + pv := "" + out_end_index.replace (n + 1) + else + if s[p+1] = '%"' then + q := s.index_of ('%"', p + 2) + if q > 0 then + pv := s.substring (p + 2, q - 1) + from + i := q + 1 + until + i > n or not s[i].is_space + loop + i := i + 1 + end + if s[i] = ';' then + i := i + 1 + end + out_end_index.replace (i) + else + err := True + pv := "" + -- missing closing double quote. + end else - err := True - -- missing closing double quote. + q := s.index_of (';', p + 1) + if q = 0 then + q := n + 1 + end + pv := s.substring (p + 1, q - 1) + out_end_index.replace (q + 1) + end + pv.right_adjust + if not err then + add_parameter (pn, pv) end - end - if not err then - add_parameter (pn, pv) end else -- expecting: attribute "=" value err := True end end + if err then + out_end_index.replace (n + 1) + end has_error := has_error or err + ensure + entry_processed: out_end_index.item > start_index end feature {NONE} -- Internal diff --git a/library/network/protocol/http/tests/http_content_type_set.e b/library/network/protocol/http/tests/http_content_type_set.e index 3222c967..1a37ffba 100644 --- a/library/network/protocol/http/tests/http_content_type_set.e +++ b/library/network/protocol/http/tests/http_content_type_set.e @@ -12,9 +12,16 @@ inherit feature -- Content type - test_http_content_type + + test_http_content_types_with_params local - ct: HTTP_CONTENT_TYPE + do + test_content_type_with_params ("text/plain; param1=%"something;foo=bar%"; param2=%"another-thing%"", + "text", "plain", <<["param1", "something;foo=bar"], ["param2", "another-thing"]>> + ) + end + + test_http_content_type do test_content_type ("application/atom+xml", "application", "atom+xml") -- Atom feeds test_content_type ("application/ecmascript", "application", "ecmascript") -- ECMAScript/JavaScript; Defined in RFC 4329 (equivalent to application/javascript but with stricter processing rules) diff --git a/library/network/protocol/http/tests/tests-safe.ecf b/library/network/protocol/http/tests/tests-safe.ecf index 958be121..e2401a84 100644 --- a/library/network/protocol/http/tests/tests-safe.ecf +++ b/library/network/protocol/http/tests/tests-safe.ecf @@ -1,5 +1,5 @@ - + @@ -7,11 +7,16 @@ /EIFGENs$ /.svn$ - - + + +