Repository: aio-libs/aiohttp

Issue:
filter_cookies not handling no path condition
## Long story short

When using cookies, if no path is specified, the cookie is incorrectly filtered. 
This results in the issue I raised on stack overflow here: http://stackoverflow.com/questions/38786216/aiohttp-client-not-handling-cookies-correctly

RFC 6265 - https://tools.ietf.org/html/rfc6265#section-5.1.4 point 2 says this:

```
2.  If the uri-path is empty or if the first character of the uri-
       path is not a %x2F ("/") character, output %x2F ("/") and skip
       the remaining steps.
```
## Expected behaviour

Cookie not to be filtered when uri is of the form: `http://127.0.0.1:8080`
## Actual behaviour

Cookie is filtered. Cookie not filtered when of the form: `http://127.0.0.1:8080/`
## Steps to reproduce

Take a look at aforementioned stack overflow question.
## Potential Fix

I was playing with helpers.py and added the following:

``` python

    @staticmethod
    def _is_path_match(req_path, cookie_path):
        """Implements path matching adhering to RFC 6265."""

        if not len(req_path):
            req_path = '/'

        if req_path == cookie_path:
            return True
```

It was like this:

``` python
    @staticmethod
    def _is_path_match(req_path, cookie_path):
        """Implements path matching adhering to RFC 6265."""
        if req_path == cookie_path:
            return True
```
## Your environment

Windows x64
Anaconda

```
> pip show aiohttp

---
Metadata-Version: 2.0
Name: aiohttp
Version: 0.22.5
Summary: http client/server for asyncio
Home-page: https://github.com/KeepSafe/aiohttp/
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
Installer: pip
License: Apache 2
Location: c:\projects\aio-rpc\venv\lib\site-packages
Requires: chardet, multidict
Classifiers:
  License :: OSI Approved :: Apache Software License
  Intended Audience :: Developers
  Programming Language :: Python
  Programming Language :: Python :: 3
  Programming Language :: Python :: 3.4
  Programming Language :: Python :: 3.5
  Topic :: Internet :: WWW/HTTP
```



Discussion:
Actually, I am no longer sure if this is a bug at all, because of this stackexchange answer http://webmasters.stackexchange.com/a/35646 referencing this RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html

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

So I guess I wasn't providing a valid URI. Perhaps the client session could append the '/' when no absolute path is given?

If nobody is working on it, I'll take it.


Resolving patch:
diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py
--- a/aiohttp/helpers.py
+++ b/aiohttp/helpers.py
@@ -741,6 +741,9 @@ def _is_domain_match(domain, hostname):
     @staticmethod
     def _is_path_match(req_path, cookie_path):
         """Implements path matching adhering to RFC 6265."""
+        if not req_path.startswith("/"):
+            req_path = "/"
+
         if req_path == cookie_path:
             return True
 


Test patch:
diff --git a/tests/test_helpers.py b/tests/test_helpers.py
--- a/tests/test_helpers.py
+++ b/tests/test_helpers.py
@@ -625,6 +625,7 @@ def test_path_matching(self):
         test_func = helpers.CookieJar._is_path_match
 
         self.assertTrue(test_func("/", ""))
+        self.assertTrue(test_func("", "/"))
         self.assertTrue(test_func("/file", ""))
         self.assertTrue(test_func("/folder/file", ""))
         self.assertTrue(test_func("/", "/"))