Skip to content

Commit

Permalink
Merge pull request #23 from Jipok/master
Browse files Browse the repository at this point in the history
Supports root `/` dav
  • Loading branch information
117503445 authored Feb 17, 2024
2 parents 4ed1583 + 24bca06 commit 1c2ce80
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
65 changes: 27 additions & 38 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package main
import (
_ "embed"
"fmt"
"golang.org/x/net/context"
"golang.org/x/net/webdav"
"net/http"
"net/url"
"os"
"strings"

"golang.org/x/net/context"
"golang.org/x/net/webdav"

"GoWebDAV/model"

"github.com/spf13/viper"
Expand All @@ -30,19 +30,24 @@ func main() {
WebDAVConfigs := make([]*model.WebDAVConfig, 0)

for _, davConfig := range davConfigs {
WebDAVConfig := &model.WebDAVConfig{}
WebDAVConfig.InitByConfigStr(davConfig)
if len(davConfig) == 0 {
continue
}
WebDAVConfig := model.InitByConfigStr(davConfig)
// Check for collision
found, _ := model.ParseURL(WebDAVConfigs, WebDAVConfig.Prefix)
if found != nil {
fmt.Printf("Dav names collision: `%s` starts with `%s`", WebDAVConfig.Prefix, found.Prefix)
os.Exit(1)
}

WebDAVConfigs = append(WebDAVConfigs, WebDAVConfig)
WebDAVConfigs = append(WebDAVConfigs, &WebDAVConfig)
}

w := &model.WebDAVConfig{}
WebDAVConfigs = append(WebDAVConfigs, w)

sMux := http.NewServeMux()
sMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {

webDAVConfig := model.WebDAVConfigFindOneByPrefix(WebDAVConfigs, parsePrefixFromURL(req.URL))
webDAVConfig, davPath := model.ParseURL(WebDAVConfigs, req.URL.Path)

if webDAVConfig == nil {

Expand Down Expand Up @@ -71,7 +76,7 @@ func main() {
}

// When the username and password in the configuration are both null, no identity check is performed
if webDAVConfig.Username != "null" && webDAVConfig.Password != "null" {
if webDAVConfig.Username != "null" && webDAVConfig.Password != "null" {
username, password, ok := req.BasicAuth()

if !ok {
Expand All @@ -92,9 +97,13 @@ func main() {
}

if webDAVConfig.ReadOnly {
allowMethods := []string{"GET", "OPTIONS", "PROPFIND", "HEAD"}
if !IsContain(allowMethods, req.Method) {
// ReadOnly
allowedMethods := map[string]bool{
"GET": true,
"OPTIONS": true,
"PROPFIND": true,
"HEAD": true,
}
if !allowedMethods[req.Method] {
w.WriteHeader(http.StatusMethodNotAllowed)
_, err := w.Write([]byte("Readonly, Method " + req.Method + " Not Allowed"))
if err != nil {
Expand All @@ -105,7 +114,7 @@ func main() {
}
}

if req.Method == "GET" && isDir(webDAVConfig.Handler.FileSystem, req) {
if req.Method == "GET" && isDir(webDAVConfig.Handler.FileSystem, davPath) {
_, err := w.Write([]byte(indexHTML))
if err != nil {
fmt.Println(err)
Expand All @@ -131,22 +140,10 @@ func main() {
}
}

// /dav1/123.txt -> dav1
func parsePrefixFromURL(url *url.URL) string {
u := fmt.Sprint(url)
return "/" + strings.Split(u, "/")[1]
}

func isDir(fs webdav.FileSystem, req *http.Request) bool {
func isDir(fs webdav.FileSystem, davPath string) bool {
ctx := context.Background()
path := req.URL.Path
//fmt.Println(path)
s := strings.Split(path, "/")[2:]
//fmt.Println(s)
path = strings.Join(s, "/")
//fmt.Println(path)

f, err := fs.OpenFile(ctx, path, os.O_RDONLY, 0)

f, err := fs.OpenFile(ctx, davPath, os.O_RDONLY, 0)
if err != nil {
return false
}
Expand All @@ -158,11 +155,3 @@ func isDir(fs webdav.FileSystem, req *http.Request) bool {
return true
}

func IsContain(items []string, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
24 changes: 18 additions & 6 deletions model/webdav_config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package model

import (
"golang.org/x/net/webdav"
"os"
"strconv"
"strings"

"golang.org/x/net/webdav"
)

type WebDAVConfig struct {
Expand All @@ -28,25 +30,35 @@ func (WebDAVConfig *WebDAVConfig) Init(prefix string, pathDir string, username s
Prefix: prefix,
}
}
func (WebDAVConfig *WebDAVConfig) InitByConfigStr(str string) {

func InitByConfigStr(str string) WebDAVConfig {
davConfigArray := strings.Split(str, ",")
prefix := davConfigArray[0]
pathDir := davConfigArray[1]
username := davConfigArray[2]
password := davConfigArray[3]

if _, err := os.Stat(pathDir); os.IsNotExist(err) {
println("Dir not exists: ", pathDir)
os.Exit(1)
}

readonly, err := strconv.ParseBool(davConfigArray[4])
if err != nil {
readonly = false
}

var WebDAVConfig WebDAVConfig
WebDAVConfig.Init(prefix, pathDir, username, password, readonly)
return WebDAVConfig
}
func WebDAVConfigFindOneByPrefix(WebDAVConfigs []*WebDAVConfig, prefix string) *WebDAVConfig {

func ParseURL(WebDAVConfigs []*WebDAVConfig, url string) (*WebDAVConfig, string) {
for _, WebDAVConfig := range WebDAVConfigs {
if WebDAVConfig.Prefix == prefix {
return WebDAVConfig
davPath, found := strings.CutPrefix(url, WebDAVConfig.Prefix)
if found {
return WebDAVConfig, davPath
}
}
return nil
return nil, ""
}

0 comments on commit 1c2ce80

Please sign in to comment.