|
15 | 15 | */ |
16 | 16 | package io.fusionauth.http.util; |
17 | 17 |
|
| 18 | +import java.io.ByteArrayInputStream; |
18 | 19 | import java.net.URLEncoder; |
19 | 20 | import java.nio.charset.StandardCharsets; |
20 | 21 | import java.util.HashMap; |
21 | 22 | import java.util.List; |
22 | 23 | import java.util.Map; |
23 | 24 |
|
24 | 25 | import com.inversoft.json.ToString; |
| 26 | +import io.fusionauth.http.HTTPMethod; |
| 27 | +import io.fusionauth.http.io.PushbackInputStream; |
| 28 | +import io.fusionauth.http.server.HTTPRequest; |
| 29 | +import io.fusionauth.http.server.HTTPServerConfiguration; |
| 30 | +import io.fusionauth.http.server.internal.HTTPBuffers; |
| 31 | +import io.fusionauth.http.server.io.HTTPInputStream; |
25 | 32 | import io.fusionauth.http.util.HTTPTools.HeaderValue; |
26 | 33 | import org.testng.annotations.Test; |
27 | 34 | import static org.testng.Assert.assertEquals; |
@@ -101,4 +108,78 @@ public void parseHeaderValue() { |
101 | 108 | assertEquals(HTTPTools.parseHeaderValue("value; f =f"), new HeaderValue("value", Map.of("f ", "f"))); |
102 | 109 | assertEquals(HTTPTools.parseHeaderValue("value; f= f"), new HeaderValue("value", Map.of("f", " f"))); |
103 | 110 | } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void parsePreamble() throws Exception { |
| 114 | + // Ensure that we can correctly read the preamble when the InputStream contains the next request. |
| 115 | + |
| 116 | + //noinspection ExtractMethodRecommender |
| 117 | + String request = """ |
| 118 | + GET / HTTP/1.1\r |
| 119 | + Host: localhost:42\r |
| 120 | + Connection: close\r |
| 121 | + Content-Length: 113\r |
| 122 | + Header1: Value1\r |
| 123 | + Header2: Value2\r |
| 124 | + Header3: Value3\r |
| 125 | + Header4: Value4\r |
| 126 | + Header5: Value5\r |
| 127 | + Header6: Value6\r |
| 128 | + Header7: Value7\r |
| 129 | + Header8: Value8\r |
| 130 | + Header9: Value9\r |
| 131 | + Header10: Value10\r |
| 132 | + \r |
| 133 | + These pretzels are making me thirsty. These pretzels are making me thirsty. These pretzels are making me thirsty.GET / HTTP/1.1\r |
| 134 | + """; |
| 135 | + |
| 136 | + // Fixed length body with the start of the next request in the buffer |
| 137 | + byte[] bytes = request.getBytes(StandardCharsets.UTF_8); |
| 138 | + int bytesAvailable = bytes.length; |
| 139 | + |
| 140 | + // Ensure the request buffer size will contain the entire request. |
| 141 | + HTTPServerConfiguration configuration = new HTTPServerConfiguration().withRequestBufferSize(bytesAvailable + 100); |
| 142 | + |
| 143 | + ByteArrayInputStream is = new ByteArrayInputStream(bytes); |
| 144 | + PushbackInputStream pushbackInputStream = new PushbackInputStream(is, null); |
| 145 | + |
| 146 | + HTTPRequest httpRequest = new HTTPRequest(); |
| 147 | + HTTPBuffers buffers = new HTTPBuffers(configuration); |
| 148 | + byte[] requestBuffer = buffers.requestBuffer(); |
| 149 | + |
| 150 | + HTTPTools.initialize(configuration.getLoggerFactory()); |
| 151 | + HTTPTools.parseRequestPreamble(pushbackInputStream, 128 * 1024, httpRequest, requestBuffer, () -> { |
| 152 | + }); |
| 153 | + |
| 154 | + // Ensure we parsed the request and that the right number of bytes is left over |
| 155 | + assertEquals(httpRequest.getMethod(), HTTPMethod.GET); |
| 156 | + assertEquals(httpRequest.getHost(), "localhost"); |
| 157 | + assertEquals(httpRequest.getPort(), 42); |
| 158 | + assertEquals(httpRequest.getContentLength(), 113); |
| 159 | + assertEquals(httpRequest.getHeaders().size(), 13); |
| 160 | + assertEquals(httpRequest.getHeader("Content-Length"), "113"); |
| 161 | + assertEquals(httpRequest.getHeader("Connection"), "close"); |
| 162 | + assertEquals(httpRequest.getHeader("Host"), "localhost:42"); |
| 163 | + for (int i = 1; i <= 10; i++) { |
| 164 | + assertEquals(httpRequest.getHeader("Header" + i), "Value" + i); |
| 165 | + } |
| 166 | + |
| 167 | + // Expect 129 bytes left over which is 113 for the body + 16 from the next request |
| 168 | + assertEquals(pushbackInputStream.getAvailableBufferedBytesRemaining(), 113 + 16); |
| 169 | + |
| 170 | + // Read the remaining bytes for this request, we should still have some left over. |
| 171 | + HTTPInputStream httpInputStream = new HTTPInputStream(configuration, httpRequest, pushbackInputStream, -1); |
| 172 | + byte[] buffer = new byte[1024]; |
| 173 | + int read = httpInputStream.read(buffer); |
| 174 | + assertEquals(read, 113); |
| 175 | + |
| 176 | + // Another read should return -1 because we are at the end of this request. |
| 177 | + int nextRead = httpInputStream.read(buffer); |
| 178 | + assertEquals(nextRead, -1); |
| 179 | + |
| 180 | + // The next read from the pushback which will be used by the next request should return the remaining bytes. |
| 181 | + assertEquals(pushbackInputStream.getAvailableBufferedBytesRemaining(), 16); |
| 182 | + int nextRequestRead = pushbackInputStream.read(buffer); |
| 183 | + assertEquals(nextRequestRead, 16); |
| 184 | + } |
104 | 185 | } |
0 commit comments