예제 #1
0
  /**
   * Returns {@code true} if and only if the specified message contains the {@code "Expect:
   * 100-continue"} header.
   */
  public static boolean is100ContinueExpected(HttpHeader message) {
    // Expect: 100-continue is for requests only.
    if (!(message instanceof HttpRequestHeader)) {
      return false;
    }

    // It works only on HTTP/1.1 or later.
    if (message.getProtocolVersion().compareTo(HttpVersion.HTTP_1_1) < 0) {
      return false;
    }

    // In most cases, there will be one or zero 'Expect' header.
    String value = message.getHeader(Names.EXPECT);
    if (value == null) {
      return false;
    }
    if (Values.CONTINUE.equalsIgnoreCase(value)) {
      return true;
    }

    // Multiple 'Expect' headers.  Search through them.
    for (String v : message.getHeaders(Names.EXPECT)) {
      if (Values.CONTINUE.equalsIgnoreCase(v)) {
        return true;
      }
    }
    return false;
  }
예제 #2
0
 /**
  * Returns the header value with the specified header name. If there are more than one header
  * value for the specified header name, the first value is returned.
  *
  * @return the header value or the {@code defaultValue} if there is no such header
  */
 public static String getHeader(HttpHeader message, String name, String defaultValue) {
   String value = message.getHeader(name);
   if (value == null) {
     return defaultValue;
   }
   return value;
 }
예제 #3
0
  /**
   * 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 HttpVersion#isKeepAliveDefault()}.
   */
  public static boolean isKeepAlive(HttpHeader message) {
    String connection = message.getHeader(Names.CONNECTION);
    if (Values.CLOSE.equalsIgnoreCase(connection)) {
      return false;
    }

    if (message.getProtocolVersion().isKeepAliveDefault()) {
      return !Values.CLOSE.equalsIgnoreCase(connection);
    } else {
      return Values.KEEP_ALIVE.equalsIgnoreCase(connection);
    }
  }
예제 #4
0
  /**
   * Returns the length of the content. Please note that this value is not retrieved from {@link
   * HttpContent#getContent()} but from the {@code "Content-Length"} header, and thus they are
   * independent from each other.
   *
   * @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(HttpHeader message, long defaultValue) {
    String contentLength = message.getHeader(Names.CONTENT_LENGTH);
    if (contentLength != null) {
      try {
        return Long.parseLong(contentLength);
      } catch (NumberFormatException e) {
        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;
  }
예제 #5
0
 /** Returns the value of the {@code "Host"} header. */
 public static String getHost(HttpHeader message) {
   return message.getHeader(Names.HOST);
 }
예제 #6
0
 /**
  * Returns the header value with the specified header name. If there are more than one header
  * value for the specified header name, the first value is returned.
  *
  * @return the header value or {@code null} if there is no such header
  */
 public static String getHeader(HttpHeader message, String name) {
   return message.getHeader(name);
 }