You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Eugeny N Dzhurinsky <bo...@redwerk.com> on 2006/07/11 17:00:02 UTC
multiple "location" headers are getting concatenated?
Hello, I facing strange problem with HttpClient
For some reason if there are more than 1 Location header in response, all
headers are collected into single string, for instance -
/madeline/?GCID=C1000x047&AID=10280172&PID=1453275, /madeline
I used this test:
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.io.IOException;
public class HTTPClientTest extends TestCase {
HttpClient client;
HttpMethod method;
protected void setUp() throws Exception {
client = new HttpClient();
String url = "http://www.allaboardtoys.com/madeline/default.asp?GCID=C1000x047&AID=10280172&PID=1453275";
URI uri = new URI(url, url.indexOf('%') != -1);
method = new GetMethod();
method.setURI(uri);
HttpMethodParams params = method.getParams();
params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
params.setParameter(HttpClientParams.USER_AGENT, "Mozilla/4.0"
+ "(compatible; MSIE 6.0; Windows NT 5.1;)");
method.setFollowRedirects(true);
}
public void testResultcode() {
try {
client.executeMethod(method);
Assert.assertEquals(HttpStatus.SC_OK, method.getStatusCode());
} catch (HttpException e) {
e.printStackTrace(System.out);
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
}
With tcpflow i found:
request was
---------------------------------------------------------------------------
GET /madeline/default.asp?GCID=C1000x047&AID=10280172&PID=1453275 HTTP/1.1
User-Agent: Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1;)
Host: www.allaboardtoys.com
---------------------------------------------------------------------------
and response
---------------------------------------------------------------------------
HTTP/1.1 302 Object moved
Server: Microsoft-IIS/5.0
Date: Tue, 11 Jul 2006 14:52:28 GMT
X-Powered-By: ASP.NET
P3P: policyref="http://www.allaboardtoys.com/w3c/p3p.xml"
Pragma: no-cache
cache-control: no-store
Cache-Control: no-cache, must-revalidate
Location: http://www.allaboardtoys.com/madeline/?GCID=C1000x047&AID=10280172&PID=1453275
Location: /madeline
Content-Length: 130
Content-Type: text/html
Expires: Mon, 10 Jul 2006 14:52:28 GMT
Set-Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=C08BA8AEFED14F27BF4EF3FF1CE0CD85; expires=Wed, 26-Jul-2006 14:52:28 GMT; domain=allaboardtoys.com; path=/
Set-Cookie: ASPSESSIONIDCATQCDDQ=JLAADADBKEKJBHKKFALPEMHH; path=/
Cache-control: no-cache
<head><title>Object moved</title></head>
<body><h1>Object Moved</h1>This object may be found <a HREF="/madeline">here</a>.</body>
---------------------------------------------------------------------------
then next request
---------------------------------------------------------------------------
GET /madeline/?GCID=C1000x047&AID=10280172&PID=1453275, /madeline HTTP/1.1
User-Agent: Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1;)
Host: www.allaboardtoys.com
Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=C08BA8AEFED14F27BF4EF3FF1CE0CD85
Cookie: ASPSESSIONIDCATQCDDQ=JLAADADBKEKJBHKKFALPEMHH
---------------------------------------------------------------------------
and finally
---------------------------------------------------------------------------
HTTP/1.1 400 Bad Request
Server: Microsoft-IIS/5.0
Date: Tue, 11 Jul 2006 14:52:28 GMT
Content-Type: text/html
Content-Length: 87
<html><head><title>Error</title></head><body>The parameter is incorrect. </body></html>
---------------------------------------------------------------------------
As you can see, instead of http://www.allaboardtoys.com/madeline HttpClient
used
/madeline/?GCID=C1000x047&AID=10280172&PID=1453275, /madeline
which is wrong.
I tried to handle redirects manually, but contents of header "location" are
http://www.allaboardtoys.com/madeline/?GCID=C1000x047&AID=10280172&PID=1453275, /madeline
Is it correct to split it by comma or I can get list of headers manually
somehow?
--
Eugene N Dzhurinsky
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
Re: multiple "location" headers are getting concatenated?
Posted by Roland Weber <ht...@dubioso.net>.
Hello Eugeny,
> For some reason if there are more than 1 Location header in response,
That is a violation of the HTTP spec. RFC 2616, section 14.30:
<quote>
The field value consists of a single absolute URI.
</quote>
It is quite common to see relative URIs instead of absolute ones,
and HttpClient tolerates that. But two URIs is new to me.
> all headers are collected into single string, for instance -
>
> /madeline/?GCID=C1000x047&AID=10280172&PID=1453275, /madeline
Well, that's what getRequestHeader(String) is supposed to do.
Multiple header fields with the same name are equivalent to
a single header field with the individual values concatenated
by commas. RFC 2616, section 4.2 says:
<quote>
Multiple message-header fields with the same field-name MAY be
present in a message if and only if the entire field-value for that
header field is defined as a comma-separated list [i.e., #(values)].
It MUST be possible to combine the multiple header fields into one
"field-name: field-value" pair, without changing the semantics of the
message, by appending each subsequent field-value to the first, each
separated by a comma.
</quote>
If you want the header fields separately, use getRequestHeaders(String)
and you'll get an array in return.
> ---------------------------------------------------------------------------
> HTTP/1.1 302 Object moved
> Server: Microsoft-IIS/5.0
Why am I not surprised?
> Date: Tue, 11 Jul 2006 14:52:28 GMT
> X-Powered-By: ASP.NET
> P3P: policyref="http://www.allaboardtoys.com/w3c/p3p.xml"
> Pragma: no-cache
> cache-control: no-store
> Cache-Control: no-cache, must-revalidate
> Location: http://www.allaboardtoys.com/madeline/?GCID=C1000x047&AID=10280172&PID=1453275
> Location: /madeline
The first one is at least absolute. In your example above,
the protocol and hostname part were missing.
> then next request
>
> ---------------------------------------------------------------------------
> GET /madeline/?GCID=C1000x047&AID=10280172&PID=1453275, /madeline HTTP/1.1
> User-Agent: Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1;)
> Host: www.allaboardtoys.com
> Cookie: ShopperManager%2F=sts=nwc45&ShopperManager%2F=C08BA8AEFED14F27BF4EF3FF1CE0CD85
> Cookie: ASPSESSIONIDCATQCDDQ=JLAADADBKEKJBHKKFALPEMHH
>
> As you can see, instead of http://www.allaboardtoys.com/madeline HttpClient
> used
>
> /madeline/?GCID=C1000x047&AID=10280172&PID=1453275, /madeline
Actually, it used
http://www.allaboardtoys.com/madeline/?GCID=C1000x047&AID=10280172&PID=1453275,
/madeline
RFC 2616 says that only the path should go into the request line, while
the host is sent as the value of the Host: header field.
> which is wrong.
And exactly what the server sent.
cheers,
Roland
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org