generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 14
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
#921 Allow reading body multiple times #923
Merged
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c6ec72e
Update common files (#912)
micronaut-build 829f754
Add the Server TCK for Oracle Cloud HTTP Function
andriy-dmytruk a966a4a
Map the Fn-Http-H- header prefix to normal headers for testing
andriy-dmytruk 32652f1
Make the FnServletRequest mutable to support request filters
andriy-dmytruk 75f62da
Add support for parsing form data
andriy-dmytruk 809c007
Fix for empty values in the form data
andriy-dmytruk eb87c27
Fix header testing by canonizing the header keys
andriy-dmytruk a9157c0
Support single values in JSON for decoding publishers
andriy-dmytruk a902b36
Support binding JSON body parts for body
andriy-dmytruk ec586fe
Parse cookies from client header
andriy-dmytruk 412d7e0
Use message body writer to write the input message if it is supplied …
andriy-dmytruk 920adf7
Minor refactor
andriy-dmytruk 44d30d8
Revert the HttpFunction change and configure the embedded server for …
andriy-dmytruk c37c84e
Refactor failed binding result into a common class
andriy-dmytruk 1726ef4
Attempt to allow reading body multiple times
andriy-dmytruk ab4601e
Improve the implementation using InputStreamByteBody
andriy-dmytruk fd99aaa
fix byteBody implementation
yawkat e3d3b87
Attempt to allow reading body multiple times
andriy-dmytruk cd53a7a
Improve the implementation using InputStreamByteBody
andriy-dmytruk fec7e83
fix byteBody implementation
yawkat bbd3235
Disable binary compatability
andriy-dmytruk efe5c9d
Merge remote-tracking branch 'origin/andriy/function-http-tck-multire…
yawkat 8121669
enable byteBody test
yawkat 2aec7dd
Revert "enable byteBody test"
yawkat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
103 changes: 103 additions & 0 deletions
103
...tp/src/main/java/io/micronaut/oraclecloud/function/http/OptionalBufferingInputStream.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,103 @@ | ||
/* | ||
* 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.oraclecloud.function.http; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.UncheckedIOException; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* This stream class wraps another upstream {@link InputStream}. Normally, read actions are simply | ||
* forwarded upstream. However, there is an additional action {@link #bufferIfNecessary()} that, if | ||
* this stream has not been closed yet, buffers all remaining data from the upstream. Downstream | ||
* consumers can then continue reading data independent of the upstream. | ||
* <p>This class is necessary because fnproject closes the upstream at some point, and we might not | ||
* have read all data yet. So we {@link #bufferIfNecessary()} if the downstream users still need | ||
* access to the data after the close. | ||
*/ | ||
final class OptionalBufferingInputStream extends InputStream { | ||
private final Lock lock = new ReentrantLock(); | ||
private final InputStream upstream; | ||
private byte[] buffered; | ||
private int bufferedIndex; | ||
private boolean closed; | ||
|
||
OptionalBufferingInputStream(InputStream upstream) { | ||
this.upstream = upstream; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
byte[] arr1 = new byte[1]; | ||
int n = read(arr1); | ||
if (n == -1) { | ||
return -1; | ||
} else if (n == 0) { | ||
throw new IllegalStateException("Read 0 bytes"); | ||
} else { | ||
return arr1[0] & 0xff; | ||
} | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) throws IOException { | ||
lock.lock(); | ||
try { | ||
if (buffered == null) { | ||
return upstream.read(b, off, len); | ||
} else { | ||
if (bufferedIndex >= buffered.length) { | ||
return -1; | ||
} else { | ||
int n = Math.min(len, buffered.length - bufferedIndex); | ||
System.arraycopy(buffered, bufferedIndex, b, off, n); | ||
bufferedIndex += n; | ||
return n; | ||
} | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
lock.lock(); | ||
try { | ||
closed = true; | ||
upstream.close(); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
void bufferIfNecessary() { | ||
lock.lock(); | ||
try { | ||
if (!closed) { | ||
try { | ||
buffered = upstream.readAllBytes(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, this is great!