-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ResponseLifecycle from core http-server
Instead of servlet-specific body encoding logic, use the ResponseLifecycle introduced in core by micronaut-projects/micronaut-core#11342 (pending review). Then we only have to take care of writing the ByteBody to the servlet response. Some other related changes: - Improved async reading/writing support, deprecate StreamedServletMessage - Rework HttpResponse lifecycle management: Core mostly expects HttpResponseFactory to return new, independent responses, but servlet returned views of the same ServletHttpResponse each time (with shared headers and such). New approach is to have only the latest HttpResponse backed by the real ServletHttpResponse, any previous responses are copied snapshots - Deprecate and stop using ServletResponseEncoder, it is replaced by core ResponseBodyEncoder. These changes fix all the remaining TCK failures that relate to response handling, except for FilterProxyTest.
- Loading branch information
Showing
46 changed files
with
1,025 additions
and
1,050 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
http-poja-apache/src/main/java/io/micronaut/http/poja/apache/ApacheResponseContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.http.poja.apache; | ||
|
||
import io.micronaut.http.HttpHeaders; | ||
import io.micronaut.http.server.exceptions.HttpServerException; | ||
import org.apache.hc.core5.http.ClassicHttpResponse; | ||
import org.apache.hc.core5.http.Header; | ||
import org.apache.hc.core5.http.HttpException; | ||
import org.apache.hc.core5.http.impl.io.ChunkedOutputStream; | ||
import org.apache.hc.core5.http.impl.io.ContentLengthOutputStream; | ||
import org.apache.hc.core5.http.impl.io.DefaultHttpResponseWriter; | ||
import org.apache.hc.core5.http.impl.io.SessionOutputBufferImpl; | ||
import org.apache.hc.core5.http.io.SessionOutputBuffer; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
final class ApacheResponseContext implements Closeable { | ||
boolean connectionClose = false; | ||
ApacheServletHttpResponse<?> primaryResponse; | ||
|
||
private final SessionOutputBuffer outputBuffer; | ||
private final OutputStream out; | ||
private OutputStream bodyStream; | ||
|
||
ApacheResponseContext(ApacheServletConfiguration configuration, OutputStream out) { | ||
this.out = out; | ||
outputBuffer = new SessionOutputBufferImpl(configuration.outputBufferSize()); | ||
} | ||
|
||
boolean isCommitted() { | ||
return bodyStream != null; | ||
} | ||
|
||
OutputStream commit(ClassicHttpResponse headers) throws IOException { | ||
if (isCommitted()) { | ||
throw new IllegalStateException("Response has already been committed"); | ||
} | ||
|
||
headers.removeHeaders(HttpHeaders.TRANSFER_ENCODING); | ||
Header contentLengthStr = headers.getFirstHeader(HttpHeaders.CONTENT_LENGTH); | ||
long contentLength = contentLengthStr == null ? -1 : Long.parseLong(contentLengthStr.getValue()); | ||
if (contentLength < 0) { | ||
headers.removeHeaders(HttpHeaders.CONTENT_LENGTH); | ||
headers.addHeader(HttpHeaders.TRANSFER_ENCODING, "chunked"); | ||
} | ||
|
||
Header connection = headers.getFirstHeader(HttpHeaders.CONNECTION); | ||
if (connection != null && connection.getValue().equalsIgnoreCase("close")) { | ||
connectionClose = true; | ||
} | ||
|
||
DefaultHttpResponseWriter responseWriter = new DefaultHttpResponseWriter(); | ||
try { | ||
responseWriter.write(headers, outputBuffer, out); | ||
} catch (HttpException e) { | ||
throw new HttpServerException("Could not write response headers", e); | ||
} | ||
|
||
OutputStream s = contentLength < 0 ? new ChunkedOutputStream(outputBuffer, out, 0) : new ContentLengthOutputStream(outputBuffer, out, contentLength); | ||
bodyStream = s; | ||
return s; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (bodyStream != null) { | ||
bodyStream.close(); | ||
} | ||
outputBuffer.flush(out); | ||
|
||
if (connectionClose) { | ||
out.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.