Open yogthos opened 8 years ago
It probably shouldn't throw an error, and I'd welcome a patch to fix it. But is it even legal for a HTTP response to have two content-types?
Oh yeah the particular example doesn't make sense, I just meant it as an illustration of the issue. I'll take a look at making a patch if I get a chance.
Isn't the issue specific to the content-type header?
Ah you know, I haven't actually tried other headers. I was going by the errata opened for my book issue 80401, and that was the header used there.
Since the stacktrace suggests it's trying to grab the charset from the content-type header, my guess is that this only affects the content-type.
You're absolutely right, the other headers aren't affected. So, I think the behavior is fine as I don't think the content-type header is repeating. I could do a pr to use a try/catch and throw a more descriptive error, if you think that would be useful.
I would propose adding the following code for content-type handling:
(defn- add-charset [resp charset]
(if-let [content-type (response/get-header resp "Content-Type")]
(try
(if (and (text-based-content-type? content-type)
(not (contains-charset? content-type)))
(response/charset resp charset)
resp)
(catch Exception _
(throw (IllegalArgumentException. (str "failed to parse the Content-Type header: " content-type)))))
resp))
It would make it clear what the problem was and show what the value of the content-type header was.
I don't think we should have a broad exception, and if we do catch it, we shouldn't throw it away, but add it as a cause. However, my preference, I think, would be to use the last charset defined instead of throwing an exception.
I think you're right, using the last defined charset would be the desired behavior.
Using a header such as
{"content-type" ["text/plain" "text/html"]}
in the response will result in the following exception:Not sure if this is the expected behavior as the spec states that header values can be seqs.