http: build headers when auth challenge fails

This commit is contained in:
Pierre Tachoire
2025-08-26 16:45:07 +02:00
parent 520a572bb4
commit 5defb5c442

View File

@@ -582,12 +582,14 @@ pub const Request = struct {
}; };
pub const AuthChallenge = struct { pub const AuthChallenge = struct {
status: u16,
source: enum { server, proxy }, source: enum { server, proxy },
scheme: enum { basic, digest }, scheme: enum { basic, digest },
realm: []const u8, realm: []const u8,
pub fn parse(header: []const u8) !AuthChallenge { pub fn parse(status: u16, header: []const u8) !AuthChallenge {
var ac: AuthChallenge = .{ var ac: AuthChallenge = .{
.status = status,
.source = undefined, .source = undefined,
.realm = "TODO", // TODO parser and set realm .realm = "TODO", // TODO parser and set realm
.scheme = undefined, .scheme = undefined,
@@ -674,7 +676,11 @@ pub const Transfer = struct {
try errorCheck(c.curl_easy_getinfo(easy, c.CURLINFO_EFFECTIVE_URL, &url)); try errorCheck(c.curl_easy_getinfo(easy, c.CURLINFO_EFFECTIVE_URL, &url));
var status: c_long = undefined; var status: c_long = undefined;
try errorCheck(c.curl_easy_getinfo(easy, c.CURLINFO_RESPONSE_CODE, &status)); if (self._auth_challenge) |_| {
status = 407;
} else {
try errorCheck(c.curl_easy_getinfo(easy, c.CURLINFO_RESPONSE_CODE, &status));
}
self.response_header = .{ self.response_header = .{
.url = url, .url = url,
@@ -871,6 +877,7 @@ pub const Transfer = struct {
// The auth challenge must be parsed from a following // The auth challenge must be parsed from a following
// WWW-Authenticate or Proxy-Authenticate header. // WWW-Authenticate or Proxy-Authenticate header.
transfer._auth_challenge = .{ transfer._auth_challenge = .{
.status = status,
.source = undefined, .source = undefined,
.scheme = undefined, .scheme = undefined,
.realm = undefined, .realm = undefined,
@@ -893,7 +900,10 @@ pub const Transfer = struct {
if (std.ascii.startsWithIgnoreCase(header, "WWW-Authenticate") or if (std.ascii.startsWithIgnoreCase(header, "WWW-Authenticate") or
std.ascii.startsWithIgnoreCase(header, "Proxy-Authenticate")) std.ascii.startsWithIgnoreCase(header, "Proxy-Authenticate"))
{ {
const ac = AuthChallenge.parse(header) catch |err| { const ac = AuthChallenge.parse(
transfer._auth_challenge.?.status,
header,
) catch |err| {
// We can't parse the auth challenge // We can't parse the auth challenge
log.err(.http, "parse auth challenge", .{ .err = err, .header = header }); log.err(.http, "parse auth challenge", .{ .err = err, .header = header });
// Should we cancel the request? I don't think so. // Should we cancel the request? I don't think so.