Search in sources :

Example 91 with ProtocolVersion

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

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

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

the class RequestTargetHost 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 (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    if (!request.containsHeader(HTTP.TARGET_HOST)) {
        HttpHost targethost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (targethost == null) {
            HttpConnection conn = (HttpConnection) context.getAttribute(ExecutionContext.HTTP_CONNECTION);
            if (conn instanceof HttpInetConnection) {
                // Populate the context with a default HTTP host based on the 
                // inet address of the target host
                InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
                int port = ((HttpInetConnection) conn).getRemotePort();
                if (address != null) {
                    targethost = new HttpHost(address.getHostName(), port);
                }
            }
            if (targethost == null) {
                ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
                if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
                    return;
                } else {
                    throw new ProtocolException("Target host missing");
                }
            }
        }
        request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpConnection(org.apache.http.HttpConnection) HttpHost(org.apache.http.HttpHost) HttpInetConnection(org.apache.http.HttpInetConnection) ProtocolVersion(org.apache.http.ProtocolVersion) InetAddress(java.net.InetAddress)

Example 93 with ProtocolVersion

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

the class ResponseConnControl method process.

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    // Always drop connection after certain type of responses
    int status = response.getStatusLine().getStatusCode();
    if (status == HttpStatus.SC_BAD_REQUEST || status == HttpStatus.SC_REQUEST_TIMEOUT || status == HttpStatus.SC_LENGTH_REQUIRED || status == HttpStatus.SC_REQUEST_TOO_LONG || status == HttpStatus.SC_REQUEST_URI_TOO_LONG || status == HttpStatus.SC_SERVICE_UNAVAILABLE || status == HttpStatus.SC_NOT_IMPLEMENTED) {
        response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
        return;
    }
    // Always drop connection for HTTP/1.0 responses and below
    // if the content body cannot be correctly delimited
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
        if (entity.getContentLength() < 0 && (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0))) {
            response.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
            return;
        }
    }
    // Drop connection if requested by the client
    HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
    if (request != null) {
        Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
        if (header != null) {
            response.setHeader(HTTP.CONN_DIRECTIVE, header.getValue());
        }
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 94 with ProtocolVersion

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

the class ResponseContent 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 (response.containsHeader(HTTP.TRANSFER_ENCODING)) {
        throw new ProtocolException("Transfer-encoding header already present");
    }
    if (response.containsHeader(HTTP.CONTENT_LEN)) {
        throw new ProtocolException("Content-Length header already present");
    }
    ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        long len = entity.getContentLength();
        if (entity.isChunked() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
            response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
        } else if (len >= 0) {
            response.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
        }
        // Specify a content type if known
        if (entity.getContentType() != null && !response.containsHeader(HTTP.CONTENT_TYPE)) {
            response.addHeader(entity.getContentType());
        }
        // Specify a content encoding if known
        if (entity.getContentEncoding() != null && !response.containsHeader(HTTP.CONTENT_ENCODING)) {
            response.addHeader(entity.getContentEncoding());
        }
    } else {
        int status = response.getStatusLine().getStatusCode();
        if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_NOT_MODIFIED && status != HttpStatus.SC_RESET_CONTENT) {
            response.addHeader(HTTP.CONTENT_LEN, "0");
        }
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) HttpEntity(org.apache.http.HttpEntity) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 95 with ProtocolVersion

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

the class BasicLineParser method parseRequestLine.

/**
     * Parses a request line.
     *
     * @param buffer    a buffer holding the line to parse
     *
     * @return  the parsed request line
     *
     * @throws ParseException        in case of a parse error
     */
public RequestLine parseRequestLine(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 {
        skipWhitespace(buffer, cursor);
        int i = cursor.getPos();
        int blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        String method = buffer.substringTrimmed(i, blank);
        cursor.updatePos(blank);
        skipWhitespace(buffer, cursor);
        i = cursor.getPos();
        blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        String uri = buffer.substringTrimmed(i, blank);
        cursor.updatePos(blank);
        ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
        skipWhitespace(buffer, cursor);
        if (!cursor.atEnd()) {
            throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
        }
        return createRequestLine(method, uri, ver);
    } catch (IndexOutOfBoundsException e) {
        throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
    }
}
Also used : ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Aggregations

ProtocolVersion (org.apache.http.ProtocolVersion)113 BasicStatusLine (org.apache.http.message.BasicStatusLine)54 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)43 StatusLine (org.apache.http.StatusLine)42 Test (org.junit.Test)33 Header (org.apache.http.Header)26 HttpEntity (org.apache.http.HttpEntity)26 HttpResponse (org.apache.http.HttpResponse)22 StringEntity (org.apache.http.entity.StringEntity)20 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 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)11 MockHttpStack (com.android.volley.mock.MockHttpStack)10 HttpRequest (org.apache.http.HttpRequest)10