Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1a207db

Browse filesBrowse files
authored
An omnibus PR for changes needed to support webfunctions (#10563)
Web functions are currently supported with servlets. These changes add/move utility classes to core to better support direct usage of core APIs * increase usage of Charset in request * Added flush mechanism to BufferedContentSink
1 parent d2dff9a commit 1a207db
Copy full SHA for 1a207db

File tree

Expand file treeCollapse file tree

33 files changed

+1144
-984
lines changed
Filter options
Expand file treeCollapse file tree

33 files changed

+1144
-984
lines changed

‎jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java

Copy file name to clipboardExpand all lines: jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/MimeTypes.java
+55-13Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Objects;
2929
import java.util.Properties;
3030
import java.util.Set;
31+
import java.util.stream.Collectors;
3132

3233
import org.eclipse.jetty.util.FileID;
3334
import org.eclipse.jetty.util.Index;
@@ -300,9 +301,14 @@ public static Charset getKnownCharset(String charsetName) throws UnsupportedEnco
300301
}
301302
}
302303

304+
private static String nameOf(Charset charset)
305+
{
306+
return charset == null ? null : charset.name();
307+
}
308+
303309
protected final Map<String, String> _mimeMap = new HashMap<>();
304-
protected final Map<String, String> _inferredEncodings = new HashMap<>();
305-
protected final Map<String, String> _assumedEncodings = new HashMap<>();
310+
protected final Map<String, Charset> _inferredEncodings = new HashMap<>();
311+
protected final Map<String, Charset> _assumedEncodings = new HashMap<>();
306312

307313
public MimeTypes()
308314
{
@@ -314,11 +320,37 @@ public MimeTypes(MimeTypes defaults)
314320
if (defaults != null)
315321
{
316322
_mimeMap.putAll(defaults.getMimeMap());
317-
_assumedEncodings.putAll(defaults.getAssumedMap());
318-
_inferredEncodings.putAll(defaults.getInferredMap());
323+
_assumedEncodings.putAll(defaults._assumedEncodings);
324+
_inferredEncodings.putAll(defaults._inferredEncodings);
319325
}
320326
}
321327

328+
/**
329+
* Get the explicit, assumed, or inferred Charset for a mime type
330+
* @param mimeType String form or a mimeType
331+
* @return A {@link Charset} or null;
332+
*/
333+
public Charset getCharset(String mimeType)
334+
{
335+
if (mimeType == null)
336+
return null;
337+
338+
MimeTypes.Type mime = MimeTypes.CACHE.get(mimeType);
339+
if (mime != null && mime.getCharset() != null)
340+
return mime.getCharset();
341+
342+
String charsetName = MimeTypes.getCharsetFromContentType(mimeType);
343+
if (charsetName != null)
344+
return Charset.forName(charsetName);
345+
346+
Charset charset = getAssumedCharset(mimeType);
347+
if (charset != null)
348+
return charset;
349+
350+
charset = getInferredCharset(mimeType);
351+
return charset;
352+
}
353+
322354
/**
323355
* Get the MIME type by filename extension.
324356
*
@@ -337,29 +369,39 @@ public String getMimeForExtension(String extension)
337369
return _mimeMap.get(extension);
338370
}
339371

340-
public String getCharsetInferredFromContentType(String contentType)
372+
public Charset getInferredCharset(String contentType)
341373
{
342374
return _inferredEncodings.get(contentType);
343375
}
344376

345-
public String getCharsetAssumedFromContentType(String contentType)
377+
public Charset getAssumedCharset(String contentType)
346378
{
347379
return _assumedEncodings.get(contentType);
348380
}
349381

382+
public String getCharsetInferredFromContentType(String contentType)
383+
{
384+
return nameOf(_inferredEncodings.get(contentType));
385+
}
386+
387+
public String getCharsetAssumedFromContentType(String contentType)
388+
{
389+
return nameOf(_assumedEncodings.get(contentType));
390+
}
391+
350392
public Map<String, String> getMimeMap()
351393
{
352394
return Collections.unmodifiableMap(_mimeMap);
353395
}
354396

355397
public Map<String, String> getInferredMap()
356398
{
357-
return Collections.unmodifiableMap(_inferredEncodings);
399+
return _inferredEncodings.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().name()));
358400
}
359401

360402
public Map<String, String> getAssumedMap()
361403
{
362-
return Collections.unmodifiableMap(_assumedEncodings);
404+
return _assumedEncodings.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().name()));
363405
}
364406

365407
public static class Mutable extends MimeTypes
@@ -390,12 +432,12 @@ public String addMimeMapping(String extension, String type)
390432

391433
public String addInferred(String contentType, String encoding)
392434
{
393-
return _inferredEncodings.put(contentType, encoding);
435+
return nameOf(_inferredEncodings.put(contentType, Charset.forName(encoding)));
394436
}
395437

396438
public String addAssumed(String contentType, String encoding)
397439
{
398-
return _assumedEncodings.put(contentType, encoding);
440+
return nameOf(_assumedEncodings.put(contentType, Charset.forName(encoding)));
399441
}
400442
}
401443

@@ -479,7 +521,7 @@ public Map<String, String> getAssumedMap()
479521
for (Type type : Type.values())
480522
{
481523
if (type.isCharsetAssumed())
482-
_assumedEncodings.put(type.asString(), type.getCharsetString());
524+
_assumedEncodings.put(type.asString(), type.getCharset());
483525
}
484526

485527
String resourceName = "mime.properties";
@@ -548,9 +590,9 @@ else if (_mimeMap.size() < props.keySet().size())
548590
{
549591
String charset = props.getProperty(t);
550592
if (charset.startsWith("-"))
551-
_assumedEncodings.put(t, charset.substring(1));
593+
_assumedEncodings.put(t, Charset.forName(charset.substring(1)));
552594
else
553-
_inferredEncodings.put(t, props.getProperty(t));
595+
_inferredEncodings.put(t, Charset.forName(props.getProperty(t)));
554596
});
555597

556598
if (_inferredEncodings.isEmpty())

‎jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java

Copy file name to clipboardExpand all lines: jetty-core/jetty-http/src/test/java/org/eclipse/jetty/http/MimeTypesTest.java
+17-15Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package org.eclipse.jetty.http;
1515

16+
import java.nio.charset.StandardCharsets;
1617
import java.util.stream.Stream;
1718

1819
import org.junit.jupiter.api.Test;
@@ -21,6 +22,7 @@
2122
import org.junit.jupiter.params.provider.MethodSource;
2223

2324
import static org.hamcrest.MatcherAssert.assertThat;
25+
import static org.hamcrest.Matchers.equalToIgnoringCase;
2426
import static org.hamcrest.Matchers.is;
2527
import static org.junit.jupiter.api.Assertions.assertNull;
2628

@@ -159,15 +161,15 @@ public void testWrapper()
159161
assertThat(wrapper.getAssumedMap().size(), is(0));
160162

161163
wrapper.addMimeMapping("txt", "text/plain");
162-
wrapper.addInferred("text/plain", "usascii");
164+
wrapper.addInferred("text/plain", "us-ascii");
163165
wrapper.addAssumed("json", "utf-8");
164166

165167
assertThat(wrapper.getMimeMap().size(), is(1));
166168
assertThat(wrapper.getInferredMap().size(), is(1));
167169
assertThat(wrapper.getAssumedMap().size(), is(1));
168170
assertThat(wrapper.getMimeByExtension("fee.txt"), is("text/plain"));
169-
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), is("usascii"));
170-
assertThat(wrapper.getCharsetAssumedFromContentType("json"), is("utf-8"));
171+
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), equalToIgnoringCase("us-ascii"));
172+
assertThat(wrapper.getCharsetAssumedFromContentType("json"), equalToIgnoringCase("utf-8"));
171173

172174
MimeTypes.Mutable wrapped = new MimeTypes.Mutable(null);
173175
wrapper.setWrapped(wrapped);
@@ -176,23 +178,23 @@ public void testWrapper()
176178
assertThat(wrapper.getInferredMap().size(), is(1));
177179
assertThat(wrapper.getAssumedMap().size(), is(1));
178180
assertThat(wrapper.getMimeByExtension("fee.txt"), is("text/plain"));
179-
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), is("usascii"));
180-
assertThat(wrapper.getCharsetAssumedFromContentType("json"), is("utf-8"));
181+
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), equalToIgnoringCase("us-ascii"));
182+
assertThat(wrapper.getCharsetAssumedFromContentType("json"), equalToIgnoringCase("utf-8"));
181183

182-
wrapped.addMimeMapping("txt", "overridden");
183-
wrapped.addInferred("text/plain", "overridden");
184-
wrapped.addAssumed("json", "overridden");
184+
wrapped.addMimeMapping("txt", StandardCharsets.UTF_16.name());
185+
wrapped.addInferred("text/plain", StandardCharsets.UTF_16.name());
186+
wrapped.addAssumed("json", StandardCharsets.UTF_16.name());
185187

186188
assertThat(wrapper.getMimeMap().size(), is(1));
187189
assertThat(wrapper.getInferredMap().size(), is(1));
188190
assertThat(wrapper.getAssumedMap().size(), is(1));
189191
assertThat(wrapper.getMimeByExtension("fee.txt"), is("text/plain"));
190-
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), is("usascii"));
191-
assertThat(wrapper.getCharsetAssumedFromContentType("json"), is("utf-8"));
192+
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), equalToIgnoringCase("us-ascii"));
193+
assertThat(wrapper.getCharsetAssumedFromContentType("json"), equalToIgnoringCase("utf-8"));
192194

193195
wrapped.addMimeMapping("xml", "text/xml");
194196
wrapped.addInferred("text/xml", "iso-8859-1");
195-
wrapped.addAssumed("text/xxx", "assumed");
197+
wrapped.addAssumed("text/xxx", StandardCharsets.UTF_16.name());
196198
assertThat(wrapped.getMimeMap().size(), is(2));
197199
assertThat(wrapped.getInferredMap().size(), is(2));
198200
assertThat(wrapped.getAssumedMap().size(), is(2));
@@ -201,10 +203,10 @@ public void testWrapper()
201203
assertThat(wrapper.getInferredMap().size(), is(2));
202204
assertThat(wrapper.getAssumedMap().size(), is(2));
203205
assertThat(wrapper.getMimeByExtension("fee.txt"), is("text/plain"));
204-
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), is("usascii"));
205-
assertThat(wrapper.getCharsetAssumedFromContentType("json"), is("utf-8"));
206+
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), equalToIgnoringCase("us-ascii"));
207+
assertThat(wrapper.getCharsetAssumedFromContentType("json"), equalToIgnoringCase("utf-8"));
206208
assertThat(wrapper.getMimeByExtension("fee.xml"), is("text/xml"));
207-
assertThat(wrapper.getCharsetInferredFromContentType("text/xml"), is("iso-8859-1"));
208-
assertThat(wrapper.getCharsetAssumedFromContentType("text/xxx"), is("assumed"));
209+
assertThat(wrapper.getCharsetInferredFromContentType("text/xml"), equalToIgnoringCase("iso-8859-1"));
210+
assertThat(wrapper.getCharsetAssumedFromContentType("text/xxx"), equalToIgnoringCase("utf-16"));
209211
}
210212
}

‎jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAggregator.java

Copy file name to clipboardExpand all lines: jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/ByteBufferAggregator.java
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ public ByteBufferAggregator(ByteBufferPool bufferPool, boolean direct, int start
5858
_currentSize = startSize;
5959
}
6060

61+
/**
62+
* Get the currently aggregated length.
63+
* @return The current total aggregated bytes.
64+
*/
65+
public int length()
66+
{
67+
return _aggregatedSize;
68+
}
69+
6170
/**
6271
* Aggregates the given ByteBuffer. This copies bytes up to the specified maximum size, at which
6372
* time this method returns {@code true} and {@link #takeRetainableByteBuffer()} must be called

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.