Package org.apache.http

Examples of org.apache.http.HttpVersion


        assertTrue(reuseStrategy.keepAlive(response, context));
    }

    public void testFutureHTTP() throws Exception {
        HttpResponse response =
            createResponse(new HttpVersion(3, 45), 200, "OK");

        assertTrue(reuseStrategy.keepAlive(response, context));
    }
View Full Code Here


    }

    public void testHttpVersionParsing() throws Exception {

        String s = "HTTP/1.1";
        HttpVersion version = (HttpVersion)
            BasicLineParser.parseProtocolVersion(s, null);
        assertEquals("HTTP protocol name", "HTTP", version.getProtocol());
        assertEquals("HTTP major version number", 1, version.getMajor());
        assertEquals("HTTP minor version number", 1, version.getMinor());
        assertEquals("HTTP version number", s, version.toString());

        s = "HTTP/123.4567";
        version = (HttpVersion)
            BasicLineParser.parseProtocolVersion(s, null);
        assertEquals("HTTP protocol name", "HTTP", version.getProtocol());
        assertEquals("HTTP major version number", 123, version.getMajor());
        assertEquals("HTTP minor version number", 4567, version.getMinor());
        assertEquals("HTTP version number", s, version.toString());
    }
View Full Code Here

        buffer.append(s);
        ParserCursor cursor = new ParserCursor(0, s.length());
       
        LineParser parser = BasicLineParser.DEFAULT;
       
        HttpVersion version = (HttpVersion) parser.parseProtocolVersion(buffer, cursor);
        assertEquals("HTTP protocol name", "HTTP", version.getProtocol());
        assertEquals("HTTP major version number", 1, version.getMajor());
        assertEquals("HTTP minor version number", 1, version.getMinor());
        assertEquals("HTTP version number", "HTTP/1.1", version.toString());
        assertEquals(s.length(), cursor.getPos());
        assertTrue(cursor.atEnd());
       
        s = "HTTP/1.123 123";
        buffer = new CharArrayBuffer(16);
        buffer.append(s);
        cursor = new ParserCursor(0, s.length());
       
        version = (HttpVersion) parser.parseProtocolVersion(buffer, cursor);
        assertEquals("HTTP protocol name", "HTTP", version.getProtocol());
        assertEquals("HTTP major version number", 1, version.getMajor());
        assertEquals("HTTP minor version number", 123, version.getMinor());
        assertEquals("HTTP version number", "HTTP/1.123", version.toString());
        assertEquals(' ', buffer.charAt(cursor.getPos()));
        assertEquals(s.length() - 4, cursor.getPos());
        assertFalse(cursor.atEnd());
    }
View Full Code Here

        try {
            minor = Integer.parseInt(s.substring(i1, i2));
        } catch (NumberFormatException e) {
            throw new ProtocolException("Invalid HTTP minor version number: " + s);
        }
        return new HttpVersion(major, minor);
    }
View Full Code Here

   
    public RequestLine getRequestLine() {
        if (this.requestline != null) {
            return this.requestline;
        } else {
            HttpVersion ver = HttpProtocolParams.getVersion(getParams());
            return new RequestLine(this.method, this.uri, ver);
        }
    }
View Full Code Here

    public boolean keepAlive(final HttpResponse response) {
        if (response == null) {
            throw new IllegalArgumentException("HTTP response may not be null");
        }
        HttpEntity entity = response.getEntity();
        HttpVersion ver = response.getStatusLine().getHttpVersion();
        if (entity != null) {
            if (entity.getContentLength() < 0) {
                if (!entity.isChunked() || ver.lessEquals(HttpVersion.HTTP_1_0)) {
                    // if the content length is not known and is not chunk
                    // encoded, the connection cannot be reused
                    return false;
                }
            }
        }
        // Check for 'Connection' directive
        Header connheader = response.getFirstHeader(HTTP.CONN_DIRECTIVE);
        if (connheader != null) {
            String conndirective = connheader.getValue();
            if (HTTP.CONN_CLOSE.equalsIgnoreCase(conndirective)) {
                return false;
            } else if (HTTP.CONN_KEEP_ALIVE.equalsIgnoreCase(conndirective)) {
                return true;
            } else {
                // log unknown directive
            }
        }
        // Resorting to protocol version default close connection policy
        return ver.greaterEquals(HttpVersion.HTTP_1_1);
    }
View Full Code Here

            throw new ProtocolException("Transfer-encoding header already present");
        }
        if (response.containsHeader(HTTP.CONTENT_LEN)) {
            throw new ProtocolException("Content-Length header already present");
        }
        HttpVersion ver = response.getStatusLine().getHttpVersion();
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            long len = entity.getContentLength();
            if (entity.isChunked() && ver.greaterEquals(HttpVersion.HTTP_1_1)) {
                response.addHeader(new Header(HTTP.TRANSFER_ENCODING,
                        HTTP.CHUNK_CODING));
            } else if (len >= 0) {
                response.addHeader(new Header(HTTP.CONTENT_LEN,
                        Long.toString(entity.getContentLength())));
View Full Code Here

            // 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 HttpVersion ver = request.getRequestLine().getHttpVersion();
            if (entityEnclRequest.expectContinue() &&
                ver.greaterEquals(HttpVersion.HTTP_1_1)) {

                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.
                if (conn.isResponseAvailable(WAIT_FOR_CONTINUE_MS)) {
View Full Code Here

        }
        if (request instanceof HttpEntityEnclosingRequest) {
            HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
            // Do not send the expect header if request body is known to be empty
            if (entity != null && entity.getContentLength() != 0) {
                HttpVersion ver = request.getRequestLine().getHttpVersion();
                if (HttpProtocolParams.useExpectContinue(request.getParams())
                        && ver.greaterEquals(HttpVersion.HTTP_1_1)) {
                    request.addHeader(new Header(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE));
                }
            }
        }
    }
View Full Code Here

                throw new ProtocolException("Transfer-encoding header already present");
            }
            if (request.containsHeader(HTTP.CONTENT_LEN)) {
                throw new ProtocolException("Content-Length header already present");
            }
            HttpVersion ver = request.getRequestLine().getHttpVersion();
            HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
            if (entity == null) {
                request.setHeader(new Header(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(new Header(HTTP.TRANSFER_ENCODING,
                        HTTP.CHUNK_CODING));
View Full Code Here

TOP

Related Classes of org.apache.http.HttpVersion

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.