From 793cbcc37f7b8fd58fc6f8aad5aebb288172ab52 Mon Sep 17 00:00:00 2001 From: Altair-sh Date: Tue, 3 Nov 2026 11:51:54 +0100 Subject: [PATCH] small fixes --- internal/http/Response.go | 2 ++ internal/server/Config.go | 1 + internal/server/SharedLocation.go | 18 +++++++++++------- internal/server/connection.go | 7 +++++-- internal/server/html.go | 13 +++++++------ serveg.example.toml | 9 ++++++++- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/internal/http/Response.go b/internal/http/Response.go index c3f8551..cc37427 100644 --- a/internal/http/Response.go +++ b/internal/http/Response.go @@ -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", }, } diff --git a/internal/server/Config.go b/internal/server/Config.go index 665e784..711c4b7 100644 --- a/internal/server/Config.go +++ b/internal/server/Config.go @@ -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/ diff --git a/internal/server/SharedLocation.go b/internal/server/SharedLocation.go index 23e49b4..a54ed5f 100644 --- a/internal/server/SharedLocation.go +++ b/internal/server/SharedLocation.go @@ -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 diff --git a/internal/server/connection.go b/internal/server/connection.go index c86098a..bde7a56 100644 --- a/internal/server/connection.go +++ b/internal/server/connection.go @@ -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 diff --git a/internal/server/html.go b/internal/server/html.go index 751647b..de6a47b 100644 --- a/internal/server/html.go +++ b/internal/server/html.go @@ -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 `) - reqUrlHtmlEscaped := htmlEscape(reqUrl) - sb.AddS(reqUrlHtmlEscaped) - sb.AddS(` + sb.AddS(htmlEscape(pageTitle + " " + reqUrl)) + sb.AddS(` + `) - sb.AddS("

").AddS(reqUrlHtmlEscaped).AddS("

") + sb.AddS("

").AddS(htmlEscape(reqUrl)).AddS("

") sb.AddS(``) sb.AddS(`