Search in sources :

Example 1 with NetworkConnector

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();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) CachingSessionDataStore(org.eclipse.jetty.server.session.CachingSessionDataStore) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpClient(org.eclipse.jetty.client.HttpClient) NullSessionDataStore(org.eclipse.jetty.server.session.NullSessionDataStore) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test)

Example 2 with NetworkConnector

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);
}
Also used : NetworkConnector(org.eclipse.jetty.server.NetworkConnector) MessageDigest(java.security.MessageDigest) Socket(java.net.Socket) Constraint(org.eclipse.jetty.util.security.Constraint) Test(org.junit.Test)

Example 3 with NetworkConnector

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);
}
Also used : NetworkConnector(org.eclipse.jetty.server.NetworkConnector) MessageDigest(java.security.MessageDigest) Socket(java.net.Socket) Constraint(org.eclipse.jetty.util.security.Constraint) Test(org.junit.Test)

Example 4 with NetworkConnector

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();
    }
}
Also used : StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) URI(java.net.URI) FileInputStream(java.io.FileInputStream) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 5 with NetworkConnector

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;
    }
}
Also used : HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) InetSocketAddress(java.net.InetSocketAddress) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) URI(java.net.URI)

Aggregations

NetworkConnector (org.eclipse.jetty.server.NetworkConnector)20 Server (org.eclipse.jetty.server.Server)9 Test (org.junit.Test)8 Connector (org.eclipse.jetty.server.Connector)7 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)6 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)6 URI (java.net.URI)5 IOException (java.io.IOException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpClient (org.eclipse.jetty.client.HttpClient)3 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)3 Request (org.eclipse.jetty.client.api.Request)3 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)2 File (java.io.File)2 InetSocketAddress (java.net.InetSocketAddress)2 Socket (java.net.Socket)2 MessageDigest (java.security.MessageDigest)2 AuthenticationStore (org.eclipse.jetty.client.api.AuthenticationStore)2 DigestAuthentication (org.eclipse.jetty.client.util.DigestAuthentication)2 HashLoginService (org.eclipse.jetty.security.HashLoginService)2