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

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

Introduction

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

Prototype

public int getPos() 

Source Link

Usage

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

/**
 * Get the next word from a buffer containing a line.
 * /*w  ww. j  a  v  a2  s  . com*/
 * @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

/**
 * Skip SP and HT characters in a line.   
 * /*from ww w .  ja v  a 2 s . co  m*/
 * @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 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.
 * //w ww .  ja  v  a 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 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.subgraph.vega.ui.httpeditor.parser.ParserBase.java

/**
 * Parse HTTP headers and add them to a IHttpMessageBuilder until a non-header line is encountered.
 * //from  w w w  .  j  a v a  2s .c  o m
 * @param parser HC line parser.
 * @param builder IHttpMessageBuilder to add parsed headers to.
 * @param buf Buffer containing header data.
 * @param bufCursor Parser cursor for buf. Adjusted to one character past the end of headers and optional CRLF line.
 */
protected void parseHeaders(final LineParser parser, final IHttpMessageBuilder builder,
        final CharArrayBuffer buf, final ParserCursor bufCursor) {
    final CharArrayBuffer lnBuf = new CharArrayBuffer(0);
    while (true) {
        lnBuf.clear();
        int idxPos = bufCursor.getPos();
        if (readLineHeader(buf, bufCursor, lnBuf) > 0) {
            try {
                // REVISIT don't want an extra step
                Header header = parser.parseHeader(lnBuf);
                builder.addHeader(header.getName(), header.getValue());
            } catch (ParseException e) {
                // for now we'll move the cursor back so the line gets treated as the start of the body
                bufCursor.updatePos(idxPos);
                return;
            }
        } else {
            break;
        }
    }
}

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.
 *    //from www.j  a v a  2s  .  c  o  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.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;
        }/*from   ww  w .  j  av  a2  s .com*/
        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.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 w  w .j a v  a2 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.//w  ww  . jav  a  2s.  c o  m
 * 
 * @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   w  ww  .ja v  a2  s.  c o m*/
 * @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);//w  ww.  ja v a  2s  . c  o  m
        }
    }
    return ranges;
}