@Override public DefaultBHttpClientConnection createConnection(final Socket socket) throws IOException { final DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection( this.h1Config, CharCodingSupport.createDecoder(this.charCodingConfig), CharCodingSupport.createEncoder(this.charCodingConfig), this.incomingContentStrategy, this.outgoingContentStrategy, this.requestWriterFactory, this.responseParserFactory); conn.bind(socket); return conn; }
@Override public void close(final CloseMode closeMode) { if (this.log.isDebugEnabled()) { this.log.debug(this.id + ": Shutdown connection"); } super.close(closeMode); }
@Override public void receiveResponseEntity( final ClassicHttpResponse response) throws HttpException, IOException { Args.notNull(response, "HTTP response"); final SocketHolder socketHolder = ensureOpen(); final long len = this.incomingContentStrategy.determineLength(response); response.setEntity(createIncomingEntity(response, this.inBuffer, socketHolder.getInputStream(), len)); } }
@Test(expected = LengthRequiredException.class) public void testWriteRequestEntityNoContentLength() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/stuff"); request.addHeader("User-Agent", "test"); request.setEntity(new StringEntity("123", ContentType.TEXT_PLAIN)); conn.sendRequestHeader(request); conn.sendRequestEntity(request); }
@Test(expected = NotImplementedException.class) public void testReadResponseEntityIdentity() throws Exception { final String s = "HTTP/1.1 200 OK\r\nServer: test\r\nTransfer-Encoding: identity\r\n\r\n123"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = conn.receiveResponseHeader(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getCode()); Assert.assertTrue(response.containsHeader("Server")); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); conn.receiveResponseEntity(response); }
coreContext.setTargetHost(host); DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024); ConnectionReuseStrategy connStrategy = DefaultConnectionReuseStrategy.INSTANCE; if (!conn.isOpen()) { Socket socket = new Socket(host.getHostName(), host.getPort()); conn.bind(socket); System.out.println("=============="); if (!connStrategy.keepAlive(response, coreContext)) { conn.close(); } else { System.out.println("Connection kept alive..."); conn.close();
@Test public void testTerminateRequestContentLengthLong() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/stuff"); request.addHeader("User-Agent", "test"); request.addHeader("Content-Length", "3000"); final ByteArrayEntity entity = new ByteArrayEntity(new byte[3000], ContentType.TEXT_PLAIN); entity.setChunked(false); request.setEntity(entity); conn.sendRequestHeader(request); conn.terminateRequest(request); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: " + "3000\r\n\r\n", s); Assert.assertFalse(conn.isConsistent()); }
@Override public void bind(final SocketHolder socketHolder) throws IOException { if (this.closed.get()) { final Socket socket = socketHolder.getSocket(); socket.close(); // allow this to throw... // ...but if it doesn't, explicitly throw one ourselves. throw new InterruptedIOException("Connection already shutdown"); } super.bind(socketHolder); socketTimeout = Timeout.ofMilliseconds(socketHolder.getSocket().getSoTimeout()); }
@Test public void testReadResponseHead() throws Exception { final String s = "HTTP/1.1 200 OK\r\nUser-Agent: test\r\n\r\n"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = conn.receiveResponseHeader(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getCode()); Assert.assertTrue(response.containsHeader("User-Agent")); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); conn = new DefaultBHttpClientConnection(H1Config.DEFAULT, null, null, DefaultContentLengthStrategy.INSTANCE, DefaultContentLengthStrategy.INSTANCE, DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE); }
@Override public void terminateRequest(final ClassicHttpRequest request) throws HttpException, IOException { Args.notNull(request, "HTTP request"); final SocketHolder socketHolder = ensureOpen(); final HttpEntity entity = request.getEntity(); if (entity == null) { return; } final long len = this.outgoingContentStrategy.determineLength(request); if (len == ContentLengthStrategy.CHUNKED) { try (final OutputStream outStream = createContentOutputStream(len, this.outbuffer, socketHolder.getOutputStream(), entity.getTrailers())) { // just close } } else if (len >= 0 && len <= 1024) { try (final OutputStream outStream = createContentOutputStream(len, this.outbuffer, socketHolder.getOutputStream(), null)) { entity.writeTo(outStream); } } else { this.consistent = false; } }
@Test public void testWriteRequestNoEntity() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/stuff"); request.addHeader("User-Agent", "test"); conn.sendRequestHeader(request); conn.sendRequestEntity(request); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\n\r\n", s); }
@Test public void testReadResponseNoEntity() throws Exception { final String s = "HTTP/1.1 200 OK\r\nServer: test\r\n\r\n"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = conn.receiveResponseHeader(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getCode()); Assert.assertTrue(response.containsHeader("Server")); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); conn.receiveResponseEntity(response); final HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue(content instanceof IdentityInputStream); }
@Test public void testTerminateRequestContentLengthShort() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/stuff"); request.addHeader("User-Agent", "test"); request.addHeader("Content-Length", "3"); final StringEntity entity = new StringEntity("123", ContentType.TEXT_PLAIN); entity.setChunked(false); request.setEntity(entity); conn.sendRequestHeader(request); conn.terminateRequest(request); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: " + "3\r\n\r\n123", s); Assert.assertTrue(conn.isConsistent()); }
@Override public void bind(final Socket socket) throws IOException { super.bind(this.wireLog.isDebugEnabled() ? new LoggingSocketHolder(socket, this.id, this.wireLog) : new SocketHolder(socket)); socketTimeout = Timeout.ofMilliseconds(socket.getSoTimeout()); }
@Override public void terminateRequest(final ClassicHttpRequest request) throws HttpException, IOException { Args.notNull(request, "HTTP request"); final SocketHolder socketHolder = ensureOpen(); final HttpEntity entity = request.getEntity(); if (entity == null) { return; } final long len = this.outgoingContentStrategy.determineLength(request); if (len == ContentLengthStrategy.CHUNKED) { try (final OutputStream outStream = createContentOutputStream(len, this.outbuffer, socketHolder.getOutputStream(), entity.getTrailers())) { // just close } } else if (len >= 0 && len <= 1024) { try (final OutputStream outStream = createContentOutputStream(len, this.outbuffer, socketHolder.getOutputStream(), null)) { entity.writeTo(outStream); } } else { this.consistent = false; } }
@Test public void testWriteRequestHead() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = new BasicClassicHttpRequest("GET", "/stuff"); request.addHeader("User-Agent", "test"); conn.sendRequestHeader(request); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("GET /stuff HTTP/1.1\r\nUser-Agent: test\r\n\r\n", s); }
@Test public void testReadResponseEntityWithContentLength() throws Exception { final String s = "HTTP/1.1 200 OK\r\nServer: test\r\nContent-Length: 3\r\n\r\n123"; final ByteArrayInputStream inStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); Mockito.when(socket.getInputStream()).thenReturn(inStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = conn.receiveResponseHeader(); Assert.assertNotNull(response); Assert.assertEquals(200, response.getCode()); Assert.assertTrue(response.containsHeader("Server")); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); conn.receiveResponseEntity(response); final HttpEntity entity = response.getEntity(); Assert.assertNotNull(entity); Assert.assertEquals(3, entity.getContentLength()); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue(content instanceof ContentLengthInputStream); }
@Test public void testTerminateRequestChunkedEntity() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getRequestCount()); final ClassicHttpRequest request = new BasicClassicHttpRequest("POST", "/stuff"); request.addHeader("User-Agent", "test"); request.addHeader("Transfer-Encoding", "chunked"); final StringEntity entity = new StringEntity("123", ContentType.TEXT_PLAIN); entity.setChunked(true); request.setEntity(entity); conn.sendRequestHeader(request); conn.terminateRequest(request); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nTransfer-Encoding: " + "chunked\r\n\r\n0\r\n\r\n", s); Assert.assertTrue(conn.isConsistent()); }