镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

the x-headers middleware appears to be incompatible with seq header values #5

Open

Description

@yogthos

Using a header such as {"content-type" ["text/plain" "text/html"]} in the response will result in the following exception:

[clojure.core$re_matcher invokeStatic core.clj 4667]
  [clojure.core$re_find invokeStatic core.clj 4716]
  [clojure.core$re_find invoke core.clj 4716]
  [ring.middleware.default_charset$text_based_content_type_QMARK_ invokeStatic default_charset.clj 7]
  [ring.middleware.default_charset$text_based_content_type_QMARK_ invoke default_charset.clj 6]
  [ring.middleware.default_charset$add_charset invokeStatic default_charset.clj 15]
  [ring.middleware.default_charset$add_charset invoke default_charset.clj 13]
  [ring.middleware.default_charset$wrap_default_charset$fn__12380 invoke default_charset.clj 27]
  [ring.middleware.not_modified$wrap_not_modified$fn__12337 invoke not_modified.clj 52]
  [ring.middleware.x_headers$wrap_xss_protection$fn__11091 invoke x_headers.clj 71]
  [ring.middleware.x_headers$wrap_frame_options$fn__11079 invoke x_headers.clj 38]
  [ring.middleware.x_headers$wrap_content_type_options$fn__11085 invoke x_headers.clj 53]

Not sure if this is the expected behavior as the spec states that header values can be seqs.

Activity

  1. weavejester commented on Jun 11, 2016

    @weavejester
    Member

    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?

  2. yogthos commented on Jun 11, 2016

    @yogthos
    Author

    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.

  3. weavejester commented on Jun 11, 2016

    @weavejester
    Member

    Isn't the issue specific to the content-type header?

  4. yogthos commented on Jun 12, 2016

    @yogthos
    Author

    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.

  5. weavejester commented on Jun 12, 2016

    @weavejester
    Member

    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.

  6. yogthos commented on Jun 12, 2016

    @yogthos
    Author

    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.

  7. yogthos commented on Jun 12, 2016

    @yogthos
    Author

    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.

  8. weavejester commented on Jun 12, 2016

    @weavejester
    Member

    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.

  9. yogthos commented on Jun 12, 2016

    @yogthos
    Author

    I think you're right, using the last defined charset would be the desired behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions