Skip to content

Commit

Permalink
/vsicurl?: accept header.<key>=<value> to specify HTTP request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Dec 17, 2024
1 parent 9e46957 commit 635d654
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions autotest/gcore/vsicurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,3 +1609,27 @@ def test_vsicurl_VSICURL_QUERY_STRING(server, filename, query_string):
assert statres.size == 3
finally:
gdal.SetPathSpecificOption(full_filename, "VSICURL_QUERY_STRING", None)


###############################################################################
# Test /vsicurl?header:foo=bar&


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

gdal.VSICurlClearCache()

handler = webserver.SequentialHandler()
handler.add(
"HEAD",
"/test_vsicurl_header_option.bin",
200,
{"Content-Length": "3"},
expected_headers={"foo": "bar", "Accept": "application/json"},
)

with webserver.install_http_handler(handler):
full_filename = f"/vsicurl?header.foo=bar&header.Accept=application%2Fjson&url=http%3A%2F%2Flocalhost%3A{server.port}%2Ftest_vsicurl_header_option.bin"
statres = gdal.VSIStatL(full_filename)
assert statres.size == 3
1 change: 1 addition & 0 deletions doc/source/user/virtual_file_systems.rst
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ Starting with GDAL 2.3, options can be passed in the filename with the following
- referer=value: HTTP Referer header
- cookie=value: HTTP Cookie header
- header_file=value: Filename that contains one or several "Header: Value" lines
- header.<key>=<value>: HTTP request header of name <key> and value <value>. (GDAL >= 3.11). e.g. ``header.Accept=application%2Fjson``
- unsafessl=yes/no
- low_speed_time=value
- low_speed_limit=value
Expand Down
11 changes: 11 additions & 0 deletions port/cpl_vsil_curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ static std::string VSICurlGetURLFromFilename(
}

std::string osURL;
std::string osHeaders;
for (int i = 0; papszTokens[i]; i++)
{
char *pszKey = nullptr;
Expand Down Expand Up @@ -433,6 +434,13 @@ static std::string VSICurlGetURLFromFilename(
*ppszPlanetaryComputerCollection = CPLStrdup(pszValue);
}
}
else if (STARTS_WITH(pszKey, "header."))
{
osHeaders += (pszKey + strlen("header."));
osHeaders += ':';
osHeaders += pszValue;
osHeaders += "\r\n";
}
else
{
CPLError(CE_Warning, CPLE_NotSupported,
Expand All @@ -442,6 +450,9 @@ static std::string VSICurlGetURLFromFilename(
CPLFree(pszKey);
}

if (paosHTTPOptions && !osHeaders.empty())
paosHTTPOptions->SetNameValue("HEADERS", osHeaders.c_str());

CSLDestroy(papszTokens);
if (osURL.empty())
{
Expand Down

0 comments on commit 635d654

Please sign in to comment.