Skip to content

Commit 708d96e

Browse files
SentryManrobaho
andauthored
Get Content Type From Multipart (robaho#30)
* get content type from multipart * fix PR. add test cases --------- Co-authored-by: robert engels <robaho@users.noreply.github.com>
1 parent 575b790 commit 708d96e

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ fileserver/
88
gradlew.bat
99
gradle/
1010
gradle/
11+
/target/
12+
.classpath
13+
.factorypath
14+
*.prefs
15+
.project

src/main/java/robaho/net/httpserver/extras/MultipartFormParser.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public record Part(String contentType, String filename, String data, File file)
3838

3939
}
4040

41-
private record PartMetadata(String name, String filename) {
41+
private record PartMetadata(String contentType, String name, String filename) {
4242

4343
}
4444

@@ -120,12 +120,12 @@ public static Map<String, List<Part>> parse(String encoding, String content_type
120120
if (meta.filename == null) {
121121
var bos = new ByteArrayOutputStream();
122122
os = bos;
123-
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(null, null, bos.toString(charset), null));
123+
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(meta.contentType, null, bos.toString(charset), null));
124124
} else {
125125
File file = Path.of(storage.toString(), meta.filename).toFile();
126126
file.deleteOnExit();
127127
os = new NoSyncBufferedOutputStream(new FileOutputStream(file));
128-
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(null, meta.filename, null, file));
128+
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(meta.contentType, meta.filename, null, file));
129129
}
130130

131131
try (os) {
@@ -170,6 +170,7 @@ public static Map<String, List<Part>> parse(String encoding, String content_type
170170
private static PartMetadata parseHeaders(List<String> headers) {
171171
String name = null;
172172
String filename = null;
173+
String contentType = null;
173174
for (var header : headers) {
174175
String[] parts = header.split(":", 2);
175176
if ("content-disposition".equalsIgnoreCase(parts[0])) {
@@ -188,9 +189,11 @@ private static PartMetadata parseHeaders(List<String> headers) {
188189
}
189190
}
190191

192+
} else if ("content-type".equalsIgnoreCase(parts[0])) {
193+
contentType = parts[1].trim();
191194
}
192195
}
193-
return new PartMetadata(name, filename);
196+
return new PartMetadata(contentType, name, filename);
194197
}
195198

196199
private static String readLine(Charset charset, InputStream is) throws IOException {

src/test/java/robaho/net/httpserver/extras/MultipartFormParserTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ public void testFiles() throws UnsupportedEncodingException, IOException {
5252
s += "111Y\r\n";
5353
s += "111Z\rCCCC\nCCCC\r\nCCCCC@\r\n";
5454

55+
Assert.assertEquals(values.get(0).contentType(),"text/plain");
5556
Assert.assertEquals(s.getBytes("UTF-8"), Files.readAllBytes((values.get(0).file()).toPath()), "file1 failed");
5657

58+
5759
s = "\r\n";
5860
s += "@22X";
5961
s += "222Y\r\n";
6062
s += "222Z\r222W\n2220\r\n666@";
6163

64+
Assert.assertEquals(values.get(1).contentType(),"text/plain");
6265
Assert.assertEquals(s.getBytes("UTF-8"), Files.readAllBytes((values.get(1).file()).toPath()), "file2 failed");
6366
}
6467

@@ -118,6 +121,7 @@ public void testFormSample() throws IOException {
118121

119122
Assert.assertEquals(results.size(), 1);
120123
List<Part> values = results.get("myfile");
124+
Assert.assertEquals(values.get(0).contentType(), "text/plain");
121125
Assert.assertEquals(values.size(), 1);
122126
}
123127

@@ -133,6 +137,12 @@ public void testMultiFileFormSample() throws IOException {
133137

134138
Assert.assertEquals(results.size(), 2);
135139
List<Part> values = results.get("myfile");
140+
Assert.assertEquals(values.get(0).contentType(), "text/plain");
141+
Assert.assertEquals(values.size(), 1);
142+
143+
values = results.get("myfile2");
144+
Assert.assertEquals(values.get(0).contentType(), "image/png");
136145
Assert.assertEquals(values.size(), 1);
146+
137147
}
138148
}

0 commit comments

Comments
 (0)