Compare commits

...

106 Commits

Author SHA1 Message Date
chenzhihui 4289e1da63 fix param order 2 years ago
chenzhihui e4daa56e53 Merge remote-tracking branch 'origin' into status-code-override 2 years ago
chenzhihui 239360516a add Redirector to the request with a redirect to url 2 years ago
dependabot[bot] 02ac2944ca build(deps): bump gopkg.in/yaml.v3 in /contrib/config/nacos (#2067) 2 years ago
dependabot[bot] f5c46e2c12 build(deps): bump gopkg.in/yaml.v3 in /contrib/config/apollo (#2068) 2 years ago
Tony Chen f7da448a88 fix with logger (#2062) 2 years ago
dependabot[bot] 402532a6cd build(deps): bump k8s.io/client-go in /contrib/config/kubernetes (#2056) 2 years ago
dependabot[bot] 8fead618c0 build(deps): bump k8s.io/client-go in /contrib/registry/kubernetes (#2057) 2 years ago
dependabot[bot] dc060e7afd build(deps): bump github.com/aliyun/aliyun-log-go-sdk (#2053) 2 years ago
dependabot[bot] 17600f8fb2 build(deps): bump k8s.io/api in /contrib/config/kubernetes (#2052) 2 years ago
Tony Chen 2dd247be8f log: remove component logger to use global logger (#2061) 2 years ago
dependabot[bot] ebf78ce69f build(deps): bump k8s.io/api in /contrib/registry/kubernetes (#2058) 2 years ago
包子 d2129bb114 fix: config log 2 years ago
包子 e388547d82 feat: add config log (#2060) 2 years ago
Tony Chen 00b71747e8 app: fix instance nil when not registered (#2059) 2 years ago
dependabot[bot] ff474e6a70 build(deps): bump actions/setup-go from 2 to 3.2.0 (#2048) 2 years ago
dependabot[bot] 418916d010 build(deps): bump golangci/golangci-lint-action from 2 to 3 (#2047) 2 years ago
dependabot[bot] ac5b5124c1 build(deps): bump actions/checkout from 2 to 3 (#2046) 2 years ago
dependabot[bot] 155ef482d0 build(deps): bump github/codeql-action from 1 to 2 (#2045) 2 years ago
包子 55237aab3e Update dependabot.yml 2 years ago
包子 c65bf30b04 Create dependabot.yml 2 years ago
longxboy 57a935c8d1 feat:add context for log (#2041) 2 years ago
包子 ded51d352c deps: upgrade kratos version to v2.3.0 (#2036) 2 years ago
Tony Chen f8e4b620f2 opensergo: fix http path & method (#2035) 2 years ago
Kagaya 850710a356 feat(contrib/opensergo): add opensergo reportMetadata fields (#1996) 2 years ago
Tony Chen c4b3b5a978 log: fix global logger (#2034) 2 years ago
Tony Chen bc25b3d81b http: handle default mux (#2033) 2 years ago
longxboy 16db9c889a fix:fix error camel case (#2032) 2 years ago
haiyux 31bffc2a7f deps: upgrade go mod version (#2028) 2 years ago
songzhibin97 d57a3c2a8b Update project.go (#2024) 2 years ago
songzhibin97 bee818891d Update server.go (#2021) 2 years ago
songzhibin97 901307e51f style:change (#2022) 2 years ago
songzhibin97 9eaee21a03 Update path.go (#2023) 2 years ago
songzhibin97 cda3eaadcd Update run.go (#2025) 2 years ago
songzhibin97 7815e482f3 stype:gen_go_errors (#2026) 2 years ago
songzhibin97 4e51b25f11 Update server.go (#2027) 2 years ago
songzhibin97 3a15f5df9e style:ewma (#2018) 2 years ago
songzhibin97 72ba4e5c43 Style:trace (#2017) 2 years ago
songzhibin97 9abcff9d30 Update jwt.go (#2016) 2 years ago
songzhibin97 a5a4fd2e0d Update metadata.go (#2015) 2 years ago
songzhibin97 996a1d0956 style:log (#2014) 2 years ago
songzhibin97 8755827c33 Update encode.go (#2019) 2 years ago
Ccheers 93ac681455 feat(registry): zookeeper watch node changed (#1986) 2 years ago
songzhibin97 fa3d9c3fc1 Update endpoint.go (#2012) 2 years ago
songzhibin97 291f0f3ad0 Update host.go (#2013) 2 years ago
songzhibin97 769a40957c Update errors.go (#2009) 2 years ago
songzhibin97 40b7a6b069 fix:#2006 config.atomicValue Other basic types are supported (#2007) 2 years ago
songzhibin97 d8118045f4 Update file.go (#2005) 2 years ago
qi 8a8694c16c fix(protoc-gen-go-errors): fix generated function comments have extra blank lines. (#2008) 2 years ago
songzhibin97 3375816ce0 fix:#2002 definition service lowercase generation cannot be exported (#2003) 2 years ago
JinChang d6e3fed0cf Added ability to configure protojson (#1993) 2 years ago
包子 57380fa021 fix: update ci gocyclo min-complexity 50 (#2000) 2 years ago
pwli ec69624f8a fix:(transport/http): fix unexpected overriding behavior when return an error (#1984) 2 years ago
houseme 784d96058e fix: Replace the docker image of Polaris (#1999) 2 years ago
songzhibin97 bcf98570fe fix:#1987 Http and grpc generate route alignment (#1988) 2 years ago
Comolli a2765e27b4 feat: decode google.protobuf.BytesValue compatible base64.URLEncoding. (#1991) 2 years ago
Kagaya 0a8ab7a48a docs: remove redundant info & fix typo in pr template (#1997) 2 years ago
Kagaya c329cffa3f ignore polaris test & add eureka consul test (#1998) 2 years ago
qi ced122a688 fix: query params: parse list failed, unsupported message type: "google.protobuf.Struct" #1967 (#1989) 2 years ago
Nikita Krasnikov be9cfde7a4 Fix buildMethodDesc for two and more message field (#1979) 2 years ago
witt 49a9572118 fix: delete redundant type conversion (#1977) 2 years ago
Jinming Guo bdcd9c4525 fix: add 'go mod tidy' cmd to make quick-start available (#1973) 2 years ago
Underworld511 f1b8ede77f Update README_zh.md (#1975) 2 years ago
包子 a61cd5a6ee deps: upgrade kratos version to v2.2.2 (#1944) 2 years ago
Elric Li b44daec0db feat(log): Helper implemented io.Writer (#1927) 2 years ago
JeffreyBool 48df7b1b71 feat: protoc-gen-go-errors add comment (#1961) 2 years ago
林晓炜 42640e92e1 feat(registry): contrib/registry/zookeeper add digest acl support (#1964) 2 years ago
张东龙 662e8c7b17 fix(transport/http): responseEncoder should not write any data when it need to write nil (#1945) 2 years ago
haiyux 9a98fcb04f feat:new and add karge warehouse (#1953) 2 years ago
haiyux eaa5b161b4 fix: fix kratos run when cmd number is one (#1956) 2 years ago
haiyux 572270ba91 fix: fix choose failed when paths cmd/server (#1954) 2 years ago
Casper-Mars 2d5da155fd fix(contrib/opensergo): fix index error (#1951) 2 years ago
张东龙 65e5d680b1 fix bind test errors (#1950) 2 years ago
李洛克 11273fc9a6 fix client do method done not use when err not nil (#1948) 2 years ago
Tony Chen 045c5001c9 feat: add opensergo metadata (#1947) 2 years ago
yeqown 00e38a6c2b feat(discovery): provide an option to disable discovery debug log (#1942) 2 years ago
Tony Chen 0dc30dde72 feat(selector): add node scheme (#1932) 2 years ago
Windfarer 13e5fd26af correct the func name in meatadata log (#1915) 2 years ago
Xudong Cai acb4588d69 fix: error case2Camel (#1923) 2 years ago
Tony Chen a9ebd0822c feat: add error cause for statck trace (#1910) 2 years ago
Casper-Mars 4133bc497c feat: change description of Kratos (#1920) 2 years ago
Casper-Mars 64c4417de3 feat(registry): consul client add DeregisterCriticalServiceAfter option (#1917) 2 years ago
Casper-Mars d0dd3309db fix(cmd/protoc-gen-go-errors):fix lint problem (#1919) 2 years ago
yoogo 53d5795def fix(cmd): protoc-gen-go-http use self release (#1909) 2 years ago
Tony Chen 58bce8dd11 add json codec for grpc (#1908) 2 years ago
包子 a58dd0c110 fix(transport): fix the problem that the context is not delivered correctly (#1906) 2 years ago
包子 1873cea771 fix(log/aliyun): Improper Input Validation in GoGo Protobuf CVE-2021-3121 (#1902) 2 years ago
Cluas a589fde695 fix(log): FilterFunc keyvals lost logger prefix (#1901) 2 years ago
Cluas 8ba2190aa8 feat(log): log load config only when Debug level (#1899) 2 years ago
weetime db93345733 feat(contrib): add eureka registry (#1792) (#1793) 2 years ago
Tony Chen c764d65454 fix: starter parent ctx (#1895) 2 years ago
longxboy 48d88ac6ab feat: supprt non-kratos instance in consul registry (#1892) 2 years ago
包子 a2702db493 deps: upgrade kratos version to 2.2.1 (#1890) 2 years ago
包子 f69bcf0f50 fix: update semantic (#1891) 2 years ago
包子 b80835a131 fix(cmd): proto-gen-go-http warn only when ending with a slash (#1887) 2 years ago
Tony Chen 1bb7e377e5 fix(log): call depth (#1885) 2 years ago
letian 22a3a9e9bb fix: kratos command error on windows (#1884) 2 years ago
Tony Chen 29824954fa fix: decode empty body (#1882) 2 years ago
Sasha Melentyev caada1acc7 ci: Add 1.18 (#1880) 2 years ago
hoslo 92357bc162 fix: graceful shutdown (#1873) 2 years ago
包子 13d3da62ac chore: remove examples (#1871) 2 years ago
包子 3e39b139bd feat: add semantic.yml (#1876) 2 years ago
kwanhur e49a3e6801 style(cmd/errors/examples/middleware/transport): fix common words' spelling mistakes (#1872) 2 years ago
Chen Xinyuan 4fcff315b2 fix: fix some typos (#1869) 2 years ago
kwanhur c3de800938 chore(examples): optimize typos of swagger/server (#1862) 2 years ago
Casper 6dc826b7ea feat(transport/http):add StatusCoder to support overriding http status code 3 years ago
  1. 20
      transport/http/codec.go
  2. 37
      transport/http/codec_test.go
  3. 18
      transport/http/redirect.go

@ -12,6 +12,18 @@ import (
// SupportPackageIsVersion1 These constants should not be referenced from any other code. // SupportPackageIsVersion1 These constants should not be referenced from any other code.
const SupportPackageIsVersion1 = true const SupportPackageIsVersion1 = true
// StatusCoder is checked by DefaultResponseEncoder. If a response value implements
// StatusCoder, the StatusCode will be used to set http status code.
type StatusCoder interface {
StatusCode() int
}
// Redirector replies to the request with a redirect to url
// which may be a path relative to the request path.
type Redirector interface {
Redirect() (string, int)
}
// Request type net/http. // Request type net/http.
type Request = http.Request type Request = http.Request
@ -57,7 +69,15 @@ func DefaultResponseEncoder(w http.ResponseWriter, r *http.Request, v interface{
if err != nil { if err != nil {
return err return err
} }
if rd, ok := v.(Redirector); ok {
url, code := rd.Redirect()
http.Redirect(w, r, url, code)
return nil
}
w.Header().Set("Content-Type", httputil.ContentType(codec.Name())) w.Header().Set("Content-Type", httputil.ContentType(codec.Name()))
if code, ok := v.(StatusCoder); ok {
w.WriteHeader(code.StatusCode())
}
_, err = w.Write(data) _, err = w.Write(data)
if err != nil { if err != nil {
return err return err

@ -52,11 +52,21 @@ func (w *mockResponseWriter) WriteHeader(statusCode int) {
w.StatusCode = statusCode w.StatusCode = statusCode
} }
type dataWithStatusCode struct { type respData struct {
A string `json:"a"` A string `json:"a"`
B int64 `json:"b"` B int64 `json:"b"`
} }
type respDataWithStatusCode struct {
A string `json:"a"`
B int64 `json:"b"`
sCode int
}
func (r *respDataWithStatusCode) StatusCode() int {
return r.sCode
}
func TestDefaultResponseEncoder(t *testing.T) { func TestDefaultResponseEncoder(t *testing.T) {
w := &mockResponseWriter{StatusCode: 200, header: make(nethttp.Header)} w := &mockResponseWriter{StatusCode: 200, header: make(nethttp.Header)}
req1 := &nethttp.Request{ req1 := &nethttp.Request{
@ -64,7 +74,7 @@ func TestDefaultResponseEncoder(t *testing.T) {
} }
req1.Header.Set("Content-Type", "application/json") req1.Header.Set("Content-Type", "application/json")
v1 := &dataWithStatusCode{A: "1", B: 2} v1 := &respData{A: "1", B: 2}
err := DefaultResponseEncoder(w, req1, v1) err := DefaultResponseEncoder(w, req1, v1)
if err != nil { if err != nil {
t.Errorf("expected no error, got %v", err) t.Errorf("expected no error, got %v", err)
@ -151,3 +161,26 @@ func TestCodecForRequest(t *testing.T) {
t.Errorf("expected %v, got %v", "json", c.Name()) t.Errorf("expected %v, got %v", "json", c.Name())
} }
} }
func TestDefaultResponseEncoderWithStatusCoder(t *testing.T) {
w := &mockResponseWriter{StatusCode: 200, header: make(nethttp.Header)}
req1 := &nethttp.Request{
Header: make(nethttp.Header),
}
req1.Header.Set("Content-Type", "application/json")
cusStatusCode := 201
v1 := &respDataWithStatusCode{A: "1", B: 2, sCode: cusStatusCode}
err := DefaultResponseEncoder(w, req1, v1)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if !reflect.DeepEqual("application/json", w.Header().Get("Content-Type")) {
t.Errorf("expected %v, got %v", "application/json", w.Header().Get("Content-Type"))
}
if !reflect.DeepEqual(cusStatusCode, w.StatusCode) {
t.Errorf("expected %v, got %v", cusStatusCode, w.StatusCode)
}
if w.Data == nil {
t.Errorf("expected not nil, got %v", w.Data)
}
}

@ -0,0 +1,18 @@
package http
type redirect struct {
URL string
Code int
}
func (r *redirect) Redirect() (string, int) {
return r.URL, r.Code
}
// NewRedirect new a redirect with url, which may be a path relative to the request path.
// The provided code should be in the 3xx range and is usually StatusMovedPermanently, StatusFound or StatusSeeOther.
// If the Content-Type header has not been set, Redirect sets it to "text/html; charset=utf-8" and writes a small HTML body.
// Setting the Content-Type header to any value, including nil, disables that behavior.
func NewRedirect(url string, code int) Redirector {
return &redirect{URL: url, Code: code}
}
Loading…
Cancel
Save