Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support WebDav HTTP methods required #1459

Closed
prabhah opened this issue Dec 10, 2019 · 2 comments
Closed

Support WebDav HTTP methods required #1459

prabhah opened this issue Dec 10, 2019 · 2 comments
Labels

Comments

@prabhah
Copy link

prabhah commented Dec 10, 2019

Issue Description

ECHO framework does not support WebDav yet?
When I use golang.org/x/net/webdav with ECHO, many methods(eg. MKCOL) failed.
but with golang native net/http, it works.

RFC2518 for WebDav: https://tools.ietf.org/html/rfc2518

Working code using native net/http & golang.org/x/net/webdav

package main

import (
        "context"
        "fmt"
        "golang.org/x/net/webdav"
        "log"
        "net/http"
        "os"
        "strings"
)

func handleDirList(fs webdav.FileSystem, w http.ResponseWriter, req *http.Request) bool {
        path := "./" + strings.TrimPrefix(req.URL.Path, "/webdav/")
        ctx := context.Background()
        f, err := fs.OpenFile(ctx, path, os.O_RDONLY, 0)
        if err != nil {
                return false
        }
        defer f.Close()
        if fi, _ := f.Stat(); fi != nil && !fi.IsDir() {
                return false
        }
        dirs, err := f.Readdir(-1)
        if err != nil {
                log.Print(w, "Error reading directory", http.StatusInternalServerError)
                return false
        }
        w.Header().Set("Content-Type", "text/html; charset=utf-8")
        fmt.Fprintf(w, "<pre>\n")
        for _, d := range dirs {
                name := d.Name()
                if d.IsDir() {
                        name += "/"
                }
                fmt.Fprintf(w, "<a href=\"%s\" >%s</a>\n", name, name)
        }
        fmt.Fprintf(w, "</pre>\n")
        return true
}

func logger(req *http.Request, err error) {
        if err != nil {
                fmt.Printf("err: %v", err)
        }
}

func main() {
        rootPath := "webdav"
        fs := &webdav.Handler{
                FileSystem: webdav.Dir("."),
                LockSystem: webdav.NewMemLS(),
                Prefix:     "/" + rootPath,
                Logger:     logger,
        }
        http.HandleFunc("/"+rootPath+"/", func(w http.ResponseWriter, req *http.Request) {
                if req.Method == "GET" && handleDirList(fs.FileSystem, w, req) {
                        return
                        return
                }
                fs.ServeHTTP(w, req)
        })
        http.ListenAndServe(":8080", nil)
}
@stale
Copy link

stale bot commented Feb 8, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@BapiGso
Copy link

BapiGso commented Mar 27, 2024

Maybe you can use the following code to temporarily solve this problem

    e := echo.New()
    methods := []string{"GET", "HEAD", "POST", "OPTIONS", "PUT", "MKCOL", "DELETE", "PROPFIND", "PROPPATCH", "COPY", "MOVE", "REPORT", "LOCK", "UNLOCK"}
    e.Match(methods, "/webdav/*", echo.WrapHandler(webdavHandler))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants