Java Code Examples for io.netty.handler.codec.http.HttpHeaders#set()
The following examples show how to use
io.netty.handler.codec.http.HttpHeaders#set() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpClientPipelineConfigurator.java From armeria with Apache License 2.0 | 6 votes |
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (needsToFilterUpgradeResponse && msg instanceof HttpResponse) { needsToFilterUpgradeResponse = false; final HttpResponse res = (HttpResponse) msg; if (res.status().code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code()) { final HttpHeaders headers = res.headers(); if (!headers.contains(HttpHeaderNames.UPGRADE)) { headers.set(HttpHeaderNames.UPGRADE, Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME); } } if (!needsToFilterUpgradeRequest) { ctx.pipeline().remove(this); } } ctx.fireChannelRead(msg); }
Example 2
Source File: HelloServerHandler.java From crow-benchmark with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf, CharSequence contentType, CharSequence contentLength) { // Decide whether to close the connection or not. boolean keepAlive = HttpHeaders.isKeepAlive(request); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf, false); HttpHeaders headers = response.headers(); headers.set(CONTENT_TYPE_ENTITY, contentType); headers.set(SERVER_ENTITY, SERVER_NAME); headers.set(DATE_ENTITY, date); headers.set(CONTENT_LENGTH_ENTITY, contentLength); // Close the non-keep-alive connection after the write operation is // done. if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { ctx.write(response, ctx.voidPromise()); } }
Example 3
Source File: HttpToHttp2ConnectionHandlerTest.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
@Test public void testOriginFormRequestTargetHandledFromUrlencodedUri() throws Exception { bootstrapEnv(2, 1, 0); final FullHttpRequest request = new DefaultFullHttpRequest( HTTP_1_1, GET, "/where%2B0?q=now%2B0&f=then%2B0#section1%2B0"); final HttpHeaders httpHeaders = request.headers(); httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 5); httpHeaders.set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), "http"); final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")) .path(new AsciiString("/where%2B0?q=now%2B0&f=then%2B0#section1%2B0")) .scheme(new AsciiString("http")); ChannelPromise writePromise = newPromise(); verifyHeadersOnly(http2Headers, writePromise, clientChannel.writeAndFlush(request, writePromise)); }
Example 4
Source File: WebSocketRequestBuilder.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
public FullHttpRequest build() { FullHttpRequest req = new DefaultFullHttpRequest(httpVersion, method, uri); HttpHeaders headers = req.headers(); if (host != null) { headers.set(Names.HOST, host); } if (upgrade != null) { headers.set(Names.UPGRADE, upgrade); } if (connection != null) { headers.set(Names.CONNECTION, connection); } if (key != null) { headers.set(Names.SEC_WEBSOCKET_KEY, key); } if (origin != null) { headers.set(Names.SEC_WEBSOCKET_ORIGIN, origin); } if (version != null) { headers.set(Names.SEC_WEBSOCKET_VERSION, version.toHttpHeaderValue()); } return req; }
Example 5
Source File: HttpProcessHandler.java From dubbo-2.6.5 with Apache License 2.0 | 5 votes |
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; }
Example 6
Source File: HttpMessageClientHeaderAdaptor.java From pinpoint with Apache License 2.0 | 5 votes |
@Override public void setHeader(HttpMessage httpMessage, String name, String value) { final HttpHeaders headers = httpMessage.headers(); if (headers != null && !headers.contains(name)) { headers.set(name, value); if (isDebug) { logger.debug("Set header {}={}", name, value); } } }
Example 7
Source File: HttpRequestClientHeaderAdaptor.java From pinpoint with Apache License 2.0 | 5 votes |
@Override public void setHeader(HttpRequest httpRequest, String name, String value) { final HttpHeaders headers = httpRequest.headers(); if (headers != null) { headers.set(name, value); if (isDebug) { logger.debug("Set header {}={}", name, value); } } }
Example 8
Source File: WebHdfsHandler.java From big-c with Apache License 2.0 | 5 votes |
private void onOpen(ChannelHandlerContext ctx) throws IOException { final String nnId = params.namenodeId(); final int bufferSize = params.bufferSize(); final long offset = params.offset(); final long length = params.length(); DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); HttpHeaders headers = response.headers(); // Allow the UI to access the file headers.set(ACCESS_CONTROL_ALLOW_METHODS, GET); headers.set(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); headers.set(CONTENT_TYPE, APPLICATION_OCTET_STREAM); headers.set(CONNECTION, CLOSE); final DFSClient dfsclient = newDfsClient(nnId, conf); HdfsDataInputStream in = dfsclient.createWrappedInputStream( dfsclient.open(path, bufferSize, true)); in.seek(offset); long contentLength = in.getVisibleLength() - offset; if (length >= 0) { contentLength = Math.min(contentLength, length); } final InputStream data; if (contentLength >= 0) { headers.set(CONTENT_LENGTH, contentLength); data = new LimitInputStream(in, contentLength); } else { data = in; } ctx.write(response); ctx.writeAndFlush(new ChunkedStream(data) { @Override public void close() throws Exception { super.close(); dfsclient.close(); } }).addListener(ChannelFutureListener.CLOSE); }
Example 9
Source File: InboundHttp2ToHttpAdapterTest.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Test public void clientRequestSingleHeaderCookieSplitIntoMultipleEntries2() throws Exception { boostrapEnv(1, 1, 1); final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/some/path/resource2", true); try { HttpHeaders httpHeaders = request.headers(); httpHeaders.set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), "https"); httpHeaders.set(HttpHeaderNames.HOST, "example.org"); httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 3); httpHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, 0); httpHeaders.set(HttpHeaderNames.COOKIE, "a=b; c=d; e=f"); httpHeaders.setShort(HttpConversionUtil.ExtensionHeaderNames.STREAM_WEIGHT.text(), (short) 16); final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")). scheme(new AsciiString("https")).authority(new AsciiString("example.org")) .path(new AsciiString("/some/path/resource2")) .add(HttpHeaderNames.COOKIE, "a=b; c=d") .add(HttpHeaderNames.COOKIE, "e=f"); runInChannel(clientChannel, new Http2Runnable() { @Override public void run() throws Http2Exception { clientHandler.encoder().writeHeaders(ctxClient(), 3, http2Headers, 0, true, newPromiseClient()); clientChannel.flush(); } }); awaitRequests(); ArgumentCaptor<FullHttpMessage> requestCaptor = ArgumentCaptor.forClass(FullHttpMessage.class); verify(serverListener).messageReceived(requestCaptor.capture()); capturedRequests = requestCaptor.getAllValues(); assertEquals(request, capturedRequests.get(0)); } finally { request.release(); } }
Example 10
Source File: NettyResponseChannelTest.java From ambry with Apache License 2.0 | 5 votes |
/** * Tests the invocation of DELAYED_CLOSE when post failures happen in {@link NettyResponseChannel}. */ @Test public void completeRequestWithDelayedCloseTest() throws Exception { Properties properties = new Properties(); long delayMs = 500; properties.setProperty(NettyConfig.NETTY_SERVER_CLOSE_DELAY_TIMEOUT_MS, String.valueOf(delayMs)); NettyConfig nettyConfig = new NettyConfig(new VerifiableProperties(properties)); MockNettyMessageProcessor processor = new MockNettyMessageProcessor(); processor.setNettyConfig(nettyConfig); ChunkedWriteHandler chunkedWriteHandler = new ChunkedWriteHandler(); EmbeddedChannel channel = new EmbeddedChannel(chunkedWriteHandler, processor); RestServiceErrorCode REST_ERROR_CODE = RestServiceErrorCode.BadRequest; String content = "@@randomContent@@@"; HttpHeaders httpHeaders = new DefaultHttpHeaders(); httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, REST_ERROR_CODE); httpHeaders.set(MockNettyMessageProcessor.INCLUDE_EXCEPTION_MESSAGE_IN_RESPONSE_HEADER_NAME, "true"); HttpRequest httpRequest = RestTestUtils.createFullRequest(HttpMethod.POST, TestingUri.OnResponseCompleteWithRestException.toString(), httpHeaders, content.getBytes()); channel.writeInbound(httpRequest); HttpResponse response = channel.readOutbound(); assertEquals("Unexpected response status", getExpectedHttpResponseStatus(REST_ERROR_CODE), response.status()); //channel should not be closed right away. assertTrue("Channel closed on the server", channel.isActive()); //wait for delayed time * 2 times (to rule out timing out on border) and then check again. Thread.sleep(delayMs * 2); channel.runPendingTasks(); assertFalse("Channel not closed on the server", channel.isActive()); assertEquals("delayed close scheduled counter mismatch", 1, processor.getNettyMetrics().delayedCloseScheduledCount.getCount()); assertEquals("delayed close executed counter mismatch", 1, processor.getNettyMetrics().delayedCloseExecutedCount.getCount()); assertEquals("delayed close expired counter mismatch", 1, processor.getNettyMetrics().delayedCloseActivatedCount.getCount()); }
Example 11
Source File: Http2StreamFrameToHttpObjectCodecTest.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Test public void testEncodeTrailersAsClient() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false)); LastHttpContent trailers = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, true); HttpHeaders headers = trailers.trailingHeaders(); headers.set("key", "value"); assertTrue(ch.writeOutbound(trailers)); Http2HeadersFrame headerFrame = ch.readOutbound(); assertThat(headerFrame.headers().get("key").toString(), is("value")); assertTrue(headerFrame.isEndStream()); assertThat(ch.readOutbound(), is(nullValue())); assertFalse(ch.finish()); }
Example 12
Source File: Http.java From xio with Apache License 2.0 | 5 votes |
public static HttpRequest post(String host, String path, String content) { HttpHeaders headers = new DefaultHttpHeaders(); headers.set(HttpHeaderNames.HOST, host); headers.set(HttpHeaderNames.CONTENT_LENGTH, content.length()); return new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, path, Unpooled.wrappedBuffer(content.getBytes()), headers, new DefaultHttpHeaders()); }
Example 13
Source File: FrontendIntegrationTest.java From ambry with Apache License 2.0 | 4 votes |
/** * Upload data chunks using chunk upload signed URL. * @param account the {@link Account} to upload into. * @param container the {@link Container} to upload into. * @param chunkSizes The sizes for each data chunk to upload. * @return the list of signed chunk IDs for the uploaded chunks and an array containing the concatenated content of * the data chunks. * @throws Exception */ private Pair<List<String>, byte[]> uploadDataChunksAndVerify(Account account, Container container, int... chunkSizes) throws Exception { IdSigningService idSigningService = new AmbryIdSigningService(); HttpHeaders chunkUploadHeaders = new DefaultHttpHeaders(); chunkUploadHeaders.add(RestUtils.Headers.URL_TYPE, RestMethod.POST.name()); chunkUploadHeaders.add(RestUtils.Headers.CHUNK_UPLOAD, "true"); setAmbryHeadersForPut(chunkUploadHeaders, TTL_SECS, !container.isCacheable(), "chunkUploader", "application/octet-stream", "stitchedUploadTest", account.getName(), container.getName()); // POST // Get signed URL FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, Operations.GET_SIGNED_URL, chunkUploadHeaders, null); ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get(); HttpResponse response = getHttpResponse(responseParts); assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status()); verifyTrackingHeaders(response); String signedPostUrl = response.headers().get(RestUtils.Headers.SIGNED_URL); assertNotNull("Did not get a signed POST URL", signedPostUrl); assertNoContent(responseParts.queue, 1); List<String> signedChunkIds = new ArrayList<>(); ByteArrayOutputStream fullContentStream = new ByteArrayOutputStream(); URI uri = new URI(signedPostUrl); for (int chunkSize : chunkSizes) { byte[] contentArray = TestUtils.getRandomBytes(chunkSize); ByteBuffer content = ByteBuffer.wrap(contentArray); // Use signed URL to POST httpRequest = buildRequest(HttpMethod.POST, uri.getPath() + "?" + uri.getQuery(), null, content); responseParts = nettyClient.sendRequest(httpRequest, null, null).get(); String signedId = verifyPostAndReturnBlobId(responseParts, chunkSize); assertTrue("Blob ID for chunk upload must be signed", idSigningService.isIdSigned(signedId.substring(1))); Pair<String, Map<String, String>> idAndMetadata = idSigningService.parseSignedId(signedId.substring(1)); // Inspect metadata fields String chunkUploadSession = idAndMetadata.getSecond().get(RestUtils.Headers.SESSION); assertNotNull("x-ambry-chunk-upload-session should be present in signed ID", chunkUploadSession); String blobSize = idAndMetadata.getSecond().get(RestUtils.Headers.BLOB_SIZE); assertNotNull("x-ambry-blob-size should be present in signed ID", blobSize); assertEquals("wrong size value in signed id", content.capacity(), Long.parseLong(blobSize)); HttpHeaders expectedGetHeaders = new DefaultHttpHeaders().add(chunkUploadHeaders); // Use signed ID and blob ID for GET request expectedGetHeaders.add(RestUtils.Headers.BLOB_SIZE, content.capacity()); // Blob TTL for chunk upload is fixed expectedGetHeaders.set(RestUtils.Headers.TTL, FRONTEND_CONFIG.chunkUploadInitialChunkTtlSecs); expectedGetHeaders.set(RestUtils.Headers.LIFE_VERSION, "0"); for (String id : new String[]{signedId, idAndMetadata.getFirst()}) { getBlobAndVerify(id, null, GetOption.None, expectedGetHeaders, !container.isCacheable(), content, account.getName(), container.getName()); getBlobInfoAndVerify(id, GetOption.None, expectedGetHeaders, !container.isCacheable(), account.getName(), container.getName(), null); } signedChunkIds.add(addClusterPrefix ? "/" + CLUSTER_NAME + signedId : signedId); fullContentStream.write(contentArray); } return new Pair<>(signedChunkIds, fullContentStream.toByteArray()); }
Example 14
Source File: ServletOutputStream.java From spring-boot-protocol with Apache License 2.0 | 4 votes |
/** * Set the response header * @param isKeepAlive keep alive * @param nettyResponse nettyResponse * @param servletRequest servletRequest * @param servletResponse servletResponse * @param sessionCookieConfig sessionCookieConfig */ private static void settingResponseHeader(boolean isKeepAlive, NettyHttpResponse nettyResponse, ServletHttpServletRequest servletRequest, ServletHttpServletResponse servletResponse, ServletSessionCookieConfig sessionCookieConfig) { HttpHeaderUtil.setKeepAlive(nettyResponse, isKeepAlive); HttpHeaders headers = nettyResponse.headers(); //Content length long contentLength = servletResponse.getContentLength(); if(contentLength >= 0) { headers.remove(HttpHeaderConstants.TRANSFER_ENCODING); headers.set(HttpHeaderConstants.CONTENT_LENGTH, contentLength); }else { nettyResponse.enableTransferEncodingChunked(); } // Time and date response header if(!headers.contains(HttpHeaderConstants.DATE)) { headers.set(HttpHeaderConstants.DATE, ServletUtil.getDateByRfcHttp()); } //Content Type The content of the response header String contentType = servletResponse.getContentType(); if (null != contentType) { String characterEncoding = servletResponse.getCharacterEncoding(); String value = (null == characterEncoding) ? contentType : RecyclableUtil.newStringBuilder() .append(contentType) .append(APPEND_CONTENT_TYPE) .append(characterEncoding).toString(); headers.set(HttpHeaderConstants.CONTENT_TYPE, value); } //Server information response header String serverHeader = servletRequest.getServletContext().getServerHeader(); if(serverHeader != null && serverHeader.length() > 0) { headers.set(HttpHeaderConstants.SERVER, serverHeader); } //language Locale locale = servletResponse.getLocale(); if(!headers.contains(HttpHeaderConstants.CONTENT_LANGUAGE)){ headers.set(HttpHeaderConstants.CONTENT_LANGUAGE,locale.toLanguageTag()); } // Cookies processing //Session is handled first. If it is a new Session and the Session id is not the same as the Session id passed by the request, it needs to be written through the Cookie List<Cookie> cookies = servletResponse.getCookies(); ServletHttpSession httpSession = servletRequest.getSession(false); if (httpSession != null && httpSession.isNew() // && !httpSession.getId().equals(servletRequest.getRequestedSessionId()) ) { String sessionCookieName = sessionCookieConfig.getName(); if(sessionCookieName == null || sessionCookieName.isEmpty()){ sessionCookieName = HttpConstants.JSESSION_ID_COOKIE; } String sessionCookiePath = sessionCookieConfig.getPath(); if(sessionCookiePath == null || sessionCookiePath.isEmpty()) { sessionCookiePath = HttpConstants.DEFAULT_SESSION_COOKIE_PATH; } String sessionCookieText = ServletUtil.encodeCookie(sessionCookieName,servletRequest.getRequestedSessionId(), -1, sessionCookiePath,sessionCookieConfig.getDomain(),sessionCookieConfig.isSecure(),Boolean.TRUE); headers.add(HttpHeaderConstants.SET_COOKIE, sessionCookieText); httpSession.setNewSessionFlag(false); } //Cookies set by other businesses or frameworks are written to the response header one by one int cookieSize = cookies.size(); if(cookieSize > 0) { for (int i=0; i<cookieSize; i++) { Cookie cookie = cookies.get(i); String cookieText = ServletUtil.encodeCookie(cookie.getName(),cookie.getValue(),cookie.getMaxAge(),cookie.getPath(),cookie.getDomain(),cookie.getSecure(),cookie.isHttpOnly()); headers.add(HttpHeaderConstants.SET_COOKIE, cookieText); } } }
Example 15
Source File: Http.java From xio with Apache License 2.0 | 4 votes |
public static HttpRequest get(String host, String path) { HttpHeaders headers = new DefaultHttpHeaders(); headers.set(HttpHeaderNames.HOST, host); headers.set(HttpHeaderNames.CONTENT_LENGTH, 0); return new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path, headers); }
Example 16
Source File: HttpPostRequestEncoder.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
/** * 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.通过准备请求中的标头完成请求,并返回准备发送的请求。一旦确定,就不需要添加任何数据。如果请求不需要chunk (isChunked() == false),此请求是发送到远程服务器的唯一对象。 * * @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 { realSize -= 1; // last '&' removed 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); } } }
Example 17
Source File: BootstrapTemplate.java From netty-cookbook with Apache License 2.0 | 4 votes |
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{ URI uri = new URI(url); String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost(); int port = uri.getPort(); if (port == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80; } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } } if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) { System.err.println("Only HTTP(S) is supported."); return; } // Configure SSL context if necessary. final boolean ssl = "https".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .handler(new HttpDownloadertInitializer(sslCtx,handler)); // Make the connection attempt. Channel ch = b.connect(host, port).sync().channel(); // Prepare the HTTP request. HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath()); HttpHeaders headers = request.headers(); headers.set(HttpHeaders.Names.HOST, host); headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE); headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP); // Set some example cookies. headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo"))); ch.writeAndFlush(request); // Wait for the server to close the connection. ch.closeFuture().sync(); Thread.sleep(1000); } finally { // Shut down executor threads to exit. group.shutdownGracefully(); } }
Example 18
Source File: BootstrapTemplate.java From netty-cookbook with Apache License 2.0 | 4 votes |
public static void newHttpClientBootstrap(String url, ChannelHandler handler) throws Exception{ URI uri = new URI(url); String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost(); int port = uri.getPort(); if (port == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80; } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } } if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) { System.err.println("Only HTTP(S) is supported."); return; } // Configure SSL context if necessary. final boolean ssl = "https".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .handler(new HttpDownloadertInitializer(sslCtx,handler)); // Make the connection attempt. Channel ch = b.connect(host, port).sync().channel(); // Prepare the HTTP request. HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath()); HttpHeaders headers = request.headers(); headers.set(HttpHeaders.Names.HOST, host); headers.set(HttpHeaders.Names.CONNECTION,HttpHeaders.Values.CLOSE); headers.set(HttpHeaders.Names.ACCEPT_ENCODING,HttpHeaders.Values.GZIP); // Set some example cookies. headers.set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(new DefaultCookie("my-cookie", "foo"))); ch.writeAndFlush(request); // Wait for the server to close the connection. ch.closeFuture().sync(); Thread.sleep(1000); } finally { // Shut down executor threads to exit. group.shutdownGracefully(); } }
Example 19
Source File: NettyResponseChannelTest.java From ambry with Apache License 2.0 | 4 votes |
/** * Tests the common workflow of the {@link NettyResponseChannel} i.e., add some content to response body via * {@link NettyResponseChannel#write(ByteBuffer, Callback)} and then complete the response. * <p/> * These responses have the header Content-Length set. * @throws Exception */ @Test public void responsesWithContentLengthTest() throws Exception { EmbeddedChannel channel = createEmbeddedChannel(); MockNettyMessageProcessor processor = channel.pipeline().get(MockNettyMessageProcessor.class); final int ITERATIONS = 10; for (int i = 0; i < ITERATIONS; i++) { boolean isKeepAlive = i != (ITERATIONS - 1); HttpHeaders httpHeaders = new DefaultHttpHeaders(); httpHeaders.set(MockNettyMessageProcessor.CHUNK_COUNT_HEADER_NAME, i); HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, TestingUri.ResponseWithContentLength.toString(), httpHeaders); HttpUtil.setKeepAlive(httpRequest, isKeepAlive); channel.writeInbound(httpRequest); verifyCallbacks(processor); // first outbound has to be response. HttpResponse response = channel.readOutbound(); assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status()); long contentLength = HttpUtil.getContentLength(response, -1); assertEquals("Unexpected Content-Length", MockNettyMessageProcessor.CHUNK.length * i, contentLength); if (contentLength == 0) { // special case. Since Content-Length is set, the response should be an instance of FullHttpResponse. assertTrue("Response not instance of FullHttpResponse", response instanceof FullHttpResponse); } else { HttpContent httpContent = null; for (int j = 0; j < i; j++) { httpContent = channel.readOutbound(); byte[] returnedContent = new byte[httpContent.content().readableBytes()]; httpContent.content().readBytes(returnedContent); httpContent.release(); assertArrayEquals("Content does not match with expected content", MockNettyMessageProcessor.CHUNK, returnedContent); } // When we know the content-length, the last httpContent would be an instance of LastHttpContent and there is no // empty last http content following it. // the last HttpContent should also be an instance of LastHttpContent assertTrue("The last part of the content is not LastHttpContent", httpContent instanceof LastHttpContent); } assertEquals("Unexpected channel state on the server", isKeepAlive, channel.isActive()); } }
Example 20
Source File: HttpClientConnect.java From reactor-netty with Apache License 2.0 | 4 votes |
Publisher<Void> requestWithBody(HttpClientOperations ch) { try { ch.resourceUrl = toURI.toExternalForm(); UriEndpoint uri = toURI; HttpHeaders headers = ch.getNettyRequest() .setUri(uri.getPathAndQuery()) .setMethod(method) .setProtocolVersion(HttpVersion.HTTP_1_1) .headers(); ch.path = HttpOperations.resolvePath(ch.uri()); if (defaultHeaders != null) { headers.set(defaultHeaders); } if (!headers.contains(HttpHeaderNames.USER_AGENT)) { headers.set(HttpHeaderNames.USER_AGENT, USER_AGENT); } SocketAddress remoteAddress = uri.getRemoteAddress(); if (!headers.contains(HttpHeaderNames.HOST)) { headers.set(HttpHeaderNames.HOST, resolveHostHeaderValue(remoteAddress)); } if (!headers.contains(HttpHeaderNames.ACCEPT)) { headers.set(HttpHeaderNames.ACCEPT, ALL); } ch.followRedirectPredicate(followRedirectPredicate); if (!Objects.equals(method, HttpMethod.GET) && !Objects.equals(method, HttpMethod.HEAD) && !Objects.equals(method, HttpMethod.DELETE) && !headers.contains(HttpHeaderNames.CONTENT_LENGTH)) { ch.chunkedTransfer(true); } ch.listener().onStateChange(ch, HttpClientState.REQUEST_PREPARED); if (websocketClientSpec != null) { Mono<Void> result = Mono.fromRunnable(() -> ch.withWebsocketSupport(websocketClientSpec, compress)); if (handler != null) { result = result.thenEmpty(Mono.fromRunnable(() -> Flux.concat(handler.apply(ch, ch)))); } return result; } Consumer<HttpClientRequest> consumer = null; if (fromURI != null && !toURI.equals(fromURI)) { if (handler instanceof RedirectSendHandler) { headers.remove(HttpHeaderNames.EXPECT) .remove(HttpHeaderNames.COOKIE) .remove(HttpHeaderNames.AUTHORIZATION) .remove(HttpHeaderNames.PROXY_AUTHORIZATION); } else { consumer = request -> request.requestHeaders() .remove(HttpHeaderNames.EXPECT) .remove(HttpHeaderNames.COOKIE) .remove(HttpHeaderNames.AUTHORIZATION) .remove(HttpHeaderNames.PROXY_AUTHORIZATION); } } if (this.redirectRequestConsumer != null) { consumer = consumer != null ? consumer.andThen(this.redirectRequestConsumer) : this.redirectRequestConsumer; } ch.redirectRequestConsumer(consumer); return handler != null ? handler.apply(ch, ch) : ch.send(); } catch (Throwable t) { return Mono.error(t); } }