List of usage examples for org.apache.http.protocol HTTP isWhitespace
public static boolean isWhitespace(char c)
From source file:android.net.http.CharArrayBuffers.java
/** * Returns true if the buffer contains the given string. Ignores leading * whitespace and case.// w w w. jav a 2 s .c om * * @param buffer to search * @param beginIndex index at which we should start * @param str to search for */ static boolean containsIgnoreCaseTrimmed(CharArrayBuffer buffer, int beginIndex, final String str) { int len = buffer.length(); char[] chars = buffer.buffer(); while (beginIndex < len && HTTP.isWhitespace(chars[beginIndex])) { beginIndex++; } int size = str.length(); boolean ok = len >= beginIndex + size; for (int j = 0; ok && (j < size); j++) { char a = chars[beginIndex + j]; char b = str.charAt(j); if (a != b) { a = toLower(a); b = toLower(b); ok = a == b; } } return ok; }
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 w ww .j a v a 2s. c om*/ 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[] 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++;//w w w . j a va 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.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// ww w . ja v a 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); }
From source file:org.apache.http.impl.client.AbstractAuthenticationHandler.java
protected Map<String, Header> parseChallenges(final Header[] headers) throws MalformedChallengeException { final Map<String, Header> map = new HashMap<String, Header>(headers.length); for (final Header header : headers) { final CharArrayBuffer buffer; int pos;/*w ww.java2 s . co m*/ if (header instanceof FormattedHeader) { buffer = ((FormattedHeader) header).getBuffer(); pos = ((FormattedHeader) header).getValuePos(); } else { final 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++; } final int beginIndex = pos; while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) { pos++; } final int endIndex = pos; final String s = buffer.substring(beginIndex, endIndex); map.put(s.toLowerCase(Locale.US), header); } return map; }
From source file:org.apache.http.impl.client.AuthenticationStrategyImpl.java
public Map<String, Header> getChallenges(final HttpHost authhost, final HttpResponse response, final HttpContext context) throws MalformedChallengeException { Args.notNull(response, "HTTP response"); final Header[] headers = response.getHeaders(this.headerName); final Map<String, Header> map = new HashMap<String, Header>(headers.length); for (final Header header : headers) { final CharArrayBuffer buffer; int pos;//www. j a v a2s. c o m if (header instanceof FormattedHeader) { buffer = ((FormattedHeader) header).getBuffer(); pos = ((FormattedHeader) header).getValuePos(); } else { final 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++; } final int beginIndex = pos; while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) { pos++; } final int endIndex = pos; final String s = buffer.substring(beginIndex, endIndex); map.put(s.toLowerCase(Locale.US), header); } return map; }