Path: blob/master/src/java.net.http/share/classes/jdk/internal/net/http/Response.java
41171 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.net.http;2627import java.net.URI;28import java.io.IOException;29import java.net.http.HttpClient;30import java.net.http.HttpHeaders;31import java.net.InetSocketAddress;32import javax.net.ssl.SSLEngine;33import javax.net.ssl.SSLSession;34import jdk.internal.net.http.common.Utils;3536/**37* Response headers and status code.38*/39class Response {40final HttpHeaders headers;41final int statusCode;42final HttpRequestImpl request;43final Exchange<?> exchange;44final HttpClient.Version version;45final boolean isConnectResponse;46final SSLSession sslSession;47final InetSocketAddress localAddress;4849Response(HttpRequestImpl req,50Exchange<?> exchange,51HttpHeaders headers,52HttpConnection connection,53int statusCode,54HttpClient.Version version) {55this(req, exchange, headers, connection, statusCode, version,56"CONNECT".equalsIgnoreCase(req.method()));57}5859Response(HttpRequestImpl req,60Exchange<?> exchange,61HttpHeaders headers,62HttpConnection connection,63int statusCode,64HttpClient.Version version,65boolean isConnectResponse) {66this.headers = headers;67this.request = req;68this.version = version;69this.exchange = exchange;70this.statusCode = statusCode;71this.isConnectResponse = isConnectResponse;72if (connection != null) {73InetSocketAddress a;74try {75a = (InetSocketAddress)connection.channel().getLocalAddress();76} catch (IOException e) {77a = null;78}79this.localAddress = a;80if (connection instanceof AbstractAsyncSSLConnection) {81AbstractAsyncSSLConnection cc = (AbstractAsyncSSLConnection)connection;82SSLEngine engine = cc.getEngine();83sslSession = Utils.immutableSession(engine.getSession());84} else {85sslSession = null;86}87} else {88sslSession = null;89localAddress = null;90}91}9293HttpRequestImpl request() {94return request;95}9697HttpClient.Version version() {98return version;99}100101HttpHeaders headers() {102return headers;103}104105int statusCode() {106return statusCode;107}108109SSLSession getSSLSession() {110return sslSession;111}112113@Override114public String toString() {115StringBuilder sb = new StringBuilder();116String method = request().method();117URI uri = request().uri();118String uristring = uri == null ? "" : uri.toString();119sb.append('(')120.append(method)121.append(" ")122.append(uristring)123.append(") ")124.append(statusCode());125sb.append(" ").append(version);126if (localAddress != null)127sb.append(" Local port: ").append(localAddress.getPort());128return sb.toString();129}130}131132133