From 30261632f60f9858015681a8d2dcf106a67a700e Mon Sep 17 00:00:00 2001 From: jvelilla Date: Thu, 19 Mar 2015 15:23:06 -0300 Subject: [PATCH] Updated HTTP_COOKIE, enable to add a cookie with empty value. Added feature to check if a date is valid rcf1123 is_valid_rfc1123_date. Added test cases related to valid cookie dates. Updated wsf_response add_cookie basedo on review comments. --- .../network/protocol/http/src/http_cookie.e | 16 +++++++++--- .../http/tests/http_cookie_test_set.e | 25 +++++++++++++++++++ library/server/wsf/src/wsf_response.e | 21 ++-------------- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/library/network/protocol/http/src/http_cookie.e b/library/network/protocol/http/src/http_cookie.e index a896e3b2..e46c07fa 100644 --- a/library/network/protocol/http/src/http_cookie.e +++ b/library/network/protocol/http/src/http_cookie.e @@ -33,7 +33,6 @@ feature {NONE} -- Initialization -- Create an object instance of cookie with name `a_name' and value `a_value'. require a_name_not_blank: a_name /= Void and then not a_name.is_whitespace - a_value_not_empty: a_value /= Void and then not a_value.is_empty a_name_has_valid_characters: a_name /= Void and then has_valid_characters (a_name) a_value_has_valid_characters: a_value /= Void and then has_valid_characters (a_value) do @@ -110,6 +109,16 @@ feature -- Access -- Does the Set-Cookie header include Expires attribute? --|By default will include both. + + is_valid_rfc1123_date (a_string: READABLE_STRING_8): BOOLEAN + -- Is the date represented by `a_string' a valid rfc1123 date? + local + d: HTTP_DATE + do + create d.make_from_string (a_string) + Result := not d.has_error and then d.rfc1123_string.same_string (a_string) + end + feature -- Change Element set_name (a_name: READABLE_STRING_8) @@ -126,7 +135,6 @@ feature -- Change Element set_value (a_value: READABLE_STRING_8) -- Set `value' with `a_value'. require - a_value_not_empty: a_value /= Void and then not a_value.is_empty a_value_has_valid_characters: a_value /= Void and then has_valid_characters (a_value) do value := a_value @@ -136,6 +144,8 @@ feature -- Change Element set_expiration (a_date: READABLE_STRING_8) -- Set `expiration' with `a_date' + require + rfc1133_date: a_date /= Void and then is_valid_rfc1123_date (a_date) do expiration := a_date ensure @@ -163,7 +173,7 @@ feature -- Change Element -- Note: you should avoid using "localhost" as `domain' for local cookies -- since they are not always handled by browser (for instance Chrome) require - domain_without_port_info: a_domain /= Void implies a_domain.index_of (':', 1) = 0 + domain_without_port_info: a_domain /= Void implies not a_domain.has (':') do domain := a_domain ensure diff --git a/library/network/protocol/http/tests/http_cookie_test_set.e b/library/network/protocol/http/tests/http_cookie_test_set.e index 55fb1f36..182f0efd 100644 --- a/library/network/protocol/http/tests/http_cookie_test_set.e +++ b/library/network/protocol/http/tests/http_cookie_test_set.e @@ -36,6 +36,15 @@ feature -- Test routines assert ("Not valid Dquote", not l_cookie.has_valid_characters ("Use!%"12")) end + test_cookie_empty_value + -- values (cookie name and value) + local + l_cookie: HTTP_COOKIE + do + create l_cookie.make ("user_id", "") + assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=; Max-Age=-1")) + end + test_cookie_full_attributes local l_cookie: HTTP_COOKIE @@ -124,6 +133,22 @@ feature -- Test routines assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Max-Age=120")) end + test_cookie_date_rfc1123_valid + local + l_cookie: HTTP_COOKIE + do + create l_cookie.make ("user_id", "u12345") + assert ("Valid RFC1123", l_cookie.is_valid_rfc1123_date ("Thu, 19 Mar 2015 16:14:03 GMT")) + end + + test_cookie_date_rfc1123_invalid + local + l_cookie: HTTP_COOKIE + do + create l_cookie.make ("user_id", "u12345") + assert ("Invalid RFC1123", not l_cookie.is_valid_rfc1123_date ("Thuesday, 19 Mar 2015 16:14:03 GMT")) + end + end diff --git a/library/server/wsf/src/wsf_response.e b/library/server/wsf/src/wsf_response.e index 0a906c20..58242c29 100644 --- a/library/server/wsf/src/wsf_response.e +++ b/library/server/wsf/src/wsf_response.e @@ -331,7 +331,7 @@ feature -- Header add cookie internal_header.headers as ic until l_same_cookie_name loop - if is_cookie_line (ic.item) then + if ic.item.starts_with ("Set-Cookie:") then l_same_cookie_name := has_cookie_name (ic.item, a_cookie.name) end end @@ -560,24 +560,7 @@ feature {NONE} -- Implemenation from until not a_cookie_line[j].is_space loop j := j + 1 end - if a_cookie_line.substring (j, i).same_string (a_cookie_name) then - Result := True - end - end - end - - - is_cookie_line (a_line: READABLE_STRING_32): BOOLEAN - -- Is the line `a_line' a cookie line? - --| Set-Cookie: user_id=%"u12;345%"; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly - local - j: INTEGER - do - Result := False - j := a_line.index_of (':', 1) - if j > 0 then - j := j - 1 - if a_line.substring (1, j).same_string ("Set-Cookie") then + if a_cookie_name.same_characters (a_cookie_line, j, i, 1) then Result := True end end