Treat 404 from a URL as missing file, not fatal error

This commit is contained in:
Pavel Yakunin 2014-12-28 18:15:44 +03:00 committed by Havoc Pennington
parent f3e11bc583
commit 7f19e46d1c

View File

@ -463,32 +463,46 @@ public abstract class Parseable implements ConfigParseable {
@Override
protected Reader reader(ConfigParseOptions options) throws IOException {
if (ConfigImpl.traceLoadsEnabled())
trace("Loading config from a URL: " + input.toExternalForm());
URLConnection connection = input.openConnection();
// allow server to serve multiple types from one URL
String acceptContent = acceptContentType(options);
if (acceptContent != null) {
connection.setRequestProperty("Accept", acceptContent);
}
connection.connect();
// save content type for later
contentType = connection.getContentType();
if (contentType != null) {
try {
if (ConfigImpl.traceLoadsEnabled())
trace("URL sets Content-Type: '" + contentType + "'");
contentType = contentType.trim();
int semi = contentType.indexOf(';');
if (semi >= 0)
contentType = contentType.substring(0, semi);
trace("Loading config from a URL: " + input.toExternalForm());
URLConnection connection = input.openConnection();
// allow server to serve multiple types from one URL
String acceptContent = acceptContentType(options);
if (acceptContent != null) {
connection.setRequestProperty("Accept", acceptContent);
}
connection.connect();
// save content type for later
contentType = connection.getContentType();
if (contentType != null) {
if (ConfigImpl.traceLoadsEnabled())
trace("URL sets Content-Type: '" + contentType + "'");
contentType = contentType.trim();
int semi = contentType.indexOf(';');
if (semi >= 0)
contentType = contentType.substring(0, semi);
}
InputStream stream = connection.getInputStream();
return readerFromStream(stream);
} catch (FileNotFoundException fnf) {
// If the resource is not found (HTTP response
// code 404 or something alike), then it's fine to
// treat it according to the allowMissing setting
// and "include" spec. But if we have something
// like HTTP 503 it seems to be better to fail
// early, because this may be a sign of broken
// environment. Java throws FileNotFoundException
// if it sees 404 or 410.
throw fnf;
} catch (IOException e) {
throw new ConfigException.BugOrBroken("Cannot load config from URL: " + input.toExternalForm(), e);
}
InputStream stream = connection.getInputStream();
return readerFromStream(stream);
}
@Override