Java Code Examples for io.netty.handler.codec.http.HttpUtil#setContentLength()
The following examples show how to use
io.netty.handler.codec.http.HttpUtil#setContentLength() .
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: ModelServerTest.java From multi-model-server with Apache License 2.0 | 6 votes |
private void testPredictionsModifyResponseHeader( Channel inferChannel, Channel managementChannel) throws NoSuchFieldException, IllegalAccessException, InterruptedException { setConfiguration("decode_input_request", "false"); loadTests(managementChannel, "respheader-test", "respheader"); result = null; latch = new CountDownLatch(1); DefaultFullHttpRequest req = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "/predictions/respheader"); req.content().writeCharSequence("{\"data\": \"test\"}", CharsetUtil.UTF_8); HttpUtil.setContentLength(req, req.content().readableBytes()); req.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON); inferChannel.writeAndFlush(req); latch.await(); Assert.assertEquals(httpStatus, HttpResponseStatus.OK); Assert.assertEquals(headers.get("dummy"), "1"); Assert.assertEquals(headers.get("content-type"), "text/plain"); Assert.assertTrue(result.contains("bytearray")); unloadTests(managementChannel, "respheader"); }
Example 2
Source File: ModelServerTest.java From multi-model-server with Apache License 2.0 | 6 votes |
private void testPredictionsNoManifest(Channel inferChannel, Channel mgmtChannel) throws InterruptedException, NoSuchFieldException, IllegalAccessException { setConfiguration("default_service_handler", "service:handle"); loadTests(mgmtChannel, "noop-no-manifest", "nomanifest"); result = null; latch = new CountDownLatch(1); DefaultFullHttpRequest req = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "/predictions/nomanifest"); req.content().writeCharSequence("{\"data\": \"test\"}", CharsetUtil.UTF_8); HttpUtil.setContentLength(req, req.content().readableBytes()); req.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON); inferChannel.writeAndFlush(req); latch.await(); Assert.assertEquals(httpStatus, HttpResponseStatus.OK); Assert.assertEquals(result, "OK"); unloadTests(mgmtChannel, "nomanifest"); }
Example 3
Source File: SnapshotTest.java From serve with Apache License 2.0 | 6 votes |
@Test( alwaysRun = true, dependsOnMethods = {"testLoadModelWithInitialWorkersSnapshot"}) public void testNoSnapshotOnPrediction() throws InterruptedException { Channel channel = TestUtils.getInferenceChannel(configManager); TestUtils.setResult(null); TestUtils.setLatch(new CountDownLatch(1)); String requestURL = "/predictions/noop_v1.0"; DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, requestURL); req.content().writeCharSequence("data=test", CharsetUtil.UTF_8); HttpUtil.setContentLength(req, req.content().readableBytes()); req.headers() .set( HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED); channel.writeAndFlush(req); }
Example 4
Source File: ModelServerTest.java From serve with Apache License 2.0 | 6 votes |
@Test( alwaysRun = true, dependsOnMethods = {"testSetDefaultVersionNoop"}) public void testLoadModelWithInitialWorkersWithJSONReqBody() throws InterruptedException { Channel channel = TestUtils.getManagementChannel(configManager); testUnregisterModel("noop", null); TestUtils.setResult(null); TestUtils.setLatch(new CountDownLatch(1)); DefaultFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/models"); req.headers().add("Content-Type", "application/json"); req.content() .writeCharSequence( "{'url':'noop.mar', 'model_name':'noop', 'initial_workers':'1', 'synchronous':'true'}", CharsetUtil.UTF_8); HttpUtil.setContentLength(req, req.content().readableBytes()); channel.writeAndFlush(req); TestUtils.getLatch().await(); StatusResponse resp = JsonUtils.GSON.fromJson(TestUtils.getResult(), StatusResponse.class); Assert.assertEquals( resp.getStatus(), "Model \"noop\" Version: 1.11 registered with 1 initial workers"); }
Example 5
Source File: HttpResourceHander.java From ext-opensource-netty with Mozilla Public License 2.0 | 6 votes |
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). int statusCode = res.status().code(); if (statusCode != HttpResponseStatus.OK.code() && res.content().readableBytes() == 0) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); } HttpUtil.setContentLength(res, res.content().readableBytes()); // Send the response and close the connection if necessary. if (!HttpUtil.isKeepAlive(req) || statusCode != HttpResponseStatus.OK.code()) { res.headers().set(CONNECTION, CLOSE); ctx.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); } else { res.headers().set(CONNECTION, CLOSE); ctx.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); } }
Example 6
Source File: WebSocketServerHandler.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
private static void sendHttpResponse( ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
Example 7
Source File: ModelServerTest.java From serve with Apache License 2.0 | 6 votes |
@Test( alwaysRun = true, dependsOnMethods = {"testInvocationsMultipart"}) public void testModelsInvokeJson() throws InterruptedException { Channel channel = TestUtils.getInferenceChannel(configManager); TestUtils.setResult(null); TestUtils.setLatch(new CountDownLatch(1)); DefaultFullHttpRequest req = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "/models/noop/invoke"); req.content().writeCharSequence("{\"data\": \"test\"}", CharsetUtil.UTF_8); HttpUtil.setContentLength(req, req.content().readableBytes()); req.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON); channel.writeAndFlush(req); TestUtils.getLatch().await(); Assert.assertEquals(TestUtils.getResult(), "OK"); }
Example 8
Source File: ModelServerTest.java From serve with Apache License 2.0 | 6 votes |
@Test( alwaysRun = true, dependsOnMethods = {"testLegacyPredict"}) public void testPredictionsInvalidRequestSize() throws InterruptedException { Channel channel = TestUtils.getInferenceChannel(configManager); TestUtils.setResult(null); TestUtils.setLatch(new CountDownLatch(1)); DefaultFullHttpRequest req = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "/predictions/noop"); req.content().writeZero(11485760); HttpUtil.setContentLength(req, req.content().readableBytes()); req.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_OCTET_STREAM); channel.writeAndFlush(req); TestUtils.getLatch().await(); Assert.assertEquals(TestUtils.getHttpStatus(), HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE); }
Example 9
Source File: ModelServerTest.java From serve with Apache License 2.0 | 6 votes |
@Test( alwaysRun = true, dependsOnMethods = {"testPredictionsInvalidRequestSize"}) public void testPredictionsValidRequestSize() throws InterruptedException { Channel channel = TestUtils.getInferenceChannel(configManager); TestUtils.setResult(null); TestUtils.setLatch(new CountDownLatch(1)); DefaultFullHttpRequest req = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "/predictions/noop"); req.content().writeZero(10385760); HttpUtil.setContentLength(req, req.content().readableBytes()); req.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_OCTET_STREAM); channel.writeAndFlush(req); TestUtils.getLatch().await(); Assert.assertEquals(TestUtils.getHttpStatus(), HttpResponseStatus.OK); }
Example 10
Source File: ModelServerTest.java From serve with Apache License 2.0 | 6 votes |
@Test( alwaysRun = true, dependsOnMethods = {"testPredictionsValidRequestSize"}) public void testPredictionsDecodeRequest() throws InterruptedException, NoSuchFieldException, IllegalAccessException { Channel inferChannel = TestUtils.getInferenceChannel(configManager); Channel mgmtChannel = TestUtils.getManagementChannel(configManager); setConfiguration("decode_input_request", "true"); loadTests(mgmtChannel, "noop-v1.0-config-tests.mar", "noop-config"); TestUtils.setResult(null); TestUtils.setLatch(new CountDownLatch(1)); DefaultFullHttpRequest req = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "/predictions/noop-config"); req.content().writeCharSequence("{\"data\": \"test\"}", CharsetUtil.UTF_8); HttpUtil.setContentLength(req, req.content().readableBytes()); req.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON); inferChannel.writeAndFlush(req); TestUtils.getLatch().await(); Assert.assertEquals(TestUtils.getHttpStatus(), HttpResponseStatus.OK); Assert.assertFalse(TestUtils.getResult().contains("bytearray")); unloadTests(mgmtChannel, "noop-config"); }
Example 11
Source File: Http1RequestHandler.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
@Override protected void sendResponse(final ChannelHandlerContext ctx, String streamId, int latency, final FullHttpResponse response, final FullHttpRequest request) { HttpUtil.setContentLength(response, response.content().readableBytes()); ctx.executor().schedule(new Runnable() { @Override public void run() { if (isKeepAlive(request)) { response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.writeAndFlush(response); } else { ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } } }, latency, TimeUnit.MILLISECONDS); }
Example 12
Source File: DFWSRequestHandler.java From dfactor with MIT License | 5 votes |
private void _sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse rsp) { // Generate an error page if response getStatus code is not OK (200). if (rsp.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(rsp.status().toString(), CharsetUtil.UTF_8); rsp.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(rsp, rsp.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(rsp); if (!HttpUtil.isKeepAlive(req) || rsp.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
Example 13
Source File: EchoMethodHandler.java From ambry with Apache License 2.0 | 5 votes |
private void updateHeaders(HttpResponse response, HttpRequest request, int contentLength) { HttpUtil.setContentLength(response, contentLength); if (request.headers().get(RESPONSE_HEADER_KEY_1) != null) { response.headers().set(RESPONSE_HEADER_KEY_1, request.headers().get(RESPONSE_HEADER_KEY_1)); } if (request.headers().get(RESPONSE_HEADER_KEY_2) != null) { response.headers().set(RESPONSE_HEADER_KEY_2, request.headers().get(RESPONSE_HEADER_KEY_2)); } }
Example 14
Source File: ModelServerTest.java From multi-model-server with Apache License 2.0 | 5 votes |
private void testPredictionsBinary(Channel channel) throws InterruptedException { result = null; latch = new CountDownLatch(1); DefaultFullHttpRequest req = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "/predictions/noop"); req.content().writeCharSequence("test", CharsetUtil.UTF_8); HttpUtil.setContentLength(req, req.content().readableBytes()); req.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_OCTET_STREAM); channel.writeAndFlush(req); latch.await(); Assert.assertEquals(result, "OK"); }
Example 15
Source File: WebSocketIndexPageHandler.java From tools-journey with Apache License 2.0 | 5 votes |
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
Example 16
Source File: WebSocketServerHandshakerFactory.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Return that we need cannot not support the web socket version */ public static ChannelFuture sendUnsupportedVersionResponse(Channel channel, ChannelPromise promise) { HttpResponse res = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.UPGRADE_REQUIRED); res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue()); HttpUtil.setContentLength(res, 0); return channel.writeAndFlush(res, promise); }
Example 17
Source File: ModelServerTest.java From serve with Apache License 2.0 | 4 votes |
@Test( alwaysRun = true, dependsOnMethods = {"testPredictionMemoryError"}) public void testErrorBatch() throws InterruptedException { Channel channel = TestUtils.getManagementChannel(configManager); Assert.assertNotNull(channel); TestUtils.setHttpStatus(null); TestUtils.setResult(null); TestUtils.setLatch(new CountDownLatch(1)); TestUtils.registerModel(channel, "error_batch.mar", "err_batch", true, false); TestUtils.getLatch().await(); StatusResponse status = JsonUtils.GSON.fromJson(TestUtils.getResult(), StatusResponse.class); Assert.assertEquals( status.getStatus(), "Model \"err_batch\" Version: 1.0 registered with 1 initial workers"); channel.close(); channel = TestUtils.connect(false, configManager); Assert.assertNotNull(channel); TestUtils.setResult(null); TestUtils.setLatch(new CountDownLatch(1)); TestUtils.setHttpStatus(null); DefaultFullHttpRequest req = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "/predictions/err_batch"); req.content().writeCharSequence("data=invalid_output", CharsetUtil.UTF_8); HttpUtil.setContentLength(req, req.content().readableBytes()); req.headers() .set( HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED); channel.writeAndFlush(req); TestUtils.getLatch().await(); Assert.assertEquals(TestUtils.getHttpStatus(), HttpResponseStatus.INSUFFICIENT_STORAGE); Assert.assertEquals(TestUtils.getResult(), "Invalid response"); }
Example 18
Source File: HttpClientOperations.java From reactor-netty with Apache License 2.0 | 4 votes |
@SuppressWarnings("FutureReturnValueIgnored") void _subscribe(CoreSubscriber<? super Void> s) { HttpDataFactory df = DEFAULT_FACTORY; try { HttpClientFormEncoder encoder = new HttpClientFormEncoder(df, parent.nettyRequest, false, HttpConstants.DEFAULT_CHARSET, HttpPostRequestEncoder.EncoderMode.RFC1738); formCallback.accept(parent, encoder); encoder = encoder.applyChanges(parent.nettyRequest); df = encoder.newFactory; if (!encoder.isMultipart()) { parent.requestHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING); } // Returned value is deliberately ignored parent.addHandlerFirst(NettyPipeline.ChunkedWriter, new ChunkedWriteHandler()); boolean chunked = HttpUtil.isTransferEncodingChunked(parent.nettyRequest); HttpRequest r = encoder.finalizeRequest(); if (!chunked) { HttpUtil.setTransferEncodingChunked(r, false); HttpUtil.setContentLength(r, encoder.length()); } ChannelFuture f = parent.channel() .writeAndFlush(r); Flux<Long> tail = encoder.progressFlux.onBackpressureLatest(); if (encoder.cleanOnTerminate) { tail = tail.doOnCancel(encoder) .doAfterTerminate(encoder); } if (encoder.isChunked()) { if (progressCallback != null) { progressCallback.accept(tail); } //"FutureReturnValueIgnored" this is deliberate parent.channel() .writeAndFlush(encoder); } else { if (progressCallback != null) { progressCallback.accept(FutureMono.from(f) .cast(Long.class) .switchIfEmpty(Mono.just(encoder.length())) .flux()); } } s.onComplete(); } catch (Throwable e) { Exceptions.throwIfJvmFatal(e); df.cleanRequestHttpData(parent.nettyRequest); s.onError(Exceptions.unwrap(e)); } }
Example 19
Source File: ResourceHandler.java From socketio with Apache License 2.0 | 4 votes |
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri()); String requestPath = queryDecoder.path(); URL resUrl = resources.get(requestPath); if (resUrl != null) { if (log.isDebugEnabled()) log.debug("Received HTTP resource request: {} {} from channel: {}", req.method(), requestPath, ctx.channel()); URLConnection fileUrl = resUrl.openConnection(); long lastModified = fileUrl.getLastModified(); // check if file has been modified since last request if (isNotModified(req, lastModified)) { sendNotModified(ctx); return; } // create resource input-stream and check existence final InputStream is = fileUrl.getInputStream(); if (is == null) { sendError(ctx, HttpResponseStatus.NOT_FOUND); return; } // create ok response HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); // set Content-Length header HttpUtil.setContentLength(res, fileUrl.getContentLengthLong()); // set Content-Type header setContentTypeHeader(res, fileUrl); // set Date, Expires, Cache-Control and Last-Modified headers setDateAndCacheHeaders(res, lastModified); // write initial response header ctx.write(res); // write the content stream ctx.pipeline().addBefore(ctx.name(), "chunked-writer-handler", new ChunkedWriteHandler()); ChannelFuture writeFuture = ctx.writeAndFlush(new ChunkedStream(is, fileUrl.getContentLength())); // add operation complete listener so we can close the channel and the input stream writeFuture.addListener(ChannelFutureListener.CLOSE); ReferenceCountUtil.release(msg); return; } } super.channelRead(ctx, msg); }
Example 20
Source File: NettyResponseChannel.java From ambry with Apache License 2.0 | 4 votes |
/** * Provided a cause, returns an error response with the right status and error message. * @param cause the cause of the error. * @return a {@link FullHttpResponse} with the error message that can be sent to the client. */ private FullHttpResponse getErrorResponse(Throwable cause) { HttpResponseStatus status; RestServiceErrorCode restServiceErrorCode = null; String errReason = null; if (cause instanceof RestServiceException) { RestServiceException restServiceException = (RestServiceException) cause; restServiceErrorCode = restServiceException.getErrorCode(); errorResponseStatus = ResponseStatus.getResponseStatus(restServiceErrorCode); status = getHttpResponseStatus(errorResponseStatus); if (shouldSendFailureReason(status, restServiceException)) { errReason = new String( Utils.getRootCause(cause).getMessage().replaceAll("[\n\t\r]", " ").getBytes(StandardCharsets.US_ASCII), StandardCharsets.US_ASCII); } } else if (Utils.isPossibleClientTermination(cause)) { nettyMetrics.clientEarlyTerminationCount.inc(); status = HttpResponseStatus.INTERNAL_SERVER_ERROR; errorResponseStatus = ResponseStatus.InternalServerError; } else { nettyMetrics.internalServerErrorCount.inc(); status = HttpResponseStatus.INTERNAL_SERVER_ERROR; errorResponseStatus = ResponseStatus.InternalServerError; } logger.trace("Constructed error response for the client - [{} - {}]", status, errReason); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status); response.headers().set(HttpHeaderNames.DATE, new GregorianCalendar().getTime()); HttpUtil.setContentLength(response, 0); if (errReason != null) { response.headers().set(FAILURE_REASON_HEADER, errReason); } if (restServiceErrorCode != null && HttpStatusClass.CLIENT_ERROR.contains(status.code())) { response.headers().set(ERROR_CODE_HEADER, restServiceErrorCode.name()); } response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); // if there is an ALLOW header in the response so far constructed, copy it if (responseMetadata.headers().contains(HttpHeaderNames.ALLOW)) { response.headers().set(HttpHeaderNames.ALLOW, responseMetadata.headers().get(HttpHeaderNames.ALLOW)); } else if (errorResponseStatus == ResponseStatus.MethodNotAllowed) { logger.warn("Response is {} but there is no value for {}", ResponseStatus.MethodNotAllowed, HttpHeaderNames.ALLOW); } copyTrackingHeaders(responseMetadata, response); HttpUtil.setKeepAlive(response, shouldKeepAlive(status)); return response; }