Search in sources :

Example 6 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project flink by apache.

the class HttpRequestHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    try {
        if (msg instanceof HttpRequest) {
            currentRequest = (HttpRequest) msg;
            currentRequestPath = null;
            if (currentDecoder != null) {
                currentDecoder.destroy();
                currentDecoder = null;
            }
            if (currentRequest.getMethod() == HttpMethod.GET || currentRequest.getMethod() == HttpMethod.DELETE) {
                // directly delegate to the router
                ctx.fireChannelRead(currentRequest);
            } else if (currentRequest.getMethod() == HttpMethod.POST) {
                // POST comes in multiple objects. First the request, then the contents
                // keep the request and path for the remaining objects of the POST request
                currentRequestPath = new QueryStringDecoder(currentRequest.getUri(), ENCODING).path();
                currentDecoder = new HttpPostRequestDecoder(DATA_FACTORY, currentRequest, ENCODING);
            } else {
                throw new IOException("Unsupported HTTP method: " + currentRequest.getMethod().name());
            }
        } else if (currentDecoder != null && msg instanceof HttpContent) {
            // received new chunk, give it to the current decoder
            HttpContent chunk = (HttpContent) msg;
            currentDecoder.offer(chunk);
            try {
                while (currentDecoder.hasNext()) {
                    InterfaceHttpData data = currentDecoder.next();
                    if (data.getHttpDataType() == HttpDataType.FileUpload && tmpDir != null) {
                        DiskFileUpload file = (DiskFileUpload) data;
                        if (file.isCompleted()) {
                            String name = file.getFilename();
                            File target = new File(tmpDir, UUID.randomUUID() + "_" + name);
                            if (!tmpDir.exists()) {
                                logExternalUploadDirDeletion(tmpDir);
                                checkAndCreateUploadDir(tmpDir);
                            }
                            file.renameTo(target);
                            QueryStringEncoder encoder = new QueryStringEncoder(currentRequestPath);
                            encoder.addParam("filepath", target.getAbsolutePath());
                            encoder.addParam("filename", name);
                            currentRequest.setUri(encoder.toString());
                        }
                    }
                }
            } catch (EndOfDataDecoderException ignored) {
            }
            if (chunk instanceof LastHttpContent) {
                HttpRequest request = currentRequest;
                currentRequest = null;
                currentRequestPath = null;
                currentDecoder.destroy();
                currentDecoder = null;
                // fire next channel handler
                ctx.fireChannelRead(request);
            }
        } else {
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    } catch (Throwable t) {
        currentRequest = null;
        currentRequestPath = null;
        if (currentDecoder != null) {
            currentDecoder.destroy();
            currentDecoder = null;
        }
        if (ctx.channel().isActive()) {
            byte[] bytes = ExceptionUtils.stringifyException(t).getBytes(ENCODING);
            DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
            response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
            response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
            ctx.writeAndFlush(response);
        }
    }
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) DefaultFullHttpResponse(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse) EndOfDataDecoderException(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException) IOException(java.io.IOException) LastHttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent) HttpPostRequestDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) QueryStringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder) DiskFileUpload(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.DiskFileUpload) InterfaceHttpData(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData) File(java.io.File) LastHttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent) HttpContent(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent) QueryStringEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder)

Example 7 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project netty by netty.

the class HttpUploadClient method main.

public static void main(String[] args) throws Exception {
    String postSimple, postFile, get;
    if (BASE_URL.endsWith("/")) {
        postSimple = BASE_URL + "formpost";
        postFile = BASE_URL + "formpostmultipart";
        get = BASE_URL + "formget";
    } else {
        postSimple = BASE_URL + "/formpost";
        postFile = BASE_URL + "/formpostmultipart";
        get = BASE_URL + "/formget";
    }
    URI uriSimple = new URI(postSimple);
    String scheme = uriSimple.getScheme() == null ? "http" : uriSimple.getScheme();
    String host = uriSimple.getHost() == null ? "127.0.0.1" : uriSimple.getHost();
    int port = uriSimple.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }
    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;
    }
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }
    URI uriFile = new URI(postFile);
    File file = new File(FILE);
    if (!file.canRead()) {
        throw new FileNotFoundException(FILE);
    }
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    // setup the factory: here using a mixed memory/disk based on size threshold
    // Disk if MINSIZE exceed
    HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
    // should delete file on exit (in normal exit)
    DiskFileUpload.deleteOnExitTemporaryFile = true;
    // system temp directory
    DiskFileUpload.baseDirectory = null;
    // should delete file on exit (in normal exit)
    DiskAttribute.deleteOnExitTemporaryFile = true;
    // system temp directory
    DiskAttribute.baseDirectory = null;
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new HttpUploadClientInitializer(sslCtx));
        // Simple Get form: no factory used (not usable)
        List<Entry<String, String>> headers = formget(b, host, port, get, uriSimple);
        if (headers == null) {
            factory.cleanAllHttpData();
            return;
        }
        // Simple Post form: factory used for big attributes
        List<InterfaceHttpData> bodylist = formpost(b, host, port, uriSimple, file, factory, headers);
        if (bodylist == null) {
            factory.cleanAllHttpData();
            return;
        }
        // Multipart Post form: factory used
        formpostmultipart(b, host, port, uriFile, factory, headers, bodylist);
    } finally {
        // Shut down executor threads to exit.
        group.shutdownGracefully();
        // Really clean all temporary files if they still exist
        factory.cleanAllHttpData();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) URI(java.net.URI) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Entry(java.util.Map.Entry) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) Bootstrap(io.netty.bootstrap.Bootstrap) File(java.io.File) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 8 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project vert.x by eclipse.

the class Http2ServerRequest method notifyException.

private void notifyException(Throwable failure) {
    InterfaceHttpData upload = null;
    HttpEventHandler handler;
    synchronized (conn) {
        if (postRequestDecoder != null) {
            upload = postRequestDecoder.currentPartialHttpData();
        }
        handler = eventHandler;
    }
    if (handler != null) {
        handler.handleException(failure);
    }
    if (upload instanceof NettyFileUpload) {
        ((NettyFileUpload) upload).handleException(failure);
    }
}
Also used : InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData)

Example 9 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project vert.x by eclipse.

the class Http2ServerRequest method handleEnd.

void handleEnd(MultiMap trailers) {
    HttpEventHandler handler;
    synchronized (conn) {
        streamEnded = true;
        ended = true;
        if (postRequestDecoder != null) {
            try {
                postRequestDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
                while (postRequestDecoder.hasNext()) {
                    InterfaceHttpData data = postRequestDecoder.next();
                    if (data instanceof Attribute) {
                        Attribute attr = (Attribute) data;
                        try {
                            formAttributes().add(attr.getName(), attr.getValue());
                        } catch (Exception e) {
                            // Will never happen, anyway handle it somehow just in case
                            handleException(e);
                        } finally {
                            attr.release();
                        }
                    }
                }
            } catch (HttpPostRequestDecoder.EndOfDataDecoderException e) {
            // ignore this as it is expected
            } catch (Exception e) {
                handleException(e);
            } finally {
                postRequestDecoder.destroy();
            }
        }
        handler = eventHandler;
    }
    if (handler != null) {
        handler.handleEnd();
    }
}
Also used : Attribute(io.netty.handler.codec.http.multipart.Attribute) InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder) URISyntaxException(java.net.URISyntaxException) StreamResetException(io.vertx.core.http.StreamResetException) ClosedChannelException(java.nio.channels.ClosedChannelException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException)

Example 10 with InterfaceHttpData

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project cloudstack by apache.

the class HttpUploadServerHandler method readFileUploadData.

private HttpResponseStatus readFileUploadData() throws IOException {
    while (decoder.hasNext()) {
        InterfaceHttpData data = decoder.next();
        if (data != null) {
            try {
                logger.info("BODY FileUpload: " + data.getHttpDataType().name() + ": " + data);
                if (data.getHttpDataType() == HttpDataType.FileUpload) {
                    FileUpload fileUpload = (FileUpload) data;
                    if (fileUpload.isCompleted()) {
                        requestProcessed = true;
                        String format = ImageStoreUtil.checkTemplateFormat(fileUpload.getFile().getAbsolutePath(), fileUpload.getFilename());
                        if (StringUtils.isNotBlank(format)) {
                            String errorString = "File type mismatch between the sent file and the actual content. Received: " + format;
                            logger.error(errorString);
                            responseContent.append(errorString);
                            storageResource.updateStateMapWithError(uuid, errorString);
                            return HttpResponseStatus.BAD_REQUEST;
                        }
                        String status = storageResource.postUpload(uuid, fileUpload.getFile().getName(), processTimeout);
                        if (status != null) {
                            responseContent.append(status);
                            storageResource.updateStateMapWithError(uuid, status);
                            return HttpResponseStatus.INTERNAL_SERVER_ERROR;
                        } else {
                            responseContent.append("upload successful.");
                            return HttpResponseStatus.OK;
                        }
                    }
                }
            } finally {
                data.release();
            }
        }
    }
    responseContent.append("received entity is not a file");
    return HttpResponseStatus.UNPROCESSABLE_ENTITY;
}
Also used : InterfaceHttpData(io.netty.handler.codec.http.multipart.InterfaceHttpData) DiskFileUpload(io.netty.handler.codec.http.multipart.DiskFileUpload) FileUpload(io.netty.handler.codec.http.multipart.FileUpload)

Aggregations

InterfaceHttpData (io.netty.handler.codec.http.multipart.InterfaceHttpData)24 Attribute (io.netty.handler.codec.http.multipart.Attribute)9 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)9 IOException (java.io.IOException)9 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)7 ArrayList (java.util.ArrayList)7 List (java.util.List)5 DiskFileUpload (io.netty.handler.codec.http.multipart.DiskFileUpload)4 Test (org.junit.Test)4 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)3 File (java.io.File)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)2 DefaultHttpDataFactory (io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)2 HttpDataFactory (io.netty.handler.codec.http.multipart.HttpDataFactory)2 HttpPostMultipartRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostMultipartRequestDecoder)2 EndOfDataDecoderException (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.EndOfDataDecoderException)2 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)2 StreamResetException (io.vertx.core.http.StreamResetException)2