]> Cypherpunks repositories - gostls13.git/commitdiff
mime: sort attributes in FormatMediaType
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 28 May 2014 15:16:09 +0000 (08:16 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 28 May 2014 15:16:09 +0000 (08:16 -0700)
Map iteration order issue. Go 1.2 and earlier had stable results
for small maps.

Fixes #8115

LGTM=r, rsc
R=golang-codereviews, r
CC=dsymonds, golang-codereviews, iant, rsc
https://golang.org/cl/98580047

src/pkg/mime/mediatype.go
src/pkg/mime/mediatype_test.go

index 608f759da8f315795310291f25fbc9a92c2c5a29..ad63f9bb98e9a4197070f99b82d512b9f3812eb0 100644 (file)
@@ -8,6 +8,7 @@ import (
        "bytes"
        "errors"
        "fmt"
+       "sort"
        "strings"
        "unicode"
 )
@@ -31,7 +32,14 @@ func FormatMediaType(t string, param map[string]string) string {
        b.WriteByte('/')
        b.WriteString(strings.ToLower(sub))
 
-       for attribute, value := range param {
+       attrs := make([]string, 0, len(param))
+       for a := range param {
+               attrs = append(attrs, a)
+       }
+       sort.Strings(attrs)
+
+       for _, attribute := range attrs {
+               value := param[attribute]
                b.WriteByte(';')
                b.WriteByte(' ')
                if !isToken(attribute) {
index 29511445bcffcdf323c5e3e8d7c40e04f0e27391..026bfa4d73468117aeca38445aedaf31122b960d 100644 (file)
@@ -293,6 +293,7 @@ var formatTests = []formatTest{
        {"foo/BAR", map[string]string{"": "empty attribute"}, ""},
        {"foo/BAR", map[string]string{"bad attribute": "baz"}, ""},
        {"foo/BAR", map[string]string{"nonascii": "not an ascii character: รค"}, ""},
+       {"foo/bar", map[string]string{"a": "av", "b": "bv", "c": "cv"}, "foo/bar; a=av; b=bv; c=cv"},
 }
 
 func TestFormatMediaType(t *testing.T) {