List of usage examples for io.netty.handler.codec.http HttpMessage headers
HttpHeaders headers();
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
/** * Returns {@code true} if and only if the connection can remain open and * thus 'kept alive'. This methods respects the value of the * {@code "Connection"} header first and then the return value of * {@link io.netty.handler.codec.http.HttpVersion#isKeepAliveDefault()}. *//*w ww . java 2 s.c o m*/ public static boolean isKeepAlive(HttpMessage message) { String connection = message.headers().get(Names.CONNECTION); if (connection != null && AsciiString.equalsIgnoreCase(Values.CLOSE, connection)) { return false; } if (message.protocolVersion().isKeepAliveDefault()) { return !AsciiString.equalsIgnoreCase(Values.CLOSE, connection); } else { return AsciiString.equalsIgnoreCase(Values.KEEP_ALIVE, connection); } }
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
/** * Sets the value of the {@code "Connection"} header depending on the * protocol version of the specified message. This getMethod sets or removes * the {@code "Connection"} header depending on what the default keep alive * mode of the message's protocol version is, as specified by * {@link io.netty.handler.codec.http.HttpVersion#isKeepAliveDefault()}. * <ul>/*from w w w .j a v a 2 s .com*/ * <li>If the connection is kept alive by default: * <ul> * <li>set to {@code "close"} if {@code keepAlive} is {@code false}.</li> * <li>remove otherwise.</li> * </ul></li> * <li>If the connection is closed by default: * <ul> * <li>set to {@code "keep-alive"} if {@code keepAlive} is {@code true}.</li> * <li>remove otherwise.</li> * </ul></li> * </ul> */ public static void setKeepAlive(HttpMessage message, boolean keepAlive) { HttpHeaders h = message.headers(); if (message.protocolVersion().isKeepAliveDefault()) { if (keepAlive) { h.remove(Names.CONNECTION); } else { h.set(Names.CONNECTION, Values.CLOSE); } } else { if (keepAlive) { h.set(Names.CONNECTION, Values.KEEP_ALIVE); } else { h.remove(Names.CONNECTION); } } }
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
/** * Returns the length of the content. Please note that this value is * not retrieved from {@link io.netty.handler.codec.http.HttpContent#content()} but from the * {@code "Content-Length"} header, and thus they are independent from each * other.//from w w w . j a v a 2 s . c o m * * @return the content length * * @throws NumberFormatException * if the message does not have the {@code "Content-Length"} header * or its value is not a number */ public static long getContentLength(HttpMessage message) { String value = message.headers().get(Names.CONTENT_LENGTH); if (value != null) { return Long.parseLong(value); } // We know the content length if it's a Web Socket message even if // Content-Length header is missing. long webSocketContentLength = getWebSocketContentLength(message); if (webSocketContentLength >= 0) { return webSocketContentLength; } // Otherwise we don't. throw new NumberFormatException("header not found: " + Names.CONTENT_LENGTH); }
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
/** * Returns the length of the content. Please note that this value is * not retrieved from {@link io.netty.handler.codec.http.HttpContent#content()} but from the * {@code "Content-Length"} header, and thus they are independent from each * other./*w ww. j av a2 s. c om*/ * * @return the content length or {@code defaultValue} if this message does * not have the {@code "Content-Length"} header or its value is not * a number */ public static long getContentLength(HttpMessage message, long defaultValue) { String contentLength = message.headers().get(Names.CONTENT_LENGTH); if (contentLength != null) { try { return Long.parseLong(contentLength); } catch (NumberFormatException ignored) { return defaultValue; } } // We know the content length if it's a Web Socket message even if // Content-Length header is missing. long webSocketContentLength = getWebSocketContentLength(message); if (webSocketContentLength >= 0) { return webSocketContentLength; } // Otherwise we don't. return defaultValue; }
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
/** * Returns the content length of the specified web socket message. If the * specified message is not a web socket message, {@code -1} is returned. *//* w w w .ja v a2 s. c om*/ private static int getWebSocketContentLength(HttpMessage message) { // WebSockset messages have constant content-lengths. HttpHeaders h = message.headers(); if (message instanceof HttpRequest) { HttpRequest req = (HttpRequest) message; if (HttpMethod.GET.equals(req.method()) && h.contains(Names.SEC_WEBSOCKET_KEY1) && h.contains(Names.SEC_WEBSOCKET_KEY2)) { return 8; } } else if (message instanceof HttpResponse) { HttpResponse res = (HttpResponse) message; if (res.status().code() == 101 && h.contains(Names.SEC_WEBSOCKET_ORIGIN) && h.contains(Names.SEC_WEBSOCKET_LOCATION)) { return 16; } } // Not a web socket message return -1; }
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
/** * Sets the {@code "Content-Length"} header. *//*from w w w.j av a2 s .c o m*/ public static void setContentLength(HttpMessage message, long length) { message.headers().set(Names.CONTENT_LENGTH, length); }
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
public static boolean isContentLengthSet(HttpMessage m) { return m.headers().contains(Names.CONTENT_LENGTH); }
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
/** * Returns {@code true} if and only if the specified message contains the * {@code "Expect: 100-continue"} header. *///from w ww. j av a 2 s.com public static boolean is100ContinueExpected(HttpMessage message) { // Expect: 100-continue is for requests only. if (!(message instanceof HttpRequest)) { return false; } // It works only on HTTP/1.1 or later. if (message.protocolVersion().compareTo(HttpVersion.HTTP_1_1) < 0) { return false; } // In most cases, there will be one or zero 'Expect' header. String value = message.headers().get(Names.EXPECT); if (value == null) { return false; } if (AsciiString.equalsIgnoreCase(Values.CONTINUE, value)) { return true; } // Multiple 'Expect' headers. Search through them. return message.headers().contains(Names.EXPECT, Values.CONTINUE, true); }
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
/** * Sets or removes the {@code "Expect: 100-continue"} header to / from the * specified message. If the specified {@code value} is {@code true}, * the {@code "Expect: 100-continue"} header is set and all other previous * {@code "Expect"} headers are removed. Otherwise, all {@code "Expect"} * headers are removed completely./* www . j av a 2 s . co m*/ */ public static void set100ContinueExpected(HttpMessage message, boolean expected) { if (expected) { message.headers().set(Names.EXPECT, Values.CONTINUE); } else { message.headers().remove(Names.EXPECT); } }
From source file:cc.changic.platform.etl.schedule.http.HttpHeaderUtil.java
License:Apache License
/** * Checks to see if the transfer encoding in a specified {@link io.netty.handler.codec.http.HttpMessage} is chunked * * @param message The message to check/*from w w w. ja v a 2 s .co m*/ * @return True if transfer encoding is chunked, otherwise false */ public static boolean isTransferEncodingChunked(HttpMessage message) { return message.headers().contains(Names.TRANSFER_ENCODING, Values.CHUNKED, true); }