Search in sources :

Example 11 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class Request method readResponse.

/**
     * Receive a single http response.
     *
     * @param httpClientConnection the request to receive the response for.
     */
void readResponse(AndroidHttpClientConnection httpClientConnection) throws IOException, ParseException {
    // don't send cancelled requests
    if (mCancelled)
        return;
    StatusLine statusLine = null;
    boolean hasBody = false;
    httpClientConnection.flush();
    int statusCode = 0;
    Headers header = new Headers();
    do {
        statusLine = httpClientConnection.parseResponseHeader(header);
        statusCode = statusLine.getStatusCode();
    } while (statusCode < HttpStatus.SC_OK);
    if (HttpLog.LOGV)
        HttpLog.v("Request.readResponseStatus() " + statusLine.toString().length() + " " + statusLine);
    ProtocolVersion v = statusLine.getProtocolVersion();
    mEventHandler.status(v.getMajor(), v.getMinor(), statusCode, statusLine.getReasonPhrase());
    mEventHandler.headers(header);
    HttpEntity entity = null;
    hasBody = canResponseHaveBody(mHttpRequest, statusCode);
    if (hasBody)
        entity = httpClientConnection.receiveResponseEntity(header);
    // restrict the range request to the servers claiming that they are
    // accepting ranges in bytes
    boolean supportPartialContent = "bytes".equalsIgnoreCase(header.getAcceptRanges());
    if (entity != null) {
        InputStream is = entity.getContent();
        // process gzip content encoding
        Header contentEncoding = entity.getContentEncoding();
        InputStream nis = null;
        byte[] buf = null;
        int count = 0;
        try {
            if (contentEncoding != null && contentEncoding.getValue().equals("gzip")) {
                nis = new GZIPInputStream(is);
            } else {
                nis = is;
            }
            /* accumulate enough data to make it worth pushing it
                 * up the stack */
            buf = mConnection.getBuf();
            int len = 0;
            int lowWater = buf.length / 2;
            while (len != -1) {
                synchronized (this) {
                    while (mLoadingPaused) {
                        // filled its internal buffers.
                        try {
                            wait();
                        } catch (InterruptedException e) {
                            HttpLog.e("Interrupted exception whilst " + "network thread paused at WebCore's request." + " " + e.getMessage());
                        }
                    }
                }
                len = nis.read(buf, count, buf.length - count);
                if (len != -1) {
                    count += len;
                    if (supportPartialContent)
                        mReceivedBytes += len;
                }
                if (len == -1 || count >= lowWater) {
                    if (HttpLog.LOGV)
                        HttpLog.v("Request.readResponse() " + count);
                    mEventHandler.data(buf, count);
                    count = 0;
                }
            }
        } catch (EOFException e) {
            /* InflaterInputStream throws an EOFException when the
                   server truncates gzipped content.  Handle this case
                   as we do truncated non-gzipped content: no error */
            if (count > 0) {
                // if there is uncommited content, we should commit them
                mEventHandler.data(buf, count);
            }
            if (HttpLog.LOGV)
                HttpLog.v("readResponse() handling " + e);
        } catch (IOException e) {
            // don't throw if we have a non-OK status code
            if (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_PARTIAL_CONTENT) {
                if (supportPartialContent && count > 0) {
                    // if there is uncommited content, we should commit them
                    // as we will continue the request
                    mEventHandler.data(buf, count);
                }
                throw e;
            }
        } finally {
            if (nis != null) {
                nis.close();
            }
        }
    }
    mConnection.setCanPersist(entity, statusLine.getProtocolVersion(), header.getConnectionType());
    mEventHandler.endData();
    complete();
    if (HttpLog.LOGV)
        HttpLog.v("Request.readResponse(): done " + mHost.getSchemeName() + "://" + getHostPort() + mPath);
}
Also used : HttpEntity(org.apache.http.HttpEntity) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) StatusLine(org.apache.http.StatusLine) GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.apache.http.Header) EOFException(java.io.EOFException)

Example 12 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class HttpRequestExecutor method doSendRequest.

/**
     * Send a request over a connection.
     * This method also handles the expect-continue handshake if necessary.
     * If it does not have to handle an expect-continue handshake, it will
     * not use the connection for reading or anything else that depends on
     * data coming in over the connection.
     *
     * @param request   the request to send, already
     *                  {@link #preProcess preprocessed}
     * @param conn      the connection over which to send the request,
     *                  already established
     * @param context   the context for sending the request
     *
     * @return  a terminal response received as part of an expect-continue
     *          handshake, or
     *          <code>null</code> if the expect-continue handshake is not used
     *
     * @throws HttpException      in case of a protocol or processing problem
     * @throws IOException        in case of an I/O problem
     */
protected HttpResponse doSendRequest(final HttpRequest request, final HttpClientConnection conn, final HttpContext context) throws IOException, HttpException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (conn == null) {
        throw new IllegalArgumentException("HTTP connection may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    HttpResponse response = null;
    context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.FALSE);
    conn.sendRequestHeader(request);
    if (request instanceof HttpEntityEnclosingRequest) {
        // Check for expect-continue handshake. We have to flush the
        // headers and wait for an 100-continue response to handle it.
        // If we get a different response, we must not send the entity.
        boolean sendentity = true;
        final ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
        if (((HttpEntityEnclosingRequest) request).expectContinue() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
            conn.flush();
            // As suggested by RFC 2616 section 8.2.3, we don't wait for a
            // 100-continue response forever. On timeout, send the entity.
            int tms = request.getParams().getIntParameter(CoreProtocolPNames.WAIT_FOR_CONTINUE, 2000);
            if (conn.isResponseAvailable(tms)) {
                response = conn.receiveResponseHeader();
                if (canResponseHaveBody(request, response)) {
                    conn.receiveResponseEntity(response);
                }
                int status = response.getStatusLine().getStatusCode();
                if (status < 200) {
                    if (status != HttpStatus.SC_CONTINUE) {
                        throw new ProtocolException("Unexpected response: " + response.getStatusLine());
                    }
                    // discard 100-continue
                    response = null;
                } else {
                    sendentity = false;
                }
            }
        }
        if (sendentity) {
            conn.sendRequestEntity((HttpEntityEnclosingRequest) request);
        }
    }
    conn.flush();
    context.setAttribute(ExecutionContext.HTTP_REQ_SENT, Boolean.TRUE);
    return response;
}
Also used : ProtocolException(java.net.ProtocolException) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 13 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class DefaultRequestDirector method createConnectRequest.

/**
     * Creates the CONNECT request for tunnelling.
     * Called by {@link #createTunnelToTarget createTunnelToTarget}.
     *
     * @param route     the route to establish
     * @param context   the context for request execution
     *
     * @return  the CONNECT request for tunnelling
     */
protected HttpRequest createConnectRequest(HttpRoute route, HttpContext context) {
    // see RFC 2817, section 5.2 and 
    // INTERNET-DRAFT: Tunneling TCP based protocols through 
    // Web proxy servers
    HttpHost target = route.getTargetHost();
    String host = target.getHostName();
    int port = target.getPort();
    if (port < 0) {
        Scheme scheme = connManager.getSchemeRegistry().getScheme(target.getSchemeName());
        port = scheme.getDefaultPort();
    }
    StringBuilder buffer = new StringBuilder(host.length() + 6);
    buffer.append(host);
    buffer.append(':');
    buffer.append(Integer.toString(port));
    String authority = buffer.toString();
    ProtocolVersion ver = HttpProtocolParams.getVersion(params);
    HttpRequest req = new BasicHttpRequest("CONNECT", authority, ver);
    return req;
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) AbortableHttpRequest(org.apache.http.client.methods.AbortableHttpRequest) Scheme(org.apache.http.conn.scheme.Scheme) AuthScheme(org.apache.http.auth.AuthScheme) HttpHost(org.apache.http.HttpHost) ProtocolVersion(org.apache.http.ProtocolVersion) BasicHttpRequest(org.apache.http.message.BasicHttpRequest)

Example 14 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class RequestWrapper method getRequestLine.

public RequestLine getRequestLine() {
    String method = getMethod();
    ProtocolVersion ver = getProtocolVersion();
    String uritext = null;
    if (uri != null) {
        uritext = uri.toASCIIString();
    }
    if (uritext == null || uritext.length() == 0) {
        uritext = "/";
    }
    return new BasicRequestLine(method, uritext, ver);
}
Also used : BasicRequestLine(org.apache.http.message.BasicRequestLine) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 15 with ProtocolVersion

use of org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion in project XobotOS by xamarin.

the class RequestContent method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (request instanceof HttpEntityEnclosingRequest) {
        if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
            throw new ProtocolException("Transfer-encoding header already present");
        }
        if (request.containsHeader(HTTP.CONTENT_LEN)) {
            throw new ProtocolException("Content-Length header already present");
        }
        ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        if (entity == null) {
            request.addHeader(HTTP.CONTENT_LEN, "0");
            return;
        }
        // Must specify a transfer encoding or a content length 
        if (entity.isChunked() || entity.getContentLength() < 0) {
            if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                throw new ProtocolException("Chunked transfer encoding not allowed for " + ver);
            }
            request.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
        } else {
            request.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
        }
        // Specify a content type if known
        if (entity.getContentType() != null && !request.containsHeader(HTTP.CONTENT_TYPE)) {
            request.addHeader(entity.getContentType());
        }
        // Specify a content encoding if known
        if (entity.getContentEncoding() != null && !request.containsHeader(HTTP.CONTENT_ENCODING)) {
            request.addHeader(entity.getContentEncoding());
        }
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) ProtocolVersion(org.apache.http.ProtocolVersion)

Aggregations

ProtocolVersion (org.apache.http.ProtocolVersion)143 BasicStatusLine (org.apache.http.message.BasicStatusLine)67 Test (org.junit.Test)53 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)50 StatusLine (org.apache.http.StatusLine)44 HttpResponse (org.apache.http.HttpResponse)32 HttpEntity (org.apache.http.HttpEntity)31 Header (org.apache.http.Header)25 StringEntity (org.apache.http.entity.StringEntity)25 IOException (java.io.IOException)18 BasicHeader (org.apache.http.message.BasicHeader)15 URL (java.net.URL)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 HttpURLConnection (java.net.HttpURLConnection)13 HashMap (java.util.HashMap)13 List (java.util.List)13 ParseException (org.apache.http.ParseException)13 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 HttpHost (org.apache.http.HttpHost)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11