diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2026-03-15 14:54:44 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2026-03-15 14:54:44 -0700 |
| commit | f78ab940a8c09702be3e0ba310ac56e02fa76701 (patch) | |
| tree | 4b128f55860d1ce7ef9c87861179d97f5732b476 /hotline/server.go | |
| parent | 72c6ddc104c349a4798fddeaa8f77dbe139fe104 (diff) | |
Add configurable text encoding for file and folder names
Replace hardcoded Mac Roman encoding globals with a configurable Encoding
field in config.yaml. Servers that exclusively serve modern UTF-8 clients
can now set Encoding: utf8 to disable Mac Roman conversion. The default
remains "macintosh" for backward compatibility.
- Add Encoding field to Config struct (macintosh|utf8)
- Store TextDecoder/TextEncoder on Server, initialized from config
- Add TextDecoder()/TextEncoder() accessors on ClientConn
- Pass decoder/encoder explicitly to ReadPath and GetFileNameList
- Remove package-level txtEncoder/txtDecoder globals
- Add warning log when files are skipped due to encoding errors
- Log and return error replies in HandleGetFileNameList on failure
- Document the new config option in docs/text-encoding.md
Diffstat (limited to 'hotline/server.go')
| -rw-r--r-- | hotline/server.go | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/hotline/server.go b/hotline/server.go index 545e84c..dcb90e7 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -18,6 +18,7 @@ import ( "time" "github.com/redis/go-redis/v9" + "golang.org/x/text/encoding" "golang.org/x/text/encoding/charmap" "golang.org/x/time/rate" ) @@ -30,12 +31,6 @@ type requestCtx struct { remoteAddr string } -// Converts bytes from Mac Roman encoding to UTF-8 -var txtDecoder = charmap.Macintosh.NewDecoder() - -// Converts bytes from UTF-8 to Mac Roman encoding -var txtEncoder = charmap.Macintosh.NewEncoder() - type Server struct { NetInterface string Port int @@ -73,6 +68,9 @@ type Server struct { // TrackerRegistrar handles tracker registration (injectable for testing) TrackerRegistrar TrackerRegistrar + TextDecoder *encoding.Decoder + TextEncoder *encoding.Encoder + TLSConfig *tls.Config TLSPort int } @@ -140,6 +138,16 @@ func NewServer(options ...Option) (*Server, error) { opt(&server) } + // Initialize text encoding based on config. + switch server.Config.Encoding { + case "utf8": + server.TextDecoder = encoding.Nop.NewDecoder() + server.TextEncoder = encoding.Nop.NewEncoder() + default: + server.TextDecoder = charmap.Macintosh.NewDecoder() + server.TextEncoder = charmap.Macintosh.NewEncoder() + } + // generate a new random passID for tracker registration _, err := rand.Read(server.TrackerPassID[:]) if err != nil { @@ -690,7 +698,7 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro "Name", string(fileTransfer.ClientConn.UserName), ) - fullPath, err := ReadPath(fileTransfer.FileRoot, fileTransfer.FilePath, fileTransfer.FileName) + fullPath, err := ReadPath(fileTransfer.FileRoot, fileTransfer.FilePath, fileTransfer.FileName, s.TextDecoder) if err != nil { return err } |