Search in sources :

Example 6 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.

the class HttpConversionUtil method toHttpRequest.

/**
     * Create a new object to contain the request data.
     *
     * @param streamId The stream associated with the request
     * @param http2Headers The initial set of HTTP/2 headers to create the request with
     * @param validateHttpHeaders <ul>
     *        <li>{@code true} to validate HTTP headers in the http-codec</li>
     *        <li>{@code false} not to validate HTTP headers in the http-codec</li>
     *        </ul>
     * @return A new request object which represents headers for a chunked request
     * @throws Http2Exception see {@link #addHttp2ToHttpHeaders(int, Http2Headers, FullHttpMessage, boolean)}
     */
public static HttpRequest toHttpRequest(int streamId, Http2Headers http2Headers, boolean validateHttpHeaders) throws Http2Exception {
    // HTTP/2 does not define a way to carry the version identifier that is included in the HTTP/1.1 request line.
    final CharSequence method = checkNotNull(http2Headers.method(), "method header cannot be null in conversion to HTTP/1.x");
    final CharSequence path = checkNotNull(http2Headers.path(), "path header cannot be null in conversion to HTTP/1.x");
    HttpRequest msg = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(method.toString()), path.toString(), validateHttpHeaders);
    try {
        addHttp2ToHttpHeaders(streamId, http2Headers, msg.headers(), msg.protocolVersion(), false, true);
    } catch (Http2Exception e) {
        throw e;
    } catch (Throwable t) {
        throw streamError(streamId, PROTOCOL_ERROR, t, "HTTP/2 to HTTP/1.x headers conversion error");
    }
    return msg;
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest)

Example 7 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project riposte by Nike-Inc.

the class DTraceStartHandlerTest method beforeMethod.

@Before
public void beforeMethod() {
    handler = new DTraceStartHandler(userIdHeaderKeys);
    channelMock = mock(Channel.class);
    ctxMock = mock(ChannelHandlerContext.class);
    stateAttributeMock = mock(Attribute.class);
    state = new HttpProcessingState();
    httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some/uri");
    doReturn(channelMock).when(ctxMock).channel();
    doReturn(stateAttributeMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(state).when(stateAttributeMock).get();
    resetTracingAndMdc();
}
Also used : Attribute(io.netty.util.Attribute) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) Channel(io.netty.channel.Channel) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Before(org.junit.Before)

Example 8 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project vert.x by eclipse.

the class Http2ServerRequestImpl method setExpectMultipart.

@Override
public HttpServerRequest setExpectMultipart(boolean expect) {
    synchronized (conn) {
        checkEnded();
        if (expect) {
            if (postRequestDecoder == null) {
                CharSequence contentType = headers.get(HttpHeaderNames.CONTENT_TYPE);
                if (contentType != null) {
                    io.netty.handler.codec.http.HttpMethod method = io.netty.handler.codec.http.HttpMethod.valueOf(headers.method().toString());
                    String lowerCaseContentType = contentType.toString().toLowerCase();
                    boolean isURLEncoded = lowerCaseContentType.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString());
                    if ((lowerCaseContentType.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString()) || isURLEncoded) && (method == io.netty.handler.codec.http.HttpMethod.POST || method == io.netty.handler.codec.http.HttpMethod.PUT || method == io.netty.handler.codec.http.HttpMethod.PATCH || method == io.netty.handler.codec.http.HttpMethod.DELETE)) {
                        HttpRequest req = new DefaultHttpRequest(io.netty.handler.codec.http.HttpVersion.HTTP_1_1, method, headers.path().toString());
                        req.headers().add(HttpHeaderNames.CONTENT_TYPE, contentType);
                        postRequestDecoder = new HttpPostRequestDecoder(new NettyFileUploadDataFactory(vertx, this, () -> uploadHandler), req);
                    }
                }
            }
        } else {
            postRequestDecoder = null;
        }
    }
    return this;
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpPostRequestDecoder(io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)

Example 9 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project rest.li by linkedin.

the class NettyRequestAdapter method toNettyRequest.

/**
   * Adapts a StreamRequest to Netty's HttpRequest
   * @param request  R2 stream request
   * @return Adapted HttpRequest.
   */
static HttpRequest toNettyRequest(StreamRequest request) throws Exception {
    HttpMethod nettyMethod = HttpMethod.valueOf(request.getMethod());
    URL url = new URL(request.getURI().toString());
    String path = url.getFile();
    //   it MUST be given as "/" (the server root).
    if (path.isEmpty()) {
        path = "/";
    }
    HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path);
    nettyRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
        //   that contains a Transfer-Encoding header field.
        if (entry.getKey().equalsIgnoreCase(HttpHeaderNames.CONTENT_LENGTH.toString())) {
            continue;
        }
        nettyRequest.headers().set(entry.getKey(), entry.getValue());
    }
    nettyRequest.headers().set(HttpHeaderNames.HOST, url.getAuthority());
    nettyRequest.headers().set(HttpConstants.REQUEST_COOKIE_HEADER_NAME, request.getCookies());
    return nettyRequest;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) AsciiString(io.netty.util.AsciiString) Map(java.util.Map) HttpMethod(io.netty.handler.codec.http.HttpMethod) URL(java.net.URL)

Example 10 with DefaultHttpRequest

use of io.netty.handler.codec.http.DefaultHttpRequest in project netty by netty.

the class HttpPostRequestDecoderTest method testBinaryStreamUpload.

private static void testBinaryStreamUpload(boolean withSpace) throws Exception {
    final String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO";
    final String contentTypeValue;
    if (withSpace) {
        contentTypeValue = "multipart/form-data; boundary=" + boundary;
    } else {
        contentTypeValue = "multipart/form-data;boundary=" + boundary;
    }
    final DefaultHttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "http://localhost");
    req.setDecoderResult(DecoderResult.SUCCESS);
    req.headers().add(HttpHeaderNames.CONTENT_TYPE, contentTypeValue);
    req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    // Force to use memory-based data.
    final DefaultHttpDataFactory inMemoryFactory = new DefaultHttpDataFactory(false);
    for (String data : Arrays.asList("", "\r", "\r\r", "\r\r\r")) {
        final String body = "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"tmp-0.txt\"\r\n" + "Content-Type: image/gif\r\n" + "\r\n" + data + "\r\n" + "--" + boundary + "--\r\n";
        // Create decoder instance to test.
        final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(inMemoryFactory, req);
        decoder.offer(new DefaultHttpContent(Unpooled.copiedBuffer(body, CharsetUtil.UTF_8)));
        decoder.offer(new DefaultHttpContent(Unpooled.EMPTY_BUFFER));
        // Validate it's enough chunks to decode upload.
        assertTrue(decoder.hasNext());
        // Decode binary upload.
        MemoryFileUpload upload = (MemoryFileUpload) decoder.next();
        // Validate data has been parsed correctly as it was passed into request.
        assertEquals("Invalid decoded data [data=" + data.replaceAll("\r", "\\\\r") + ", upload=" + upload + ']', data, upload.getString(CharsetUtil.UTF_8));
        upload.release();
        decoder.destroy();
    }
}
Also used : DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent)

Aggregations

DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)19 HttpRequest (io.netty.handler.codec.http.HttpRequest)13 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)7 Test (org.junit.Test)6 Channel (io.netty.channel.Channel)5 ByteBuf (io.netty.buffer.ByteBuf)4 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)4 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)4 ChannelFuture (io.netty.channel.ChannelFuture)3 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)3 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 HttpMethod (io.netty.handler.codec.http.HttpMethod)2 HttpPostRequestEncoder (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder)2 AsciiString (io.netty.util.AsciiString)2 URI (java.net.URI)2 Map (java.util.Map)2