Skip to content

Commit

Permalink
Fix Echo.Serve() will not serve on HTTP port correctly when there is …
Browse files Browse the repository at this point in the history
…already TLSListener set to Echo instance. (#1785) (#1793)
  • Loading branch information
aldas authored Feb 28, 2021
1 parent d9e2354 commit c79ffed
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 55 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ benchmark: ## Run benchmarks
help: ## Display this help screen
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

goversion ?= "1.12"
test_version: ## Run tests inside Docker with given version (defaults to 1.12 oldest supported). Example: make test_version goversion=1.13
@docker run --rm -it -v $(shell pwd):/project golang:$(goversion) /bin/sh -c "cd /project && make check"
goversion ?= "1.15"
test_version: ## Run tests inside Docker with given version (defaults to 1.15 oldest supported). Example: make test_version goversion=1.15
@docker run --rm -it -v $(shell pwd):/project golang:$(goversion) /bin/sh -c "cd /project && make init check"
13 changes: 13 additions & 0 deletions _fixture/certs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
To generate a valid certificate and private key use the following command:

```bash
# In OpenSSL ≥ 1.1.1
openssl req -x509 -newkey rsa:4096 -sha256 -days 9999 -nodes \
-keyout key.pem -out cert.pem -subj "/CN=localhost" \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1,IP:::1"
```

To check a certificate use the following command:
```bash
openssl x509 -in cert.pem -text
```
44 changes: 28 additions & 16 deletions _fixture/certs/cert.pem
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
-----BEGIN CERTIFICATE-----
MIIC+TCCAeGgAwIBAgIQe/dw9alKTWAPhsHoLdkn+TANBgkqhkiG9w0BAQsFADAS
MRAwDgYDVQQKEwdBY21lIENvMB4XDTE2MDkyNTAwNDcxN1oXDTE3MDkyNTAwNDcx
N1owEjEQ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/OyI/tsuboOW50Pka9vQHV+Z0aM06Yu+HNDAq/UTpEOb/3MQvZd6Ooy
PTpZtFb/+5jIQa1dIsfFWmpBxF0+wUd9GEkX3j7nekwoZfJ8Ze4GWYERZbOFpDAQ
rIHdthH5VJztnpQJmaKqzgIOF+Rurwlp5ecSC33xNNjDaYtuf/fiWnoKGhHVSBhT
61+0yxn3rTgh/Dsm95xY00rSX6lmcvI+kRNTUc8GGPz0ajBH6xyY7bNhfMjmnSW/
C/XTEDbTAhT7ndWC5vvzp7ZU0TvN+WY6A0f2kxSnnrEk6QRUvRtKkjAkmAFz8exi
ttBBW0I3E5HNIC5CYRimq/9z+3clM/P1KbNblwuC65bL+PZ+nzFnn5hFaK9eLPol
OwZQXv7IvAw8GfgLTrEUT7eBCQwe1IqesA7NTxF1BVwmNUb2XamvQZ7ly67QybRw
0uJq80XjpVjBWYTTQy1dsnC2OTKdqGsV9TVIDR+UGfIG9cxL70pEbiSH2AX+IDCy
i3kNIvpXgBliAyOjW6Hj1fv6dNfAat/hqEfnquWkfvcs3HNrG/InwpwNAUs=
-----END CERTIFICATE-----
79 changes: 52 additions & 27 deletions _fixture/certs/key.pem
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAvxbCEsZsrweSIQMpXQluO1anf8RYqEoVbdi0q09886Chl6N0
2f1UTQEiVDivBrXCawz00MoLSbrSiI05h7K6DH0+gUjdFfO4pVfbHsTktNbSy/Qs
L08KCBkxlOKHUCJi3AkhmX5orqzh1Nv89Q4sN1xFNlMXZ6CR0CJxtSVBPgqf4DeM
eIT4BMKHcyVXYEdquECFZvQWs6DZsS0WszIUNotscjvFuP68H0KQYH0vm05s/Yz2
VALEfx0SVaQmPGDu92SEnn6xY1pjzS602KlR1U7zjm0gRHBFdFG+IVik5y41+clg
L2TBGO8JFqeHd6laEbRwVBVXl3jmwd/q3kX6OwIDAQABAoIBAQCR69EcAUZxinh+
mSl3EIK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GR5I/jEdP0lJVh0xapoZNUlT5dWYCgo20hY
ZAmKpU+veyUn+5Li0pmm959vnLK5LJzEA5mpz3w1QPPtVwQs05dwmEV3CRAcCeeh
8sXp49WNCSW4I3BxuTZzRV845SGIFhZwgVV42PTp2LPKl2p6E7Bk8xpUCCvBpALp
QmA5yIMx+u2Jpr7fUsXEXEPTEhvjff0=
-----END PRIVATE KEY-----
15 changes: 6 additions & 9 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ func (e *Echo) Start(address string) error {
return err
}
e.startupMutex.Unlock()
return e.serve()
return e.Server.Serve(e.Listener)
}

// StartTLS starts an HTTPS server.
Expand Down Expand Up @@ -740,8 +740,12 @@ func (e *Echo) StartServer(s *http.Server) (err error) {
e.startupMutex.Unlock()
return err
}
if s.TLSConfig != nil {
e.startupMutex.Unlock()
return s.Serve(e.TLSListener)
}
e.startupMutex.Unlock()
return e.serve()
return s.Serve(e.Listener)
}

func (e *Echo) configureServer(s *http.Server) (err error) {
Expand Down Expand Up @@ -782,13 +786,6 @@ func (e *Echo) configureServer(s *http.Server) (err error) {
return nil
}

func (e *Echo) serve() error {
if e.TLSListener != nil {
return e.Server.Serve(e.TLSListener)
}
return e.Server.Serve(e.Listener)
}

// ListenerAddr returns net.Addr for Listener
func (e *Echo) ListenerAddr() net.Addr {
e.startupMutex.RLock()
Expand Down
54 changes: 54 additions & 0 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,60 @@ func TestEcho_StartTLS(t *testing.T) {
}
}

func TestEchoStartTLSAndStart(t *testing.T) {
// We test if Echo and listeners work correctly when Echo is simultaneously attached to HTTP and HTTPS server
e := New()
e.GET("/", func(c Context) error {
return c.String(http.StatusOK, "OK")
})

errTLSChan := make(chan error)
go func() {
certFile := "_fixture/certs/cert.pem"
keyFile := "_fixture/certs/key.pem"
err := e.StartTLS("localhost:", certFile, keyFile)
if err != nil {
errTLSChan <- err
}
}()

err := waitForServerStart(e, errTLSChan, true)
assert.NoError(t, err)
defer func() {
if err := e.Shutdown(stdContext.Background()); err != nil {
t.Error(err)
}
}()

// check if HTTPS works (note: we are using self signed certs so InsecureSkipVerify=true)
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
res, err := client.Get("https://" + e.TLSListenerAddr().String())
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)

errChan := make(chan error)
go func() {
err := e.Start("localhost:")
if err != nil {
errChan <- err
}
}()
err = waitForServerStart(e, errChan, false)
assert.NoError(t, err)

// now we are serving both HTTPS and HTTP listeners. see if HTTP works in addition to HTTPS
res, err = http.Get("http://" + e.ListenerAddr().String())
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)

// see if HTTPS works after HTTP listener is also added
res, err = client.Get("https://" + e.TLSListenerAddr().String())
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
}

func TestEchoStartTLSByteString(t *testing.T) {
cert, err := ioutil.ReadFile("_fixture/certs/cert.pem")
require.NoError(t, err)
Expand Down

0 comments on commit c79ffed

Please sign in to comment.