Search in sources :

Example 26 with QueryStringDecoder

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

the class ApolloRequestHandlerTest method mockRequest.

private MockHttpServletRequest mockRequest(String method, String requestURI, Map<String, String> headers) {
    QueryStringDecoder decoder = new QueryStringDecoder(requestURI);
    final MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest(method, decoder.path());
    mockHttpServletRequest.setParameters(decoder.parameters().entrySet().stream().collect(toMap(Map.Entry::getKey, e -> {
        List<String> value = e.getValue();
        return value.toArray(new String[value.size()]);
    })));
    mockHttpServletRequest.setQueryString(requestURI.replace(decoder.path() + "?", ""));
    headers.forEach(mockHttpServletRequest::addHeader);
    mockHttpServletRequest.setProtocol("HTTP/1.1");
    mockHttpServletRequest.setRemoteHost("123.45.67.89");
    mockHttpServletRequest.setRemoteAddr("123.45.67.89");
    mockHttpServletRequest.setRemotePort(8734);
    return mockHttpServletRequest;
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ByteString(okio.ByteString)

Example 27 with QueryStringDecoder

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

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

the class Http2RequestHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
    QueryStringDecoder queryString = new QueryStringDecoder(request.uri());
    String streamId = streamId(request);
    int latency = toInt(firstValue(queryString, LATENCY_FIELD_NAME), 0);
    if (latency < MIN_LATENCY || latency > MAX_LATENCY) {
        sendBadRequest(ctx, streamId);
        return;
    }
    String x = firstValue(queryString, IMAGE_COORDINATE_X);
    String y = firstValue(queryString, IMAGE_COORDINATE_Y);
    if (x == null || y == null) {
        handlePage(ctx, streamId, latency, request);
    } else {
        handleImage(x, y, ctx, streamId, latency, request);
    }
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder)

Example 29 with QueryStringDecoder

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

the class DefaultHttpRequest method newRequest.

public static HttpRequest newRequest(final FullHttpRequest request) {
    QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
    ImmutableMap<String, String[]> queries = toQueries(decoder);
    return builder().withVersion(HttpProtocolVersion.versionOf(request.protocolVersion().text())).withHeaders(toHeaders(request)).withMethod(HttpMethod.valueOf(request.method().toString().toUpperCase())).withUri(decoder.path()).withQueries(queries).withContent(toMessageContent(request)).build();
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder)

Example 30 with QueryStringDecoder

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

the class SqlHttpHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
    if (request.uri().startsWith("/_sql")) {
        Session session = ensureSession(request);
        Map<String, List<String>> parameters = new QueryStringDecoder(request.uri()).parameters();
        ByteBuf content = request.content();
        handleSQLRequest(session, content, paramContainFlag(parameters, "types")).whenComplete((result, t) -> {
            try {
                sendResponse(session, ctx, request, parameters, result, t);
            } catch (Throwable ex) {
                LOGGER.error("Error sending response", ex);
                throw ex;
            } finally {
                request.release();
            }
        });
    } else {
        ctx.fireChannelRead(request);
    }
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) Session(io.crate.action.sql.Session)

Aggregations

QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)69 List (java.util.List)29 Test (org.junit.Test)15 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 Map (java.util.Map)10 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 URI (java.net.URI)7 ByteBuf (io.netty.buffer.ByteBuf)6 HttpContent (io.netty.handler.codec.http.HttpContent)6 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)6 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)6 ArrayList (java.util.ArrayList)6 HttpMethod (io.netty.handler.codec.http.HttpMethod)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 DeviceSession (org.traccar.DeviceSession)5 Position (org.traccar.model.Position)5 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)4 Channel (io.netty.channel.Channel)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3