public void received(HttpResponse response) {
try {
if (levelOfRedirect < maxRedirectLevels) {
String location = response.getHeaders().get(Http.LOCATION);
if (location != null) {
Address newAddress = request.getAddress();
String newPath;
String l = null;
boolean secure = false;
int defaultPort = Http.DEFAULT_PORT;
if (location.startsWith(Http.SECURE_PROTOCOL)) {
l = location.substring(Http.SECURE_PROTOCOL.length());
secure = true;
defaultPort = Http.DEFAULT_SECURE_PORT;
} else if (location.startsWith(Http.PROTOCOL)) {
l = location.substring(Http.PROTOCOL.length());
}
if (l != null) {
int i = l.indexOf(Http.PATH_SEPARATOR);
if (i > 0) {
newPath = l.substring(i);
l = l.substring(0, i);
} else {
newPath = String.valueOf(Http.PATH_SEPARATOR);
}
int j = l.indexOf(Http.PORT_SEPARATOR);
if (j < 0) {
newAddress = new Address(l, defaultPort);
} else {
int newPort;
String portValue = l.substring(j + 1);
if (portValue.isEmpty()) {
newPort = defaultPort;
} else {
try {
newPort = Integer.parseInt(portValue);
} catch (NumberFormatException e) {
throw new IOException("Bad location: " + location);
}
}
newAddress = new Address(l.substring(0, j), newPort);
}
} else {
newPath = location;
}