strugglng to understand this

156 views
Skip to first unread message

djh

unread,
Apr 30, 2011, 4:10:43 PM4/30/11
to spray-user
I'm not sure if this is a Spray framework issue, or a Scala issue but
I'm hoping someone can shed some light on this

I've built a small web service that basically just spits out JSON
responses when you call them on the following web address http://localhost:8080/*

Now I can successfully call my service using the standard UNIX command
"curl" and get a response fine.

But if I try and use the Scala IO api: -

io.Source.fromURL("http://localhost:8080/request/current")

I just get the following exception

java.io.IOException: Server returned HTTP response code: 400 for URL:
http://localhost:8080/request/current
at
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:
1436)
at java.net.URL.openStream(URL.java:1010)
at scala.io.Source$.fromURL(Source.scala:133)
at .<init>(<console>:9)

Any help/ideas why this is happening woud definitely be appreciated!

djh

unread,
Apr 30, 2011, 4:53:24 PM4/30/11
to spray-user
I've just checked my akka.log and this is the exception being thrown

04/30 21:52:25 ERROR[akka:event-driven:dispatcher:global-7]
c.s.RootService - Exception during request processing: {}
cc.spray.http.HttpException: Illegal HTTP header 'Accept':
Invalid input ';', expected '/' or CTL (line 1, pos 36):
text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

at cc.spray.http.HttpHeader$.apply(HttpHeader.scala:58)
~[spray_2.8.1-0.5.0.jar:na]
at cc.spray.ServletConverter$$anonfun$buildHeaders$1$$anonfun$apply
$1.apply(ServletConverter.scala:55) ~[spray_2.8.1-0.5.0.jar:na]
at cc.spray.ServletConverter$$anonfun$buildHeaders$1$$anonfun$apply
$1.apply(ServletConverter.scala:53) ~[spray_2.8.1-0.5.0.jar:na]

On Apr 30, 9:10 pm, djh <djharpe...@gmail.com> wrote:
> I'm not sure if this is a Spray framework issue, or a Scala issue but
> I'm hoping someone can shed some light on this
>
> I've built a small web service that basically just spits out JSON
> responses when you call them on the following web addresshttp://localhost:8080/*
>
> Now I can successfully call my service using the standard UNIX command
> "curl" and get a response fine.
>
> But if I try and use the Scala IO api: -
>
> io.Source.fromURL("http://localhost:8080/request/current")
>
> I just get the following exception
>
> java.io.IOException: Server returned HTTP response code: 400 for URL:http://localhost:8080/request/current
>         at
> sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection....

Mathias

unread,
May 2, 2011, 3:23:46 AM5/2/11
to spray...@googlegroups.com
Hmm....
The reason that you are seeing this error is that the content of the Accept header is malformed.
The HTTP specs (1.1 and 1.0) clearly define the grammar for the accept header content:
(http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.8 and http://ftp.ics.uci.edu/pub/ietf/http/rfc1945.html#Accept resp.).

The offending part is the "*; q=.2" entry. A single star is not allowed according to my interpretation of the spec. It should be "*/*" (as the final entry of the Accept header shows it). However, I'm sure there is a reason why the developers of scala.io.Source put in that entry, I just don't see it yet.

Anybody have any idea, why spray should also allow this actually malformed variant?

Cheers,
Mathias

---
mat...@spray.cc
http://www.spray.cc

djh

unread,
May 2, 2011, 3:18:33 PM5/2/11
to spray-user
I see.

I've tried calling the URL using the standard java HTTPURLConnection
library too and I get the same exception

Making a request using the apache HttpClient library works OK and
making requests from other language libraries such as the standard one
you get in Ruby works fine too.

It just appears that the standard java libraries have this issue (and
the io.Source library too)

On May 2, 8:23 am, Mathias <math...@spray.cc> wrote:
> Hmm....
> The reason that you are seeing this error is that the content of the Accept header is malformed.
> The HTTP specs (1.1 and 1.0) clearly define the grammar for the accept header content:
> (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.8andhttp://ftp.ics.uci.edu/pub/ietf/http/rfc1945.html#Acceptresp.).
>
> The offending part is the "*; q=.2" entry. A single star is not allowed according to my interpretation of the spec. It should be "*/*" (as the final entry of the Accept header shows it). However, I'm sure there is a reason why the developers of scala.io.Source put in that entry, I just don't see it yet.
>
> Anybody have any idea, why spray should also allow this actually malformed variant?
>
> Cheers,
> Mathias
>
> ---
> math...@spray.cchttp://www.spray.cc

Mathias

unread,
May 3, 2011, 3:46:33 AM5/3/11
to spray...@googlegroups.com
We have looked into this a bit further.

The culprit is the Sun-specific implementation of the abstract java.net.HttpURLConnection class (sun.net.www.protocol.http.HttpURLConnection). It defines the offending content of the Accept header as a static field. All code using java.net.HttpURLConnection on a Sun JVM (directly or indirectly, like scala.io.Source) has this problem.

We still don't know, why Sun put in that particular irregular header, but actually it doesn't really matter.
Since we definitely want to allow users to talk to spray servers using the standard Java HTTP client we will relax the Accept header constrains to allow the single star notation as well. I just added a respective issue (https://github.com/spray/spray/issues/12)

Cheers,
Mathias

---
mat...@spray.cc
http://www.spray.cc

djh

unread,
May 3, 2011, 4:42:59 AM5/3/11
to spray-user
Hey thanks for the informative reply, will keep an eye on the issue in
github.

Cheers

On May 3, 8:46 am, Mathias <math...@spray.cc> wrote:
> We have looked into this a bit further.
>
> The culprit is the Sun-specific implementation of the abstract java.net.HttpURLConnection class (sun.net.www.protocol.http.HttpURLConnection). It defines the offending content of the Accept header as a static field. All code using java.net.HttpURLConnection on a Sun JVM (directly or indirectly, like scala.io.Source) has this problem.
>
> We still don't know, why Sun put in that particular irregular header, but actually it doesn't really matter.
> Since we definitely want to allow users to talk to spray servers using the standard Java HTTP client we will relax the Accept header constrains to allow the single star notation as well. I just added a respective issue (https://github.com/spray/spray/issues/12)
>
> Cheers,
> Mathias
>
> ---
> math...@spray.cchttp://www.spray.cc
>
> On 02.05.2011, at 21:18, djh wrote:
>
>
>
>
>
>
>
> > I see.
>
> > I've tried calling the URL using the standard java HTTPURLConnection
> > library too and I get the same exception
>
> > Making a request using the apache HttpClient library works OK and
> > making requests from other language libraries such as the standard one
> > you get in Ruby works fine too.
>
> > It just appears that the standard java libraries have this issue (and
> > the io.Source library too)
>
> > On May 2, 8:23 am, Mathias <math...@spray.cc> wrote:
> >> Hmm....
> >> The reason that you are seeing this error is that the content of the Accept header is malformed.
> >> The HTTP specs (1.1 and 1.0) clearly define the grammar for the accept header content:
> >> (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.8andhttp....).
Reply all
Reply to author
Forward
0 new messages