Search in sources :

Example 36 with CharArrayBuffer

use of org.apache.http.util.CharArrayBuffer in project android_frameworks_base by DirtyUnicorns.

the class HttpHeaderTest method testCacheControl.

/**
     * Tests that cache control header supports multiple instances of the header,
     * according to HTTP specification.
     *
     * The HTTP specification states the following about the fields:
     * Multiple message-header fields with the same field-name MAY be present
     * in a message if and only if the entire field-value for that header field
     * is defined as a comma-separated list [i.e., #(values)]. It MUST be
     * possible to combine the multiple header fields into one "field-name:
     * field-value" pair, without changing the semantics of the message, by
     * appending each subsequent field-value to the first, each separated by a
     * comma. The order in which header fields with the same field-name are
     * received is therefore significant to the interpretation of the combined
     * field value, and thus a proxy MUST NOT change the order of these field
     * values when a message is forwarded.
     */
public void testCacheControl() throws Exception {
    Headers h = new Headers();
    CharArrayBuffer buffer = new CharArrayBuffer(64);
    buffer.append(CACHE_CONTROL_MAX_AGE);
    h.parseHeader(buffer);
    buffer.clear();
    buffer.append(LAST_MODIFIED);
    h.parseHeader(buffer);
    assertEquals("max-age=15", h.getCacheControl());
    buffer.clear();
    buffer.append(CACHE_CONTROL_PRIVATE);
    h.parseHeader(buffer);
    assertEquals("max-age=15,private", h.getCacheControl());
}
Also used : Headers(android.net.http.Headers) CharArrayBuffer(org.apache.http.util.CharArrayBuffer)

Example 37 with CharArrayBuffer

use of org.apache.http.util.CharArrayBuffer in project wildfly by wildfly.

the class JBossNegotiateScheme method authenticate.

/**
     * Produces Negotiate authorization Header based on token created by processChallenge.
     *
     * @param credentials Never used be the Negotiate scheme but must be provided to satisfy common-httpclient API. Credentials
     *        from JAAS will be used instead.
     * @param request The request being authenticated
     *
     * @throws AuthenticationException if authorization string cannot be generated due to an authentication failure
     *
     * @return an Negotiate authorization Header
     */
@Override
public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context) throws AuthenticationException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (state == State.TOKEN_GENERATED) {
        // hack for auto redirects
        return new BasicHeader("X-dummy", "Token already generated");
    }
    if (state != State.CHALLENGE_RECEIVED) {
        throw new IllegalStateException("Negotiation authentication process has not been initiated");
    }
    try {
        String key = null;
        if (isProxy()) {
            key = ExecutionContext.HTTP_PROXY_HOST;
        } else {
            key = HttpCoreContext.HTTP_TARGET_HOST;
        }
        HttpHost host = (HttpHost) context.getAttribute(key);
        if (host == null) {
            throw new AuthenticationException("Authentication host is not set " + "in the execution context");
        }
        String authServer;
        if (!this.stripPort && host.getPort() > 0) {
            authServer = host.toHostString();
        } else {
            authServer = host.getHostName();
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("init " + authServer);
        }
        final Oid negotiationOid = new Oid(SPNEGO_OID);
        final GSSManager manager = GSSManager.getInstance();
        final GSSName serverName = manager.createName("HTTP@" + authServer, GSSName.NT_HOSTBASED_SERVICE);
        final GSSContext gssContext = manager.createContext(serverName.canonicalize(negotiationOid), negotiationOid, null, DEFAULT_LIFETIME);
        gssContext.requestMutualAuth(true);
        gssContext.requestCredDeleg(true);
        if (token == null) {
            token = new byte[0];
        }
        token = gssContext.initSecContext(token, 0, token.length);
        if (token == null) {
            state = State.FAILED;
            throw new AuthenticationException("GSS security context initialization failed");
        }
        state = State.TOKEN_GENERATED;
        String tokenstr = new String(base64codec.encode(token));
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Sending response '" + tokenstr + "' back to the auth server");
        }
        CharArrayBuffer buffer = new CharArrayBuffer(32);
        if (isProxy()) {
            buffer.append(AUTH.PROXY_AUTH_RESP);
        } else {
            buffer.append(AUTH.WWW_AUTH_RESP);
        }
        buffer.append(": Negotiate ");
        buffer.append(tokenstr);
        return new BufferedHeader(buffer);
    } catch (GSSException gsse) {
        state = State.FAILED;
        if (gsse.getMajor() == GSSException.DEFECTIVE_CREDENTIAL || gsse.getMajor() == GSSException.CREDENTIALS_EXPIRED)
            throw new InvalidCredentialsException(gsse.getMessage(), gsse);
        if (gsse.getMajor() == GSSException.NO_CRED)
            throw new InvalidCredentialsException(gsse.getMessage(), gsse);
        if (gsse.getMajor() == GSSException.DEFECTIVE_TOKEN || gsse.getMajor() == GSSException.DUPLICATE_TOKEN || gsse.getMajor() == GSSException.OLD_TOKEN)
            throw new AuthenticationException(gsse.getMessage(), gsse);
        // other error
        throw new AuthenticationException(gsse.getMessage());
    }
}
Also used : GSSName(org.ietf.jgss.GSSName) AuthenticationException(org.apache.http.auth.AuthenticationException) BufferedHeader(org.apache.http.message.BufferedHeader) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) Oid(org.ietf.jgss.Oid) GSSException(org.ietf.jgss.GSSException) InvalidCredentialsException(org.apache.http.auth.InvalidCredentialsException) HttpHost(org.apache.http.HttpHost) GSSManager(org.ietf.jgss.GSSManager) GSSContext(org.ietf.jgss.GSSContext) BasicHeader(org.apache.http.message.BasicHeader)

Example 38 with CharArrayBuffer

use of org.apache.http.util.CharArrayBuffer in project platform_external_apache-http by android.

the class BasicLineFormatter method formatHeader.

// non-javadoc, see interface LineFormatter
public CharArrayBuffer formatHeader(CharArrayBuffer buffer, Header header) {
    if (header == null) {
        throw new IllegalArgumentException("Header may not be null");
    }
    CharArrayBuffer result = null;
    if (header instanceof FormattedHeader) {
        // If the header is backed by a buffer, re-use the buffer
        result = ((FormattedHeader) header).getBuffer();
    } else {
        result = initBuffer(buffer);
        doFormatHeader(result, header);
    }
    return result;
}
Also used : CharArrayBuffer(org.apache.http.util.CharArrayBuffer) FormattedHeader(org.apache.http.FormattedHeader)

Example 39 with CharArrayBuffer

use of org.apache.http.util.CharArrayBuffer in project platform_external_apache-http by android.

the class BasicLineParser method parseProtocolVersion.

public static final ProtocolVersion parseProtocolVersion(String value, LineParser parser) throws ParseException {
    if (value == null) {
        throw new IllegalArgumentException("Value to parse may not be null.");
    }
    if (parser == null)
        parser = BasicLineParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    ParserCursor cursor = new ParserCursor(0, value.length());
    return parser.parseProtocolVersion(buffer, cursor);
}
Also used : CharArrayBuffer(org.apache.http.util.CharArrayBuffer)

Example 40 with CharArrayBuffer

use of org.apache.http.util.CharArrayBuffer in project platform_external_apache-http by android.

the class BasicLineParser method parseHeader.

public static final Header parseHeader(final String value, LineParser parser) throws ParseException {
    if (value == null) {
        throw new IllegalArgumentException("Value to parse may not be null");
    }
    if (parser == null)
        parser = BasicLineParser.DEFAULT;
    CharArrayBuffer buffer = new CharArrayBuffer(value.length());
    buffer.append(value);
    return parser.parseHeader(buffer);
}
Also used : CharArrayBuffer(org.apache.http.util.CharArrayBuffer)

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