@Override public DefaultBHttpServerConnection createConnection(final Socket socket) throws IOException { final DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection( this.scheme, this.h1Config, CharCodingSupport.createDecoder(this.charCodingConfig), CharCodingSupport.createEncoder(this.charCodingConfig), this.incomingContentStrategy, this.outgoingContentStrategy, this.requestParserFactory, this.responseWriterFactory); 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 receiveRequestEntity(final ClassicHttpRequest request) throws HttpException, IOException { Args.notNull(request, "HTTP request"); final SocketHolder socketHolder = ensureOpen(); final long len = this.incomingContentStrategy.determineLength(request); if (len == ContentLengthStrategy.UNDEFINED) { return; } request.setEntity(createIncomingEntity(request, this.inBuffer, socketHolder.getInputStream(), len)); }
@Override public void sendResponseHeader(final ClassicHttpResponse response) throws HttpException, IOException { Args.notNull(response, "HTTP response"); final SocketHolder socketHolder = ensureOpen(); this.responseWriter.write(response, this.outbuffer, socketHolder.getOutputStream()); onResponseSubmitted(response); if (response.getCode() >= HttpStatus.SC_SUCCESS) { incrementResponseCount(); } }
@Test public void testWriteResponse100Head() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = new BasicClassicHttpResponse(100, "Go on"); conn.sendResponseHeader(response); conn.flush(); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("HTTP/1.1 100 Go on\r\n\r\n", s); }
@Test(expected = ProtocolException.class) public void testReadRequestEntityIdentity() throws Exception { final String s = "POST /stuff HTTP/1.1\r\nUser-Agent: 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().getRequestCount()); final ClassicHttpRequest request = conn.receiveRequestHeader(); Assert.assertNotNull(request); Assert.assertEquals("/stuff", request.getPath()); Assert.assertEquals("POST", request.getMethod()); Assert.assertTrue(request.containsHeader("User-Agent")); Assert.assertNull(request.getEntity()); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); conn.receiveRequestEntity(request); }
@Test public void testReadRequestHead() throws Exception { final String s = "GET / HTTP/1.1\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().getRequestCount()); final ClassicHttpRequest request = conn.receiveRequestHeader(); Assert.assertNotNull(request); Assert.assertEquals("/", request.getPath()); Assert.assertEquals("GET", request.getMethod()); Assert.assertTrue(request.containsHeader("User-Agent")); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); }
@Override public void bind(final Socket socket) throws IOException { super.bind(this.wire.isEnabled() ? new LoggingSocketHolder(socket, wire) : new SocketHolder(socket)); }
@Override public ClassicHttpRequest receiveRequestHeader() throws HttpException, IOException { final SocketHolder socketHolder = ensureOpen(); final ClassicHttpRequest request = this.requestParser.parse(this.inBuffer, socketHolder.getInputStream()); final ProtocolVersion transportVersion = request.getVersion(); if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) { throw new UnsupportedHttpVersionException(transportVersion); } request.setScheme(this.scheme); this.version = transportVersion; onRequestReceived(request); incrementRequestCount(); return request; }
@Override public void sendResponseEntity(final ClassicHttpResponse response) throws HttpException, IOException { Args.notNull(response, "HTTP response"); final SocketHolder socketHolder = ensureOpen(); final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final long len = this.outgoingContentStrategy.determineLength(response); try (final OutputStream outStream = createContentOutputStream(len, this.outbuffer, socketHolder.getOutputStream(), entity.getTrailers())) { entity.writeTo(outStream); } } }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); conn = new DefaultBHttpServerConnection("http", H1Config.DEFAULT, null, null, DefaultContentLengthStrategy.INSTANCE, DefaultContentLengthStrategy.INSTANCE, DefaultHttpRequestParserFactory.INSTANCE, DefaultHttpResponseWriterFactory.INSTANCE); }
@Test public void testWriteResponseNoEntity() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK"); response.addHeader("Server", "test"); conn.sendResponseHeader(response); conn.sendResponseEntity(response); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("HTTP/1.1 200 OK\r\nServer: test\r\n\r\n", s); }
@Test public void testReadRequestNoEntity() throws Exception { final String s = "POST /stuff HTTP/1.1\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().getRequestCount()); final ClassicHttpRequest request = conn.receiveRequestHeader(); Assert.assertNotNull(request); Assert.assertEquals("/stuff", request.getPath()); Assert.assertEquals("POST", request.getMethod()); Assert.assertTrue(request.containsHeader("User-Agent")); Assert.assertNull(request.getEntity()); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); conn.receiveRequestEntity(request); final HttpEntity entity = request.getEntity(); Assert.assertNull(entity); }
@Override public void sendResponseHeader(final ClassicHttpResponse response) throws HttpException, IOException { Args.notNull(response, "HTTP response"); final SocketHolder socketHolder = ensureOpen(); this.responseWriter.write(response, this.outbuffer, socketHolder.getOutputStream()); onResponseSubmitted(response); if (response.getCode() >= HttpStatus.SC_SUCCESS) { incrementResponseCount(); } }
@Override public void bind(final Socket socket) throws IOException { super.bind(this.wire.isEnabled() ? new LoggingSocketHolder(socket, wire) : new SocketHolder(socket)); }
@Override public ClassicHttpRequest receiveRequestHeader() throws HttpException, IOException { final SocketHolder socketHolder = ensureOpen(); final ClassicHttpRequest request = this.requestParser.parse(this.inBuffer, socketHolder.getInputStream()); final ProtocolVersion transportVersion = request.getVersion(); if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) { throw new UnsupportedHttpVersionException(transportVersion); } request.setScheme(this.scheme); this.version = transportVersion; onRequestReceived(request); incrementRequestCount(); return request; }
@Override public void sendResponseEntity(final ClassicHttpResponse response) throws HttpException, IOException { Args.notNull(response, "HTTP response"); final SocketHolder socketHolder = ensureOpen(); final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final long len = this.outgoingContentStrategy.determineLength(response); try (final OutputStream outStream = createContentOutputStream(len, this.outbuffer, socketHolder.getOutputStream(), entity.getTrailers())) { entity.writeTo(outStream); } } }
@Test public void testWriteResponseHead() throws Exception { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); Mockito.when(socket.getOutputStream()).thenReturn(outStream); conn.bind(socket); Assert.assertEquals(0, conn.getEndpointDetails().getResponseCount()); final ClassicHttpResponse response = new BasicClassicHttpResponse(200, "OK"); response.addHeader("User-Agent", "test"); conn.sendResponseHeader(response); conn.flush(); Assert.assertEquals(1, conn.getEndpointDetails().getResponseCount()); final String s = new String(outStream.toByteArray(), "ASCII"); Assert.assertEquals("HTTP/1.1 200 OK\r\nUser-Agent: test\r\n\r\n", s); }
@Test public void testReadRequestEntityWithContentLength() throws Exception { final String s = "POST / HTTP/1.1\r\nUser-Agent: 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().getRequestCount()); final ClassicHttpRequest request = conn.receiveRequestHeader(); Assert.assertNotNull(request); Assert.assertEquals("/", request.getPath()); Assert.assertEquals("POST", request.getMethod()); Assert.assertTrue(request.containsHeader("User-Agent")); Assert.assertNull(request.getEntity()); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); conn.receiveRequestEntity(request); final HttpEntity entity = request.getEntity(); Assert.assertNotNull(entity); Assert.assertEquals(3, entity.getContentLength()); Assert.assertEquals(1, conn.getEndpointDetails().getRequestCount()); final InputStream content = entity.getContent(); Assert.assertNotNull(content); Assert.assertTrue(content instanceof ContentLengthInputStream); }
@Override public DefaultBHttpServerConnection createConnection(final Socket socket) throws IOException { final DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection( this.scheme, this.h1Config, CharCodingSupport.createDecoder(this.charCodingConfig), CharCodingSupport.createEncoder(this.charCodingConfig), this.incomingContentStrategy, this.outgoingContentStrategy, this.requestParserFactory, this.responseWriterFactory); conn.bind(socket); return conn; }