List of usage examples for org.apache.http.util CharArrayBuffer charAt
public char charAt(int i)
From source file:org.vietspider.net.apache.DefaultResponseParser.java
public static Header[] parseHeaders(final SessionInputBuffer inbuffer, int maxHeaderCount, int maxLineLen, LineParser parser) throws HttpException, IOException { if (inbuffer == null) { throw new IllegalArgumentException("Session input buffer may not be null"); }//from w w w . j a v a 2s .c o m if (parser == null) parser = BasicLineParser.DEFAULT; ArrayList<CharArrayBuffer> headerLines = new ArrayList<CharArrayBuffer>(); CharArrayBuffer current = null; CharArrayBuffer previous = null; for (;;) { if (current == null) { current = new CharArrayBuffer(64); } else { current.clear(); } int l = inbuffer.readLine(current); if (l == -1 || current.length() < 1) { break; } // Parse the header name and value // Check for folded headers first // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2 // discussion on folded headers if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) { // we have continuation folded header // so append value int i = 0; while (i < current.length()) { char ch = current.charAt(i); if (ch != ' ' && ch != '\t') { break; } i++; } if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) { throw new IOException("Maximum line length limit exceeded"); } previous.append(' '); previous.append(current, i, current.length() - i); } else { headerLines.add(current); previous = current; current = null; } if (maxHeaderCount > 0 && headerLines.size() >= maxHeaderCount) { throw new IOException("Maximum header count exceeded"); } } Header[] headers = new Header[headerLines.size()]; for (int i = 0; i < headerLines.size(); i++) { CharArrayBuffer buffer = headerLines.get(i); try { headers[i] = parser.parseHeader(buffer); } catch (ParseException ex) { throw new ProtocolException(ex.getMessage()); } } return headers; }
From source file:com.subgraph.vega.ui.httpeditor.parser.ParserBase.java
/** * Skip SP and HT characters in a line. * /* w w w. j a va 2 s . c o 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. * //from www . java2 s . 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.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 a 2s. 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. * //from ww w .jav 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.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 www.j a v a 2s . 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.mcxiaoke.next.http.entity.BasicHeaderValueParser.java
public HeaderElement parseHeaderElement(final CharArrayBuffer buffer, final ParserCursor cursor) { AssertUtils.notNull(buffer, "Char array buffer"); AssertUtils.notNull(cursor, "Parser cursor"); final NameValuePair nvp = parseNameValuePair(buffer, cursor); NameValuePair[] params = null; if (!cursor.atEnd()) { final char ch = buffer.charAt(cursor.getPos() - 1); if (ch != ELEM_DELIMITER) { params = parseParameters(buffer, cursor); }/* w w w . j a va 2 s . c om*/ } return createHeaderElement(nvp.getName(), nvp.getValue(), params); }
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 w w w . j a v a 2 s .c o m } } return ranges; }
From source file:com.soundcloud.playerapi.OAuth2Scheme.java
@Override public void processChallenge(Header header) throws MalformedChallengeException { if (header == null) { throw new IllegalArgumentException("Header may not be null"); }/*from ww w. j av a 2 s . c o m*/ String authHeader = header.getName(); if (!authHeader.equalsIgnoreCase(AUTH.WWW_AUTH)) { throw new MalformedChallengeException("Unexpected header name: " + authHeader); } CharArrayBuffer buffer; int pos; if (header instanceof FormattedHeader) { buffer = ((FormattedHeader) header).getBuffer(); pos = ((FormattedHeader) header).getValuePos(); } else { String s = header.getValue(); if (s == null) { throw new MalformedChallengeException("Header value is null"); } buffer = new CharArrayBuffer(s.length()); buffer.append(s); pos = 0; } while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) { pos++; } int beginIndex = pos; while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) { pos++; } int endIndex = pos; String s = buffer.substring(beginIndex, endIndex); if (!s.equalsIgnoreCase(getSchemeName())) { throw new MalformedChallengeException("Invalid scheme identifier: " + s); } HeaderValueParser parser = BasicHeaderValueParser.DEFAULT; ParserCursor cursor = new ParserCursor(pos, buffer.length()); HeaderElement[] elements = parser.parseElements(buffer, cursor); if (elements.length == 0) { throw new MalformedChallengeException("Authentication challenge is empty"); } for (HeaderElement element : elements) { this.mParams.put(element.getName(), element.getValue()); } }
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 ww.j a va 2 s.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); }