mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
fix issue in index folder feature for ghttp.Server
This commit is contained in:
@ -12,6 +12,8 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
|
||||
"github.com/gogf/gf/os/gres"
|
||||
@ -276,7 +278,11 @@ func (s *Server) listDir(r *Request, f http.File) {
|
||||
r.Response.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
}
|
||||
r.Response.Write(`<html>`)
|
||||
r.Response.Write(`<head></head>`)
|
||||
r.Response.Write(`<head>`)
|
||||
r.Response.Write(`<style>`)
|
||||
r.Response.Write(`body {font-family:Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;}`)
|
||||
r.Response.Write(`</style>`)
|
||||
r.Response.Write(`</head>`)
|
||||
r.Response.Write(`<body>`)
|
||||
r.Response.Writef(`<h1>Index of %s</h1>`, r.URL.Path)
|
||||
r.Response.Writef(`<hr />`)
|
||||
@ -288,6 +294,7 @@ func (s *Server) listDir(r *Request, f http.File) {
|
||||
}
|
||||
name := ""
|
||||
size := ""
|
||||
prefix := gstr.TrimRight(r.URL.Path, "/")
|
||||
for _, file := range files {
|
||||
name = file.Name()
|
||||
size = gfile.FormatSize(file.Size())
|
||||
@ -296,9 +303,9 @@ func (s *Server) listDir(r *Request, f http.File) {
|
||||
size = "-"
|
||||
}
|
||||
r.Response.Write(`<tr>`)
|
||||
r.Response.Writef(`<td><a href="%s/%s">%s</a></td>`, r.URL.Path, name, ghtml.SpecialChars(name))
|
||||
r.Response.Writef(`<td><a href="%s/%s">%s</a></td>`, prefix, name, ghtml.SpecialChars(name))
|
||||
r.Response.Writef(`<td style="width:300px;text-align:center;">%s</td>`, gtime.New(file.ModTime()).ISO8601())
|
||||
r.Response.Writef(`<td style="width:80px;text-align:center;">%s</td>`, size)
|
||||
r.Response.Writef(`<td style="width:80px;text-align:right;">%s</td>`, size)
|
||||
r.Response.Write(`</tr>`)
|
||||
}
|
||||
r.Response.Write(`</table>`)
|
||||
|
||||
@ -12,6 +12,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gfile"
|
||||
@ -96,6 +98,7 @@ func Test_Static_IndexFolder(t *testing.T) {
|
||||
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
||||
|
||||
gtest.AssertNE(client.GetContent("/"), "Forbidden")
|
||||
gtest.AssertNE(gstr.Pos(client.GetContent("/"), `<a href="/test.html"`), -1)
|
||||
gtest.Assert(client.GetContent("/index.html"), "Not Found")
|
||||
gtest.Assert(client.GetContent("/test.html"), "test")
|
||||
})
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package gf
|
||||
|
||||
const VERSION = "v1.9.3"
|
||||
const VERSION = "v1.9.4"
|
||||
const AUTHORS = "john<john@goframe.org>"
|
||||
|
||||
Reference in New Issue
Block a user