echo icon indicating copy to clipboard operation
echo copied to clipboard

Support for custom HTTP methods

Open mdouchement opened this issue 3 years ago • 0 comments

Issue Description

The Router object does not allow custom HTTP methods like COPY or anything else.

func (n *node) addHandler(method string, h HandlerFunc) and func (n *node) findHandler(method string) HandlerFunc have static switch cases.

I know this is not a common use case.

Checklist

  • [x] Dependencies installed
  • [x] No typos
  • [x] Searched existing issues and docs

Expected behaviour

Calls the handler defined by:

e.Add("COPY", "/*", func(c echo.Context) error {
	return c.NoContent(http.StatusCreated)
})

Actual behaviour

It returns echo.NotFoundHandler or echo.MethodNotAllowedHandler.

Steps to reproduce

Working code to debug

package main

import (
	"fmt"
	"net/http"
	"net/http/httputil"
	"time"

	"github.com/labstack/echo/v4"
)

func main() {
	e := echo.New()
	e.Add("COPY", "/*", func(c echo.Context) error {
		return c.NoContent(http.StatusCreated)
	})

	fmt.Println("Routes:")
	for _, route := range e.Routes() {
		fmt.Printf("%6s %s\n", route.Method, route.Path)
	}

	go func() {
		err := e.Start("localhost:5000")
		if err != nil {
			panic(err)
		}
	}()
	time.Sleep(time.Second)
	fmt.Println()

	//
	//
	//

	req, err := http.NewRequest("COPY", "http://localhost:5000/somepath", nil)
	if err != nil {
		panic(err)
	}

	payload, err := httputil.DumpRequest(req, false)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(payload))

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}

	payload, err = httputil.DumpResponse(resp, true)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(payload))
}

Version/commit

4.5.0

mdouchement avatar Aug 10 '21 13:08 mdouchement