Permalink
Browse files

Character encoding set only when charset in content-type header

  • Loading branch information...
1 parent bd8a0b3 commit 2b3f560e9d9c15a8da0ec626f0f10bf9c5014ff4 @weavejester weavejester committed Jul 15, 2011
Showing with 8 additions and 6 deletions.
  1. +7 −5 ring-servlet/src/ring/util/servlet.clj
  2. +1 −1 ring-servlet/test/ring/util/servlet_test.clj
View
12 ring-servlet/src/ring/util/servlet.clj
@@ -68,12 +68,14 @@
first
string/trim)))
+(defn- get-charset [content-type]
+ (->> content-type
+ (re-find #"charset=(.+);?")
+ (second)))
+
(defn- set-character-encoding [response content-type]
- (.setCharacterEncoding
- response
- (-> (re-find #"charset=(.+);?" content-type)
- second
- (or "utf-8"))))
+ (when-let [charset (get-charset content-type)]
+ (.setCharacterEncoding response charset)))
(defn set-headers
"Update a HttpServletResponse with a map of headers."
View
2 ring-servlet/test/ring/util/servlet_test.clj
@@ -88,7 +88,7 @@
(run-servlet handler request response)
(is (= (@response :status) 200))
(is (= (@response :content-type) "text/plain"))
- (is (= (@response :character-encoding) "utf-8"))
+ (is (= (@response :character-encoding) "UTF-8"))
(is (= (get-in @response [:headers "X-Server"]) "Bar"))))
(testing "response with character encoding"
(letfn [(handler [r]

0 comments on commit 2b3f560

Please sign in to comment.