package main import ( "io" "fmt" "net/http" "strings" "noahcloud/webdav" "noahcloud/handlers/nextcloud" //"noahcloud/handlers/proxy" "net/http/httptest" ) func copyHeader(dst, src http.Header) { for k, vv := range src { for _, v := range vv { dst.Add(k, v) } } } func main() { nc_normal_handler := &nextcloud.Handler{} //proxy_handler := &proxy.Handler{} nc_webdav_handler := &webdav.Handler{ Prefix: "/remote.php/webdav", FileSystem: webdav.Dir("/data/ngo/files/"), } webdav_handler := &webdav.Handler{ Prefix: "/webdav", FileSystem: webdav.Dir("/data/ngo/files/"), } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Println(r.Method, r.URL.Path) writer := httptest.NewRecorder() //proxy_handler.ServeHTTP(writer, r) //fmt.Printf("%d - %s", writer.Code, writer.Body.String()) //copyHeader(w.Header(), writer.Header) //w.WriteHeader(writer.StatusCode) //io.Copy(w, writer.Body) //return if strings.HasPrefix(r.URL.Path, "/remote.php/webdav") { username, password, ok := r.BasicAuth() fmt.Println(username, password) if !ok { w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) w.WriteHeader(http.StatusUnauthorized) return } if username != "vscode" || password != "password" { //http.Error(w, "WebDAV: need authorized!", http.StatusUnauthorized) //return } nc_webdav_handler.ServeHTTP(writer, r) copyHeader(w.Header(), writer.Header()) w.WriteHeader(writer.Code) for k, vv := range writer.Header() { for _, v := range vv { fmt.Println(k, v) } } fmt.Printf("%s\n", writer.Body.String()) io.Copy(w, writer.Body) } else if strings.HasPrefix(r.URL.Path, "/webdav") { username, password, ok := r.BasicAuth() fmt.Println(username, password) if !ok { w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) w.WriteHeader(http.StatusUnauthorized) return } if username != "vscode" || password != "password" { http.Error(w, "WebDAV: need authorized!", http.StatusUnauthorized) return } webdav_handler.ServeHTTP(w, r) } else if r.URL.Path == "/status.php" { nc_normal_handler.ServeHTTP(w, r) } else if r.URL.Path == "/ocs/v2.php/cloud/user" { nc_normal_handler.ServeHTTP(w, r) } else if r.URL.Path == "/index.php/login/v2" { // POST nc_normal_handler.ServeHTTP(w, r) } else if r.URL.Path == "/index.php/login/flow" { nc_normal_handler.ServeHTTP(w, r) } else { http.Error(w, "Not Found", http.StatusNotFound) } }) fmt.Println("wait requests") http.ListenAndServe(":8080", nil) }