Search in sources :

Example 31 with HeaderElement

use of org.apache.http.HeaderElement in project android_frameworks_base by ParanoidAndroid.

the class Headers method parseHeader.

public void parseHeader(CharArrayBuffer buffer) {
    int pos = CharArrayBuffers.setLowercaseIndexOf(buffer, ':');
    if (pos == -1) {
        return;
    }
    String name = buffer.substringTrimmed(0, pos);
    if (name.length() == 0) {
        return;
    }
    pos++;
    String val = buffer.substringTrimmed(pos, buffer.length());
    if (HttpLog.LOGV) {
        HttpLog.v("hdr " + buffer.length() + " " + buffer);
    }
    switch(name.hashCode()) {
        case HASH_TRANSFER_ENCODING:
            if (name.equals(TRANSFER_ENCODING)) {
                mHeaders[IDX_TRANSFER_ENCODING] = val;
                HeaderElement[] encodings = BasicHeaderValueParser.DEFAULT.parseElements(buffer, new ParserCursor(pos, buffer.length()));
                // The chunked encoding must be the last one applied RFC2616,
                // 14.41
                int len = encodings.length;
                if (HTTP.IDENTITY_CODING.equalsIgnoreCase(val)) {
                    transferEncoding = ContentLengthStrategy.IDENTITY;
                } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(encodings[len - 1].getName()))) {
                    transferEncoding = ContentLengthStrategy.CHUNKED;
                } else {
                    transferEncoding = ContentLengthStrategy.IDENTITY;
                }
            }
            break;
        case HASH_CONTENT_LEN:
            if (name.equals(CONTENT_LEN)) {
                mHeaders[IDX_CONTENT_LEN] = val;
                try {
                    contentLength = Long.parseLong(val);
                } catch (NumberFormatException e) {
                    if (false) {
                        Log.v(LOGTAG, "Headers.headers(): error parsing" + " content length: " + buffer.toString());
                    }
                }
            }
            break;
        case HASH_CONTENT_TYPE:
            if (name.equals(CONTENT_TYPE)) {
                mHeaders[IDX_CONTENT_TYPE] = val;
            }
            break;
        case HASH_CONTENT_ENCODING:
            if (name.equals(CONTENT_ENCODING)) {
                mHeaders[IDX_CONTENT_ENCODING] = val;
            }
            break;
        case HASH_CONN_DIRECTIVE:
            if (name.equals(CONN_DIRECTIVE)) {
                mHeaders[IDX_CONN_DIRECTIVE] = val;
                setConnectionType(buffer, pos);
            }
            break;
        case HASH_LOCATION:
            if (name.equals(LOCATION)) {
                mHeaders[IDX_LOCATION] = val;
            }
            break;
        case HASH_PROXY_CONNECTION:
            if (name.equals(PROXY_CONNECTION)) {
                mHeaders[IDX_PROXY_CONNECTION] = val;
                setConnectionType(buffer, pos);
            }
            break;
        case HASH_WWW_AUTHENTICATE:
            if (name.equals(WWW_AUTHENTICATE)) {
                mHeaders[IDX_WWW_AUTHENTICATE] = val;
            }
            break;
        case HASH_PROXY_AUTHENTICATE:
            if (name.equals(PROXY_AUTHENTICATE)) {
                mHeaders[IDX_PROXY_AUTHENTICATE] = val;
            }
            break;
        case HASH_CONTENT_DISPOSITION:
            if (name.equals(CONTENT_DISPOSITION)) {
                mHeaders[IDX_CONTENT_DISPOSITION] = val;
            }
            break;
        case HASH_ACCEPT_RANGES:
            if (name.equals(ACCEPT_RANGES)) {
                mHeaders[IDX_ACCEPT_RANGES] = val;
            }
            break;
        case HASH_EXPIRES:
            if (name.equals(EXPIRES)) {
                mHeaders[IDX_EXPIRES] = val;
            }
            break;
        case HASH_CACHE_CONTROL:
            if (name.equals(CACHE_CONTROL)) {
                // This should be ok, according to RFC 2616 chapter 4.2
                if (mHeaders[IDX_CACHE_CONTROL] != null && mHeaders[IDX_CACHE_CONTROL].length() > 0) {
                    mHeaders[IDX_CACHE_CONTROL] += (',' + val);
                } else {
                    mHeaders[IDX_CACHE_CONTROL] = val;
                }
            }
            break;
        case HASH_LAST_MODIFIED:
            if (name.equals(LAST_MODIFIED)) {
                mHeaders[IDX_LAST_MODIFIED] = val;
            }
            break;
        case HASH_ETAG:
            if (name.equals(ETAG)) {
                mHeaders[IDX_ETAG] = val;
            }
            break;
        case HASH_SET_COOKIE:
            if (name.equals(SET_COOKIE)) {
                mHeaders[IDX_SET_COOKIE] = val;
                cookies.add(val);
            }
            break;
        case HASH_PRAGMA:
            if (name.equals(PRAGMA)) {
                mHeaders[IDX_PRAGMA] = val;
            }
            break;
        case HASH_REFRESH:
            if (name.equals(REFRESH)) {
                mHeaders[IDX_REFRESH] = val;
            }
            break;
        case HASH_X_PERMITTED_CROSS_DOMAIN_POLICIES:
            if (name.equals(X_PERMITTED_CROSS_DOMAIN_POLICIES)) {
                mHeaders[IDX_X_PERMITTED_CROSS_DOMAIN_POLICIES] = val;
            }
            break;
        default:
            mExtraHeaderNames.add(name);
            mExtraHeaderValues.add(val);
    }
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) HeaderElement(org.apache.http.HeaderElement)

Example 32 with HeaderElement

use of org.apache.http.HeaderElement in project XobotOS by xamarin.

the class HttpOptions method getAllowedMethods.

public Set<String> getAllowedMethods(final HttpResponse response) {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    HeaderIterator it = response.headerIterator("Allow");
    Set<String> methods = new HashSet<String>();
    while (it.hasNext()) {
        Header header = it.nextHeader();
        HeaderElement[] elements = header.getElements();
        for (HeaderElement element : elements) {
            methods.add(element.getName());
        }
    }
    return methods;
}
Also used : Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement) HeaderIterator(org.apache.http.HeaderIterator) HashSet(java.util.HashSet)

Example 33 with HeaderElement

use of org.apache.http.HeaderElement in project xUtils by wyouflf.

the class OtherUtils method getFileNameFromHttpResponse.

public static String getFileNameFromHttpResponse(final HttpResponse response) {
    if (response == null)
        return null;
    String result = null;
    Header header = response.getFirstHeader("Content-Disposition");
    if (header != null) {
        for (HeaderElement element : header.getElements()) {
            NameValuePair fileNamePair = element.getParameterByName("filename");
            if (fileNamePair != null) {
                result = fileNamePair.getValue();
                // try to get correct encoding str
                result = CharsetUtils.toCharset(result, HTTP.UTF_8, result.length());
                break;
            }
        }
    }
    return result;
}
Also used : NameValuePair(org.apache.http.NameValuePair) Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement)

Example 34 with HeaderElement

use of org.apache.http.HeaderElement in project XobotOS by xamarin.

the class RFC2617Scheme method parseChallenge.

@Override
protected void parseChallenge(final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException {
    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");
    }
    this.params = new HashMap<String, String>(elements.length);
    for (HeaderElement element : elements) {
        this.params.put(element.getName(), element.getValue());
    }
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) HeaderElement(org.apache.http.HeaderElement) HeaderValueParser(org.apache.http.message.HeaderValueParser) BasicHeaderValueParser(org.apache.http.message.BasicHeaderValueParser) MalformedChallengeException(org.apache.http.auth.MalformedChallengeException)

Example 35 with HeaderElement

use of org.apache.http.HeaderElement in project XobotOS by xamarin.

the class RFC2965Spec method parse.

@Override
public List<Cookie> parse(final Header header, 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");
    }
    origin = adjustEffectiveHost(origin);
    HeaderElement[] elems = header.getElements();
    List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
    for (HeaderElement headerelement : elems) {
        String name = headerelement.getName();
        String value = headerelement.getValue();
        if (name == null || name.length() == 0) {
            throw new MalformedCookieException("Cookie name may not be empty");
        }
        BasicClientCookie cookie;
        if (header.getName().equals(SM.SET_COOKIE2)) {
            cookie = createCookie2(name, value, origin);
        } else {
            cookie = createCookie(name, value, origin);
        }
        // cycle through the parameters
        NameValuePair[] attribs = headerelement.getParameters();
        // Eliminate duplicate attributes. The first occurrence takes precedence
        // See RFC2965: 3.2  Origin Server Role
        Map<String, NameValuePair> attribmap = new HashMap<String, NameValuePair>(attribs.length);
        for (int j = attribs.length - 1; j >= 0; j--) {
            NameValuePair param = attribs[j];
            attribmap.put(param.getName().toLowerCase(Locale.ENGLISH), param);
        }
        for (Map.Entry<String, NameValuePair> entry : attribmap.entrySet()) {
            NameValuePair attrib = entry.getValue();
            String s = attrib.getName().toLowerCase(Locale.ENGLISH);
            cookie.setAttribute(s, attrib.getValue());
            CookieAttributeHandler handler = findAttribHandler(s);
            if (handler != null) {
                handler.parse(cookie, attrib.getValue());
            }
        }
        cookies.add(cookie);
    }
    return cookies;
}
Also used : ClientCookie(org.apache.http.cookie.ClientCookie) Cookie(org.apache.http.cookie.Cookie) NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) HeaderElement(org.apache.http.HeaderElement) CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HeaderElement (org.apache.http.HeaderElement)58 Header (org.apache.http.Header)17 NameValuePair (org.apache.http.NameValuePair)14 HeaderElementIterator (org.apache.http.HeaderElementIterator)10 BasicHeaderElementIterator (org.apache.http.message.BasicHeaderElementIterator)10 HttpResponse (org.apache.http.HttpResponse)9 MalformedCookieException (org.apache.http.cookie.MalformedCookieException)9 ParserCursor (org.apache.http.message.ParserCursor)9 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Cookie (org.apache.http.cookie.Cookie)5 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)5 HttpContext (org.apache.http.protocol.HttpContext)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConnectionKeepAliveStrategy (org.apache.http.conn.ConnectionKeepAliveStrategy)4 HashSet (java.util.HashSet)3 NoSuchElementException (java.util.NoSuchElementException)3 ParseException (org.apache.http.ParseException)3 ProtocolException (org.apache.http.ProtocolException)3