use of org.apache.http.util.CharArrayBuffer in project platform_external_apache-http by android.
the class BasicScheme method authenticate.
/**
* Returns a basic <tt>Authorization</tt> header value for the given
* {@link Credentials} and charset.
*
* @param credentials The credentials to encode.
* @param charset The charset to use for encoding the credentials
*
* @return a basic authorization header
*/
public static Header authenticate(final Credentials credentials, final String charset, boolean proxy) {
if (credentials == null) {
throw new IllegalArgumentException("Credentials may not be null");
}
if (charset == null) {
throw new IllegalArgumentException("charset may not be null");
}
StringBuilder tmp = new StringBuilder();
tmp.append(credentials.getUserPrincipal().getName());
tmp.append(":");
tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
byte[] base64password = Base64.encodeBase64(EncodingUtils.getBytes(tmp.toString(), charset));
CharArrayBuffer buffer = new CharArrayBuffer(32);
if (proxy) {
buffer.append(AUTH.PROXY_AUTH_RESP);
} else {
buffer.append(AUTH.WWW_AUTH_RESP);
}
buffer.append(": Basic ");
buffer.append(base64password, 0, base64password.length);
return new BufferedHeader(buffer);
}
use of org.apache.http.util.CharArrayBuffer in project platform_external_apache-http by android.
the class HttpHost method toHostString.
/**
* Obtains the host string, without scheme prefix.
*
* @return the host string, for example <code>localhost:8080</code>
*/
public String toHostString() {
CharArrayBuffer buffer = new CharArrayBuffer(32);
buffer.append(this.hostname);
if (this.port != -1) {
buffer.append(':');
buffer.append(Integer.toString(this.port));
}
return buffer.toString();
}
use of org.apache.http.util.CharArrayBuffer in project platform_external_apache-http by android.
the class NetscapeDraftSpec method parse.
/**
* Parses the Set-Cookie value into an array of <tt>Cookie</tt>s.
*
* <p>Syntax of the Set-Cookie HTTP Response Header:</p>
*
* <p>This is the format a CGI script would use to add to
* the HTTP headers a new piece of data which is to be stored by
* the client for later retrieval.</p>
*
* <PRE>
* Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
* </PRE>
*
* <p>Please note that Netscape draft specification does not fully
* conform to the HTTP header format. Netscape draft does not specify
* whether multiple cookies may be sent in one header. Hence, comma
* character may be present in unquoted cookie value or unquoted
* parameter value.</p>
*
* @see <a href="http://wp.netscape.com/newsref/std/cookie_spec.html">
* The Cookie Spec.</a>
*
* @param header the <tt>Set-Cookie</tt> received from the server
* @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value
* @throws MalformedCookieException if an exception occurs during parsing
*/
public List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException {
if (header == null) {
throw new IllegalArgumentException("Header may not be null");
}
if (origin == null) {
throw new IllegalArgumentException("Cookie origin may not be null");
}
NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
CharArrayBuffer buffer;
ParserCursor cursor;
if (header instanceof FormattedHeader) {
buffer = ((FormattedHeader) header).getBuffer();
cursor = new ParserCursor(((FormattedHeader) header).getValuePos(), buffer.length());
} else {
String s = header.getValue();
if (s == null) {
throw new MalformedCookieException("Header value is null");
}
buffer = new CharArrayBuffer(s.length());
buffer.append(s);
cursor = new ParserCursor(0, buffer.length());
}
return parse(new HeaderElement[] { parser.parseHeader(buffer, cursor) }, origin);
}
use of org.apache.http.util.CharArrayBuffer in project platform_external_apache-http by android.
the class NetscapeDraftSpec method formatCookies.
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;
}
use of org.apache.http.util.CharArrayBuffer in project platform_external_apache-http by android.
the class RFC2109Spec method doFormatOneHeader.
private List<Header> doFormatOneHeader(final List<Cookie> cookies) {
int version = Integer.MAX_VALUE;
// Pick the lowest common denominator
for (Cookie cookie : cookies) {
if (cookie.getVersion() < version) {
version = cookie.getVersion();
}
}
CharArrayBuffer buffer = new CharArrayBuffer(40 * cookies.size());
buffer.append(SM.COOKIE);
buffer.append(": ");
buffer.append("$Version=");
buffer.append(Integer.toString(version));
for (Cookie cooky : cookies) {
buffer.append("; ");
Cookie cookie = cooky;
formatCookieAsVer(buffer, cookie, version);
}
List<Header> headers = new ArrayList<Header>(1);
headers.add(new BufferedHeader(buffer));
return headers;
}
Aggregations