Search in sources :

Example 76 with HttpHeaders

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

the class HttpServletRequestWrapperForRequestInfoTest method getHeaderNames_delegates_to_requestInfo_headers.

@Test
public void getHeaderNames_delegates_to_requestInfo_headers() {
    // given
    HttpHeaders headersMock = mock(HttpHeaders.class);
    Set<String> headerNames = new LinkedHashSet<>(Arrays.asList(UUID.randomUUID().toString(), UUID.randomUUID().toString()));
    doReturn(headerNames).when(headersMock).names();
    doReturn(headersMock).when(requestInfoMock).getHeaders();
    // when
    Enumeration<String> result = wrapper.getHeaderNames();
    // then
    verify(headersMock).names();
    assertThat(new LinkedHashSet<>(Collections.list(result))).isEqualTo(headerNames);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Example 77 with HttpHeaders

use of io.netty.handler.codec.http.HttpHeaders in project dubbo by alibaba.

the class HttpProcessHandler method http_404.

private static final FullHttpResponse http_404() {
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    HttpHeaders httpHeaders = response.headers();
    httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
    httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
    return response;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 78 with HttpHeaders

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

the class ClientConnection method toWebSocket.

synchronized void toWebSocket(String requestURI, MultiMap headers, WebsocketVersion vers, String subProtocols, int maxWebSocketFrameSize, Handler<WebSocket> wsConnect) {
    if (ws != null) {
        throw new IllegalStateException("Already websocket");
    }
    try {
        URI wsuri = new URI(requestURI);
        if (!wsuri.isAbsolute()) {
            // Netty requires an absolute url
            wsuri = new URI((ssl ? "https:" : "http:") + "//" + host + ":" + port + requestURI);
        }
        WebSocketVersion version = WebSocketVersion.valueOf((vers == null ? WebSocketVersion.V13 : vers).toString());
        HttpHeaders nettyHeaders;
        if (headers != null) {
            nettyHeaders = new DefaultHttpHeaders();
            for (Map.Entry<String, String> entry : headers) {
                nettyHeaders.add(entry.getKey(), entry.getValue());
            }
        } else {
            nettyHeaders = null;
        }
        handshaker = WebSocketClientHandshakerFactory.newHandshaker(wsuri, version, subProtocols, false, nettyHeaders, maxWebSocketFrameSize, !client.getOptions().isSendUnmaskedFrames(), false);
        ChannelPipeline p = channel.pipeline();
        p.addBefore("handler", "handshakeCompleter", new HandshakeInboundHandler(wsConnect, version != WebSocketVersion.V00));
        handshaker.handshake(channel).addListener(future -> {
            Handler<Throwable> handler = exceptionHandler();
            if (!future.isSuccess() && handler != null) {
                handler.handle(future.cause());
            }
        });
    } catch (Exception e) {
        handleException(e);
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) URI(java.net.URI) VertxException(io.vertx.core.VertxException) MultiMap(io.vertx.core.MultiMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 79 with HttpHeaders

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

the class ClientConnection method prepareHeaders.

private void prepareHeaders(HttpRequest request, String hostHeader, boolean chunked) {
    HttpHeaders headers = request.headers();
    headers.remove(TRANSFER_ENCODING);
    if (!headers.contains(HOST)) {
        request.headers().set(HOST, hostHeader);
    }
    if (chunked) {
        HttpHeaders.setTransferEncodingChunked(request);
    }
    if (client.getOptions().isTryUseCompression() && request.headers().get(ACCEPT_ENCODING) == null) {
        // if compression should be used but nothing is specified by the user support deflate and gzip.
        request.headers().set(ACCEPT_ENCODING, DEFLATE_GZIP);
    }
    if (!client.getOptions().isKeepAlive() && client.getOptions().getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_1) {
        request.headers().set(CONNECTION, CLOSE);
    } else if (client.getOptions().isKeepAlive() && client.getOptions().getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_0) {
        request.headers().set(CONNECTION, KEEP_ALIVE);
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders)

Example 80 with HttpHeaders

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

the class HttpPostRequestEncoder method finalizeRequest.

/**
     * Finalize the request by preparing the Header in the request and returns the request ready to be sent.<br>
     * Once finalized, no data must be added.<br>
     * If the request does not need chunk (isChunked() == false), this request is the only object to send to the remote
     * server.
     *
     * @return the request object (chunked or not according to size of body)
     * @throws ErrorDataEncoderException
     *             if the encoding is in error or if the finalize were already done
     */
public HttpRequest finalizeRequest() throws ErrorDataEncoderException {
    // Finalize the multipartHttpDatas
    if (!headerFinalized) {
        if (isMultipart) {
            InternalAttribute internal = new InternalAttribute(charset);
            if (duringMixedMode) {
                internal.addValue("\r\n--" + multipartMixedBoundary + "--");
            }
            internal.addValue("\r\n--" + multipartDataBoundary + "--\r\n");
            multipartHttpDatas.add(internal);
            multipartMixedBoundary = null;
            currentFileUpload = null;
            duringMixedMode = false;
            globalBodySize += internal.size();
        }
        headerFinalized = true;
    } else {
        throw new ErrorDataEncoderException("Header already encoded");
    }
    HttpHeaders headers = request.headers();
    List<String> contentTypes = headers.getAll(HttpHeaderNames.CONTENT_TYPE);
    List<String> transferEncoding = headers.getAll(HttpHeaderNames.TRANSFER_ENCODING);
    if (contentTypes != null) {
        headers.remove(HttpHeaderNames.CONTENT_TYPE);
        for (String contentType : contentTypes) {
            // "multipart/form-data; boundary=--89421926422648"
            String lowercased = contentType.toLowerCase();
            if (lowercased.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString()) || lowercased.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString())) {
            // ignore
            } else {
                headers.add(HttpHeaderNames.CONTENT_TYPE, contentType);
            }
        }
    }
    if (isMultipart) {
        String value = HttpHeaderValues.MULTIPART_FORM_DATA + "; " + HttpHeaderValues.BOUNDARY + '=' + multipartDataBoundary;
        headers.add(HttpHeaderNames.CONTENT_TYPE, value);
    } else {
        // Not multipart
        headers.add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED);
    }
    // Now consider size for chunk or not
    long realSize = globalBodySize;
    if (isMultipart) {
        iterator = multipartHttpDatas.listIterator();
    } else {
        // last '&' removed
        realSize -= 1;
        iterator = multipartHttpDatas.listIterator();
    }
    headers.set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(realSize));
    if (realSize > HttpPostBodyUtil.chunkSize || isMultipart) {
        isChunked = true;
        if (transferEncoding != null) {
            headers.remove(HttpHeaderNames.TRANSFER_ENCODING);
            for (CharSequence v : transferEncoding) {
                if (HttpHeaderValues.CHUNKED.contentEqualsIgnoreCase(v)) {
                // ignore
                } else {
                    headers.add(HttpHeaderNames.TRANSFER_ENCODING, v);
                }
            }
        }
        HttpUtil.setTransferEncodingChunked(request, true);
        // wrap to hide the possible content
        return new WrappedHttpRequest(request);
    } else {
        // get the only one body and set it to the request
        HttpContent chunk = nextChunk();
        if (request instanceof FullHttpRequest) {
            FullHttpRequest fullRequest = (FullHttpRequest) request;
            ByteBuf chunkContent = chunk.content();
            if (fullRequest.content() != chunkContent) {
                fullRequest.content().clear().writeBytes(chunkContent);
                chunkContent.release();
            }
            return fullRequest;
        } else {
            return new WrappedFullHttpRequest(request, chunk);
        }
    }
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent)

Aggregations

HttpHeaders (io.netty.handler.codec.http.HttpHeaders)128 Test (org.junit.Test)61 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)49 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)42 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)34 AsciiString (io.netty.util.AsciiString)27 Test (org.testng.annotations.Test)25 ByteBuf (io.netty.buffer.ByteBuf)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 ChannelPromise (io.netty.channel.ChannelPromise)15 FullHttpMessage (io.netty.handler.codec.http.FullHttpMessage)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 Cookie (io.netty.handler.codec.http.cookie.Cookie)12 DefaultCookie (io.netty.handler.codec.http.cookie.DefaultCookie)12 Map (java.util.Map)12 HttpTest (org.asynchttpclient.testserver.HttpTest)12 Http2CodecUtil.getEmbeddedHttp2Exception (io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception)11 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)11 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)10 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)10