[to #1] support Nextcloud IOS
This commit is contained in:
@@ -1,29 +1,96 @@
|
||||
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() {
|
||||
fs := &webdav.Handler{
|
||||
FileSystem: webdav.Dir("."),
|
||||
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)
|
||||
username, password, ok := r.BasicAuth()
|
||||
fmt.Println(username, password)
|
||||
if !ok {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
|
||||
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)
|
||||
}
|
||||
if username != "vscode" || password != "password" {
|
||||
//http.Error(w, "WebDAV: need authorized!", http.StatusUnauthorized)
|
||||
//return
|
||||
}
|
||||
fs.ServeHTTP(w, r)
|
||||
})
|
||||
fmt.Println("wait requests")
|
||||
http.ListenAndServe(":8080", nil)
|
||||
|
||||
Reference in New Issue
Block a user