List of usage examples for io.netty.handler.codec.http HttpVersion HTTP_1_0
HttpVersion HTTP_1_0
To view the source code for io.netty.handler.codec.http HttpVersion HTTP_1_0.
Click Source Link
From source file:HttpUploadServerHandler.java
License:Apache License
private void writeResponse(Channel channel) { // Convert the response content to a ChannelBuffer. ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8); responseContent.setLength(0);/*from w w w .jav a2s . co m*/ // Decide whether to close the connection or not. boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION)); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (!close) { // There's no need to add 'Content-Length' header // if this is the last response. response.headers().set(CONTENT_LENGTH, String.valueOf(buf.readableBytes())); } Set<Cookie> cookies; String value = request.headers().get(COOKIE); if (value == null) { cookies = Collections.emptySet(); } else { cookies = CookieDecoder.decode(value); } if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } // Write the response. ChannelFuture future = channel.write(response); // Close the connection after the write operation is done if necessary. if (close) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:c5db.control.ClientHttpProtostuffEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception { Class<?> messageType = msg.getClass(); LowCopyProtobufOutput outputSerializer = new LowCopyProtobufOutput(); msg.cachedSchema().writeTo(outputSerializer, msg); List<ByteBuffer> serializedBuffers = outputSerializer.buffer.finish(); ByteBuf requestContent = Unpooled.wrappedBuffer(serializedBuffers.toArray(new ByteBuffer[] {})); DefaultFullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.POST, "foo", requestContent); httpRequest.headers().set(HttpProtostuffConstants.PROTOSTUFF_HEADER_NAME, messageType.getName()); httpRequest.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/octet-stream"); httpRequest.headers().set(HttpHeaders.Names.CONTENT_LENGTH, requestContent.readableBytes()); out.add(httpRequest);//from w ww . ja v a 2s.c o m }
From source file:c5db.control.ServerHttpProtostuffEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Message msg, List<Object> out) throws Exception { Class<?> messageType = msg.getClass(); LowCopyProtobufOutput outputSerializer = new LowCopyProtobufOutput(); msg.cachedSchema().writeTo(outputSerializer, msg); List<ByteBuffer> serializedBuffers = outputSerializer.buffer.finish(); ByteBuf replyContent = Unpooled.wrappedBuffer(serializedBuffers.toArray(new ByteBuffer[] {})); DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, replyContent); httpResponse.headers().set(HttpProtostuffConstants.PROTOSTUFF_HEADER_NAME, messageType.getName()); httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/octet-stream"); out.add(httpResponse);//from w ww . ja va 2 s . co m }
From source file:cc.io.lessons.server.HttpUploadServerHandler.java
License:Apache License
private void writeResponse(Channel channel) { // Convert the response content to a ChannelBuffer. ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8); responseContent.setLength(0);// w w w . j a v a2 s.c o m // Decide whether to close the connection or not. boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION)); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); if (!close) { // There's no need to add 'Content-Length' header // if this is the last response. response.headers().set(CONTENT_LENGTH, buf.readableBytes()); } Set<Cookie> cookies; String value = request.headers().get(COOKIE); if (value == null) { cookies = Collections.emptySet(); } else { cookies = CookieDecoder.decode(value); } if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie)); } } // Write the response. ChannelFuture future = channel.writeAndFlush(response); // Close the connection after the write operation is done if necessary. if (close) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.chiorichan.http.HttpResponseWrapper.java
License:Mozilla Public License
public void sendRedirectRepost(String target) { sendRedirect(target, request.getHttpVersion() == HttpVersion.HTTP_1_0 ? 302 : 307); }
From source file:com.cmz.http.upload.HttpUploadServerHandler.java
License:Apache License
private void writeResponse(Channel channel) { // Convert the response content to a ChannelBuffer. ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8); responseContent.setLength(0);//w w w.j a v a 2s. c o m // Decide whether to close the connection or not. boolean close = request.headers().contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE, true) || request.protocolVersion().equals(HttpVersion.HTTP_1_0) && !request.headers() .contains(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE, true); // Build the response object. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8"); if (!close) { // There's no need to add 'Content-Length' header // if this is the last response. response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, buf.readableBytes()); } Set<Cookie> cookies; String value = request.headers().get(HttpHeaderNames.COOKIE); if (value == null) { cookies = Collections.emptySet(); } else { cookies = ServerCookieDecoder.STRICT.decode(value); } if (!cookies.isEmpty()) { // Reset the cookies if necessary. for (Cookie cookie : cookies) { response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(cookie)); } } // Write the response. ChannelFuture future = channel.writeAndFlush(response); // Close the connection after the write operation is done if necessary. if (close) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.creamsugardonut.CustomHttpRequestDecoder.java
License:Apache License
@Override protected HttpMessage createInvalidMessage() { return new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/bad-request", Unpooled.EMPTY_BUFFER, validateHeaders); }
From source file:com.linkedin.flashback.netty.builder.RecordedHttpMessageBuilderTest.java
License:Open Source License
@Test public void testAddHeaders() throws Exception { HttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "www.google.com"); nettyRequest.headers().add("key1", "value1"); nettyRequest.headers().add("key1", "value2"); nettyRequest.headers().add("key2", "value1"); RecordedHttpRequestBuilder recordedHttpRequestBuilder = new RecordedHttpRequestBuilder(nettyRequest); Map<String, String> headers = recordedHttpRequestBuilder.getHeaders(); Assert.assertEquals(headers.size(), 2); }
From source file:com.linkedin.flashback.netty.builder.RecordedHttpMessageBuilderTest.java
License:Open Source License
@Test public void testGetHeaders() throws Exception { HttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "www.google.com"); nettyRequest.headers().add("key1", "value1"); nettyRequest.headers().add("key1", "value2"); nettyRequest.headers().add("key2", "value1"); RecordedHttpRequestBuilder recordedHttpRequestBuilder = new RecordedHttpRequestBuilder(nettyRequest); Map<String, String> headers = recordedHttpRequestBuilder.getHeaders(); Assert.assertEquals(headers.size(), 2); Assert.assertEquals(headers.get("key1"), "value1, value2"); Assert.assertEquals(headers.get("key2"), "value1"); }
From source file:com.linkedin.flashback.netty.builder.RecordedHttpRequestBuilderTest.java
License:Open Source License
@Test public void testBuildHttpMethod() { HttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "http://www.google.com"); RecordedHttpRequestBuilder recordedHttpRequestBuilder = new RecordedHttpRequestBuilder(nettyRequest); RecordedHttpRequest recordedHttpRequest = recordedHttpRequestBuilder.build(); Assert.assertEquals(recordedHttpRequest.getMethod(), HttpMethod.GET.toString()); }