Skip to content

Commit 3f5e16c

Browse files
authored
Merge pull request #591 from arnaudgeiser/566
Multipart: Fix filename Fixes #566
2 parents 985d535 + 059355e commit 3f5e16c

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed

src/aleph/http/multipart.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
(.addBodyFileUpload encoder
140140
(or part-name name)
141141
;; Netty's multipart encoder ignores empty strings here
142-
(or name "")
142+
(or filename "")
143143
content
144144
content-type
145145
false))

test/aleph/http/multipart_test.clj

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
(ns aleph.http.multipart-test
2-
(:use
3-
[clojure test])
42
(:require
53
[aleph.http :as http]
4+
[aleph.http.core :as core]
65
[aleph.http.multipart :as mp]
76
[byte-streams :as bs]
7+
[clojure.edn :as edn]
8+
[clojure.test :refer [deftest testing is]]
89
[manifold.deferred :as d]
910
[manifold.stream :as s]
10-
[clojure.string :as str]
11-
[clojure.edn :as edn])
11+
[clojure.string :as str])
1212
(:import
13+
[io.netty.buffer
14+
ByteBufAllocator]
15+
[io.netty.handler.codec.http
16+
HttpContent]
17+
[io.netty.handler.stream
18+
ChunkedInput]
1319
[java.io
1420
File]))
1521

@@ -84,37 +90,47 @@
8490

8591
(deftest reject-unknown-transfer-encoding
8692
(is (thrown? IllegalArgumentException
87-
(mp/encode-body [{:part-name "part1"
88-
:content "content1"
89-
:transfer-encoding :uknown-transfer-encoding}]))))
93+
(mp/encode-body [{:part-name "part1"
94+
:content "content1"
95+
:transfer-encoding :uknown-transfer-encoding}]))))
9096

9197
(deftest test-content-as-file
92-
(let [body (mp/encode-body [{:part-name "part1"
93-
:content file-to-send}
94-
{:part-name "part2"
95-
:mime-type "application/png"
96-
:content file-to-send}
97-
{:part-name "part3"
98-
:name "text-file-to-send.txt"
99-
:content file-to-send}
100-
{:part-name "part4"
101-
:charset "UTF-8"
102-
:content file-to-send}
103-
{:content file-to-send}
104-
{:content file-to-send
105-
:transfer-encoding :base64}])
106-
body-str (bs/to-string body)]
107-
(is (.contains body-str "name=\"part1\""))
108-
(is (.contains body-str "name=\"part2\""))
109-
(is (.contains body-str "name=\"part3\""))
110-
(is (.contains body-str "name=\"part4\""))
111-
(is (.contains body-str "name=\"file.txt\""))
112-
(is (.contains body-str "filename=\"file.txt\""))
113-
(is (.contains body-str "filename=\"text-file-to-send.txt\""))
114-
(is (.contains body-str "Content-Type: text/plain\r\n"))
115-
(is (.contains body-str "Content-Type: text/plain; charset=UTF-8\r\n"))
116-
(is (.contains body-str "Content-Type: application/png\r\n"))
117-
(is (.contains body-str "Content-Transfer-Encoding: base64\r\n"))))
98+
(let [parts [{:part-name "part1"
99+
:content file-to-send}
100+
{:part-name "part2"
101+
:mime-type "application/png"
102+
:content file-to-send}
103+
{:part-name "part3"
104+
:name "text-file-to-send.txt"
105+
:content file-to-send}
106+
{:part-name "part4"
107+
:charset "UTF-8"
108+
:content file-to-send}
109+
{:content file-to-send}
110+
{:content file-to-send
111+
:transfer-encoding :base64}]
112+
validate (fn [^String body-str]
113+
(is (.contains body-str "name=\"part1\""))
114+
(is (.contains body-str "name=\"part2\""))
115+
(is (.contains body-str "name=\"part3\""))
116+
(is (.contains body-str "name=\"part4\""))
117+
(is (.contains body-str "name=\"file.txt\""))
118+
(is (.contains body-str "filename=\"file.txt\""))
119+
(is (.contains body-str "filename=\"file.txt\""))
120+
(is (.contains (str/lower-case body-str) (str/lower-case "Content-Type: text/plain\r\n")))
121+
(is (.contains (str/lower-case body-str) (str/lower-case "Content-Type: text/plain; charset=UTF-8\r\n")))
122+
(is (.contains (str/lower-case body-str) (str/lower-case "Content-Type: application/png\r\n"))))]
123+
(testing "legacy encode-body"
124+
(let [body (mp/encode-body parts)
125+
body-str (bs/to-string body)]
126+
(validate body-str)
127+
(is (.contains body-str "Content-Transfer-Encoding: base64\r\n"))))
128+
(testing "encode-request"
129+
(let [req (core/ring-request->netty-request {:request-method :get})
130+
[_ body] (mp/encode-request req parts)
131+
body-str (-> ^ChunkedInput body ^HttpContent (.readChunk ByteBufAllocator/DEFAULT) .content bs/to-string)]
132+
(validate body-str)
133+
(is (.contains body-str "content-transfer-encoding: binary\r\n"))))))
118134

119135
(def port1 26023)
120136
(def port2 26024)
@@ -165,7 +181,7 @@
165181
(is (.contains resp "; charset=ISO-8859-1"))
166182

167183
;; explicit filename
168-
(is (.contains resp "filename=\"text-file-to-send.txt\""))
184+
(is (.contains resp "filename=\"file.txt\""))
169185

170186
(.close ^java.io.Closeable s)))
171187

@@ -205,7 +221,7 @@
205221

206222
;; filename
207223
(is (= "file.txt" (get-in chunks [3 :name])))
208-
(is (= "text-file-to-send.txt" (get-in chunks [4 :name])))
224+
(is (= "file.txt" (get-in chunks [4 :name])))
209225

210226
;; charset
211227
(is (= "ISO-8859-1" (get-in chunks [5 :charset])))

0 commit comments

Comments
 (0)