11/*
2- * Copyright (c) 1998-2019 University Corporation for Atmospheric Research/Unidata
2+ * Copyright (c) 1998-2025 University Corporation for Atmospheric Research/Unidata
33 * See LICENSE for license information.
44 */
5+
56package ucar .unidata .io .http ;
67
78import java .io .FileNotFoundException ;
@@ -40,6 +41,9 @@ public final class HTTPRandomAccessFile extends RemoteRandomAccessFile {
4041 private static final long httpMaxCacheSize = Long
4142 .parseLong (System .getProperty ("ucar.unidata.io.http.maxReadCacheSize" , String .valueOf (defaultMaxReadCacheSize )));
4243
44+ private static final String HTTPS = "https" ;
45+ private static final String HTTP = "http" ;
46+
4347 private static final boolean debug = false , debugDetails = false ;
4448
4549 ///////////////////////////////////////////////////////////////////////////////////
@@ -234,7 +238,7 @@ public long getLastModified() {
234238 */
235239 public static class Provider implements RandomAccessFileProvider {
236240
237- private static final List <String > possibleSchemes = Arrays .asList ("http" , "https" , "nodods" , "httpserver" );
241+ private static final List <String > possibleSchemes = Arrays .asList (HTTP , HTTPS , "nodods" , "httpserver" );
238242
239243 @ Override
240244 public boolean isOwnerOf (String location ) {
@@ -250,10 +254,26 @@ public RandomAccessFile open(String location) throws IOException {
250254 @ Override
251255 public RandomAccessFile open (String location , int bufferSize ) throws IOException {
252256 String scheme = location .split (":" )[0 ];
253- if (!scheme .equalsIgnoreCase ("https" ) && !scheme .equalsIgnoreCase ("http" )) {
254- location = location .replace (scheme , "http" );
257+ boolean fallback = false ;
258+ if (!scheme .equalsIgnoreCase (HTTPS ) && !scheme .equalsIgnoreCase (HTTP )) {
259+ location = location .replaceFirst (scheme , HTTPS );
260+ fallback = true ;
261+ }
262+ HTTPRandomAccessFile raf ;
263+ try {
264+ // try with https first
265+ raf = new HTTPRandomAccessFile (location , bufferSize , httpMaxCacheSize );
266+ } catch (IOException e ) {
267+ if (fallback ) {
268+ // if we had to guess the scheme, fallback to http, and if it does not work, let the IOError be thrown
269+ raf = new HTTPRandomAccessFile (location .replaceFirst (HTTPS , HTTP ), bufferSize , httpMaxCacheSize );
270+ } else {
271+ // didn't try to fallback to http, just throw the original error
272+ throw e ;
273+ }
255274 }
256- return new HTTPRandomAccessFile (location , bufferSize , httpMaxCacheSize );
275+
276+ return raf ;
257277 }
258278 }
259279}
0 commit comments