small fixes

This commit is contained in:
Altair-sh
2026-11-03 11:51:54 +01:00
parent 86dce6584b
commit 793cbcc37f
6 changed files with 34 additions and 16 deletions
+2
View File
@@ -38,6 +38,8 @@ func (conn *Connection) CreateResponse(
Status: status,
Headers: HeadersMap{
"date": time.Now().UTC().Format(TimeFormat),
// without it browser may remember response and not resend it
"cache-control": "no-store",
},
}
+1
View File
@@ -18,6 +18,7 @@ type Shares []*SharedLocation
type Config struct {
Addr string `toml:"address"`
MaxUpload int64 `toml:"max_upload"`
HtmlTitle string `toml:"html_title"`
// sorted by Url in reverse to allow this pattern:
// shared/private
// shared/
+11 -7
View File
@@ -56,25 +56,29 @@ func (sh *SharedLocation) normalize(configDir string) error {
func (sh *SharedLocation) String() string {
var r, w string
if sh.ReadPassword == "" {
r = "public"
r = "pub"
} else {
r = "password"
r = "pas"
}
if sh.WritePassword == "" {
w = "no"
w = "no "
} else {
w = "password"
w = "pas"
}
access := "{ " + "r:" + r + ", w:" + w + " }"
url := sh.Url
if url == "" {
url = "/"
}
return "share '" + url + "' r:" + r + " w:" + w +
" -> '" + sh.Path + "'"
return "share " + access + " '" + url + "' -> '" + sh.Path + "'"
}
func (sh *SharedLocation) mkdir() error {
return os.MkdirAll(sh.Path, 0o755)
_, err := os.Stat(sh.Path)
if err == os.ErrNotExist {
return os.MkdirAll(sh.Path, 0o755)
}
return err
}
type UrlStatus uint32
+5 -2
View File
@@ -164,8 +164,8 @@ func (conn *connection) serveFile(filePath string) *http.Response {
// if not truncate, mtime will always be > ifModifiedSince
mtime := finf.ModTime().UTC().Truncate(time.Second)
ifModifiedSince, errTimeParse := time.Parse(http.TimeFormat, conn.req.Headers["if-modified-since"])
notModified := errTimeParse == nil && !mtime.After(ifModifiedSince)
ifModifiedSince, errTime := time.Parse(http.TimeFormat, conn.req.Headers["if-modified-since"])
notModified := errTime == nil && !mtime.After(ifModifiedSince)
if notModified {
return conn.http.CreateResponse(http.StatusNotModified, conn.req, false)
}
@@ -178,6 +178,9 @@ func (conn *connection) serveFile(filePath string) *http.Response {
fileOwnershipTransferred = true
res.AttachBodyReader(f, size, http.GetMimeType(fileName))
}
// Tell browser to not use its own caching, remember last-modified
// On next request browser will send if-modified-since and let server check if file changed.
res.Headers["cache-control"] = "no-cache"
res.Headers["last-modified"] = mtime.Format(http.TimeFormat)
res.Headers["content-disposition"] = http.ContentDisposition("inline", fileName)
//TODO: accept "range" header and return StatusPartialContent
+7 -6
View File
@@ -41,7 +41,8 @@ type listingRow struct {
}
func (conn *connection) serveDirListing(dirPath string) *http.Response {
htmlBytes, err := genDirListingHtml(&conn.log, dirPath, conn.req.Url)
htmlBytes, err := genDirListingHtml(&conn.log, dirPath,
conn.req.Url, conn.srv.Conf.HtmlTitle)
if err != nil {
return conn.createErrorResponse(false, http.StatusInternalServerError, err.Error())
}
@@ -51,7 +52,7 @@ func (conn *connection) serveDirListing(dirPath string) *http.Response {
return res
}
func genDirListingHtml(logger *log.LoggerContext, dirPath, reqUrl string) ([]byte, error) {
func genDirListingHtml(logger *log.LoggerContext, dirPath, reqUrl, pageTitle string) ([]byte, error) {
dirEntries, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
@@ -89,9 +90,9 @@ func genDirListingHtml(logger *log.LoggerContext, dirPath, reqUrl string) ([]byt
<head>
<meta charset="utf-8">
<title>`)
reqUrlHtmlEscaped := htmlEscape(reqUrl)
sb.AddS(reqUrlHtmlEscaped)
sb.AddS(`</title>
sb.AddS(htmlEscape(pageTitle + " " + reqUrl))
sb.AddS(`
</title>
<style>
body { font-family: sans-serif; margin: 2em; }
td { padding: .2em 1em .2em 0; }
@@ -99,7 +100,7 @@ func genDirListingHtml(logger *log.LoggerContext, dirPath, reqUrl string) ([]byt
</style>
</head>
<body>`)
sb.AddS("<h1>").AddS(reqUrlHtmlEscaped).AddS("</h1>")
sb.AddS("<h1>").AddS(htmlEscape(reqUrl)).AddS("</h1>")
sb.AddS(`<table id="listing">`)
sb.AddS(`<tr><td><a href="`)
sb.AddS(http.PercentEcodeUrl(path.Dir(reqUrl)))