Skip to content

Commit c153338

Browse files
committed
WIP
1 parent 05e73f4 commit c153338

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@
2929
import java.util.Map;
3030
import java.util.Optional;
3131
import java.util.TreeMap;
32+
import java.util.concurrent.ExecutionException;
3233
import java.util.regex.Matcher;
3334
import java.util.regex.Pattern;
35+
import java.util.stream.StreamSupport;
3436
import org.eclipse.jetty.http.HttpField;
3537
import org.eclipse.jetty.http.HttpHeader;
3638
import org.eclipse.jetty.http.MimeTypes;
39+
import org.eclipse.jetty.http.MimeTypes.Type;
40+
import org.eclipse.jetty.http.MultiPart;
3741
import org.eclipse.jetty.http.MultiPart.Part;
42+
import org.eclipse.jetty.http.MultiPartFormData;
3843
import org.eclipse.jetty.io.Content;
3944
import org.eclipse.jetty.server.Request;
4045
import org.eclipse.jetty.util.Fields.Field;
@@ -75,7 +80,21 @@ public Map<String, List<String>> getQueryParameters() {
7580

7681
@Override
7782
public Map<String, HttpPart> getParts() {
78-
// TODO
83+
84+
// TODO initiate reading the parts asynchronously before invocation
85+
String contentType = request.getHeaders().get(HttpHeader.CONTENT_TYPE);
86+
if (Type.MULTIPART_FORM_DATA.is(MimeTypes.getContentTypeWithoutCharset(contentType))) {
87+
String boundary = MultiPart.extractBoundary(contentType);
88+
try {
89+
MultiPartFormData.Parts parts =
90+
MultiPartFormData.from(request, boundary, parser -> parser.parse(request)).get();
91+
return StreamSupport.stream(parts.spliterator(), false)
92+
.map(HttpPartImpl::new)
93+
.collect(toMap(HttpPartImpl::getName, p -> p));
94+
} catch (InterruptedException | ExecutionException e) {
95+
throw new RuntimeException(e);
96+
}
97+
}
7998
return Collections.emptyMap();
8099
}
81100

@@ -123,6 +142,10 @@ private HttpPartImpl(Part part) {
123142
this.part = part;
124143
}
125144

145+
public String getName() {
146+
return part.getName();
147+
}
148+
126149
@Override
127150
public Optional<String> getFileName() {
128151
return Optional.ofNullable(part.getFileName());

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.BufferedWriter;
2121
import java.io.IOException;
2222
import java.io.OutputStream;
23+
import java.nio.charset.Charset;
2324
import java.nio.charset.StandardCharsets;
2425
import java.util.ArrayList;
2526
import java.util.Collection;
@@ -30,7 +31,6 @@
3031
import java.util.TreeMap;
3132
import org.eclipse.jetty.http.HttpField;
3233
import org.eclipse.jetty.http.HttpHeader;
33-
import org.eclipse.jetty.http.MimeTypes;
3434
import org.eclipse.jetty.io.Content;
3535
import org.eclipse.jetty.io.writer.AbstractOutputStreamWriter;
3636
import org.eclipse.jetty.server.Response;
@@ -96,19 +96,11 @@ public OutputStream getOutputStream() throws IOException {
9696
@Override
9797
public synchronized BufferedWriter getWriter() throws IOException {
9898
if (writer == null) {
99-
// TODO request a utility method from the Jetty project to do this
10099
String contentType = getContentType().orElse(null);
101-
String encoding = MimeTypes.getCharsetFromContentType(contentType);
102-
if (encoding == null && contentType != null) {
103-
encoding = MimeTypes.DEFAULTS.getCharsetAssumedFromContentType(contentType);
104-
}
105-
if (encoding == null && contentType != null) {
106-
encoding = MimeTypes.DEFAULTS.getCharsetInferredFromContentType(contentType);
107-
}
108-
encoding = Objects.requireNonNullElse(encoding, StandardCharsets.UTF_8.name());
109-
110-
writer = new BufferedWriter(AbstractOutputStreamWriter
111-
.newWriter(getOutputStream(), encoding));
100+
Charset charset = Objects.requireNonNullElse(
101+
response.getRequest().getContext().getMimeTypes().getCharset(contentType),
102+
StandardCharsets.UTF_8);
103+
writer = new BufferedWriter(AbstractOutputStreamWriter.newWriter(getOutputStream(), charset));
112104
}
113105
return writer;
114106
}

0 commit comments

Comments
 (0)