mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-08-20 18:23:50 +02:00
42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
![]() |
package util
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// ContentTypeWriter is an implementation of http.ResponseWriter that will detect the content type and set the
|
||
|
// Content-Type and (optionally) Content-Disposition headers accordingly.
|
||
|
//
|
||
|
// It will always set a Content-Type based on http.DetectContentType, but will never send the "text/html"
|
||
|
// content type.
|
||
|
type ContentTypeWriter struct {
|
||
|
w http.ResponseWriter
|
||
|
sniffed bool
|
||
|
}
|
||
|
|
||
|
// NewContentTypeWriter creates a new ContentTypeWriter
|
||
|
func NewContentTypeWriter(w http.ResponseWriter) *ContentTypeWriter {
|
||
|
return &ContentTypeWriter{w, false}
|
||
|
}
|
||
|
|
||
|
func (w *ContentTypeWriter) Write(p []byte) (n int, err error) {
|
||
|
if w.sniffed {
|
||
|
return w.w.Write(p)
|
||
|
}
|
||
|
// Detect and set Content-Type header
|
||
|
// Fix content types that we don't want to inline-render in the browser. In particular,
|
||
|
// we don't want to render HTML in the browser for security reasons.
|
||
|
contentType := http.DetectContentType(p)
|
||
|
if strings.HasPrefix(contentType, "text/html") {
|
||
|
contentType = strings.ReplaceAll(contentType, "text/html", "text/plain")
|
||
|
} else if contentType == "application/octet-stream" {
|
||
|
contentType = "" // Reset to let downstream http.ResponseWriter take care of it
|
||
|
}
|
||
|
if contentType != "" {
|
||
|
w.w.Header().Set("Content-Type", contentType)
|
||
|
}
|
||
|
w.sniffed = true
|
||
|
return w.w.Write(p)
|
||
|
}
|