Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Fix: Make api.SetContentDispositionHeader thread-safe
Browse files Browse the repository at this point in the history
Per https://groups.google.com/forum/#!topic/golang-nuts/AAVPOI_G9bM, and as observed in production,
the use of transform.Chain across multiple goroutines is not supported. We allocate a transformer
per call now.
  • Loading branch information
impl committed Feb 8, 2019
1 parent 3916e12 commit 4982868
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions httputil/api/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const (
)

var (
contentDispositionDiacriticRemover = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
contentDispositionASCIIPrintableFilter = regexp.MustCompile(`[^\w\.-]+`)
)

Expand All @@ -38,12 +37,16 @@ func SetContentDispositionHeader(w http.ResponseWriter, filename string) {
filename = DefaultAttachmentFilename + ext
}

// transform.Chain transformers are not thread safe. Do not move this to a
// global.
diacriticRemover := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)

// Remove diacritics before sanitizing to ASCII.
if tr, _, err := transform.String(contentDispositionDiacriticRemover, filename); err != nil {
if tr, _, err := transform.String(diacriticRemover, filename); err != nil {
// Bad filename. We'll try to save the extension and just return a
// generic filename.
ext := path.Ext(filename)
ext, _, _ = transform.String(contentDispositionDiacriticRemover, ext)
ext, _, _ = transform.String(diacriticRemover, ext)

filename = DefaultAttachmentFilename + ext
} else {
Expand Down

0 comments on commit 4982868

Please sign in to comment.