Search in sources :

Example 11 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest in project spring-framework by spring-projects.

the class HttpComponentsClientHttpRequestFactoryTests method testRequestBodyAllowed.

private void testRequestBodyAllowed(URI uri, HttpMethod method, boolean allowed) {
    HttpUriRequest request = ((HttpComponentsClientHttpRequestFactory) this.factory).createHttpUriRequest(method, uri);
    assertEquals(allowed, request instanceof HttpEntityEnclosingRequest);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest)

Example 12 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest 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)

Example 13 with HttpEntityEnclosingRequest

use of org.apache.http.HttpEntityEnclosingRequest 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 14 with HttpEntityEnclosingRequest

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

the class AndroidHttpClient method toCurl.

/**
     * Generates a cURL command equivalent to the given request.
     */
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
    StringBuilder builder = new StringBuilder();
    builder.append("curl ");
    for (Header header : request.getAllHeaders()) {
        if (!logAuthToken && (header.getName().equals("Authorization") || header.getName().equals("Cookie"))) {
            continue;
        }
        builder.append("--header \"");
        builder.append(header.toString().trim());
        builder.append("\" ");
    }
    URI uri = request.getURI();
    // relative URI. We want an absolute URI.
    if (request instanceof RequestWrapper) {
        HttpRequest original = ((RequestWrapper) request).getOriginal();
        if (original instanceof HttpUriRequest) {
            uri = ((HttpUriRequest) original).getURI();
        }
    }
    builder.append("\"");
    builder.append(uri);
    builder.append("\"");
    if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
        HttpEntity entity = entityRequest.getEntity();
        if (entity != null && entity.isRepeatable()) {
            if (entity.getContentLength() < 1024) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                entity.writeTo(stream);
                if (isBinaryContent(request)) {
                    String base64 = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
                    builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; ");
                    builder.append(" --data-binary @/tmp/$$.bin");
                } else {
                    String entityString = stream.toString();
                    builder.append(" --data-ascii \"").append(entityString).append("\"");
                }
            } else {
                builder.append(" [TOO MUCH DATA TO INCLUDE]");
            }
        }
    }
    return builder.toString();
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) Header(org.apache.http.Header) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpEntity(org.apache.http.HttpEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) RequestWrapper(org.apache.http.impl.client.RequestWrapper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI)

Example 15 with HttpEntityEnclosingRequest

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

the class Request method sendRequest.

/**
     * Send the request line and headers
     */
void sendRequest(AndroidHttpClientConnection httpClientConnection) throws HttpException, IOException {
    // don't send cancelled requests
    if (mCancelled)
        return;
    if (HttpLog.LOGV) {
        HttpLog.v("Request.sendRequest() " + mHost.getSchemeName() + "://" + getHostPort());
        // HttpLog.v(mHttpRequest.getRequestLine().toString());
        if (false) {
            Iterator i = mHttpRequest.headerIterator();
            while (i.hasNext()) {
                Header header = (Header) i.next();
                HttpLog.v(header.getName() + ": " + header.getValue());
            }
        }
    }
    requestContentProcessor.process(mHttpRequest, mConnection.getHttpContext());
    httpClientConnection.sendRequestHeader(mHttpRequest);
    if (mHttpRequest instanceof HttpEntityEnclosingRequest) {
        httpClientConnection.sendRequestEntity((HttpEntityEnclosingRequest) mHttpRequest);
    }
    if (HttpLog.LOGV) {
        HttpLog.v("Request.requestSent() " + mHost.getSchemeName() + "://" + getHostPort() + mPath);
    }
}
Also used : Header(org.apache.http.Header) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) BasicHttpEntityEnclosingRequest(org.apache.http.message.BasicHttpEntityEnclosingRequest) Iterator(java.util.Iterator)

Aggregations

HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)38 HttpEntity (org.apache.http.HttpEntity)24 HttpResponse (org.apache.http.HttpResponse)14 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)12 Header (org.apache.http.Header)11 ProtocolVersion (org.apache.http.ProtocolVersion)11 HttpRequest (org.apache.http.HttpRequest)10 URI (java.net.URI)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 RequestWrapper (org.apache.http.impl.client.RequestWrapper)5 IOException (java.io.IOException)4 HttpException (org.apache.http.HttpException)4 InputStreamEntity (org.apache.http.entity.InputStreamEntity)4 ProtocolException (java.net.ProtocolException)3 Iterator (java.util.Iterator)3 HttpHost (org.apache.http.HttpHost)3 ProtocolException (org.apache.http.ProtocolException)3 HttpPost (org.apache.http.client.methods.HttpPost)3 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)3 AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)3