Skip to content

Commit cb15226

Browse files
committed
Merge branch 'master' of https://github.com/labstack/echo
2 parents 8b2c77b + a908413 commit cb15226

36 files changed

+1309
-151
lines changed

.github/workflows/echo.yml

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@ on:
44
push:
55
branches:
66
- master
7+
paths:
8+
- '**.go'
9+
- 'go.*'
10+
- '_fixture/**'
11+
- '.github/**'
12+
- 'codecov.yml'
713
pull_request:
814
branches:
915
- master
10-
11-
env:
12-
GO111MODULE: on
13-
GOPROXY: https://proxy.golang.org
16+
paths:
17+
- '**.go'
18+
- 'go.*'
19+
- '_fixture/**'
20+
- '.github/**'
21+
- 'codecov.yml'
1422

1523
jobs:
1624
test:
1725
strategy:
1826
matrix:
1927
os: [ubuntu-latest, macos-latest, windows-latest]
20-
go: [1.11, 1.12, 1.13]
28+
go: [1.12, 1.13, 1.14, 1.15]
2129
name: ${{ matrix.os }} @ Go ${{ matrix.go }}
2230
runs-on: ${{ matrix.os }}
2331
steps:
@@ -28,10 +36,15 @@ jobs:
2836

2937
- name: Set GOPATH and PATH
3038
run: |
31-
echo "::set-env name=GOPATH::$(dirname $GITHUB_WORKSPACE)"
32-
echo "::add-path::$(dirname $GITHUB_WORKSPACE)/bin"
39+
echo "GOPATH=$(dirname $GITHUB_WORKSPACE)" >> $GITHUB_ENV
40+
echo "$(dirname $GITHUB_WORKSPACE)/bin" >> $GITHUB_PATH
3341
shell: bash
3442

43+
- name: Set build variables
44+
run: |
45+
echo "GOPROXY=https://proxy.golang.org" >> $GITHUB_ENV
46+
echo "GO111MODULE=on" >> $GITHUB_ENV
47+
3548
- name: Checkout Code
3649
uses: actions/checkout@v1
3750
with:
@@ -51,3 +64,55 @@ jobs:
5164
with:
5265
token:
5366
fail_ci_if_error: false
67+
benchmark:
68+
needs: test
69+
strategy:
70+
matrix:
71+
os: [ubuntu-latest]
72+
go: [1.15]
73+
name: Benchmark comparison ${{ matrix.os }} @ Go ${{ matrix.go }}
74+
runs-on: ${{ matrix.os }}
75+
steps:
76+
- name: Set up Go ${{ matrix.go }}
77+
uses: actions/setup-go@v1
78+
with:
79+
go-version: ${{ matrix.go }}
80+
81+
- name: Set GOPATH and PATH
82+
run: |
83+
echo "GOPATH=$(dirname $GITHUB_WORKSPACE)" >> $GITHUB_ENV
84+
echo "$(dirname $GITHUB_WORKSPACE)/bin" >> $GITHUB_PATH
85+
shell: bash
86+
87+
- name: Set build variables
88+
run: |
89+
echo "GOPROXY=https://proxy.golang.org" >> $GITHUB_ENV
90+
echo "GO111MODULE=on" >> $GITHUB_ENV
91+
92+
- name: Checkout Code (Previous)
93+
uses: actions/checkout@v2
94+
with:
95+
ref: ${{ github.base_ref }}
96+
path: previous
97+
98+
- name: Checkout Code (New)
99+
uses: actions/checkout@v2
100+
with:
101+
path: new
102+
103+
- name: Install Dependencies
104+
run: go get -v golang.org/x/perf/cmd/benchstat
105+
106+
- name: Run Benchmark (Previous)
107+
run: |
108+
cd previous
109+
go test -run="-" -bench=".*" -count=8 ./... > benchmark.txt
110+
111+
- name: Run Benchmark (New)
112+
run: |
113+
cd new
114+
go test -run="-" -bench=".*" -count=8 ./... > benchmark.txt
115+
116+
- name: Run Benchstat
117+
run: |
118+
benchstat previous/benchmark.txt new/benchmark.txt

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
arch:
2+
- amd64
3+
- ppc64le
4+
15
language: go
26
go:
3-
- 1.12.x
4-
- 1.13.x
7+
- 1.14.x
8+
- 1.15.x
59
- tip
610
env:
711
- GO111MODULE=on

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<a href="https://echo.labstack.com"><img height="80" src="https://cdn.labstack.com/images/echo-logo.svg"></a>
22

33
[![Sourcegraph](https://sourcegraph.com/github.com/labstack/echo/-/badge.svg?style=flat-square)](https://sourcegraph.com/github.com/labstack/echo?badge)
4-
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/labstack/echo)
4+
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://pkg.go.dev/github.com/labstack/echo/v4)
55
[![Go Report Card](https://goreportcard.com/badge/github.com/labstack/echo?style=flat-square)](https://goreportcard.com/report/github.com/labstack/echo)
66
[![Build Status](http://img.shields.io/travis/labstack/echo.svg?style=flat-square)](https://travis-ci.org/labstack/echo)
77
[![Codecov](https://img.shields.io/codecov/c/github/labstack/echo.svg?style=flat-square)](https://codecov.io/gh/labstack/echo)
@@ -17,7 +17,7 @@ Therefore a Go version capable of understanding /vN suffixed imports is required
1717

1818
- 1.9.7+
1919
- 1.10.3+
20-
- 1.11+
20+
- 1.14+
2121

2222
Any of these versions will allow you to import Echo as `github.com/labstack/echo/v4` which is the recommended
2323
way of using Echo going forward.
@@ -52,7 +52,7 @@ Lower is better!
5252

5353
### Installation
5454

55-
```go
55+
```sh
5656
// go get github.com/labstack/echo/{version}
5757
go get github.com/labstack/echo/v4
5858
```

_fixture/_fixture/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This directory is used for the static middleware test

bind_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ func TestBindbindData(t *testing.T) {
332332

333333
func TestBindParam(t *testing.T) {
334334
e := New()
335-
*e.maxParam = 2
336335
req := httptest.NewRequest(GET, "/", nil)
337336
rec := httptest.NewRecorder()
338337
c := e.NewContext(req, rec)
@@ -363,7 +362,6 @@ func TestBindParam(t *testing.T) {
363362
// Bind something with param and post data payload
364363
body := bytes.NewBufferString(`{ "name": "Jon Snow" }`)
365364
e2 := New()
366-
*e2.maxParam = 2
367365
req2 := httptest.NewRequest(POST, "/", body)
368366
req2.Header.Set(HeaderContentType, MIMEApplicationJSON)
369367

codecov.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
threshold: 1%
6+
patch:
7+
default:
8+
threshold: 1%
9+
10+
comment:
11+
require_changes: true

context.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,11 @@ func (c *context) RealIP() string {
276276
}
277277
// Fall back to legacy behavior
278278
if ip := c.request.Header.Get(HeaderXForwardedFor); ip != "" {
279-
return strings.Split(ip, ", ")[0]
279+
i := strings.IndexAny(ip, ", ")
280+
if i > 0 {
281+
return ip[:i]
282+
}
283+
return ip
280284
}
281285
if ip := c.request.Header.Get(HeaderXRealIP); ip != "" {
282286
return ip
@@ -310,17 +314,15 @@ func (c *context) ParamNames() []string {
310314

311315
func (c *context) SetParamNames(names ...string) {
312316
c.pnames = names
317+
*c.echo.maxParam = len(names)
313318
}
314319

315320
func (c *context) ParamValues() []string {
316321
return c.pvalues[:len(c.pnames)]
317322
}
318323

319324
func (c *context) SetParamValues(values ...string) {
320-
// NOTE: Don't just set c.pvalues = values, because it has to have length c.echo.maxParam at all times
321-
for i, val := range values {
322-
c.pvalues[i] = val
323-
}
325+
c.pvalues = values
324326
}
325327

326328
func (c *context) QueryParam(name string) string {
@@ -363,7 +365,7 @@ func (c *context) FormFile(name string) (*multipart.FileHeader, error) {
363365
if err != nil {
364366
return nil, err
365367
}
366-
defer f.Close()
368+
f.Close()
367369
return fh, nil
368370
}
369371

context_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ func BenchmarkAllocXML(b *testing.B) {
7272
}
7373
}
7474

75+
func BenchmarkRealIPForHeaderXForwardFor(b *testing.B) {
76+
c := context{request: &http.Request{
77+
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1, 127.0.1.1, "}},
78+
}}
79+
for i := 0; i < b.N; i++ {
80+
c.RealIP()
81+
}
82+
}
83+
7584
func (t *Template) Render(w io.Writer, name string, data interface{}, c Context) error {
7685
return t.templates.ExecuteTemplate(w, name, data)
7786
}
@@ -93,7 +102,6 @@ func (responseWriterErr) WriteHeader(statusCode int) {
93102

94103
func TestContext(t *testing.T) {
95104
e := New()
96-
*e.maxParam = 1
97105
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
98106
rec := httptest.NewRecorder()
99107
c := e.NewContext(req, rec).(*context)
@@ -472,7 +480,6 @@ func TestContextPath(t *testing.T) {
472480

473481
func TestContextPathParam(t *testing.T) {
474482
e := New()
475-
*e.maxParam = 2
476483
req := httptest.NewRequest(http.MethodGet, "/", nil)
477484
c := e.NewContext(req, nil)
478485

@@ -491,7 +498,8 @@ func TestContextPathParam(t *testing.T) {
491498

492499
func TestContextGetAndSetParam(t *testing.T) {
493500
e := New()
494-
*e.maxParam = 2
501+
r := e.Router()
502+
r.Add(http.MethodGet, "/:foo", func(Context) error { return nil })
495503
req := httptest.NewRequest(http.MethodGet, "/:foo", nil)
496504
c := e.NewContext(req, nil)
497505
c.SetParamNames("foo")
@@ -848,6 +856,14 @@ func TestContext_RealIP(t *testing.T) {
848856
},
849857
"127.0.0.1",
850858
},
859+
{
860+
&context{
861+
request: &http.Request{
862+
Header: http.Header{HeaderXForwardedFor: []string{"127.0.0.1"}},
863+
},
864+
},
865+
"127.0.0.1",
866+
},
851867
{
852868
&context{
853869
request: &http.Request{

0 commit comments

Comments
 (0)