Search in sources :

Example 46 with CharArrayBuffer

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);
}
Also used : BufferedHeader(org.apache.http.message.BufferedHeader) CharArrayBuffer(org.apache.http.util.CharArrayBuffer)

Example 47 with CharArrayBuffer

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();
}
Also used : CharArrayBuffer(org.apache.http.util.CharArrayBuffer)

Example 48 with CharArrayBuffer

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);
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) FormattedHeader(org.apache.http.FormattedHeader)

Example 49 with CharArrayBuffer

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;
}
Also used : ClientCookie(org.apache.http.cookie.ClientCookie) Cookie(org.apache.http.cookie.Cookie) BufferedHeader(org.apache.http.message.BufferedHeader) Header(org.apache.http.Header) FormattedHeader(org.apache.http.FormattedHeader) BufferedHeader(org.apache.http.message.BufferedHeader) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) ArrayList(java.util.ArrayList)

Example 50 with CharArrayBuffer

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;
}
Also used : ClientCookie(org.apache.http.cookie.ClientCookie) Cookie(org.apache.http.cookie.Cookie) BufferedHeader(org.apache.http.message.BufferedHeader) Header(org.apache.http.Header) BufferedHeader(org.apache.http.message.BufferedHeader) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) ArrayList(java.util.ArrayList)

Aggregations

CharArrayBuffer (org.apache.http.util.CharArrayBuffer)126 BufferedHeader (org.apache.http.message.BufferedHeader)27 FormattedHeader (org.apache.http.FormattedHeader)24 Header (org.apache.http.Header)21 ArrayList (java.util.ArrayList)19 ClientCookie (org.apache.http.cookie.ClientCookie)12 Cookie (org.apache.http.cookie.Cookie)12 ParserCursor (org.apache.http.message.ParserCursor)11 Headers (android.net.http.Headers)6 IOException (java.io.IOException)6 MalformedChallengeException (org.apache.http.auth.MalformedChallengeException)6 MalformedCookieException (org.apache.http.cookie.MalformedCookieException)6 AuthenticationException (org.apache.http.auth.AuthenticationException)5 InvalidCredentialsException (org.apache.http.auth.InvalidCredentialsException)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 NTCredentials (org.apache.http.auth.NTCredentials)4 HashMap (java.util.HashMap)3 HeaderElement (org.apache.http.HeaderElement)3 NoHttpResponseException (org.apache.http.NoHttpResponseException)3 ParseException (org.apache.http.ParseException)3