Search in sources :

Example 6 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class CustomSSLEngineProvider method trustManagers.

@Override
public TrustManager[] trustManagers() {
    try {
        final TrustManagerFactory trustManagerFactory = sslCertFingerprints.isEmpty() ? TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()) : FingerprintTrustManagerFactory.builder("SHA1").fingerprints(sslCertFingerprints).build();
        trustManagerFactory.init(loadKeystore(sslTrustStore, sslTrustStorePassword));
        return trustManagerFactory.getTrustManagers();
    } catch (GeneralSecurityException e) {
        // replicate exception handling from SSLEngineProvider
        throw new RemoteTransportException("Server SSL connection could not be established because SSL context could not be constructed", e);
    }
}
Also used : RemoteTransportException(akka.remote.RemoteTransportException) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) FingerprintTrustManagerFactory(org.apache.flink.shaded.netty4.io.netty.handler.ssl.util.FingerprintTrustManagerFactory) GeneralSecurityException(java.security.GeneralSecurityException)

Example 7 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class HttpTestClient method sendRequest.

/**
 * Sends a request to to the server.
 *
 * <pre>
 * HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/overview");
 * request.headers().set(HttpHeaders.Names.HOST, host);
 * request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
 *
 * sendRequest(request);
 * </pre>
 *
 * @param request The {@link HttpRequest} to send to the server
 */
public void sendRequest(HttpRequest request, Duration timeout) throws InterruptedException, TimeoutException {
    LOG.debug("Writing {}.", request);
    // Make the connection attempt.
    ChannelFuture connect = bootstrap.connect(host, port);
    Channel channel;
    if (connect.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
        channel = connect.channel();
    } else {
        throw new TimeoutException("Connection failed");
    }
    channel.writeAndFlush(request);
}
Also used : ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) NioSocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class StaticFileServerHandler method respondToRequest.

/**
 * Response when running with leading JobManager.
 */
private void respondToRequest(ChannelHandlerContext ctx, HttpRequest request, String requestPath) throws IOException, ParseException, URISyntaxException, RestHandlerException {
    // convert to absolute path
    final File file = new File(rootPath, requestPath);
    if (!file.exists()) {
        // file does not exist. Try to load it with the classloader
        ClassLoader cl = StaticFileServerHandler.class.getClassLoader();
        try (InputStream resourceStream = cl.getResourceAsStream("web" + requestPath)) {
            boolean success = false;
            try {
                if (resourceStream != null) {
                    URL root = cl.getResource("web");
                    URL requested = cl.getResource("web" + requestPath);
                    if (root != null && requested != null) {
                        URI rootURI = new URI(root.getPath()).normalize();
                        URI requestedURI = new URI(requested.getPath()).normalize();
                        // expected scope.
                        if (!rootURI.relativize(requestedURI).equals(requestedURI)) {
                            logger.debug("Loading missing file from classloader: {}", requestPath);
                            // ensure that directory to file exists.
                            file.getParentFile().mkdirs();
                            Files.copy(resourceStream, file.toPath());
                            success = true;
                        }
                    }
                }
            } catch (Throwable t) {
                logger.error("error while responding", t);
            } finally {
                if (!success) {
                    logger.debug("Unable to load requested file {} from classloader", requestPath);
                    throw new NotFoundException(String.format("Unable to load requested file %s.", requestPath));
                }
            }
        }
    }
    checkFileValidity(file, rootPath, logger);
    // cache validation
    final String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE);
    if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
        SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
        Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince);
        // Only compare up to the second because the datetime format we send to the client
        // does not have milliseconds
        long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
        long fileLastModifiedSeconds = file.lastModified() / 1000;
        if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
            if (logger.isDebugEnabled()) {
                logger.debug("Responding 'NOT MODIFIED' for file '" + file.getAbsolutePath() + '\'');
            }
            sendNotModified(ctx);
            return;
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Responding with file '" + file.getAbsolutePath() + '\'');
    }
    // Don't need to close this manually. Netty's DefaultFileRegion will take care of it.
    final RandomAccessFile raf;
    try {
        raf = new RandomAccessFile(file, "r");
    } catch (FileNotFoundException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Could not find file {}.", file.getAbsolutePath());
        }
        throw new NotFoundException("File not found.");
    }
    try {
        long fileLength = raf.length();
        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        setContentTypeHeader(response, file);
        setDateAndCacheHeaders(response, file);
        if (HttpHeaders.isKeepAlive(request)) {
            response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        }
        HttpHeaders.setContentLength(response, fileLength);
        // write the initial line and the header.
        ctx.write(response);
        // write the content.
        ChannelFuture lastContentFuture;
        if (ctx.pipeline().get(SslHandler.class) == null) {
            ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise());
            lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
        } else {
            lastContentFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise());
        // HttpChunkedInput will write the end marker (LastHttpContent) for us.
        }
        // close the connection, if no keep-alive is needed
        if (!HttpHeaders.isKeepAlive(request)) {
            lastContentFuture.addListener(ChannelFutureListener.CLOSE);
        }
    } catch (Exception e) {
        raf.close();
        logger.error("Failed to serve file.", e);
        throw new RestHandlerException("Internal server error.", INTERNAL_SERVER_ERROR);
    }
}
Also used : ChannelFuture(org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture) InputStream(java.io.InputStream) ChunkedFile(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedFile) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(org.apache.flink.runtime.rest.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) FullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse) HttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultFileRegion(org.apache.flink.shaded.netty4.io.netty.channel.DefaultFileRegion) URI(java.net.URI) URL(java.net.URL) Date(java.util.Date) SslHandler(org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler) URISyntaxException(java.net.URISyntaxException) NotFoundException(org.apache.flink.runtime.rest.NotFoundException) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) IOException(java.io.IOException) RestHandlerException(org.apache.flink.runtime.rest.handler.RestHandlerException) HttpChunkedInput(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpChunkedInput) RandomAccessFile(java.io.RandomAccessFile) DefaultHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ChunkedFile(org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedFile) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class StaticFileServerHandler method sendNotModified.

// ------------------------------------------------------------------------
// Utilities to encode headers and responses
// ------------------------------------------------------------------------
/**
 * Send the "304 Not Modified" response. This response can be used when the file timestamp is
 * the same as what the browser is sending up.
 *
 * @param ctx The channel context to write the response to.
 */
public static void sendNotModified(ChannelHandlerContext ctx) {
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED);
    setDateHeader(response);
    // close the connection as soon as the error message is sent.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
Also used : DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 10 with CONNECTION

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION in project flink by apache.

the class RestClientTest method testRestClientClosedHandling.

/**
 * Tests that we fail the operation if the client closes.
 */
@Test
public void testRestClientClosedHandling() throws Exception {
    final Configuration config = new Configuration();
    config.setLong(RestOptions.IDLENESS_TIMEOUT, 5000L);
    Socket connectionSocket = null;
    try (final ServerSocket serverSocket = new ServerSocket(0);
        final RestClient restClient = new RestClient(config, TestingUtils.defaultExecutor())) {
        final String targetAddress = "localhost";
        final int targetPort = serverSocket.getLocalPort();
        // start server
        final CompletableFuture<Socket> socketCompletableFuture = CompletableFuture.supplyAsync(CheckedSupplier.unchecked(() -> NetUtils.acceptWithoutTimeout(serverSocket)));
        final CompletableFuture<EmptyResponseBody> responseFuture = restClient.sendRequest(targetAddress, targetPort, new TestMessageHeaders(), EmptyMessageParameters.getInstance(), EmptyRequestBody.getInstance(), Collections.emptyList());
        try {
            connectionSocket = socketCompletableFuture.get(TIMEOUT, TimeUnit.SECONDS);
        } catch (TimeoutException ignored) {
            // could not establish a server connection --> see that the response failed
            socketCompletableFuture.cancel(true);
        }
        restClient.close();
        try {
            responseFuture.get();
        } catch (ExecutionException ee) {
            if (!ExceptionUtils.findThrowable(ee, IOException.class).isPresent()) {
                throw ee;
            }
        }
    } finally {
        if (connectionSocket != null) {
            connectionSocket.close();
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) ServerSocket(java.net.ServerSocket) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) EmptyResponseBody(org.apache.flink.runtime.rest.messages.EmptyResponseBody) ExecutionException(java.util.concurrent.ExecutionException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) TimeoutException(java.util.concurrent.TimeoutException) ConnectTimeoutException(org.apache.flink.shaded.netty4.io.netty.channel.ConnectTimeoutException) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)8 Test (org.junit.Test)7 ExecutionException (java.util.concurrent.ExecutionException)4 ByteBuf (org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf)4 Channel (org.apache.flink.shaded.netty4.io.netty.channel.Channel)4 ChannelFuture (org.apache.flink.shaded.netty4.io.netty.channel.ChannelFuture)4 SslHandler (org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler)4 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 RandomAccessFile (java.io.RandomAccessFile)3 TimeoutException (java.util.concurrent.TimeoutException)3 RemoteTransportException (org.apache.flink.runtime.io.network.netty.exception.RemoteTransportException)3 SocketChannel (org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel)3 DefaultHttpResponse (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultHttpResponse)3 HttpResponse (org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse)3 InputStream (java.io.InputStream)2 ConnectException (java.net.ConnectException)2 InetSocketAddress (java.net.InetSocketAddress)2 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2