Skip to content

Support WebDav HTTP methods required #1459

Closed
@prabhah

Description

@prabhah

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions