Search in sources :

Example 1 with HostPort

use of org.eclipse.jetty.util.HostPort in project jetty.project by eclipse.

the class ConnectorServer method startRegistry.

/**
     * Check that local RMI registry is used, and ensure it is started. If local RMI registry is being used and not started, start it.
     *
     * @param hostPath
     *            hostname and port number of RMI registry
     * @throws Exception
     */
private String startRegistry(String hostPath) throws Exception {
    HostPort hostPort = new HostPort(hostPath);
    String rmiHost = hostPort.getHost();
    int rmiPort = hostPort.getPort(1099);
    // Verify that local registry is being used
    InetAddress hostAddress = InetAddress.getByName(rmiHost);
    if (hostAddress.isLoopbackAddress()) {
        if (rmiPort == 0) {
            ServerSocket socket = new ServerSocket(0);
            rmiPort = socket.getLocalPort();
            socket.close();
        } else {
            try {
                // Check if a local registry is already running
                LocateRegistry.getRegistry(rmiPort).list();
                return null;
            } catch (Exception ex) {
                LOG.ignore(ex);
            }
        }
        _registry = LocateRegistry.createRegistry(rmiPort);
        Thread.sleep(1000);
        rmiHost = HostPort.normalizeHost(InetAddress.getLocalHost().getCanonicalHostName());
        return rmiHost + ':' + Integer.toString(rmiPort);
    }
    return null;
}
Also used : HostPort(org.eclipse.jetty.util.HostPort) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress)

Example 2 with HostPort

use of org.eclipse.jetty.util.HostPort in project jetty.project by eclipse.

the class ConnectHandler method handleConnect.

/**
     * <p>Handles a CONNECT request.</p>
     * <p>CONNECT requests may have authentication headers such as {@code Proxy-Authorization}
     * that authenticate the client with the proxy.</p>
     *
     * @param baseRequest  Jetty-specific http request
     * @param request       the http request
     * @param response      the http response
     * @param serverAddress the remote server address in the form {@code host:port}
     */
protected void handleConnect(Request baseRequest, HttpServletRequest request, HttpServletResponse response, String serverAddress) {
    baseRequest.setHandled(true);
    try {
        boolean proceed = handleAuthentication(request, response, serverAddress);
        if (!proceed) {
            if (LOG.isDebugEnabled())
                LOG.debug("Missing proxy authentication");
            sendConnectResponse(request, response, HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED);
            return;
        }
        HostPort hostPort = new HostPort(serverAddress);
        String host = hostPort.getHost();
        int port = hostPort.getPort(80);
        if (!validateDestination(host, port)) {
            if (LOG.isDebugEnabled())
                LOG.debug("Destination {}:{} forbidden", host, port);
            sendConnectResponse(request, response, HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        HttpTransport transport = baseRequest.getHttpChannel().getHttpTransport();
        // TODO Handle CONNECT over HTTP2!
        if (!(transport instanceof HttpConnection)) {
            if (LOG.isDebugEnabled())
                LOG.debug("CONNECT not supported for {}", transport);
            sendConnectResponse(request, response, HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        AsyncContext asyncContext = request.startAsync();
        asyncContext.setTimeout(0);
        if (LOG.isDebugEnabled())
            LOG.debug("Connecting to {}:{}", host, port);
        connectToServer(request, host, port, new Promise<SocketChannel>() {

            @Override
            public void succeeded(SocketChannel channel) {
                ConnectContext connectContext = new ConnectContext(request, response, asyncContext, (HttpConnection) transport);
                if (channel.isConnected())
                    selector.accept(channel, connectContext);
                else
                    selector.connect(channel, connectContext);
            }

            @Override
            public void failed(Throwable x) {
                onConnectFailure(request, response, asyncContext, x);
            }
        });
    } catch (Exception x) {
        onConnectFailure(request, response, null, x);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) HttpConnection(org.eclipse.jetty.server.HttpConnection) HostPort(org.eclipse.jetty.util.HostPort) AsyncContext(javax.servlet.AsyncContext) EndPoint(org.eclipse.jetty.io.EndPoint) SelectChannelEndPoint(org.eclipse.jetty.io.SelectChannelEndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpTransport(org.eclipse.jetty.server.HttpTransport)

Aggregations

HostPort (org.eclipse.jetty.util.HostPort)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 ServerSocket (java.net.ServerSocket)1 SocketChannel (java.nio.channels.SocketChannel)1 AsyncContext (javax.servlet.AsyncContext)1 ServletException (javax.servlet.ServletException)1 EndPoint (org.eclipse.jetty.io.EndPoint)1 SelectChannelEndPoint (org.eclipse.jetty.io.SelectChannelEndPoint)1 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)1 HttpConnection (org.eclipse.jetty.server.HttpConnection)1 HttpTransport (org.eclipse.jetty.server.HttpTransport)1