// non-javadoc, see interface HeaderValueFormatter
  public CharArrayBuffer formatHeaderElement(
      CharArrayBuffer buffer, final HeaderElement elem, final boolean quote) {
    if (elem == null) {
      throw new IllegalArgumentException("Header element must not be null.");
    }

    int len = estimateHeaderElementLen(elem);
    if (buffer == null) {
      buffer = new CharArrayBuffer(len);
    } else {
      buffer.ensureCapacity(len);
    }

    buffer.append(elem.getName());
    final String value = elem.getValue();
    if (value != null) {
      buffer.append('=');
      doFormatValue(buffer, value, quote);
    }

    final int parcnt = elem.getParameterCount();
    if (parcnt > 0) {
      for (int i = 0; i < parcnt; i++) {
        buffer.append("; ");
        formatNameValuePair(buffer, elem.getParameter(i), quote);
      }
    }

    return buffer;
  }
예제 #2
0
 /** Adds valid Port attribute value, e.g. "8000,8001,8002" */
 @Override
 protected void formatCookieAsVer(final CharArrayBuffer buffer, final Cookie cookie, int version) {
   super.formatCookieAsVer(buffer, cookie, version);
   // format port attribute
   if (cookie instanceof ClientCookie) {
     // Test if the port attribute as set by the origin server is not blank
     String s = ((ClientCookie) cookie).getAttribute(ClientCookie.PORT_ATTR);
     if (s != null) {
       buffer.append("; $Port");
       buffer.append("=\"");
       if (s.trim().length() > 0) {
         int[] ports = cookie.getPorts();
         if (ports != null) {
           for (int i = 0, len = ports.length; i < len; i++) {
             if (i > 0) {
               buffer.append(",");
             }
             buffer.append(Integer.toString(ports[i]));
           }
         }
       }
       buffer.append("\"");
     }
   }
 }
예제 #3
0
 @Override
 public Header getVersionHeader() {
   CharArrayBuffer buffer = new CharArrayBuffer(40);
   buffer.append(SM.COOKIE2);
   buffer.append(": ");
   buffer.append("$Version=");
   buffer.append(Integer.toString(getVersion()));
   return new BufferedHeader(buffer);
 }
  // non-javadoc, see interface HeaderValueFormatter
  public CharArrayBuffer formatElements(
      CharArrayBuffer buffer, final HeaderElement[] elems, final boolean quote) {
    if (elems == null) {
      throw new IllegalArgumentException("Header element array must not be null.");
    }

    int len = estimateElementsLen(elems);
    if (buffer == null) {
      buffer = new CharArrayBuffer(len);
    } else {
      buffer.ensureCapacity(len);
    }

    for (int i = 0; i < elems.length; i++) {
      if (i > 0) {
        buffer.append(", ");
      }
      formatHeaderElement(buffer, elems[i], quote);
    }

    return buffer;
  }
  /**
   * Actually formats the value of a name-value pair. This does not include a leading = character.
   * Called from {@link #formatNameValuePair formatNameValuePair}.
   *
   * @param buffer the buffer to append to, never <code>null</code>
   * @param value the value to append, never <code>null</code>
   * @param quote <code>true</code> to always format with quotes, <code>false</code> to use quotes
   *     only when necessary
   */
  protected void doFormatValue(final CharArrayBuffer buffer, final String value, boolean quote) {

    if (!quote) {
      for (int i = 0; (i < value.length()) && !quote; i++) {
        quote = isSeparator(value.charAt(i));
      }
    }

    if (quote) {
      buffer.append('"');
    }
    for (int i = 0; i < value.length(); i++) {
      char ch = value.charAt(i);
      if (isUnsafe(ch)) {
        buffer.append('\\');
      }
      buffer.append(ch);
    }
    if (quote) {
      buffer.append('"');
    }
  }
  // non-javadoc, see interface HeaderValueFormatter
  public CharArrayBuffer formatNameValuePair(
      CharArrayBuffer buffer, final NameValuePair nvp, final boolean quote) {
    if (nvp == null) {
      throw new IllegalArgumentException("NameValuePair must not be null.");
    }

    int len = estimateNameValuePairLen(nvp);
    if (buffer == null) {
      buffer = new CharArrayBuffer(len);
    } else {
      buffer.ensureCapacity(len);
    }

    buffer.append(nvp.getName());
    final String value = nvp.getValue();
    if (value != null) {
      buffer.append('=');
      doFormatValue(buffer, value, quote);
    }

    return buffer;
  }
  // non-javadoc, see interface HeaderValueFormatter
  public CharArrayBuffer formatParameters(
      CharArrayBuffer buffer, NameValuePair[] nvps, boolean quote) {
    if (nvps == null) {
      throw new IllegalArgumentException("Parameters must not be null.");
    }

    int len = estimateParametersLen(nvps);
    if (buffer == null) {
      buffer = new CharArrayBuffer(len);
    } else {
      buffer.ensureCapacity(len);
    }

    for (int i = 0; i < nvps.length; i++) {
      if (i > 0) {
        buffer.append("; ");
      }
      formatNameValuePair(buffer, nvps[i], quote);
    }

    return buffer;
  }