Search in sources :

Example 51 with HeaderElement

use of org.apache.http.HeaderElement in project android-delicious by lexs.

the class Response method getCharset.

public String getCharset() {
    String contentType = connection.getContentType();
    if (contentType != null) {
        HeaderValueParser parser = new BasicHeaderValueParser();
        HeaderElement[] values = BasicHeaderValueParser.parseElements(contentType, parser);
        if (values.length > 0) {
            NameValuePair param = values[0].getParameterByName("charset");
            if (param != null) {
                return param.getValue();
            }
        }
    }
    // No encoding specified
    return HTTP.DEFAULT_CONTENT_CHARSET;
}
Also used : NameValuePair(org.apache.http.NameValuePair) HeaderElement(org.apache.http.HeaderElement) HeaderValueParser(org.apache.http.message.HeaderValueParser) BasicHeaderValueParser(org.apache.http.message.BasicHeaderValueParser) BasicHeaderValueParser(org.apache.http.message.BasicHeaderValueParser)

Example 52 with HeaderElement

use of org.apache.http.HeaderElement in project newsrob by marianokamp.

the class Asset method loadTextFromUrl.

static Map<String, String> loadTextFromUrl(NewsRobHttpClient httpClient, URL pageUrl, long started, Job job, Context context) throws DownloadException, DownloadCancelledException, URISyntaxException, SocketException, SocketTimeoutException, DownloadTimedOutException {
    Map<String, String> returnValues = new HashMap<String, String>(2);
    CharSequence result = null;
    HttpResponse response;
    HttpContext localContext = new BasicHttpContext();
    try {
        HttpGet loadRequest = new HttpGet(pageUrl.toURI());
        response = httpClient.executeZipped(loadRequest, localContext);
    } catch (IOException e) {
        throw new DownloadException("Problem during download of " + pageUrl + ".", e);
    }
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != HttpStatus.SC_OK)
        throw new WrongStatusException(pageUrl, statusCode);
    String newUri = extractUriFromHttpContext(localContext);
    if (!pageUrl.toString().equals(newUri)) {
        PL.log("WPDD Downloader: Changed page's url after redirect from " + pageUrl + " to " + newUri + ".", context);
        try {
            pageUrl = new URL(newUri);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        // keep the existing pageUrl
        }
    }
    try {
        String charsetName = null;
        for (HeaderElement he : response.getEntity().getContentType().getElements()) {
            NameValuePair nvp = he.getParameterByName("charset");
            if (nvp != null) {
                charsetName = nvp.getValue();
                break;
            }
        }
        result = U2.readInputStreamIntoString(NewsRobHttpClient.getUngzippedContent(response.getEntity(), context), charsetName, started, job);
        response.getEntity().consumeContent();
    } catch (IOException e) {
        throw new DownloadException("Problem during reading of InputStream when loading " + pageUrl + ".", e);
    }
    returnValues.put("url", pageUrl.toExternalForm());
    returnValues.put("content", result.toString());
    return returnValues;
}
Also used : NameValuePair(org.apache.http.NameValuePair) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HeaderElement(org.apache.http.HeaderElement) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URL(java.net.URL)

Example 53 with HeaderElement

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

the class LaxContentLengthStrategy method determineLength.

public long determineLength(final HttpMessage message) throws HttpException {
    if (message == null) {
        throw new IllegalArgumentException("HTTP message may not be null");
    }
    HttpParams params = message.getParams();
    boolean strict = params.isParameterTrue(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
    Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
    Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
    // RFC2616, 4.4 item number 3
    if (transferEncodingHeader != null) {
        HeaderElement[] encodings = null;
        try {
            encodings = transferEncodingHeader.getElements();
        } catch (ParseException px) {
            throw new ProtocolException("Invalid Transfer-Encoding header value: " + transferEncodingHeader, px);
        }
        if (strict) {
            // Currently only chunk and identity are supported
            for (int i = 0; i < encodings.length; i++) {
                String encoding = encodings[i].getName();
                if (encoding != null && encoding.length() > 0 && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING) && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
                    throw new ProtocolException("Unsupported transfer encoding: " + encoding);
                }
            }
        }
        // The chunked encoding must be the last one applied RFC2616, 14.41
        int len = encodings.length;
        if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
            return IDENTITY;
        } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(encodings[len - 1].getName()))) {
            return CHUNKED;
        } else {
            if (strict) {
                throw new ProtocolException("Chunk-encoding must be the last one applied");
            }
            return IDENTITY;
        }
    } else if (contentLengthHeader != null) {
        long contentlen = -1;
        Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
        if (strict && headers.length > 1) {
            throw new ProtocolException("Multiple content length headers");
        }
        for (int i = headers.length - 1; i >= 0; i--) {
            Header header = headers[i];
            try {
                contentlen = Long.parseLong(header.getValue());
                break;
            } catch (NumberFormatException e) {
                if (strict) {
                    throw new ProtocolException("Invalid content length: " + header.getValue());
                }
            }
        // See if we can have better luck with another header, if present
        }
        if (contentlen >= 0) {
            return contentlen;
        } else {
            return IDENTITY;
        }
    } else {
        return IDENTITY;
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement) ParseException(org.apache.http.ParseException)

Example 54 with HeaderElement

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

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)

Example 55 with HeaderElement

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

the class BestMatchSpec method parse.

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");
    }
    HeaderElement[] helems = header.getElements();
    boolean versioned = false;
    boolean netscape = false;
    for (HeaderElement helem : helems) {
        if (helem.getParameterByName("version") != null) {
            versioned = true;
        }
        if (helem.getParameterByName("expires") != null) {
            netscape = true;
        }
    }
    if (netscape) {
    }
    // Do we have a cookie with a version attribute?
    if (versioned) {
        return getStrict().parse(helems, origin);
    } else if (netscape) {
        // comma separators
        return getNetscape().parse(header, origin);
    } else {
        return getCompat().parse(helems, origin);
    }
}
Also used : HeaderElement(org.apache.http.HeaderElement)

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