fix issue in index folder feature for ghttp.Server

This commit is contained in:
John
2019-09-25 10:56:44 +08:00
parent b0f859cc91
commit cb7c3a9fa4
3 changed files with 14 additions and 4 deletions

View File

@ -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>`)

View File

@ -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")
})

View File

@ -1,4 +1,4 @@
package gf
const VERSION = "v1.9.3"
const VERSION = "v1.9.4"
const AUTHORS = "john<john@goframe.org>"