Skip to content

Commit 14d7d61

Browse files
authored
fix: parse the mime metadata when do get/head object (#2106)
1 parent 258935a commit 14d7d61

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"hash"
3131
"io"
3232
"math/rand"
33+
"mime"
3334
"net"
3435
"net/http"
3536
"net/url"
@@ -210,6 +211,7 @@ func extractObjMetadata(header http.Header) http.Header {
210211
"X-Amz-Server-Side-Encryption",
211212
"X-Amz-Tagging-Count",
212213
"X-Amz-Meta-",
214+
"X-Minio-Meta-",
213215
// Add new headers to be preserved.
214216
// if you add new headers here, please extend
215217
// PutObjectOptions{} to preserve them
@@ -223,6 +225,16 @@ func extractObjMetadata(header http.Header) http.Header {
223225
continue
224226
}
225227
found = true
228+
if prefix == "X-Amz-Meta-" || prefix == "X-Minio-Meta-" {
229+
for index, val := range v {
230+
if strings.HasPrefix(val, "=?") {
231+
decoder := mime.WordDecoder{}
232+
if decoded, err := decoder.DecodeHeader(val); err == nil {
233+
v[index] = decoded
234+
}
235+
}
236+
}
237+
}
226238
break
227239
}
228240
if found {

utils_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import (
2121
"errors"
2222
"fmt"
2323
"math/rand"
24+
"mime"
25+
"net/http"
2426
"net/url"
27+
"reflect"
28+
"strings"
2529
"testing"
2630
"time"
2731

@@ -467,3 +471,74 @@ func TestFullObjectChecksum64(t *testing.T) {
467471
})
468472
}
469473
}
474+
475+
func TestExtractObjMetadata(t *testing.T) {
476+
tests := []struct {
477+
name string
478+
header http.Header
479+
want http.Header
480+
}{
481+
{
482+
name: "Test with valid header",
483+
header: http.Header{
484+
"X-Minio-Meta-Test": []string{"test"},
485+
},
486+
want: http.Header{
487+
"X-Minio-Meta-Test": []string{"test"},
488+
},
489+
},
490+
{
491+
name: "Test with valid header with QEncoding characters",
492+
header: http.Header{
493+
"X-Minio-Meta-Test": []string{mime.QEncoding.Encode("UTF-8", "öha, das")},
494+
},
495+
want: http.Header{
496+
"X-Minio-Meta-Test": []string{"öha, das"},
497+
},
498+
},
499+
{
500+
name: "Test with valid header with BEncoding characters",
501+
header: http.Header{
502+
"X-Minio-Meta-Test": []string{mime.BEncoding.Encode("UTF-8", "öha, das")},
503+
},
504+
want: http.Header{
505+
"X-Minio-Meta-Test": []string{"öha, das"},
506+
},
507+
},
508+
{
509+
name: "Test with valid header with multi-QEncoding characters",
510+
header: http.Header{
511+
"X-Minio-Meta-Test": []string{mime.QEncoding.Encode("UTF-8", strings.Repeat("öha, das", 100))},
512+
},
513+
want: http.Header{
514+
"X-Minio-Meta-Test": []string{strings.Repeat("öha, das", 100)},
515+
},
516+
},
517+
{
518+
name: "Test with valid header with multi-BEncoding characters",
519+
header: http.Header{
520+
"X-Minio-Meta-Test": []string{mime.BEncoding.Encode("UTF-8", strings.Repeat("öha, das", 100))},
521+
},
522+
want: http.Header{
523+
"X-Minio-Meta-Test": []string{strings.Repeat("öha, das", 100)},
524+
},
525+
},
526+
{
527+
name: "Test with valid header with multi-BEncoding characters",
528+
header: http.Header{
529+
"X-Minio-Meta-Test": []string{mime.BEncoding.Encode("UTF-8", strings.Repeat("öha, das", 100)), mime.BEncoding.Encode("UTF-8", strings.Repeat("öha, das123", 100))},
530+
},
531+
want: http.Header{
532+
"X-Minio-Meta-Test": []string{strings.Repeat("öha, das", 100), strings.Repeat("öha, das123", 100)},
533+
},
534+
},
535+
}
536+
for _, tt := range tests {
537+
t.Run(tt.name, func(t *testing.T) {
538+
got := extractObjMetadata(tt.header)
539+
if !reflect.DeepEqual(got, tt.want) {
540+
t.Errorf("extractObjMetadata() = %v, want %v", got, tt.want)
541+
}
542+
})
543+
}
544+
}

0 commit comments

Comments
 (0)