Skip to content

Commit a4ab482

Browse files
authored
Add Go 1.16 to CI and drop 1.12 specific code (#1850)
* Correct incorrect years in CHANGELOG.md * CI tests with last 4 versions. Remove 1.12 and below specific code * Rename proxy test
1 parent bb7f222 commit a4ab482

16 files changed

+156
-243
lines changed

.github/workflows/echo.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ jobs:
2525
strategy:
2626
matrix:
2727
os: [ubuntu-latest, macos-latest, windows-latest]
28-
go: [1.12, 1.13, 1.14, 1.15]
28+
# Each major Go release is supported until there are two newer major releases. https://golang.org/doc/devel/release.html#policy
29+
# Echo tests with last four major releases
30+
go: [1.13, 1.14, 1.15, 1.16]
2931
name: ${{ matrix.os }} @ Go ${{ matrix.go }}
3032
runs-on: ${{ matrix.os }}
3133
steps:
@@ -59,7 +61,7 @@ jobs:
5961
go test -race --coverprofile=coverage.coverprofile --covermode=atomic ./...
6062
6163
- name: Upload coverage to Codecov
62-
if: success() && matrix.go == 1.15 && matrix.os == 'ubuntu-latest'
64+
if: success() && matrix.go == 1.16 && matrix.os == 'ubuntu-latest'
6365
uses: codecov/codecov-action@v1
6466
with:
6567
token:
@@ -69,7 +71,7 @@ jobs:
6971
strategy:
7072
matrix:
7173
os: [ubuntu-latest]
72-
go: [1.15]
74+
go: [1.16]
7375
name: Benchmark comparison ${{ matrix.os }} @ Go ${{ matrix.go }}
7476
runs-on: ${{ matrix.os }}
7577
steps:

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## v4.2.2 - 2020-04-07
3+
## v4.2.2 - 2021-04-07
44

55
**Fixes**
66

@@ -10,7 +10,7 @@
1010
* Fix panic in redirect middleware on short host name (#1813)
1111
* Fix timeout middleware docs (#1836)
1212

13-
## v4.2.1 - 2020-03-08
13+
## v4.2.1 - 2021-03-08
1414

1515
**Important notes**
1616

@@ -32,7 +32,7 @@ A performance regression has been fixed, even bringing better performance than b
3232
This release was made possible by our **contributors**:
3333
aldas, clwluvw, lammel, Le0tk0k, maciej-jezierski, rkilingr, stffabi, withshubh
3434

35-
## v4.2.0 - 2020-02-11
35+
## v4.2.0 - 2021-02-11
3636

3737
**Important notes**
3838

echo_go1.13_test.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

echo_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,23 @@ func TestHTTPError(t *testing.T) {
957957
})
958958
}
959959

960+
func TestHTTPError_Unwrap(t *testing.T) {
961+
t.Run("non-internal", func(t *testing.T) {
962+
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
963+
"code": 12,
964+
})
965+
966+
assert.Nil(t, errors.Unwrap(err))
967+
})
968+
t.Run("internal", func(t *testing.T) {
969+
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
970+
"code": 12,
971+
})
972+
err.SetInternal(errors.New("internal error"))
973+
assert.Equal(t, "internal error", errors.Unwrap(err).Error())
974+
})
975+
}
976+
960977
func TestDefaultHTTPErrorHandler(t *testing.T) {
961978
e := New()
962979
e.Debug = true

middleware/csrf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func CSRFWithConfig(config CSRFConfig) echo.MiddlewareFunc {
110110
if config.CookieMaxAge == 0 {
111111
config.CookieMaxAge = DefaultCSRFConfig.CookieMaxAge
112112
}
113-
if config.CookieSameSite == SameSiteNoneMode {
113+
if config.CookieSameSite == http.SameSiteNoneMode {
114114
config.CookieSecure = true
115115
}
116116

middleware/csrf_samesite.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

middleware/csrf_samesite_1.12.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

middleware/csrf_samesite_test.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

middleware/csrf_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,23 @@ func TestCSRFWithSameSiteDefaultMode(t *testing.T) {
138138
fmt.Println(rec.Header()["Set-Cookie"])
139139
assert.NotRegexp(t, "SameSite=", rec.Header()["Set-Cookie"])
140140
}
141+
142+
func TestCSRFWithSameSiteModeNone(t *testing.T) {
143+
e := echo.New()
144+
req := httptest.NewRequest(http.MethodGet, "/", nil)
145+
rec := httptest.NewRecorder()
146+
c := e.NewContext(req, rec)
147+
148+
csrf := CSRFWithConfig(CSRFConfig{
149+
CookieSameSite: http.SameSiteNoneMode,
150+
})
151+
152+
h := csrf(func(c echo.Context) error {
153+
return c.String(http.StatusOK, "test")
154+
})
155+
156+
r := h(c)
157+
assert.NoError(t, r)
158+
assert.Regexp(t, "SameSite=None", rec.Header()["Set-Cookie"])
159+
assert.Regexp(t, "Secure", rec.Header()["Set-Cookie"])
160+
}

middleware/proxy.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package middleware
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"math/rand"
78
"net"
89
"net/http"
10+
"net/http/httputil"
911
"net/url"
1012
"regexp"
13+
"strings"
1114
"sync"
1215
"sync/atomic"
1316
"time"
@@ -264,3 +267,37 @@ func ProxyWithConfig(config ProxyConfig) echo.MiddlewareFunc {
264267
}
265268
}
266269
}
270+
271+
// StatusCodeContextCanceled is a custom HTTP status code for situations
272+
// where a client unexpectedly closed the connection to the server.
273+
// As there is no standard error code for "client closed connection", but
274+
// various well-known HTTP clients and server implement this HTTP code we use
275+
// 499 too instead of the more problematic 5xx, which does not allow to detect this situation
276+
const StatusCodeContextCanceled = 499
277+
278+
func proxyHTTP(tgt *ProxyTarget, c echo.Context, config ProxyConfig) http.Handler {
279+
proxy := httputil.NewSingleHostReverseProxy(tgt.URL)
280+
proxy.ErrorHandler = func(resp http.ResponseWriter, req *http.Request, err error) {
281+
desc := tgt.URL.String()
282+
if tgt.Name != "" {
283+
desc = fmt.Sprintf("%s(%s)", tgt.Name, tgt.URL.String())
284+
}
285+
// If the client canceled the request (usually by closing the connection), we can report a
286+
// client error (4xx) instead of a server error (5xx) to correctly identify the situation.
287+
// The Go standard library (at of late 2020) wraps the exported, standard
288+
// context.Canceled error with unexported garbage value requiring a substring check, see
289+
// https://github.com/golang/go/blob/6965b01ea248cabb70c3749fd218b36089a21efb/src/net/net.go#L416-L430
290+
if err == context.Canceled || strings.Contains(err.Error(), "operation was canceled") {
291+
httpError := echo.NewHTTPError(StatusCodeContextCanceled, fmt.Sprintf("client closed connection: %v", err))
292+
httpError.Internal = err
293+
c.Set("_error", httpError)
294+
} else {
295+
httpError := echo.NewHTTPError(http.StatusBadGateway, fmt.Sprintf("remote %s unreachable, could not forward: %v", desc, err))
296+
httpError.Internal = err
297+
c.Set("_error", httpError)
298+
}
299+
}
300+
proxy.Transport = config.Transport
301+
proxy.ModifyResponse = config.ModifyResponse
302+
return proxy
303+
}

middleware/proxy_1_11.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

middleware/proxy_1_11_n.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)