@@ -30,6 +30,7 @@ import (
30
30
31
31
"github.com/gohugoio/httpcache"
32
32
"github.com/gohugoio/hugo/common/hashing"
33
+ "github.com/gohugoio/hugo/common/hstrings"
33
34
"github.com/gohugoio/hugo/common/hugio"
34
35
"github.com/gohugoio/hugo/common/loggers"
35
36
"github.com/gohugoio/hugo/common/maps"
@@ -51,18 +52,28 @@ type HTTPError struct {
51
52
Body string
52
53
}
53
54
54
- func responseToData (res * http.Response , readBody bool ) map [string ]any {
55
+ func responseToData (res * http.Response , readBody bool , includeHeaders [] string ) map [string ]any {
55
56
var body []byte
56
57
if readBody {
57
58
body , _ = io .ReadAll (res .Body )
58
59
}
59
60
61
+ responseHeaders := make (map [string ][]string )
62
+ if true || len (includeHeaders ) > 0 {
63
+ for k , v := range res .Header {
64
+ if hstrings .InSlicEqualFold (includeHeaders , k ) {
65
+ responseHeaders [k ] = v
66
+ }
67
+ }
68
+ }
69
+
60
70
m := map [string ]any {
61
71
"StatusCode" : res .StatusCode ,
62
72
"Status" : res .Status ,
63
73
"TransferEncoding" : res .TransferEncoding ,
64
74
"ContentLength" : res .ContentLength ,
65
75
"ContentType" : res .Header .Get ("Content-Type" ),
76
+ "Headers" : responseHeaders ,
66
77
}
67
78
68
79
if readBody {
@@ -72,7 +83,7 @@ func responseToData(res *http.Response, readBody bool) map[string]any {
72
83
return m
73
84
}
74
85
75
- func toHTTPError (err error , res * http.Response , readBody bool ) * HTTPError {
86
+ func toHTTPError (err error , res * http.Response , readBody bool , responseHeaders [] string ) * HTTPError {
76
87
if err == nil {
77
88
panic ("err is nil" )
78
89
}
@@ -85,7 +96,7 @@ func toHTTPError(err error, res *http.Response, readBody bool) *HTTPError {
85
96
86
97
return & HTTPError {
87
98
error : err ,
88
- Data : responseToData (res , readBody ),
99
+ Data : responseToData (res , readBody , responseHeaders ),
89
100
}
90
101
}
91
102
@@ -213,7 +224,7 @@ func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resou
213
224
}
214
225
215
226
if res .StatusCode < 200 || res .StatusCode > 299 {
216
- return nil , toHTTPError (fmt .Errorf ("failed to fetch remote resource from '%s': %s" , uri , http .StatusText (res .StatusCode )), res , ! isHeadMethod )
227
+ return nil , toHTTPError (fmt .Errorf ("failed to fetch remote resource from '%s': %s" , uri , http .StatusText (res .StatusCode )), res , ! isHeadMethod , options . ResponseHeaders )
217
228
}
218
229
219
230
var (
@@ -280,7 +291,7 @@ func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resou
280
291
}
281
292
282
293
userKey = filename [:len (filename )- len (path .Ext (filename ))] + "_" + userKey + mediaType .FirstSuffix .FullSuffix
283
- data := responseToData (res , false )
294
+ data := responseToData (res , false , options . ResponseHeaders )
284
295
285
296
return c .rs .NewResource (
286
297
resources.ResourceSourceDescriptor {
@@ -345,9 +356,10 @@ func hasHeaderKey(m http.Header, key string) bool {
345
356
}
346
357
347
358
type fromRemoteOptions struct {
348
- Method string
349
- Headers map [string ]any
350
- Body []byte
359
+ Method string
360
+ Headers map [string ]any
361
+ Body []byte
362
+ ResponseHeaders []string
351
363
}
352
364
353
365
func (o fromRemoteOptions ) BodyReader () io.Reader {
@@ -432,7 +444,7 @@ func (t *transport) RoundTrip(req *http.Request) (resp *http.Response, err error
432
444
if resp != nil {
433
445
msg = resp .Status
434
446
}
435
- err := toHTTPError (fmt .Errorf ("retry timeout (configured to %s) fetching remote resource: %s" , t .Cfg .Timeout (), msg ), resp , req .Method != "HEAD" )
447
+ err := toHTTPError (fmt .Errorf ("retry timeout (configured to %s) fetching remote resource: %s" , t .Cfg .Timeout (), msg ), resp , req .Method != "HEAD" , nil )
436
448
return resp , err
437
449
}
438
450
time .Sleep (nextSleep )
0 commit comments