File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -39,6 +39,11 @@ func main() {
3939 return c .String (http .StatusOK , "OK" )
4040 })
4141
42+ server .GET ("/header" , func (c echo.Context ) error {
43+ name := c .QueryParam ("name" )
44+ return c .String (http .StatusOK , c .Request ().Header .Get (name ))
45+ })
46+
4247 server .GET ("/error" , func (c echo.Context ) error {
4348 return c .NoContent (http .StatusBadRequest )
4449 })
Original file line number Diff line number Diff line change 11package proxy
22
33import (
4+ "bytes"
5+
46 "github.com/fagongzi/gateway/pkg/filter"
7+ "github.com/fagongzi/util/hack"
8+ )
9+
10+ var (
11+ headerName = []byte ("X-Forwarded-For" )
512)
613
714// XForwardForFilter XForwardForFilter
@@ -25,6 +32,17 @@ func (f *XForwardForFilter) Name() string {
2532
2633// Pre execute before proxy
2734func (f * XForwardForFilter ) Pre (c filter.Context ) (statusCode int , err error ) {
28- c .ForwardRequest ().Header .Add ("X-Forwarded-For" , c .OriginRequest ().RemoteIP ().String ())
35+ prevForward := c .OriginRequest ().Request .Header .PeekBytes (headerName )
36+ if len (prevForward ) == 0 {
37+ c .ForwardRequest ().Header .SetBytesKV (headerName , hack .StringToSlice (c .OriginRequest ().RemoteIP ().String ()))
38+ } else {
39+ var buf bytes.Buffer
40+ buf .Write (prevForward )
41+ buf .WriteByte (',' )
42+ buf .WriteByte (' ' )
43+ buf .WriteString (c .OriginRequest ().RemoteIP ().String ())
44+ c .ForwardRequest ().Header .SetBytesKV (headerName , buf .Bytes ())
45+ }
46+
2947 return f .BaseFilter .Pre (c )
3048}
You can’t perform that action at this time.
0 commit comments