Skip to content

Instantly share code, notes, and snippets.

@robinsk
Created February 23, 2016 07:32
Show Gist options
  • Save robinsk/a3bb20f56a38df3d6bd8 to your computer and use it in GitHub Desktop.
Save robinsk/a3bb20f56a38df3d6bd8 to your computer and use it in GitHub Desktop.
public synchronized InputStream getInputStream() throws IOException {
this.connecting = true;
SocketPermission var1 = this.URLtoSocketPermission(this.url);
if(var1 != null) {
try {
return (InputStream)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public InputStream run() throws IOException {
return HttpURLConnection.this.getInputStream0();
}
}, (AccessControlContext)null, new Permission[]{var1});
} catch (PrivilegedActionException var3) {
throw (IOException)var3.getException();
}
} else {
return this.getInputStream0();
}
}
private synchronized InputStream getInputStream0() throws IOException {
if(!this.doInput) {
throw new ProtocolException("Cannot read from URLConnection if doInput=false (call setDoInput(true))");
} else if(this.rememberedException != null) {
if(this.rememberedException instanceof RuntimeException) {
throw new RuntimeException(this.rememberedException);
} else {
throw this.getChainedException((IOException)this.rememberedException);
}
} else if(this.inputStream != null) {
return this.inputStream;
} else {
if(this.streaming()) {
if(this.strOutputStream == null) {
this.getOutputStream();
}
this.strOutputStream.close();
if(!this.strOutputStream.writtenOK()) {
throw new IOException("Incomplete output stream");
}
}
int var1 = 0;
boolean var2 = false;
long var3 = -1L;
Object var5 = null;
AuthenticationInfo var6 = null;
AuthenticationHeader var7 = null;
boolean var8 = false;
boolean var9 = false;
this.isUserServerAuth = this.requests.getKey("Authorization") != -1;
this.isUserProxyAuth = this.requests.getKey("Proxy-Authorization") != -1;
InputStream var45;
try {
int var33;
while(true) {
if(!this.checkReuseConnection()) {
this.connect();
}
if(this.cachedInputStream != null) {
InputStream var34 = this.cachedInputStream;
return var34;
}
boolean var10 = ProgressMonitor.getDefault().shouldMeterInput(this.url, this.method);
if(var10) {
this.pi = new ProgressSource(this.url, this.method);
this.pi.beginTracking();
}
this.ps = (PrintStream)this.http.getOutputStream();
if(!this.streaming()) {
this.writeRequests();
}
this.http.parseHTTP(this.responses, this.pi, this);
if(logger.isLoggable(Level.FINE)) {
logger.fine(this.responses.toString());
}
boolean var35 = this.responses.filterNTLMResponses("WWW-Authenticate");
boolean var12 = this.responses.filterNTLMResponses("Proxy-Authenticate");
if((var35 || var12) && logger.isLoggable(Level.FINE)) {
logger.fine(">>>> Headers are filtered");
logger.fine(this.responses.toString());
}
this.inputStream = this.http.getInputStream();
var33 = this.getResponseCode();
if(var33 == -1) {
this.disconnectInternal();
throw new IOException("Invalid Http response");
}
label763: {
boolean var13;
Iterator var14;
String var15;
if(var33 == 407) {
if(this.streaming()) {
this.disconnectInternal();
throw new HttpRetryException("cannot retry due to proxy authentication, in streaming mode", 407);
}
var13 = false;
var14 = this.responses.multiValueIterator("Proxy-Authenticate");
label711: {
do {
if(!var14.hasNext()) {
break label711;
}
var15 = ((String)var14.next()).trim();
} while(!var15.equalsIgnoreCase("Negotiate") && !var15.equalsIgnoreCase("Kerberos"));
if(!var9) {
var9 = true;
} else {
var13 = true;
this.doingNTLMp2ndStage = false;
var6 = null;
}
}
AuthenticationHeader var39 = new AuthenticationHeader("Proxy-Authenticate", this.responses, new HttpCallerInfo(this.url, this.http.getProxyHostUsed(), this.http.getProxyPortUsed()), var13);
if(this.doingNTLMp2ndStage) {
String var16 = this.responses.findValue("Proxy-Authenticate");
this.reset();
if(!var6.setHeaders(this, var39.headerParser(), var16)) {
this.disconnectInternal();
throw new IOException("Authentication failure");
}
if(var5 != null && var7 != null && !((AuthenticationInfo)var5).setHeaders(this, var7.headerParser(), var16)) {
this.disconnectInternal();
throw new IOException("Authentication failure");
}
this.authObj = null;
this.doingNTLMp2ndStage = false;
break label763;
}
var6 = this.resetProxyAuthentication(var6, var39);
if(var6 != null) {
++var1;
this.disconnectInternal();
break label763;
}
} else {
var9 = false;
this.doingNTLMp2ndStage = false;
if(!this.isUserProxyAuth) {
this.requests.remove("Proxy-Authorization");
}
}
if(var6 != null) {
var6.addToCache();
}
if(var33 == 401) {
if(this.streaming()) {
this.disconnectInternal();
throw new HttpRetryException("cannot retry due to server authentication, in streaming mode", 401);
}
var13 = false;
var14 = this.responses.multiValueIterator("WWW-Authenticate");
label693: {
do {
if(!var14.hasNext()) {
break label693;
}
var15 = ((String)var14.next()).trim();
} while(!var15.equalsIgnoreCase("Negotiate") && !var15.equalsIgnoreCase("Kerberos"));
if(!var8) {
var8 = true;
} else {
var13 = true;
this.doingNTLM2ndStage = false;
var5 = null;
}
}
var7 = new AuthenticationHeader("WWW-Authenticate", this.responses, new HttpCallerInfo(this.url), var13);
var15 = var7.raw();
if(this.doingNTLM2ndStage) {
this.reset();
if(!((AuthenticationInfo)var5).setHeaders(this, (HeaderParser)null, var15)) {
this.disconnectWeb();
throw new IOException("Authentication failure");
}
this.doingNTLM2ndStage = false;
this.authObj = null;
this.setCookieHeader();
break label763;
}
if(var5 != null && ((AuthenticationInfo)var5).getAuthScheme() != AuthScheme.NTLM) {
if(((AuthenticationInfo)var5).isAuthorizationStale(var15)) {
this.disconnectWeb();
++var1;
this.requests.set(((AuthenticationInfo)var5).getHeaderName(), ((AuthenticationInfo)var5).getHeaderValue(this.url, this.method));
this.currentServerCredentials = (AuthenticationInfo)var5;
this.setCookieHeader();
break label763;
}
((AuthenticationInfo)var5).removeFromCache();
}
var5 = this.getServerAuthentication(var7);
this.currentServerCredentials = (AuthenticationInfo)var5;
if(var5 != null) {
this.disconnectWeb();
++var1;
this.setCookieHeader();
break label763;
}
}
if(var5 != null) {
if(var5 instanceof DigestAuthentication && this.domain != null) {
DigestAuthentication var41 = (DigestAuthentication)var5;
StringTokenizer var37 = new StringTokenizer(this.domain, " ");
var15 = var41.realm;
PasswordAuthentication var42 = var41.pw;
this.digestparams = var41.params;
while(var37.hasMoreTokens()) {
String var17 = var37.nextToken();
try {
URL var18 = new URL(this.url, var17);
DigestAuthentication var19 = new DigestAuthentication(false, var18, var15, "Digest", var42, this.digestparams);
var19.addToCache();
} catch (Exception var29) {
;
}
}
} else {
if(var5 instanceof BasicAuthentication) {
String var38 = AuthenticationInfo.reducePath(this.url.getPath());
String var36 = ((AuthenticationInfo)var5).path;
if(!var36.startsWith(var38) || var38.length() >= var36.length()) {
var38 = BasicAuthentication.getRootPath(var36, var38);
}
BasicAuthentication var43 = (BasicAuthentication)((AuthenticationInfo)var5).clone();
((AuthenticationInfo)var5).removeFromCache();
var43.path = var38;
var5 = var43;
}
((AuthenticationInfo)var5).addToCache();
}
}
var8 = false;
var9 = false;
this.doingNTLMp2ndStage = false;
this.doingNTLM2ndStage = false;
if(!this.isUserServerAuth) {
this.requests.remove("Authorization");
}
if(!this.isUserProxyAuth) {
this.requests.remove("Proxy-Authorization");
}
if(var33 == 200) {
this.checkResponseCredentials(false);
} else {
this.needToCheck = false;
}
this.needToCheck = true;
if(!this.followRedirect()) {
try {
var3 = Long.parseLong(this.responses.findValue("content-length"));
} catch (Exception var28) {
;
}
break;
}
++var1;
this.setCookieHeader();
}
if(var1 >= maxRedirects) {
throw new ProtocolException("Server redirected too many times (" + var1 + ")");
}
}
if(this.method.equals("HEAD") || var3 == 0L || var33 == 304 || var33 == 204) {
if(this.pi != null) {
this.pi.finishTracking();
this.pi = null;
}
this.http.finished();
this.http = null;
this.inputStream = new EmptyInputStream();
this.connected = false;
}
if((var33 == 200 || var33 == 203 || var33 == 206 || var33 == 300 || var33 == 301 || var33 == 410) && this.cacheHandler != null && this.getUseCaches()) {
URI var44 = ParseUtil.toURI(this.url);
if(var44 != null) {
Object var40 = this;
if("https".equalsIgnoreCase(var44.getScheme())) {
try {
var40 = (URLConnection)this.getClass().getField("httpsURLConnection").get(this);
} catch (NoSuchFieldException | IllegalAccessException var27) {
;
}
}
CacheRequest var46 = this.cacheHandler.put(var44, (URLConnection)var40);
if(var46 != null && this.http != null) {
this.http.setCacheRequest(var46);
this.inputStream = new HttpURLConnection.HttpInputStream(this.inputStream, var46);
}
}
}
if(!(this.inputStream instanceof HttpURLConnection.HttpInputStream)) {
this.inputStream = new HttpURLConnection.HttpInputStream(this.inputStream);
}
if(var33 >= 400) {
if(var33 != 404 && var33 != 410) {
throw new IOException("Server returned HTTP response code: " + var33 + " for URL: " + this.url.toString());
}
throw new FileNotFoundException(this.url.toString());
}
this.poster = null;
this.strOutputStream = null;
var45 = this.inputStream;
} catch (RuntimeException var30) {
this.disconnectInternal();
this.rememberedException = var30;
throw var30;
} catch (IOException var31) {
this.rememberedException = var31;
String var11 = this.responses.findValue("Transfer-Encoding");
if(this.http != null && this.http.isKeepingAlive() && enableESBuffer && (var3 > 0L || var11 != null && var11.equalsIgnoreCase("chunked"))) {
this.errorStream = HttpURLConnection.ErrorStream.getErrorStream(this.inputStream, var3, this.http);
}
throw var31;
} finally {
if(this.proxyAuthKey != null) {
AuthenticationInfo.endAuthRequest(this.proxyAuthKey);
}
if(this.serverAuthKey != null) {
AuthenticationInfo.endAuthRequest(this.serverAuthKey);
}
}
return var45;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment