Search in sources :

Example 6 with QueryStringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder 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 QueryStringEncoder

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

the class HttpUploadClient method formget.

/**
 * Standard usage of HTTP API in Netty without file Upload (get is not able to achieve File upload
 * due to limitation on request size).
 *
 * @return the list of headers that will be used in every example after
 */
private static List<Entry<String, String>> formget(Bootstrap bootstrap, String host, int port, String get, URI uriSimple) throws Exception {
    // XXX /formget
    // No use of HttpPostRequestEncoder since not a POST
    Channel channel = bootstrap.connect(host, port).sync().channel();
    // Prepare the HTTP request.
    QueryStringEncoder encoder = new QueryStringEncoder(get);
    // add Form attribute
    encoder.addParam("getform", "GET");
    encoder.addParam("info", "first value");
    encoder.addParam("secondinfo", "secondvalue ���&");
    // not the big one since it is not compatible with GET size
    // encoder.addParam("thirdinfo", textArea);
    encoder.addParam("thirdinfo", "third value\r\ntest second line\r\n\r\nnew line\r\n");
    encoder.addParam("Send", "Send");
    URI uriGet = new URI(encoder.toString());
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uriGet.toASCIIString());
    HttpHeaders headers = request.headers();
    headers.set(HttpHeaderNames.HOST, host);
    headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    headers.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP + "," + HttpHeaderValues.DEFLATE);
    headers.set(HttpHeaderNames.ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    headers.set(HttpHeaderNames.ACCEPT_LANGUAGE, "fr");
    headers.set(HttpHeaderNames.REFERER, uriSimple.toString());
    headers.set(HttpHeaderNames.USER_AGENT, "Netty Simple Http Client side");
    headers.set(HttpHeaderNames.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    // connection will not close but needed
    // headers.set("Connection","keep-alive");
    // headers.set("Keep-Alive","300");
    headers.set(HttpHeaderNames.COOKIE, ClientCookieEncoder.STRICT.encode(new DefaultCookie("my-cookie", "foo"), new DefaultCookie("another-cookie", "bar")));
    // send request
    channel.writeAndFlush(request);
    // Wait for the server to close the connection.
    channel.closeFuture().sync();
    // convert headers to list
    return headers.entries();
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultCookie(io.netty.handler.codec.http.cookie.DefaultCookie) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) URI(java.net.URI) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Example 8 with QueryStringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder in project zipkin by openzipkin.

the class ServerIntegratedBenchmark method prometheusValue.

static String prometheusValue(WebClient prometheus, String query) throws Exception {
    QueryStringEncoder encoder = new QueryStringEncoder("/api/v1/query");
    encoder.addParam("query", query);
    String response = prometheus.get(encoder.toString()).aggregate().join().contentUtf8();
    return OBJECT_MAPPER.readTree(response).at("/data/result/0/value/1").asText();
}
Also used : QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Example 9 with QueryStringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder in project hono by eclipse.

the class MqttPublishTestBase method getTopicWithPropertyBag.

/**
 * Creates a topic String with contained property bag properties.
 *
 * @param topicWithoutPropertyBag The topic prefix.
 * @param propertyBag The properties.
 * @return The topic string.
 */
protected static final String getTopicWithPropertyBag(final String topicWithoutPropertyBag, final Map<String, String> propertyBag) {
    if (propertyBag == null || propertyBag.isEmpty()) {
        return topicWithoutPropertyBag;
    }
    final QueryStringEncoder encoder = new QueryStringEncoder(topicWithoutPropertyBag + "/");
    propertyBag.forEach(encoder::addParam);
    return encoder.toString();
}
Also used : QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Example 10 with QueryStringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringEncoder in project curiostack by curioswitch.

the class ServiceAccountAccessTokenProvider method refreshRequestContent.

@Override
ByteBuf refreshRequestContent(Type type) {
    long currentTimeMillis = clock().millis();
    String assertion = createAssertion(type, currentTimeMillis);
    QueryStringEncoder formEncoder = new QueryStringEncoder("");
    formEncoder.addParam("grant_type", GRANT_TYPE);
    formEncoder.addParam("assertion", assertion);
    String contentWithQuestionMark = formEncoder.toString();
    ByteBufAllocator alloc = RequestContext.mapCurrent(RequestContext::alloc, () -> PooledByteBufAllocator.DEFAULT);
    assert alloc != null;
    ByteBuf content = alloc.buffer(contentWithQuestionMark.length() - 1);
    ByteBufUtil.writeAscii(content, contentWithQuestionMark.substring(1));
    return content;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) RequestContext(com.linecorp.armeria.common.RequestContext) ByteBuf(io.netty.buffer.ByteBuf) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Aggregations

QueryStringEncoder (io.netty.handler.codec.http.QueryStringEncoder)18 MultiMap (io.vertx.core.MultiMap)6 ByteBuf (io.netty.buffer.ByteBuf)4 HttpRequest (io.netty.handler.codec.http.HttpRequest)4 HttpMethod (io.vertx.core.http.HttpMethod)4 RequestParameters (io.vertx.ext.web.api.RequestParameters)4 BodyHandler (io.vertx.ext.web.handler.BodyHandler)4 URLEncoder (java.net.URLEncoder)4 Test (org.junit.Test)4 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)3 RequestContext (com.linecorp.armeria.common.RequestContext)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)2 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)2 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)2 HttpObject (io.netty.handler.codec.http.HttpObject)2 HttpClientRequest (io.vertx.core.http.HttpClientRequest)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 URI (java.net.URI)2 QueryParam (javax.ws.rs.QueryParam)2