use of org.eclipse.jetty.server.NetworkConnector in project jetty.project by eclipse.
the class TestMemcachedSessions method testMemcached.
@Test
public void testMemcached() throws Exception {
String contextPath = "/";
Server server = new Server(0);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setResourceBase(System.getProperty("java.io.tmpdir"));
server.setHandler(context);
NullSessionCache dsc = new NullSessionCache(context.getSessionHandler());
dsc.setSessionDataStore(new CachingSessionDataStore(new MemcachedSessionDataMap("localhost", "11211"), new NullSessionDataStore()));
context.getSessionHandler().setSessionCache(dsc);
// Add a test servlet
ServletHolder h = new ServletHolder();
h.setServlet(new TestServlet());
context.addServlet(h, "/");
try {
server.start();
int port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
HttpClient client = new HttpClient();
client.start();
try {
int value = 42;
ContentResponse response = client.GET("http://localhost:" + port + contextPath + "?action=set&value=" + value);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
String sessionCookie = response.getHeaders().get("Set-Cookie");
assertTrue(sessionCookie != null);
// Mangle the cookie, replacing Path with $Path, etc.
sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
String resp = response.getContentAsString();
assertEquals(resp.trim(), String.valueOf(value));
// Be sure the session value is still there
Request request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
resp = response.getContentAsString();
assertEquals(String.valueOf(value), resp.trim());
//Delete the session
request = client.newRequest("http://localhost:" + port + contextPath + "?action=del");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
//Check that the session is gone
request = client.newRequest("http://localhost:" + port + contextPath + "?action=get");
request.header("Cookie", sessionCookie);
response = request.send();
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
resp = response.getContentAsString();
assertEquals("No session", resp.trim());
} finally {
client.stop();
}
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.NetworkConnector in project jetty.project by eclipse.
the class DigestPostTest method testServerDirectlyHTTP10.
@Test
public void testServerDirectlyHTTP10() throws Exception {
Socket socket = new Socket("127.0.0.1", ((NetworkConnector) _server.getConnectors()[0]).getLocalPort());
byte[] bytes = __message.getBytes(StandardCharsets.UTF_8);
_received = null;
socket.getOutputStream().write(("POST /test/ HTTP/1.0\r\n" + "Host: 127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "\r\n" + "Content-Length: " + bytes.length + "\r\n" + "\r\n").getBytes(StandardCharsets.UTF_8));
socket.getOutputStream().write(bytes);
socket.getOutputStream().flush();
String result = IO.toString(socket.getInputStream());
Assert.assertTrue(result.startsWith("HTTP/1.1 401 Unauthorized"));
Assert.assertEquals(null, _received);
int n = result.indexOf("nonce=");
String nonce = result.substring(n + 7, result.indexOf('"', n + 7));
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] b = md.digest(String.valueOf(System.currentTimeMillis()).getBytes(org.eclipse.jetty.util.StringUtil.__ISO_8859_1));
String cnonce = encode(b);
String digest = "Digest username=\"testuser\" realm=\"test\" nonce=\"" + nonce + "\" uri=\"/test/\" algorithm=MD5 response=\"" + newResponse("POST", "/test/", cnonce, "testuser", "test", "password", nonce, "auth") + "\" qop=auth nc=" + NC + " cnonce=\"" + cnonce + "\"";
socket = new Socket("127.0.0.1", ((NetworkConnector) _server.getConnectors()[0]).getLocalPort());
_received = null;
socket.getOutputStream().write(("POST /test/ HTTP/1.0\r\n" + "Host: 127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "\r\n" + "Content-Length: " + bytes.length + "\r\n" + "Authorization: " + digest + "\r\n" + "\r\n").getBytes(StandardCharsets.UTF_8));
socket.getOutputStream().write(bytes);
socket.getOutputStream().flush();
result = IO.toString(socket.getInputStream());
Assert.assertTrue(result.startsWith("HTTP/1.1 200 OK"));
Assert.assertEquals(__message, _received);
}
use of org.eclipse.jetty.server.NetworkConnector in project jetty.project by eclipse.
the class DigestPostTest method testServerDirectlyHTTP11.
@Test
public void testServerDirectlyHTTP11() throws Exception {
Socket socket = new Socket("127.0.0.1", ((NetworkConnector) _server.getConnectors()[0]).getLocalPort());
byte[] bytes = __message.getBytes(StandardCharsets.UTF_8);
_received = null;
socket.getOutputStream().write(("POST /test/ HTTP/1.1\r\n" + "Host: 127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "\r\n" + "Content-Length: " + bytes.length + "\r\n" + "\r\n").getBytes("UTF-8"));
socket.getOutputStream().write(bytes);
socket.getOutputStream().flush();
Thread.sleep(100);
byte[] buf = new byte[4096];
int len = socket.getInputStream().read(buf);
String result = new String(buf, 0, len, StandardCharsets.UTF_8);
Assert.assertTrue(result.startsWith("HTTP/1.1 401 Unauthorized"));
Assert.assertEquals(null, _received);
int n = result.indexOf("nonce=");
String nonce = result.substring(n + 7, result.indexOf('"', n + 7));
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] b = md.digest(String.valueOf(System.currentTimeMillis()).getBytes(StringUtil.__ISO_8859_1));
String cnonce = encode(b);
String digest = "Digest username=\"testuser\" realm=\"test\" nonce=\"" + nonce + "\" uri=\"/test/\" algorithm=MD5 response=\"" + newResponse("POST", "/test/", cnonce, "testuser", "test", "password", nonce, "auth") + "\" qop=auth nc=" + NC + " cnonce=\"" + cnonce + "\"";
_received = null;
socket.getOutputStream().write(("POST /test/ HTTP/1.0\r\n" + "Host: 127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "\r\n" + "Content-Length: " + bytes.length + "\r\n" + "Authorization: " + digest + "\r\n" + "\r\n").getBytes("UTF-8"));
socket.getOutputStream().write(bytes);
socket.getOutputStream().flush();
result = IO.toString(socket.getInputStream());
Assert.assertTrue(result.startsWith("HTTP/1.1 200 OK"));
Assert.assertEquals(__message, _received);
}
use of org.eclipse.jetty.server.NetworkConnector in project jetty.project by eclipse.
the class DigestPostTest method testServerWithHttpClientStreamContent.
@Test
public void testServerWithHttpClientStreamContent() throws Exception {
String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
HttpClient client = new HttpClient();
try {
AuthenticationStore authStore = client.getAuthenticationStore();
authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
client.start();
String sent = IO.toString(new FileInputStream("src/test/resources/message.txt"));
Request request = client.newRequest(srvUrl);
request.method(HttpMethod.POST);
request.content(new StringContentProvider(sent));
_received = null;
request = request.timeout(5, TimeUnit.SECONDS);
ContentResponse response = request.send();
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(sent, _received);
} finally {
client.stop();
}
}
use of org.eclipse.jetty.server.NetworkConnector in project knox by apache.
the class GatewayServer method startGateway.
public static GatewayServer startGateway(GatewayConfig config, GatewayServices svcs) throws Exception {
log.startingGateway();
server = new GatewayServer(config);
synchronized (server) {
// KM[ Commented this out because is causes problems with
// multiple services instance used in a single test process.
// I'm not sure what drive including this check though.
// if (services == null) {
services = svcs;
// }
// KM]
services.start();
DeploymentFactory.setGatewayServices(services);
server.start();
// Coverity CID 1352654
URI uri = server.jetty.getURI();
// Logging for topology <-> port
InetSocketAddress[] addresses = new InetSocketAddress[server.jetty.getConnectors().length];
for (int i = 0, n = addresses.length; i < n; i++) {
NetworkConnector connector = (NetworkConnector) server.jetty.getConnectors()[i];
if (connector != null) {
for (ConnectionFactory x : connector.getConnectionFactories()) {
if (x instanceof HttpConnectionFactory) {
((HttpConnectionFactory) x).getHttpConfiguration().setSendServerVersion(config.isGatewayServerHeaderEnabled());
}
}
if (connector.getName() == null) {
log.startedGateway(connector.getLocalPort());
} else {
log.startedGateway(connector.getName(), connector.getLocalPort());
}
}
}
return server;
}
}
Aggregations