|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, FusionAuth, All Rights Reserved |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an |
| 12 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 13 | + * either express or implied. See the License for the specific |
| 14 | + * language governing permissions and limitations under the License. |
| 15 | + */ |
| 16 | +package io.fusionauth.http.io; |
| 17 | + |
| 18 | +import java.io.ByteArrayInputStream; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | + |
| 21 | +import io.fusionauth.http.server.HTTPRequest; |
| 22 | +import io.fusionauth.http.server.HTTPServerConfiguration; |
| 23 | +import io.fusionauth.http.server.io.HTTPInputStream; |
| 24 | +import org.testng.annotations.Test; |
| 25 | +import static org.testng.Assert.assertEquals; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Daniel DeGroff |
| 29 | + */ |
| 30 | +public class HTTPInputStreamTest { |
| 31 | + @Test |
| 32 | + public void read_chunked_withPushback() throws Exception { |
| 33 | + // Ensure that when we read a chunked encoded body that the InputStream returns the correct number of bytes read even when |
| 34 | + // we read past the end of the current request and use the PushbackInputStream. |
| 35 | + |
| 36 | + int contentLength = 113; |
| 37 | + String content = "These pretzels are making me thirsty. These pretzels are making me thirsty. These pretzels are making me thirsty."; |
| 38 | + |
| 39 | + // Chunk the content |
| 40 | + byte[] bytes = """ |
| 41 | + 26\r |
| 42 | + These pretzels are making me thirsty. \r |
| 43 | + 26\r |
| 44 | + These pretzels are making me thirsty. \r |
| 45 | + 25\r |
| 46 | + These pretzels are making me thirsty.\r |
| 47 | + 0\r |
| 48 | + \r |
| 49 | + GET / HTTP/1.1\r |
| 50 | + """.getBytes(); |
| 51 | + |
| 52 | + HTTPRequest request = new HTTPRequest(); |
| 53 | + request.setHeader("Transfer-Encoding", "chunked"); |
| 54 | + |
| 55 | + assertReadWithPushback(bytes, content, contentLength, request); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void read_fixedLength_withPushback() throws Exception { |
| 60 | + // Ensure that when we read a fixed length body that the InputStream returns the correct number of bytes read even when |
| 61 | + // we read past the end of the current request and use the PushbackInputStream. |
| 62 | + |
| 63 | + int contentLength = 113; |
| 64 | + String content = "These pretzels are making me thirsty. These pretzels are making me thirsty. These pretzels are making me thirsty."; |
| 65 | + |
| 66 | + // Fixed length body with the start of the next request in the buffer |
| 67 | + byte[] bytes = (content + "GET / HTTP/1.1\r\n").getBytes(StandardCharsets.UTF_8); |
| 68 | + |
| 69 | + HTTPRequest request = new HTTPRequest(); |
| 70 | + request.setHeader("Content-Length", contentLength + ""); |
| 71 | + |
| 72 | + assertReadWithPushback(bytes, content, contentLength, request); |
| 73 | + } |
| 74 | + |
| 75 | + private void assertReadWithPushback(byte[] bytes, String content, int contentLength, HTTPRequest request) throws Exception { |
| 76 | + int bytesAvailable = bytes.length; |
| 77 | + System.out.println("available bytes [" + bytesAvailable + "]"); |
| 78 | + System.out.println("buffer size [" + (bytesAvailable + 100) + "]"); |
| 79 | + HTTPServerConfiguration configuration = new HTTPServerConfiguration().withRequestBufferSize(bytesAvailable + 100); |
| 80 | + |
| 81 | + ByteArrayInputStream is = new ByteArrayInputStream(bytes); |
| 82 | + PushbackInputStream pushbackInputStream = new PushbackInputStream(is, null); |
| 83 | + |
| 84 | + HTTPInputStream httpInputStream = new HTTPInputStream(configuration, request, pushbackInputStream, -1); |
| 85 | + byte[] buffer = new byte[configuration.getRequestBufferSize()]; |
| 86 | + int read = httpInputStream.read(buffer); |
| 87 | + |
| 88 | + assertEquals(read, contentLength); |
| 89 | + assertEquals(new String(buffer, 0, read), content); |
| 90 | + |
| 91 | + // We should be at the end of the request, so expect -1 even though we have extra bytes in the PushbackInputStream |
| 92 | + int secondRead = httpInputStream.read(buffer); |
| 93 | + assertEquals(secondRead, -1); |
| 94 | + |
| 95 | + // We have 16 bytes left over |
| 96 | + assertEquals(pushbackInputStream.getAvailableBufferedBytesRemaining(), 16); |
| 97 | + |
| 98 | + // Next read should start at the next request |
| 99 | + byte[] leftOverBuffer = new byte[100]; |
| 100 | + int leftOverRead = pushbackInputStream.read(leftOverBuffer); |
| 101 | + assertEquals(leftOverRead, 16); |
| 102 | + assertEquals(new String(leftOverBuffer, 0, leftOverRead), "GET / HTTP/1.1\r\n"); |
| 103 | + } |
| 104 | +} |
0 commit comments