本文整理汇总了Java中io.netty.handler.codec.http.HttpHeaders.isKeepAlive方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaders.isKeepAlive方法的具体用法?Java HttpHeaders.isKeepAlive怎么用?Java HttpHeaders.isKeepAlive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.handler.codec.http.HttpHeaders
的用法示例。
在下文中一共展示了HttpHeaders.isKeepAlive方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendHttpResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* 返回http信息
* @param ctx
* @param req
* @param res
*/
private static void sendHttpResponse(
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
HttpHeaders.setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
示例2: writeResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
// Decide whether to close the connection or not.
boolean keepAlive = HttpHeaders.isKeepAlive(request);
// Build the response object.
FullHttpResponse response = new DefaultFullHttpResponse(
HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "application/json");
if (keepAlive) {
// Add 'Content-Length' header only for a keep-alive connection.
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
// Add keep alive header as per:
// - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例3: sendHttpResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
public void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
if (res.getStatus().code() != 200) {
ByteBuf f = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().clear();
res.content().writeBytes(f);
f.release();
}
HttpHeaders.setContentLength(res, res.content().readableBytes());
ChannelFuture f1;
f1 = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
f1.addListener(ChannelFutureListener.CLOSE);
}
}
示例4: channelRead0
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
ByteBuf buf = msg.content();
byte[] bytes = new byte[buf.readableBytes()];
buf.getBytes(0, bytes);
YarRequest yarRequest = YarProtocol.buildRequest(bytes);
YarResponse yarResponse = process(yarRequest);
FullHttpResponse response =
new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(YarProtocol
.toProtocolBytes(yarResponse)));
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded");
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
if (HttpHeaders.isKeepAlive(msg)) {
response.headers().set(HttpHeaders.Names.CONNECTION, Values.KEEP_ALIVE);
}
ctx.write(response);
ctx.flush();
ctx.close();
}
示例5: channelRead
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
if (msg instanceof HttpRequest) {
request = (HttpRequest) msg;
String uri = request.getUri();
System.out.println("Uri:" + uri);
}
if (msg instanceof HttpContent) {
HttpContent content = (HttpContent) msg;
ByteBuf buf = content.content();
System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
buf.release();
String res = "I am OK";
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
OK, Unpooled.wrappedBuffer(res.getBytes("UTF-8")));
response.headers().set(CONTENT_TYPE, "text/plain");
response.headers().set(CONTENT_LENGTH,
response.content().readableBytes());
if (HttpHeaders.isKeepAlive(request)) {
response.headers().set(CONNECTION, Values.KEEP_ALIVE);
}
ctx.write(response);
ctx.flush();
}
}
示例6: RequestInfoImpl
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
public RequestInfoImpl(HttpRequest request) {
this(request.getUri(), request.getMethod(), request.headers(),
HttpUtils.extractTrailingHeadersIfPossible(request), null, HttpUtils.extractCookies(request), null,
HttpUtils.extractContentChunks(request), request.getProtocolVersion(), HttpHeaders.isKeepAlive(request),
(request instanceof FullHttpRequest),
HttpPostRequestDecoder.isMultipart(request));
}
示例7: keepAlive
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Implemented in accordance with RFC 7230 section 6.1 https://tools.ietf.org/html/rfc7230#section-6.1
*/
@Override
public boolean keepAlive(Request ahcRequest, HttpRequest request, HttpResponse response) {
return HttpHeaders.isKeepAlive(response)//
&& HttpHeaders.isKeepAlive(request)
// support non standard Proxy-Connection
&& !response.headers().contains("Proxy-Connection", HttpHeaders.Values.CLOSE, true);
}
示例8: shouldCloseClientConnection
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Determine whether or not the client connection should be closed.
*
* @param req
* @param res
* @param httpObject
* @return
*/
private boolean shouldCloseClientConnection(HttpRequest req,
HttpResponse res, HttpObject httpObject) {
if (ProxyUtils.isChunked(res)) {
// If the response is chunked, we want to return false unless it's
// the last chunk. If it is the last chunk, then we want to pass
// through to the same close semantics we'd otherwise use.
if (httpObject != null) {
if (!ProxyUtils.isLastChunk(httpObject)) {
String uri = null;
if (req != null) {
uri = req.getUri();
}
LOG.debug("Not closing client connection on middle chunk for {}", uri);
return false;
} else {
LOG.debug("Handling last chunk. Using normal client connection closing rules.");
}
}
}
if (!HttpHeaders.isKeepAlive(req)) {
LOG.debug("Closing client connection since request is not keep alive: {}", req);
// Here we simply want to close the connection because the
// client itself has requested it be closed in the request.
return true;
}
// ignore the response's keep-alive; we can keep this client connection open as long as the client allows it.
LOG.debug("Not closing client connection for request: {}", req);
return false;
}
示例9: shouldCloseServerConnection
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Determines if the remote connection should be closed based on the request
* and response pair. If the request is HTTP 1.0 with no keep-alive header,
* for example, the connection should be closed.
*
* This in part determines if we should close the connection. Here's the
* relevant section of RFC 2616:
*
* "HTTP/1.1 defines the "close" connection option for the sender to signal
* that the connection will be closed after completion of the response. For
* example,
*
* Connection: close
*
* in either the request or the response header fields indicates that the
* connection SHOULD NOT be considered `persistent' (section 8.1) after the
* current request/response is complete."
*
* @param req
* The request.
* @param res
* The response.
* @param msg
* The message.
* @return Returns true if the connection should close.
*/
private boolean shouldCloseServerConnection(HttpRequest req,
HttpResponse res, HttpObject msg) {
if (ProxyUtils.isChunked(res)) {
// If the response is chunked, we want to return false unless it's
// the last chunk. If it is the last chunk, then we want to pass
// through to the same close semantics we'd otherwise use.
if (msg != null) {
if (!ProxyUtils.isLastChunk(msg)) {
String uri = null;
if (req != null) {
uri = req.getUri();
}
LOG.debug("Not closing server connection on middle chunk for {}", uri);
return false;
} else {
LOG.debug("Handling last chunk. Using normal server connection closing rules.");
}
}
}
// ignore the request's keep-alive; we can keep this server connection open as long as the server allows it.
if (!HttpHeaders.isKeepAlive(res)) {
LOG.debug("Closing server connection since response is not keep alive: {}", res);
// In this case, we want to honor the Connection: close header
// from the remote server and close that connection. We don't
// necessarily want to close the connection to the client, however
// as it's possible it has other connections open.
return true;
}
LOG.debug("Not closing server connection for response: {}", res);
return false;
}
示例10: respondWithShortCircuitResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Responds to the client with the specified "short-circuit" response. The response will be sent through the
* {@link HttpFilters#proxyToClientResponse(HttpObject)} filter method before writing it to the client. The client
* will not be disconnected, unless the response includes a "Connection: close" header, or the filter returns
* a null HttpResponse (in which case no response will be written to the client and the connection will be
* disconnected immediately). If the response is not a Bad Gateway or Gateway Timeout response, the response's headers
* will be modified to reflect proxying, including adding a Via header, Date header, etc.
*
* @param httpResponse the response to return to the client
* @return true if the connection will be kept open, or false if it will be disconnected.
*/
private boolean respondWithShortCircuitResponse(HttpResponse httpResponse) {
// we are sending a response to the client, so we are done handling this request
this.currentRequest = null;
HttpResponse filteredResponse = (HttpResponse) currentFilters.proxyToClientResponse(httpResponse);
if (filteredResponse == null) {
disconnect();
return false;
}
// allow short-circuit messages to close the connection. normally the Connection header would be stripped when modifying
// the message for proxying, so save the keep-alive status before the modifications are made.
boolean isKeepAlive = HttpHeaders.isKeepAlive(httpResponse);
// if the response is not a Bad Gateway or Gateway Timeout, modify the headers "as if" the short-circuit response were proxied
int statusCode = httpResponse.getStatus().code();
if (statusCode != HttpResponseStatus.BAD_GATEWAY.code() && statusCode != HttpResponseStatus.GATEWAY_TIMEOUT.code()) {
modifyResponseHeadersToReflectProxying(httpResponse);
}
// restore the keep alive status, if it was overwritten when modifying headers for proxying
HttpHeaders.setKeepAlive(httpResponse, isKeepAlive);
write(httpResponse);
if (ProxyUtils.isLastChunk(httpResponse)) {
writeEmptyBuffer();
}
if (!HttpHeaders.isKeepAlive(httpResponse)) {
disconnect();
return false;
}
return true;
}
示例11: sendHttpResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
/**
* Provide the response as per the requests
* @param ChannelHandlerContext, FullHttpRequest, FullHttpResponse
* @return void
*/
private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) throws Exception {
if (res.getStatus().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
HttpHeaders.setContentLength(res, res.content().readableBytes());
}
ChannelFuture f = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
示例12: sendHttpResponse
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
HttpHeaders.setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
开发者ID:spring-cloud,项目名称:spring-cloud-stream-app-starters,代码行数:16,代码来源:WebsocketSinkServerHandler.java
示例13: beforeProcess
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
protected void beforeProcess(Exchange exchange, final ChannelHandlerContext ctx, final Object message) {
if (consumer.getConfiguration().isBridgeEndpoint()) {
exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
exchange.setProperty(Exchange.SKIP_WWW_FORM_URLENCODED, Boolean.TRUE);
}
HttpRequest request = (HttpRequest) message;
// setup the connection property in case of the message header is removed
boolean keepAlive = HttpHeaders.isKeepAlive(request);
if (!keepAlive) {
// Just make sure we close the connection this time.
exchange.setProperty(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
}
}
示例14: getResponseMessage
import io.netty.handler.codec.http.HttpHeaders; //导入方法依赖的package包/类
@Override
protected Message getResponseMessage(Exchange exchange, ChannelHandlerContext ctx, Object message) throws Exception {
FullHttpResponse response = (FullHttpResponse) message;
if (!HttpHeaders.isKeepAlive(response)) {
// just want to make sure we close the channel if the keepAlive is not true
exchange.setProperty(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, true);
}
// use the binding
return producer.getEndpoint().getNettyHttpBinding().toCamelMessage(response, exchange, producer.getConfiguration());
}