Java 类org.apache.http.util.CharArrayBuffer 实例源码

项目:lams    文件:NetscapeDraftHeaderParser.java   
public HeaderElement parseHeader(
        final CharArrayBuffer buffer,
        final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    NameValuePair nvp = parseNameValuePair(buffer, cursor);
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair param = parseNameValuePair(buffer, cursor);
        params.add(param);
    }
    return new BasicHeaderElement(
            nvp.getName(),
            nvp.getValue(), params.toArray(new NameValuePair[params.size()]));
}
项目:Hotspot-master-devp    文件:URLEncodedUtils.java   
/**
 * Returns a list of {@link org.apache.http.NameValuePair NameValuePairs} as parsed from the given string
 * using the given character encoding.
 *
 * @param s       text to parse.
 * @param charset Encoding to use when decoding the parameters.
 * @since 4.2
 */
public static List<NameValuePair> parse(final String s, final Charset charset) {
    if (s == null) {
        return Collections.emptyList();
    }
    BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(
                    decodeFormFields(nvp.getName(), charset),
                    decodeFormFields(nvp.getValue(), charset)));
        }
    }
    return list;
}
项目:ats-framework    文件:GGSSchemeBase.java   
@Override
protected void parseChallenge(
                               final CharArrayBuffer buffer,
                               int beginIndex,
                               int endIndex ) throws MalformedChallengeException {

    String challenge = buffer.substringTrimmed(beginIndex, endIndex);
    if (log.isDebugEnabled()) {
        log.debug("Received challenge '" + challenge + "' from the auth server");
    }
    if (state == State.UNINITIATED) {
        token = base64codec.decode(challenge.getBytes());
        state = State.CHALLENGE_RECEIVED;
    } else {
        log.debug("Authentication already attempted");
        state = State.FAILED;
    }
}
项目:lams    文件:URLEncodedUtils.java   
/**
 * Returns a list of {@link NameValuePair NameValuePairs} as parsed from the given string
 * using the given character encoding.
 *
 * @param s
 *            text to parse.
 * @param charset
 *            Encoding to use when decoding the parameters.
 *
 * @since 4.2
 */
public static List<NameValuePair> parse (final String s, final Charset charset) {
    if (s == null) {
        return Collections.emptyList();
    }
    BasicHeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, buffer.length());
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, DELIM);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(
                    decodeFormFields(nvp.getName(), charset),
                    decodeFormFields(nvp.getValue(), charset)));
        }
    }
    return list;
}
项目:lams    文件:NetscapeDraftSpec.java   
public List<Header> formatCookies(final List<Cookie> cookies) {
    if (cookies == null) {
        throw new IllegalArgumentException("List of cookies may not be null");
    }
    if (cookies.isEmpty()) {
        throw new IllegalArgumentException("List of cookies may not be empty");
    }
    CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
    buffer.append(SM.COOKIE);
    buffer.append(": ");
    for (int i = 0; i < cookies.size(); i++) {
        Cookie cookie = cookies.get(i);
        if (i > 0) {
            buffer.append("; ");
        }
        buffer.append(cookie.getName());
        String s = cookie.getValue();
        if (s != null) {
            buffer.append("=");
            buffer.append(s);
        }
    }
    List<Header> headers = new ArrayList<Header>(1);
    headers.add(new BufferedHeader(buffer));
    return headers;
}
项目:lams    文件:BrowserCompatSpec.java   
public List<Header> formatCookies(final List<Cookie> cookies) {
    if (cookies == null) {
        throw new IllegalArgumentException("List of cookies may not be null");
    }
    if (cookies.isEmpty()) {
        throw new IllegalArgumentException("List of cookies may not be empty");
    }
    CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
    buffer.append(SM.COOKIE);
    buffer.append(": ");
    for (int i = 0; i < cookies.size(); i++) {
        Cookie cookie = cookies.get(i);
        if (i > 0) {
            buffer.append("; ");
        }
        buffer.append(cookie.getName());
        buffer.append("=");
        String s = cookie.getValue();
        if (s != null) {
            buffer.append(s);
        }
    }
    List<Header> headers = new ArrayList<Header>(1);
    headers.add(new BufferedHeader(buffer));
    return headers;
}
项目:lams    文件:RFC2109Spec.java   
/**
 * Return a string suitable for sending in a <tt>"Cookie"</tt> header
 * as defined in RFC 2109 for backward compatibility with cookie version 0
 * @param buffer The char array buffer to use for output
 * @param cookie The {@link Cookie} to be formatted as string
 * @param version The version to use.
 */
protected void formatCookieAsVer(final CharArrayBuffer buffer,
        final Cookie cookie, int version) {
    formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version);
    if (cookie.getPath() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Path", cookie.getPath(), version);
        }
    }
    if (cookie.getDomain() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version);
        }
    }
}
项目:lams    文件:NTLMScheme.java   
@Override
protected void parseChallenge(
        final CharArrayBuffer buffer,
        int beginIndex, int endIndex) throws MalformedChallengeException {
    String challenge = buffer.substringTrimmed(beginIndex, endIndex);
    if (challenge.length() == 0) {
        if (this.state == State.UNINITIATED) {
            this.state = State.CHALLENGE_RECEIVED;
        } else {
            this.state = State.FAILED;
        }
        this.challenge = null;
    } else {
        this.state = State.MSG_TYPE2_RECEVIED;
        this.challenge = challenge;
    }
}
项目:lams    文件:GGSSchemeBase.java   
@Override
protected void parseChallenge(
        final CharArrayBuffer buffer,
        int beginIndex, int endIndex) throws MalformedChallengeException {
    String challenge = buffer.substringTrimmed(beginIndex, endIndex);
    if (log.isDebugEnabled()) {
        log.debug("Received challenge '" + challenge + "' from the auth server");
    }
    if (state == State.UNINITIATED) {
        token = base64codec.decode(challenge.getBytes());
        state = State.CHALLENGE_RECEIVED;
    } else {
        log.debug("Authentication already attempted");
        state = State.FAILED;
    }
}
项目:lams    文件:BasicHeaderValueFormatter.java   
/**
 * 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('"');
    }
}
项目:lams    文件:BasicHeaderValueParser.java   
/**
 * Parses elements with the given parser.
 *
 * @param value     the header value to parse
 * @param parser    the parser to use, or <code>null</code> for default
 *
 * @return  array holding the header elements, never <code>null</code>
 */
public final static
    HeaderElement[] parseElements(final String value,
                                  HeaderValueParser parser)
    throws ParseException {

    if (value == null) {
        throw new IllegalArgumentException
            ("Value to parse may not be null");
    }

    if (parser == null)
        parser = BasicHeaderValueParser.DEFAULT;

    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    ParserCursor cursor = new ParserCursor(0, value.length());
    return parser.parseElements(buffer, cursor);
}
项目:lams    文件:BasicHeaderValueParser.java   
public HeaderElement[] parseElements(final CharArrayBuffer buffer,
                                     final ParserCursor cursor) {

    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }

    List<HeaderElement> elements = new ArrayList<HeaderElement>();
    while (!cursor.atEnd()) {
        HeaderElement element = parseHeaderElement(buffer, cursor);
        if (!(element.getName().length() == 0 && element.getValue() == null)) {
            elements.add(element);
        }
    }
    return elements.toArray(new HeaderElement[elements.size()]);
}
项目:lams    文件:BasicHeaderValueParser.java   
/**
 * Parses an element with the given parser.
 *
 * @param value     the header element to parse
 * @param parser    the parser to use, or <code>null</code> for default
 *
 * @return  the parsed header element
 */
public final static
    HeaderElement parseHeaderElement(final String value,
                                     HeaderValueParser parser)
    throws ParseException {

    if (value == null) {
        throw new IllegalArgumentException
            ("Value to parse may not be null");
    }

    if (parser == null)
        parser = BasicHeaderValueParser.DEFAULT;

    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    ParserCursor cursor = new ParserCursor(0, value.length());
    return parser.parseHeaderElement(buffer, cursor);
}
项目:lams    文件:BasicHeaderValueParser.java   
public HeaderElement parseHeaderElement(final CharArrayBuffer buffer,
                                        final ParserCursor cursor) {

    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }

    NameValuePair nvp = parseNameValuePair(buffer, cursor);
    NameValuePair[] params = null;
    if (!cursor.atEnd()) {
        char ch = buffer.charAt(cursor.getPos() - 1);
        if (ch != ELEM_DELIMITER) {
            params = parseParameters(buffer, cursor);
        }
    }
    return createHeaderElement(nvp.getName(), nvp.getValue(), params);
}
项目:lams    文件:BasicHeaderValueParser.java   
/**
 * Parses parameters with the given parser.
 *
 * @param value     the parameter list to parse
 * @param parser    the parser to use, or <code>null</code> for default
 *
 * @return  array holding the parameters, never <code>null</code>
 */
public final static
    NameValuePair[] parseParameters(final String value,
                                    HeaderValueParser parser)
    throws ParseException {

    if (value == null) {
        throw new IllegalArgumentException
            ("Value to parse may not be null");
    }

    if (parser == null)
        parser = BasicHeaderValueParser.DEFAULT;

    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    ParserCursor cursor = new ParserCursor(0, value.length());
    return parser.parseParameters(buffer, cursor);
}
项目:lams    文件:BasicHeaderValueParser.java   
/**
 * Parses a name-value-pair with the given parser.
 *
 * @param value     the NVP to parse
 * @param parser    the parser to use, or <code>null</code> for default
 *
 * @return  the parsed name-value pair
 */
public final static
   NameValuePair parseNameValuePair(final String value,
                                    HeaderValueParser parser)
    throws ParseException {

    if (value == null) {
        throw new IllegalArgumentException
            ("Value to parse may not be null");
    }

    if (parser == null)
        parser = BasicHeaderValueParser.DEFAULT;

    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    ParserCursor cursor = new ParserCursor(0, value.length());
    return parser.parseNameValuePair(buffer, cursor);
}
项目:lams    文件:HeaderGroup.java   
/**
 * Gets a header representing all of the header values with the given name.
 * If more that one header with the given name exists the values will be
 * combined with a "," as per RFC 2616.
 *
 * <p>Header name comparison is case insensitive.
 *
 * @param name the name of the header(s) to get
 * @return a header with a condensed value or <code>null</code> if no
 * headers by the given name are present
 */
public Header getCondensedHeader(String name) {
    Header[] headers = getHeaders(name);

    if (headers.length == 0) {
        return null;
    } else if (headers.length == 1) {
        return headers[0];
    } else {
        CharArrayBuffer valueBuffer = new CharArrayBuffer(128);
        valueBuffer.append(headers[0].getValue());
        for (int i = 1; i < headers.length; i++) {
            valueBuffer.append(", ");
            valueBuffer.append(headers[i].getValue());
        }

        return new BasicHeader(name.toLowerCase(Locale.ENGLISH), valueBuffer.toString());
    }
}
项目:lams    文件:BasicHeaderElementIterator.java   
private void bufferHeaderValue() {
    this.cursor = null;
    this.buffer = null;
    while (this.headerIt.hasNext()) {
        Header h = this.headerIt.nextHeader();
        if (h instanceof FormattedHeader) {
            this.buffer = ((FormattedHeader) h).getBuffer();
            this.cursor = new ParserCursor(0, this.buffer.length());
            this.cursor.updatePos(((FormattedHeader) h).getValuePos());
            break;
        } else {
            String value = h.getValue();
            if (value != null) {
                this.buffer = new CharArrayBuffer(value.length());
                this.buffer.append(value);
                this.cursor = new ParserCursor(0, this.buffer.length());
                break;
            }
        }
    }
}
项目:lams    文件:BufferedHeader.java   
/**
 * Creates a new header from a buffer.
 * The name of the header will be parsed immediately,
 * the value only if it is accessed.
 *
 * @param buffer    the buffer containing the header to represent
 *
 * @throws ParseException   in case of a parse error
 */
public BufferedHeader(final CharArrayBuffer buffer)
    throws ParseException {

    super();
    if (buffer == null) {
        throw new IllegalArgumentException
            ("Char array buffer may not be null");
    }
    int colon = buffer.indexOf(':');
    if (colon == -1) {
        throw new ParseException
            ("Invalid header: " + buffer.toString());
    }
    String s = buffer.substringTrimmed(0, colon);
    if (s.length() == 0) {
        throw new ParseException
            ("Invalid header: " + buffer.toString());
    }
    this.buffer = buffer;
    this.name = s;
    this.valuePos = colon + 1;
}
项目:lams    文件:BasicLineParser.java   
public final static
    ProtocolVersion parseProtocolVersion(String value,
                                         LineParser parser)
    throws ParseException {

    if (value == null) {
        throw new IllegalArgumentException
            ("Value to parse may not be null.");
    }

    if (parser == null)
        parser = BasicLineParser.DEFAULT;

    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    ParserCursor cursor = new ParserCursor(0, value.length());
    return parser.parseProtocolVersion(buffer, cursor);
}
项目:lams    文件:BasicLineParser.java   
public final static
    RequestLine parseRequestLine(final String value,
                                 LineParser parser)
    throws ParseException {

    if (value == null) {
        throw new IllegalArgumentException
            ("Value to parse may not be null.");
    }

    if (parser == null)
        parser = BasicLineParser.DEFAULT;

    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    ParserCursor cursor = new ParserCursor(0, value.length());
    return parser.parseRequestLine(buffer, cursor);
}
项目:lams    文件:BasicLineParser.java   
public final static
    StatusLine parseStatusLine(final String value,
                               LineParser parser)
    throws ParseException {

    if (value == null) {
        throw new IllegalArgumentException
            ("Value to parse may not be null.");
    }

    if (parser == null)
        parser = BasicLineParser.DEFAULT;

    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    ParserCursor cursor = new ParserCursor(0, value.length());
    return parser.parseStatusLine(buffer, cursor);
}
项目:lams    文件:BasicLineParser.java   
public final static
    Header parseHeader(final String value,
                       LineParser parser)
    throws ParseException {

    if (value == null) {
        throw new IllegalArgumentException
            ("Value to parse may not be null");
    }

    if (parser == null)
        parser = BasicLineParser.DEFAULT;

    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    return parser.parseHeader(buffer);
}
项目:lams    文件:BasicLineFormatter.java   
/**
 * Actually formats a request line.
 * Called from {@link #formatRequestLine}.
 *
 * @param buffer    the empty buffer into which to format,
 *                  never <code>null</code>
 * @param reqline   the request line to format, never <code>null</code>
 */
protected void doFormatRequestLine(final CharArrayBuffer buffer,
                                   final RequestLine reqline) {
    final String method = reqline.getMethod();
    final String uri    = reqline.getUri();

    // room for "GET /index.html HTTP/1.1"
    int len = method.length() + 1 + uri.length() + 1 +
        estimateProtocolVersionLen(reqline.getProtocolVersion());
    buffer.ensureCapacity(len);

    buffer.append(method);
    buffer.append(' ');
    buffer.append(uri);
    buffer.append(' ');
    appendProtocolVersion(buffer, reqline.getProtocolVersion());
}
项目:lams    文件:BasicLineFormatter.java   
/**
 * Actually formats a status line.
 * Called from {@link #formatStatusLine}.
 *
 * @param buffer    the empty buffer into which to format,
 *                  never <code>null</code>
 * @param statline  the status line to format, never <code>null</code>
 */
protected void doFormatStatusLine(final CharArrayBuffer buffer,
                                  final StatusLine statline) {

    int len = estimateProtocolVersionLen(statline.getProtocolVersion())
        + 1 + 3 + 1; // room for "HTTP/1.1 200 "
    final String reason = statline.getReasonPhrase();
    if (reason != null) {
        len += reason.length();
    }
    buffer.ensureCapacity(len);

    appendProtocolVersion(buffer, statline.getProtocolVersion());
    buffer.append(' ');
    buffer.append(Integer.toString(statline.getStatusCode()));
    buffer.append(' '); // keep whitespace even if reason phrase is empty
    if (reason != null) {
        buffer.append(reason);
    }
}
项目:lams    文件:BasicLineFormatter.java   
public CharArrayBuffer formatHeader(CharArrayBuffer buffer,
                                    Header header) {
    if (header == null) {
        throw new IllegalArgumentException
            ("Header may not be null");
    }
    CharArrayBuffer result = null;

    if (header instanceof FormattedHeader) {
        // If the header is backed by a buffer, re-use the buffer
        result = ((FormattedHeader)header).getBuffer();
    } else {
        result = initBuffer(buffer);
        doFormatHeader(result, header);
    }
    return result;

}
项目:lams    文件:BasicLineFormatter.java   
/**
 * Actually formats a header.
 * Called from {@link #formatHeader}.
 *
 * @param buffer    the empty buffer into which to format,
 *                  never <code>null</code>
 * @param header    the header to format, never <code>null</code>
 */
protected void doFormatHeader(final CharArrayBuffer buffer,
                              final Header header) {
    final String name = header.getName();
    final String value = header.getValue();

    int len = name.length() + 2;
    if (value != null) {
        len += value.length();
    }
    buffer.ensureCapacity(len);

    buffer.append(name);
    buffer.append(": ");
    if (value != null) {
        buffer.append(value);
    }
}
项目:lams    文件:AbstractSessionInputBuffer.java   
/**
 * Reads a complete line of characters up to a line delimiter from this
 * session buffer. The line delimiter itself is discarded. If no char is
 * available because the end of the stream has been reached,
 * <code>null</code> is returned. This method blocks until input data is
 * available, end of file is detected, or an exception is thrown.
 * <p>
 * This method treats a lone LF as a valid line delimiters in addition
 * to CR-LF required by the HTTP specification.
 *
 * @return HTTP line as a string
 * @exception  IOException  if an I/O error occurs.
 */
private int lineFromLineBuffer(final CharArrayBuffer charbuffer)
        throws IOException {
    // discard LF if found
    int len = this.linebuffer.length();
    if (len > 0) {
        if (this.linebuffer.byteAt(len - 1) == HTTP.LF) {
            len--;
        }
        // discard CR if found
        if (len > 0) {
            if (this.linebuffer.byteAt(len - 1) == HTTP.CR) {
                len--;
            }
        }
    }
    if (this.ascii) {
        charbuffer.append(this.linebuffer, 0, len);
    } else {
        ByteBuffer bbuf =  ByteBuffer.wrap(this.linebuffer.buffer(), 0, len);
        len = appendDecoded(charbuffer, bbuf);
    }
    this.linebuffer.clear();
    return len;
}
项目:lams    文件:AbstractSessionInputBuffer.java   
private int lineFromReadBuffer(final CharArrayBuffer charbuffer, int pos)
        throws IOException {
    int off = this.bufferpos;
    int len;
    this.bufferpos = pos + 1;
    if (pos > off && this.buffer[pos - 1] == HTTP.CR) {
        // skip CR if found
        pos--;
    }
    len = pos - off;
    if (this.ascii) {
        charbuffer.append(this.buffer, off, len);
    } else {
        ByteBuffer bbuf =  ByteBuffer.wrap(this.buffer, off, len);
        len = appendDecoded(charbuffer, bbuf);
    }
    return len;
}
项目:lams    文件:AbstractSessionOutputBuffer.java   
/**
 * Writes characters from the specified char array followed by a line
 * delimiter to this session buffer.
 * <p>
 * This method uses CR-LF as a line delimiter.
 *
 * @param      charbuffer the buffer containing chars of the line.
 * @exception  IOException  if an I/O error occurs.
 */
public void writeLine(final CharArrayBuffer charbuffer) throws IOException {
    if (charbuffer == null) {
        return;
    }
    if (this.ascii) {
        int off = 0;
        int remaining = charbuffer.length();
        while (remaining > 0) {
            int chunk = this.buffer.capacity() - this.buffer.length();
            chunk = Math.min(chunk, remaining);
            if (chunk > 0) {
                this.buffer.append(charbuffer, off, chunk);
            }
            if (this.buffer.isFull()) {
                flushBuffer();
            }
            off += chunk;
            remaining -= chunk;
        }
    } else {
        CharBuffer cbuf = CharBuffer.wrap(charbuffer.buffer(), 0, charbuffer.length());
        writeEncoded(cbuf);
    }
    write(CRLF);
}
项目:lams    文件:AbstractMessageParser.java   
/**
 * Creates an instance of this class.
 *
 * @param buffer the session input buffer.
 * @param parser the line parser.
 * @param params HTTP parameters.
 */
public AbstractMessageParser(
        final SessionInputBuffer buffer,
        final LineParser parser,
        final HttpParams params) {
    super();
    if (buffer == null) {
        throw new IllegalArgumentException("Session input buffer may not be null");
    }
    if (params == null) {
        throw new IllegalArgumentException("HTTP parameters may not be null");
    }
    this.sessionBuffer = buffer;
    this.maxHeaderCount = params.getIntParameter(
            CoreConnectionPNames.MAX_HEADER_COUNT, -1);
    this.maxLineLen = params.getIntParameter(
            CoreConnectionPNames.MAX_LINE_LENGTH, -1);
    this.lineParser = (parser != null) ? parser : BasicLineParser.DEFAULT;
    this.headerLines = new ArrayList<CharArrayBuffer>();
    this.state = HEAD_LINE;
}
项目:diadocsdk-java    文件:DiadocAuthScheme.java   
/**
 * Returns a Diadoc <tt>Authorization</tt> header value for the given
 * {@link DiadocCredentials}.
 *
 * @param credentials The credentials to encode.
 * @return a Diadoc authorization header
 */
public static Header authenticate(final DiadocCredentials credentials) {
    if (credentials == null) {
        throw new IllegalArgumentException("Credentials may not be null");
    }

    StringBuilder sb = new StringBuilder();
    sb.append("ddauth_api_client_id=");
    sb.append(credentials.getApiClientId());
    if (credentials.getAuthToken() != null) {
        sb.append(",ddauth_token=");
        sb.append(credentials.getAuthToken());
    }

    CharArrayBuffer buffer = new CharArrayBuffer(128);
    buffer.append(AUTH.WWW_AUTH_RESP);
    buffer.append(": DiadocAuth ");
    buffer.append(sb.toString());

    return new BufferedHeader(buffer);
}
项目:remote-files-sync    文件:BasicHeaderValueFormatterHC4.java   
/**
 * 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,
                             final boolean quote) {

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

    if (quoteFlag) {
        buffer.append('"');
    }
    for (int i = 0; i < value.length(); i++) {
        final char ch = value.charAt(i);
        if (isUnsafe(ch)) {
            buffer.append('\\');
        }
        buffer.append(ch);
    }
    if (quoteFlag) {
        buffer.append('"');
    }
}
项目:remote-files-sync    文件:BasicLineFormatterHC4.java   
/**
 * Actually formats a request line.
 * Called from {@link #formatRequestLine}.
 *
 * @param buffer    the empty buffer into which to format,
 *                  never <code>null</code>
 * @param reqline   the request line to format, never <code>null</code>
 */
protected void doFormatRequestLine(final CharArrayBuffer buffer,
                                   final RequestLine reqline) {
    final String method = reqline.getMethod();
    final String uri    = reqline.getUri();

    // room for "GET /index.html HTTP/1.1"
    final int len = method.length() + 1 + uri.length() + 1 +
        estimateProtocolVersionLen(reqline.getProtocolVersion());
    buffer.ensureCapacity(len);

    buffer.append(method);
    buffer.append(' ');
    buffer.append(uri);
    buffer.append(' ');
    appendProtocolVersion(buffer, reqline.getProtocolVersion());
}
项目:remote-files-sync    文件:BasicLineFormatterHC4.java   
/**
 * Actually formats a status line.
 * Called from {@link #formatStatusLine}.
 *
 * @param buffer    the empty buffer into which to format,
 *                  never <code>null</code>
 * @param statline  the status line to format, never <code>null</code>
 */
protected void doFormatStatusLine(final CharArrayBuffer buffer,
                                  final StatusLine statline) {

    int len = estimateProtocolVersionLen(statline.getProtocolVersion())
        + 1 + 3 + 1; // room for "HTTP/1.1 200 "
    final String reason = statline.getReasonPhrase();
    if (reason != null) {
        len += reason.length();
    }
    buffer.ensureCapacity(len);

    appendProtocolVersion(buffer, statline.getProtocolVersion());
    buffer.append(' ');
    buffer.append(Integer.toString(statline.getStatusCode()));
    buffer.append(' '); // keep whitespace even if reason phrase is empty
    if (reason != null) {
        buffer.append(reason);
    }
}
项目:remote-files-sync    文件:BasicLineFormatterHC4.java   
/**
 * Actually formats a header.
 * Called from {@link #formatHeader}.
 *
 * @param buffer    the empty buffer into which to format,
 *                  never <code>null</code>
 * @param header    the header to format, never <code>null</code>
 */
protected void doFormatHeader(final CharArrayBuffer buffer,
                              final Header header) {
    final String name = header.getName();
    final String value = header.getValue();

    int len = name.length() + 2;
    if (value != null) {
        len += value.length();
    }
    buffer.ensureCapacity(len);

    buffer.append(name);
    buffer.append(": ");
    if (value != null) {
        buffer.append(value);
    }
}
项目:remote-files-sync    文件:URLEncodedUtilsHC4.java   
/**
 * Returns a list of {@link NameValuePair NameValuePairs} as parsed from the given string using the given character
 * encoding.
 *
 * @param s
 *            text to parse.
 * @param charset
 *            Encoding to use when decoding the parameters.
 * @param parameterSeparator
 *            The characters used to separate parameters, by convention, {@code '&'} and {@code ';'}.
 * @return a list of {@link NameValuePair} as built from the URI's query portion.
 *
 * @since 4.3
 */
public static List<NameValuePair> parse(final String s, final Charset charset, final char... parameterSeparator) {
    if (s == null) {
        return Collections.emptyList();
    }
    final BasicHeaderValueParserHC4 parser = BasicHeaderValueParserHC4.INSTANCE;
    final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
    buffer.append(s);
    final ParserCursor cursor = new ParserCursor(0, buffer.length());
    final List<NameValuePair> list = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        final NameValuePair nvp = parser.parseNameValuePair(buffer, cursor, parameterSeparator);
        if (nvp.getName().length() > 0) {
            list.add(new BasicNameValuePair(
                    decodeFormFields(nvp.getName(), charset),
                    decodeFormFields(nvp.getValue(), charset)));
        }
    }
    return list;
}
项目:remote-files-sync    文件:RFC2109SpecHC4.java   
/**
 * Return a string suitable for sending in a <tt>"Cookie"</tt> header
 * as defined in RFC 2109 for backward compatibility with cookie version 0
 * @param buffer The char array buffer to use for output
 * @param cookie The {@link Cookie} to be formatted as string
 * @param version The version to use.
 */
protected void formatCookieAsVer(final CharArrayBuffer buffer,
        final Cookie cookie, final int version) {
    formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version);
    if (cookie.getPath() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Path", cookie.getPath(), version);
        }
    }
    if (cookie.getDomain() != null) {
        if (cookie instanceof ClientCookie
                && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
            buffer.append("; ");
            formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version);
        }
    }
}
项目:purecloud-iot    文件:TestDefaultHttpResponseParser.java   
@Test(expected=ProtocolException.class)
public void testResponseParsingWithTooMuchGarbage() throws Exception {
    final String s =
        "garbage\r\n" +
        "garbage\r\n" +
        "more garbage\r\n" +
        "HTTP/1.1 200 OK\r\n" +
        "header1: value1\r\n" +
        "header2: value2\r\n" +
        "\r\n";

    final SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
    final HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer) {

        @Override
        protected boolean reject(final CharArrayBuffer line, final int count) {
            return count >= 2;
        }

    };
    parser.parse();
}
项目:remote-files-sync    文件:NTLMSchemeHC4.java   
@Override
protected void parseChallenge(
        final CharArrayBuffer buffer,
        final int beginIndex, final int endIndex) throws MalformedChallengeException {
    this.challenge = buffer.substringTrimmed(beginIndex, endIndex);
    if (this.challenge.length() == 0) {
        if (this.state == State.UNINITIATED) {
            this.state = State.CHALLENGE_RECEIVED;
        } else {
            this.state = State.FAILED;
        }
    } else {
        if (this.state.compareTo(State.MSG_TYPE1_GENERATED) < 0) {
            this.state = State.FAILED;
            throw new MalformedChallengeException("Out of sequence NTLM response message");
        } else if (this.state == State.MSG_TYPE1_GENERATED) {
            this.state = State.MSG_TYPE2_RECEVIED;
        }
    }
}