Search in sources :

Example 1 with NettyBody

use of org.asynchttpclient.netty.request.body.NettyBody in project async-http-client by AsyncHttpClient.

the class NettyRequestFactory method body.

private NettyBody body(Request request, boolean connect) {
    NettyBody nettyBody = null;
    if (!connect) {
        Charset bodyCharset = withDefault(request.getCharset(), DEFAULT_CHARSET);
        if (request.getByteData() != null) {
            nettyBody = new NettyByteArrayBody(request.getByteData());
        } else if (request.getCompositeByteData() != null) {
            nettyBody = new NettyCompositeByteArrayBody(request.getCompositeByteData());
        } else if (request.getStringData() != null) {
            nettyBody = new NettyByteBufferBody(StringUtils.charSequence2ByteBuffer(request.getStringData(), bodyCharset));
        } else if (request.getByteBufferData() != null) {
            nettyBody = new NettyByteBufferBody(request.getByteBufferData());
        } else if (request.getStreamData() != null) {
            nettyBody = new NettyInputStreamBody(request.getStreamData());
        } else if (isNonEmpty(request.getFormParams())) {
            CharSequence contentType = null;
            if (!request.getHeaders().contains(CONTENT_TYPE)) {
                contentType = HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED;
            }
            nettyBody = new NettyByteBufferBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentType);
        } else if (isNonEmpty(request.getBodyParts())) {
            nettyBody = new NettyMultipartBody(request.getBodyParts(), request.getHeaders(), config);
        } else if (request.getFile() != null) {
            nettyBody = new NettyFileBody(request.getFile(), config);
        } else if (request.getBodyGenerator() instanceof FileBodyGenerator) {
            FileBodyGenerator fileBodyGenerator = (FileBodyGenerator) request.getBodyGenerator();
            nettyBody = new NettyFileBody(fileBodyGenerator.getFile(), fileBodyGenerator.getRegionSeek(), fileBodyGenerator.getRegionLength(), config);
        } else if (request.getBodyGenerator() instanceof InputStreamBodyGenerator) {
            InputStreamBodyGenerator inStreamGenerator = InputStreamBodyGenerator.class.cast(request.getBodyGenerator());
            nettyBody = new NettyInputStreamBody(inStreamGenerator.getInputStream(), inStreamGenerator.getContentLength());
        } else if (request.getBodyGenerator() instanceof ReactiveStreamsBodyGenerator) {
            ReactiveStreamsBodyGenerator reactiveStreamsBodyGenerator = (ReactiveStreamsBodyGenerator) request.getBodyGenerator();
            nettyBody = new NettyReactiveStreamsBody(reactiveStreamsBodyGenerator.getPublisher(), reactiveStreamsBodyGenerator.getContentLength());
        } else if (request.getBodyGenerator() != null) {
            nettyBody = new NettyBodyBody(request.getBodyGenerator().createBody(), config);
        }
    }
    return nettyBody;
}
Also used : InputStreamBodyGenerator(org.asynchttpclient.request.body.generator.InputStreamBodyGenerator) NettyMultipartBody(org.asynchttpclient.netty.request.body.NettyMultipartBody) NettyInputStreamBody(org.asynchttpclient.netty.request.body.NettyInputStreamBody) Charset(java.nio.charset.Charset) NettyCompositeByteArrayBody(org.asynchttpclient.netty.request.body.NettyCompositeByteArrayBody) FileBodyGenerator(org.asynchttpclient.request.body.generator.FileBodyGenerator) NettyByteBufferBody(org.asynchttpclient.netty.request.body.NettyByteBufferBody) NettyByteArrayBody(org.asynchttpclient.netty.request.body.NettyByteArrayBody) ReactiveStreamsBodyGenerator(org.asynchttpclient.request.body.generator.ReactiveStreamsBodyGenerator) NettyBodyBody(org.asynchttpclient.netty.request.body.NettyBodyBody) NettyBody(org.asynchttpclient.netty.request.body.NettyBody) NettyFileBody(org.asynchttpclient.netty.request.body.NettyFileBody) NettyReactiveStreamsBody(org.asynchttpclient.netty.request.body.NettyReactiveStreamsBody)

Example 2 with NettyBody

use of org.asynchttpclient.netty.request.body.NettyBody in project async-http-client by AsyncHttpClient.

the class NettyRequestFactory method newNettyRequest.

public NettyRequest newNettyRequest(Request request, boolean forceConnect, ProxyServer proxyServer, Realm realm, Realm proxyRealm) {
    Uri uri = request.getUri();
    HttpMethod method = forceConnect ? HttpMethod.CONNECT : HttpMethod.valueOf(request.getMethod());
    boolean connect = method == HttpMethod.CONNECT;
    HttpVersion httpVersion = HttpVersion.HTTP_1_1;
    String requestUri = requestUri(uri, proxyServer, connect);
    NettyBody body = body(request, connect);
    HttpRequest httpRequest;
    NettyRequest nettyRequest;
    if (body instanceof NettyDirectBody) {
        ByteBuf buf = NettyDirectBody.class.cast(body).byteBuf();
        httpRequest = new DefaultFullHttpRequest(httpVersion, method, requestUri, buf);
        // body is passed as null as it's written directly with the request
        nettyRequest = new NettyRequest(httpRequest, null);
    } else if (body == null) {
        httpRequest = new DefaultFullHttpRequest(httpVersion, method, requestUri, Unpooled.EMPTY_BUFFER);
        nettyRequest = new NettyRequest(httpRequest, null);
    } else {
        httpRequest = new DefaultHttpRequest(httpVersion, method, requestUri);
        nettyRequest = new NettyRequest(httpRequest, body);
    }
    HttpHeaders headers = httpRequest.headers();
    if (connect) {
        // assign proxy-auth as configured on request
        headers.set(PROXY_AUTHORIZATION, request.getHeaders().getAll(PROXY_AUTHORIZATION));
    } else {
        // assign headers as configured on request
        headers.set(request.getHeaders());
        if (isNonEmpty(request.getCookies()))
            headers.set(COOKIE, ClientCookieEncoder.STRICT.encode(request.getCookies()));
        String userDefinedAcceptEncoding = headers.get(ACCEPT_ENCODING);
        if (userDefinedAcceptEncoding != null) {
            // we don't support Brotly ATM
            if (userDefinedAcceptEncoding.endsWith(BROTLY_ACCEPT_ENCODING_SUFFIX)) {
                headers.set(ACCEPT_ENCODING, userDefinedAcceptEncoding.subSequence(0, userDefinedAcceptEncoding.length() - BROTLY_ACCEPT_ENCODING_SUFFIX.length()));
            }
        } else if (config.isCompressionEnforced()) {
            headers.set(ACCEPT_ENCODING, GZIP_DEFLATE);
        }
    }
    if (body != null) {
        if (body.getContentLength() < 0)
            headers.set(TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
        else
            headers.set(CONTENT_LENGTH, body.getContentLength());
        if (body.getContentType() != null)
            headers.set(CONTENT_TYPE, body.getContentType());
    }
    // connection header and friends
    if (!connect && uri.isWebSocket()) {
        //
        headers.set(UPGRADE, HttpHeaderValues.WEBSOCKET).set(CONNECTION, //
        HttpHeaderValues.UPGRADE).set(ORIGIN, //
        "http://" + uri.getHost() + ":" + uri.getExplicitPort()).set(SEC_WEBSOCKET_KEY, //
        getKey()).set(SEC_WEBSOCKET_VERSION, "13");
    } else if (!headers.contains(CONNECTION)) {
        CharSequence connectionHeaderValue = connectionHeader(config.isKeepAlive(), httpVersion);
        if (connectionHeaderValue != null)
            headers.set(CONNECTION, connectionHeaderValue);
    }
    if (!headers.contains(HOST))
        headers.set(HOST, hostHeader(request, uri));
    // don't override authorization but append
    addAuthorizationHeader(headers, perRequestAuthorizationHeader(realm));
    // only set proxy auth on request over plain HTTP, or when performing CONNECT
    if (!uri.isSecured() || connect) {
        setProxyAuthorizationHeader(headers, perRequestProxyAuthorizationHeader(proxyRealm));
    }
    // Add default accept headers
    if (!headers.contains(ACCEPT))
        headers.set(ACCEPT, "*/*");
    // Add default user agent
    if (!headers.contains(USER_AGENT) && config.getUserAgent() != null)
        headers.set(USER_AGENT, config.getUserAgent());
    return nettyRequest;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) Uri(org.asynchttpclient.uri.Uri) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) NettyBody(org.asynchttpclient.netty.request.body.NettyBody) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) NettyDirectBody(org.asynchttpclient.netty.request.body.NettyDirectBody)

Aggregations

NettyBody (org.asynchttpclient.netty.request.body.NettyBody)2 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 HttpVersion (io.netty.handler.codec.http.HttpVersion)1 Charset (java.nio.charset.Charset)1 NettyBodyBody (org.asynchttpclient.netty.request.body.NettyBodyBody)1 NettyByteArrayBody (org.asynchttpclient.netty.request.body.NettyByteArrayBody)1 NettyByteBufferBody (org.asynchttpclient.netty.request.body.NettyByteBufferBody)1 NettyCompositeByteArrayBody (org.asynchttpclient.netty.request.body.NettyCompositeByteArrayBody)1 NettyDirectBody (org.asynchttpclient.netty.request.body.NettyDirectBody)1 NettyFileBody (org.asynchttpclient.netty.request.body.NettyFileBody)1 NettyInputStreamBody (org.asynchttpclient.netty.request.body.NettyInputStreamBody)1 NettyMultipartBody (org.asynchttpclient.netty.request.body.NettyMultipartBody)1 NettyReactiveStreamsBody (org.asynchttpclient.netty.request.body.NettyReactiveStreamsBody)1 FileBodyGenerator (org.asynchttpclient.request.body.generator.FileBodyGenerator)1 InputStreamBodyGenerator (org.asynchttpclient.request.body.generator.InputStreamBodyGenerator)1