@@ -17,6 +17,7 @@ import (
17
17
"os"
18
18
"path"
19
19
"path/filepath"
20
+ "sort"
20
21
"strconv"
21
22
"strings"
22
23
"time"
@@ -68,24 +69,28 @@ type File interface {
68
69
}
69
70
70
71
func dirList (w ResponseWriter , f File ) {
72
+ dirs , err := f .Readdir (- 1 )
73
+ if err != nil {
74
+ // TODO: log err.Error() to the Server.ErrorLog, once it's possible
75
+ // for a handler to get at its Server via the ResponseWriter. See
76
+ // Issue 12438.
77
+ Error (w , "Error reading directory" , StatusInternalServerError )
78
+ return
79
+ }
80
+ sort .Sort (byName (dirs ))
81
+
71
82
w .Header ().Set ("Content-Type" , "text/html; charset=utf-8" )
72
83
fmt .Fprintf (w , "<pre>\n " )
73
- for {
74
- dirs , err := f .Readdir (100 )
75
- if err != nil || len (dirs ) == 0 {
76
- break
77
- }
78
- for _ , d := range dirs {
79
- name := d .Name ()
80
- if d .IsDir () {
81
- name += "/"
82
- }
83
- // name may contain '?' or '#', which must be escaped to remain
84
- // part of the URL path, and not indicate the start of a query
85
- // string or fragment.
86
- url := url.URL {Path : name }
87
- fmt .Fprintf (w , "<a href=\" %s\" >%s</a>\n " , url .String (), htmlReplacer .Replace (name ))
84
+ for _ , d := range dirs {
85
+ name := d .Name ()
86
+ if d .IsDir () {
87
+ name += "/"
88
88
}
89
+ // name may contain '?' or '#', which must be escaped to remain
90
+ // part of the URL path, and not indicate the start of a query
91
+ // string or fragment.
92
+ url := url.URL {Path : name }
93
+ fmt .Fprintf (w , "<a href=\" %s\" >%s</a>\n " , url .String (), htmlReplacer .Replace (name ))
89
94
}
90
95
fmt .Fprintf (w , "</pre>\n " )
91
96
}
@@ -585,3 +590,9 @@ func sumRangesSize(ranges []httpRange) (size int64) {
585
590
}
586
591
return
587
592
}
593
+
594
+ type byName []os.FileInfo
595
+
596
+ func (s byName ) Len () int { return len (s ) }
597
+ func (s byName ) Less (i , j int ) bool { return s [i ].Name () < s [j ].Name () }
598
+ func (s byName ) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
0 commit comments