Search in sources :

Example 11 with HeaderIterator

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

the class DefaultConnectionReuseStrategy method keepAlive.

// see interface ConnectionReuseStrategy
public boolean keepAlive(final HttpResponse response, final HttpContext context) {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null.");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null.");
    }
    HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
    if (conn != null && !conn.isOpen())
        return false;
    // do NOT check for stale connection, that is an expensive operation
    // Check for a self-terminating entity. If the end of the entity will
    // be indicated by closing the connection, there is no keep-alive.
    HttpEntity entity = response.getEntity();
    ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
    if (entity != null) {
        if (entity.getContentLength() < 0) {
            if (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0)) {
                // encoded, the connection cannot be reused
                return false;
            }
        }
    }
    // Check for the "Connection" header. If that is absent, check for
    // the "Proxy-Connection" header. The latter is an unspecified and
    // broken but unfortunately common extension of HTTP.
    HeaderIterator hit = response.headerIterator(HTTP.CONN_DIRECTIVE);
    if (!hit.hasNext())
        hit = response.headerIterator("Proxy-Connection");
    if (hit.hasNext()) {
        try {
            TokenIterator ti = createTokenIterator(hit);
            boolean keepalive = false;
            while (ti.hasNext()) {
                final String token = ti.nextToken();
                if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) {
                    return false;
                } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(token)) {
                    // continue the loop, there may be a "close" afterwards
                    keepalive = true;
                }
            }
            if (keepalive)
                return true;
        // neither "close" nor "keep-alive", use default policy
        } catch (ParseException px) {
            // we don't have logging in HttpCore, so the exception is lost
            return false;
        }
    }
    // default since HTTP/1.1 is persistent, before it was non-persistent
    return !ver.lessEquals(HttpVersion.HTTP_1_0);
}
Also used : HttpEntity(org.apache.http.HttpEntity) TokenIterator(org.apache.http.TokenIterator) BasicTokenIterator(org.apache.http.message.BasicTokenIterator) HttpConnection(org.apache.http.HttpConnection) HeaderIterator(org.apache.http.HeaderIterator) ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 12 with HeaderIterator

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

the class ResponseProcessCookies method process.

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    // Obtain cookie store
    CookieStore cookieStore = (CookieStore) context.getAttribute(ClientContext.COOKIE_STORE);
    if (cookieStore == null) {
        this.log.info("Cookie store not available in HTTP context");
        return;
    }
    // Obtain actual CookieSpec instance
    CookieSpec cookieSpec = (CookieSpec) context.getAttribute(ClientContext.COOKIE_SPEC);
    if (cookieSpec == null) {
        this.log.info("CookieSpec not available in HTTP context");
        return;
    }
    // Obtain actual CookieOrigin instance
    CookieOrigin cookieOrigin = (CookieOrigin) context.getAttribute(ClientContext.COOKIE_ORIGIN);
    if (cookieOrigin == null) {
        this.log.info("CookieOrigin not available in HTTP context");
        return;
    }
    HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
    processCookies(it, cookieSpec, cookieOrigin, cookieStore);
    // see if the cookie spec supports cookie versioning.
    if (cookieSpec.getVersion() > 0) {
        // process set-cookie2 headers.
        // Cookie2 will replace equivalent Cookie instances
        it = response.headerIterator(SM.SET_COOKIE2);
        processCookies(it, cookieSpec, cookieOrigin, cookieStore);
    }
}
Also used : CookieStore(org.apache.http.client.CookieStore) CookieSpec(org.apache.http.cookie.CookieSpec) HeaderIterator(org.apache.http.HeaderIterator) CookieOrigin(org.apache.http.cookie.CookieOrigin)

Example 13 with HeaderIterator

use of org.apache.http.HeaderIterator in project lucene-solr by apache.

the class HttpSolrCall method remoteQuery.

private void remoteQuery(String coreUrl, HttpServletResponse resp) throws IOException {
    HttpRequestBase method = null;
    HttpEntity httpEntity = null;
    try {
        String urlstr = coreUrl + queryParams.toQueryString();
        boolean isPostOrPutRequest = "POST".equals(req.getMethod()) || "PUT".equals(req.getMethod());
        if ("GET".equals(req.getMethod())) {
            method = new HttpGet(urlstr);
        } else if ("HEAD".equals(req.getMethod())) {
            method = new HttpHead(urlstr);
        } else if (isPostOrPutRequest) {
            HttpEntityEnclosingRequestBase entityRequest = "POST".equals(req.getMethod()) ? new HttpPost(urlstr) : new HttpPut(urlstr);
            // Prevent close of container streams
            InputStream in = new CloseShieldInputStream(req.getInputStream());
            HttpEntity entity = new InputStreamEntity(in, req.getContentLength());
            entityRequest.setEntity(entity);
            method = entityRequest;
        } else if ("DELETE".equals(req.getMethod())) {
            method = new HttpDelete(urlstr);
        } else {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unexpected method type: " + req.getMethod());
        }
        for (Enumeration<String> e = req.getHeaderNames(); e.hasMoreElements(); ) {
            String headerName = e.nextElement();
            if (!"host".equalsIgnoreCase(headerName) && !"authorization".equalsIgnoreCase(headerName) && !"accept".equalsIgnoreCase(headerName)) {
                method.addHeader(headerName, req.getHeader(headerName));
            }
        }
        // These headers not supported for HttpEntityEnclosingRequests
        if (method instanceof HttpEntityEnclosingRequest) {
            method.removeHeaders(TRANSFER_ENCODING_HEADER);
            method.removeHeaders(CONTENT_LENGTH_HEADER);
        }
        final HttpResponse response = solrDispatchFilter.httpClient.execute(method, HttpClientUtil.createNewHttpClientRequestContext());
        int httpStatus = response.getStatusLine().getStatusCode();
        httpEntity = response.getEntity();
        resp.setStatus(httpStatus);
        for (HeaderIterator responseHeaders = response.headerIterator(); responseHeaders.hasNext(); ) {
            Header header = responseHeaders.nextHeader();
            // encoding issues with Tomcat
            if (header != null && !header.getName().equalsIgnoreCase(TRANSFER_ENCODING_HEADER) && !header.getName().equalsIgnoreCase(CONNECTION_HEADER)) {
                resp.addHeader(header.getName(), header.getValue());
            }
        }
        if (httpEntity != null) {
            if (httpEntity.getContentEncoding() != null)
                resp.setCharacterEncoding(httpEntity.getContentEncoding().getValue());
            if (httpEntity.getContentType() != null)
                resp.setContentType(httpEntity.getContentType().getValue());
            InputStream is = httpEntity.getContent();
            OutputStream os = resp.getOutputStream();
            IOUtils.copyLarge(is, os);
        }
    } catch (IOException e) {
        sendError(new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error trying to proxy request for url: " + coreUrl, e));
    } finally {
        Utils.consumeFully(httpEntity);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHead(org.apache.http.client.methods.HttpHead) HttpPut(org.apache.http.client.methods.HttpPut) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HeaderIterator(org.apache.http.HeaderIterator) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) SolrException(org.apache.solr.common.SolrException)

Aggregations

HeaderIterator (org.apache.http.HeaderIterator)13 Header (org.apache.http.Header)4 HttpEntity (org.apache.http.HttpEntity)4 HttpResponse (org.apache.http.HttpResponse)4 HashSet (java.util.HashSet)3 HeaderElement (org.apache.http.HeaderElement)3 HttpConnection (org.apache.http.HttpConnection)3 ParseException (org.apache.http.ParseException)3 ProtocolVersion (org.apache.http.ProtocolVersion)3 TokenIterator (org.apache.http.TokenIterator)3 CookieStore (org.apache.http.client.CookieStore)3 CookieOrigin (org.apache.http.cookie.CookieOrigin)3 CookieSpec (org.apache.http.cookie.CookieSpec)3 BasicHeader (org.apache.http.message.BasicHeader)3 BasicTokenIterator (org.apache.http.message.BasicTokenIterator)3 Test (org.junit.Test)3 TestHttpResponse (org.robolectric.shadows.httpclient.TestHttpResponse)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1