Skip to content

Commit

Permalink
Merge pull request #10661 from rouault/pathspecificoption_streaming
Browse files Browse the repository at this point in the history
CPLHTTPGetOptionsFromEnv(): fallback to non-streaming filename with no path specific option has been found on the streaming one
  • Loading branch information
rouault committed Sep 7, 2024
2 parents ec02d42 + e4df9be commit 5f3eb89
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 49 deletions.
41 changes: 32 additions & 9 deletions autotest/gcore/vsicurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,15 +1139,38 @@ def test_vsicurl_GDAL_HTTP_HEADERS(server):
filename = (
"/vsicurl/http://localhost:%d/test_vsicurl_GDAL_HTTP_HEADERS.bin" % server.port
)
gdal.SetPathSpecificOption(
filename,
"GDAL_HTTP_HEADERS",
r'Foo: Bar,"Baz: escaped backslash \\, escaped double-quote \", end of value",Another: Header',
)
with webserver.install_http_handler(handler):
statres = gdal.VSIStatL(filename)
gdal.SetPathSpecificOption(filename, "GDAL_HTTP_HEADERS", None)
assert statres.size == 3
try:
gdal.SetPathSpecificOption(
filename,
"GDAL_HTTP_HEADERS",
r'Foo: Bar,"Baz: escaped backslash \\, escaped double-quote \", end of value",Another: Header',
)
with webserver.install_http_handler(handler):
statres = gdal.VSIStatL(filename)
assert statres.size == 3

gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add(
"HEAD",
"/test_vsicurl_GDAL_HTTP_HEADERS.bin",
200,
{"Content-Length": "3"},
expected_headers={
"Foo": "Bar",
"Baz": r'escaped backslash \, escaped double-quote ", end of value',
"Another": "Header",
},
)
with webserver.install_http_handler(handler):
statres = gdal.VSIStatL(
"/vsicurl_streaming/http://localhost:%d/test_vsicurl_GDAL_HTTP_HEADERS.bin"
% server.port
)
assert statres.size == 3

finally:
gdal.SetPathSpecificOption(filename, "GDAL_HTTP_HEADERS", None)


###############################################################################
Expand Down
38 changes: 33 additions & 5 deletions port/cpl_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "cpl_http.h"
#include "cpl_error.h"
#include "cpl_multiproc.h"
#include "cpl_vsi_virtual.h"
#include "cpl_vsil_curl_class.h"

// gcc or clang complains about C-style cast in #define like
Expand Down Expand Up @@ -554,13 +555,40 @@ constexpr TupleEnvVarOptionName asAssocEnvVarOptionName[] = {
char **CPLHTTPGetOptionsFromEnv(const char *pszFilename)
{
CPLStringList aosOptions;
std::string osNonStreamingFilename;
if (STARTS_WITH(pszFilename, "/vsi"))
{
VSIFilesystemHandler *poFSHandler =
VSIFileManager::GetHandler(pszFilename);
osNonStreamingFilename =
poFSHandler->GetNonStreamingFilename(pszFilename);
if (osNonStreamingFilename == pszFilename)
{
osNonStreamingFilename.clear();
}
else
{
// CPLDebug("HTTP", "Non-streaming filename for %s: %s", pszFilename, osNonStreamingFilename.c_str());
}
}
for (const auto &sTuple : asAssocEnvVarOptionName)
{
const char *pszVal =
pszFilename ? VSIGetPathSpecificOption(pszFilename,
sTuple.pszEnvVar, nullptr)
: CPLGetConfigOption(sTuple.pszEnvVar, nullptr);
if (pszVal != nullptr)
const char *pszVal = nullptr;
if (pszFilename)
{
pszVal = VSIGetPathSpecificOption(pszFilename, sTuple.pszEnvVar,
nullptr);
if (!pszVal && !osNonStreamingFilename.empty())
{
pszVal = VSIGetPathSpecificOption(
osNonStreamingFilename.c_str(), sTuple.pszEnvVar, nullptr);
}
}
if (!pszVal)
{
pszVal = CPLGetConfigOption(sTuple.pszEnvVar, nullptr);
}
if (pszVal)
{
aosOptions.AddNameValue(sTuple.pszOptionName, pszVal);
}
Expand Down
6 changes: 6 additions & 0 deletions port/cpl_vsi_virtual.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ class CPL_DLL VSIFilesystemHandler
return osFilename;
}

virtual std::string
GetNonStreamingFilename(const std::string &osFilename) const
{
return osFilename;
}

/** Return the canonical filename.
*
* May be implemented by case-insensitive filesystems
Expand Down
Loading

0 comments on commit 5f3eb89

Please sign in to comment.