Couldn't work property if the path in an URI is omitted. #115

Closed
Teshootub7 opened this Issue Sep 23, 2012 · 1 comment

2 participants

@Teshootub7

Hi,

I noticed that if the path in an URI is omitted, e.g. "http://www.example.com" (It has no trailing slash), HTTPClient won't handle the cookies properly.

clnt = HTTPClient.new
clnt.debug_dev = STDERR
clnt.get( "http://www.google.com" ) # the first "get"
clnt.get( "http://www.google.com" ) # the second "get"

The above code prints out the following:

= Request
GET / HTTP/1.1
Date: Sun, 23 Sep 2012 05:20:06 GMT
Host: www.google.com

= Response
HTTP/1.1 302 Found
[...]
Set-Cookie: PREF=ID=fc560d845d0d369f:FF=0:TM=1348377606:LM=1348377606:S=ZIpQA7KNL8WVOWIn; expires=Tue, 23-Sep-2014 05:20:06 GMT; path=/; domain=.google.com
Set-Cookie: NID=64=CqPkgeYo3P91cloWjQM45Zxg_7YvQl2iUcvhm_Xhd7qQmjfbSjlAlWqvEETvzhkbkFyom7c-Nxci3yhzJvQZp-vylny5fxAAWqafuRQXoMOJ1LV2My0NBKVCh1E--j87; expires=Mon, 25-Mar-2013 05:20:06 GMT; path=/; domain=.google.com; HttpOnly
[...]

= Request
GET / HTTP/1.1
Date: Sun, 23 Sep 2012 05:20:06 GMT
Host: www.google.com

In the first "get", www.google.com returns the response with two Set-Cookie headers. However, the second "get" does not send the HTTP request with "Cookie" header.


RFC 2616 (HTTP/1.1) and RFC 3986 (URI: Generic Syntax) stated that if the path component in an URI is omitted, the application should assume it as "/".

RFC 2616 Section 5.1.2 describes:

Note that the absolute path cannot be empty; if none is present in
the original URI, it MUST be given as "/" (the server root).

And RFC 3986 Section 6.2.3 describes:

For example,
because the "http" scheme makes use of an authority component, has a
default port of "80", and defines an empty path to be equivalent to
"/", the following four URIs are equivalent:

http://example.com
http://example.com/
http://example.com:/
http://example.com:80/

I made a patch with testcase to fix the problem.

diff --git a/lib/httpclient/cookie.rb b/lib/httpclient/cookie.rb
index 1610384..7865438 100644
--- a/lib/httpclient/cookie.rb
+++ b/lib/httpclient/cookie.rb
@@ -133,7 +133,7 @@ class WebAgent
       domainname = url.host
       if (!domainname ||
      !domain_match(domainname, @domain) ||
-     (@path && !head_match?(@path, url.path)) ||
+     (@path && !head_match?(@path, url.path.empty? ? "/" : url.path)) ||
      (@secure && (url.scheme != 'https')) )
    return false
       else
diff --git a/test/test_cookie.rb b/test/test_cookie.rb
index 36bb645..b5e28b4 100644
--- a/test/test_cookie.rb
+++ b/test/test_cookie.rb
@@ -68,6 +68,11 @@ class TestCookie < Test::Unit::TestCase
 #    @c.port = [80,8080]
     assert_equal(true, @c.match?(url))

+    url_nopath = URI.parse('http://www.rubycolor.org')
+    @c.domain = 'www.rubycolor.org'
+    @c.path = '/'
+    assert_equal(true, @c.match?(url_nopath))
+
   end

   def test_head_match?()

Thanks,

@nahi nahi added a commit that closed this issue Oct 10, 2012
@nahi Cookies couldn't work property if the path in an URI is omitted
Merged the patch from Teshootub7. Thanks!

Fixes #115.
17d5474
@nahi nahi closed this in 17d5474 Oct 10, 2012
@nahi
Owner

Sorry for late response. Thanks for the patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment