Skip to content

Commit 5417e87

Browse files
committed
chore(implementation)!: use Jetty-12 core without servlets
Port the invoker to upgrade to Eclipse Jetty-12 version 12. Specifically using the new core APIs of Eclipse Jetty-12 that allow the overhead of a Servlet container to be avoided. Several other minor optimizations are included. BREAKING CHANGE: use Java 17 or above, as required by Eclipse Jetty-12.
1 parent e8e2d4d commit 5417e87

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

invoker/core/src/main/java/com/google/cloud/functions/invoker/http/HttpRequestImpl.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@
2222
import java.nio.charset.Charset;
2323
import java.nio.charset.StandardCharsets;
2424
import java.util.ArrayList;
25-
import java.util.Collection;
2625
import java.util.Collections;
2726
import java.util.HashMap;
2827
import java.util.List;
2928
import java.util.Map;
29+
import java.util.Objects;
3030
import java.util.Optional;
3131
import java.util.TreeMap;
3232
import java.util.concurrent.ExecutionException;
33-
import java.util.regex.Matcher;
34-
import java.util.regex.Pattern;
3533
import org.eclipse.jetty.http.HttpField;
3634
import org.eclipse.jetty.http.HttpFields;
3735
import org.eclipse.jetty.http.HttpHeader;
36+
import org.eclipse.jetty.http.MimeTypes;
3837
import org.eclipse.jetty.http.MultiPart;
3938
import org.eclipse.jetty.http.MultiPart.Part;
4039
import org.eclipse.jetty.http.MultiPartFormData;
@@ -149,7 +148,7 @@ public BufferedReader getReader() throws IOException {
149148
}
150149
inputStream = Content.Source.asInputStream(request);
151150
reader = new BufferedReader(new InputStreamReader(getInputStream(),
152-
getCharacterEncoding().orElse(StandardCharsets.UTF_8.name())));
151+
Objects.requireNonNullElse(Request.getCharset(request), StandardCharsets.UTF_8)));
153152
}
154153
return reader;
155154
}
@@ -169,9 +168,11 @@ static Map<String, List<String>> toStringListMap(HttpFields headers) {
169168

170169
private static class HttpPartImpl implements HttpPart {
171170
private final Part part;
171+
private final String contentType;
172172

173173
private HttpPartImpl(Part part) {
174174
this.part = part;
175+
contentType = part.getHeaders().get(HttpHeader.CONTENT_TYPE);
175176
}
176177

177178
public String getName() {
@@ -185,7 +186,7 @@ public Optional<String> getFileName() {
185186

186187
@Override
187188
public Optional<String> getContentType() {
188-
return Optional.ofNullable(part.getHeaders().get(HttpHeader.CONTENT_TYPE));
189+
return Optional.ofNullable(contentType);
189190
}
190191

191192
@Override
@@ -195,13 +196,7 @@ public long getContentLength() {
195196

196197
@Override
197198
public Optional<String> getCharacterEncoding() {
198-
String contentType = getContentType().orElse(null);
199-
if (contentType == null) {
200-
return Optional.empty();
201-
}
202-
Pattern charsetPattern = Pattern.compile("(?i).*;\\s*charset\\s*=([^;\\s]*)\\s*(;|$)");
203-
Matcher matcher = charsetPattern.matcher(contentType);
204-
return matcher.matches() ? Optional.of(matcher.group(1)) : Optional.empty();
199+
return Optional.ofNullable(MimeTypes.getCharsetFromContentType(contentType));
205200
}
206201

207202
@Override
@@ -211,19 +206,17 @@ public InputStream getInputStream() throws IOException {
211206

212207
@Override
213208
public BufferedReader getReader() throws IOException {
214-
String encoding = getCharacterEncoding().orElse("utf-8");
215-
return new BufferedReader(new InputStreamReader(getInputStream(), encoding));
209+
return new BufferedReader(
210+
new InputStreamReader(getInputStream(),
211+
Objects.requireNonNullElse(MimeTypes.DEFAULTS.getCharset(contentType),
212+
StandardCharsets.UTF_8)));
216213
}
217214

218215
@Override
219216
public Map<String, List<String>> getHeaders() {
220217
return HttpRequestImpl.toStringListMap(part.getHeaders());
221218
}
222219

223-
private static <T> List<T> list(Collection<T> collection) {
224-
return (collection instanceof List<?>) ? (List<T>) collection : new ArrayList<>(collection);
225-
}
226-
227220
@Override
228221
public String toString() {
229222
return "%s{%s}".formatted(super.toString(), part);

invoker/core/src/main/java/com/google/cloud/functions/invoker/http/HttpResponseImpl.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class HttpResponseImpl implements HttpResponse {
3434
private final Response response;
3535
private OutputStream outputStream;
3636
private BufferedWriter writer;
37+
private Charset charset;
3738

3839
public HttpResponseImpl(Response response) {
3940
this.response = response;
@@ -53,6 +54,7 @@ public void setStatusCode(int code, String message) {
5354
@Override
5455
public void setContentType(String contentType) {
5556
response.getHeaders().put(HttpHeader.CONTENT_TYPE, contentType);
57+
charset = response.getRequest().getContext().getMimeTypes().getCharset(contentType);
5658
}
5759

5860
@Override
@@ -62,7 +64,11 @@ public Optional<String> getContentType() {
6264

6365
@Override
6466
public void appendHeader(String key, String value) {
65-
response.getHeaders().add(key, value);
67+
if (HttpHeader.CONTENT_TYPE.is(key)) {
68+
setContentType(value);
69+
} else {
70+
response.getHeaders().add(key, value);
71+
}
6672
}
6773

6874
@Override
@@ -87,13 +93,10 @@ public synchronized BufferedWriter getWriter() throws IOException {
8793
if (outputStream != null) {
8894
throw new IllegalStateException("getOutputStream called");
8995
}
90-
String contentType = getContentType().orElse(null);
91-
Charset charset = Objects.requireNonNullElse(
92-
response.getRequest().getContext().getMimeTypes().getCharset(contentType),
93-
StandardCharsets.UTF_8);
94-
// TODO should we buffer in the input stream rather than as characters
9596
outputStream = Content.Sink.asOutputStream(response);
96-
writer = new BufferedWriter(WriteThroughWriter.newWriter(getOutputStream(), charset));
97+
// TODO extend close in such a way as to make the buffer flush the last write.
98+
writer = new BufferedWriter(WriteThroughWriter.newWriter(getOutputStream(),
99+
Objects.requireNonNullElse(charset, StandardCharsets.UTF_8)));
97100
}
98101
return writer;
99102
}

0 commit comments

Comments
 (0)