Search in sources :

Example 16 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project android-uploader by nightscout.

the class RestLegacyUploaderTest method setUpExecuteCaptor.

public void setUpExecuteCaptor(int status) throws IOException {
    captor = ArgumentCaptor.forClass(HttpUriRequest.class);
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("mock", 1, 2), status, ""));
    response.setEntity(new StringEntity(""));
    when(mockHttpClient.execute(captor.capture())).thenReturn(response);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StringEntity(org.apache.http.entity.StringEntity) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Example 17 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project robovm by robovm.

the class BasicLineParser method parseStatusLine.

// non-javadoc, see interface LineParser
public StatusLine parseStatusLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();
    try {
        // handle the HTTP-Version
        ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
        // handle the Status-Code
        skipWhitespace(buffer, cursor);
        int i = cursor.getPos();
        int blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            blank = indexTo;
        }
        int statusCode = 0;
        try {
            statusCode = Integer.parseInt(buffer.substringTrimmed(i, blank));
        } catch (NumberFormatException e) {
            throw new ParseException("Unable to parse status code from status line: " + buffer.substring(indexFrom, indexTo));
        }
        //handle the Reason-Phrase
        i = blank;
        String reasonPhrase = null;
        if (i < indexTo) {
            reasonPhrase = buffer.substringTrimmed(i, indexTo);
        } else {
            reasonPhrase = "";
        }
        return createStatusLine(ver, statusCode, reasonPhrase);
    } catch (IndexOutOfBoundsException e) {
        throw new ParseException("Invalid status line: " + buffer.substring(indexFrom, indexTo));
    }
}
Also used : ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 18 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project robovm by robovm.

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 19 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project robovm by robovm.

the class BasicHttpResponse method setStatusCode.

// non-javadoc, see interface HttpResponse
public void setStatusCode(int code) {
    // argument checked in BasicStatusLine constructor
    ProtocolVersion ver = this.statusline.getProtocolVersion();
    this.statusline = new BasicStatusLine(ver, code, getReason(code));
}
Also used : ProtocolVersion(org.apache.http.ProtocolVersion)

Example 20 with ProtocolVersion

use of org.apache.http.ProtocolVersion in project robovm by robovm.

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)

Aggregations

ProtocolVersion (org.apache.http.ProtocolVersion)105 BasicStatusLine (org.apache.http.message.BasicStatusLine)46 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)43 StatusLine (org.apache.http.StatusLine)42 Header (org.apache.http.Header)26 HttpEntity (org.apache.http.HttpEntity)26 Test (org.junit.Test)25 HttpResponse (org.apache.http.HttpResponse)22 URL (java.net.URL)16 BasicHeader (org.apache.http.message.BasicHeader)16 IOException (java.io.IOException)15 HttpURLConnection (java.net.HttpURLConnection)15 List (java.util.List)15 HashMap (java.util.HashMap)14 HttpHost (org.apache.http.HttpHost)12 ParseException (org.apache.http.ParseException)12 StringEntity (org.apache.http.entity.StringEntity)12 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 MockHttpStack (com.android.volley.mock.MockHttpStack)10 HttpRequest (org.apache.http.HttpRequest)10