我将使用Hyper发送HTTP请求,但当响应中包含多个cookie时,Hyper会将它们合并成一个,导致解析程序无法正常运行。例如,以下是一个简单的PHP脚本:
<?php
setcookie("hello", "world");
setcookie("foo", "bar");
使用curl进行响应:
$ curl -sLD - http://local.example.com/test.php
HTTP/1.1 200 OK
Date: Sat, 24 Dec 2016 09:24:04 GMT
Server: Apache/2.4.25 (Unix) PHP/7.0.14
X-Powered-By: PHP/7.0.14
Set-Cookie: hello=world
Set-Cookie: foo=bar
Content-Length: 0
Content-Type: text/html; charset=UTF-8
然而对于下面的Rust代码:
let client = Client::new();
let response = client.get("http://local.example.com/test.php")
.send()
.unwrap();
println!("{:?}", response);
for header in response.headers.iter() {
println!("{}: {}", header.name(), header.value_string());
}
输出结果将是:
Response { status: Ok, headers: Headers { Date: Sat, 24 Dec 2016 09:31:54 GMT, Server: Apache/2.4.25 (Unix) PHP/7.0.14, X-Powered-By: PHP/7.0.14, Set-Cookie: hello=worldfoo=bar, Content-Length: 0, Content-Type: text/html; charset=UTF-8, }, version: Http11, url: "http://local.example.com/test.php", status_raw: RawStatus(200, "OK"), message: Http11Message { is_proxied: false, method: None, stream: Wrapper { obj: Some(Reading(SizedReader(remaining=0))) } } }
Date: Sat, 24 Dec 2016 09:31:54 GMT
Server: Apache/2.4.25 (Unix) PHP/7.0.14
X-Powered-By: PHP/7.0.14
Set-Cookie: hello=worldfoo=bar
Content-Length: 0
Content-Type: text/html; charset=UTF-8
这对我来说似乎很奇怪。我使用Wireshark捕获响应,发现其中有两个 Set-Cookie
标头。我还查了Hyper文档但没有找到线索...
我注意到Hyper内部使用VecMap<HeaderName, Item>
存储标头。所以他们把它们连接在一起了吗?那么我应该如何将它们划分为单独的cookie呢?
hyper
可能更喜欢将 cookie 合并到一个分号分隔的标题字段中... (http://hyper.rs/hyper/async/hyper/header/struct.Cookie.html) - Frederick Zhang