@Test public void testUnsupportedHttpVersionException() throws Exception { final HttpCoreContext context = HttpCoreContext.create(); final ClassicHttpRequest request = new BasicClassicHttpRequest("whatever", "/"); Mockito.when(conn.receiveRequestHeader()).thenReturn(request); Mockito.when(responseFactory.newHttpResponse(200)).thenReturn(response); Mockito.when(handlerResolver.resolve(request, context)).thenReturn(requestHandler); Mockito.doThrow(new UnsupportedHttpVersionException()).when( requestHandler).handle(request, response, context); httpservice.handleRequest(conn, context); final ArgumentCaptor<ClassicHttpResponse> responseCaptor = ArgumentCaptor.forClass(ClassicHttpResponse.class); Mockito.verify(conn).sendResponseHeader(responseCaptor.capture()); final ClassicHttpResponse error = responseCaptor.getValue(); Assert.assertNotNull(error); Assert.assertSame(request, context.getRequest()); Assert.assertEquals(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED, error.getCode()); Mockito.verify(httprocessor).process(error, error.getEntity(), context); Mockito.verify(conn).sendResponseHeader(error); Mockito.verify(conn).sendResponseEntity(error); Mockito.verify(conn).close(); }
public void handleRequest( final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException { final AtomicBoolean responseSubmitted = new AtomicBoolean(false); try { final ClassicHttpRequest request = conn.receiveRequestHeader(); if (streamListener != null) { streamListener.onRequestHead(conn, request); } conn.receiveRequestEntity(request); final ProtocolVersion transportVersion = request.getVersion(); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession()); context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails()); context.setAttribute(HttpCoreContext.HTTP_REQUEST, request); this.processor.process(request, request.getEntity(), context); this.requestHandler.handle(request, new HttpServerRequestHandler.ResponseTrigger() { @Override public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException { if (responseSubmitted.get()) { throw new HttpException("Response already submitted"); } if (response.getCode() >= HttpStatus.SC_SUCCESS) { throw new HttpException("Invalid intermediate response"); } if (streamListener != null) { streamListener.onResponseHead(conn, response); } conn.sendResponseHeader(response); conn.flush(); } @Override public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException { try { final ProtocolVersion transportVersion = response.getVersion(); if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) { throw new UnsupportedHttpVersionException(transportVersion); } ServerSupport.validateResponse(response, response.getEntity()); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response); processor.process(response, response.getEntity(), context); responseSubmitted.set(true); conn.sendResponseHeader(response); if (streamListener != null) { streamListener.onResponseHead(conn, response); } if (MessageSupport.canResponseHaveBody(request.getMethod(), response)) { conn.sendResponseEntity(response); } EntityUtils.consume(request.getEntity()); final boolean keepAlive = connReuseStrategy.keepAlive(request, response, context); if (streamListener != null) { streamListener.onExchangeComplete(conn, keepAlive); } if (!keepAlive) { conn.close(); } conn.flush(); } finally { response.close(); } } }, context); } catch (final HttpException ex) { if (responseSubmitted.get()) { throw ex; } try (final ClassicHttpResponse errorResponse = new BasicClassicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR)) { handleException(ex, errorResponse); errorResponse.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, errorResponse); this.processor.process(errorResponse, errorResponse.getEntity(), context); conn.sendResponseHeader(errorResponse); if (streamListener != null) { streamListener.onResponseHead(conn, errorResponse); } conn.sendResponseEntity(errorResponse); conn.close(); } } }
HttpService { public void handleRequest( final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException { final AtomicBoolean responseSubmitted = new AtomicBoolean(false); try { final ClassicHttpRequest request = conn.receiveRequestHeader(); if (streamListener != null) { streamListener.onRequestHead(conn, request); } conn.receiveRequestEntity(request); final ProtocolVersion transportVersion = request.getVersion(); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession()); context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails()); context.setAttribute(HttpCoreContext.HTTP_REQUEST, request); this.processor.process(request, request.getEntity(), context); this.requestHandler.handle(request, new HttpServerRequestHandler.ResponseTrigger() { @Override public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException { if (responseSubmitted.get()) { throw new HttpException("Response already submitted"); } if (response.getCode() >= HttpStatus.SC_SUCCESS) { throw new HttpException("Invalid intermediate response"); } if (streamListener != null) { streamListener.onResponseHead(conn, response); } conn.sendResponseHeader(response); conn.flush(); } @Override public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException { try { final ProtocolVersion transportVersion = response.getVersion(); if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) { throw new UnsupportedHttpVersionException(transportVersion); } ServerSupport.validateResponse(response, response.getEntity()); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response); processor.process(response, response.getEntity(), context); responseSubmitted.set(true); conn.sendResponseHeader(response); if (streamListener != null) { streamListener.onResponseHead(conn, response); } if (MessageSupport.canResponseHaveBody(request.getMethod(), response)) { conn.sendResponseEntity(response); } EntityUtils.consume(request.getEntity()); final boolean keepAlive = connReuseStrategy.keepAlive(request, response, context); if (streamListener != null) { streamListener.onExchangeComplete(conn, keepAlive); } if (!keepAlive) { conn.close(); } conn.flush(); } finally { response.close(); } } }, context); } catch (final HttpException ex) { if (responseSubmitted.get()) { throw ex; } try (final ClassicHttpResponse errorResponse = new BasicClassicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR)) { handleException(ex, errorResponse); errorResponse.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, errorResponse); this.processor.process(errorResponse, errorResponse.getEntity(), context); conn.sendResponseHeader(errorResponse); if (streamListener != null) { streamListener.onResponseHead(conn, errorResponse); } conn.sendResponseEntity(errorResponse); conn.close(); } } } }
HttpService { public void handleRequest( final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException { final AtomicBoolean responseSubmitted = new AtomicBoolean(false); try { final ClassicHttpRequest request = conn.receiveRequestHeader(); if (streamListener != null) { streamListener.onRequestHead(conn, request); } conn.receiveRequestEntity(request); final ProtocolVersion transportVersion = request.getVersion(); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession()); context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails()); context.setAttribute(HttpCoreContext.HTTP_REQUEST, request); this.processor.process(request, request.getEntity(), context); this.requestHandler.handle(request, new HttpServerRequestHandler.ResponseTrigger() { @Override public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException { if (responseSubmitted.get()) { throw new HttpException("Response already submitted"); } if (response.getCode() >= HttpStatus.SC_SUCCESS) { throw new HttpException("Invalid intermediate response"); } if (streamListener != null) { streamListener.onResponseHead(conn, response); } conn.sendResponseHeader(response); conn.flush(); } @Override public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException { try { final ProtocolVersion transportVersion = response.getVersion(); if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) { throw new UnsupportedHttpVersionException(transportVersion); } ServerSupport.validateResponse(response, response.getEntity()); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response); processor.process(response, response.getEntity(), context); responseSubmitted.set(true); conn.sendResponseHeader(response); if (streamListener != null) { streamListener.onResponseHead(conn, response); } if (MessageSupport.canResponseHaveBody(request.getMethod(), response)) { conn.sendResponseEntity(response); } EntityUtils.consume(request.getEntity()); final boolean keepAlive = connReuseStrategy.keepAlive(request, response, context); if (streamListener != null) { streamListener.onExchangeComplete(conn, keepAlive); } if (!keepAlive) { conn.close(); } conn.flush(); } finally { response.close(); } } }, context); } catch (final HttpException ex) { if (responseSubmitted.get()) { throw ex; } try (final ClassicHttpResponse errorResponse = new BasicClassicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR)) { handleException(ex, errorResponse); errorResponse.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, errorResponse); this.processor.process(errorResponse, errorResponse.getEntity(), context); conn.sendResponseHeader(errorResponse); if (streamListener != null) { streamListener.onResponseHead(conn, errorResponse); } conn.sendResponseEntity(errorResponse); conn.close(); } } } HttpService(
            final HttpProcessor processor,
            final HttpRequestMapper<HttpRequestHandler> handlerMapper,
            final ConnectionReuseStrategy connReuseStrategy,
            final HttpResponseFactory<ClassicHttpResponse> responseFactory,
            final Http1StreamListener streamListener);  HttpService(
            final HttpProcessor processor,
            final HttpRequestMapper<HttpRequestHandler> handlerMapper,
            final ConnectionReuseStrategy connReuseStrategy,
            final HttpResponseFactory<ClassicHttpResponse> responseFactory);  HttpService(
            final HttpProcessor processor,
            final HttpServerRequestHandler requestHandler,
            final ConnectionReuseStrategy connReuseStrategy,
            final Http1StreamListener streamListener);  HttpService(
            final HttpProcessor processor, final HttpServerRequestHandler requestHandler); }
HttpService { public void handleRequest( final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException { final AtomicBoolean responseSubmitted = new AtomicBoolean(false); try { final ClassicHttpRequest request = conn.receiveRequestHeader(); if (streamListener != null) { streamListener.onRequestHead(conn, request); } conn.receiveRequestEntity(request); final ProtocolVersion transportVersion = request.getVersion(); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession()); context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails()); context.setAttribute(HttpCoreContext.HTTP_REQUEST, request); this.processor.process(request, request.getEntity(), context); this.requestHandler.handle(request, new HttpServerRequestHandler.ResponseTrigger() { @Override public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException { if (responseSubmitted.get()) { throw new HttpException("Response already submitted"); } if (response.getCode() >= HttpStatus.SC_SUCCESS) { throw new HttpException("Invalid intermediate response"); } if (streamListener != null) { streamListener.onResponseHead(conn, response); } conn.sendResponseHeader(response); conn.flush(); } @Override public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException { try { final ProtocolVersion transportVersion = response.getVersion(); if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) { throw new UnsupportedHttpVersionException(transportVersion); } ServerSupport.validateResponse(response, response.getEntity()); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response); processor.process(response, response.getEntity(), context); responseSubmitted.set(true); conn.sendResponseHeader(response); if (streamListener != null) { streamListener.onResponseHead(conn, response); } if (MessageSupport.canResponseHaveBody(request.getMethod(), response)) { conn.sendResponseEntity(response); } EntityUtils.consume(request.getEntity()); final boolean keepAlive = connReuseStrategy.keepAlive(request, response, context); if (streamListener != null) { streamListener.onExchangeComplete(conn, keepAlive); } if (!keepAlive) { conn.close(); } conn.flush(); } finally { response.close(); } } }, context); } catch (final HttpException ex) { if (responseSubmitted.get()) { throw ex; } try (final ClassicHttpResponse errorResponse = new BasicClassicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR)) { handleException(ex, errorResponse); errorResponse.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, errorResponse); this.processor.process(errorResponse, errorResponse.getEntity(), context); conn.sendResponseHeader(errorResponse); if (streamListener != null) { streamListener.onResponseHead(conn, errorResponse); } conn.sendResponseEntity(errorResponse); conn.close(); } } } HttpService(
            final HttpProcessor processor,
            final HttpRequestMapper<HttpRequestHandler> handlerMapper,
            final ConnectionReuseStrategy connReuseStrategy,
            final HttpResponseFactory<ClassicHttpResponse> responseFactory,
            final Http1StreamListener streamListener);  HttpService(
            final HttpProcessor processor,
            final HttpRequestMapper<HttpRequestHandler> handlerMapper,
            final ConnectionReuseStrategy connReuseStrategy,
            final HttpResponseFactory<ClassicHttpResponse> responseFactory);  HttpService(
            final HttpProcessor processor,
            final HttpServerRequestHandler requestHandler,
            final ConnectionReuseStrategy connReuseStrategy,
            final Http1StreamListener streamListener);  HttpService(
            final HttpProcessor processor, final HttpServerRequestHandler requestHandler); void handleRequest(
            final HttpServerConnection conn,
            final HttpContext context); }
HttpService { public void handleRequest( final HttpServerConnection conn, final HttpContext context) throws IOException, HttpException { final AtomicBoolean responseSubmitted = new AtomicBoolean(false); try { final ClassicHttpRequest request = conn.receiveRequestHeader(); if (streamListener != null) { streamListener.onRequestHead(conn, request); } conn.receiveRequestEntity(request); final ProtocolVersion transportVersion = request.getVersion(); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.SSL_SESSION, conn.getSSLSession()); context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, conn.getEndpointDetails()); context.setAttribute(HttpCoreContext.HTTP_REQUEST, request); this.processor.process(request, request.getEntity(), context); this.requestHandler.handle(request, new HttpServerRequestHandler.ResponseTrigger() { @Override public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException { if (responseSubmitted.get()) { throw new HttpException("Response already submitted"); } if (response.getCode() >= HttpStatus.SC_SUCCESS) { throw new HttpException("Invalid intermediate response"); } if (streamListener != null) { streamListener.onResponseHead(conn, response); } conn.sendResponseHeader(response); conn.flush(); } @Override public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException { try { final ProtocolVersion transportVersion = response.getVersion(); if (transportVersion != null && transportVersion.greaterEquals(HttpVersion.HTTP_2)) { throw new UnsupportedHttpVersionException(transportVersion); } ServerSupport.validateResponse(response, response.getEntity()); context.setProtocolVersion(transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response); processor.process(response, response.getEntity(), context); responseSubmitted.set(true); conn.sendResponseHeader(response); if (streamListener != null) { streamListener.onResponseHead(conn, response); } if (MessageSupport.canResponseHaveBody(request.getMethod(), response)) { conn.sendResponseEntity(response); } EntityUtils.consume(request.getEntity()); final boolean keepAlive = connReuseStrategy.keepAlive(request, response, context); if (streamListener != null) { streamListener.onExchangeComplete(conn, keepAlive); } if (!keepAlive) { conn.close(); } conn.flush(); } finally { response.close(); } } }, context); } catch (final HttpException ex) { if (responseSubmitted.get()) { throw ex; } try (final ClassicHttpResponse errorResponse = new BasicClassicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR)) { handleException(ex, errorResponse); errorResponse.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE); context.setAttribute(HttpCoreContext.HTTP_RESPONSE, errorResponse); this.processor.process(errorResponse, errorResponse.getEntity(), context); conn.sendResponseHeader(errorResponse); if (streamListener != null) { streamListener.onResponseHead(conn, errorResponse); } conn.sendResponseEntity(errorResponse); conn.close(); } } } HttpService(
            final HttpProcessor processor,
            final HttpRequestMapper<HttpRequestHandler> handlerMapper,
            final ConnectionReuseStrategy connReuseStrategy,
            final HttpResponseFactory<ClassicHttpResponse> responseFactory,
            final Http1StreamListener streamListener);  HttpService(
            final HttpProcessor processor,
            final HttpRequestMapper<HttpRequestHandler> handlerMapper,
            final ConnectionReuseStrategy connReuseStrategy,
            final HttpResponseFactory<ClassicHttpResponse> responseFactory);  HttpService(
            final HttpProcessor processor,
            final HttpServerRequestHandler requestHandler,
            final ConnectionReuseStrategy connReuseStrategy,
            final Http1StreamListener streamListener);  HttpService(
            final HttpProcessor processor, final HttpServerRequestHandler requestHandler); void handleRequest(
            final HttpServerConnection conn,
            final HttpContext context);  }