Example usage for org.apache.http.message ParserCursor getUpperBound

List of usage examples for org.apache.http.message ParserCursor getUpperBound

Introduction

In this page you can find the example usage for org.apache.http.message ParserCursor getUpperBound.

Prototype

public int getUpperBound() 

Source Link

Usage

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Skip SP and HT characters in a line.   
 * /*from w w w  . j  av a2  s.  c om*/
 * @param lnBuf Buffer containing line.
 * @param lnCursor Parser cursor for lnBuf. Adjusted to one character after and SP and HT.
 */
protected void skipSpHt(final CharArrayBuffer lnBuf, final ParserCursor lnCursor) {
    int idxTo = lnCursor.getUpperBound();
    int idxPos = lnCursor.getPos();
    while (idxPos < idxTo && (lnBuf.charAt(idxPos) == HTTP.SP || lnBuf.charAt(idxPos) == HTTP.HT)) {
        idxPos++;
    }
    lnCursor.updatePos(idxPos);
}

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Get the next word from a buffer containing a line.
 * /*from ww  w  . ja  v a2s  .  c om*/
 * @param lnBuf Buffer containing line.
 * @param lnCursor Parser cursor for lnBuf. Adjusted to one character after the word.
 * @return Next word, or null if none is found.
 */
protected String nextWord(final CharArrayBuffer lnBuf, final ParserCursor lnCursor) {
    skipSpHt(lnBuf, lnCursor);
    int idxPos = lnCursor.getPos();
    int idxLineEnd = lnBuf.indexOf(' ', idxPos, lnCursor.getUpperBound());
    if (idxLineEnd < 0) {
        if (idxPos == lnCursor.getUpperBound()) {
            return null;
        }
        idxLineEnd = lnCursor.getUpperBound();
    }
    lnCursor.updatePos(idxLineEnd);
    return lnBuf.substringTrimmed(idxPos, idxLineEnd);
}

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Get the next line of characters from a CharArrayBuffer into another CharArrayBuffer. Treats LF and CRLF as valid
 * line delimiters. Treats the entire buffer as a line if no line delimiters are found.
 * //from  w ww .  j  av  a2s.  com
 * @param src Source buffer to read line from.
 * @param srcCursor Parser cursor for src. Adjusted to discard line delimiters.
 * @param dst Destination buffer for characters from line. 
 * @return Number of characters in line minus line delimiters, or < 0 if none found.
 */
protected int readLine(final CharArrayBuffer src, final ParserCursor srcCursor, final CharArrayBuffer dst) {
    if (srcCursor.atEnd()) {
        return -1;
    }

    int idxPos = srcCursor.getPos();
    int idxLf = src.indexOf(HTTP.LF, idxPos, srcCursor.getUpperBound());
    int idxEnd;

    if (idxLf > 0) {
        if (src.charAt(idxLf - 1) == HTTP.CR) {
            idxEnd = idxLf - 1;
        } else {
            idxEnd = idxLf;
        }
    } else {
        idxEnd = srcCursor.getUpperBound();
        idxLf = idxEnd - 1;
    }

    dst.append(src, idxPos, idxEnd - idxPos);
    srcCursor.updatePos(idxLf + 1);
    return idxEnd - idxPos;
}

From source file:com.ok2c.lightmtp.util.InetAddressRangeParser.java

public InetAddressRange parse(final CharArrayBuffer buffer, final ParserCursor cursor, final char[] delimiters)
        throws ParseException, UnknownHostException {

    Args.notNull(buffer, "Char array buffer");
    Args.notNull(cursor, "Parser cursor");

    int pos = cursor.getPos();
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();

    while (pos < indexTo) {
        char ch = buffer.charAt(pos);
        if (ch == '/') {
            break;
        }//w w  w .j  a  v a2s .  co m
        if (isOneOf(ch, delimiters)) {
            break;
        }
        pos++;
    }

    InetAddress address = InetAddress.getByName(buffer.substringTrimmed(indexFrom, pos));
    int mask = 0;

    if (pos < indexTo && buffer.charAt(pos) == '/') {
        pos++;
        indexFrom = pos;
        while (pos < indexTo) {
            char ch = buffer.charAt(pos);
            if (isOneOf(ch, delimiters)) {
                break;
            }
            pos++;
        }
        try {
            mask = Integer.parseInt(buffer.substringTrimmed(indexFrom, pos));
            if (mask < 0) {
                throw new ParseException("Negative range mask", indexFrom);
            }
        } catch (NumberFormatException ex) {
            throw new ParseException("Invalid range mask", indexFrom);
        }
    }
    cursor.updatePos(pos);
    return new InetAddressRange(address, mask);
}

From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Get the next header line of characters from a CharArrayBuffer into another CharArrayBuffer. Treats LF and CRLF as
 * valid line delimiters. Treats the entire buffer as a line if no line delimiters are found. Supports folded header
 * field values as per the HTTP/1.1 specification.
 *    //  w w  w .  j  ava  2s. co m
 * @param src Source buffer to read line from.
 * @param srcCursor Parser cursor for src. Adjusted to discard line delimiters.
 * @param dst Destination buffer for characters from line. 
 * @return Number of characters in line minus line delimiters, or < 0 if none found.
 */
protected int readLineHeader(final CharArrayBuffer src, final ParserCursor srcCursor,
        final CharArrayBuffer dst) {
    if (srcCursor.atEnd()) {
        return -1;
    }

    int idxPos = srcCursor.getPos();
    int chCnt = 0;
    int idxLf, idxEnd;

    do {
        idxLf = src.indexOf(HTTP.LF, idxPos, srcCursor.getUpperBound());

        if (idxLf > 0) {
            if (src.charAt(idxLf - 1) == HTTP.CR) {
                idxEnd = idxLf - 1;
            } else {
                idxEnd = idxLf;
            }
        } else {
            idxEnd = srcCursor.getUpperBound();
            idxLf = idxEnd - 1;
        }

        if (chCnt != 0) {
            while (idxPos < idxEnd && (src.charAt(idxPos) == HTTP.HT || src.charAt(idxPos) == HTTP.SP)) {
                idxPos++;
            }
            if (idxPos != idxEnd) {
                dst.append(' ');
            }
        }

        dst.append(src, idxPos, idxEnd - idxPos);
        chCnt += idxEnd - idxPos;
        idxPos = idxLf + 1;
        srcCursor.updatePos(idxPos);
    } while (idxPos < srcCursor.getUpperBound()
            && (src.charAt(idxPos) == HTTP.HT || src.charAt(idxPos) == HTTP.SP));

    return chCnt;
}

From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java

public NameValuePair[] parseParameters(final CharArrayBuffer buffer, final ParserCursor cursor) {
    AssertUtils.notNull(buffer, "Char array buffer");
    AssertUtils.notNull(cursor, "Parser cursor");
    int pos = cursor.getPos();
    final int indexTo = cursor.getUpperBound();

    while (pos < indexTo) {
        final char ch = buffer.charAt(pos);
        if (HTTP.isWhitespace(ch)) {
            pos++;/*from w  ww.jav a 2  s .  c o  m*/
        } else {
            break;
        }
    }
    cursor.updatePos(pos);
    if (cursor.atEnd()) {
        return new NameValuePair[] {};
    }

    final List<NameValuePair> params = new ArrayList<NameValuePair>();
    while (!cursor.atEnd()) {
        final NameValuePair param = parseNameValuePair(buffer, cursor);
        params.add(param);
        final char ch = buffer.charAt(cursor.getPos() - 1);
        if (ch == ELEM_DELIMITER) {
            break;
        }
    }

    return params.toArray(new NameValuePair[params.size()]);
}

From source file:com.subgraph.vega.ui.httpeditor.parser.HttpResponseParser.java

/**
 * Parse a HTTP response in a string./* www  . j ava 2 s  .  com*/
 * 
 * @param content HTTP response string.
 * @return HttpResponse, or null if the given HTTP response was empty. 
 * @throws UnsupportedEncodingException
 */
public void parseResponse(final String content) throws UnsupportedEncodingException {
    final CharArrayBuffer buf = new CharArrayBuffer(0);
    buf.append(content);
    final ParserCursor bufCursor = new ParserCursor(0, buf.length());
    final LineParser parser = new BasicLineParser();

    if (parseStatusLine(parser, builder, buf, bufCursor) < 0) {
        return;
    }
    builder.clearHeaders();
    parseHeaders(parser, builder, buf, bufCursor);
    if (!bufCursor.atEnd() && parseInlineEntities) {
        StringEntity entity = new StringEntity(buf.substring(bufCursor.getPos(), bufCursor.getUpperBound()));
        builder.setEntity(entity);
    }
}

From source file:com.subgraph.vega.ui.httpeditor.parser.HttpRequestParser.java

/**
 * Parse a manually-entered HTTP request into the IHttpRequestBuilder.
 * /*from www.  j a  va 2s.  c om*/
 * @param content Manually-entered HTTP request.
 */
public void parseRequest(final String content) throws URISyntaxException, UnsupportedEncodingException {
    final CharArrayBuffer buf = new CharArrayBuffer(0);
    buf.append(content);
    final ParserCursor bufCursor = new ParserCursor(0, buf.length());
    final LineParser parser = new BasicLineParser();

    if (parseRequestLine(parser, builder, buf, bufCursor) < 0) {
        return;
    }
    builder.clearHeaders();
    parseHeaders(parser, builder, buf, bufCursor);
    if (!bufCursor.atEnd() && parseInlineEntities) {
        StringEntity entity = new StringEntity(buf.substring(bufCursor.getPos(), bufCursor.getUpperBound()));
        builder.setEntity(entity);
    }
}

From source file:com.ok2c.lightmtp.util.InetAddressRangeParser.java

public List<InetAddressRange> parseAll(final CharArrayBuffer buffer, final ParserCursor cursor,
        final char[] delimiters) throws ParseException, UnknownHostException {

    Args.notNull(buffer, "Char array buffer");
    Args.notNull(cursor, "Parser cursor");

    char[] delims = delimiters != null ? delimiters : COMMA;

    List<InetAddressRange> ranges = new ArrayList<InetAddressRange>();
    while (!cursor.atEnd()) {
        ranges.add(parse(buffer, cursor, delims));
        int pos = cursor.getPos();
        if (pos < cursor.getUpperBound() && isOneOf(buffer.charAt(pos), delims)) {
            cursor.updatePos(pos + 1);//from  ww  w . ja  v  a2  s  .c  o  m
        }
    }
    return ranges;
}

From source file:com.mcxiaoke.next.http.entity.BasicHeaderValueParser.java

public NameValuePair parseNameValuePair(final CharArrayBuffer buffer, final ParserCursor cursor,
        final char[] delimiters) {
    AssertUtils.notNull(buffer, "Char array buffer");
    AssertUtils.notNull(cursor, "Parser cursor");

    boolean terminated = false;

    int pos = cursor.getPos();
    final int indexFrom = cursor.getPos();
    final int indexTo = cursor.getUpperBound();

    // Find name/*from   w w  w .j  a va  2s  .c  o  m*/
    final String name;
    while (pos < indexTo) {
        final char ch = buffer.charAt(pos);
        if (ch == '=') {
            break;
        }
        if (isOneOf(ch, delimiters)) {
            terminated = true;
            break;
        }
        pos++;
    }

    if (pos == indexTo) {
        terminated = true;
        name = buffer.substringTrimmed(indexFrom, indexTo);
    } else {
        name = buffer.substringTrimmed(indexFrom, pos);
        pos++;
    }

    if (terminated) {
        cursor.updatePos(pos);
        return createNameValuePair(name, null);
    }

    // Find value
    final String value;
    int i1 = pos;

    boolean qouted = false;
    boolean escaped = false;
    while (pos < indexTo) {
        final char ch = buffer.charAt(pos);
        if (ch == '"' && !escaped) {
            qouted = !qouted;
        }
        if (!qouted && !escaped && isOneOf(ch, delimiters)) {
            terminated = true;
            break;
        }
        if (escaped) {
            escaped = false;
        } else {
            escaped = qouted && ch == '\\';
        }
        pos++;
    }

    int i2 = pos;
    // Trim leading white spaces
    while (i1 < i2 && (HTTP.isWhitespace(buffer.charAt(i1)))) {
        i1++;
    }
    // Trim trailing white spaces
    while ((i2 > i1) && (HTTP.isWhitespace(buffer.charAt(i2 - 1)))) {
        i2--;
    }
    // Strip away quotes if necessary
    if (((i2 - i1) >= 2) && (buffer.charAt(i1) == '"') && (buffer.charAt(i2 - 1) == '"')) {
        i1++;
        i2--;
    }
    value = buffer.substring(i1, i2);
    if (terminated) {
        pos++;
    }
    cursor.updatePos(pos);
    return createNameValuePair(name, value);
}