public long determineLength(HttpMessage httpMessage) throws HttpException {
   Args.notNull(httpMessage, "HTTP message");
   Header firstHeader = httpMessage.getFirstHeader(HTTP.TRANSFER_ENCODING);
   if (firstHeader != null) {
     String value = firstHeader.getValue();
     if (HTTP.CHUNK_CODING.equalsIgnoreCase(value)) {
       if (!httpMessage.getProtocolVersion().lessEquals(HttpVersion.HTTP_1_0)) {
         return -2;
       }
       throw new ProtocolException(
           "Chunked transfer encoding not allowed for " + httpMessage.getProtocolVersion());
     } else if (HTTP.IDENTITY_CODING.equalsIgnoreCase(value)) {
       return -1;
     } else {
       throw new ProtocolException("Unsupported transfer encoding: " + value);
     }
   }
   firstHeader = httpMessage.getFirstHeader(HTTP.CONTENT_LEN);
   if (firstHeader == null) {
     return (long) this.implicitLen;
   }
   String value2 = firstHeader.getValue();
   try {
     long parseLong = Long.parseLong(value2);
     if (parseLong >= 0) {
       return parseLong;
     }
     throw new ProtocolException("Negative content length: " + value2);
   } catch (NumberFormatException e) {
     throw new ProtocolException("Invalid content length: " + value2);
   }
 }
Exemplo n.º 2
0
 public static UserAgent fromHttpMessage(HttpMessage httpMessage) {
   Header firstHeader = httpMessage.getFirstHeader(NAME);
   if (firstHeader == null) {
     return null;
   }
   return new UserAgent(firstHeader.getValue());
 }
Exemplo n.º 3
0
 /*
  * Retrieves the full header value by combining multiple headers and
  * separating with commas, canonicalizing whitespace along the way.
  *
  * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
  */
 public static String getCanonicalHeaderValue(HttpMessage r, String name) {
   if (isSingleHeader(name)) {
     Header h = r.getFirstHeader(name);
     return (h != null) ? h.getValue() : null;
   }
   StringBuilder buf = new StringBuilder();
   boolean first = true;
   for (Header h : r.getHeaders(name)) {
     if (!first) {
       buf.append(", ");
     }
     buf.append(h.getValue().trim());
     first = false;
   }
   return buf.toString();
 }