Skip to content

Commit

Permalink
fix response header Content-Type
Browse files Browse the repository at this point in the history
  • Loading branch information
xuweiwei committed Aug 2, 2022
1 parent 28e040d commit aa3b8df
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
10 changes: 6 additions & 4 deletions pkg/common/constant/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
package constant

const (
HeaderKeyContextType = "Content-Type"
HeaderKeyContentType = "Content-Type"

HeaderKeyAccessControlAllowOrigin = "Access-Control-Allow-Origin"
HeaderKeyAccessControlExposeHeaders = "Access-Control-Expose-Headers"
HeaderKeyAccessControlAllowMethods = "Access-Control-Allow-Methods"
HeaderKeyAccessControlMaxAge = "Access-Control-Max-Age"
HeaderKeyAccessControlAllowCredentials = "Access-Control-Allow-Credentials"

HeaderValueJsonUtf8 = "application/json;charset=UTF-8"
HeaderValueTextPlain = "text/plain"
HeaderValueAll = "*"
HeaderValueJsonUtf8 = "application/json;charset=UTF-8"
HeaderValueTextPlain = "text/plain"
HeaderValueApplicationJson = "application/json"

HeaderValueAll = "*"

PathSlash = "/"
ProtocolSlash = "://"
Expand Down
9 changes: 7 additions & 2 deletions pkg/common/http/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package http

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
stdHttp "net/http"
Expand Down Expand Up @@ -141,13 +142,17 @@ func (hcm *HttpConnectionManager) buildTargetResponse(c *pch.HttpContext) {
c.TargetResp = &client.Response{Data: body}
case []byte:
c.StatusCode(stdHttp.StatusOK)
c.AddHeader(constant.HeaderKeyContextType, constant.HeaderValueTextPlain)
if json.Valid(res) {
c.AddHeader(constant.HeaderKeyContentType, constant.HeaderValueApplicationJson)
} else {
c.AddHeader(constant.HeaderKeyContentType, constant.HeaderValueTextPlain)
}
c.TargetResp = &client.Response{Data: res}
default:
//dubbo go generic invoke
response := util.NewDubboResponse(res, false)
c.StatusCode(stdHttp.StatusOK)
c.AddHeader(constant.HeaderKeyContextType, constant.HeaderValueJsonUtf8)
c.AddHeader(constant.HeaderKeyContentType, constant.HeaderValueJsonUtf8)
c.TargetResp = response
}
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/context/http/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package http

import (
"context"
"encoding/json"
"math"
"net"
"net/http"
Expand Down Expand Up @@ -177,8 +178,11 @@ func (hc *HttpContext) SendLocalReply(status int, body []byte) {
hc.statusCode = status
hc.localReplyBody = body
hc.TargetResp = &client.Response{Data: body}
hc.AddHeader(constant.HeaderKeyContextType, constant.HeaderValueTextPlain)

if json.Valid(body) {
hc.AddHeader(constant.HeaderKeyContentType, constant.HeaderValueApplicationJson)
} else {
hc.AddHeader(constant.HeaderKeyContentType, constant.HeaderValueTextPlain)
}
writer := hc.Writer
writer.WriteHeader(status)
_, err := writer.Write(body)
Expand Down

0 comments on commit aa3b8df

Please sign in to comment.