if (logger.isLoggable(Level.FINEST)){
logger.finest("Request headers set on connection:");
logger.finest(getReqHeaders(connection));
}
HttpResponse response = null;
int responseCode = -1;
try{
connection.setRequestMethod(requestFromBrowser.method().toUpperCase());
if (requestFromBrowser.isPost() || requestFromBrowser.isPut()){
if (logger.isLoggable(Level.FINEST)){
logger.finest("In post requestFromBrowser.data() = " + requestFromBrowser.data());
}
connection.setDoOutput(true);
OutputStream outputStreamToHost = connection.getOutputStream();
outputStreamToHost.write(requestFromBrowser.data());
outputStreamToHost.close();
}
InputStream inputStreamFromHost = null;
responseCode = connection.getResponseCode();
if (logger.isLoggable(Level.FINE)){
logger.fine("responseCode = " + responseCode);
}
if (responseCode < 400){
inputStreamFromHost = connection.getInputStream();
}else{
if (logger.isLoggable(Level.FINE)){
logger.fine("Fetching error stream");
}
inputStreamFromHost = connection.getErrorStream();
}
boolean isGZIP = "gzip".equals(connection.getContentEncoding());
if (logger.isLoggable(Level.FINER)){
logger.finer("isGZIP=" + isGZIP + "; connection.getContentEncoding()=" + connection.getContentEncoding());
}
if (isGZIP){
if (logger.isLoggable(Level.FINE)){
logger.fine("Using GZIPInputStream");
}
try {
inputStreamFromHost = new GZIPInputStream(inputStreamFromHost);
} catch (IOException ioe) {
// happens for redirects etc. where there is no body. Ignore
}
}
if (responseCode >= 500 && !requestFromBrowser.isAjax()){
if (logger.isLoggable(Level.FINE)){
logger.fine("Returning get5xxResponse");
}
response = getWrappedResponse(get5xxResponse(responseCode, inputStreamFromHost));
} else if (responseCode == 401){
if (logger.isLoggable(Level.FINE)){
logger.fine("Returning process401");
}
response = process401(connection, inputStreamFromHost);
}else {
response = getResponse(inputStreamFromHost, connection);
TrafficLogger.storeResponseHeader(response.headers().toString().getBytes(), "unmodified");
TrafficLogger.storeResponseBody(response.data(), "unmodified");
}
if (requestFromBrowser.isAjax() && responseCode > 300 && responseCode < 308){
String redirectedTo = response.getLastSetValueOfHeader("Location");
if (redirectedTo != null) session.addAjaxRedirect(redirectedTo);
}
if (isGZIP) {
response.removeHeader("Content-Encoding", "gzip");
if (response instanceof StreamingHttpResponse) response.setContentLength(-1);
}
}catch(IOException uhe){
if (uhe instanceof SSLHandshakeException)
uhe.printStackTrace();
if (logger.isLoggable(Level.WARNING) && !urlStr.contains("DesignerOutput")){
logger.warning("Returning CannotConnectResponse for: " + urlStr);
logger.fine(Utils.getStackTraceString(uhe));
}
response = getWrappedResponse(getCannotConnectResponse(uhe));
}
if (responseCode != 204 && responseCode != 304){
String contentTypeHeader = response.contentTypeHeader();
boolean downloadContentType = isDownloadContentType(contentTypeHeader);
boolean attachment = response.isAttachment();
String fileName = null;
boolean contentDispositionForcesDownload = false;
if (fileName == null) fileName = requestFromBrowser.fileName();
boolean downloadURL = isDownloadURL(urlStr);
if (logger.isLoggable(Level.FINER)){
logger.finer("downloadURL = " + downloadURL);
logger.finer("response.isAttachment() = " + attachment);
logger.finer("fileName = " + fileName);
logger.finer("contentTypeHeader = " + contentTypeHeader);
logger.finer("downloadContentType = " + downloadContentType);
logger.finer("Content-Disposition=" + response.getLastSetValueOfHeader("Content-Disposition"));
}
if (responseCode == 200
&& (downloadContentType ||
contentDispositionForcesDownload ||
downloadURL)) {
if (logger.isLoggable(Level.INFO)){
StringBuilder sb = new StringBuilder();
sb.append("\n-- Calling downloadFile --\n");
sb.append(requestFromBrowser.url());
sb.append("\ndownloadURL = " + downloadURL);
sb.append("\nresponse.isAttachment() = " + attachment);
sb.append("\nfileName = " + fileName);
sb.append("\ncontentTypeHeader = " + contentTypeHeader);
sb.append("\ndownloadContentType = " + downloadContentType);
sb.append("\nContent-Disposition=" + response.getLastSetValueOfHeader("Content-Disposition"));
sb.append("\ncontentDispositionForcesDownload = " + contentDispositionForcesDownload);
sb.append("\n--");
logger.info(sb.toString());
}
downloadFile(requestFromBrowser, response, fileName);