Skip to content

Commit

Permalink
#9078 prototype fix
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <[email protected]>
  • Loading branch information
lorban committed Dec 22, 2022
1 parent 040ed85 commit 6605455
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public long getContentLengthValue()
@Override
public ByteBuffer getByteBuffer()
{
return _buffer;
return _buffer == null ? null : _buffer.duplicate();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ public ByteBuffer getByteBuffer()
{
ByteBuffer buffer = _buffer;
if (buffer != null)
return (buffer == SENTINEL_BUFFER) ? super.getByteBuffer() : buffer;
return (buffer == SENTINEL_BUFFER) ? super.getByteBuffer() : buffer.duplicate();

try (AutoLock lock = _lock.lock())
{
if (_buffer == null)
_buffer = getMappedByteBuffer();
return (_buffer == SENTINEL_BUFFER) ? super.getByteBuffer() : _buffer;
return (_buffer == SENTINEL_BUFFER) ? super.getByteBuffer() : _buffer.duplicate();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ protected void writeHttpContent(Request request, Response response, Callback cal
{
try
{
ByteBuffer buffer = content.getByteBuffer();
ByteBuffer buffer = content.getByteBuffer(); // this buffer is going to be consumed by response.write()
if (buffer != null)
{
response.write(true, buffer, callback);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.eclipse.jetty.ee10.webapp;

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.Server;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class WebAppDefaultServletCacheTest
{
private LocalConnector connector;
private Server server;
private Path resourcePath;

@Test
public void testCacheRefreshing() throws Exception
{
String fileName = "jetty-pic.png";
long fileSize = Files.size(resourcePath.resolve(fileName));

HttpTester.Response response1 = sendRequest(connector, fileName);
assertEquals(response1.getLongField("Content-Length"), fileSize);
assertEquals(200, response1.getStatus());

HttpTester.Response response2 = sendRequest(connector, fileName, "If-Modified-Since: " + response1.get("Last-Modified"));
assertEquals(response2.getLongField("Content-Length"), 0L);
assertEquals(304, response2.getStatus());

HttpTester.Response response3 = sendRequest(connector, fileName, "Cache-Control: no-cache", "Pragma: no-cache");
assertEquals(response3.getLongField("Content-Length"), fileSize);
assertEquals(200, response3.getStatus());
}

@BeforeEach
protected void setUp() throws Exception
{
server = new Server();
connector = new LocalConnector(server);
server.addConnector(connector);

URI uri = getClass().getResource("/org/acme").toURI();
resourcePath = Paths.get(uri);
server.addHandler(new WebAppContext(uri.toString(), "/"));

server.start();
}

@AfterEach
protected void tearDown() throws Exception
{
server.stop();
}

private static HttpTester.Response sendRequest(LocalConnector connector, String file, String... extraHeaders) throws Exception
{
StringBuilder rawRequest = new StringBuilder();
rawRequest.append("GET /").append(file).append(" HTTP/1.1\r\n");
rawRequest.append("Host: local\r\n");
for (String extraHeader : extraHeaders)
{
rawRequest.append(extraHeader).append("\r\n");
}
rawRequest.append("\r\n");
String rawResponse = connector.getResponse(rawRequest.toString());
return HttpTester.parseResponse(rawResponse);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6605455

Please sign in to comment.