Skip to content

Commit

Permalink
/vsicurl/: re-emit HTTP error code next times we try opening a resour…
Browse files Browse the repository at this point in the history
…ce that failed the first time (fixes OSGeo#8922)
  • Loading branch information
rouault committed Dec 6, 2023
1 parent 7a1830a commit 6a90d25
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions autotest/gcore/vsicurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,23 @@ def test_vsicurl_bearer():
gdal.VSIFSeekL(f, 0, 0)
data = gdal.VSIFReadL(1, vsilen, f).decode("ascii")
assert token in data


###############################################################################
# Test https://github.com/OSGeo/gdal/issues/8922


@gdaltest.enable_exceptions()
def test_vsicurl_404_repeated_same_resource(server):

gdal.VSICurlClearCache()

handler = webserver.SequentialHandler()
handler.add("HEAD", "/does/not/exist.bin", 404)
handler.add("GET", "/does/not/", 404)
with webserver.install_http_handler(handler):
with pytest.raises(Exception, match="404"):
gdal.Open("/vsicurl/http://localhost:%d/does/not/exist.bin" % server.port)

with pytest.raises(Exception, match="404"):
gdal.Open("/vsicurl/http://localhost:%d/does/not/exist.bin" % server.port)
13 changes: 13 additions & 0 deletions port/cpl_vsil_curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,7 @@ vsi_l_offset VSICurlHandle::GetFileSizeOrHeaders(bool bSetError,
}

oFileProp.eExists = EXIST_NO;
oFileProp.nHTTPCode = static_cast<int>(response_code);
oFileProp.fileSize = 0;
}
else if (sWriteFuncData.pBuffer != nullptr)
Expand Down Expand Up @@ -1541,6 +1542,18 @@ bool VSICurlHandle::Exists(bool bSetError)
{
GetFileSize(bSetError);
}
else if (oFileProp.eExists == EXIST_NO)
{
// If there was no VSI error thrown in the process,
// and we know the HTTP error code of the first request where the
// file could not be retrieved, fail by reporting the HTTP code.
if (bSetError && VSIGetLastErrorNo() == 0 && oFileProp.nHTTPCode)
{
VSIError(VSIE_HttpError, "HTTP response code: %d",
oFileProp.nHTTPCode);
}
}

return oFileProp.eExists == EXIST_YES;
}

Expand Down
1 change: 1 addition & 0 deletions port/cpl_vsil_curl_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class FileProp
public:
unsigned int nGenerationAuthParameters = 0;
ExistStatus eExists = EXIST_UNKNOWN;
int nHTTPCode = 0;
vsi_l_offset fileSize = 0;
time_t mTime = 0;
time_t nExpireTimestampLocal = 0;
Expand Down

0 comments on commit 6a90d25

Please sign in to comment.