Skip to content
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

Use sessionRequest for wrapping HTTP stream instead of original Request #12303

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public boolean handle(Request request, Response response, Callback callback) thr
return false;

SessionRequest sessionRequest = new SessionRequest(request);
addSessionStreamWrapper(request);
addSessionStreamWrapper(sessionRequest);
return sessionRequest.process(next, response, callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.eclipse.jetty.session;

import java.io.File;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

Expand All @@ -25,7 +26,9 @@
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Session;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.IO;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -721,4 +724,70 @@ public void testUriParameterOnly() throws Exception
}
}

@Test
public void testFlushOnResponseCommit() throws Exception
{
// Setup temporary datastore with write-through null cache

File datastoreDir = MavenTestingUtils.getTargetTestingDir("datastore");
IO.delete(datastoreDir);

FileSessionDataStore datastore = new FileSessionDataStore();
datastore.setStoreDir(datastoreDir);

NullSessionCache cache = new NullSessionCache(_sessionHandler);
cache.setSessionDataStore(datastore);
cache.setFlushOnResponseCommit(true);

_sessionHandler.setSessionCache(cache);

_server.start();

LocalConnector.LocalEndPoint endPoint = _connector.connect();
endPoint.addInput("""
GET /create HTTP/1.1
Host: localhost
""");

HttpTester.Response response = HttpTester.parseResponse(endPoint.getResponse());
assertThat(response.getStatus(), equalTo(200));
String setCookie = response.get(HttpHeader.SET_COOKIE);
String id = setCookie.substring(setCookie.indexOf("SESSION_ID=") + 11, setCookie.indexOf("; Path=/"));
String content = response.getContent();
assertThat(content, startsWith("Session="));

endPoint.addInput("""
GET /set/attribute/value HTTP/1.1
Host: localhost
Cookie: SESSION_ID=%s
""".formatted(id));

response = HttpTester.parseResponse(endPoint.getResponse());
assertThat(response.getStatus(), equalTo(200));
assertThat(response.get(HttpHeader.SET_COOKIE), nullValue());
content = response.getContent();
assertThat(content, containsString("Session=" + id.substring(0, id.indexOf(".node0"))));
assertThat(content, containsString("attribute = value"));

// Session should persist through restart
_server.stop();
_server.start();

endPoint.addInput("""
GET /set/attribute/value HTTP/1.1
Host: localhost
Cookie: SESSION_ID=%s
""".formatted(id));

response = HttpTester.parseResponse(endPoint.getResponse());
assertThat(response.getStatus(), equalTo(200));
assertThat(response.get(HttpHeader.SET_COOKIE), nullValue());
content = response.getContent();
assertThat(content, containsString("Session=" + id.substring(0, id.indexOf(".node0"))));
assertThat(content, containsString("attribute = value"));
}

}
Loading