@Override public void exchangeTerminated(HttpExchange exchange, Result result) { super.exchangeTerminated(exchange, result); Response response = result.getResponse(); HttpFields responseHeaders = response.getHeaders(); String closeReason = null; if (result.isFailed()) closeReason = "failure"; else if (receiver.isShutdown()) closeReason = "server close"; if (closeReason == null) { if (response.getVersion().compareTo(HttpVersion.HTTP_1_1) < 0) { // HTTP 1.0 must close the connection unless it has // an explicit keep alive or it's a CONNECT method. boolean keepAlive = responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString()); boolean connect = HttpMethod.CONNECT.is(exchange.getRequest().getMethod()); if (!keepAlive && !connect) closeReason = "http/1.0"; } else { // HTTP 1.1 or greater closes only if it has an explicit close. if (responseHeaders.contains(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString())) closeReason = "http/1.1"; } } if (closeReason != null) { if (LOG.isDebugEnabled()) LOG.debug("Closing, reason: {} - {}", closeReason, connection); connection.close(); } else { if (response.getStatus() == HttpStatus.SWITCHING_PROTOCOLS_101) connection.remove(); else release(); } }
@Override public Result exchangeTerminating(HttpExchange exchange, Result result) { if (result.isFailed()) return result; HttpResponse response = exchange.getResponse(); if ((response.getVersion() == HttpVersion.HTTP_1_1) && (response.getStatus() == HttpStatus.SWITCHING_PROTOCOLS_101)) { String connection = response.getHeaders().get(HttpHeader.CONNECTION); if ((connection == null) || !connection.toLowerCase(Locale.US).contains("upgrade")) { return new Result( result, new HttpResponseException( "101 Switching Protocols without Connection: Upgrade not supported", response)); } // Upgrade Response HttpRequest request = exchange.getRequest(); if (request instanceof HttpConnectionUpgrader) { HttpConnectionUpgrader listener = (HttpConnectionUpgrader) request; try { listener.upgrade(response, getHttpConnection()); } catch (Throwable x) { return new Result(result, x); } } } return result; }
protected boolean success() { HttpExchange exchange = connection.getExchange(); if (exchange == null) return false; AtomicMarkableReference<Result> completion = exchange.responseComplete(null); if (!completion.isMarked()) return false; parser.reset(); decoder = null; if (!updateState(State.RECEIVE, State.IDLE)) throw new IllegalStateException(); exchange.terminateResponse(); HttpResponse response = exchange.getResponse(); List<Response.ResponseListener> listeners = exchange.getConversation().getResponseListeners(); ResponseNotifier notifier = connection.getDestination().getResponseNotifier(); notifier.notifySuccess(listeners, response); LOG.debug("Received {}", response); Result result = completion.getReference(); if (result != null) { connection.complete(exchange, !result.isFailed()); notifier.notifyComplete(listeners, result); } return true; }