Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An omnibus PR for changes needed to support webfunctions #10563

Merged
merged 17 commits into from
Sep 26, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.jetty.util.FileID;
import org.eclipse.jetty.util.Index;
Expand Down Expand Up @@ -300,9 +301,14 @@ public static Charset getKnownCharset(String charsetName) throws UnsupportedEnco
}
}

private static String nameOf(Charset charset)
{
return charset == null ? null : charset.name();
}

protected final Map<String, String> _mimeMap = new HashMap<>();
protected final Map<String, String> _inferredEncodings = new HashMap<>();
protected final Map<String, String> _assumedEncodings = new HashMap<>();
protected final Map<String, Charset> _inferredEncodings = new HashMap<>();
protected final Map<String, Charset> _assumedEncodings = new HashMap<>();

public MimeTypes()
{
Expand All @@ -314,11 +320,37 @@ public MimeTypes(MimeTypes defaults)
if (defaults != null)
{
_mimeMap.putAll(defaults.getMimeMap());
_assumedEncodings.putAll(defaults.getAssumedMap());
_inferredEncodings.putAll(defaults.getInferredMap());
_assumedEncodings.putAll(defaults._assumedEncodings);
_inferredEncodings.putAll(defaults._inferredEncodings);
}
}

/**
* Get the explicit, assumed, or inferred Charset for a mime type
* @param mimeType String form or a mimeType
* @return A {@link Charset} or null;
*/
public Charset getCharset(String mimeType)
{
if (mimeType == null)
return null;

MimeTypes.Type mime = MimeTypes.CACHE.get(mimeType);
if (mime != null && mime.getCharset() != null)
return mime.getCharset();

String charsetName = MimeTypes.getCharsetFromContentType(mimeType);
if (charsetName != null)
return Charset.forName(charsetName);

Charset charset = getAssumedCharset(mimeType);
if (charset != null)
return charset;

charset = getInferredCharset(mimeType);
return charset;
}

/**
* Get the MIME type by filename extension.
*
Expand All @@ -337,29 +369,39 @@ public String getMimeForExtension(String extension)
return _mimeMap.get(extension);
}

public String getCharsetInferredFromContentType(String contentType)
public Charset getInferredCharset(String contentType)
{
return _inferredEncodings.get(contentType);
}

public String getCharsetAssumedFromContentType(String contentType)
public Charset getAssumedCharset(String contentType)
{
return _assumedEncodings.get(contentType);
}

public String getCharsetInferredFromContentType(String contentType)
{
return nameOf(_inferredEncodings.get(contentType));
}

public String getCharsetAssumedFromContentType(String contentType)
{
return nameOf(_assumedEncodings.get(contentType));
}

public Map<String, String> getMimeMap()
{
return Collections.unmodifiableMap(_mimeMap);
}

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

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

public static class Mutable extends MimeTypes
Expand Down Expand Up @@ -390,12 +432,12 @@ public String addMimeMapping(String extension, String type)

public String addInferred(String contentType, String encoding)
{
return _inferredEncodings.put(contentType, encoding);
return nameOf(_inferredEncodings.put(contentType, Charset.forName(encoding)));
}

public String addAssumed(String contentType, String encoding)
{
return _assumedEncodings.put(contentType, encoding);
return nameOf(_assumedEncodings.put(contentType, Charset.forName(encoding)));
}
}

Expand Down Expand Up @@ -479,7 +521,7 @@ public Map<String, String> getAssumedMap()
for (Type type : Type.values())
{
if (type.isCharsetAssumed())
_assumedEncodings.put(type.asString(), type.getCharsetString());
_assumedEncodings.put(type.asString(), type.getCharset());
}

String resourceName = "mime.properties";
Expand Down Expand Up @@ -548,9 +590,9 @@ else if (_mimeMap.size() < props.keySet().size())
{
String charset = props.getProperty(t);
if (charset.startsWith("-"))
_assumedEncodings.put(t, charset.substring(1));
_assumedEncodings.put(t, Charset.forName(charset.substring(1)));
else
_inferredEncodings.put(t, props.getProperty(t));
_inferredEncodings.put(t, Charset.forName(props.getProperty(t)));
});

if (_inferredEncodings.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.jetty.http;

import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
Expand All @@ -21,6 +22,7 @@
import org.junit.jupiter.params.provider.MethodSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNull;

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

wrapper.addMimeMapping("txt", "text/plain");
wrapper.addInferred("text/plain", "usascii");
wrapper.addInferred("text/plain", "us-ascii");
wrapper.addAssumed("json", "utf-8");

assertThat(wrapper.getMimeMap().size(), is(1));
assertThat(wrapper.getInferredMap().size(), is(1));
assertThat(wrapper.getAssumedMap().size(), is(1));
assertThat(wrapper.getMimeByExtension("fee.txt"), is("text/plain"));
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), is("usascii"));
assertThat(wrapper.getCharsetAssumedFromContentType("json"), is("utf-8"));
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), equalToIgnoringCase("us-ascii"));
assertThat(wrapper.getCharsetAssumedFromContentType("json"), equalToIgnoringCase("utf-8"));

MimeTypes.Mutable wrapped = new MimeTypes.Mutable(null);
wrapper.setWrapped(wrapped);
Expand All @@ -176,23 +178,23 @@ public void testWrapper()
assertThat(wrapper.getInferredMap().size(), is(1));
assertThat(wrapper.getAssumedMap().size(), is(1));
assertThat(wrapper.getMimeByExtension("fee.txt"), is("text/plain"));
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), is("usascii"));
assertThat(wrapper.getCharsetAssumedFromContentType("json"), is("utf-8"));
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), equalToIgnoringCase("us-ascii"));
assertThat(wrapper.getCharsetAssumedFromContentType("json"), equalToIgnoringCase("utf-8"));

wrapped.addMimeMapping("txt", "overridden");
wrapped.addInferred("text/plain", "overridden");
wrapped.addAssumed("json", "overridden");
wrapped.addMimeMapping("txt", StandardCharsets.UTF_16.name());
wrapped.addInferred("text/plain", StandardCharsets.UTF_16.name());
wrapped.addAssumed("json", StandardCharsets.UTF_16.name());

assertThat(wrapper.getMimeMap().size(), is(1));
assertThat(wrapper.getInferredMap().size(), is(1));
assertThat(wrapper.getAssumedMap().size(), is(1));
assertThat(wrapper.getMimeByExtension("fee.txt"), is("text/plain"));
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), is("usascii"));
assertThat(wrapper.getCharsetAssumedFromContentType("json"), is("utf-8"));
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), equalToIgnoringCase("us-ascii"));
assertThat(wrapper.getCharsetAssumedFromContentType("json"), equalToIgnoringCase("utf-8"));

wrapped.addMimeMapping("xml", "text/xml");
wrapped.addInferred("text/xml", "iso-8859-1");
wrapped.addAssumed("text/xxx", "assumed");
wrapped.addAssumed("text/xxx", StandardCharsets.UTF_16.name());
assertThat(wrapped.getMimeMap().size(), is(2));
assertThat(wrapped.getInferredMap().size(), is(2));
assertThat(wrapped.getAssumedMap().size(), is(2));
Expand All @@ -201,10 +203,10 @@ public void testWrapper()
assertThat(wrapper.getInferredMap().size(), is(2));
assertThat(wrapper.getAssumedMap().size(), is(2));
assertThat(wrapper.getMimeByExtension("fee.txt"), is("text/plain"));
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), is("usascii"));
assertThat(wrapper.getCharsetAssumedFromContentType("json"), is("utf-8"));
assertThat(wrapper.getCharsetInferredFromContentType("text/plain"), equalToIgnoringCase("us-ascii"));
assertThat(wrapper.getCharsetAssumedFromContentType("json"), equalToIgnoringCase("utf-8"));
assertThat(wrapper.getMimeByExtension("fee.xml"), is("text/xml"));
assertThat(wrapper.getCharsetInferredFromContentType("text/xml"), is("iso-8859-1"));
assertThat(wrapper.getCharsetAssumedFromContentType("text/xxx"), is("assumed"));
assertThat(wrapper.getCharsetInferredFromContentType("text/xml"), equalToIgnoringCase("iso-8859-1"));
assertThat(wrapper.getCharsetAssumedFromContentType("text/xxx"), equalToIgnoringCase("utf-16"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public ByteBufferAggregator(ByteBufferPool bufferPool, boolean direct, int start
_currentSize = startSize;
}

/**
* Get the currently aggregated length.
* @return The current total aggregated bytes.
*/
public int length()
{
return _aggregatedSize;
}

/**
* Aggregates the given ByteBuffer. This copies bytes up to the specified maximum size, at which
* time this method returns {@code true} and {@link #takeRetainableByteBuffer()} must be called
Expand Down
Loading