Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ package echo

import (
"bytes"
stdContext "context"
"crypto/tls"
"errors"
"fmt"
"io"
stdLog "log"
"net"
"net/http"
"net/url"
"path"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -445,7 +447,7 @@ func (e *Echo) Static(prefix, root string) *Route {

func static(i i, prefix, root string) *Route {
h := func(c Context) error {
p, err := PathUnescape(c.Param("*"))
p, err := url.PathUnescape(c.Param("*"))
if err != nil {
return err
}
Expand Down Expand Up @@ -678,6 +680,24 @@ func (e *Echo) StartServer(s *http.Server) (err error) {
return s.Serve(e.TLSListener)
}

// Close immediately stops the server.
// It internally calls `http.Server#Close()`.
func (e *Echo) Close() error {
if err := e.TLSServer.Close(); err != nil {
return err
}
return e.Server.Close()
}

// Shutdown stops server the gracefully.
// It internally calls `http.Server#Shutdown()`.
func (e *Echo) Shutdown(ctx stdContext.Context) error {
if err := e.TLSServer.Shutdown(ctx); err != nil {
return err
}
return e.Server.Shutdown(ctx)
}

// NewHTTPError creates a new HTTPError instance.
func NewHTTPError(code int, message ...interface{}) *HTTPError {
he := &HTTPError{Code: code, Message: http.StatusText(code)}
Expand Down Expand Up @@ -729,6 +749,11 @@ func handlerName(h HandlerFunc) string {
return t.String()
}

// // PathUnescape is wraps `url.PathUnescape`
// func PathUnescape(s string) (string, error) {
// return url.PathUnescape(s)
// }

// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
Expand Down
25 changes: 0 additions & 25 deletions echo_go1.8.go

This file was deleted.

30 changes: 0 additions & 30 deletions echo_go1.8_test.go

This file was deleted.

43 changes: 43 additions & 0 deletions echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package echo

import (
"bytes"
stdContext "context"
"errors"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -461,3 +462,45 @@ func TestHTTPError(t *testing.T) {
})
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
}

func TestEchoClose(t *testing.T) {
e := New()
errCh := make(chan error)

go func() {
errCh <- e.Start(":0")
}()

time.Sleep(200 * time.Millisecond)

if err := e.Close(); err != nil {
t.Fatal(err)
}

assert.NoError(t, e.Close())

err := <-errCh
assert.Equal(t, err.Error(), "http: Server closed")
}

func TestEchoShutdown(t *testing.T) {
e := New()
errCh := make(chan error)

go func() {
errCh <- e.Start(":0")
}()

time.Sleep(200 * time.Millisecond)

if err := e.Close(); err != nil {
t.Fatal(err)
}

ctx, cancel := stdContext.WithTimeout(stdContext.Background(), 10*time.Second)
defer cancel()
assert.NoError(t, e.Shutdown(ctx))

err := <-errCh
assert.Equal(t, err.Error(), "http: Server closed")
}
3 changes: 2 additions & 1 deletion middleware/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"fmt"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -76,7 +77,7 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
if strings.HasSuffix(c.Path(), "*") { // When serving from a group, e.g. `/static*`.
p = c.Param("*")
}
p, err = echo.PathUnescape(p)
p, err = url.PathUnescape(p)
if err != nil {
return
}
Expand Down
12 changes: 0 additions & 12 deletions util_go17.go

This file was deleted.

10 changes: 0 additions & 10 deletions util_go18.go

This file was deleted.