严重的CookieContainer错误? [英] Serious CookieContainer bug?
问题描述
我在这里丢失了某些东西吗,或者这是CookieContainer中的错误吗?
Am I missing something here or is this a bug in the CookieContainer?
我要向容器中添加3个cookie,然后为2个URL调用GetCookieHeader函数:
I'm adding 3 cookies to the container and then I call the GetCookieHeader function for 2 urls:
CookieContainer cc = new CookieContainer();
cc.Add(new Cookie("Cookie1", "1", "/a", "test.com"));
cc.Add(new Cookie("Cookie2", "2", "/a/0/", "test.com"));
cc.Add(new Cookie("Cookie3", "3", "/a/1/", "test.com"));
var result1 = cc.GetCookieHeader(new Uri("http://test.com/a/1/list"));
Assert.AreEqual("Cookie3=3; Cookie1=1", result1);
var result2 = cc.GetCookieHeader(new Uri("http://test.com/a/0/list"));
Assert.AreEqual("Cookie2=2; Cookie1=1", result2);
问题在于最后一个断言会引发异常,因为返回的标头仅为"Cookie2 = 2".我看不到任何原因会在其中省略Cookie1 cookie-根据RFC6265,它应该返回类似于上面第一个断言的两个cookie,不是吗?
The problem is the last assertion which throws an exception as the returned header is only "Cookie2=2". I don't see any reason why the Cookie1 cookie is omitted there - according to RFC6265 it should return two cookies similar to the first assertion above, shouldn't it?
几句话:
-
Cookie都在容器中,因此问题不是添加,而是GetHeader函数.
The cookies are all in the container so it's not the adding that's the problem but the GetHeader function.
添加4个,5个等cookie时,此行为保持不变.只有与最后添加的cookie匹配的路径才会包含基本路径的cookie!
This behaviour stays the same when adding 4, 5 etc. cookies: Only a path matching the last added cookie will include the cookie for the base path!
当删除路径中的所有"a"并且仅将"/","/0/"和"/1/"用作3个cookie和" http://test.com/0/list ").然后所有的断言都成功了-我期望与"a"具有相同的行为!
The behaviour changes when removing all "a" in the paths and using only "/", "/0/" and "/1/" as paths for the 3 cookies and "http://test.com/1/list" and "http://test.com/0/list" in the assertion urls). All the assertions then succeed - I would expect the same behaviour with the "a"!
PS:让我从规格中添加相关部分:
如果满足以下至少一项条件,则请求路径路径将匹配给定的cookie路径:
-Cookie路径和请求路径相同.
-cookie路径是请求路径的前缀,并且cookie路径的最后一个字符是%x2F(/").
-cookie路径是请求路径的前缀,并且cookie路径中未包括的请求路径的第一个字符是%x2F("/")字符./em>
- The cookie-path is a prefix of the request-path, and the first character of the request-path that is not included in the cookie-path is a %x2F ("/") character.
所以对我来说这显然是个错误...?
推荐答案
我相信我找到了问题.在第700-730行附近的函数InternalGetCookies(Uri)中的System.Net.CookieContainer类中,路径上存在一个迭代.找到第一个路径后,将添加这些cookie,然后中断迭代,并仅添加"/"的值!
I believe I found the problem. In the System.Net.CookieContainer class in function InternalGetCookies(Uri) around lines 700-730 there's an iteration over the paths. After the first path is found, those cookies are added and the iteration is then breaked and only the values for "/" are additionally being added!
换句话说:您只能从第一个匹配路径获得cookie,而仅从"/"获得cookie.对我来说,这显然是不当行为,因此是一个错误-或我不理解上面提到的RFC.
In other words: You only get the cookies from the first matching path and those from "/". To me this is a clear misbehaviour and therefore a bug - or I don't understand the RFC I mentioned above.
这篇关于严重的CookieContainer错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!