From 2fb58a7195a04889802245ac88964ef330c02c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B8=D0=BD=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=20?= =?UTF-8?q?=D0=A2=D0=B8=D0=BC=D1=83=D1=80=20=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 28 Aug 2026 13:24:17 +0300 Subject: [PATCH] MSP-8438 Add setting for overriding standart json content-header to "application/json; charset=utf-8" --- assembly/locator.go | 6 ++++++ conf/remote.go | 6 ++++++ middleware/json_charset_utf-8.go | 15 +++++++++++++++ proxy/http.go | 13 +++++++++++++ request/context.go | 10 ++++++++++ 5 files changed, 50 insertions(+) create mode 100644 middleware/json_charset_utf-8.go diff --git a/assembly/locator.go b/assembly/locator.go index 98eccb8..bdd8459 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -124,6 +124,11 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location) (http.Ha forwardReqIdByAppId[setting.ApplicationId] = setting.ForwardRequestId } + enableUtf8JsonCharsetByApp := make(map[int]bool, len(config.ResponseContentTypeSettings)) + for _, setting := range config.ResponseContentTypeSettings { + enableUtf8JsonCharsetByApp[setting.ApplicationId] = setting.AddJsonUtf8Charset + } + metricsStorage := http_metrics.NewServerStorage(metrics.DefaultRegistry) handler := middleware.Chain( @@ -140,6 +145,7 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location) (http.Ha middleware.Authenticate(authentication), middleware.AdminAuthenticate(adminService), middleware.ClientRequestId(config.EnableClientRequestIdForwarding, forwardReqIdByAppId), + middleware.JsonResponseUtf8Charset(enableUtf8JsonCharsetByApp), middleware.Authorize(authorization, l.logger), middleware.AdminAuthorize(adminService), middleware.Throttling(throttlingService), diff --git a/conf/remote.go b/conf/remote.go index 9e2563c..c686dfe 100644 --- a/conf/remote.go +++ b/conf/remote.go @@ -30,6 +30,7 @@ type Remote struct { EnableClientRequestIdForwarding bool `schema:"Включить проброс requestId из заголовка запроса"` ForwardReqIdClientSettings []ForwardReqIdClientSettings `schema:"Настройки проброcа requestId для приложений"` CustomAuth CustomAuth `schema:"Настройка кастомной аутентификации/авторизации"` + ResponseContentTypeSettings []ResponseContentTypeSetting `schema:"Настройки Content-Type ответов"` } type ForwardReqIdClientSettings struct { @@ -98,3 +99,8 @@ type UserAuthSetting struct { CacheDataInSec int `schema:"Время кеширования данных аутентификации/авторизации пользователя,отключен при значениях <=0,в секундах"` SkipAppAuth bool `schema:"Пропустить аутентификацию и авторизацию приложения"` } + +type ResponseContentTypeSetting struct { + ApplicationId int `validate:"required" schema:"ID приложения"` + AddJsonUtf8Charset bool `schema:"Добавлять charset=utf-8 в Content-Type ответа: application/json для locations с protocol=http"` +} diff --git a/middleware/json_charset_utf-8.go b/middleware/json_charset_utf-8.go new file mode 100644 index 0000000..a0bcef9 --- /dev/null +++ b/middleware/json_charset_utf-8.go @@ -0,0 +1,15 @@ +package middleware + +import ( + "isp-gate-service/request" +) + +func JsonResponseUtf8Charset(applicationIds map[int]bool) Middleware { + return func(next Handler) Handler { + return HandlerFunc(func(ctx *request.Context) error { + authData, _ := ctx.GetAuthData() + ctx.EnableHttpJsonUtf8Charset(applicationIds[authData.ApplicationId]) + return next.Handle(ctx) + }) + } +} diff --git a/proxy/http.go b/proxy/http.go index d195e03..113e80c 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -9,6 +9,7 @@ import ( "net/http/httputil" "net/url" "strconv" + "strings" "time" "isp-gate-service/httperrors" @@ -79,6 +80,9 @@ func (p Http) Handle(ctx *request.Context) error { errors.WithMessagef(err, "http proxy to %s", host), ) } + if ctx.IsJsonUtf8CharsetEnabled() { + reverseProxy.ModifyResponse = addJsonUtf8Charset + } context, cancel := context.WithTimeout(request.Context(), p.timeout) defer cancel() @@ -124,3 +128,12 @@ func setHttpHeaders(ctx *request.Context, header http.Header, skipAuth bool) { func defaultTransportDialContext(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) { return dialer.DialContext } + +func addJsonUtf8Charset(response *http.Response) error { + contentType := response.Header.Get("Content-Type") + if strings.EqualFold(contentType, "application/json") { + response.Header.Set("Content-Type", "application/json; charset=utf-8") + } + + return nil +} diff --git a/request/context.go b/request/context.go index 1ff7c86..6c9fbc8 100644 --- a/request/context.go +++ b/request/context.go @@ -30,6 +30,8 @@ type Context struct { adminToken string queryParams map[string]string + + httpJsonUtf8Charset bool } func NewContext( @@ -114,6 +116,14 @@ func (c *Context) SetContext(ctx context.Context) { c.request = c.request.WithContext(ctx) } +func (c *Context) EnableHttpJsonUtf8Charset(enable bool) { + c.httpJsonUtf8Charset = enable +} + +func (c *Context) IsJsonUtf8CharsetEnabled() bool { + return c.httpJsonUtf8Charset +} + func (c *Context) Param(name string) string { value := c.request.Header.Get(name) if value != "" {