Skip to content

Commit 4a35ff7

Browse files
committed
Updated code based on Jocelyn's comments.
1 parent 871e979 commit 4a35ff7

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

library/network/protocol/http/src/http_cookie.e

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ note
33
This class represents the value of a HTTP cookie, transferred in a request.
44
The class has features to build an HTTP cookie.
55
6-
Following a newer RFC standard for Cookies RCF6265.
6+
Following a newer RFC standard for Cookies http://tools.ietf.org/html/rfc6265
77
88
Domain
99
* WARNING: Some existing user agents treat an absent Domain attribute as if the Domain attribute were present and contained the current host name.
@@ -20,7 +20,7 @@ note
2020
]"
2121
date: "$Date$"
2222
revision: "$Revision$"
23-
EIS: "name=HTTP Cookie specification", "src=https://httpwg.github.io/specs/rfc6265.html", "protocol=uri"
23+
EIS: "name=HTTP Cookie specification", "src=http://tools.ietf.org/html/rfc6265", "protocol=uri"
2424
class
2525
HTTP_COOKIE
2626

@@ -29,7 +29,7 @@ create
2929

3030
feature {NONE} -- Initialization
3131

32-
make (a_name: READABLE_STRING_32; a_value: READABLE_STRING_32)
32+
make (a_name: READABLE_STRING_8; a_value: READABLE_STRING_8)
3333
-- Create an object instance of cookie with name `a_name' and value `a_value'.
3434
require
3535
make_sense: (a_name /= Void and a_value /= Void) and then (not a_name.is_empty and not a_value.is_empty)
@@ -45,21 +45,21 @@ feature {NONE} -- Initialization
4545

4646
feature -- Access
4747

48-
name: STRING_32
48+
name: STRING_8
4949
-- name of the cookie.
5050

51-
value: STRING_32
51+
value: STRING_8
5252
-- value of the cookie.
5353

54-
expiration: detachable STRING
54+
expiration: detachable STRING_8
5555
-- Value of the Expires attribute.
5656

57-
path: detachable STRING_32
57+
path: detachable STRING_8
5858
-- Value of the Path attribute.
5959
-- Path to which the cookie applies.
6060
--| The path "/", specify a cookie that apply to all URLs in your site.
6161

62-
domain: detachable STRING_32
62+
domain: detachable STRING_8
6363
-- Value of the Domain attribute.
6464
-- Domain to which the cookies apply.
6565

@@ -79,23 +79,22 @@ feature -- Access
7979
--| By default max_age < 0 indicate a cookie will last only for the current user-agent (Browser, etc) session.
8080
--| A value of 0 instructs the user-agent to delete the cookie.
8181

82-
has_valid_characters (a_name: READABLE_STRING_32):BOOLEAN
82+
has_valid_characters (a_name: READABLE_STRING_GENERAL):BOOLEAN
8383
-- Has `a_name' valid characters for cookies?
8484
local
8585
l_iterator: STRING_ITERATION_CURSOR
8686
l_found: BOOLEAN
8787
do
8888
create l_iterator.make (a_name)
89-
from
90-
l_iterator.start
89+
90+
across
91+
l_iterator as ic
9192
until
92-
l_iterator.after or l_found
93+
l_found
9394
loop
94-
if valid_characters.index_of (l_iterator.item.to_character_8, 0) = -1 then
95+
if is_valid_character (ic.item.to_character_8) then
9596
Result := False
9697
l_found := True
97-
else
98-
l_iterator.forth
9998
end
10099
end
101100
end
@@ -110,28 +109,28 @@ feature -- Access
110109

111110
feature -- Change Element
112111

113-
set_name (a_name: READABLE_STRING_32)
112+
set_name (a_name: READABLE_STRING_GENERAL)
114113
-- Set `name' with `a_name'.
115114
do
116-
name := a_name
115+
name := a_name.as_string_8
117116
ensure
118117
name_set: name = a_name
119118
end
120119

121-
set_value (a_value: READABLE_STRING_32)
120+
set_value (a_value: READABLE_STRING_GENERAL)
122121
-- Set `value' with `a_value'.
123122
do
124-
value := a_value
123+
value := a_value.as_string_8
125124
ensure
126125
value_set: value = a_value
127126
end
128127

129-
set_expiration (a_date: READABLE_STRING_32)
128+
set_expiration (a_date: READABLE_STRING_GENERAL)
130129
-- Set `expiration' with `a_date'
131130
do
132-
expiration := a_date
131+
expiration := a_date.as_string_32
133132
ensure
134-
expiration_set: attached expiration as l_expiration and then l_expiration.same_string (a_date)
133+
expiration_set: attached expiration as l_expiration and then l_expiration.same_string (a_date.as_string_8)
135134
end
136135

137136
set_expiration_date (a_date: DATE_TIME)
@@ -142,22 +141,22 @@ feature -- Change Element
142141
expiration_set: attached expiration as l_expiration and then l_expiration.same_string (date_to_rfc1123_http_date_format (a_date))
143142
end
144143

145-
set_path (a_path: READABLE_STRING_32)
144+
set_path (a_path: READABLE_STRING_GENERAL)
146145
-- Set `path' with `a_path'
147146
do
148-
path := a_path
147+
path := a_path.as_string_8
149148
ensure
150149
path_set: path = a_path
151150
end
152151

153-
set_domain (a_domain: READABLE_STRING_32)
152+
set_domain (a_domain: READABLE_STRING_GENERAL)
154153
-- Set `domain' with `a_domain'
155154
-- Note: you should avoid using "localhost" as `domain' for local cookies
156155
-- since they are not always handled by browser (for instance Chrome)
157156
require
158157
domain_without_port_info: a_domain /= Void implies a_domain.index_of (':', 1) = 0
159158
do
160-
domain := a_domain
159+
domain := a_domain.as_string_8
161160
ensure
162161
domain_set: domain = a_domain
163162
end
@@ -236,7 +235,7 @@ feature -- Date Utils
236235

237236
feature -- Output
238237

239-
cookie_header: STRING
238+
header_line: STRING
240239
-- String representation of Set-Cookie header of current.
241240
local
242241
s: STRING
@@ -307,6 +306,31 @@ feature {NONE} -- Constants
307306
Result := ("!#$%%&'()*+-./0123456789:<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~").area
308307
end
309308

309+
310+
is_valid_character (c: CHARACTER): BOOLEAN
311+
-- RFC6265 that specifies that the following is valid for characters in cookies. Cookies are also supposed to be double quoted.
312+
-- The following character ranges are valid:http://tools.ietf.org/html/rfc6265#section-4.1.1
313+
-- %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
314+
-- 0x21: !
315+
-- 0x23-2B: #$%&'()*+
316+
-- 0x2D-3A: -./0123456789:
317+
-- 0x3C-5B: <=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[
318+
-- 0x5D-7E: ]^_`abcdefghijklmnopqrstuvwxyz{|}~
319+
note
320+
EIS: "name=valid-characters", "src=http://tools.ietf.org/html/rfc6265#section-4.1.1", "protocol=uri"
321+
do
322+
Result := True
323+
inspect c.natural_32_code
324+
when 0x21 then
325+
when 0x23 .. 0x2B then
326+
when 0x2D .. 0x3A then
327+
when 0x3C .. 0x5B then
328+
when 0x5D .. 0x7E then
329+
else
330+
Result := False
331+
end
332+
end
333+
310334
note
311335
copyright: "2011-2015, Jocelyn Fiat, Eiffel Software and others"
312336
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"

library/network/protocol/http/tests/http_cookie_test_set.e

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ feature -- Test routines
2020
l_cookie: HTTP_COOKIE
2121
do
2222
create l_cookie.make ("user_id", "u12345")
23-
assert("Expected", l_cookie.cookie_header.same_string ("Set-Cookie: user_id=u12345; Max-Age=-1"))
23+
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Max-Age=-1"))
2424
end
2525

2626
test_cookie_value_with_illegal_characters
@@ -46,7 +46,7 @@ feature -- Test routines
4646
l_cookie.set_path ("/")
4747
l_cookie.set_secure (True)
4848
l_cookie.set_http_only (True)
49-
assert("Expected", l_cookie.cookie_header.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly"))
49+
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly"))
5050
end
5151
5252
test_cookie_include_expires
@@ -60,7 +60,7 @@ feature -- Test routines
6060
l_cookie.set_secure (True)
6161
l_cookie.set_http_only (True)
6262
l_cookie.mark_expires
63-
assert("Expected", l_cookie.cookie_header.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Secure; HttpOnly"))
63+
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Secure; HttpOnly"))
6464
end
6565
6666
test_cookie_full_include_max_age
@@ -74,7 +74,7 @@ feature -- Test routines
7474
l_cookie.set_secure (True)
7575
l_cookie.set_http_only (True)
7676
l_cookie.mark_max_age
77-
assert("Expected", l_cookie.cookie_header.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Max-Age=-1; Secure; HttpOnly"))
77+
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Max-Age=-1; Secure; HttpOnly"))
7878
end
7979
8080
test_cookie_defaults_and_http_only
@@ -83,7 +83,7 @@ feature -- Test routines
8383
do
8484
create l_cookie.make ("user_id", "u12345")
8585
l_cookie.set_http_only (True)
86-
assert("Expected", l_cookie.cookie_header.same_string ("Set-Cookie: user_id=u12345; Max-Age=-1; HttpOnly"))
86+
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Max-Age=-1; HttpOnly"))
8787
end
8888
8989
test_cookie_defaults_and_secure
@@ -92,7 +92,7 @@ feature -- Test routines
9292
do
9393
create l_cookie.make ("user_id", "u12345")
9494
l_cookie.set_secure (True)
95-
assert("Expected", l_cookie.cookie_header.same_string ("Set-Cookie: user_id=u12345; Max-Age=-1; Secure"))
95+
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Max-Age=-1; Secure"))
9696
end
9797
9898
@@ -102,7 +102,7 @@ feature -- Test routines
102102
do
103103
create l_cookie.make ("user_id", "u12345")
104104
l_cookie.set_domain ("www.example.com")
105-
assert("Expected", l_cookie.cookie_header.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Max-Age=-1"))
105+
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Max-Age=-1"))
106106
end
107107
108108
@@ -112,7 +112,7 @@ feature -- Test routines
112112
do
113113
create l_cookie.make ("user_id", "u12345")
114114
l_cookie.set_path ("/")
115-
assert("Expected", l_cookie.cookie_header.same_string ("Set-Cookie: user_id=u12345; Path=/; Max-Age=-1"))
115+
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Path=/; Max-Age=-1"))
116116
end
117117
118118
test_cookie_default_and_custom_max_age
@@ -121,7 +121,7 @@ feature -- Test routines
121121
do
122122
create l_cookie.make ("user_id", "u12345")
123123
l_cookie.set_max_age (120)
124-
assert("Expected", l_cookie.cookie_header.same_string ("Set-Cookie: user_id=u12345; Max-Age=120"))
124+
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Max-Age=120"))
125125
end
126126
127127

0 commit comments

Comments
 (0)