From 695b900f28dc7c784e6d641abe7ea1ec25573811 Mon Sep 17 00:00:00 2001 From: Lukas Schulte Pelkum Date: Sat, 17 Jun 2023 18:35:26 +0200 Subject: [PATCH] write Content-Type and Content-Length headers --- cmd/pasty/main.go | 2 +- internal/config/compatibility.go | 28 ++++++++++++++-------------- internal/config/config.go | 30 +++++++++++++++--------------- internal/web/response_writer.go | 7 +++++++ 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/cmd/pasty/main.go b/cmd/pasty/main.go index 24f9622..b1fe949 100644 --- a/cmd/pasty/main.go +++ b/cmd/pasty/main.go @@ -91,7 +91,7 @@ func main() { PasteIDLength: cfg.PasteIDLength, PasteIDCharset: cfg.PasteIDCharset, PasteLengthCap: cfg.PasteLengthCap, - ModificationTokensEnabled: cfg.ModificationTokens, + ModificationTokensEnabled: cfg.ModificationTokensEnabled, ModificationTokenLength: cfg.ModificationTokenLength, ModificationTokenCharset: cfg.ModificationTokenCharset, } diff --git a/internal/config/compatibility.go b/internal/config/compatibility.go index 976ad1b..3a73709 100644 --- a/internal/config/compatibility.go +++ b/internal/config/compatibility.go @@ -22,19 +22,19 @@ var removedKeys = []string{ } var keyRedirects = map[string][]string{ - "PASTY_ADDRESS": {"PASTY_WEB_ADDRESS"}, - "PASTY_STORAGE_DRIVER": {"PASTY_STORAGE_TYPE"}, - "PASTY_POSTGRES_DSN": {"PASTY_STORAGE_POSTGRES_DSN"}, - "PASTY_PASTE_ID_LENGTH": {"PASTY_ID_LENGTH"}, - "PASTY_PASTE_ID_CHARSET": {"PASTY_ID_CHARACTERS"}, - "PASTY_PASTE_LENGTH_CAP": {"PASTY_LENGTH_CAP"}, - "PASTY_REPORTS_ENABLED": {"PASTY_REPORTS_ENABLED"}, - "PASTY_REPORTS_WEBHOOK_URL": {"PASTY_REPORT_WEBHOOK"}, - "PASTY_REPORTS_WEBHOOK_TOKEN": {"PASTY_REPORT_WEBHOOK_TOKEN"}, - "PASTY_MODIFICATION_TOKEN_CHARSET": {"PASTY_MODIFICATION_TOKEN_CHARACTERS"}, - "PASTY_MODIFICATION_TOKENS": {"PASTY_DELETION_TOKENS"}, - "PASTY_MODIFICATION_TOKEN_MASTER": {"PASTY_DELETION_TOKEN_MASTER"}, - "PASTY_MODIFICATION_TOKEN_LENGTH": {"PASTY_DELETION_TOKEN_LENGTH"}, + "PASTY_ADDRESS": {"PASTY_WEB_ADDRESS"}, + "PASTY_STORAGE_DRIVER": {"PASTY_STORAGE_TYPE"}, + "PASTY_POSTGRES_DSN": {"PASTY_STORAGE_POSTGRES_DSN"}, + "PASTY_PASTE_ID_LENGTH": {"PASTY_ID_LENGTH"}, + "PASTY_PASTE_ID_CHARSET": {"PASTY_ID_CHARACTERS"}, + "PASTY_PASTE_LENGTH_CAP": {"PASTY_LENGTH_CAP"}, + "PASTY_REPORTS_ENABLED": {"PASTY_REPORTS_ENABLED"}, + "PASTY_REPORTS_WEBHOOK_URL": {"PASTY_REPORT_WEBHOOK"}, + "PASTY_REPORTS_WEBHOOK_TOKEN": {"PASTY_REPORT_WEBHOOK_TOKEN"}, + "PASTY_MODIFICATION_TOKENS_ENABLED": {"PASTY_MODIFICATION_TOKENS", "PASTY_DELETION_TOKENS"}, + "PASTY_MODIFICATION_TOKEN_CHARSET": {"PASTY_MODIFICATION_TOKEN_CHARACTERS"}, + "PASTY_MODIFICATION_TOKEN_MASTER": {"PASTY_DELETION_TOKEN_MASTER"}, + "PASTY_MODIFICATION_TOKEN_LENGTH": {"PASTY_DELETION_TOKEN_LENGTH"}, } // Compatibility runs several compatibility measurements. @@ -55,7 +55,7 @@ func Compatibility() { if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { continue } - log.Warn().Msgf("You have set the '%s' environment variable. This variable has been renamed to '%s'. The value has been propagated, but please consider changing your configuration to avoid further complications.", oldKey, newKey) + log.Warn().Msgf("You have set the '%s' environment variable. This variable has been renamed to '%s'. The value has been propagated, but please consider adjusting your configuration to avoid further complications.", oldKey, newKey) break } } diff --git a/internal/config/config.go b/internal/config/config.go index 3cac944..d32789f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -7,21 +7,21 @@ import ( ) type Config struct { - LogLevel string `default:"info" split_words:"true"` - Address string `default:":8080" split_words:"true"` - StorageDriver string `default:"sqlite" split_words:"true"` - PasteIDLength int `default:"6" split_words:"true"` - PasteIDCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"` - ModificationTokens bool `default:"true" split_words:"true"` - ModificationTokenMaster string `split_words:"true"` - ModificationTokenLength int `default:"12" split_words:"true"` - ModificationTokenCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"` - RateLimit string `default:"30-M" split_words:"true"` - PasteLengthCap int `default:"50000" split_words:"true"` - AutoDelete *AutoDeleteConfig `split_words:"true"` - Reports *ReportConfig - Postgres *PostgresConfig - SQLite *SQLiteConfig + LogLevel string `default:"info" split_words:"true"` + Address string `default:":8080" split_words:"true"` + StorageDriver string `default:"sqlite" split_words:"true"` + PasteIDLength int `default:"6" split_words:"true"` + PasteIDCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"` + ModificationTokensEnabled bool `default:"true" split_words:"true"` + ModificationTokenMaster string `split_words:"true"` + ModificationTokenLength int `default:"12" split_words:"true"` + ModificationTokenCharset string `default:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" split_words:"true"` + RateLimit string `default:"30-M" split_words:"true"` + PasteLengthCap int `default:"50000" split_words:"true"` + AutoDelete *AutoDeleteConfig `split_words:"true"` + Reports *ReportConfig + Postgres *PostgresConfig + SQLite *SQLiteConfig } type AutoDeleteConfig struct { diff --git a/internal/web/response_writer.go b/internal/web/response_writer.go index accaee9..bb82d97 100644 --- a/internal/web/response_writer.go +++ b/internal/web/response_writer.go @@ -4,14 +4,19 @@ import ( "encoding/json" "github.com/lus/pasty/pkg/chizerolog" "net/http" + "strconv" ) func writeErr(request *http.Request, writer http.ResponseWriter, err error) { chizerolog.InjectError(request, err) + writer.Header().Set("Content-Type", "text/plain") + writer.Header().Set("Content-Length", strconv.Itoa(len(err.Error()))) writeString(writer, http.StatusInternalServerError, err.Error()) } func writeString(writer http.ResponseWriter, status int, value string) { + writer.Header().Set("Content-Type", "text/plain") + writer.Header().Set("Content-Length", strconv.Itoa(len(value))) writer.WriteHeader(status) writer.Write([]byte(value)) } @@ -22,6 +27,8 @@ func writeJSON(writer http.ResponseWriter, status int, value any) error { return err } + writer.Header().Set("Content-Type", "application/json") + writer.Header().Set("Content-Length", strconv.Itoa(len(jsonData))) writer.WriteHeader(status) writer.Write(jsonData)